-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuildSystem.cpp
More file actions
288 lines (258 loc) · 10.3 KB
/
Copy pathBuildSystem.cpp
File metadata and controls
288 lines (258 loc) · 10.3 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
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
287
288
#include "BuildSystem.h"
#include "AssetDatabase.h"
#include "Meta.h"
#include "Project.h"
#include "EditorResource.h"
#include "Core/AssetBundle.h"
#include "Core/FileSystem.h"
#include "Game/World.h"
#include "Game/GameObject.h"
#include "Game/GameManager.h"
#include "Game/Asset/WorldAsset.h"
#include "Game/Asset/ShaderAsset.h"
#include "Game/Asset/MaterialAsset.h"
#include "Game/Asset/MeshAsset.h"
#include "Game/Asset/TextureAsset.h"
#include <fstream>
namespace sh::editor
{
BuildSystem::BuildSystem() :
uuidRegex("^[0-9a-f]{32}$", std::regex::optimize)
{
}
SH_EDITOR_API void BuildSystem::Build(Project& project, const std::filesystem::path& outputPath)
{
worldUUIDs.clear();
uuids.clear();
currentProject = &project;
core::AssetBundle bundle;
std::vector<game::World*> worldPtrs;
if (!project.GetProjectSetting().startingWorldUUID.IsEmpty())
{
auto objPtr = core::SObjectManager::GetInstance()->GetSObject(project.GetProjectSetting().startingWorldUUID);
worldPtrs.push_back(static_cast<game::World*>(objPtr));
}
for (int i = 0; i < worldPtrs.size(); ++i)
{
auto worldPtr = worldPtrs[i];
core::Json worldJson{};
if (worldPtr->IsLoaded())
worldJson = worldPtr->Serialize();
else
{
core::Json* worldPointPtr = worldPtr->GetWorldPoint();
if (worldPointPtr == nullptr)
{
SH_WARN_FORMAT("World({}) is empty!", worldPtr->GetUUID().ToString());
continue;
}
worldJson = *worldPointPtr;
}
std::unordered_set<std::string> uuids;
ExtractUUIDs(uuids, worldJson);
for (const auto& uuidStr : uuids)
{
const core::UUID uuid{ uuidStr };
this->uuids.insert(uuidStr);
worldUUIDs[worldPtr->GetUUID().ToString()].push_back(uuidStr);
auto obj = core::SObjectManager::GetInstance()->GetSObject(uuid);
if (obj != nullptr)
{
if (obj->GetType().IsChildOf(game::World::GetStaticType()))
{
auto it = std::find(worldPtrs.begin(), worldPtrs.end(), static_cast<game::World*>(obj));
if (it == worldPtrs.end())
worldPtrs.push_back(static_cast<game::World*>(obj));
}
}
}
PackingAssets(bundle, *worldPtr);
}
bundle.SaveBundle(outputPath / "assets.bundle");
ExportGameManager(outputPath / "gameManager.bin");
CopyRuntimeBinaries(outputPath);
}
void BuildSystem::CopyRuntimeBinaries(const std::filesystem::path& outputPath)
{
const std::filesystem::path engineDir = core::FileSystem::GetExecutableDirectory();
#if _WIN32
constexpr const char* exeExt = ".exe";
constexpr const char* libExt = ".dll";
constexpr const char* libPrefix = "";
#else
constexpr const char* exeExt = "";
constexpr const char* libExt = ".so";
constexpr const char* libPrefix = "lib";
#endif
const std::vector<std::string> executables = {
"ShellGame"
};
const std::vector<std::string> libraries = {
"ShellEngineCore",
"ShellEngineWindow",
"ShellEngineRender",
"ShellEnginePhysics",
"ShellEngineGame",
"ShellEngineNetwork",
"ShellEngineSound"
};
#if _WIN32
const std::vector<std::string> extraFiles = {
"OpenAL32.dll"
};
#else
const std::vector<std::string> extraFiles = {
"libopenal.so"
};
#endif
const std::vector<std::string> directories = {
"fonts"
};
std::error_code ec;
if (!std::filesystem::exists(outputPath))
std::filesystem::create_directories(outputPath, ec);
auto copyIfMissingFn = [&](const std::filesystem::path& src, const std::filesystem::path& dst)
{
if (!std::filesystem::exists(src))
{
SH_WARN_FORMAT("Runtime binary not found in engine path: {}", src.u8string());
return;
}
std::error_code copyEc;
std::filesystem::copy_file(src, dst, std::filesystem::copy_options::overwrite_existing, copyEc);
if (copyEc)
SH_ERROR_FORMAT("Failed to copy {} -> {}: {}", src.u8string(), dst.u8string(), copyEc.message());
};
for (const std::string& name : executables)
{
const std::string fileName = name + exeExt;
copyIfMissingFn(engineDir / fileName, outputPath / fileName);
}
for (const std::string& name : libraries)
{
const std::string fileName = libPrefix + name + libExt;
copyIfMissingFn(engineDir / fileName, outputPath / fileName);
}
for (const std::string& fileName : extraFiles)
{
copyIfMissingFn(engineDir / fileName, outputPath / fileName);
}
for (const std::string& dirName : directories)
{
const std::filesystem::path src = engineDir / dirName;
const std::filesystem::path dst = outputPath / dirName;
if (!std::filesystem::exists(src))
{
SH_WARN_FORMAT("Runtime directory not found in engine path: {}", src.u8string());
continue;
}
std::error_code copyEc;
std::filesystem::copy(src, dst,
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing,
copyEc);
if (copyEc)
SH_ERROR_FORMAT("Failed to copy directory {} -> {}: {}", src.u8string(), dst.u8string(), copyEc.message());
}
}
void BuildSystem::ExtractUUIDs(std::unordered_set<std::string>& set, const core::Json& worldJson)
{
if (worldJson.is_object())
{
for (auto const& [key, val] : worldJson.items())
{
ExtractUUIDs(set, val);
}
}
else if (worldJson.is_array())
{
for (const auto& item : worldJson)
{
ExtractUUIDs(set, item);
}
}
else if (worldJson.is_string())
{
const std::string& value = worldJson.get<std::string>();
if (std::regex_match(value, uuidRegex))
{
if (set.find(value) == set.end())
{
set.insert(value);
core::SObject* obj = core::SObjectManager::GetInstance()->GetSObject(core::UUID{ value });
if (core::IsValid(obj))
{
ExtractUUIDs(set,obj->Serialize());
}
}
}
}
}
void BuildSystem::PackingAssets(core::AssetBundle& bundle, game::World& world)
{
auto editorResource = EditorResource::GetInstance();
game::ShaderAsset errorShaderAsset{ *editorResource->GetShader("ErrorShader") };
bundle.AddAsset(errorShaderAsset, true);
game::ShaderAsset lineShaderAsset{ *editorResource->GetShader("Line") };
bundle.AddAsset(lineShaderAsset, true);
game::ShaderAsset uiTextShaderAsset{ *editorResource->GetShader("UITextShader") };
bundle.AddAsset(uiTextShaderAsset, true);
game::ShaderAsset ssaoShaderAsset{ *editorResource->GetShader("SSAOShader") };
bundle.AddAsset(ssaoShaderAsset, true);
game::MaterialAsset errorMatAsset{ *editorResource->GetMaterial("ErrorMaterial") };
bundle.AddAsset(errorMatAsset, true);
game::MaterialAsset lineMatAsset{ *editorResource->GetMaterial("LineMaterial") };
bundle.AddAsset(lineMatAsset, true);
game::MaterialAsset uiTextMatAsset{ *editorResource->GetMaterial("UITextMaterial") };
bundle.AddAsset(uiTextMatAsset, true);
game::MeshAsset cubeMesh{ *editorResource->GetModel("CubeModel")->GetMeshes()[0] };
bundle.AddAsset(cubeMesh, true);
game::MeshAsset sphereMesh{ *editorResource->GetModel("SphereModel")->GetMeshes()[0] };
bundle.AddAsset(sphereMesh, true);
game::MeshAsset planeMesh{ *editorResource->GetModel("PlaneModel")->GetMeshes()[0] };
bundle.AddAsset(planeMesh, true);
game::TextureAsset blackTex{ *editorResource->GetTexture("BlackTexture") };
bundle.AddAsset(blackTex, true);
for (const auto& uuid : uuids)
{
auto obj = core::SObjectManager::GetInstance()->GetSObject(core::UUID{ uuid });
if (obj != nullptr)
{
if (obj->GetType().IsChildOf(game::World::GetStaticType()))
continue;
}
auto asset = AssetDatabase::GetInstance()->GetAsset(core::UUID{ uuid });
if (asset != nullptr)
bundle.AddAsset(*asset, true);
}
game::WorldAsset worldAsset{ world };
worldAsset.ConvertToGameWorldType();
bundle.AddAsset(worldAsset, true);
}
void BuildSystem::ExportGameManager(const std::filesystem::path& outputPath)
{
game::GameManager& manager = *game::GameManager::GetInstance();
ProjectSetting& projectSetting = currentProject->GetProjectSetting();
core::Json mainJson;
if (!projectSetting.startingWorldUUID.IsEmpty())
mainJson["starting"] = projectSetting.startingWorldUUID.ToString();
for (const auto& [worldUUIDStr, uuids] : worldUUIDs)
{
core::Json worldUUIDs{};
for (const auto& uuid : uuids)
{
worldUUIDs[worldUUIDStr].push_back(uuid);
}
mainJson["uuids"].push_back(std::move(worldUUIDs));
}
const std::vector<uint8_t> data{ core::Json::to_bson(mainJson) };
std::ofstream of{ outputPath, std::ios_base::binary };
if (!of.is_open())
{
SH_ERROR_FORMAT("Failed to export game setting!: {}", outputPath.u8string());
return;
}
of.write(reinterpret_cast<const char*>(data.data()), data.size());
of.close();
}
}//namespace