Better conditional updates

This commit is contained in:
Matt Nadareski
2025-01-06 15:37:32 -05:00
parent 3dee4fd376
commit 8d845df8b9
6 changed files with 104 additions and 30 deletions

View File

@@ -123,6 +123,89 @@ namespace SabreTools.DatItems
/// <returns>Clone of the DatItem</returns> /// <returns>Clone of the DatItem</returns>
public abstract object Clone(); public abstract object Clone();
/// <summary>
/// Conditionally copy all machine information from another item
/// </summary>
/// <param name="item">Existing item to copy information from</param>
/// <remarks>
/// 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
/// </remarks>
public void ConditionalUpdateMachine(DatItem item)
{
// Get the machines for the two items
Machine? selfMachine = GetFieldValue<Machine>(DatItem.MachineKey);
Machine? itemMachine = item.GetFieldValue<Machine>(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());
}
}
/// <summary>
/// Conditionally copy all source information from another item
/// </summary>
/// <param name="item">Existing item to copy information from</param>
/// <remarks>
/// 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
/// </remarks>
public void ConditionalUpdateSource(DatItem item)
{
// Get the sources for comparison
Source? selfSource = GetFieldValue<Source?>(DatItem.SourceKey);
Source? itemSource = item.GetFieldValue<Source?>(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<Source?>(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<Source?>(DatItem.SourceKey, itemSource.Clone() as Source);
CopyMachineInformation(item);
SetName(item.GetName());
return;
}
}
/// <summary> /// <summary>
/// Copy all machine information over in one shot /// Copy all machine information over in one shot
/// </summary> /// </summary>

View File

@@ -342,33 +342,6 @@ namespace SabreTools.DatItems
// Set the duplicate type on the saved item // Set the duplicate type on the saved item
savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype); savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
// Get the sources for the two items
Source? itemSource = item.GetFieldValue<Source?>(DatItem.SourceKey);
Source? savedItemSource = savedItem.GetFieldValue<Source?>(DatItem.SourceKey);
// If the current system has a lower ID than the previous, set the system accordingly
if (itemSource?.Index < savedItemSource?.Index)
{
item.SetFieldValue<Source?>(DatItem.SourceKey, item.GetFieldValue<Source?>(DatItem.SourceKey)!.Clone() as Source);
savedItem.CopyMachineInformation(item);
savedItem.SetName(item.GetName());
}
// Get the machines for the two items
Machine? itemMachine = item.GetFieldValue<Machine>(DatItem.MachineKey);
Machine? savedItemMachine = savedItem.GetFieldValue<Machine>(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; break;
} }

View File

@@ -98,7 +98,12 @@ namespace SabreTools.DatItems.Formats
/// Fill any missing size and hash information from another Disk /// Fill any missing size and hash information from another Disk
/// </summary> /// </summary>
/// <param name="other">Disk to fill information from</param> /// <param name="other">Disk to fill information from</param>
public void FillMissingInformation(Disk other) => _internal.FillMissingHashes(other?._internal); public void FillMissingInformation(Disk other)
{
_internal.FillMissingHashes(other._internal);
ConditionalUpdateSource(other);
ConditionalUpdateMachine(other);
}
/// <summary> /// <summary>
/// Get unique duplicate suffix on name collision /// Get unique duplicate suffix on name collision

View File

@@ -207,6 +207,9 @@ namespace SabreTools.DatItems.Formats
if (_sha256.IsNullOrEmpty() && !other._sha256.IsNullOrEmpty()) if (_sha256.IsNullOrEmpty() && !other._sha256.IsNullOrEmpty())
_sha256 = other._sha256; _sha256 = other._sha256;
ConditionalUpdateSource(other);
ConditionalUpdateMachine(other);
} }
/// <summary> /// <summary>

View File

@@ -59,7 +59,12 @@ namespace SabreTools.DatItems.Formats
/// Fill any missing size and hash information from another Media /// Fill any missing size and hash information from another Media
/// </summary> /// </summary>
/// <param name="other">Media to fill information from</param> /// <param name="other">Media to fill information from</param>
public void FillMissingInformation(Media other) => _internal.FillMissingHashes(other?._internal); public void FillMissingInformation(Media other)
{
_internal.FillMissingHashes(other._internal);
ConditionalUpdateSource(other);
ConditionalUpdateMachine(other);
}
/// <summary> /// <summary>
/// Get unique duplicate suffix on name collision /// Get unique duplicate suffix on name collision

View File

@@ -102,7 +102,12 @@ namespace SabreTools.DatItems.Formats
/// Fill any missing size and hash information from another Rom /// Fill any missing size and hash information from another Rom
/// </summary> /// </summary>
/// <param name="other">Rom to fill information from</param> /// <param name="other">Rom to fill information from</param>
public void FillMissingInformation(Rom other) => _internal.FillMissingHashes(other?._internal); public void FillMissingInformation(Rom other)
{
_internal.FillMissingHashes(other._internal);
ConditionalUpdateSource(other);
ConditionalUpdateMachine(other);
}
/// <summary> /// <summary>
/// Get unique duplicate suffix on name collision /// Get unique duplicate suffix on name collision