diff --git a/SabreTools.DatFiles.Test/DatFileTests.Splitting.cs b/SabreTools.DatFiles.Test/DatFileTests.Splitting.cs index ea2693d1..402b47d8 100644 --- a/SabreTools.DatFiles.Test/DatFileTests.Splitting.cs +++ b/SabreTools.DatFiles.Test/DatFileTests.Splitting.cs @@ -13,18 +13,18 @@ namespace SabreTools.DatFiles.Test #endregion - #region AddItemsFromDevices - - // TODO: Implement AddItemsFromDevices tests - - #endregion - #region AddItemsFromCloneOfParent // TODO: Implement AddItemsFromCloneOfParent tests #endregion + #region AddItemsFromDevices + + // TODO: Implement AddItemsFromDevices tests + + #endregion + #region AddItemsFromRomOfParent // TODO: Implement AddItemsFromRomOfParent tests diff --git a/SabreTools.DatFiles/DatFile.Splitting.cs b/SabreTools.DatFiles/DatFile.Splitting.cs index 480ddfd4..507f8a64 100644 --- a/SabreTools.DatFiles/DatFile.Splitting.cs +++ b/SabreTools.DatFiles/DatFile.Splitting.cs @@ -23,6 +23,16 @@ namespace SabreTools.DatFiles AddItemsFromChildrenImplDB(subfolder, skipDedup); } + /// + /// Use cloneof tags to add items to the children, setting the new romof tag in the process + /// + /// Assumes items are bucketed by + public void AddItemsFromCloneOfParent() + { + AddItemsFromCloneOfParentImpl(); + AddItemsFromCloneOfParentImplDB(); + } + /// /// Use device_ref and optionally slotoption tags to add items to the children /// @@ -36,16 +46,6 @@ namespace SabreTools.DatFiles return foundnew; } - /// - /// Use cloneof tags to add items to the children, setting the new romof tag in the process - /// - /// Assumes items are bucketed by - public void AddItemsFromCloneOfParent() - { - AddItemsFromCloneOfParentImpl(); - AddItemsFromCloneOfParentImplDB(); - } - /// /// Use romof tags to add items to the children /// @@ -362,6 +362,124 @@ namespace SabreTools.DatFiles } } + /// + /// Use cloneof tags to add items to the children, setting the new romof tag in the process + /// + /// + /// Applies to . + /// Assumes items are bucketed by . + /// + private void AddItemsFromCloneOfParentImpl() + { + List buckets = [.. Items.Keys]; + buckets.Sort(); + + foreach (string bucket in buckets) + { + // If the bucket has no items in it + List items = GetItemsForBucket(bucket); + if (items.Count == 0) + continue; + + // Get the machine + var machine = items[0].GetFieldValue(DatItem.MachineKey); + if (machine == null) + continue; + + // Get the cloneof parent items + string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); + 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 + DatItem copyFrom = items[0]; + foreach (DatItem item in parentItems) + { + DatItem datItem = (DatItem)item.Clone(); + datItem.CopyMachineInformation(copyFrom); + if (items.FindIndex(i => string.Equals(i.GetName(), datItem.GetName(), StringComparison.OrdinalIgnoreCase)) == -1 + && !items.Contains(datItem)) + { + Add(bucket, datItem); + } + } + + // 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); + foreach (DatItem item in items) + { + item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); + } + } + } + + /// + /// Use cloneof tags to add items to the children, setting the new romof tag in the process + /// + /// + /// Applies to . + /// Assumes items are bucketed by . + /// + private void AddItemsFromCloneOfParentImplDB() + { + List buckets = [.. ItemsDB.SortedKeys]; + foreach (string bucket in buckets) + { + // If the bucket has no items in it + Dictionary items = GetItemsForBucketDB(bucket); + if (items.Count == 0) + continue; + + // Get the source for the first item + var source = ItemsDB.GetSourceForItem(items.First().Key); + + // Get the machine for the first item in the list + var machine = ItemsDB.GetMachineForItem(items.First().Key); + if (machine.Value == null) + continue; + + // Get the clone parent + 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 + 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 + foreach (var item in parentItems) + { + DatItem datItem = (DatItem)item.Value.Clone(); + if (items.Values.Any(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()) + && items.Values.Any(i => i == datItem)) + { + ItemsDB.AddItem(datItem, machine.Key, source.Key); + } + } + + // Get the parent machine + var parentMachine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf).First().Key); + if (parentMachine.Value == null) + continue; + + // Now we want to get the parent romof tag and put it in each of the items + items = GetItemsForBucketDB(bucket); + string? romof = parentMachine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + foreach (var key in items.Keys) + { + var itemMachine = ItemsDB.GetMachineForItem(key); + if (itemMachine.Value == null) + continue; + + itemMachine.Value.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); + } + } + } + /// /// Use device_ref and optionally slotoption tags to add items to the children /// @@ -678,124 +796,6 @@ namespace SabreTools.DatFiles return foundnew; } - /// - /// Use cloneof tags to add items to the children, setting the new romof tag in the process - /// - /// - /// Applies to . - /// Assumes items are bucketed by . - /// - private void AddItemsFromCloneOfParentImpl() - { - List buckets = [.. Items.Keys]; - buckets.Sort(); - - foreach (string bucket in buckets) - { - // If the bucket has no items in it - List items = GetItemsForBucket(bucket); - if (items.Count == 0) - continue; - - // Get the machine - var machine = items[0].GetFieldValue(DatItem.MachineKey); - if (machine == null) - continue; - - // Get the cloneof parent items - string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - 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 - DatItem copyFrom = items[0]; - foreach (DatItem item in parentItems) - { - DatItem datItem = (DatItem)item.Clone(); - datItem.CopyMachineInformation(copyFrom); - if (items.FindIndex(i => string.Equals(i.GetName(), datItem.GetName(), StringComparison.OrdinalIgnoreCase)) == -1 - && !items.Contains(datItem)) - { - Add(bucket, datItem); - } - } - - // 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); - foreach (DatItem item in items) - { - item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); - } - } - } - - /// - /// Use cloneof tags to add items to the children, setting the new romof tag in the process - /// - /// - /// Applies to . - /// Assumes items are bucketed by . - /// - private void AddItemsFromCloneOfParentImplDB() - { - List buckets = [.. ItemsDB.SortedKeys]; - foreach (string bucket in buckets) - { - // If the bucket has no items in it - Dictionary items = GetItemsForBucketDB(bucket); - if (items.Count == 0) - continue; - - // Get the source for the first item - var source = ItemsDB.GetSourceForItem(items.First().Key); - - // Get the machine for the first item in the list - var machine = ItemsDB.GetMachineForItem(items.First().Key); - if (machine.Value == null) - continue; - - // Get the clone parent - 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 - 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 - foreach (var item in parentItems) - { - DatItem datItem = (DatItem)item.Value.Clone(); - if (items.Values.Any(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()) - && items.Values.Any(i => i == datItem)) - { - ItemsDB.AddItem(datItem, machine.Key, source.Key); - } - } - - // Get the parent machine - var parentMachine = ItemsDB.GetMachineForItem(GetItemsForBucketDB(cloneOf).First().Key); - if (parentMachine.Value == null) - continue; - - // Now we want to get the parent romof tag and put it in each of the items - items = GetItemsForBucketDB(bucket); - string? romof = parentMachine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - foreach (var key in items.Keys) - { - var itemMachine = ItemsDB.GetMachineForItem(key); - if (itemMachine.Value == null) - continue; - - itemMachine.Value.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); - } - } - } - /// /// Use romof tags to add items to the children ///