Fix GetDuplicates strange logic

This commit is contained in:
Matt Nadareski
2025-01-14 19:51:59 -05:00
parent 651802a776
commit 0e67113200
2 changed files with 17 additions and 33 deletions

View File

@@ -454,6 +454,7 @@ namespace SabreTools.DatFiles
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
/// <returns>List of matched DatItem objects</returns>
/// <remarks>This also sets the remove flag on any duplicates found</remarks>
/// TODO: Figure out if removal should be a flag or just removed entirely
internal List<DatItem> 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<DatItem> roms = GetItemsForBucket(key, filter: false);
if (roms.Count == 0)
List<DatItem> items = GetItemsForBucket(key, filter: false);
if (items.Count == 0)
return [];
// Remove the current key
RemoveBucket(key);
// Try to find duplicates
List<DatItem> 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<bool?>(DatItem.RemoveKey, true);
output.Add(other);
}
AddItem(key, other);
}
// Return any matching items
return output;
}

View File

@@ -727,49 +727,38 @@ namespace SabreTools.DatFiles
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
/// <returns>List of matched DatItem objects</returns>
/// <remarks>This also sets the remove flag on any duplicates found</remarks>
/// TODO: Figure out if removal should be a flag or just removed entirely
internal Dictionary<long, DatItem> GetDuplicates(KeyValuePair<long, DatItem> datItem, bool sorted = false)
{
Dictionary<long, DatItem> 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<long, DatItem> left = [];
foreach (var rom in roms)
Dictionary<long, DatItem> 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<bool?>(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;
}