From 6b95d2deee25b68752af91efa4579e75db709f14 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Mon, 2 Dec 2024 01:13:39 +0100 Subject: [PATCH 1/2] refactor: move enabledmods.json path in header --- primedev/mods/modmanager.cpp | 8 +++++--- primedev/mods/modmanager.h | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/primedev/mods/modmanager.cpp b/primedev/mods/modmanager.cpp index 75bd43291..49b945ab7 100644 --- a/primedev/mods/modmanager.cpp +++ b/primedev/mods/modmanager.cpp @@ -22,6 +22,8 @@ ModManager* g_pModManager; ModManager::ModManager() { + cfgPath = GetNorthstarPrefix() + "/enabledmods.json"; + // precaculated string hashes // note: use backslashes for these, since we use lexically_normal for file paths which uses them m_hScriptsRsonHash = STR_HASH("scripts\\vscripts\\scripts.rson"); @@ -120,7 +122,7 @@ void ModManager::LoadMods() m_DependencyConstants.clear(); // read enabled mods cfg - std::ifstream enabledModsStream(GetNorthstarPrefix() + "/enabledmods.json"); + std::ifstream enabledModsStream(cfgPath); std::stringstream enabledModsStringStream; if (!enabledModsStream.fail()) @@ -421,7 +423,7 @@ void ModManager::LoadMods() // If there are new mods, we write entries accordingly in enabledmods.json if (newModsDetected) { - std::ofstream writeStream(GetNorthstarPrefix() + "/enabledmods.json"); + std::ofstream writeStream(cfgPath); rapidjson::OStreamWrapper writeStreamWrapper(writeStream); rapidjson::PrettyWriter writer(writeStreamWrapper); m_EnabledModsCfg.Accept(writer); @@ -511,7 +513,7 @@ void ModManager::UnloadMods() m_EnabledModsCfg[mod.Name.c_str()].SetBool(mod.m_bEnabled); } - std::ofstream writeStream(GetNorthstarPrefix() + "/enabledmods.json"); + std::ofstream writeStream(cfgPath); rapidjson::OStreamWrapper writeStreamWrapper(writeStream); rapidjson::PrettyWriter writer(writeStreamWrapper); m_EnabledModsCfg.Accept(writer); diff --git a/primedev/mods/modmanager.h b/primedev/mods/modmanager.h index 58ab319d7..7e5db4ca1 100644 --- a/primedev/mods/modmanager.h +++ b/primedev/mods/modmanager.h @@ -34,6 +34,7 @@ class ModManager bool m_bHasLoadedMods = false; bool m_bHasEnabledModsCfg; rapidjson_document m_EnabledModsCfg; + std::string cfgPath; // precalculated hashes size_t m_hScriptsRsonHash; From ae35548030133a2c0eaec8649a89ec399d6af3b6 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Mon, 2 Dec 2024 01:29:04 +0100 Subject: [PATCH 2/2] refactor: move mod info export code to dedicated method --- primedev/mods/modmanager.cpp | 37 +++++++++++++++++++++++------------- primedev/mods/modmanager.h | 11 +++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/primedev/mods/modmanager.cpp b/primedev/mods/modmanager.cpp index 49b945ab7..2b1d4b9eb 100644 --- a/primedev/mods/modmanager.cpp +++ b/primedev/mods/modmanager.cpp @@ -502,21 +502,10 @@ void ModManager::UnloadMods() fs::remove(GetCompiledAssetsPath() / fs::path(kvPaths.second).lexically_relative(mod.m_ModDirectory)); mod.KeyValues.clear(); - - // write to m_enabledModsCfg - // should we be doing this here or should scripts be doing this manually? - // main issue with doing this here is when we reload mods for connecting to a server, we write enabled mods, which isn't necessarily - // what we wanna do - if (!m_EnabledModsCfg.HasMember(mod.Name.c_str())) - m_EnabledModsCfg.AddMember(rapidjson_document::StringRefType(mod.Name.c_str()), false, m_EnabledModsCfg.GetAllocator()); - - m_EnabledModsCfg[mod.Name.c_str()].SetBool(mod.m_bEnabled); } - std::ofstream writeStream(cfgPath); - rapidjson::OStreamWrapper writeStreamWrapper(writeStream); - rapidjson::PrettyWriter writer(writeStreamWrapper); - m_EnabledModsCfg.Accept(writer); + // save mods configuration to disk + ExportModsConfigurationToFile(); // do we need to dealloc individual entries in m_loadedMods? idk, rework m_LoadedMods.clear(); @@ -630,6 +619,28 @@ void ModManager::SearchFilesystemForMods() std::sort(m_LoadedMods.begin(), m_LoadedMods.end(), [](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; }); } +void ModManager::ExportModsConfigurationToFile() +{ + m_EnabledModsCfg.SetObject(); + + for (Mod& mod : m_LoadedMods) + { + // write to m_enabledModsCfg + // should we be doing this here or should scripts be doing this manually? + // main issue with doing this here is when we reload mods for connecting to a server, we write enabled mods, which isn't necessarily + // what we wanna do + if (!m_EnabledModsCfg.HasMember(mod.Name.c_str())) + m_EnabledModsCfg.AddMember(rapidjson_document::StringRefType(mod.Name.c_str()), false, m_EnabledModsCfg.GetAllocator()); + + m_EnabledModsCfg[mod.Name.c_str()].SetBool(mod.m_bEnabled); + } + + std::ofstream writeStream(cfgPath); + rapidjson::OStreamWrapper writeStreamWrapper(writeStream); + rapidjson::PrettyWriter writer(writeStreamWrapper); + m_EnabledModsCfg.Accept(writer); +} + std::string ModManager::NormaliseModFilePath(const fs::path path) { std::string str = path.lexically_normal().string(); diff --git a/primedev/mods/modmanager.h b/primedev/mods/modmanager.h index 7e5db4ca1..403a5c48d 100644 --- a/primedev/mods/modmanager.h +++ b/primedev/mods/modmanager.h @@ -48,6 +48,17 @@ class ModManager std::unordered_set m_PluginDependencyConstants; private: + /** + * Saves mod enabled state to enabledmods.json file. + * + * This loops over loaded mods (stored in `m_LoadedMods` list), exports their + * state (enabled or disabled) to a local JSON document, then exports this + * document to local profile. + * + * @returns nothing + **/ + void ExportModsConfigurationToFile(); + /** * Load information for all mods from filesystem. *