Make Machine inherent to DatItem instead of passing through

This commit is contained in:
Matt Nadareski
2026-04-02 16:23:16 -04:00
parent ca95e96402
commit 355aa4b94c
21 changed files with 182 additions and 190 deletions

View File

@@ -119,7 +119,7 @@ namespace SabreTools.Metadata.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetMachine();
var machine = item.Machine;
if (machine is null)
continue;
@@ -326,7 +326,7 @@ namespace SabreTools.Metadata.DatFiles
DatItem item = GetItemsForBucket(key)[0];
// Get machine information
Machine? machine = item.GetMachine();
Machine? machine = item.Machine;
string? machineName = machine?.Name?.ToLowerInvariant();
if (machine is null || machineName is null)
continue;
@@ -514,7 +514,7 @@ namespace SabreTools.Metadata.DatFiles
return;
// Get the current machine
var machine = datItem.GetMachine();
var machine = datItem.Machine;
if (machine is null)
return;
@@ -522,7 +522,7 @@ namespace SabreTools.Metadata.DatFiles
machine = (Machine)machine.Clone();
// Reassign the item to the new machine
datItem.Write(DatItem.MachineKey, machine);
datItem.Machine = machine;
// Remove extensions from File and Rom items
if (datItem is DatItems.Formats.File || datItem is Rom)
@@ -646,7 +646,7 @@ namespace SabreTools.Metadata.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetMachine();
var machine = item.Machine;
if (machine is null)
continue;
@@ -655,10 +655,10 @@ namespace SabreTools.Metadata.DatFiles
string? machineDesc = machine.Description;
if (machineName is not null && Regex.IsMatch(machineName, SceneNamePattern))
item.GetMachine()!.Name = Regex.Replace(machineName, SceneNamePattern, "$2");
item.Machine!.Name = Regex.Replace(machineName, SceneNamePattern, "$2");
if (machineDesc is not null && Regex.IsMatch(machineDesc, SceneNamePattern))
item.GetMachine()!.Description = Regex.Replace(machineDesc, SceneNamePattern, "$2");
item.Machine!.Description = Regex.Replace(machineDesc, SceneNamePattern, "$2");
}
#if NET40_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
});
@@ -727,7 +727,7 @@ namespace SabreTools.Metadata.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetMachine();
var machine = item.Machine;
if (machine is null)
continue;

View File

@@ -268,7 +268,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
continue;
@@ -283,8 +283,8 @@ namespace SabreTools.Metadata.DatFiles
if (parentItems.Count == 0)
{
copyFrom = new Rom();
copyFrom.GetMachine()!.Name = cloneOf;
copyFrom.GetMachine()!.Description = cloneOf;
copyFrom.Machine!.Name = cloneOf;
copyFrom.Machine!.Description = cloneOf;
}
else
{
@@ -350,7 +350,7 @@ namespace SabreTools.Metadata.DatFiles
.Contains(mergeTag))
{
if (subfolder)
rom.Name = $"{rom.GetMachine()!.Name}\\{rom.Name}";
rom.Name = $"{rom.Machine!.Name}\\{rom.Name}";
rom.CopyMachineInformation(copyFrom);
AddItem(rom, statsOnly: false);
@@ -360,7 +360,7 @@ namespace SabreTools.Metadata.DatFiles
else if (!GetItemsForBucket(cloneOf).Exists(i => i.Equals(item)) || skipDedup)
{
if (subfolder)
rom.Name = $"{item.GetMachine()!.Name}\\{rom.Name}";
rom.Name = $"{item.Machine!.Name}\\{rom.Name}";
rom.CopyMachineInformation(copyFrom);
AddItem(rom, statsOnly: false);
@@ -371,7 +371,7 @@ namespace SabreTools.Metadata.DatFiles
else if (!GetItemsForBucket(cloneOf).Exists(i => i.Equals(item)))
{
if (subfolder)
item.SetName($"{item.GetMachine()!.Name}\\{item.GetName()}");
item.SetName($"{item.Machine!.Name}\\{item.GetName()}");
item.CopyMachineInformation(copyFrom);
AddItem(item, statsOnly: false);
@@ -537,7 +537,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
continue;
@@ -562,10 +562,10 @@ namespace SabreTools.Metadata.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].GetMachine()!.ReadString(Data.Models.Metadata.Machine.RomOfKey);
string? romof = GetItemsForBucket(cloneOf)[0].Machine!.ReadString(Data.Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items)
{
item.GetMachine()!.Write<string?>(Data.Models.Metadata.Machine.RomOfKey, romof);
item.Machine!.Write<string?>(Data.Models.Metadata.Machine.RomOfKey, romof);
}
}
}
@@ -659,7 +659,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// If the machine (is/is not) a device, we want to continue
if (deviceOnly ^ (datItems[0].GetMachine()!.IsDevice == true))
if (deviceOnly ^ (datItems[0].Machine!.IsDevice == true))
continue;
// Get the first item from the bucket
@@ -976,7 +976,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
continue;
@@ -1069,7 +1069,7 @@ namespace SabreTools.Metadata.DatFiles
#endif
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
#if NET40_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
return;
@@ -1158,7 +1158,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
continue;
@@ -1186,10 +1186,10 @@ namespace SabreTools.Metadata.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].GetMachine()!.ReadString(Data.Models.Metadata.Machine.RomOfKey);
string? romof = GetItemsForBucket(cloneOf)[0].Machine!.ReadString(Data.Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items)
{
item.GetMachine()!.Write<string?>(Data.Models.Metadata.Machine.RomOfKey, romof);
item.Machine!.Write<string?>(Data.Models.Metadata.Machine.RomOfKey, romof);
}
}
}
@@ -1268,7 +1268,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine
var machine = items[0].GetMachine();
var machine = items[0].Machine;
if (machine is null)
continue;
@@ -1366,7 +1366,7 @@ namespace SabreTools.Metadata.DatFiles
item.Remove(Data.Models.Metadata.Rom.MergeKey);
// Get the machine
var machine = item.GetMachine();
var machine = item.Machine;
if (machine is null)
continue;

View File

@@ -76,7 +76,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Create a machine to hold everything
var machine = items[0].GetMachine()!.GetInternalClone();
var machine = items[0].Machine!.GetInternalClone();
// Handle Trurip object, if it exists
if (machine.ContainsKey(Data.Models.Metadata.Machine.TruripKey))

View File

@@ -1176,8 +1176,8 @@ namespace SabreTools.Metadata.DatFiles
}
// If machine names don't match
string? xMachineName = x.GetMachine()?.Name;
string? yMachineName = y.GetMachine()?.Name;
string? xMachineName = x.Machine?.Name;
string? yMachineName = y.Machine?.Name;
if (xMachineName != yMachineName)
return nc.Compare(xMachineName, yMachineName);

View File

@@ -88,7 +88,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
WriteDatItem(sw, datItem, lastgame);
// Set the new data to compare against
lastgame = datItem.GetMachine()!.Name;
lastgame = datItem.Machine!.Name;
}
}
@@ -174,7 +174,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
/// <param name="lastgame">The name of the last game to be output</param>
private void WriteDatItem(StreamWriter sw, DatItem datItem, string? lastgame)
{
var machine = datItem.GetMachine();
var machine = datItem.Machine;
WriteDatItemImpl(sw, datItem, machine!, lastgame);
}

View File

@@ -458,11 +458,11 @@ namespace SabreTools.Metadata.DatFiles.Formats
DatItem datItem = datItems[index];
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame is not null && !string.Equals(lastgame, datItem.GetMachine()!.Name, StringComparison.OrdinalIgnoreCase))
if (lastgame is not null && !string.Equals(lastgame, datItem.Machine!.Name, StringComparison.OrdinalIgnoreCase))
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame is null || !string.Equals(lastgame, datItem.GetMachine()!.Name, StringComparison.OrdinalIgnoreCase))
if (lastgame is null || !string.Equals(lastgame, datItem.Machine!.Name, StringComparison.OrdinalIgnoreCase))
WriteStartGame(jtw, datItem);
// Check for a "null" item
@@ -473,7 +473,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
WriteDatItem(jtw, datItem);
// Set the new data to compare against
lastgame = datItem.GetMachine()!.Name;
lastgame = datItem.Machine!.Name;
}
}
@@ -601,8 +601,8 @@ namespace SabreTools.Metadata.DatFiles.Formats
private static void WriteStartGame(JsonTextWriter jtw, DatItem datItem)
{
// No game should start with a path separator
if (!string.IsNullOrEmpty(datItem.GetMachine()!.Name))
datItem.GetMachine()!.Name = datItem.GetMachine()!.Name!.TrimStart(Path.DirectorySeparatorChar);
if (!string.IsNullOrEmpty(datItem.Machine!.Name))
datItem.Machine!.Name = datItem.Machine!.Name!.TrimStart(Path.DirectorySeparatorChar);
// Build the state
jtw.WriteStartObject();
@@ -610,7 +610,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
// Write the Machine
jtw.WritePropertyName("machine");
JsonSerializer js = new() { Formatting = Formatting.Indented };
js.Serialize(jtw, datItem.GetMachine()!);
js.Serialize(jtw, datItem.Machine!);
jtw.WritePropertyName("items");
jtw.WriteStartArray();
@@ -641,7 +641,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
private void WriteDatItem(JsonTextWriter jtw, DatItem datItem)
{
// Get the machine for the item
var machine = datItem.GetMachine();
var machine = datItem.Machine;
// Pre-process the item name
ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false);

View File

@@ -282,11 +282,11 @@ namespace SabreTools.Metadata.DatFiles.Formats
DatItem datItem = datItems[index];
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame is not null && !string.Equals(lastgame, datItem.GetMachine()!.Name, StringComparison.OrdinalIgnoreCase))
if (lastgame is not null && !string.Equals(lastgame, datItem.Machine!.Name, StringComparison.OrdinalIgnoreCase))
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame is null || !string.Equals(lastgame, datItem.GetMachine()!.Name, StringComparison.OrdinalIgnoreCase))
if (lastgame is null || !string.Equals(lastgame, datItem.Machine!.Name, StringComparison.OrdinalIgnoreCase))
WriteStartGame(xtw, datItem);
// Check for a "null" item
@@ -297,7 +297,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
WriteDatItem(xtw, datItem);
// Set the new data to compare against
lastgame = datItem.GetMachine()!.Name;
lastgame = datItem.Machine!.Name;
}
}
@@ -429,14 +429,14 @@ namespace SabreTools.Metadata.DatFiles.Formats
private static void WriteStartGame(XmlTextWriter xtw, DatItem datItem)
{
// No game should start with a path separator
datItem.GetMachine()!.Name = datItem.GetMachine()!.Name?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty;
datItem.Machine!.Name = datItem.Machine!.Name?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty;
// Write the machine
xtw.WriteStartElement("directory");
XmlSerializer xs = new(typeof(Machine));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, datItem.GetMachine(), ns);
xs.Serialize(xtw, datItem.Machine, ns);
xtw.WriteStartElement("files");
@@ -466,7 +466,7 @@ namespace SabreTools.Metadata.DatFiles.Formats
private void WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{
// Get the machine for the item
var machine = datItem.GetMachine();
var machine = datItem.Machine;
// Pre-process the item name
ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false);

View File

@@ -437,9 +437,9 @@ namespace SabreTools.Metadata.DatFiles
var lastSource = last.Read<Source?>(DatItem.SourceKey);
// Get the machines for comparison
var selfMachine = self.GetMachine();
var selfMachine = self.Machine;
string? selfMachineName = selfMachine?.Name;
var lastMachine = last.GetMachine();
var lastMachine = last.Machine;
string? lastMachineName = lastMachine?.Name;
// If the duplicate is external already
@@ -546,8 +546,8 @@ namespace SabreTools.Metadata.DatFiles
var itemSource = datItem.Read<Source?>(DatItem.SourceKey);
// Get the machines associated with the items
var savedMachine = savedItem.GetMachine();
var itemMachine = datItem.GetMachine();
var savedMachine = savedItem.Machine;
var itemMachine = datItem.Machine;
// If the current source has a lower ID than the saved, use the saved source
if (itemSource?.Index < savedSource?.Index)
@@ -732,7 +732,7 @@ namespace SabreTools.Metadata.DatFiles
bucketBy = ItemKey.Machine;
// Get the machine and source
var machine = datItem.GetMachine();
var machine = datItem.Machine;
var source = datItem.Read<Source?>(DatItem.SourceKey);
// Get the bucket key
@@ -771,7 +771,7 @@ namespace SabreTools.Metadata.DatFiles
continue;
// Get the machine and source
var machine = item.GetMachine();
var machine = item.Machine;
var source = item.Read<Source?>(DatItem.SourceKey);
// We want to get the key most appropriate for the given sorting type
@@ -852,8 +852,8 @@ namespace SabreTools.Metadata.DatFiles
}
// Get the machines
Machine? xMachine = x.GetMachine();
Machine? yMachine = y.GetMachine();
Machine? xMachine = x.Machine;
Machine? yMachine = y.Machine;
// If machine names don't match
string? xMachineName = xMachine?.Name;