From 31b0a1e36fe6723ede7a7634e859fa7bb65fcb37 Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Sun, 24 Jul 2022 19:53:15 +0200 Subject: [PATCH] SporeModManager: introduce GetCoreLibFileVersionInfo() and CheckIfCoreLibMatchesVersion() --- SporeModManager/SporeModManager.cpp | 6 +++ SporeModManager/SporeModManagerHelpers.hpp | 10 ++++ .../SporeModManagerHelpers/FileVersion.cpp | 51 +++++++++++++++++++ .../SporeModManagerHelpers/SporeMod.cpp | 31 +---------- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/SporeModManager/SporeModManager.cpp b/SporeModManager/SporeModManager.cpp index d8f92b3..9262743 100644 --- a/SporeModManager/SporeModManager.cpp +++ b/SporeModManager/SporeModManager.cpp @@ -105,6 +105,12 @@ bool SporeModManager::UpdateMod(std::filesystem::path path) return false; } + // make sure we have the modapi dll that the mod requires + if (!FileVersion::CheckIfCoreLibMatchesVersion(sporeModInfo.MinimumModAPILibVersion, sporeModInfo.Name)) + { + return false; + } + installedSporeModUniqueName = sporeModInfo.UniqueName; free(modInfoFileBuffer); diff --git a/SporeModManager/SporeModManagerHelpers.hpp b/SporeModManager/SporeModManagerHelpers.hpp index ba1997e..8052b51 100644 --- a/SporeModManager/SporeModManagerHelpers.hpp +++ b/SporeModManager/SporeModManagerHelpers.hpp @@ -43,6 +43,16 @@ namespace SporeModManagerHelpers } }; + /// + /// Retrieves file version info for the core lib + /// + bool GetCoreLibFileVersionInfo(FileVersionInfo& fileVersionInfo); + + /// + /// Returns whether the core lib matches the version required by the mod + /// + bool CheckIfCoreLibMatchesVersion(FileVersionInfo& modFileVersionInfo, std::string modName); + /// /// Parses FileVersionInfo from string /// diff --git a/SporeModManager/SporeModManagerHelpers/FileVersion.cpp b/SporeModManager/SporeModManagerHelpers/FileVersion.cpp index 129f9ba..d3cdfbc 100644 --- a/SporeModManager/SporeModManagerHelpers/FileVersion.cpp +++ b/SporeModManager/SporeModManagerHelpers/FileVersion.cpp @@ -25,6 +25,57 @@ using namespace SporeModManagerHelpers; // Exported Functions // +bool FileVersion::GetCoreLibFileVersionInfo(FileVersionInfo& fileVersionInfo) +{ + std::filesystem::path coreLibPath; + static FileVersionInfo cachedFileVersionInfo = { 0 }; + static bool hasCachedFileVersionInfo = false; + + if (hasCachedFileVersionInfo) + { + fileVersionInfo = cachedFileVersionInfo; + return true; + } + + coreLibPath = Path::Combine({ Path::GetCoreLibsPath(), "march2017", "SporeModAPI.dll" }); + + if (!std::filesystem::is_regular_file(coreLibPath)) + { + std::cerr << "\"" << coreLibPath.string() << "\" doesn't exist!" << std::endl; + return false; + } + + if (!FileVersion::ParseFile(coreLibPath, fileVersionInfo)) + { + std::cerr << "FileVersion::ParseFile() Failed!" << std::endl; + return false; + } + + cachedFileVersionInfo = fileVersionInfo; + hasCachedFileVersionInfo = true; + return true; +} + +bool FileVersion::CheckIfCoreLibMatchesVersion(FileVersionInfo& modFileVersionInfo, std::string modName) +{ + FileVersionInfo coreLibFileVersionInfo; + + if (!GetCoreLibFileVersionInfo(coreLibFileVersionInfo)) + { + std::cerr << "FileVersion::GetCoreLibFileVersionInfo() Failed!" << std::endl; + return false; + } + + if (modFileVersionInfo > coreLibFileVersionInfo) + { + std::cerr << "\"" << modName << "\" requires newer modapi dll (\"" + modFileVersionInfo.string() << + "\") than what's currently installed (\"" << coreLibFileVersionInfo.string() << "\")" << std::endl; + return false; + } + + return true; +} + bool FileVersion::ParseString(std::string string, FileVersionInfo& fileVersionInfo) { std::vector splitString; diff --git a/SporeModManager/SporeModManagerHelpers/SporeMod.cpp b/SporeModManager/SporeModManagerHelpers/SporeMod.cpp index 5b1b1d8..e38abbb 100644 --- a/SporeModManager/SporeModManagerHelpers/SporeMod.cpp +++ b/SporeModManager/SporeModManagerHelpers/SporeMod.cpp @@ -41,35 +41,6 @@ static bool IsModAlreadyInstalled(std::string uniqueName) return false; } -static bool CheckModAPILibVersion(FileVersion::FileVersionInfo fileVersionInfo) -{ - std::filesystem::path coreLibPath; - FileVersion::FileVersionInfo coreLibFileVersionInfo; - - coreLibPath = Path::Combine({ Path::GetCoreLibsPath(), "march2017", "SporeModAPI.dll" }); - - if (!std::filesystem::is_regular_file(coreLibPath)) - { - std::cerr << "\"" << coreLibPath.string() << "\" doesn't exist!" << std::endl; - return false; - } - - if (!FileVersion::ParseFile(coreLibPath, coreLibFileVersionInfo)) - { - std::cerr << "FileVersion::ParseFile() Failed!" << std::endl; - return false; - } - - if (fileVersionInfo > coreLibFileVersionInfo) - { - std::cerr << "mod requires newer modapi dll (\"" + fileVersionInfo.string() << - "\") than what's currently installed (\"" << coreLibFileVersionInfo.string() << "\")" << std::endl; - return false; - } - - return true; -} - static bool CheckIfOtherModContainsFiles(std::vector sporeModFiles) { std::vector installedSporeMods; @@ -168,7 +139,7 @@ bool SporeMod::InstallSporeMod(std::filesystem::path path) } // make sure we have the modapi dll that the mod requires - if (!CheckModAPILibVersion(sporeModInfo.MinimumModAPILibVersion)) + if (!FileVersion::CheckIfCoreLibMatchesVersion(sporeModInfo.MinimumModAPILibVersion, sporeModInfo.Name)) { return false; }