diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index 5a6116b1..eaa8e9b1 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -608,6 +608,52 @@ namespace SabreTools.DatFiles } } + /// + /// List all duplicates found in a DAT based on a DatItem + /// + /// Item to try to match + /// True if the DAT is already sorted accordingly, false otherwise (default) + /// List of matched DatItem objects + public ConcurrentList<(long, DatItem)> GetDuplicates(DatItem datItem, bool sorted = false) + { + ConcurrentList<(long, DatItem)> output = []; + + // Check for an empty rom list first + if (DatStatistics.TotalCount == 0) + return output; + + // 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.Length == 0) + return output; + + // Try to find duplicates + ConcurrentList<(long, DatItem)> left = []; + for (int i = 0; i < roms.Length; i++) + { + DatItem other = roms[i].Item2; + if (other.GetBoolFieldValue(DatItem.RemoveKey) == true) + continue; + + if (datItem.Equals(other)) + { + other.SetFieldValue(DatItem.RemoveKey, true); + output.Add(other); + } + else + { + left.Add(other); + } + } + + // Add back all roms with the proper flags + _buckets[key] = output.Concat(left).Select(i => i.Item1).ToConcurrentList(); + return output; + } + /// /// List all duplicates found in a DAT based on a DatItem /// @@ -1129,6 +1175,22 @@ namespace SabreTools.DatFiles return true; } + /// + /// Sort the input DAT and get the key to be used by the item + /// + /// Item to try to match + /// True if the DAT is already sorted accordingly, false otherwise (default) + /// Key to try to use + private string SortAndGetKey(DatItem datItem, bool sorted = false) + { + // If we're not already sorted, take care of it + if (!sorted) + BucketBy(GetBestAvailable(), DedupeType.None); + + // Now that we have the sorted type, we get the proper key + return datItem.GetKey(_bucketedBy, null); + } + /// /// Sort the input DAT and get the key to be used by the item ///