CoriEngine
Loading...
Searching...
No Matches
PathManager.cpp
Go to the documentation of this file.
1#include "PathManager.hpp"
2#include <nlohmann/json.hpp>
3using json = nlohmann::json;
4
5namespace Cori {
6 namespace FileSystem {
7 std::filesystem::path PathManager::GetAliasedPath(const std::string& alias) {
8 if (Get().m_AliasedPaths.contains(alias)) {
9 return Get().m_AliasedPaths[alias];
10 }
11
12 if (Logger::GetStatus()) {
13 CORI_CORE_ERROR_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Failed to retrieve path for alias '{}', returned empty path.", alias);
14 } else {
15 std::println(stderr, "[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
16 }
17
18 return {};
19 }
20
21 std::filesystem::path PathManager::GetAliasedPath(const std::string_view alias) {
22 if (Get().m_AliasedPaths.contains(alias)) {
23 return Get().m_AliasedPaths.find(alias)->second;
24 }
25
26 if (Logger::GetStatus()) {
27 CORI_CORE_ERROR_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Failed to retrieve path for alias '{}', returned empty path.", alias);
28 } else {
29 std::println(stderr, "[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
30 }
31
32 return {};
33 }
34
35 std::filesystem::path PathManager::GetAliasedPath(const char* alias) {
36 if (Get().m_AliasedPaths.contains(alias)) {
37 return Get().m_AliasedPaths.find(alias)->second;
38 }
39
40 if (Logger::GetStatus()) {
41 CORI_CORE_ERROR_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Failed to retrieve path for alias '{}', returned empty path.", alias);
42 } else {
43 std::println(stderr, "[ERROR] Failed to retrieve path for alias '{}', returned empty path.", alias);
44 }
45
46 return {};
47 }
48
49 PathManager::PathManager() {
50 m_AliasedPaths.insert({"BIN", ""});
51
52 std::filesystem::path pathFile = "../fsgame.json";
53
54 try {
55 std::ifstream f(pathFile);
56 if (Logger::GetStatus()) {
57 CORI_CORE_INFO_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Loading global path definitions from: {}", pathFile.string());
58 } else {
59 std::println("Loading global path definitions from: {}", pathFile.string());
60 }
61 if (!f.good()) {
62 throw Core::CoriError(std::format("Failed to open json file {}", pathFile.string()));
63 }
64
65 json data = json::parse(f);
66
67 const json& paths = data["paths"];
68
69 for (const auto& path : paths) {
70 if (path.contains("alias") && path.contains("path") && path.contains("root")) {
71 std::string root = path["root"];
72
73 if (Logger::GetStatus()) {
74 CORI_CORE_ASSERT(m_AliasedPaths.contains(root), "Path for alias '{}' was not defined before it was used.", root);
75 } else {
76 if (!m_AliasedPaths.contains(root)) {
77 const std::source_location& loc = std::source_location::current();
78 std::println(stderr,
79 "[FATAL] {}:{}:{} in {}(): Path for alias '{}' was not defined before it was used.",
80 loc.file_name(),
81 loc.line(),
82 loc.column(),
83 loc.function_name(),
84 root);
85 std::abort();
86 }
87 }
88
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});
94 } else {
95 if (Logger::GetStatus()) {
96 CORI_CORE_ERROR_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Failed to load an entry from \"paths\" array, entry doesn't contain on of following fields: \"alias\", \"path\", \"root\"");
97 } else {
98 std::println(stderr, "[ERROR] Failed to load an entry from \"paths\" array, entry doesn't contain on of following fields: \"alias\", \"path\", \"root\"");
99 }
100 }
101 }
102 }
103 catch (std::exception& e) {
104 if (Logger::GetStatus()) {
105 CORI_CORE_FATAL_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Encountered an error when trying to parse global path config '{}', this can blow any time now. \nError: {}", pathFile.string(), e.what());
106 } else {
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());
108 }
109 }
110 }
111
112 #if 0
113 void PathManager::Init(const std::filesystem::path& pathFile) {
114 if (!s_Data) {
115 s_Data = new Data();
116
117 s_Data->m_AliasedPaths.insert({"BIN", ""});
118
119 try {
120 std::ifstream f(pathFile);
121 CORI_CORE_INFO_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Loading global path definitions from: {}", pathFile.string());
122 if (!f.good()) {
123 throw Core::CoriError(std::format("Failed to open json file {}", pathFile.string()));
124 }
125
126 json data = json::parse(f);
127
128 const json& paths = data["paths"];
129
130 for (const auto& path : paths) {
131 if (path.contains("alias") && path.contains("path") && path.contains("root")) {
132 std::string root = path["root"];
133
134 CORI_CORE_ASSERT(s_Data->m_AliasedPaths.contains(root), "Path for alias '{}' was not defined before it was used.", root);
135
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});
141 } else {
142 CORI_CORE_ERROR_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Failed to load an entry from \"paths\" array, entry doesn't contain on of following fields: \"alias\", \"path\", \"root\"");
143 }
144 }
145 }
146 catch (std::exception& e) {
147 CORI_CORE_FATAL_TAGGED({ Logger::Tags::FileSystem::Self, Logger::Tags::FileSystem::PathManager }, "Encountered an error when trying to parse global path config '{}', this can blow any time now. Error: {}", pathFile.string(), e.what());
148 }
149 }
150 }
151 #endif
152
153 //void PathManager::Shutdown() {
154 // if (s_Data) {
155 // delete s_Data;
156 // }
157 //}
158 }
159}
#define CORI_CORE_FATAL_TAGGED(...)
Definition Logger.hpp:1040
#define CORI_CORE_ASSERT(x,...)
Definition Logger.hpp:1029
#define CORI_CORE_ERROR_TAGGED(...)
Definition Logger.hpp:1039
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
nlohmann::json json
static std::filesystem::path GetAliasedPath(const std::string &alias)
Retries the full aliased path defined in fsgame.json.
static bool GetStatus()
Definition Logger.cpp:95
Global engine namespace.
static constexpr char PathManager[]
Definition Logger.hpp:108
static constexpr char Self[]
Definition Logger.hpp:105