2#include <nlohmann/json.hpp>
3using json = nlohmann::json;
8 if (Get().m_AliasedPaths.contains(alias)) {
9 return Get().m_AliasedPaths[alias];
15 std::println(stderr,
"[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
22 if (Get().m_AliasedPaths.contains(alias)) {
23 return Get().m_AliasedPaths.find(alias)->second;
29 std::println(stderr,
"[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
36 if (Get().m_AliasedPaths.contains(alias)) {
37 return Get().m_AliasedPaths.find(alias)->second;
43 std::println(stderr,
"[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
49 PathManager::PathManager() {
50 m_AliasedPaths.insert({
"BIN",
""});
52 std::filesystem::path pathFile =
"../fsgame.json";
55 std::ifstream f(pathFile);
59 std::println(
"Loading global path definitions from: {}", pathFile.string());
62 throw Core::CoriError(std::format(
"Failed to open json file {}", pathFile.string()));
65 json data = json::parse(f);
67 const json& paths = data[
"paths"];
69 for (
const auto& path : paths) {
70 if (path.contains(
"alias") && path.contains(
"path") && path.contains(
"root")) {
71 std::string root = path[
"root"];
74 CORI_CORE_ASSERT(m_AliasedPaths.contains(root),
"Path for alias '{}' was not defined before it was used.", root);
76 if (!m_AliasedPaths.contains(root)) {
77 const std::source_location& loc = std::source_location::current();
79 "[FATAL] {}:{}:{} in {}(): Path for alias '{}' was not defined before it was used.",
89 std::string alias = path[
"alias"];
90 std::filesystem::path rootPath = m_AliasedPaths[root];
91 std::filesystem::path aliasPath = path[
"path"];
92 std::filesystem::path fullPath = rootPath / aliasPath;
93 m_AliasedPaths.insert({alias, fullPath});
98 std::println(stderr,
"[ERROR] Failed to load an entry from \"paths\" array, entry doesn't contain on of following fields: \"alias\", \"path\", \"root\"");
103 catch (std::exception& e) {
107 std::println(stderr,
"[FATAL] Encountered an error when trying to parse global path config '{}', this can blow any time now. \nError: {}", pathFile.string(), e.what());
113 void PathManager::Init(
const std::filesystem::path& pathFile) {
117 s_Data->m_AliasedPaths.insert({
"BIN",
""});
120 std::ifstream f(pathFile);
123 throw Core::CoriError(std::format(
"Failed to open json file {}", pathFile.string()));
126 json data = json::parse(f);
128 const json& paths = data[
"paths"];
130 for (
const auto& path : paths) {
131 if (path.contains(
"alias") && path.contains(
"path") && path.contains(
"root")) {
132 std::string root = path[
"root"];
134 CORI_CORE_ASSERT(s_Data->m_AliasedPaths.contains(root),
"Path for alias '{}' was not defined before it was used.", root);
136 std::string alias = path[
"alias"];
137 std::filesystem::path rootPath = s_Data->m_AliasedPaths[root];
138 std::filesystem::path aliasPath = path[
"path"];
139 std::filesystem::path fullPath = rootPath / aliasPath;
140 s_Data->m_AliasedPaths.insert({alias, fullPath});
146 catch (std::exception& e) {
#define CORI_CORE_FATAL_TAGGED(...)
#define CORI_CORE_ASSERT(x,...)
#define CORI_CORE_ERROR_TAGGED(...)
#define CORI_CORE_INFO_TAGGED(...)
static std::filesystem::path GetAliasedPath(const std::string &alias)
Retries the full aliased path defined in fsgame.json.