Skip to content

Commit

Permalink
Merge pull request #54 from jac3km4/cache-output-file
Browse files Browse the repository at this point in the history
feat: write modded redscripts to a separate file
  • Loading branch information
WopsS authored Mar 8, 2024
2 parents 400f7db + 36ef6f2 commit 503e668
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 15 deletions.
29 changes: 24 additions & 5 deletions src/dll/Hooks/ExecuteProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,25 @@ 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()->GetDefaultScriptsBlob();
auto moddedCacheFile = blobPath.replace_extension("redscripts.modded");

ScriptCompilerSettings settings(scc, r6Dir);
if (scriptSystem->HasScriptsBlob())
{
settings.SetCustomCacheFile(scriptSystem->GetScriptsBlob());
settings.SetCustomCacheFile(blobPath);
}

if (settings.SupportsOutputCacheFileParameter())
{
settings.SetOutputCacheFile(moddedCacheFile);
}

for (const auto& [_, path] : scriptSystem->GetScriptPaths())
{
settings.AddScriptPath(path);
Expand All @@ -104,7 +115,7 @@ bool ExecuteScc(SccApi& scc)

if (const auto error = std::get_if<ScriptCompilerFailure>(&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;
}
Expand Down Expand Up @@ -146,6 +157,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->SetModdedScriptsBlob(moddedCacheFile);
engine->scriptsBlobPath = Utils::Narrow(moddedCacheFile.c_str());
spdlog::info(L"Scripts blob path was updated to '{}'", moddedCacheFile);
}

return true;
}
14 changes: 5 additions & 9 deletions src/dll/Hooks/LoadScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/dll/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ std::filesystem::path Paths::GetR6Scripts() const
return GetRootDir() / L"r6" / L"scripts";
}

std::filesystem::path Paths::GetDefaultScriptsBlob() const
{
return GetRootDir() / L"r6" / L"cache" / "final.redscripts";
}

std::filesystem::path Paths::GetR6CacheModded() const
{
return GetRootDir() / L"r6" / L"cache" / L"modded";
Expand Down
1 change: 1 addition & 0 deletions src/dll/Paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Paths
std::filesystem::path GetRedscriptPathsFile() const;

std::filesystem::path GetR6Scripts() const;
std::filesystem::path GetDefaultScriptsBlob() const;
std::filesystem::path GetR6CacheModded() const;
std::filesystem::path GetR6Dir() const;

Expand Down
18 changes: 18 additions & 0 deletions src/dll/ScriptCompiler/ScriptCompilerSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -29,6 +41,12 @@ ScriptCompilerSettings::Result ScriptCompilerSettings::Compile()
m_scc.settings_set_custom_cache_file(settings, reinterpret_cast<const char*>(customCacheFileStr.c_str()));
}

if (SupportsOutputCacheFileParameter() && !m_outputCacheFile.empty())
{
auto outputCacheFileStr = m_outputCacheFile.u8string();
m_scc.settings_set_output_cache_file(settings, reinterpret_cast<const char*>(outputCacheFileStr.c_str()));
}

for (const auto& path : m_scriptPaths)
{
auto pathStr = path.u8string();
Expand Down
4 changes: 4 additions & 0 deletions src/dll/ScriptCompiler/ScriptCompilerSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ 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:
SccApi& m_scc;
std::filesystem::path m_r6Path;
std::vector<std::filesystem::path> m_scriptPaths;
std::filesystem::path m_customCacheFile;
std::filesystem::path m_outputCacheFile;
};
16 changes: 16 additions & 0 deletions src/dll/Systems/ScriptCompilationSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginBase> aPlugin, const wchar_t* aPath)
{
spdlog::trace(L"Adding path to script compilation: '{}'", aPath);
Expand Down
6 changes: 6 additions & 0 deletions src/dll/Systems/ScriptCompilationSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
};

0 comments on commit 503e668

Please sign in to comment.