fix: Multiple versions of a same mod won't crash the client anymore (#852)

* If a mod is installed several times, all mod versions are disabled to prevent crashing


Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com>
This commit is contained in:
Rémy Raes 2025-06-02 17:52:04 +02:00 committed by GitHub
parent 3a8aa058bf
commit 79e8790c4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 0 deletions

View file

@ -172,6 +172,9 @@ void ModManager::LoadMods()
// Load mod info from filesystem into `m_LoadedMods`
SearchFilesystemForMods();
// Do not activate the same mod multiple times
DisableMultipleModVersions();
// This is used to check if some mods have a folder but no entry in enabledmods.json
bool newModsDetected = false;
@ -693,6 +696,50 @@ void ModManager::SearchFilesystemForMods()
std::sort(m_LoadedMods.begin(), m_LoadedMods.end(), [](Mod& a, Mod& b) { return a.LoadPriority < b.LoadPriority; });
}
void ModManager::DisableMultipleModVersions()
{
// Stores versions, for each mod, associated to their position in the `m_LoadedMods` array, *e.g.*:
//
// {
// "Northstar.Client": [ {"1.30.2", 0} ],
// "Northstar.Custom": [ {"1.30.2", 1} ],
// "Northstar.CustomServers": [ {"1.30.2", 2} ],
// "Extraction": [ {"1.2.0", 3}, {"1.2.1", 4}, {"1.3.0", 5} ]
// }
//
std::unordered_map<std::string, std::vector<std::tuple<const char*, int>>> modVersions;
// Load up the dictionary
int i = 0;
for (Mod& mod : m_LoadedMods)
{
// Store versions for enabled mods only, as disabled mods are not loaded and won't collide
if (mod.m_bEnabled)
{
modVersions[mod.Name].push_back({mod.Version.c_str(), i});
}
i++;
}
// Find duplicate mods and disable them
for (const auto& pair : modVersions)
{
if (pair.second.size() <= 1)
{
continue;
}
spdlog::warn("Mod '{}' has several versions enabled, disabling them all.", pair.first);
for (auto& [version, versionIndex] : pair.second)
{
m_LoadedMods[versionIndex].m_bEnabled = false;
spdlog::warn(" -> v{} is now disabled.", version);
}
}
}
void ModManager::ExportModsConfigurationToFile()
{
m_EnabledModsCfg.SetObject();

View file

@ -70,6 +70,20 @@ private:
**/
void SearchFilesystemForMods();
/**
* Prevents crashes caused by mods being installed several times.
*
* Whether through manual install or remote mod downloading, several versions of
* a same mod can be located in the current profile: enabling all of them would
* lead to a crash, due to some files loaded several times.
*
* This checks the local `m_LoadedMods` mods list for multiple versions of a
* same mod: if so, this disables all versions of the relevant mod.
*
* @returns nothing
**/
void DisableMultipleModVersions();
public:
ModManager();
void LoadMods();