Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Mod information file export #832

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions primedev/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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<rapidjson::OStreamWrapper> writer(writeStreamWrapper);
m_EnabledModsCfg.Accept(writer);
Expand Down Expand Up @@ -500,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(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 @@ -628,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does m_EnabledModsCfg.SetObject(); have to be set again here?

(I don't think I fully understand what that line does... ^^)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It resets the state of the m_EnabledModsCfg object (same as dict = {} in js for instance)


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<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
12 changes: 12 additions & 0 deletions primedev/mods/modmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +48,17 @@ class ModManager
std::unordered_set<std::string> 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.
*
Expand Down
Loading