From 2dad1afe0f2bd8d617e997117da91a987c3e06d5 Mon Sep 17 00:00:00 2001 From: jekky <11986158+jac3km4@users.noreply.github.com> Date: Fri, 8 Mar 2024 00:36:05 +0000 Subject: [PATCH 1/3] feat: write modded redscripts to a separate file --- deps/redscript | 2 +- src/dll/Hooks/ExecuteProcess.cpp | 30 +++++++++++++++---- src/dll/Paths.cpp | 5 ++++ src/dll/Paths.hpp | 1 + .../ScriptCompiler/ScriptCompilerSettings.cpp | 18 +++++++++++ .../ScriptCompiler/ScriptCompilerSettings.hpp | 4 +++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/deps/redscript b/deps/redscript index 85dbc000..9d553bc7 160000 --- a/deps/redscript +++ b/deps/redscript @@ -1 +1 @@ -Subproject commit 85dbc0002a7b2c9ec0c62bf706f8186fb33a6f97 +Subproject commit 9d553bc7af66a46cbdca146832adfc8f13d91472 diff --git a/src/dll/Hooks/ExecuteProcess.cpp b/src/dll/Hooks/ExecuteProcess.cpp index 2f2a4dcc..8dea7e85 100644 --- a/src/dll/Hooks/ExecuteProcess.cpp +++ b/src/dll/Hooks/ExecuteProcess.cpp @@ -87,14 +87,26 @@ bool Hooks::ExecuteProcess::Detach() bool ExecuteScc(SccApi& scc) { - const auto r6Dir = App::Get()->GetPaths()->GetR6Dir(); auto scriptSystem = App::Get()->GetScriptCompilationSystem(); + auto engine = RED4ext::CGameEngine::Get(); + + ScriptCompilerSettings settings(scc, App::Get()->GetPaths()->GetR6Dir()); + + std::filesystem::path blobPath = scriptSystem->HasScriptsBlob() + ? scriptSystem->GetScriptsBlob() + : App::Get()->GetPaths()->GetDefaultScriptBundleFile(); + auto outputCacheFile = blobPath.replace_extension("redscripts.modded"); - ScriptCompilerSettings settings(scc, r6Dir); if (scriptSystem->HasScriptsBlob()) { - settings.SetCustomCacheFile(scriptSystem->GetScriptsBlob()); + settings.SetCustomCacheFile(blobPath); + } + + if (settings.SupportsOutputCacheFileParameter()) + { + settings.SetOutputCacheFile(outputCacheFile); } + for (const auto& [_, path] : scriptSystem->GetScriptPaths()) { settings.AddScriptPath(path); @@ -104,7 +116,7 @@ bool ExecuteScc(SccApi& scc) if (const auto error = std::get_if(&result)) { - RED4ext::CGameEngine::Get()->scriptsCompilationErrors = error->GetMessage().c_str(); + engine->scriptsCompilationErrors = error->GetMessage().c_str(); spdlog::warn("scc invocation failed with an error: {}", error->GetMessage()); return false; } @@ -146,6 +158,14 @@ bool ExecuteScc(SccApi& scc) } } - spdlog::info("scc invoked successfully, {} source refs were returned", refCount); + spdlog::info("scc invoked successfully, {} source refs were registered", refCount); + + if (settings.SupportsOutputCacheFileParameter()) + { + scriptSystem->SetScriptsBlob(outputCacheFile); + engine->scriptsBlobPath = Utils::Narrow(outputCacheFile.c_str()); + spdlog::info(L"script blob path was updated to '{}'", outputCacheFile); + } + return true; } diff --git a/src/dll/Paths.cpp b/src/dll/Paths.cpp index 66e528f0..4958b705 100644 --- a/src/dll/Paths.cpp +++ b/src/dll/Paths.cpp @@ -58,6 +58,11 @@ std::filesystem::path Paths::GetR6Scripts() const return GetRootDir() / L"r6" / L"scripts"; } +std::filesystem::path Paths::GetDefaultScriptBundleFile() const +{ + return GetRootDir() / L"r6" / L"cache" / "final.redscripts"; +} + std::filesystem::path Paths::GetR6CacheModded() const { return GetRootDir() / L"r6" / L"cache" / L"modded"; diff --git a/src/dll/Paths.hpp b/src/dll/Paths.hpp index de312c97..a59187cf 100644 --- a/src/dll/Paths.hpp +++ b/src/dll/Paths.hpp @@ -17,6 +17,7 @@ class Paths std::filesystem::path GetRedscriptPathsFile() const; std::filesystem::path GetR6Scripts() const; + std::filesystem::path GetDefaultScriptBundleFile() const; std::filesystem::path GetR6CacheModded() const; std::filesystem::path GetR6Dir() const; diff --git a/src/dll/ScriptCompiler/ScriptCompilerSettings.cpp b/src/dll/ScriptCompiler/ScriptCompilerSettings.cpp index 61cdb75b..92bbddee 100644 --- a/src/dll/ScriptCompiler/ScriptCompilerSettings.cpp +++ b/src/dll/ScriptCompiler/ScriptCompilerSettings.cpp @@ -6,6 +6,12 @@ ScriptCompilerSettings::ScriptCompilerSettings(SccApi& aApi, std::filesystem::pa { } +bool ScriptCompilerSettings::SupportsOutputCacheFileParameter() const +{ + // added in redscript 0.5.18, previous versions will have this set to NULL + return m_scc.settings_set_output_cache_file != nullptr; +} + ScriptCompilerSettings* ScriptCompilerSettings::AddScriptPath(std::filesystem::path aPath) { m_scriptPaths.emplace_back(aPath); @@ -18,6 +24,12 @@ ScriptCompilerSettings* ScriptCompilerSettings::SetCustomCacheFile(std::filesyst return this; } +ScriptCompilerSettings* ScriptCompilerSettings::SetOutputCacheFile(std::filesystem::path aPath) +{ + m_outputCacheFile = aPath; + return this; +} + ScriptCompilerSettings::Result ScriptCompilerSettings::Compile() { auto r6PathStr = m_r6Path.u8string(); @@ -29,6 +41,12 @@ ScriptCompilerSettings::Result ScriptCompilerSettings::Compile() m_scc.settings_set_custom_cache_file(settings, reinterpret_cast(customCacheFileStr.c_str())); } + if (SupportsOutputCacheFileParameter() && !m_outputCacheFile.empty()) + { + auto outputCacheFileStr = m_outputCacheFile.u8string(); + m_scc.settings_set_output_cache_file(settings, reinterpret_cast(outputCacheFileStr.c_str())); + } + for (const auto& path : m_scriptPaths) { auto pathStr = path.u8string(); diff --git a/src/dll/ScriptCompiler/ScriptCompilerSettings.hpp b/src/dll/ScriptCompiler/ScriptCompilerSettings.hpp index 8e6279d7..89ee404c 100644 --- a/src/dll/ScriptCompiler/ScriptCompilerSettings.hpp +++ b/src/dll/ScriptCompiler/ScriptCompilerSettings.hpp @@ -10,8 +10,11 @@ class ScriptCompilerSettings ScriptCompilerSettings(SccApi& aApi, std::filesystem::path aR6Path); + bool SupportsOutputCacheFileParameter() const; + ScriptCompilerSettings* AddScriptPath(std::filesystem::path aPath); ScriptCompilerSettings* SetCustomCacheFile(std::filesystem::path aPath); + ScriptCompilerSettings* SetOutputCacheFile(std::filesystem::path aPath); Result Compile(); private: @@ -19,4 +22,5 @@ class ScriptCompilerSettings std::filesystem::path m_r6Path; std::vector m_scriptPaths; std::filesystem::path m_customCacheFile; + std::filesystem::path m_outputCacheFile; }; From 63bdab2149aa7ea0291d7b11e7a79b505d308747 Mon Sep 17 00:00:00 2001 From: jekky <11986158+jac3km4@users.noreply.github.com> Date: Fri, 8 Mar 2024 01:08:38 +0000 Subject: [PATCH 2/3] fix: handle modded blob separately to allow multiple invocations --- src/dll/Hooks/ExecuteProcess.cpp | 10 +++++----- src/dll/Hooks/LoadScripts.cpp | 14 +++++--------- src/dll/Systems/ScriptCompilationSystem.cpp | 16 ++++++++++++++++ src/dll/Systems/ScriptCompilationSystem.hpp | 6 ++++++ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/dll/Hooks/ExecuteProcess.cpp b/src/dll/Hooks/ExecuteProcess.cpp index 8dea7e85..4bc7c1e5 100644 --- a/src/dll/Hooks/ExecuteProcess.cpp +++ b/src/dll/Hooks/ExecuteProcess.cpp @@ -95,7 +95,7 @@ bool ExecuteScc(SccApi& scc) std::filesystem::path blobPath = scriptSystem->HasScriptsBlob() ? scriptSystem->GetScriptsBlob() : App::Get()->GetPaths()->GetDefaultScriptBundleFile(); - auto outputCacheFile = blobPath.replace_extension("redscripts.modded"); + auto moddedCacheFile = blobPath.replace_extension("redscripts.modded"); if (scriptSystem->HasScriptsBlob()) { @@ -104,7 +104,7 @@ bool ExecuteScc(SccApi& scc) if (settings.SupportsOutputCacheFileParameter()) { - settings.SetOutputCacheFile(outputCacheFile); + settings.SetOutputCacheFile(moddedCacheFile); } for (const auto& [_, path] : scriptSystem->GetScriptPaths()) @@ -162,9 +162,9 @@ bool ExecuteScc(SccApi& scc) if (settings.SupportsOutputCacheFileParameter()) { - scriptSystem->SetScriptsBlob(outputCacheFile); - engine->scriptsBlobPath = Utils::Narrow(outputCacheFile.c_str()); - spdlog::info(L"script blob path was updated to '{}'", outputCacheFile); + scriptSystem->SetModdedScriptsBlob(moddedCacheFile); + engine->scriptsBlobPath = Utils::Narrow(moddedCacheFile.c_str()); + spdlog::info(L"Scripts blob path was updated to '{}'", moddedCacheFile); } return true; diff --git a/src/dll/Hooks/LoadScripts.cpp b/src/dll/Hooks/LoadScripts.cpp index be17b852..15b14d49 100644 --- a/src/dll/Hooks/LoadScripts.cpp +++ b/src/dll/Hooks/LoadScripts.cpp @@ -16,16 +16,12 @@ bool _CBaseEngine_LoadScripts(RED4ext::CBaseEngine* aEngine, const RED4ext::CStr uint64_t a4) { auto scriptCompilationSystem = App::Get()->GetScriptCompilationSystem(); - const auto& scriptsBlobPath = scriptCompilationSystem->GetScriptsBlob(); + const auto& scriptsBlobPath = + scriptCompilationSystem->HasModdedScriptsBlob() ? scriptCompilationSystem->GetModdedScriptsBlob().string() + : scriptCompilationSystem->HasScriptsBlob() ? scriptCompilationSystem->GetScriptsBlob().string() + : aPath; - if (!scriptsBlobPath.empty()) - { - return CBaseEngine_LoadScripts(aEngine, scriptsBlobPath.string(), aTimestamp, a4); - } - else - { - return CBaseEngine_LoadScripts(aEngine, aPath, aTimestamp, a4); - } + return CBaseEngine_LoadScripts(aEngine, scriptsBlobPath, aTimestamp, a4); } } // namespace diff --git a/src/dll/Systems/ScriptCompilationSystem.cpp b/src/dll/Systems/ScriptCompilationSystem.cpp index eb502f8e..7424972b 100644 --- a/src/dll/Systems/ScriptCompilationSystem.cpp +++ b/src/dll/Systems/ScriptCompilationSystem.cpp @@ -42,6 +42,22 @@ bool ScriptCompilationSystem::HasScriptsBlob() const return m_hasScriptsBlob; } +void ScriptCompilationSystem::SetModdedScriptsBlob(const std::filesystem::path& aPath) +{ + m_moddedScriptsBlobPath = aPath; + m_hasModdedScriptsBlob = true; +} + +const std::filesystem::path& ScriptCompilationSystem::GetModdedScriptsBlob() const +{ + return m_moddedScriptsBlobPath; +} + +bool ScriptCompilationSystem::HasModdedScriptsBlob() const +{ + return m_hasModdedScriptsBlob; +} + bool ScriptCompilationSystem::Add(std::shared_ptr aPlugin, const wchar_t* aPath) { spdlog::trace(L"Adding path to script compilation: '{}'", aPath); diff --git a/src/dll/Systems/ScriptCompilationSystem.hpp b/src/dll/Systems/ScriptCompilationSystem.hpp index 3011e3fe..a2525c94 100644 --- a/src/dll/Systems/ScriptCompilationSystem.hpp +++ b/src/dll/Systems/ScriptCompilationSystem.hpp @@ -32,6 +32,10 @@ class ScriptCompilationSystem : public ISystem const std::filesystem::path& GetScriptsBlob() const; bool HasScriptsBlob() const; + void SetModdedScriptsBlob(const std::filesystem::path& aPath); + const std::filesystem::path& GetModdedScriptsBlob() const; + bool HasModdedScriptsBlob() const; + std::wstring GetCompilationArgs(const FixedWString& aOriginal); const Map_t& GetScriptPaths() const; @@ -46,5 +50,7 @@ class ScriptCompilationSystem : public ISystem Map_t m_scriptPaths; bool m_hasScriptsBlob; std::filesystem::path m_scriptsBlobPath; + bool m_hasModdedScriptsBlob; + std::filesystem::path m_moddedScriptsBlobPath; SourceRefRepository m_sourceRefs; }; From 36ef6f21d4b7b8cb450bcd116b13701d9da2fafe Mon Sep 17 00:00:00 2001 From: jekky <11986158+jac3km4@users.noreply.github.com> Date: Fri, 8 Mar 2024 01:33:23 +0000 Subject: [PATCH 3/3] chore: rename for consistency --- src/dll/Hooks/ExecuteProcess.cpp | 5 ++--- src/dll/Paths.cpp | 2 +- src/dll/Paths.hpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/dll/Hooks/ExecuteProcess.cpp b/src/dll/Hooks/ExecuteProcess.cpp index 4bc7c1e5..06c9a2cd 100644 --- a/src/dll/Hooks/ExecuteProcess.cpp +++ b/src/dll/Hooks/ExecuteProcess.cpp @@ -92,9 +92,8 @@ bool ExecuteScc(SccApi& scc) ScriptCompilerSettings settings(scc, App::Get()->GetPaths()->GetR6Dir()); - std::filesystem::path blobPath = scriptSystem->HasScriptsBlob() - ? scriptSystem->GetScriptsBlob() - : App::Get()->GetPaths()->GetDefaultScriptBundleFile(); + std::filesystem::path blobPath = scriptSystem->HasScriptsBlob() ? scriptSystem->GetScriptsBlob() + : App::Get()->GetPaths()->GetDefaultScriptsBlob(); auto moddedCacheFile = blobPath.replace_extension("redscripts.modded"); if (scriptSystem->HasScriptsBlob()) diff --git a/src/dll/Paths.cpp b/src/dll/Paths.cpp index 4958b705..64920d2b 100644 --- a/src/dll/Paths.cpp +++ b/src/dll/Paths.cpp @@ -58,7 +58,7 @@ std::filesystem::path Paths::GetR6Scripts() const return GetRootDir() / L"r6" / L"scripts"; } -std::filesystem::path Paths::GetDefaultScriptBundleFile() const +std::filesystem::path Paths::GetDefaultScriptsBlob() const { return GetRootDir() / L"r6" / L"cache" / "final.redscripts"; } diff --git a/src/dll/Paths.hpp b/src/dll/Paths.hpp index a59187cf..45b2fc92 100644 --- a/src/dll/Paths.hpp +++ b/src/dll/Paths.hpp @@ -17,7 +17,7 @@ class Paths std::filesystem::path GetRedscriptPathsFile() const; std::filesystem::path GetR6Scripts() const; - std::filesystem::path GetDefaultScriptBundleFile() const; + std::filesystem::path GetDefaultScriptsBlob() const; std::filesystem::path GetR6CacheModded() const; std::filesystem::path GetR6Dir() const;