From a040bff98c16b79a3e817af441a795cf4f407069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Raes?= Date: Mon, 9 Oct 2023 01:34:47 +0200 Subject: [PATCH] Create mod entry in `enabledmods.json` if it doesn't exist (#410) 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. --- NorthstarDLL/mods/modmanager.cpp | 22 ++++++++++++++++++++++ NorthstarDLL/mods/modmanager.h | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/NorthstarDLL/mods/modmanager.cpp b/NorthstarDLL/mods/modmanager.cpp index 1ca51ad7a..8ad832c1d 100644 --- a/NorthstarDLL/mods/modmanager.cpp +++ b/NorthstarDLL/mods/modmanager.cpp @@ -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; } @@ -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 @@ -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 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--) { diff --git a/NorthstarDLL/mods/modmanager.h b/NorthstarDLL/mods/modmanager.h index 15367e4da..813edec79 100644 --- a/NorthstarDLL/mods/modmanager.h +++ b/NorthstarDLL/mods/modmanager.h @@ -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: