diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index 671fb3a0..d8589229 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -454,6 +454,7 @@ namespace SabreTools.DatFiles /// True if the DAT is already sorted accordingly, false otherwise (default) /// List of matched DatItem objects /// This also sets the remove flag on any duplicates found + /// TODO: Figure out if removal should be a flag or just removed entirely internal List GetDuplicates(DatItem datItem, bool sorted = false) { // Check for an empty rom list first @@ -464,33 +465,27 @@ namespace SabreTools.DatFiles string key = SortAndGetKey(datItem, sorted); // Get the items for the current key, if possible - List roms = GetItemsForBucket(key, filter: false); - if (roms.Count == 0) + List items = GetItemsForBucket(key, filter: false); + if (items.Count == 0) return []; - // Remove the current key - RemoveBucket(key); - // Try to find duplicates List output = []; - for (int i = 0; i < roms.Count; i++) + foreach (DatItem other in items) { - DatItem other = roms[i]; + // Skip items marked for removal if (other.GetBoolFieldValue(DatItem.RemoveKey) == true) - { - AddItem(key, other); continue; - } + // Mark duplicates for future removal if (datItem.Equals(other)) { other.SetFieldValue(DatItem.RemoveKey, true); output.Add(other); } - - AddItem(key, other); } + // Return any matching items return output; } diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index 53ecad1e..38aead22 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -727,49 +727,38 @@ namespace SabreTools.DatFiles /// True if the DAT is already sorted accordingly, false otherwise (default) /// List of matched DatItem objects /// This also sets the remove flag on any duplicates found + /// TODO: Figure out if removal should be a flag or just removed entirely internal Dictionary GetDuplicates(KeyValuePair datItem, bool sorted = false) { - Dictionary output = []; - // Check for an empty rom list first if (DatStatistics.TotalCount == 0) - return output; + return []; // We want to get the proper key for the DatItem string key = SortAndGetKey(datItem, sorted); // If the key doesn't exist, return the empty list - var roms = GetItemsForBucket(key); - if (roms == null || roms.Count == 0) - return output; + var items = GetItemsForBucket(key); + if (items.Count == 0) + return []; // Try to find duplicates - Dictionary left = []; - foreach (var rom in roms) + Dictionary output = []; + foreach (var rom in items) { + // Skip items marked for removal if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true) - { - left[rom.Key] = rom.Value; continue; - } + // Mark duplicates for future removal if (datItem.Value.Equals(rom.Value)) { rom.Value.SetFieldValue(DatItem.RemoveKey, true); output[rom.Key] = rom.Value; } - else - { - left[rom.Key] = rom.Value; - } } - // Add back all roms with the proper flags -#if NET40_OR_GREATER || NETCOREAPP - _buckets.TryAdd(key, [.. output.Keys, .. left.Keys]); -#else - _buckets[key] = [.. output.Keys, .. left.Keys]; -#endif + // Return any matching items return output; }