Replace more direct accessors to ItemDictionary

This commit is contained in:
Matt Nadareski
2025-01-13 09:37:59 -05:00
parent 3c4de2cc07
commit c94d8d712f
5 changed files with 30 additions and 29 deletions

View File

@@ -109,7 +109,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -124,7 +124,7 @@ namespace SabreTools.DatFiles
continue; continue;
// If the parent doesn't have any items, we want to continue // If the parent doesn't have any items, we want to continue
var parentItems = Items[romOf!]; var parentItems = GetItemsForBucket(romOf!);
if (parentItems == null || parentItems.Count == 0) if (parentItems == null || parentItems.Count == 0)
continue; continue;
@@ -205,7 +205,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -220,7 +220,7 @@ namespace SabreTools.DatFiles
continue; continue;
// Get the parent items // Get the parent items
var parentItems = Items[cloneOf!]; var parentItems = GetItemsForBucket(cloneOf!);
// Otherwise, move the items from the current game to a subfolder of the parent game // Otherwise, move the items from the current game to a subfolder of the parent game
DatItem copyFrom; DatItem copyFrom;
@@ -235,7 +235,7 @@ namespace SabreTools.DatFiles
copyFrom = parentItems[0]; copyFrom = parentItems[0];
} }
items = Items[bucket]; items = GetItemsForBucket(bucket);
foreach (DatItem item in items!) foreach (DatItem item in items!)
{ {
// Special disk handling // Special disk handling
@@ -244,7 +244,7 @@ namespace SabreTools.DatFiles
string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey); string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey);
// If the merge tag exists and the parent already contains it, skip // If the merge tag exists and the parent already contains it, skip
if (mergeTag != null && Items[cloneOf!]! if (mergeTag != null && GetItemsForBucket(cloneOf!)!
.FindAll(i => i is Disk) .FindAll(i => i is Disk)
.ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag)) .ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag))
{ {
@@ -252,7 +252,7 @@ namespace SabreTools.DatFiles
} }
// If the merge tag exists but the parent doesn't contain it, add to parent // If the merge tag exists but the parent doesn't contain it, add to parent
else if (mergeTag != null && !Items[cloneOf!]! else if (mergeTag != null && !GetItemsForBucket(cloneOf!)!
.FindAll(i => i is Disk) .FindAll(i => i is Disk)
.ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag)) .ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag))
{ {
@@ -272,7 +272,7 @@ namespace SabreTools.DatFiles
else if (item is Rom rom) else if (item is Rom rom)
{ {
// If the merge tag exists and the parent already contains it, skip // If the merge tag exists and the parent already contains it, skip
if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && Items[cloneOf!]! if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && GetItemsForBucket(cloneOf!)!
.FindAll(i => i is Rom) .FindAll(i => i is Rom)
.ConvertAll(i => (i as Rom)!.GetName()) .ConvertAll(i => (i as Rom)!.GetName())
.Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey))) .Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey)))
@@ -281,7 +281,7 @@ namespace SabreTools.DatFiles
} }
// If the merge tag exists but the parent doesn't contain it, add to subfolder of parent // If the merge tag exists but the parent doesn't contain it, add to subfolder of parent
else if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && !Items[cloneOf!]! else if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && !GetItemsForBucket(cloneOf!)!
.FindAll(i => i is Rom) .FindAll(i => i is Rom)
.ConvertAll(i => (i as Rom)!.GetName()) .ConvertAll(i => (i as Rom)!.GetName())
.Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey))) .Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey)))
@@ -294,7 +294,7 @@ namespace SabreTools.DatFiles
} }
// 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
else if (!Items[cloneOf!]!.Contains(item) || skipDedup) else if (!GetItemsForBucket(cloneOf!)!.Contains(item) || skipDedup)
{ {
if (subfolder) if (subfolder)
rom.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}"); rom.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}");
@@ -305,7 +305,7 @@ namespace SabreTools.DatFiles
} }
// All other that would be missing to subfolder of parent // All other that would be missing to subfolder of parent
else if (!Items[cloneOf!]!.Contains(item)) else if (!GetItemsForBucket(cloneOf!)!.Contains(item))
{ {
if (subfolder) if (subfolder)
item.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{item.GetName()}"); item.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{item.GetName()}");
@@ -472,7 +472,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket doesn't have items // If the bucket doesn't have items
var datItems = Items[bucket]; var datItems = GetItemsForBucket(bucket);
if (datItems == null || datItems.Count == 0) if (datItems == null || datItems.Count == 0)
continue; continue;
@@ -510,7 +510,7 @@ namespace SabreTools.DatFiles
continue; continue;
// Add to the list of new device reference names // Add to the list of new device reference names
var devItems = Items[deviceReference!]; var devItems = GetItemsForBucket(deviceReference!);
if (devItems == null || devItems.Count == 0) if (devItems == null || devItems.Count == 0)
continue; continue;
@@ -561,7 +561,7 @@ namespace SabreTools.DatFiles
continue; continue;
// Add to the list of new slot option names // Add to the list of new slot option names
var slotItems = Items[slotOption!]; var slotItems = GetItemsForBucket(slotOption!);
if (slotItems == null || slotItems.Count == 0) if (slotItems == null || slotItems.Count == 0)
continue; continue;
@@ -793,7 +793,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -808,7 +808,7 @@ namespace SabreTools.DatFiles
continue; continue;
// If the parent doesn't have any items, we want to continue // If the parent doesn't have any items, we want to continue
var parentItems = Items[cloneOf!]; var parentItems = GetItemsForBucket(cloneOf!);
if (parentItems == null || parentItems.Count == 0) if (parentItems == null || parentItems.Count == 0)
continue; continue;
@@ -826,8 +826,8 @@ namespace SabreTools.DatFiles
} }
// Now we want to get the parent romof tag and put it in each of the items // Now we want to get the parent romof tag and put it in each of the items
items = Items[bucket]; items = GetItemsForBucket(bucket);
string? romof = Items[cloneOf!]![0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); string? romof = GetItemsForBucket(cloneOf!)![0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items!) foreach (DatItem item in items!)
{ {
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof); item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
@@ -915,7 +915,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -984,7 +984,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -1003,7 +1003,7 @@ namespace SabreTools.DatFiles
continue; continue;
// If the parent doesn't have any items, we want to continue // If the parent doesn't have any items, we want to continue
var parentItems = Items[romOf!]; var parentItems = GetItemsForBucket(romOf!);
if (parentItems == null || parentItems.Count == 0) if (parentItems == null || parentItems.Count == 0)
continue; continue;
@@ -1084,7 +1084,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;
@@ -1099,7 +1099,7 @@ namespace SabreTools.DatFiles
continue; continue;
// If the parent doesn't have any items, we want to continue // If the parent doesn't have any items, we want to continue
var parentItems = Items[cloneOf!]; var parentItems = GetItemsForBucket(cloneOf!);
if (parentItems == null || parentItems.Count == 0) if (parentItems == null || parentItems.Count == 0)
continue; continue;
@@ -1114,8 +1114,8 @@ namespace SabreTools.DatFiles
} }
// Now we want to get the parent romof tag and put it in each of the remaining items // Now we want to get the parent romof tag and put it in each of the remaining items
items = Items[bucket]; items = GetItemsForBucket(bucket);
string? romof = Items[cloneOf!]![0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); string? romof = GetItemsForBucket(cloneOf!)![0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items!) foreach (DatItem item in items!)
{ {
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof); item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
@@ -1195,7 +1195,7 @@ namespace SabreTools.DatFiles
foreach (string bucket in buckets) foreach (string bucket in buckets)
{ {
// If the bucket has no items in it // If the bucket has no items in it
var items = Items[bucket]; var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
continue; continue;

View File

@@ -20,6 +20,7 @@ using SabreTools.Hashing;
using SabreTools.IO.Logging; using SabreTools.IO.Logging;
using SabreTools.Matching.Compare; using SabreTools.Matching.Compare;
// TODO: Remove IDictionary implementation
namespace SabreTools.DatFiles namespace SabreTools.DatFiles
{ {
/// <summary> /// <summary>

View File

@@ -387,7 +387,7 @@ namespace SabreTools.DatTools
blankRom.SetName(romname); blankRom.SetName(romname);
blankRom.SetFieldValue<Machine?>(DatItem.MachineKey, blankMachine); blankRom.SetFieldValue<Machine?>(DatItem.MachineKey, blankMachine);
datFile.Items["null"]?.Add(blankRom); datFile.Add("null", blankRom);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else

View File

@@ -168,7 +168,7 @@ namespace SabreTools.DatTools
foreach (var key in itemDictionary.Keys) foreach (var key in itemDictionary.Keys)
{ {
List<DatItem>? items = itemDictionary[key]; List<DatItem>? items = itemDictionary.GetItemsForBucket(key);
if (items == null) if (items == null)
continue; continue;

View File

@@ -797,7 +797,7 @@ namespace SabreTools.DatTools
foreach (string machine in keys) foreach (string machine in keys)
{ {
// Get the current machine // Get the current machine
var items = datFile.Items[machine]; var items = datFile.GetItemsForBucket(machine);
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
{ {
_staticLogger.Error($"{machine} contains no items and will be skipped"); _staticLogger.Error($"{machine} contains no items and will be skipped");