Add new methods to DB implementation

This commit is contained in:
Matt Nadareski
2025-01-14 21:02:37 -05:00
parent bea43d38f4
commit d52f139e73
3 changed files with 42 additions and 32 deletions

View File

@@ -609,7 +609,7 @@ namespace SabreTools.DatFiles
return; return;
// Reassign the item to the new machine // Reassign the item to the new machine
ItemsDB._itemToMachineMapping[datItem.Key] = newMachineIndex; ItemsDB.RemapDatItemToMachine(datItem.Key, newMachineIndex);
// Remove extensions from File and Rom items // Remove extensions from File and Rom items
if (datItem.Value is DatItems.Formats.File || datItem.Value is Rom) if (datItem.Value is DatItems.Formats.File || datItem.Value is Rom)

View File

@@ -240,7 +240,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromChildrenImpl(bool subfolder, bool skipDedup) 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 // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -395,6 +395,9 @@ namespace SabreTools.DatFiles
items = GetItemsForBucketDB(bucket); items = GetItemsForBucketDB(bucket);
foreach (var item in items) 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 // Get the parent items and current machine name
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf); Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf);
if (parentItems.Count == 0) if (parentItems.Count == 0)
@@ -423,15 +426,15 @@ namespace SabreTools.DatFiles
.Select(i => (i as Disk)!.GetName()) .Select(i => (i as Disk)!.GetName())
.Contains(mergeTag)) .Contains(mergeTag))
{ {
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB._buckets[cloneOf!].Add(item.Key); ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
} }
// If there is no merge tag, add to parent // If there is no merge tag, add to parent
else if (mergeTag == null) else if (mergeTag == null)
{ {
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB._buckets[cloneOf!].Add(item.Key); ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
} }
} }
@@ -456,8 +459,8 @@ namespace SabreTools.DatFiles
if (subfolder) if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}"); rom.SetName($"{machineName}\\{rom.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; ItemsDB.RemapDatItemToMachine(item.Key, machineIndex: cloneOfMachine.Key);
ItemsDB._buckets[cloneOf!].Add(item.Key); ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
} }
// If the parent doesn't already contain this item, add to subfolder of parent // If the parent doesn't already contain this item, add to subfolder of parent
@@ -466,8 +469,8 @@ namespace SabreTools.DatFiles
if (subfolder) if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}"); rom.SetName($"{machineName}\\{rom.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB._buckets[cloneOf!].Add(item.Key); ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
} }
} }
@@ -477,17 +480,13 @@ namespace SabreTools.DatFiles
if (subfolder) if (subfolder)
item.Value.SetName($"{machineName}\\{item.Value.GetName()}"); item.Value.SetName($"{machineName}\\{item.Value.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key; ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB._buckets[cloneOf!].Add(item.Key); ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
} }
}
// Then, remove the old game so it's not picked up by the writer // Remove the current item
#if NET40_OR_GREATER || NETCOREAPP ItemsDB.RemoveItem(item.Key);
ItemsDB._buckets.TryRemove(bucket, out _); }
#else
ItemsDB._buckets.Remove(bucket);
#endif
} }
} }
@@ -500,7 +499,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromCloneOfParentImpl() private void AddItemsFromCloneOfParentImpl()
{ {
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket has no items in it // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -619,7 +618,7 @@ namespace SabreTools.DatFiles
private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions) private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions)
{ {
bool foundnew = false; bool foundnew = false;
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket doesn't have items // If the bucket doesn't have items
List<DatItem> datItems = GetItemsForBucket(bucket); List<DatItem> datItems = GetItemsForBucket(bucket);
@@ -928,7 +927,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromRomOfParentImpl() private void AddItemsFromRomOfParentImpl()
{ {
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket has no items in it // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -1011,7 +1010,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void RemoveBiosAndDeviceSetsImpl() private void RemoveBiosAndDeviceSetsImpl()
{ {
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket has no items in it // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -1078,7 +1077,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void RemoveItemsFromCloneOfChildImpl() private void RemoveItemsFromCloneOfChildImpl()
{ {
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket has no items in it // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -1182,7 +1181,7 @@ namespace SabreTools.DatFiles
private void RemoveItemsFromRomOfChildImpl() private void RemoveItemsFromRomOfChildImpl()
{ {
// Loop through the romof tags // 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 // If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket); List<DatItem> items = GetItemsForBucket(bucket);
@@ -1260,7 +1259,7 @@ namespace SabreTools.DatFiles
/// <remarks>Applies to <see cref="Items"/></remarks> /// <remarks>Applies to <see cref="Items"/></remarks>
private void RemoveMachineRelationshipTagsImpl() private void RemoveMachineRelationshipTagsImpl()
{ {
foreach (string bucket in Items.SortedKeys) foreach (string bucket in Items.SortedKeys)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = GetItemsForBucket(bucket); var items = GetItemsForBucket(bucket);

View File

@@ -95,12 +95,11 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Internal dictionary for item to machine mappings /// Internal dictionary for item to machine mappings
/// </summary> /// </summary>
/// TODO: Make private when access issues are figured out
[JsonIgnore, XmlIgnore] [JsonIgnore, XmlIgnore]
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
internal readonly ConcurrentDictionary<long, long> _itemToMachineMapping = []; private readonly ConcurrentDictionary<long, long> _itemToMachineMapping = [];
#else #else
internal readonly Dictionary<long, long> _itemToMachineMapping = []; private readonly Dictionary<long, long> _itemToMachineMapping = [];
#endif #endif
/// <summary> /// <summary>
@@ -116,12 +115,11 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Internal dictionary representing the current buckets /// Internal dictionary representing the current buckets
/// </summary> /// </summary>
/// TODO: Make private when access issues are figured out
[JsonIgnore, XmlIgnore] [JsonIgnore, XmlIgnore]
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
internal readonly ConcurrentDictionary<string, List<long>> _buckets = []; private readonly ConcurrentDictionary<string, List<long>> _buckets = [];
#else #else
internal readonly Dictionary<string, List<long>> _buckets = []; private readonly Dictionary<string, List<long>> _buckets = [];
#endif #endif
/// <summary> /// <summary>
@@ -572,6 +570,19 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
public IDictionary<long, Source> GetSources() => _sources; public IDictionary<long, Source> GetSources() => _sources;
/// <summary>
/// Remap an item to a new machine index without validation
/// </summary>
/// <param name="itemIndex">Current item index</param>
/// <param name="machineIndex">New machine index</param>
public void RemapDatItemToMachine(long itemIndex, long machineIndex)
{
lock (_itemToMachineMapping)
{
_itemToMachineMapping[itemIndex] = machineIndex;
}
}
/// <summary> /// <summary>
/// Remove an item, returning if it could be removed /// Remove an item, returning if it could be removed
/// </summary> /// </summary>