Skip to content

Commit

Permalink
SporeModLoader: add support for legacy dlls
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Oct 14, 2023
1 parent 28d4ca2 commit d40c1cb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 37 deletions.
26 changes: 15 additions & 11 deletions SporeModLoader/SporeModLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ bool SporeModLoader::Initialize()
try
{
Logger::Clear();

for (const auto& path : { Path::GetCoreLibsPath(), Path::GetModLibsPath() })


for (const auto& paths : { Path::GetCoreLibsPaths(), Path::GetModLibsPaths() })
{
if (!std::filesystem::is_directory(path))
for (const auto& path : paths)
{
std::wstring errorMessage;
errorMessage = L"\"";
errorMessage += path.wstring();
errorMessage += L"\" doesn't exist!";
UI::ShowErrorMessage(errorMessage);
throw std::exception();
if (!std::filesystem::exists(path))
{
std::wstring errorMessage;
errorMessage = L"\"";
errorMessage += path.wstring();
errorMessage += L"\" doesn't exist!";
UI::ShowErrorMessage(errorMessage);
throw std::exception();
}
}
}

Expand All @@ -53,7 +57,7 @@ bool SporeModLoader::LoadCoreLibs()
try
{
Logger::AddMessage(L"SporeModLoader::LoadCoreLibs()");
if (!Library::LoadAllInPath(Path::GetCoreLibsPath()))
if (!Library::LoadAll(Path::GetCoreLibsPaths()))
{
throw std::exception();
}
Expand All @@ -73,7 +77,7 @@ bool SporeModLoader::LoadModLibs()
try
{
Logger::AddMessage(L"SporeModLoader::LoadModLibs()");
if (!Library::LoadAllInPath(Path::GetModLibsPath()))
if (!Library::LoadAll(Path::GetModLibsPaths()))
{
throw std::exception();
}
Expand Down
98 changes: 78 additions & 20 deletions SporeModLoader/SporeModLoaderHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ std::filesystem::path Path::GetLogFilePath(void)
return logFilePath;
}

std::filesystem::path Path::GetCoreLibsPath(void)
std::vector<std::filesystem::path> Path::GetCoreLibsPaths(void)
{
std::vector<std::filesystem::path> coreLibsPaths;
std::string legacyLibFile;
std::filesystem::path legacyLibPath;
std::filesystem::path coreLibPath;
std::filesystem::path coreLibsPath;

coreLibsPath = GetModLoaderPath();
Expand All @@ -70,31 +74,93 @@ std::filesystem::path Path::GetCoreLibsPath(void)
throw std::exception();

case Game::GameVersion::Origin_1_5_1:
case Game::GameVersion::Origin_March2017:
UI::ShowErrorMessage(L"Unsupported Spore version!");
throw std::exception();

case Game::GameVersion::GogOrSteam_1_5_1:
UI::ShowErrorMessage(L"Update Spore to the latest version!");
throw std::exception();

case Game::GameVersion::Origin_March2017:
case Game::GameVersion::GogOrSteam_March2017:
coreLibsPath += "\\march2017";
coreLibsPath += "\\march2017\\";
legacyLibFile = "SporeModAPI-steam_patched.dll";
break;

case Game::GameVersion::Disk_1_5_1:
coreLibsPath += "\\disk";
coreLibsPath += "\\disk\\";
legacyLibFile = "SporeModAPI-disk.dll";
break;
}

return coreLibsPath;
coreLibPath = coreLibsPath;
coreLibPath += "SporeModAPI.dll";

legacyLibPath = coreLibsPath;
legacyLibPath += legacyLibFile;

coreLibsPaths.push_back(coreLibPath);
coreLibsPaths.push_back(legacyLibPath);

return coreLibsPaths;
}

std::filesystem::path Path::GetModLibsPath(void)
std::vector<std::filesystem::path> Path::GetModLibsPaths(void)
{
std::vector<std::filesystem::path> modLibsPaths;
std::filesystem::path modLibsPath;
Game::GameVersion gameVersion;
std::vector<std::string> excludePostfixes;

modLibsPath = GetModLoaderPath();
modLibsPath += "\\ModLibs";

return modLibsPath;
// we have to create an exclusion list
// for the postfixes for the mod dlls
// due to support for the legacy ModAPI DLLs
// where games shipped a dll with either the
// -steam, -steam_patched or -disk postfix
gameVersion = Game::GetCurrentVersion();
if (gameVersion == Game::GameVersion::GogOrSteam_March2017)
{
excludePostfixes.push_back("-steam.dll");
excludePostfixes.push_back("-disk.dll");
}
else
{
excludePostfixes.push_back("-steam.dll");
excludePostfixes.push_back("-steam_patched.dll");
}

for (const auto& entry : std::filesystem::recursive_directory_iterator(modLibsPath))
{
// skip non-files & non-dlls
if (!entry.is_regular_file() ||
!entry.path().has_extension() ||
entry.path().extension() != ".dll")
{
continue;
}

// ensure we have an allowed postfix
bool skipLib = false;
for (const auto& postFix : excludePostfixes)
{
if (entry.path().filename().string().ends_with(postFix))
{
skipLib = true;
break;
}
}


if (!skipLib)
{
modLibsPaths.push_back(entry.path());
}
}

return modLibsPaths;
}

void Logger::Clear(void)
Expand Down Expand Up @@ -137,24 +203,16 @@ void UI::ShowErrorMessage(std::wstring message)
MessageBoxW(nullptr, message.c_str(), L"SporeModLoader", MB_OK | MB_ICONERROR);
}

bool Library::LoadAllInPath(std::filesystem::path path)
bool Library::LoadAll(std::vector<std::filesystem::path> paths)
{
for (const auto& entry : std::filesystem::recursive_directory_iterator(path))
for (const auto& path : paths)
{
// skip non-files & non-dlls
if (!entry.is_regular_file() ||
!entry.path().has_extension() ||
entry.path().extension() != ".dll")
{
continue;
}

// attempt to load library
bool ret = LoadLibraryW(entry.path().wstring().c_str()) != nullptr;
bool ret = LoadLibraryW(path.wstring().c_str()) != nullptr;

std::wstring logMessage;
logMessage = L"LoadLibraryW(\"";
logMessage += entry.path().wstring();
logMessage += path.wstring();
logMessage += L"\") == ";
logMessage += std::to_wstring((ret ? 1 : GetLastError()));
Logger::AddMessage(logMessage);
Expand All @@ -163,7 +221,7 @@ bool Library::LoadAllInPath(std::filesystem::path path)
{
std::wstring errorMessage;
errorMessage = L"LoadLibraryW(\"";
errorMessage += entry.path().wstring();
errorMessage += path.wstring();
errorMessage += L"\") Failed!";
UI::ShowErrorMessage(errorMessage);
return false;
Expand Down
13 changes: 7 additions & 6 deletions SporeModLoader/SporeModLoaderHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <string>
#include <filesystem>
#include <vector>

namespace SporeModLoaderHelpers
{
Expand All @@ -33,14 +34,14 @@ namespace SporeModLoaderHelpers
std::filesystem::path GetLogFilePath(void);

/// <summary>
/// Returns path to the core libs
/// Returns paths to the core libs
/// </summary>
std::filesystem::path GetCoreLibsPath(void);
std::vector<std::filesystem::path> GetCoreLibsPaths(void);

/// <summary>
/// Returns path to the mod libs
/// Returns paths to the mod libs
/// </summary>
std::filesystem::path GetModLibsPath(void);
std::vector<std::filesystem::path> GetModLibsPaths(void);
}

namespace Logger
Expand All @@ -67,9 +68,9 @@ namespace SporeModLoaderHelpers
namespace Library
{
/// <summary>
/// Loads all DLL files that are in path
/// Loads all specified DLL files
/// </summary>
bool LoadAllInPath(std::filesystem::path path);
bool LoadAll(std::vector<std::filesystem::path> paths);
}

namespace Game
Expand Down

0 comments on commit d40c1cb

Please sign in to comment.