-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.h
286 lines (240 loc) · 8.6 KB
/
ResourceManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <fstream>
#include <future>
#include <thread>
#include <utility>
#include <filesystem>
#include "Compression.h"
#include <yaml-cpp/yaml.h>
#include "UIElements.h"
#include "Utils/Utils.h"
#include "Library/RyoCompression/utils.h"
inline static const std::string ResourcePackageExtension = ".pck";
inline static const std::filesystem::path ResourcePath("Resources");
inline static const std::filesystem::path PackedPath("build");
inline static const char* PASSWORD = "Cj5WX_nyC7mhWL[CjdPL.DnqDvXsgcbB";
inline static const char* IV = ")1RUg5}mL$~62#Lw";
inline static constexpr RC::Type ComressionType = RC::Type::TAR_BZ2;
namespace Mio
{
class GUIManifest;
class ResourcePackage : public std::enable_shared_from_this<ResourcePackage>
{
public:
ResourcePackage(std::string name, std::vector<std::string> files = {}): name(std::move(name)),
files(std::move(files))
{
}
static std::shared_ptr<ResourcePackage> Create(std::string name, std::vector<std::string> files = {})
{
return std::make_shared<ResourcePackage>(std::move(name), files);
}
void AddFiles(std::vector<std::string> files);
std::string AddFile(std::string file);
void SetRootPath(std::string rpath)
{
rootPath = std::move(rpath);
files[0] = rootPath.string();
auto tmp = files;
files.clear();
files.emplace_back(tmp[0]);
AddFiles(tmp);
}
void SetBuildPath(std::filesystem::path bpath)
{
if (!RC::Utils::Directory::Exists(bpath.string()))
{
RC::Utils::Directory::Create(bpath.string());
}
finalpath = (bpath / name).string() + ResourcePackageExtension;
path = (bpath / name).string() + ".tmp";
}
bool Pack();
bool UnpackAll();
std::string Unpack(std::string selectFile);
private:
std::string name;
std::vector<std::string> files;
std::filesystem::path rootPath = ResourcePath;
std::string finalpath = (PackedPath / name).string() + ResourcePackageExtension;
std::string path = (PackedPath / name).string() + ".tmp";
};
class ResourcePackageManager
{
public:
static std::shared_ptr<ResourcePackage> Load(const std::string& path);
static void AddResource(std::shared_ptr<ResourcePackage> package, std::vector<std::string> files);
static bool Pack(std::shared_ptr<ResourcePackage> package);
static bool UnpackAll(std::shared_ptr<ResourcePackage> package);
static std::string Unpack(std::shared_ptr<ResourcePackage> package, std::string selectFile);
};
class ResourceManager
{
public:
static std::shared_ptr<GUIManifest> LoadManifest(const std::string& name);
static void LoadManifest(const std::string& name, std::shared_ptr<GUIManifest> manifest);
static bool SaveManifest(const std::shared_ptr<GUIManifest>& manifest);
template <typename T>
static std::shared_ptr<T> GetUI(const std::shared_ptr<GUIManifest>& manifest, const std::string& name)
{
if (contains(UIResources, manifest))
return std::dynamic_pointer_cast<T>(UIResources[manifest][name]);
return nullptr;
}
static void RemoveUI(const std::shared_ptr<GUIManifest>& manifest, const std::string& name)
{
if (contains(UIResources, manifest))
UIResources[manifest].erase(name);
}
template <typename T>
static std::shared_ptr<T> GetUI(const std::shared_ptr<GUIManifest>& manifest, const UUid& uid)
{
if (contains(UIResources, manifest))
{
for (auto& pair : UIResources[manifest])
{
auto& ui = pair.second;
if (ui->UID() == uid)
return std::dynamic_pointer_cast<T>(ui);
}
}
return nullptr;
}
static void RemoveUI(const std::shared_ptr<GUIManifest>& manifest, const UUid& uid)
{
if (contains(UIResources, manifest))
{
for (auto it = UIResources[manifest].begin(); it != UIResources[manifest].end();)
{
auto& ui = it->second;
if (ui->UID() == uid)
{
it = UIResources[manifest].erase(it);
return;
}
else
{
++it;
}
}
}
}
template <typename T>
static bool Save(const T& data, const std::string& path)
{
std::string parent = std::filesystem::path(path).parent_path().string();
if (!parent.empty())
if (!std::filesystem::exists(parent))
std::filesystem::create_directories(parent);
std::ofstream file(path, std::ios::out | std::ios::trunc);
file << YAML::convert<T>::encode(data);
file.close();
return true;
}
template <typename T>
static T Load(const std::string& path)
{
std::ifstream file(path);
if (!file.is_open())
{
throw std::runtime_error("Unable to open file for loading");
}
YAML::Node node = YAML::Load(file);
file.close();
T data;
YAML::convert<T>::decode(node, data);
return data;
}
inline static std::unordered_map<std::string, std::shared_ptr<GUIManifest>> Manifests = {};
private:
inline static std::unordered_map<std::shared_ptr<GUIManifest>, std::unordered_map<std::string, std::shared_ptr<
UIBase>>>
UIResources = {};
};
struct Manifests
{
enum class LoadState
{
Loading,
Loaded,
Failed
};
std::map<std::string, std::string> ManifestsList;
std::shared_ptr<GUIManifest> Load(const std::string& name)
{
return std::move(ResourceManager::LoadManifest(ManifestsList[name]));
}
void Load(const std::string& name, std::shared_ptr<GUIManifest> manifest)
{
ResourceManager::LoadManifest(name, std::move(manifest));
}
LoadState& LoadAysnc(const std::string& name, std::shared_ptr<GUIManifest> manifest)
{
LoadStates[name] = LoadState::Loading;
std::thread([this, name, manifest]()
{
Load(name, manifest);
LoadStates[name] = LoadState::Loaded;
}).detach();
return LoadStates[name];
}
std::future<std::shared_ptr<GUIManifest>> LoadAsysnc(const std::string& name)
{
return std::async([this, name]()
{
return Load(name);
});
}
LoadState& LoadAsync(const std::string& name)
{
LoadStates[name] = LoadState::Loading;
std::thread([this, name]()
{
Load(name);
LoadStates[name] = LoadState::Loaded;
}).detach();
return LoadStates[name];
}
LoadState GetLoadState(const std::string& name)
{
return LoadStates[name];
}
std::shared_ptr<GUIManifest> Get(const std::string& name)
{
auto res = std::move(Results[name]);
Results.erase(name);
return res;
}
private:
std::map<std::string, LoadState> LoadStates;
std::map<std::string, std::shared_ptr<GUIManifest>> Results;
};
inline static ResourcePackage Resource("Resource", {ResourcePath.string()});
}
#define RESOURCING(x) Mio::Resource.AddFile(x)
namespace YAML
{
template <>
struct convert<Mio::Manifests>
{
static Node encode(const Mio::Manifests& manifests)
{
Node node;
for (auto& [name, path] : manifests.ManifestsList)
{
node[name] = path;
}
return node;
}
static bool decode(const Node& node, Mio::Manifests& manifests)
{
for (auto& [name, path] : node.as<std::map<std::string, std::string>>())
{
manifests.ManifestsList[name] = path;
}
return true;
}
};
}
#endif //RESOURCEMANAGER_H