diff --git a/SabreTools.DatFiles.Test/ItemDictionaryTests.cs b/SabreTools.DatFiles.Test/ItemDictionaryTests.cs
index e0a87afc..2dcd6532 100644
--- a/SabreTools.DatFiles.Test/ItemDictionaryTests.cs
+++ b/SabreTools.DatFiles.Test/ItemDictionaryTests.cs
@@ -438,7 +438,7 @@ namespace SabreTools.DatFiles.Test
var dict = new ItemDictionary();
dict.AddItem(datItem, statsOnly: false);
- dict.RemoveItem("game-1", (Rom)datItem.Clone());
+ dict.RemoveItem("game-1", (Rom)datItem.Clone(), 0);
Assert.Empty(dict.GetItemsForBucket("default"));
Assert.Empty(dict.GetItemsForBucket("game-1"));
diff --git a/SabreTools.DatFiles/DatFile.Splitting.cs b/SabreTools.DatFiles/DatFile.Splitting.cs
index cd968860..39df2264 100644
--- a/SabreTools.DatFiles/DatFile.Splitting.cs
+++ b/SabreTools.DatFiles/DatFile.Splitting.cs
@@ -1098,10 +1098,17 @@ namespace SabreTools.DatFiles
// If the parent exists and has items, we remove the parent items from the current game
foreach (DatItem item in parentItems)
{
- var matchedItems = items.FindAll(i => i.Equals(item));
- foreach (var match in matchedItems)
+ while (true)
{
- RemoveItem(bucket, match);
+ // Reset the items list each time an item is removed so the index is accurate
+ items = GetItemsForBucket(bucket);
+
+ // Find the next index that matches the item
+ int index = items.FindIndex(i => i.Equals(item));
+ if (index < 0)
+ break;
+
+ RemoveItem(bucket, items[index], index);
}
}
@@ -1199,13 +1206,20 @@ namespace SabreTools.DatFiles
if (parentItems.Count == 0)
continue;
- // If the parent exists and has items, we remove the items that are in the parent from the current game
+ // If the parent exists and has items, we remove the parent items from the current game
foreach (DatItem item in parentItems)
{
- var matchedItems = items.FindAll(i => i.Equals(item));
- foreach (var match in matchedItems)
+ while (true)
{
- RemoveItem(bucket, match);
+ // Reset the items list each time an item is removed so the index is accurate
+ items = GetItemsForBucket(bucket);
+
+ // Find the next index that matches the item
+ int index = items.FindIndex(i => i.Equals(item));
+ if (index < 0)
+ break;
+
+ RemoveItem(bucket, items[index], index);
}
}
}
diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs
index 3b17deaf..34bb4518 100644
--- a/SabreTools.DatFiles/DatFile.cs
+++ b/SabreTools.DatFiles/DatFile.cs
@@ -303,13 +303,14 @@ namespace SabreTools.DatFiles
}
///
- /// Remove the first instance of a value from the file dictionary if it exists
+ /// Remove the indexed instance of a value from the file dictionary if it exists
///
/// Key in the dictionary to remove from
/// Value to remove from the dictionary
- public bool RemoveItem(string key, DatItem value)
+ /// Index of the item to be removed
+ public bool RemoveItem(string key, DatItem value, int index)
{
- return Items.RemoveItem(key, value);
+ return Items.RemoveItem(key, value, index);
}
///
diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs
index 4cd32114..e42ed603 100644
--- a/SabreTools.DatFiles/ItemDictionary.cs
+++ b/SabreTools.DatFiles/ItemDictionary.cs
@@ -284,43 +284,7 @@ namespace SabreTools.DatFiles
}
///
- /// Remove the first instance of a value from the file dictionary if it exists
- ///
- /// Key in the dictionary to remove from
- /// Value to remove from the dictionary
- public bool RemoveItem(string key, DatItem value)
- {
- // Explicit lock for some weird corner cases
- lock (key)
- {
- // If the key doesn't exist, return
-#if NET40_OR_GREATER || NETCOREAPP
- if (!_items.TryGetValue(key, out var list) || list == null)
- return false;
-#else
- if (!_items.ContainsKey(key))
- return false;
-
- var list = _items[key];
- if (list == null)
- return false;
-#endif
-
- // If the value doesn't exist in the key, assume it has been removed
- int removeIndex = list.FindIndex(i => i.Equals(value));
- if (removeIndex < 0)
- return false;
-
- // Remove the statistics first
- DatStatistics.RemoveItemStatistics(value);
-
- list.RemoveAt(removeIndex);
- return true;
- }
- }
-
- ///
- /// Remove the first instance of a value from the file dictionary if it exists
+ /// Remove the indexed instance of a value from the file dictionary if it exists
///
/// Key in the dictionary to remove from
/// Value to remove from the dictionary