From 5e07df82e7bbd3296863995f688213e8639e8dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Raes?= Date: Sun, 1 Dec 2024 21:35:22 +0100 Subject: [PATCH 1/4] mods: Move collecting mods to own function (#830) Moves the logic that goes through all the paths where mods can be installed to its own function as part of improving code maintainability and future refactoring. --- primedev/mods/modmanager.cpp | 213 ++++++++++++++++++----------------- primedev/mods/modmanager.h | 12 ++ 2 files changed, 122 insertions(+), 103 deletions(-) diff --git a/primedev/mods/modmanager.cpp b/primedev/mods/modmanager.cpp index aa685c469..75bd43291 100644 --- a/primedev/mods/modmanager.cpp +++ b/primedev/mods/modmanager.cpp @@ -111,8 +111,6 @@ void ModManager::LoadMods() if (m_bHasLoadedMods) UnloadMods(); - std::vector modDirs; - // ensure dirs exist fs::remove_all(GetCompiledAssetsPath()); fs::create_directories(GetModFolderPath()); @@ -137,107 +135,8 @@ void ModManager::LoadMods() m_bHasEnabledModsCfg = m_EnabledModsCfg.IsObject(); } - // get mod directories - std::filesystem::directory_iterator classicModsDir = fs::directory_iterator(GetModFolderPath()); - std::filesystem::directory_iterator remoteModsDir = fs::directory_iterator(GetRemoteModFolderPath()); - std::filesystem::directory_iterator thunderstoreModsDir = fs::directory_iterator(GetThunderstoreModFolderPath()); - - for (fs::directory_entry dir : classicModsDir) - if (fs::exists(dir.path() / "mod.json")) - modDirs.push_back(dir.path()); - - // Special case for Thunderstore and remote mods directories - // Set up regex for `AUTHOR-MOD-VERSION` pattern - std::regex pattern(R"(.*\\([a-zA-Z0-9_]+)-([a-zA-Z0-9_]+)-(\d+\.\d+\.\d+))"); - - for (fs::directory_iterator dirIterator : {thunderstoreModsDir, remoteModsDir}) - { - for (fs::directory_entry dir : dirIterator) - { - fs::path modsDir = dir.path() / "mods"; // Check for mods folder in the Thunderstore mod - // Use regex to match `AUTHOR-MOD-VERSION` pattern - if (!std::regex_match(dir.path().string(), pattern)) - { - spdlog::warn("The following directory did not match 'AUTHOR-MOD-VERSION': {}", dir.path().string()); - continue; // skip loading mod that doesn't match - } - if (fs::exists(modsDir) && fs::is_directory(modsDir)) - { - for (fs::directory_entry subDir : fs::directory_iterator(modsDir)) - { - if (fs::exists(subDir.path() / "mod.json")) - { - modDirs.push_back(subDir.path()); - } - } - } - } - } - - for (fs::path modDir : modDirs) - { - // read mod json file - std::ifstream jsonStream(modDir / "mod.json"); - std::stringstream jsonStringStream; - - // fail if no mod json - if (jsonStream.fail()) - { - spdlog::warn( - "Mod file at '{}' does not exist or could not be read, is it installed correctly?", (modDir / "mod.json").string()); - continue; - } - - while (jsonStream.peek() != EOF) - jsonStringStream << (char)jsonStream.get(); - - jsonStream.close(); - - Mod mod(modDir, (char*)jsonStringStream.str().c_str()); - - for (auto& pair : mod.DependencyConstants) - { - if (m_DependencyConstants.find(pair.first) != m_DependencyConstants.end() && m_DependencyConstants[pair.first] != pair.second) - { - spdlog::error( - "'{}' attempted to register a dependency constant '{}' for '{}' that already exists for '{}'. " - "Change the constant name.", - mod.Name, - pair.first, - pair.second, - m_DependencyConstants[pair.first]); - mod.m_bWasReadSuccessfully = false; - break; - } - if (m_DependencyConstants.find(pair.first) == m_DependencyConstants.end()) - m_DependencyConstants.emplace(pair); - } - - for (std::string& dependency : mod.PluginDependencyConstants) - { - m_PluginDependencyConstants.insert(dependency); - } - - if (m_bHasEnabledModsCfg && m_EnabledModsCfg.HasMember(mod.Name.c_str())) - mod.m_bEnabled = m_EnabledModsCfg[mod.Name.c_str()].IsTrue(); - else - mod.m_bEnabled = true; - - if (mod.m_bWasReadSuccessfully) - { - if (mod.m_bEnabled) - spdlog::info("'{}' loaded successfully, version {}", mod.Name, mod.Version); - else - spdlog::info("'{}' loaded successfully, version {} (DISABLED)", mod.Name, mod.Version); - - m_LoadedMods.push_back(mod); - } - else - spdlog::warn("Mod file at '{}' failed to load", (modDir / "mod.json").string()); - } - - // sort by load prio, lowest-highest - std::sort(m_LoadedMods.begin(), m_LoadedMods.end(), [](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; }); + // Load mod info from filesystem into `m_LoadedMods` + SearchFilesystemForMods(); // This is used to check if some mods have a folder but no entry in enabledmods.json bool newModsDetected = false; @@ -621,6 +520,114 @@ void ModManager::UnloadMods() m_LoadedMods.clear(); } +void ModManager::SearchFilesystemForMods() +{ + std::vector modDirs; + m_LoadedMods.clear(); + + // get mod directories + std::filesystem::directory_iterator classicModsDir = fs::directory_iterator(GetModFolderPath()); + std::filesystem::directory_iterator remoteModsDir = fs::directory_iterator(GetRemoteModFolderPath()); + std::filesystem::directory_iterator thunderstoreModsDir = fs::directory_iterator(GetThunderstoreModFolderPath()); + + for (fs::directory_entry dir : classicModsDir) + if (fs::exists(dir.path() / "mod.json")) + modDirs.push_back(dir.path()); + + // Special case for Thunderstore and remote mods directories + // Set up regex for `AUTHOR-MOD-VERSION` pattern + std::regex pattern(R"(.*\\([a-zA-Z0-9_]+)-([a-zA-Z0-9_]+)-(\d+\.\d+\.\d+))"); + + for (fs::directory_iterator dirIterator : {thunderstoreModsDir, remoteModsDir}) + { + for (fs::directory_entry dir : dirIterator) + { + fs::path modsDir = dir.path() / "mods"; // Check for mods folder in the Thunderstore mod + // Use regex to match `AUTHOR-MOD-VERSION` pattern + if (!std::regex_match(dir.path().string(), pattern)) + { + spdlog::warn("The following directory did not match 'AUTHOR-MOD-VERSION': {}", dir.path().string()); + continue; // skip loading mod that doesn't match + } + if (fs::exists(modsDir) && fs::is_directory(modsDir)) + { + for (fs::directory_entry subDir : fs::directory_iterator(modsDir)) + { + if (fs::exists(subDir.path() / "mod.json")) + { + modDirs.push_back(subDir.path()); + } + } + } + } + } + + for (fs::path modDir : modDirs) + { + // read mod json file + std::ifstream jsonStream(modDir / "mod.json"); + std::stringstream jsonStringStream; + + // fail if no mod json + if (jsonStream.fail()) + { + spdlog::warn( + "Mod file at '{}' does not exist or could not be read, is it installed correctly?", (modDir / "mod.json").string()); + continue; + } + + while (jsonStream.peek() != EOF) + jsonStringStream << (char)jsonStream.get(); + + jsonStream.close(); + + Mod mod(modDir, (char*)jsonStringStream.str().c_str()); + + for (auto& pair : mod.DependencyConstants) + { + if (m_DependencyConstants.find(pair.first) != m_DependencyConstants.end() && m_DependencyConstants[pair.first] != pair.second) + { + spdlog::error( + "'{}' attempted to register a dependency constant '{}' for '{}' that already exists for '{}'. " + "Change the constant name.", + mod.Name, + pair.first, + pair.second, + m_DependencyConstants[pair.first]); + mod.m_bWasReadSuccessfully = false; + break; + } + if (m_DependencyConstants.find(pair.first) == m_DependencyConstants.end()) + m_DependencyConstants.emplace(pair); + } + + for (std::string& dependency : mod.PluginDependencyConstants) + { + m_PluginDependencyConstants.insert(dependency); + } + + if (m_bHasEnabledModsCfg && m_EnabledModsCfg.HasMember(mod.Name.c_str())) + mod.m_bEnabled = m_EnabledModsCfg[mod.Name.c_str()].IsTrue(); + else + mod.m_bEnabled = true; + + if (mod.m_bWasReadSuccessfully) + { + if (mod.m_bEnabled) + spdlog::info("'{}' loaded successfully, version {}", mod.Name, mod.Version); + else + spdlog::info("'{}' loaded successfully, version {} (DISABLED)", mod.Name, mod.Version); + + m_LoadedMods.push_back(mod); + } + else + spdlog::warn("Mod file at '{}' failed to load", (modDir / "mod.json").string()); + } + + // sort by load prio, lowest-highest + std::sort(m_LoadedMods.begin(), m_LoadedMods.end(), [](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; }); +} + std::string ModManager::NormaliseModFilePath(const fs::path path) { std::string str = path.lexically_normal().string(); diff --git a/primedev/mods/modmanager.h b/primedev/mods/modmanager.h index 6350a1fcd..58ab319d7 100644 --- a/primedev/mods/modmanager.h +++ b/primedev/mods/modmanager.h @@ -46,6 +46,18 @@ class ModManager std::unordered_map m_DependencyConstants; std::unordered_set m_PluginDependencyConstants; +private: + /** + * Load information for all mods from filesystem. + * + * This looks for mods in several directories (expecting them to be formatted in + * some way); it then uses respective `mod.json` manifest files to create `Mod` + * instances, which are then stored in the `m_LoadedMods` variable. + * + * @returns nothing + **/ + void SearchFilesystemForMods(); + public: ModManager(); void LoadMods(); From feab262e44372c937903bcc48aab360976fa24a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Raes?= Date: Fri, 6 Dec 2024 09:05:05 +0100 Subject: [PATCH 2/4] Rework MAD `cleanup` handles (#817) Mods are no longer marked as successfully installed when the process failed. When mod downloading/extraction fails, the mod directory is removed again. --- primedev/mods/autodownload/moddownloader.cpp | 57 +++++++++++++------- primedev/mods/autodownload/moddownloader.h | 14 ++--- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 21b98942a..de95a0c2c 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -176,7 +176,7 @@ int ModDownloader::ModFetchingProgressCallback( return 0; } -std::optional ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version) +std::tuple ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version) { std::string url = version.downloadLink; std::string archiveName = fs::path(url).filename().generic_string(); @@ -190,7 +190,7 @@ std::optional ModDownloader::FetchModFromDistantStore(std::string_view modState.state = DOWNLOADING; // Download the actual archive - bool failed = false; + bool success = false; FILE* fp = fopen(downloadPath.generic_string().c_str(), "wb"); CURLcode result; CURL* easyhandle; @@ -219,13 +219,14 @@ std::optional ModDownloader::FetchModFromDistantStore(std::string_view if (result == CURLcode::CURLE_OK) { spdlog::info("Mod archive successfully fetched."); - return std::optional(downloadPath); + success = true; } else { spdlog::error("Fetching mod archive failed: {}", curl_easy_strerror(result)); - return std::optional(); } + + return {downloadPath, success}; } bool ModDownloader::IsModLegit(fs::path modPath, std::string_view expectedChecksum) @@ -399,11 +400,9 @@ int GetModArchiveSize(unzFile file, unz_global_info64 info) return totalSize; } -void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform) +void ModDownloader::ExtractMod(fs::path modPath, fs::path destinationPath, VerifiedModPlatform platform) { unzFile file; - std::string name; - fs::path modDirectory; file = unzOpen(modPath.generic_string().c_str()); ScopeGuard cleanup( @@ -445,11 +444,6 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform) return; } - // Mod directory name (removing the ".zip" fom the archive name) - name = modPath.filename().string(); - name = name.substr(0, name.length() - 4); - modDirectory = GetRemoteModFolderPath() / name; - for (int i = 0; i < gi.number_entry; i++) { char zipFilename[256]; @@ -459,7 +453,7 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform) // Extract file { std::error_code ec; - fs::path fileDestination = modDirectory / zipFilename; + fs::path fileDestination = destinationPath / zipFilename; spdlog::info("=> {}", fileDestination.generic_string()); // Create parent directory if needed @@ -589,6 +583,9 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform) } } } + + // Mod extraction went fine + modState.state = DONE; } void ModDownloader::DownloadMod(std::string modName, std::string modVersion) @@ -603,11 +600,14 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) std::thread requestThread( [this, modName, modVersion]() { + std::string name; fs::path archiveLocation; + fs::path modDirectory; ScopeGuard cleanup( [&] { + // Remove downloaded archive try { remove(archiveLocation); @@ -617,14 +617,31 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) spdlog::error("Error while removing downloaded archive: {}", a.what()); } + // Remove mod if auto-download process failed + if (modState.state != DONE) + { + try + { + remove_all(modDirectory); + } + catch (const std::exception& e) + { + spdlog::error("Error while removing downloaded mod: {}", e.what()); + } + } + spdlog::info("Done cleaning after downloading {}.", modName); }); // Download mod archive VerifiedModVersion fullVersion = verifiedMods[modName].versions[modVersion]; std::string expectedHash = fullVersion.checksum; - std::optional fetchingResult = FetchModFromDistantStore(std::string_view(modName), fullVersion); - if (!fetchingResult.has_value()) + + const std::tuple downloadResult = FetchModFromDistantStore(std::string_view(modName), fullVersion); + archiveLocation = get<0>(downloadResult); + bool downloadSuccessful = get<1>(downloadResult); + + if (!downloadSuccessful) { spdlog::error("Something went wrong while fetching archive, aborting."); if (modState.state != ABORTED) @@ -633,7 +650,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) } return; } - archiveLocation = fetchingResult.value(); + if (!IsModLegit(archiveLocation, std::string_view(expectedHash))) { spdlog::warn("Archive hash does not match expected checksum, aborting."); @@ -641,9 +658,13 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) return; } + // Mod directory name (removing the ".zip" fom the archive name) + name = archiveLocation.filename().string(); + name = name.substr(0, name.length() - 4); + modDirectory = GetRemoteModFolderPath() / name; + // Extract downloaded mod archive - ExtractMod(archiveLocation, fullVersion.platform); - modState.state = DONE; + ExtractMod(archiveLocation, modDirectory, fullVersion.platform); }); requestThread.detach(); diff --git a/primedev/mods/autodownload/moddownloader.h b/primedev/mods/autodownload/moddownloader.h index 2ac72a48c..f8c0c6646 100644 --- a/primedev/mods/autodownload/moddownloader.h +++ b/primedev/mods/autodownload/moddownloader.h @@ -43,14 +43,12 @@ class ModDownloader * input mod name as mod dependency string if bypass flag is set up; fetched * archive is then stored in a temporary location. * - * If something went wrong during archive download, this will return an empty - * optional object. * * @param modName name of the mod to be downloaded * @param modVersion version of the mod to be downloaded - * @returns location of the downloaded archive + * @returns tuple containing location of the downloaded archive and whether download completed successfully */ - std::optional FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion); + std::tuple FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion); /** * Tells if a mod archive has not been corrupted. @@ -69,14 +67,16 @@ class ModDownloader /** * Extracts a mod archive to the game folder. * - * This extracts a downloaded mod archive from its original location to the - * current game profile; the install folder is defined by the platform parameter. + * This extracts a downloaded mod archive from its original location, `modPath`, + * to the specified `destinationPath`; the way to decompress the archive is + * defined by the `platform` parameter. * * @param modPath location of the downloaded archive + * @param destinationPath destination of the extraction * @param platform origin of the downloaded archive * @returns nothing */ - void ExtractMod(fs::path modPath, VerifiedModPlatform platform); + void ExtractMod(fs::path modPath, fs::path destinationPath, VerifiedModPlatform platform); public: ModDownloader(); From 104547893807b2e961bee0dbf457ef35bd7bf80c Mon Sep 17 00:00:00 2001 From: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Sun, 15 Dec 2024 22:52:23 +0000 Subject: [PATCH 3/4] scripts/client: Remove uses of Autohook from `scriptbrowserhooks.cpp` (#833) * Manually hook OpenExternalWebBrowser * Remove AUTOHOOK_INIT and AUTOHOOK_DISPATCH --- primedev/scripts/client/scriptbrowserhooks.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/primedev/scripts/client/scriptbrowserhooks.cpp b/primedev/scripts/client/scriptbrowserhooks.cpp index dcf051d22..3c4af1b57 100644 --- a/primedev/scripts/client/scriptbrowserhooks.cpp +++ b/primedev/scripts/client/scriptbrowserhooks.cpp @@ -1,25 +1,22 @@ -AUTOHOOK_INIT() - bool* bIsOriginOverlayEnabled; -// clang-format off -AUTOHOOK(OpenExternalWebBrowser, engine.dll + 0x184E40, -void, __fastcall, (char* pUrl, char flags)) -// clang-format on +static void(__fastcall* o_pOpenExternalWebBrowser)(char* pUrl, char flags) = nullptr; +static void __fastcall h_OpenExternalWebBrowser(char* pUrl, char flags) { bool bIsOriginOverlayEnabledOriginal = *bIsOriginOverlayEnabled; bool isHttp = !strncmp(pUrl, "http://", 7) || !strncmp(pUrl, "https://", 8); if (flags & 2 && isHttp) // custom force external browser flag *bIsOriginOverlayEnabled = false; // if this bool is false, game will use an external browser rather than the origin overlay one - OpenExternalWebBrowser(pUrl, flags); + o_pOpenExternalWebBrowser(pUrl, flags); *bIsOriginOverlayEnabled = bIsOriginOverlayEnabledOriginal; } ON_DLL_LOAD_CLIENT("engine.dll", ScriptExternalBrowserHooks, (CModule module)) { - AUTOHOOK_DISPATCH() + o_pOpenExternalWebBrowser = module.Offset(0x184E40).RCast(); + HookAttach(&(PVOID&)o_pOpenExternalWebBrowser, (PVOID)h_OpenExternalWebBrowser); bIsOriginOverlayEnabled = module.Offset(0x13978255).RCast(); } From f98f6b9acecf73742c4f894b626858933c59dc24 Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:45:03 -0500 Subject: [PATCH 4/4] Fix reloading plugins (#835) Individually reload all plugins instead of dropping them all and loading them all at once --- primedev/plugins/pluginmanager.cpp | 12 +++++++----- primedev/plugins/plugins.cpp | 14 +++++++++----- primedev/plugins/plugins.h | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/primedev/plugins/pluginmanager.cpp b/primedev/plugins/pluginmanager.cpp index 14d5692b3..7793cf260 100644 --- a/primedev/plugins/pluginmanager.cpp +++ b/primedev/plugins/pluginmanager.cpp @@ -1,6 +1,7 @@ #include "pluginmanager.h" #include +#include #include "plugins.h" #include "config/profile.h" #include "core/convar/concommand.h" @@ -111,13 +112,14 @@ bool PluginManager::LoadPlugins(bool reloaded) void PluginManager::ReloadPlugins() { - for (const Plugin& plugin : this->GetLoadedPlugins()) + NS::log::PLUGINSYS->info("Reloading plugins"); + + for (const Plugin& plugin : this->plugins | std::views::reverse) { - plugin.Unload(); + std::string name = plugin.GetName(); + if (plugin.Reload()) + NS::log::PLUGINSYS->info("Reloaded {}", name); } - - this->plugins.clear(); - this->LoadPlugins(true); } void PluginManager::RemovePlugin(HMODULE handle) diff --git a/primedev/plugins/plugins.cpp b/primedev/plugins/plugins.cpp index 3e6231676..8542f7ed6 100644 --- a/primedev/plugins/plugins.cpp +++ b/primedev/plugins/plugins.cpp @@ -138,9 +138,9 @@ bool Plugin::Unload() const if (IsValid()) { - bool unloaded = m_callbacks->Unload(); + bool shouldUnload = m_callbacks->Unload(); - if (!unloaded) + if (!shouldUnload) return false; } @@ -154,14 +154,18 @@ bool Plugin::Unload() const return true; } -void Plugin::Reload() const +bool Plugin::Reload() const { + std::string location = m_location; + bool unloaded = Unload(); if (!unloaded) - return; + return false; - g_pPluginManager->LoadPlugin(fs::path(m_location), true); + g_pPluginManager->LoadPlugin(fs::path(location), true); + + return true; } void Plugin::Log(spdlog::level::level_enum level, char* msg) const diff --git a/primedev/plugins/plugins.h b/primedev/plugins/plugins.h index 95ec08b5c..057c7438c 100644 --- a/primedev/plugins/plugins.h +++ b/primedev/plugins/plugins.h @@ -28,7 +28,7 @@ class Plugin Plugin(std::string path); bool Unload() const; - void Reload() const; + bool Reload() const; // sys void Log(spdlog::level::level_enum level, char* msg) const;