diff options
author | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-09-30 21:46:08 +0100 |
---|---|---|
committer | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-09-30 21:46:08 +0100 |
commit | 1ff8e068e62d095775ef2ae54d3685d89238f28c (patch) | |
tree | 5aedcc16f144c2225d7398dd1f19b4f11cd467c1 /NorthstarDedicatedTest/modmanager.cpp | |
parent | 8b93a09f5b12ce7967df5824794ed24b3ff31cdc (diff) | |
download | NorthstarLauncher-1ff8e068e62d095775ef2ae54d3685d89238f28c.tar.gz NorthstarLauncher-1ff8e068e62d095775ef2ae54d3685d89238f28c.zip |
small modmanager refactors
Diffstat (limited to 'NorthstarDedicatedTest/modmanager.cpp')
-rw-r--r-- | NorthstarDedicatedTest/modmanager.cpp | 121 |
1 files changed, 59 insertions, 62 deletions
diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp index f5deed1a..c687f70f 100644 --- a/NorthstarDedicatedTest/modmanager.cpp +++ b/NorthstarDedicatedTest/modmanager.cpp @@ -11,7 +11,7 @@ #include <sstream> #include <vector> -ModManager* g_ModManager; +std::unique_ptr<ModManager> g_ModManager; Mod::Mod(fs::path modDir, char* jsonBuf) { @@ -85,20 +85,20 @@ Mod::Mod(fs::path modDir, char* jsonBuf) if (!convarObj.IsObject() || !convarObj.HasMember("Name") || !convarObj.HasMember("DefaultValue")) continue; - ModConVar* convar = new ModConVar; - convar->Name = convarObj["Name"].GetString(); - convar->DefaultValue = convarObj["DefaultValue"].GetString(); + ModConVar convar; + convar.Name = convarObj["Name"].GetString(); + convar.DefaultValue = convarObj["DefaultValue"].GetString(); if (convarObj.HasMember("HelpString")) - convar->HelpString = convarObj["HelpString"].GetString(); + convar.HelpString = convarObj["HelpString"].GetString(); else - convar->HelpString = ""; + convar.HelpString = ""; // todo: could possibly parse FCVAR names here instead if (convarObj.HasMember("Flags")) - convar->Flags = convarObj["Flags"].GetInt(); + convar.Flags = convarObj["Flags"].GetInt(); else - convar->Flags = FCVAR_NONE; + convar.Flags = FCVAR_NONE; ConVars.push_back(convar); } @@ -112,51 +112,51 @@ Mod::Mod(fs::path modDir, char* jsonBuf) if (!scriptObj.IsObject() || !scriptObj.HasMember("Path") || !scriptObj.HasMember("RunOn")) continue; - ModScript* script = new ModScript; + ModScript script; - script->Path = scriptObj["Path"].GetString(); - script->RsonRunOn = scriptObj["RunOn"].GetString(); + script.Path = scriptObj["Path"].GetString(); + script.RsonRunOn = scriptObj["RunOn"].GetString(); if (scriptObj.HasMember("ServerCallback") && scriptObj["ServerCallback"].IsObject()) { - ModScriptCallback* callback = new ModScriptCallback; - callback->Context = SERVER; + ModScriptCallback callback; + callback.Context = SERVER; if (scriptObj["ServerCallback"].HasMember("Before") && scriptObj["ServerCallback"]["Before"].IsString()) - callback->BeforeCallback = scriptObj["ServerCallback"]["Before"].GetString(); + callback.BeforeCallback = scriptObj["ServerCallback"]["Before"].GetString(); if (scriptObj["ServerCallback"].HasMember("After") && scriptObj["ServerCallback"]["After"].IsString()) - callback->AfterCallback = scriptObj["ServerCallback"]["After"].GetString(); + callback.AfterCallback = scriptObj["ServerCallback"]["After"].GetString(); - script->Callbacks.push_back(callback); + script.Callbacks.push_back(callback); } if (scriptObj.HasMember("ClientCallback") && scriptObj["ClientCallback"].IsObject()) { - ModScriptCallback* callback = new ModScriptCallback; - callback->Context = CLIENT; + ModScriptCallback callback; + callback.Context = CLIENT; if (scriptObj["ClientCallback"].HasMember("Before") && scriptObj["ClientCallback"]["Before"].IsString()) - callback->BeforeCallback = scriptObj["ClientCallback"]["Before"].GetString(); + callback.BeforeCallback = scriptObj["ClientCallback"]["Before"].GetString(); if (scriptObj["ClientCallback"].HasMember("After") && scriptObj["ClientCallback"]["After"].IsString()) - callback->AfterCallback = scriptObj["ClientCallback"]["After"].GetString(); + callback.AfterCallback = scriptObj["ClientCallback"]["After"].GetString(); - script->Callbacks.push_back(callback); + script.Callbacks.push_back(callback); } if (scriptObj.HasMember("UICallback") && scriptObj["UICallback"].IsObject()) { - ModScriptCallback* callback = new ModScriptCallback; - callback->Context = UI; + ModScriptCallback callback; + callback.Context = UI; if (scriptObj["UICallback"].HasMember("Before") && scriptObj["UICallback"]["Before"].IsString()) - callback->BeforeCallback = scriptObj["UICallback"]["Before"].GetString(); + callback.BeforeCallback = scriptObj["UICallback"]["Before"].GetString(); if (scriptObj["UICallback"].HasMember("After") && scriptObj["UICallback"]["After"].IsString()) - callback->AfterCallback = scriptObj["UICallback"]["After"].GetString(); + callback.AfterCallback = scriptObj["UICallback"]["After"].GetString(); - script->Callbacks.push_back(callback); + script.Callbacks.push_back(callback); } Scripts.push_back(script); @@ -214,41 +214,38 @@ void ModManager::LoadMods() jsonStream.close(); - Mod* mod = new Mod(modDir, (char*)jsonStringStream.str().c_str()); + Mod mod = Mod(modDir, (char*)jsonStringStream.str().c_str()); - if (mod->wasReadSuccessfully) + if (mod.wasReadSuccessfully) { - spdlog::info("Loaded mod {} successfully", mod->Name); + spdlog::info("Loaded mod {} successfully", mod.Name); m_loadedMods.push_back(mod); } else - { spdlog::warn("Skipping loading mod file {}", (modDir / "mod.json").string()); - delete mod; - } } // sort by load prio, lowest-highest - std::sort(m_loadedMods.begin(), m_loadedMods.end(), [](Mod* a, Mod* b) { - return a->LoadPriority > b->LoadPriority; + std::sort(m_loadedMods.begin(), m_loadedMods.end(), [](Mod a, Mod b) { + return a.LoadPriority > b.LoadPriority; }); - for (Mod* mod : m_loadedMods) + for (Mod mod : m_loadedMods) { - if (!mod->Enabled) + if (!mod.Enabled) continue; // 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 // potentially it might also be good to unregister convars if they get removed on a reload, but unsure if necessary - for (ModConVar* convar : mod->ConVars) - if (g_CustomConvars.find(convar->Name) == g_CustomConvars.end()) // make sure convar isn't registered yet, unsure if necessary but idk what behaviour is for defining same convar multiple times - RegisterConVar(convar->Name.c_str(), convar->DefaultValue.c_str(), convar->Flags, convar->HelpString.c_str()); + for (ModConVar convar : mod.ConVars) + if (g_CustomConvars.find(convar.Name) == g_CustomConvars.end()) // make sure convar isn't registered yet, unsure if necessary but idk what behaviour is for defining same convar multiple times + RegisterConVar(convar.Name.c_str(), convar.DefaultValue.c_str(), convar.Flags, convar.HelpString.c_str()); // read vpk paths - if (fs::exists(mod->ModDirectory / "vpk")) + if (fs::exists(mod.ModDirectory / "vpk")) { - for (fs::directory_entry file : fs::directory_iterator(mod->ModDirectory / "vpk")) + for (fs::directory_entry file : fs::directory_iterator(mod.ModDirectory / "vpk")) { // a bunch of checks to make sure we're only adding dir vpks and their paths are good // note: the game will literally only load vpks with the english prefix @@ -258,22 +255,22 @@ void ModManager::LoadMods() std::string formattedPath = file.path().filename().string(); // this really fucking sucks but it'll work - mod->Vpks.push_back((file.path().parent_path() / formattedPath.substr(strlen("english"), formattedPath.find(".bsp") - 3)).string()); + mod.Vpks.push_back((file.path().parent_path() / formattedPath.substr(strlen("english"), formattedPath.find(".bsp") - 3)).string()); } } } // read keyvalues paths - if (fs::exists(mod->ModDirectory / "keyvalues")) + if (fs::exists(mod.ModDirectory / "keyvalues")) { - for (fs::directory_entry file : fs::recursive_directory_iterator(mod->ModDirectory / "keyvalues")) + for (fs::directory_entry file : fs::recursive_directory_iterator(mod.ModDirectory / "keyvalues")) { if (fs::is_regular_file(file)) { - std::string kvStr = file.path().lexically_relative(mod->ModDirectory / "keyvalues").lexically_normal().string(); - mod->KeyValuesHash.push_back(std::hash<std::string>{}(kvStr)); - mod->KeyValues.push_back(kvStr); + std::string kvStr = file.path().lexically_relative(mod.ModDirectory / "keyvalues").lexically_normal().string(); + mod.KeyValuesHash.push_back(std::hash<std::string>{}(kvStr)); + mod.KeyValues.push_back(kvStr); } } } @@ -282,20 +279,20 @@ void ModManager::LoadMods() // in a seperate loop because we register mod files in reverse order, since mods loaded later should have their files prioritised for (int i = m_loadedMods.size() - 1; i > -1; i--) { - if (!m_loadedMods[i]->Enabled) + if (!m_loadedMods[i].Enabled) continue; - if (fs::exists(m_loadedMods[i]->ModDirectory / MOD_OVERRIDE_DIR)) + if (fs::exists(m_loadedMods[i].ModDirectory / MOD_OVERRIDE_DIR)) { - for (fs::directory_entry file : fs::recursive_directory_iterator(m_loadedMods[i]->ModDirectory / MOD_OVERRIDE_DIR)) + for (fs::directory_entry file : fs::recursive_directory_iterator(m_loadedMods[i].ModDirectory / MOD_OVERRIDE_DIR)) { - fs::path path = file.path().lexically_relative(m_loadedMods[i]->ModDirectory / MOD_OVERRIDE_DIR).lexically_normal(); + fs::path path = file.path().lexically_relative(m_loadedMods[i].ModDirectory / MOD_OVERRIDE_DIR).lexically_normal(); if (file.is_regular_file() && m_modFiles.find(path.string()) == m_modFiles.end()) { - ModOverrideFile* modFile = new ModOverrideFile; - modFile->owningMod = m_loadedMods[i]; - modFile->path = path; + ModOverrideFile modFile; + modFile.owningMod = &m_loadedMods[i]; + modFile.path = path; m_modFiles.insert(std::make_pair(path.string(), modFile)); } } @@ -311,14 +308,14 @@ void ModManager::UnloadMods() m_modFiles.clear(); fs::remove_all(COMPILED_ASSETS_PATH); - for (Mod* mod : m_loadedMods) + for (Mod mod : m_loadedMods) { // remove all built kvs - for (std::string kvPaths : mod->KeyValues) - fs::remove(COMPILED_ASSETS_PATH / fs::path(kvPaths).lexically_relative(mod->ModDirectory)); + for (std::string kvPaths : mod.KeyValues) + fs::remove(COMPILED_ASSETS_PATH / fs::path(kvPaths).lexically_relative(mod.ModDirectory)); - mod->KeyValuesHash.clear(); - mod->KeyValues.clear(); + mod.KeyValuesHash.clear(); + mod.KeyValues.clear(); } // do we need to dealloc individual entries in m_loadedMods? idk, rework @@ -334,10 +331,10 @@ void ModManager::CompileAssetsForFile(const char* filename) else //if (!strcmp((filename + strlen(filename)) - 3, "txt")) // check if it's a .txt { // check if we should build keyvalues, depending on whether any of our mods have patch kvs for this file - for (Mod* mod : m_loadedMods) + for (Mod mod : m_loadedMods) { size_t fileHash = std::hash<std::string>{}(fs::path(filename).lexically_normal().string()); - if (std::find(mod->KeyValuesHash.begin(), mod->KeyValuesHash.end(), fileHash) != mod->KeyValuesHash.end()) + if (std::find(mod.KeyValuesHash.begin(), mod.KeyValuesHash.end(), fileHash) != mod.KeyValuesHash.end()) { TryBuildKeyValues(filename); return; @@ -353,7 +350,7 @@ void ReloadModsCommand(const CCommand& args) void InitialiseModManager(HMODULE baseAddress) { - g_ModManager = new ModManager(); + g_ModManager = std::make_unique<ModManager>(); RegisterConCommand("reload_mods", ReloadModsCommand, "idk", FCVAR_NONE); }
\ No newline at end of file |