Slight cleanup of DatItem

This commit is contained in:
Matt Nadareski
2024-10-24 05:11:17 -04:00
parent d1393d3731
commit 3f353c745f

View File

@@ -170,11 +170,8 @@ namespace SabreTools.DatItems
/// <param name="item">Existing item to copy information from</param> /// <param name="item">Existing item to copy information from</param>
public void CopyMachineInformation(DatItem item) public void CopyMachineInformation(DatItem item)
{ {
if (item?.GetFieldValue<Machine>(DatItem.MachineKey) == null) var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
return; CopyMachineInformation(machine);
if (item!.GetFieldValue<Machine>(DatItem.MachineKey)!.Clone() is Machine cloned)
SetFieldValue<Machine>(DatItem.MachineKey, cloned);
} }
/// <summary> /// <summary>
@@ -383,17 +380,27 @@ namespace SabreTools.DatItems
break; break;
case ItemKey.Machine: case ItemKey.Machine:
key = (norename ? string.Empty string sourceString = string.Empty;
: GetFieldValue<Source?>(DatItem.SourceKey)?.Index.ToString().PadLeft(10, '0') if (!norename)
+ "-") {
+ (string.IsNullOrEmpty(GetFieldValue<Machine>(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey)) var source = GetFieldValue<Source?>(DatItem.SourceKey);
? "Default" if (source != null)
: GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!); sourceString = source.Index.ToString().PadLeft(10, '0') + "-";
}
string machineString = "Default";
var machine = GetFieldValue<Machine>(DatItem.MachineKey);
if (machine != null)
{
var machineName = machine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
if (!string.IsNullOrEmpty(machineName))
machineString = machineName!;
}
key = $"{sourceString}{machineString}";
if (lower) if (lower)
key = key.ToLowerInvariant(); key = key.ToLowerInvariant();
key ??= "null";
break; break;
case ItemKey.MD5: case ItemKey.MD5:
@@ -448,17 +455,23 @@ namespace SabreTools.DatItems
break; break;
case ItemKey.Machine: case ItemKey.Machine:
key = (norename ? string.Empty string sourceString = string.Empty;
: source?.Index.ToString().PadLeft(10, '0') if (!norename && source != null)
+ "-") sourceString = source.Index.ToString().PadLeft(10, '0') + "-";
+ (string.IsNullOrEmpty(GetFieldValue<Machine>(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey))
? "Default" string machineString = "Default";
: GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!); var machine = GetFieldValue<Machine>(DatItem.MachineKey);
if (machine != null)
{
var machineName = machine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
if (!string.IsNullOrEmpty(machineName))
machineString = machineName!;
}
key = $"{sourceString}{machineString}";
if (lower) if (lower)
key = key.ToLowerInvariant(); key = key.ToLowerInvariant();
key ??= "null";
break; break;
case ItemKey.MD5: case ItemKey.MD5:
@@ -526,9 +539,7 @@ namespace SabreTools.DatItems
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates // If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
if (item is not Disk && item is not Formats.File && item is not Media && item is not Rom) if (item is not Disk && item is not Formats.File && item is not Media && item is not Rom)
{
continue; continue;
}
// If it's a nodump, add and skip // If it's a nodump, add and skip
if (item is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump) if (item is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
@@ -646,10 +657,14 @@ namespace SabreTools.DatItems
} }
// Get the last item name, if applicable // Get the last item name, if applicable
string lastItemName = lastItem.GetName() ?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue() ?? string.Empty; string lastItemName = lastItem.GetName()
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
// Get the current item name, if applicable // Get the current item name, if applicable
string datItemName = datItem.GetName() ?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue() ?? string.Empty; string datItemName = datItem.GetName()
?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
// If the current item exactly matches the last item, then we don't add it // If the current item exactly matches the last item, then we don't add it
#if NETFRAMEWORK #if NETFRAMEWORK