diff --git a/primedev/mods/modmanager.cpp b/primedev/mods/modmanager.cpp index 2c8897bad..0b55c40ed 100644 --- a/primedev/mods/modmanager.cpp +++ b/primedev/mods/modmanager.cpp @@ -1065,21 +1065,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(GetNorthstarPrefix() + "/enabledmods.json"); - 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(); @@ -1195,6 +1184,32 @@ 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) + { + // Creating mod key (with name) + 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()].SetObject(); + } + + // Creating version key + if (!m_EnabledModsCfg[mod.Name.c_str()].HasMember(mod.Version.c_str())) + m_EnabledModsCfg[mod.Name.c_str()].AddMember( + rapidjson_document::StringRefType(mod.Version.c_str()), false, m_EnabledModsCfg.GetAllocator()); + m_EnabledModsCfg[mod.Name.c_str()][mod.Version.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();