/
Mr.Stalin
/
FOnline-Engine
Обзор
Документация
Войти
/
Mr.Stalin
/
FOnline-Engine
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Source/Applications/ASCompilerApp.cpp
187 строк
8 KB
cvet
Non const locals (#190)
24 июл 2026, 10:46
Не верифицирован
24 июл 2026, 10:46
4883d25
Код
Авторство
О чём код?
// __________ ___ ______ _ // / ____/ __ \____ / (_)___ ___ / ____/___ ____ _(_)___ ___ // / /_ / / / / __ \/ / / __ \/ _ \ / __/ / __ \/ __ `/ / __ \/ _ ` // / __/ / /_/ / / / / / / / / / __/ / /___/ / / / /_/ / / / / / __/ // /_/ \____/_/ /_/_/_/_/ /_/\___/ /_____/_/ /_/\__, /_/_/ /_/\___/ // /____/ // FOnline Engine // https://fonline.ru // https://github.com/cvet/fonline // // MIT License // // Copyright (c) 2006 - 2026, Anton Tsvetinskiy aka cvet <cvet@tut.by> // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // #include "Common.h" #if FO_ANGELSCRIPT_SCRIPTING #include "AngelScriptBaker.h" #include "Application.h" #include "FileSystem.h" #include "MetadataBaker.h" #include "ScriptSystem.h" #include "Settings.h" FO_USING_NAMESPACE(); #if !FO_TESTING_APP int main(int argc, char** argv) #else [[maybe_unused]] static auto ASCompilerApp(CommandLineArgs args) -> int #endif { FO_STACK_TRACE_ENTRY(); #if !FO_TESTING_APP CommandLineArgs args {numeric_cast<int32_t>(argc), argv}; #endif try { InitApp(args, AppInitFlags::DisableLogTags); FO_VERIFY_AND_THROW(!GetApp()->Settings.BakeOutput.empty(), "AngelScript compiler cannot prepare metadata without a bake output directory", GetApp()->Settings.GetResourcePacks().size()); WriteLog("Prepare metadata"); FileSystem metadata_files; for (const auto& res_pack : GetApp()->Settings.GetResourcePacks()) { if (!vec_exists(res_pack.Bakers, MetadataBaker::NAME)) { continue; } FileSystem res_files; for (const auto& dir : res_pack.InputDirs) { res_files.AddDirSource(dir, true); } auto write_file = [&](string_view path, const_span<uint8_t> data) FO_DEFERRED { string output_path = strex(GetApp()->Settings.BakeOutput).combine_path(res_pack.Name).combine_path(path).str(); string dir = strex(output_path).extract_dir().str(); if (!dir.empty()) { bool dir_ok = fs_create_directories(dir); FO_VERIFY_AND_THROW(dir_ok, "Failed to create metadata output directory", dir, output_path, res_pack.Name); } std::ofstream file {std::filesystem::path {fs_make_path(output_path)}, std::ios::binary | std::ios::trunc}; FO_VERIFY_AND_THROW(file, "Failed to open metadata output file for writing", output_path, res_pack.Name, path, data.size()); if (!data.empty()) { auto data_chars = make_ptr(data.data()).reinterpret_as<char>(); file.write(data_chars.get(), numeric_cast<std::streamsize>(data.size())); } FO_VERIFY_AND_THROW(file, "Failed while writing metadata output file", output_path, res_pack.Name, path, data.size()); return BakingWriteResult::Changed; }; auto settings_ptr = make_nptr(&GetApp()->Settings); auto baking_ctx = SafeAlloc::MakeShared<BakingContext>(BakingContext {.Settings = settings_ptr, .PackName = res_pack.Name, .WriteData = write_file, .ForceSyncMode = true}); auto metadata_baker = MetadataBaker(std::move(baking_ctx)); try { metadata_baker.BakeFiles(res_files.FilterFiles(res_pack.IncludePatterns, res_pack.ExcludePatterns), ""); metadata_files.AddDirSource(strex(GetApp()->Settings.BakeOutput).combine_path(res_pack.Name), false); } catch (const MetadataBakerException& ex) { const_span<string> params = ex.params(); if (params.size() >= 2 && !params.front().empty()) { WriteLog("{}", strex("{}({},{}): {} : {}", params[0], strex(params[1]).to_int64(), 0, "error", ex.message())); } else { WriteLog("{}", ex.what()); } WriteLog("Metadata preparing failed!"); ExitApp(false); } catch (const std::exception& ex) { WriteLog("{}", ex.what()); WriteLog("Metadata preparing failed!"); ExitApp(false); } } WriteLog("Compile scripts"); for (const auto& res_pack : GetApp()->Settings.GetResourcePacks()) { if (!vec_exists(res_pack.Bakers, AngelScriptBaker::NAME)) { continue; } FileSystem res_files; for (const auto& dir : res_pack.InputDirs) { res_files.AddDirSource(dir, true); } auto write_file = [&](string_view path, const_span<uint8_t> data) FO_DEFERRED { string output_path = strex(GetApp()->Settings.BakeOutput).combine_path(res_pack.Name).combine_path(path).str(); string dir = strex(output_path).extract_dir().str(); if (!dir.empty()) { bool dir_ok = fs_create_directories(dir); FO_VERIFY_AND_THROW(dir_ok, "Failed to create AngelScript output directory", dir, output_path, res_pack.Name); } std::ofstream file {std::filesystem::path {fs_make_path(output_path)}, std::ios::binary | std::ios::trunc}; FO_VERIFY_AND_THROW(file, "Failed to open AngelScript output file for writing", output_path, res_pack.Name, path, data.size()); if (!data.empty()) { auto data_chars = make_ptr(data.data()).reinterpret_as<char>(); file.write(data_chars.get(), numeric_cast<std::streamsize>(data.size())); } FO_VERIFY_AND_THROW(file, "Failed while writing AngelScript output file", output_path, res_pack.Name, path, data.size()); return BakingWriteResult::Changed; }; auto settings_ptr = make_nptr(&GetApp()->Settings); auto metadata_files_ptr = make_nptr(&metadata_files); auto baking_ctx = SafeAlloc::MakeShared<BakingContext>(BakingContext {.Settings = settings_ptr, .PackName = res_pack.Name, .WriteData = write_file, .BakedFiles = metadata_files_ptr, .ForceSyncMode = true}); auto scripts_baker = AngelScriptBaker(std::move(baking_ctx)); try { scripts_baker.BakeFiles(res_files.FilterFiles(res_pack.IncludePatterns, res_pack.ExcludePatterns), ""); } catch (const std::exception& ex) { WriteLog("AngelScript compile error: {}", ex.what()); WriteLog("Scripts compilation failed!"); ExitApp(false); } } WriteLog("Scripts compilation succeeded!"); ExitApp(true); } catch (const std::exception& ex) { ReportExceptionAndExit(ex); } catch (...) { FO_UNKNOWN_EXCEPTION(); } } #endif