10 std::string StandaloneBuilder::ReadProjectName(
const std::filesystem::path& root)
13 for (
const auto& entry : std::filesystem::directory_iterator(root, ec))
15 if (entry.path().extension() !=
".nojobproject")
continue;
16 std::ifstream in(entry.path());
20 if ((in >> header >> version) && header ==
"NOJOB_PROJECT" &&
21 (in >> std::quoted(name)) && !name.empty())
24 return root.filename().string().empty() ? std::string(
"NoJobGame") : root.filename().string();
27 std::filesystem::path StandaloneBuilder::FindBuiltRuntimeExecutable(
28 const std::filesystem::path& buildRoot,
const std::wstring& configuration)
30 const std::vector<std::filesystem::path> candidates = {
31 buildRoot / configuration /
"NoJobRuntime.exe",
32 buildRoot /
"NoJobRuntime.exe",
33 buildRoot /
"Runtime" / configuration /
"NoJobRuntime.exe"
35 for (
const auto& candidate : candidates)
36 if (std::filesystem::exists(candidate))
return candidate;
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")
47 bool StandaloneBuilder::IsRuntimeAssetExtension(
const std::filesystem::path& path)
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"
58 return std::find(extensions.begin(), extensions.end(), ext) != extensions.end();
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)
67 if (!std::filesystem::exists(source, ec) || !std::filesystem::is_regular_file(source, ec))
70 auto relative = std::filesystem::relative(source, root, ec);
71 if (ec || relative.empty() || relative.native().find(L
"..") == 0)
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);
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)
90 for (
const auto& sceneRelative : settings.Scenes)
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(); };
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; } } }
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';
109 bool StandaloneBuilder::ValidateStandalonePackage(
110 const std::filesystem::path& outputRoot,
const std::string& buildName,
113 const std::vector<std::filesystem::path> required = {
114 outputRoot / (buildName +
".exe"),
115 outputRoot /
"NoJobRuntimeAssets.manifest",
116 outputRoot /
"Assets"
118 for (
const auto& path : required)
119 if (!std::filesystem::exists(path))
121 error =
"Missing runtime package item: " + path.string();
125 const std::vector<std::filesystem::path> forbiddenDirectories = {
126 outputRoot /
"out", outputRoot /
"Editor", outputRoot /
"Engine",
127 outputRoot /
".git", outputRoot /
".vs"
129 for (
const auto& path : forbiddenDirectories)
130 if (std::filesystem::exists(path))
132 error =
"Development directory leaked into package: " + path.string();
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)
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")
148 error =
"Development/source file leaked into package: " + it->path().string();
154 error =
"No .nojobproject file found in package.";
161 const std::filesystem::path& root,
162 const std::filesystem::path& cmake,
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)
175 result.
Error = std::move(message);
176 log(
"[Build] " + result.
Error);
180 log(
"[Build] Building standalone " + std::string(configuration.begin(), configuration.end()) +
"...");
182 if (runProcess(cmake, {L
"-S", root.wstring(), L
"-B", buildRoot.wstring()},
"[Build/CMake] ") != 0)
183 return fail(
"CMake configure failed.");
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.");
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.");
193 const auto runtimeExe = FindBuiltRuntimeExecutable(buildRoot, configuration);
194 if (runtimeExe.empty())
return fail(
"NoJobRuntime.exe was not found after build.");
197 if (std::filesystem::exists(outputRoot, ec))
200 std::filesystem::remove_all(outputRoot, ec);
202 return fail(
"Could not clean previous output: " + ec.message() +
203 ". Close the previous game EXE/Explorer handles and retry.");
206 std::filesystem::create_directories(outputRoot, ec);
207 if (ec)
return fail(
"Could not create output: " + ec.message());
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());
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).");
219 for (
const auto& entry : std::filesystem::directory_iterator(root, ec))
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());
228 const auto scriptsDestination = outputRoot /
"RuntimeData" /
"ProjectScripts";
229 std::filesystem::create_directories(scriptsDestination, ec);
230 if (ec)
return fail(
"ProjectScripts output failed: " + ec.message());
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)
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)
248 selectedDll = it->path();
253 if (!selectedDll.empty())
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());
262 log(
"[Build] No matching " + std::string(releaseBuild ?
"Release" :
"Debug") +
263 " ProjectScripts DLL found; package will contain no project scripts.");
266 std::string validationError;
267 if (!ValidateStandalonePackage(outputRoot, buildName, validationError))
268 return fail(
"Package validation FAILED: " + validationError);
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());
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