diff --git a/SabreTools.DatFiles/DatFile.Splitting.cs b/SabreTools.DatFiles/DatFile.Splitting.cs index 1e2477bb..df3b6775 100644 --- a/SabreTools.DatFiles/DatFile.Splitting.cs +++ b/SabreTools.DatFiles/DatFile.Splitting.cs @@ -116,8 +116,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -125,14 +125,10 @@ namespace SabreTools.DatFiles if (machine == null) continue; - // Get the bios parent + // Get the romof parent items string? romOf = machine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - if (string.IsNullOrEmpty(romOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucket(romOf!); - if (parentItems == null || parentItems.Count == 0) + List parentItems = GetItemsForBucket(romOf); + if (parentItems.Count == 0) continue; // If the parent exists and has items, we copy the items from the parent to the current game @@ -160,8 +156,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the source for the first item @@ -172,14 +168,10 @@ namespace SabreTools.DatFiles if (machine.Value == null) continue; - // Get the bios parent + // Get the romof parent items string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - if (string.IsNullOrEmpty(romOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucketDB(romOf!); - if (parentItems == null || parentItems.Count == 0) + Dictionary parentItems = GetItemsForBucketDB(romOf!); + if (parentItems.Count == 0) continue; // If the parent exists and has items, we copy the items from the parent to the current game @@ -212,8 +204,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -221,17 +213,13 @@ namespace SabreTools.DatFiles if (machine == null) continue; - // Get the clone parent + // Get the cloneof parent items string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - if (string.IsNullOrEmpty(cloneOf)) - continue; - - // Get the parent items - var parentItems = GetItemsForBucket(cloneOf!); + List parentItems = GetItemsForBucket(cloneOf); // Otherwise, move the items from the current game to a subfolder of the parent game DatItem copyFrom; - if (parentItems == null || parentItems.Count == 0) + if (parentItems.Count == 0) { copyFrom = new Rom(); copyFrom.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.NameKey, cloneOf); @@ -251,7 +239,7 @@ namespace SabreTools.DatFiles string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey); // If the merge tag exists and the parent already contains it, skip - if (mergeTag != null && GetItemsForBucket(cloneOf!)! + if (mergeTag != null && GetItemsForBucket(cloneOf) .FindAll(i => i is Disk) .ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag)) { @@ -259,7 +247,7 @@ namespace SabreTools.DatFiles } // If the merge tag exists but the parent doesn't contain it, add to parent - else if (mergeTag != null && !GetItemsForBucket(cloneOf!)! + else if (mergeTag != null && !GetItemsForBucket(cloneOf) .FindAll(i => i is Disk) .ConvertAll(i => (i as Disk)!.GetName()).Contains(mergeTag)) { @@ -279,7 +267,7 @@ namespace SabreTools.DatFiles else if (item is Rom rom) { // If the merge tag exists and the parent already contains it, skip - if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && GetItemsForBucket(cloneOf!)! + if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && GetItemsForBucket(cloneOf) .FindAll(i => i is Rom) .ConvertAll(i => (i as Rom)!.GetName()) .Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey))) @@ -288,7 +276,7 @@ namespace SabreTools.DatFiles } // 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 && !GetItemsForBucket(cloneOf!)! + else if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && !GetItemsForBucket(cloneOf) .FindAll(i => i is Rom) .ConvertAll(i => (i as Rom)!.GetName()) .Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey))) @@ -301,7 +289,7 @@ namespace SabreTools.DatFiles } // If the parent doesn't already contain this item, add to subfolder of parent - else if (!GetItemsForBucket(cloneOf!)!.Contains(item) || skipDedup) + else if (!GetItemsForBucket(cloneOf).Contains(item) || skipDedup) { if (subfolder) rom.SetName($"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}"); @@ -312,7 +300,7 @@ namespace SabreTools.DatFiles } // All other that would be missing to subfolder of parent - else if (!GetItemsForBucket(cloneOf!)!.Contains(item)) + else if (!GetItemsForBucket(cloneOf).Contains(item)) { if (subfolder) item.SetName($"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{item.GetName()}"); @@ -342,8 +330,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the machine for the first item @@ -365,8 +353,8 @@ namespace SabreTools.DatFiles foreach (var item in items) { // Get the parent items and current machine name - var parentItems = GetItemsForBucketDB(cloneOf!); - if (parentItems == null || parentItems.Count == 0) + Dictionary parentItems = GetItemsForBucketDB(cloneOf); + if (parentItems.Count == 0) continue; string? machineName = ItemsDB.GetMachineForItem(item.Key).Value? @@ -479,8 +467,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket doesn't have items - var datItems = GetItemsForBucket(bucket); - if (datItems == null || datItems.Count == 0) + List datItems = GetItemsForBucket(bucket); + if (datItems.Count == 0) continue; // If the machine (is/is not) a device, we want to continue @@ -512,13 +500,9 @@ namespace SabreTools.DatFiles var newDeviceReferences = new HashSet(); foreach (string? deviceReference in deviceReferences) { - // If the device reference is missing - if (string.IsNullOrEmpty(deviceReference)) - continue; - // Add to the list of new device reference names - var devItems = GetItemsForBucket(deviceReference!); - if (devItems == null || devItems.Count == 0) + List devItems = GetItemsForBucket(deviceReference); + if (devItems.Count == 0) continue; newDeviceReferences.UnionWith(devItems @@ -562,14 +546,9 @@ namespace SabreTools.DatFiles var newSlotOptions = new HashSet(); foreach (string? slotOption in slotOptions) { - // If the slot option is missing - if (string.IsNullOrEmpty(slotOption)) - // If the machine doesn't exist then we continue - continue; - // Add to the list of new slot option names - var slotItems = GetItemsForBucket(slotOption!); - if (slotItems == null || slotItems.Count == 0) + List slotItems = GetItemsForBucket(slotOption); + if (slotItems.Count == 0) continue; newSlotOptions.UnionWith(slotItems @@ -633,8 +612,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the source for the first item @@ -679,8 +658,8 @@ namespace SabreTools.DatFiles continue; // If the machine doesn't exist then we continue - var devItems = GetItemsForBucketDB(deviceReference); - if (devItems == null || devItems.Count == 0) + Dictionary devItems = GetItemsForBucketDB(deviceReference); + if (devItems.Count == 0) continue; // Add to the list of new device reference names @@ -733,8 +712,8 @@ namespace SabreTools.DatFiles continue; // If the machine doesn't exist then we continue - var slotItems = GetItemsForBucketDB(slotOption); - if (slotItems == null || slotItems.Count == 0) + Dictionary slotItems = GetItemsForBucketDB(slotOption); + if (slotItems.Count == 0) continue; // Add to the list of new slot option names @@ -745,7 +724,7 @@ namespace SabreTools.DatFiles .Select(o => o.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)!)); // Set new machine information and add to the current machine - var copyFrom = ItemsDB.GetMachineForItem(GetItemsForBucketDB(bucket)!.First().Key); + var copyFrom = ItemsDB.GetMachineForItem(GetItemsForBucketDB(bucket).First().Key); if (copyFrom.Value == null) continue; @@ -800,8 +779,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -809,14 +788,10 @@ namespace SabreTools.DatFiles if (machine == null) continue; - // Get the clone parent + // Get the cloneof parent items string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - if (string.IsNullOrEmpty(cloneOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucket(cloneOf!); - if (parentItems == null || parentItems.Count == 0) + List parentItems = GetItemsForBucket(cloneOf!); + if (parentItems.Count == 0) continue; // If the parent exists and has items, we copy the items from the parent to the current game @@ -834,7 +809,7 @@ namespace SabreTools.DatFiles // Now we want to get the parent romof tag and put it in each of the items items = GetItemsForBucket(bucket); - string? romof = GetItemsForBucket(cloneOf!)![0].GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + string? romof = GetItemsForBucket(cloneOf)[0].GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); foreach (DatItem item in items) { item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); @@ -855,8 +830,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the source for the first item @@ -873,8 +848,8 @@ namespace SabreTools.DatFiles continue; // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucketDB(cloneOf!); - if (parentItems == null || parentItems.Count == 0) + Dictionary parentItems = GetItemsForBucketDB(cloneOf!); + if (parentItems.Count == 0) continue; // If the parent exists and has items, we copy the items from the parent to the current game @@ -889,7 +864,7 @@ namespace SabreTools.DatFiles } // Get the parent machine - var parentMachine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf!)!.First().Key); + var parentMachine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf).First().Key); if (parentMachine.Value == null) continue; @@ -922,8 +897,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -953,8 +928,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the machine @@ -989,8 +964,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -998,14 +973,10 @@ namespace SabreTools.DatFiles if (machine == null) continue; - // Get the clone parent + // Get the cloneof parent items string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - if (string.IsNullOrEmpty(cloneOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucket(cloneOf!); - if (parentItems == null || parentItems.Count == 0) + List parentItems = GetItemsForBucket(cloneOf); + if (parentItems.Count == 0) continue; // If the parent exists and has items, we remove the parent items from the current game @@ -1020,7 +991,7 @@ namespace SabreTools.DatFiles // Now we want to get the parent romof tag and put it in each of the remaining items items = GetItemsForBucket(bucket); - string? romof = GetItemsForBucket(cloneOf!)[0].GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + string? romof = GetItemsForBucket(cloneOf)[0].GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); foreach (DatItem item in items) { item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); @@ -1041,8 +1012,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the machine for the first item @@ -1050,13 +1021,9 @@ namespace SabreTools.DatFiles if (machine.Value == null) continue; - // Get the clone parent + // Get the cloneof parent items string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - if (string.IsNullOrEmpty(cloneOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucketDB(cloneOf!); + Dictionary parentItems = GetItemsForBucketDB(cloneOf!); if (parentItems == null || parentItems.Count == 0) continue; @@ -1072,7 +1039,7 @@ namespace SabreTools.DatFiles // Now we want to get the parent romof tag and put it in each of the remaining items items = GetItemsForBucketDB(bucket); - machine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf!)!.First().Key); + machine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf).First().Key); if (machine.Value == null) continue; @@ -1105,8 +1072,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucket(bucket); - if (items == null || items.Count == 0) + List items = GetItemsForBucket(bucket); + if (items.Count == 0) continue; // Get the machine @@ -1118,14 +1085,10 @@ namespace SabreTools.DatFiles if (bios ^ (machine.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)) continue; - // Get the bios parent + // Get the romof parent items string? romOf = machine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - if (string.IsNullOrEmpty(romOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucket(romOf!); - if (parentItems == null || parentItems.Count == 0) + List parentItems = GetItemsForBucket(romOf!); + 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 @@ -1155,8 +1118,8 @@ namespace SabreTools.DatFiles foreach (string bucket in buckets) { // If the bucket has no items in it - var items = GetItemsForBucketDB(bucket); - if (items == null || items.Count == 0) + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) continue; // Get the machine for the item @@ -1168,14 +1131,10 @@ namespace SabreTools.DatFiles if (bios ^ (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)) continue; - // Get the bios parent + // Get the romof parent items string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - if (string.IsNullOrEmpty(romOf)) - continue; - - // If the parent doesn't have any items, we want to continue - var parentItems = GetItemsForBucketDB(romOf!); - if (parentItems == null || parentItems.Count == 0) + Dictionary parentItems = GetItemsForBucketDB(romOf!); + 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 @@ -1212,7 +1171,7 @@ namespace SabreTools.DatFiles var machine = item.GetFieldValue(DatItem.MachineKey); if (machine == null) continue; - + machine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, null); machine.SetFieldValue(Models.Metadata.Machine.RomOfKey, null); machine.SetFieldValue(Models.Metadata.Machine.SampleOfKey, null); diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 6c6d126a..015c87ad 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -224,13 +224,13 @@ namespace SabreTools.DatFiles /// /// Get the items associated with a bucket name /// - public List GetItemsForBucket(string bucketName, bool filter = false) + public List GetItemsForBucket(string? bucketName, bool filter = false) => Items.GetItemsForBucket(bucketName, filter); /// /// Get the indices and items associated with a bucket name /// - public Dictionary GetItemsForBucketDB(string bucketName, bool filter = false) + public Dictionary GetItemsForBucketDB(string? bucketName, bool filter = false) => ItemsDB.GetItemsForBucket(bucketName, filter); /// diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index b714866b..5e570aec 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -416,8 +416,11 @@ namespace SabreTools.DatFiles /// /// Get the items associated with a bucket name /// - public List GetItemsForBucket(string bucketName, bool filter = false) + public List GetItemsForBucket(string? bucketName, bool filter = false) { + if (bucketName == null) + return []; + if (!_items.ContainsKey(bucketName)) return []; diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index ffc3cd18..6f283583 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -352,8 +352,11 @@ namespace SabreTools.DatFiles /// /// Get the indices and items associated with a bucket name /// - public Dictionary GetItemsForBucket(string bucketName, bool filter = false) + public Dictionary GetItemsForBucket(string? bucketName, bool filter = false) { + if (bucketName == null) + return []; + if (!_buckets.ContainsKey(bucketName)) return []; diff --git a/SabreTools.DatTools/Rebuilder.cs b/SabreTools.DatTools/Rebuilder.cs index 43894b1b..4965df7e 100644 --- a/SabreTools.DatTools/Rebuilder.cs +++ b/SabreTools.DatTools/Rebuilder.cs @@ -463,7 +463,7 @@ namespace SabreTools.DatTools continue; // If we should check for the items in the machine - var items = datFile.GetItemsForBucket(machine.GetStringFieldValue(Models.Metadata.Machine.NameKey)!); + var items = datFile.GetItemsForBucket(machine.GetStringFieldValue(Models.Metadata.Machine.NameKey)); if (shouldCheck && items!.Count > 1) outputFormat = OutputFormat.Folder; else if (shouldCheck && items!.Count == 1)