diff --git a/SabreTools.DatItems/DatItem.cs b/SabreTools.DatItems/DatItem.cs index d1dee63d..7071ae27 100644 --- a/SabreTools.DatItems/DatItem.cs +++ b/SabreTools.DatItems/DatItem.cs @@ -123,6 +123,89 @@ namespace SabreTools.DatItems /// Clone of the DatItem public abstract object Clone(); + /// + /// Conditionally copy all machine information from another item + /// + /// Existing item to copy information from + /// + /// The cases when Machine data is updated: + /// - Current machine is a clone of the other machine + /// - Current machine is a rom of the other machine + /// + public void ConditionalUpdateMachine(DatItem item) + { + // Get the machines for the two items + Machine? selfMachine = GetFieldValue(DatItem.MachineKey); + Machine? itemMachine = item.GetFieldValue(DatItem.MachineKey); + + // If either machine is missing + if (selfMachine == null || itemMachine == null) + return; + + // Get the required strings + string? selfCloneOf = selfMachine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); + string? selfRomOf = selfMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + string? otherMachineName = itemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey); + + // If the other machine has no name + if (otherMachineName == null) + return; + + // If the current machine is a child of the new machine, use the new machine instead + if (selfCloneOf == otherMachineName) + { + CopyMachineInformation(item); + SetName(item.GetName()); + } + else if (selfRomOf == otherMachineName) + { + CopyMachineInformation(item); + SetName(item.GetName()); + } + } + + /// + /// Conditionally copy all source information from another item + /// + /// Existing item to copy information from + /// + /// The cases when Source data is updated: + /// - Current source data is missing and the other item has a source + /// - Current source data has an index higher than the other item + /// + public void ConditionalUpdateSource(DatItem item) + { + // Get the sources for comparison + Source? selfSource = GetFieldValue(DatItem.SourceKey); + Source? itemSource = item.GetFieldValue(DatItem.SourceKey); + + // If both sources are missing, do nothing + if (selfSource == null && itemSource == null) + return; + + // Use the new source if missing + if (selfSource == null && itemSource != null) + { + SetFieldValue(DatItem.SourceKey, itemSource.Clone() as Source); + CopyMachineInformation(item); + SetName(item.GetName()); + return; + } + + // If either source is missing + if (selfSource == null || itemSource == null) + return; + + // Use the new source if less than + if (selfSource.Index > itemSource.Index) + { + SetFieldValue(DatItem.SourceKey, itemSource.Clone() as Source); + CopyMachineInformation(item); + SetName(item.GetName()); + return; + } + } + /// /// Copy all machine information over in one shot /// diff --git a/SabreTools.DatItems/DatItemTool.cs b/SabreTools.DatItems/DatItemTool.cs index 5b9cedc7..d5ef9b54 100644 --- a/SabreTools.DatItems/DatItemTool.cs +++ b/SabreTools.DatItems/DatItemTool.cs @@ -342,33 +342,6 @@ namespace SabreTools.DatItems // Set the duplicate type on the saved item savedItem.SetFieldValue(DatItem.DupeTypeKey, dupetype); - - // Get the sources for the two items - Source? itemSource = item.GetFieldValue(DatItem.SourceKey); - Source? savedItemSource = savedItem.GetFieldValue(DatItem.SourceKey); - - // If the current system has a lower ID than the previous, set the system accordingly - if (itemSource?.Index < savedItemSource?.Index) - { - item.SetFieldValue(DatItem.SourceKey, item.GetFieldValue(DatItem.SourceKey)!.Clone() as Source); - savedItem.CopyMachineInformation(item); - savedItem.SetName(item.GetName()); - } - - // Get the machines for the two items - Machine? itemMachine = item.GetFieldValue(DatItem.MachineKey); - Machine? savedItemMachine = savedItem.GetFieldValue(DatItem.MachineKey); - - // If the current machine is a child of the new machine, use the new machine instead - if (itemMachine != null - && savedItemMachine != null - && (itemMachine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == savedItemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey) - || itemMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == savedItemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey))) - { - savedItem.CopyMachineInformation(item); - savedItem.SetName(item.GetName()); - } - break; } diff --git a/SabreTools.DatItems/Formats/Disk.cs b/SabreTools.DatItems/Formats/Disk.cs index 75d1ae36..826cdba5 100644 --- a/SabreTools.DatItems/Formats/Disk.cs +++ b/SabreTools.DatItems/Formats/Disk.cs @@ -98,7 +98,12 @@ namespace SabreTools.DatItems.Formats /// Fill any missing size and hash information from another Disk /// /// Disk to fill information from - public void FillMissingInformation(Disk other) => _internal.FillMissingHashes(other?._internal); + public void FillMissingInformation(Disk other) + { + _internal.FillMissingHashes(other._internal); + ConditionalUpdateSource(other); + ConditionalUpdateMachine(other); + } /// /// Get unique duplicate suffix on name collision diff --git a/SabreTools.DatItems/Formats/File.cs b/SabreTools.DatItems/Formats/File.cs index b288a56f..01a5fcc6 100644 --- a/SabreTools.DatItems/Formats/File.cs +++ b/SabreTools.DatItems/Formats/File.cs @@ -207,6 +207,9 @@ namespace SabreTools.DatItems.Formats if (_sha256.IsNullOrEmpty() && !other._sha256.IsNullOrEmpty()) _sha256 = other._sha256; + + ConditionalUpdateSource(other); + ConditionalUpdateMachine(other); } /// diff --git a/SabreTools.DatItems/Formats/Media.cs b/SabreTools.DatItems/Formats/Media.cs index ee9ab3de..35a8def1 100644 --- a/SabreTools.DatItems/Formats/Media.cs +++ b/SabreTools.DatItems/Formats/Media.cs @@ -59,7 +59,12 @@ namespace SabreTools.DatItems.Formats /// Fill any missing size and hash information from another Media /// /// Media to fill information from - public void FillMissingInformation(Media other) => _internal.FillMissingHashes(other?._internal); + public void FillMissingInformation(Media other) + { + _internal.FillMissingHashes(other._internal); + ConditionalUpdateSource(other); + ConditionalUpdateMachine(other); + } /// /// Get unique duplicate suffix on name collision diff --git a/SabreTools.DatItems/Formats/Rom.cs b/SabreTools.DatItems/Formats/Rom.cs index dcf0b06c..50b07c0e 100644 --- a/SabreTools.DatItems/Formats/Rom.cs +++ b/SabreTools.DatItems/Formats/Rom.cs @@ -102,7 +102,12 @@ namespace SabreTools.DatItems.Formats /// Fill any missing size and hash information from another Rom /// /// Rom to fill information from - public void FillMissingInformation(Rom other) => _internal.FillMissingHashes(other?._internal); + public void FillMissingInformation(Rom other) + { + _internal.FillMissingHashes(other._internal); + ConditionalUpdateSource(other); + ConditionalUpdateMachine(other); + } /// /// Get unique duplicate suffix on name collision