forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetDatabase.cpp
More file actions
226 lines (196 loc) · 5.98 KB
/
Copy pathAssetDatabase.cpp
File metadata and controls
226 lines (196 loc) · 5.98 KB
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
#include "Game/PCH.h"
#include "AssetDatabase.h"
#include "Game/World.h"
#include "Game/TextureLoader.h"
#include "Game/ModelLoader.h"
#include "Game/MaterialLoader.h"
#include "Core/FileLoader.h"
#include <random>
#include <istream>
#include <ostream>
#include <cassert>
#include <algorithm>
namespace sh::editor
{
core::Observer<false, core::SObject*>::Listener AssetDatabase::onDestroyListener{ [&](core::SObject* destoryedObj)
{
auto it = std::find(dirtyObjs.begin(), dirtyObjs.end(), destoryedObj);
if (it != dirtyObjs.end())
dirtyObjs.erase(it);
}
};
void AssetDatabase::CreateMeta(core::SObject* ptr, const std::filesystem::path& metaDir)
{
core::Json metaJson = ptr->Serialize();
std::ofstream os{ metaDir.string() };
if (os.is_open())
{
os << std::setw(4) << metaJson;
os.close();
}
else
{
SH_ERROR_FORMAT("Can't create meta: {}", metaDir.string());
}
}
void AssetDatabase::CreateOrLoadMeta(core::SObject* ptr, const std::filesystem::path& metaDir)
{
if (std::filesystem::exists(metaDir))
{
core::FileLoader loader;
auto file = loader.LoadText(metaDir.string());
if (!file)
{
SH_ERROR_FORMAT("Can't load file: {}", metaDir.string());
return;
}
if (file.value().empty())
{
CreateMeta(ptr, metaDir);
return;
}
core::Json metaJson{ core::Json::parse(file.value()) };
ptr->Deserialize(metaJson);
assert(ptr->GetUUID().ToString() == metaJson["uuid"].get<std::string>());
}
else
{
CreateMeta(ptr, metaDir);
}
}
auto AssetDatabase::LoadMesh(game::World& world, const std::filesystem::path& dir, const std::filesystem::path& metaDir) -> render::Mesh*
{
static game::ModelLoader loader{ world.renderer };
auto ptr = loader.Load(dir.string());
if (ptr == nullptr)
return nullptr;
world.meshes.AddResource(dir.string(), ptr);
ptr->SetName(dir.stem().string());
CreateOrLoadMeta(ptr, metaDir);
uuids.insert_or_assign(dir, ptr->GetUUID());
paths.insert_or_assign(ptr->GetUUID().ToString(), dir);
return ptr;
}
auto AssetDatabase::LoadTexture(game::World& world, const std::filesystem::path& dir, const std::filesystem::path& metaDir) -> render::Texture*
{
static game::TextureLoader loader{ world.renderer };
auto ptr = loader.Load(dir.string());
if (ptr == nullptr)
return nullptr;
world.textures.AddResource(dir.string(), ptr);
ptr->SetName(dir.stem().u8string());
CreateOrLoadMeta(ptr, metaDir);
uuids.insert_or_assign(dir, ptr->GetUUID());
paths.insert_or_assign(ptr->GetUUID().ToString(), dir);
return ptr;
}
auto AssetDatabase::LoadMaterial(game::World& world, const std::filesystem::path& dir) -> render::Material*
{
static game::MaterialLoader loader{ world.renderer };
auto ptr = loader.Load(dir.string());
if (ptr == nullptr)
return nullptr;
world.materials.AddResource(dir.string(), ptr);
ptr->SetName(dir.stem().string());
uuids.insert_or_assign(dir, ptr->GetUUID());
paths.insert_or_assign(ptr->GetUUID().ToString(), dir);
return ptr;
}
void AssetDatabase::SaveMaterial(render::Material* mat, const std::filesystem::path& dir)
{
if (!core::IsValid(mat))
return;
core::Json matJson{ mat->Serialize() };
std::ofstream os{ dir };
os << std::setw(4) << matJson;
os.close();
}
SH_EDITOR_API auto AssetDatabase::ImportAsset(game::World& world, const std::filesystem::path& dir) -> core::SObject*
{
if (!std::filesystem::exists(dir))
return nullptr;
if (std::filesystem::is_directory(dir))
return nullptr;
std::string filename = dir.filename().string() + ".meta";
std::string extension = dir.extension().string();
auto parentDir = dir.parent_path();
auto metaDir = parentDir / filename;
bool hasMetaFile = std::filesystem::exists(metaDir);
if (extension == ".jpg" || extension == ".png")
return LoadTexture(world, dir, metaDir);
if (extension == ".obj")
return LoadMesh(world, dir, metaDir);
if (extension == ".mat")
return LoadMaterial(world, dir);
return nullptr;
}
SH_EDITOR_API void AssetDatabase::SaveAllAssets()
{
for (auto obj : dirtyObjs)
{
auto it = paths.find(obj->GetUUID().ToString());
if (it == paths.end())
continue;
if (obj->GetType() == render::Material::GetStaticType())
{
SaveMaterial(static_cast<render::Material*>(obj), it->second);
}
}
dirtyObjs.clear();
}
void AssetDatabase::LoadAllAssetsHelper(const std::filesystem::path& dir, bool recursive)
{
for (auto& entry : std::filesystem::directory_iterator(dir))
{
if (entry.is_directory())
{
if (recursive)
LoadAllAssetsHelper(entry.path(), recursive);
}
else if (entry.is_regular_file())
{
if (entry.path().extension() == ".meta")
continue;
int priority = 0;
if (entry.path().extension() == ".mat")
priority = -1;
loadingAssetsQueue.push(AssetLoadData{ priority, entry.path() });
}
}
}
SH_EDITOR_API void AssetDatabase::LoadAllAssets(game::World& world, const std::filesystem::path& dir, bool recursive)
{
LoadAllAssetsHelper(dir, recursive);
while (!loadingAssetsQueue.empty())
{
AssetLoadData data = std::move(const_cast<AssetLoadData&>(loadingAssetsQueue.top()));
loadingAssetsQueue.pop();
ImportAsset(world, data.path);
}
}
SH_EDITOR_API bool AssetDatabase::CreateAsset(game::World& world, const std::filesystem::path& dir, const core::ISerializable& serializable)
{
if (std::filesystem::exists(dir))
return false;
auto json = serializable.Serialize();
if (json.contains("uuid"))
uuids.insert_or_assign(dir, core::UUID(json["uuid"].get<std::string>()));
std::ofstream os(dir);
os << std::setw(4) << json;
os.close();
return true;
}
SH_EDITOR_API auto AssetDatabase::GetAssetUUID(const std::filesystem::path& dir) -> std::optional<core::UUID>
{
auto it = uuids.find(dir);
if (it == uuids.end())
return std::nullopt;
return it->second;
}
SH_EDITOR_API void AssetDatabase::SetDirty(core::SObject* obj)
{
obj->onDestroy.Register(onDestroyListener);
if(core::IsValid(obj))
dirtyObjs.push_back(obj);
}
}//namespace