Skip to content

Commit

Permalink
SporeModManager: introduce GetCoreLibFileVersionInfo() and CheckIfCor…
Browse files Browse the repository at this point in the history
…eLibMatchesVersion()
  • Loading branch information
Rosalie241 committed Jul 24, 2022
1 parent b044199 commit 31b0a1e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
6 changes: 6 additions & 0 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions SporeModManager/SporeModManagerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ namespace SporeModManagerHelpers
}
};

/// <summary>
/// Retrieves file version info for the core lib
/// </summary>
bool GetCoreLibFileVersionInfo(FileVersionInfo& fileVersionInfo);

/// <summary>
/// Returns whether the core lib matches the version required by the mod
/// </summary>
bool CheckIfCoreLibMatchesVersion(FileVersionInfo& modFileVersionInfo, std::string modName);

/// <summary>
/// Parses FileVersionInfo from string
/// </summary>
Expand Down
51 changes: 51 additions & 0 deletions SporeModManager/SporeModManagerHelpers/FileVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> splitString;
Expand Down
31 changes: 1 addition & 30 deletions SporeModManager/SporeModManagerHelpers/SporeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SporeMod::Xml::SporeModFile> sporeModFiles)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 31b0a1e

Please sign in to comment.