From aa3ee64526343b7eadbf36f4c3cce97d6b7297be Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Tue, 30 Sep 2025 12:06:14 -0400 Subject: [PATCH] Prevent `key has already been added` exception when checking for duplicate mods and building `modPackageData` --- SPTarkov.Server/Modding/ModValidator.cs | 15 ++++++++------- Testing/TestMod/TestMod.csproj | 8 ++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/SPTarkov.Server/Modding/ModValidator.cs b/SPTarkov.Server/Modding/ModValidator.cs index 81631708..8b08a066 100644 --- a/SPTarkov.Server/Modding/ModValidator.cs +++ b/SPTarkov.Server/Modding/ModValidator.cs @@ -23,11 +23,11 @@ public class ModValidator(ISptLogger logger, ServerLocalisationSer // Validate and remove broken mods from mod list var validMods = GetValidMods(mods).ToList(); // ToList now so we can .Sort later + CheckForDuplicateMods(validMods); + // Key to guid for easy comparision later var modPackageData = validMods.ToDictionary(m => m.ModMetadata.ModGuid, m => m.ModMetadata); - CheckForDuplicateMods(modPackageData); - // Used to check all errors before stopping the load execution var errorsFound = false; @@ -82,19 +82,20 @@ public class ModValidator(ISptLogger logger, ServerLocalisationSer /// /// Check for duplicate mods loaded, show error if any /// - /// Dictionary of mod package.json data - protected void CheckForDuplicateMods(Dictionary modPackageData) + /// List of validated mods to check for duplicates + protected void CheckForDuplicateMods(List validMods) { var groupedMods = new Dictionary>(); - foreach (var mod in modPackageData.Values) + foreach (var mod in validMods.Select(mod => mod.ModMetadata).ToArray()) { - groupedMods.Add(mod.ModGuid, [.. groupedMods.GetValueOrDefault(mod.ModGuid) ?? [], mod]); + groupedMods[mod.ModGuid] = [.. groupedMods.GetValueOrDefault(mod.ModGuid) ?? [], mod]; - // if there's more than one entry for a given mod it means there's at least 2 mods with the same author and name trying to load. + // if there's more than one entry for a given mod it means there's at least 2 mods with the same GUID trying to load. if (groupedMods[mod.ModGuid].Count > 1) { SkippedMods.Add(mod.ModGuid); + validMods.RemoveAll(modInner => modInner.ModMetadata.ModGuid == mod.ModGuid); } } diff --git a/Testing/TestMod/TestMod.csproj b/Testing/TestMod/TestMod.csproj index cb614f39..459a5d64 100644 --- a/Testing/TestMod/TestMod.csproj +++ b/Testing/TestMod/TestMod.csproj @@ -2,6 +2,7 @@ enable + TestMod2 @@ -16,10 +17,13 @@ - +