Skip to content

Commit

Permalink
Create mod entry in enabledmods.json if it doesn't exist (#410)
Browse files Browse the repository at this point in the history
Currently, when you add a new mod to your `mods/` directory, when there's no associated entry in the `enabledmods.json` file, it is considered enabled by default; mod entries are only written in `enabledmods.json` when toggling them via the mods interface.

In this pull request, I propose to create mod entries in `enabledmods.json` on startup when detecting they don't exist.
  • Loading branch information
Alystrasz authored Oct 8, 2023
1 parent 70a0114 commit a040bff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions NorthstarDLL/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Mod::Mod(fs::path modDir, char* jsonBuf)
ParseDependencies(modJson);
ParseInitScript(modJson);

// A mod is remote if it's located in the remote mods folder
m_bIsRemote = m_ModDirectory.generic_string().find(GetRemoteModFolderPath().generic_string()) != std::string::npos;

m_bWasReadSuccessfully = true;
}

Expand Down Expand Up @@ -706,11 +709,21 @@ void ModManager::LoadMods()
// sort by load prio, lowest-highest
std::sort(m_LoadedMods.begin(), m_LoadedMods.end(), [](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; });

// This is used to check if some mods have a folder but no entry in enabledmods.json
bool newModsDetected = false;

for (Mod& mod : m_LoadedMods)
{
if (!mod.m_bEnabled)
continue;

// Add mod entry to enabledmods.json if it doesn't exist
if (!mod.m_bIsRemote && !m_EnabledModsCfg.HasMember(mod.Name.c_str()))
{
m_EnabledModsCfg.AddMember(rapidjson_document::StringRefType(mod.Name.c_str()), true, m_EnabledModsCfg.GetAllocator());
newModsDetected = true;
}

// register convars
// for reloads, this is sorta barebones, when we have a good findconvar method, we could probably reset flags and stuff on
// preexisting convars note: we don't delete convars if they already exist because they're used for script stuff, unfortunately this
Expand Down Expand Up @@ -940,6 +953,15 @@ void ModManager::LoadMods()
}
}

// If there are new mods, we write entries accordingly in enabledmods.json
if (newModsDetected)
{
std::ofstream writeStream(GetNorthstarPrefix() + "/enabledmods.json");
rapidjson::OStreamWrapper writeStreamWrapper(writeStream);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(writeStreamWrapper);
m_EnabledModsCfg.Accept(writer);
}

// in a seperate loop because we register mod files in reverse order, since mods loaded later should have their files prioritised
for (int64_t i = m_LoadedMods.size() - 1; i > -1; i--)
{
Expand Down
2 changes: 1 addition & 1 deletion NorthstarDLL/mods/modmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Mod
bool m_bEnabled = true;
bool m_bWasReadSuccessfully = false;
fs::path m_ModDirectory;
// bool m_bIsRemote;
bool m_bIsRemote;

// mod.json stuff:

Expand Down

0 comments on commit a040bff

Please sign in to comment.