NoJobEngine 2.0.7
Loading...
Searching...
No Matches
StandaloneBuilder.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cctype>
5#include <fstream>
6#include <iomanip>
7
8namespace NoJob
9{
10 std::string StandaloneBuilder::ReadProjectName(const std::filesystem::path& root)
11 {
12 std::error_code ec;
13 for (const auto& entry : std::filesystem::directory_iterator(root, ec))
14 {
15 if (entry.path().extension() != ".nojobproject") continue;
16 std::ifstream in(entry.path());
17 std::string header;
18 int version = 0;
19 std::string name;
20 if ((in >> header >> version) && header == "NOJOB_PROJECT" &&
21 (in >> std::quoted(name)) && !name.empty())
22 return name;
23 }
24 return root.filename().string().empty() ? std::string("NoJobGame") : root.filename().string();
25 }
26
27 std::filesystem::path StandaloneBuilder::FindBuiltRuntimeExecutable(
28 const std::filesystem::path& buildRoot, const std::wstring& configuration)
29 {
30 const std::vector<std::filesystem::path> candidates = {
31 buildRoot / configuration / "NoJobRuntime.exe",
32 buildRoot / "NoJobRuntime.exe",
33 buildRoot / "Runtime" / configuration / "NoJobRuntime.exe"
34 };
35 for (const auto& candidate : candidates)
36 if (std::filesystem::exists(candidate)) return candidate;
37
38 std::error_code ec;
39 for (auto it = std::filesystem::recursive_directory_iterator(
40 buildRoot, std::filesystem::directory_options::skip_permission_denied, ec);
41 !ec && it != std::filesystem::recursive_directory_iterator(); ++it)
42 if (it->is_regular_file(ec) && it->path().filename() == "NoJobRuntime.exe")
43 return it->path();
44 return {};
45 }
46
47 bool StandaloneBuilder::IsRuntimeAssetExtension(const std::filesystem::path& path)
48 {
49 std::string ext = path.extension().string();
50 std::transform(ext.begin(), ext.end(), ext.begin(),
51 [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
52 static const std::vector<std::string> extensions = {
53 ".nojobscene", ".nojobmat", ".nojobprefab",
54 ".png", ".jpg", ".jpeg", ".tga", ".bmp", ".hdr",
55 ".obj", ".fbx", ".gltf", ".glb", ".dae", ".stl", ".ply", ".3ds", ".blend",
56 ".wav", ".mp3", ".flac"
57 };
58 return std::find(extensions.begin(), extensions.end(), ext) != extensions.end();
59 }
60
61 bool StandaloneBuilder::CopyRuntimeAsset(
62 const std::filesystem::path& root, const std::filesystem::path& source,
63 const std::filesystem::path& outputRoot,
64 std::vector<std::filesystem::path>& cooked, std::string& error)
65 {
66 std::error_code ec;
67 if (!std::filesystem::exists(source, ec) || !std::filesystem::is_regular_file(source, ec))
68 return true;
69
70 auto relative = std::filesystem::relative(source, root, ec);
71 if (ec || relative.empty() || relative.native().find(L"..") == 0)
72 return true;
73
74 const auto destination = outputRoot / relative;
75 std::filesystem::create_directories(destination.parent_path(), ec);
76 if (ec) { error = ec.message(); return false; }
77 std::filesystem::copy_file(source, destination,
78 std::filesystem::copy_options::overwrite_existing, ec);
79 if (ec) { error = ec.message(); return false; }
80 cooked.push_back(relative);
81 return true;
82 }
83
84 bool StandaloneBuilder::CookRuntimeAssets(
85 const std::filesystem::path& root, const BuildSettings& settings,
86 const std::filesystem::path& outputRoot,
87 std::vector<std::filesystem::path>& cooked, std::string& error)
88 {
89 std::error_code ec;
90 for (const auto& sceneRelative : settings.Scenes)
91 {
92 const auto scenePath = sceneRelative.is_absolute() ? sceneRelative : root / sceneRelative;
93 if (!std::filesystem::exists(scenePath)) { error = "Scene does not exist: " + scenePath.string(); return false; }
94 if (!CopyRuntimeAsset(root, scenePath, outputRoot, cooked, error)) return false;
95 std::ifstream scene(scenePath);
96 std::string contents((std::istreambuf_iterator<char>(scene)), std::istreambuf_iterator<char>()), token;
97 std::vector<std::filesystem::path> candidates;
98 auto flush=[&](){ if(token.empty())return; for(char& c:token)if(c=='\\')c='/'; std::filesystem::path q(token); if(IsRuntimeAssetExtension(q))candidates.push_back(q); token.clear(); };
99 bool quoted=false;
100 for(char c:contents){ if(c=='"'){quoted=!quoted;flush();continue;} if(quoted)token+=c; else if(std::isalnum((unsigned char)c)||c=='/'||c=='\\'||c=='.'||c=='_'||c=='-'||c==':')token+=c; else flush(); } flush();
101 for(const auto& candidate:candidates){ auto source=candidate.is_absolute()?candidate:root/candidate; if(!CopyRuntimeAsset(root,source,outputRoot,cooked,error))return false; std::string ext=source.extension().string(); std::transform(ext.begin(),ext.end(),ext.begin(),[](unsigned char c){return(char)std::tolower(c);}); if(ext==".obj"||ext==".gltf"||ext==".dae"){ for(const auto& sibling:std::filesystem::directory_iterator(source.parent_path(),ec)){ if(ec)break; if(!sibling.is_regular_file(ec))continue; auto se=sibling.path().extension().string(); if(IsRuntimeAssetExtension(sibling.path())||se==".mtl"||se==".bin") if(!CopyRuntimeAsset(root,sibling.path(),outputRoot,cooked,error))return false; } } }
102 }
103 std::sort(cooked.begin(),cooked.end()); cooked.erase(std::unique(cooked.begin(),cooked.end()),cooked.end());
104 std::ofstream manifest(outputRoot/"NoJobRuntimeAssets.manifest"); manifest<<"NOJOB_RUNTIME_ASSETS 2\n"; for(auto& q:cooked)manifest<<q.generic_string()<<'\n';
105 std::ofstream config(outputRoot/"NoJobBuildConfig.nojobbuild"); config<<"NOJOB_BUILD_CONFIG 1\n"<<std::quoted(settings.BuildName)<<'\n'<<std::quoted(settings.StartupScene().generic_string())<<'\n'<<settings.Scenes.size()<<'\n'; for(auto& q:settings.Scenes)config<<std::quoted(q.generic_string())<<'\n';
106 return true;
107 }
108
109 bool StandaloneBuilder::ValidateStandalonePackage(
110 const std::filesystem::path& outputRoot, const std::string& buildName,
111 std::string& error)
112 {
113 const std::vector<std::filesystem::path> required = {
114 outputRoot / (buildName + ".exe"),
115 outputRoot / "NoJobRuntimeAssets.manifest",
116 outputRoot / "Assets"
117 };
118 for (const auto& path : required)
119 if (!std::filesystem::exists(path))
120 {
121 error = "Missing runtime package item: " + path.string();
122 return false;
123 }
124
125 const std::vector<std::filesystem::path> forbiddenDirectories = {
126 outputRoot / "out", outputRoot / "Editor", outputRoot / "Engine",
127 outputRoot / ".git", outputRoot / ".vs"
128 };
129 for (const auto& path : forbiddenDirectories)
130 if (std::filesystem::exists(path))
131 {
132 error = "Development directory leaked into package: " + path.string();
133 return false;
134 }
135
136 std::error_code ec;
137 bool hasProjectFile = false;
138 for (auto it = std::filesystem::recursive_directory_iterator(
139 outputRoot, std::filesystem::directory_options::skip_permission_denied, ec);
140 !ec && it != std::filesystem::recursive_directory_iterator(); ++it)
141 {
142 if (!it->is_regular_file(ec)) continue;
143 const auto ext = it->path().extension().string();
144 if (ext == ".nojobproject") hasProjectFile = true;
145 if (ext == ".cpp" || ext == ".h" || ext == ".hpp" ||
146 ext == ".pdb" || ext == ".ilk" || ext == ".obj")
147 {
148 error = "Development/source file leaked into package: " + it->path().string();
149 return false;
150 }
151 }
152 if (!hasProjectFile)
153 {
154 error = "No .nojobproject file found in package.";
155 return false;
156 }
157 return true;
158 }
159
161 const std::filesystem::path& root,
162 const std::filesystem::path& cmake,
163 const BuildSettings& settings,
164 const LogFunction& log,
165 const ProcessFunction& runProcess)
166 {
167 Result result;
168 const std::wstring configuration = settings.Configuration;
169 const std::string projectName = ReadProjectName(root);
170 const std::string buildName = settings.BuildName;
171 const auto buildRoot = root / "out" / "StandaloneBuild";
172 const auto outputRoot = root / "Builds" / buildName;
173 auto fail = [&](std::string message)
174 {
175 result.Error = std::move(message);
176 log("[Build] " + result.Error);
177 return result;
178 };
179
180 log("[Build] Building standalone " + std::string(configuration.begin(), configuration.end()) + "...");
181
182 if (runProcess(cmake, {L"-S", root.wstring(), L"-B", buildRoot.wstring()}, "[Build/CMake] ") != 0)
183 return fail("CMake configure failed.");
184
185 if (runProcess(cmake, {L"--build", buildRoot.wstring(), L"--config", configuration,
186 L"--target", L"NoJobRuntime"}, "[Build/CMake] ") != 0)
187 return fail("NoJobRuntime build failed.");
188
189 if (runProcess(cmake, {L"--build", buildRoot.wstring(), L"--config", configuration,
190 L"--target", L"NoJobProjectScripts"}, "[Build/Scripts] ") != 0)
191 return fail("ProjectScripts " + std::string(configuration.begin(), configuration.end()) + " build failed.");
192
193 const auto runtimeExe = FindBuiltRuntimeExecutable(buildRoot, configuration);
194 if (runtimeExe.empty()) return fail("NoJobRuntime.exe was not found after build.");
195
196 std::error_code ec;
197 if (std::filesystem::exists(outputRoot, ec))
198 {
199 ec.clear();
200 std::filesystem::remove_all(outputRoot, ec);
201 if (ec)
202 return fail("Could not clean previous output: " + ec.message() +
203 ". Close the previous game EXE/Explorer handles and retry.");
204 }
205
206 std::filesystem::create_directories(outputRoot, ec);
207 if (ec) return fail("Could not create output: " + ec.message());
208
209 std::filesystem::copy_file(runtimeExe, outputRoot / (buildName + ".exe"),
210 std::filesystem::copy_options::overwrite_existing, ec);
211 if (ec) return fail("Could not copy runtime executable: " + ec.message());
212
213 std::string copyError;
214 std::vector<std::filesystem::path> cookedAssets;
215 if (!CookRuntimeAssets(root, settings, outputRoot, cookedAssets, copyError))
216 return fail("Asset cooking failed: " + copyError);
217 log("[Build] Cooked " + std::to_string(cookedAssets.size()) + " runtime asset(s).");
218
219 for (const auto& entry : std::filesystem::directory_iterator(root, ec))
220 {
221 if (entry.path().extension() != ".nojobproject") continue;
222 std::filesystem::copy_file(entry.path(), outputRoot / entry.path().filename(),
223 std::filesystem::copy_options::overwrite_existing, ec);
224 if (ec) return fail("Project config copy failed: " + ec.message());
225 break;
226 }
227
228 const auto scriptsDestination = outputRoot / "RuntimeData" / "ProjectScripts";
229 std::filesystem::create_directories(scriptsDestination, ec);
230 if (ec) return fail("ProjectScripts output failed: " + ec.message());
231
232 const bool releaseBuild = configuration == L"Release";
233 std::filesystem::path selectedDll;
234 std::filesystem::file_time_type selectedTime{};
235 for (auto it = std::filesystem::recursive_directory_iterator(
236 buildRoot, std::filesystem::directory_options::skip_permission_denied, ec);
237 !ec && it != std::filesystem::recursive_directory_iterator(); ++it)
238 {
239 if (!it->is_regular_file(ec) || it->path().extension() != ".dll") continue;
240 const std::string stem = it->path().stem().string();
241 if (stem.rfind("NoJobProjectScripts_", 0) != 0) continue;
242 const bool debugDll = !stem.empty() && stem.back() == 'd';
243 if (releaseBuild == debugDll) continue;
244 const auto time = std::filesystem::last_write_time(it->path(), ec);
245 if (ec) { ec.clear(); continue; }
246 if (selectedDll.empty() || time > selectedTime)
247 {
248 selectedDll = it->path();
249 selectedTime = time;
250 }
251 }
252
253 if (!selectedDll.empty())
254 {
255 std::filesystem::copy_file(selectedDll, scriptsDestination / selectedDll.filename(),
256 std::filesystem::copy_options::overwrite_existing, ec);
257 if (ec) return fail("ProjectScripts DLL copy failed: " + ec.message());
258 log("[Build] Packaged ProjectScripts: " + selectedDll.filename().string());
259 }
260 else
261 {
262 log("[Build] No matching " + std::string(releaseBuild ? "Release" : "Debug") +
263 " ProjectScripts DLL found; package will contain no project scripts.");
264 }
265
266 std::string validationError;
267 if (!ValidateStandalonePackage(outputRoot, buildName, validationError))
268 return fail("Package validation FAILED: " + validationError);
269
270 result.Succeeded = true;
271 result.OutputDirectory = outputRoot;
272 log("[Build] Package validation passed.");
273 log("[Build] Package layout: Assets + RuntimeData + manifest (no development out folder).");
274 log("[Build] Standalone build completed: " + outputRoot.string());
275 return result;
276 }
277}
StandaloneBuilder declarations for the Build and Packaging subsystem.
static Result Build(const std::filesystem::path &projectRoot, const std::filesystem::path &cmakeExecutable, const BuildSettings &settings, const LogFunction &log, const ProcessFunction &runProcess)
std::function< void(std::string)> LogFunction
std::function< int( const std::filesystem::path &, const std::vector< std::wstring > &, const std::string &)> ProcessFunction
std::wstring Configuration
std::filesystem::path OutputDirectory