From d52f139e739824dfae4b651e5cf0882ab2f73afe Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 14 Jan 2025 21:02:37 -0500 Subject: [PATCH] Add new methods to DB implementation --- SabreTools.DatFiles/DatFile.Filtering.cs | 2 +- SabreTools.DatFiles/DatFile.Splitting.cs | 49 ++++++++++++------------ SabreTools.DatFiles/ItemDictionaryDB.cs | 23 ++++++++--- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/SabreTools.DatFiles/DatFile.Filtering.cs b/SabreTools.DatFiles/DatFile.Filtering.cs index 6838f1e4..000851c1 100644 --- a/SabreTools.DatFiles/DatFile.Filtering.cs +++ b/SabreTools.DatFiles/DatFile.Filtering.cs @@ -609,7 +609,7 @@ namespace SabreTools.DatFiles return; // Reassign the item to the new machine - ItemsDB._itemToMachineMapping[datItem.Key] = newMachineIndex; + ItemsDB.RemapDatItemToMachine(datItem.Key, newMachineIndex); // Remove extensions from File and Rom items if (datItem.Value is DatItems.Formats.File || datItem.Value is Rom) diff --git a/SabreTools.DatFiles/DatFile.Splitting.cs b/SabreTools.DatFiles/DatFile.Splitting.cs index d3228667..f9bba9d1 100644 --- a/SabreTools.DatFiles/DatFile.Splitting.cs +++ b/SabreTools.DatFiles/DatFile.Splitting.cs @@ -240,7 +240,7 @@ namespace SabreTools.DatFiles /// private void AddItemsFromChildrenImpl(bool subfolder, bool skipDedup) { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -395,6 +395,9 @@ namespace SabreTools.DatFiles items = GetItemsForBucketDB(bucket); foreach (var item in items) { + // Get the source for the current item + var source = ItemsDB.GetSourceForItem(item.Key); + // Get the parent items and current machine name Dictionary parentItems = GetItemsForBucketDB(cloneOf); if (parentItems.Count == 0) @@ -423,15 +426,15 @@ namespace SabreTools.DatFiles .Select(i => (i as Disk)!.GetName()) .Contains(mergeTag)) { - ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; - ItemsDB._buckets[cloneOf!].Add(item.Key); + ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key); + ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key); } // If there is no merge tag, add to parent else if (mergeTag == null) { - ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; - ItemsDB._buckets[cloneOf!].Add(item.Key); + ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key); + ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key); } } @@ -456,8 +459,8 @@ namespace SabreTools.DatFiles if (subfolder) rom.SetName($"{machineName}\\{rom.GetName()}"); - ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; - ItemsDB._buckets[cloneOf!].Add(item.Key); + ItemsDB.RemapDatItemToMachine(item.Key, machineIndex: cloneOfMachine.Key); + ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key); } // If the parent doesn't already contain this item, add to subfolder of parent @@ -466,8 +469,8 @@ namespace SabreTools.DatFiles if (subfolder) rom.SetName($"{machineName}\\{rom.GetName()}"); - ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; - ItemsDB._buckets[cloneOf!].Add(item.Key); + ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key); + ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key); } } @@ -477,17 +480,13 @@ namespace SabreTools.DatFiles if (subfolder) item.Value.SetName($"{machineName}\\{item.Value.GetName()}"); - ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; - ItemsDB._buckets[cloneOf!].Add(item.Key); + ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key); + ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key); } - } - // Then, remove the old game so it's not picked up by the writer -#if NET40_OR_GREATER || NETCOREAPP - ItemsDB._buckets.TryRemove(bucket, out _); -#else - ItemsDB._buckets.Remove(bucket); -#endif + // Remove the current item + ItemsDB.RemoveItem(item.Key); + } } } @@ -500,7 +499,7 @@ namespace SabreTools.DatFiles /// private void AddItemsFromCloneOfParentImpl() { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -619,7 +618,7 @@ namespace SabreTools.DatFiles private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions) { bool foundnew = false; - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket doesn't have items List datItems = GetItemsForBucket(bucket); @@ -928,7 +927,7 @@ namespace SabreTools.DatFiles /// private void AddItemsFromRomOfParentImpl() { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -1011,7 +1010,7 @@ namespace SabreTools.DatFiles /// private void RemoveBiosAndDeviceSetsImpl() { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -1078,7 +1077,7 @@ namespace SabreTools.DatFiles /// private void RemoveItemsFromCloneOfChildImpl() { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -1182,7 +1181,7 @@ namespace SabreTools.DatFiles private void RemoveItemsFromRomOfChildImpl() { // Loop through the romof tags - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it List items = GetItemsForBucket(bucket); @@ -1260,7 +1259,7 @@ namespace SabreTools.DatFiles /// Applies to private void RemoveMachineRelationshipTagsImpl() { - foreach (string bucket in Items.SortedKeys) + foreach (string bucket in Items.SortedKeys) { // If the bucket has no items in it var items = GetItemsForBucket(bucket); diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index 700835cc..da86ff8b 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -95,12 +95,11 @@ namespace SabreTools.DatFiles /// /// Internal dictionary for item to machine mappings /// - /// TODO: Make private when access issues are figured out [JsonIgnore, XmlIgnore] #if NET40_OR_GREATER || NETCOREAPP - internal readonly ConcurrentDictionary _itemToMachineMapping = []; + private readonly ConcurrentDictionary _itemToMachineMapping = []; #else - internal readonly Dictionary _itemToMachineMapping = []; + private readonly Dictionary _itemToMachineMapping = []; #endif /// @@ -116,12 +115,11 @@ namespace SabreTools.DatFiles /// /// Internal dictionary representing the current buckets /// - /// TODO: Make private when access issues are figured out [JsonIgnore, XmlIgnore] #if NET40_OR_GREATER || NETCOREAPP - internal readonly ConcurrentDictionary> _buckets = []; + private readonly ConcurrentDictionary> _buckets = []; #else - internal readonly Dictionary> _buckets = []; + private readonly Dictionary> _buckets = []; #endif /// @@ -572,6 +570,19 @@ namespace SabreTools.DatFiles /// public IDictionary GetSources() => _sources; + /// + /// Remap an item to a new machine index without validation + /// + /// Current item index + /// New machine index + public void RemapDatItemToMachine(long itemIndex, long machineIndex) + { + lock (_itemToMachineMapping) + { + _itemToMachineMapping[itemIndex] = machineIndex; + } + } + /// /// Remove an item, returning if it could be removed ///