Skip to content

Commit

Permalink
refactor: export mod configuration saving into ExportModsConfiguratio…
Browse files Browse the repository at this point in the history
…nToFile method
  • Loading branch information
Alystrasz committed Nov 23, 2024
1 parent b9d89c6 commit 18f6ada
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions primedev/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rapidjson::OStreamWrapper> 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();
Expand Down Expand Up @@ -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<rapidjson::OStreamWrapper> writer(writeStreamWrapper);
m_EnabledModsCfg.Accept(writer);
}

std::string ModManager::NormaliseModFilePath(const fs::path path)
{
std::string str = path.lexically_normal().string();
Expand Down

0 comments on commit 18f6ada

Please sign in to comment.