Fix some issues with DB RemoveItem

This commit is contained in:
Matt Nadareski
2025-01-16 15:37:11 -05:00
parent 34929b02d7
commit 9c1c063b79

View File

@@ -516,7 +516,6 @@ namespace SabreTools.DatFiles
#endif #endif
RemoveItem(index); RemoveItem(index);
DatStatistics.RemoveItemStatistics(datItem);
} }
return removed; return removed;
@@ -527,22 +526,38 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
public bool RemoveItem(long itemIndex) public bool RemoveItem(long itemIndex)
{ {
// If the key doesn't exist, return
#if NET40_OR_GREATER || NETCOREAPP
if (!_items.TryRemove(itemIndex, out var datItem))
return false;
#else
if (!_items.ContainsKey(itemIndex)) if (!_items.ContainsKey(itemIndex))
return false; return false;
#if NET40_OR_GREATER || NETCOREAPP var datItem = _items[itemIndex];
_items.TryRemove(itemIndex, out _);
#else
_items.Remove(itemIndex); _items.Remove(itemIndex);
#endif #endif
if (_itemToMachineMapping.ContainsKey(itemIndex)) // Remove statistics, if possible
if (datItem != null)
DatStatistics.RemoveItemStatistics(datItem);
// Remove the machine mapping
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
_itemToMachineMapping.TryRemove(itemIndex, out _); _itemToMachineMapping.TryRemove(itemIndex, out _);
#else #else
if (_itemToMachineMapping.ContainsKey(itemIndex))
_itemToMachineMapping.Remove(itemIndex); _itemToMachineMapping.Remove(itemIndex);
#endif #endif
// Remove the source mapping
#if NET40_OR_GREATER || NETCOREAPP
_itemToSourceMapping.TryRemove(itemIndex, out _);
#else
if (_itemToSourceMapping.ContainsKey(itemIndex))
_itemToSourceMapping.Remove(itemIndex);
#endif
return true; return true;
} }