Increase readability of Machine access

This commit is contained in:
Matt Nadareski
2025-05-02 16:46:20 -04:00
parent d5ae1dffaa
commit 7d35594040
33 changed files with 124 additions and 114 deletions

View File

@@ -120,7 +120,7 @@ namespace SabreTools.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = item.GetMachine();
if (machine == null)
continue;
@@ -331,7 +331,7 @@ namespace SabreTools.DatFiles
DatItem item = GetItemsForBucket(key)[0];
// Get machine information
Machine? machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
Machine? machine = item.GetMachine();
string? machineName = machine?.GetName()?.ToLowerInvariant();
if (machine == null || machineName == null)
continue;
@@ -521,7 +521,7 @@ namespace SabreTools.DatFiles
return;
// Get the current machine
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = datItem.GetMachine();
if (machine == null)
return;
@@ -657,7 +657,7 @@ namespace SabreTools.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = item.GetMachine();
if (machine == null)
continue;
@@ -666,10 +666,10 @@ namespace SabreTools.DatFiles
string? machineDesc = machine.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey);
if (machineName != null && Regex.IsMatch(machineName, SceneNamePattern))
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetName(Regex.Replace(machineName, SceneNamePattern, "$2"));
item.GetMachine()!.SetName(Regex.Replace(machineName, SceneNamePattern, "$2"));
if (machineDesc != null && Regex.IsMatch(machineDesc, SceneNamePattern))
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Regex.Replace(machineDesc, SceneNamePattern, "$2"));
item.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Regex.Replace(machineDesc, SceneNamePattern, "$2"));
}
#if NET40_OR_GREATER || NETCOREAPP
});
@@ -742,7 +742,7 @@ namespace SabreTools.DatFiles
foreach (DatItem item in items)
{
// Get the current machine
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = item.GetMachine();
if (machine == null)
continue;

View File

@@ -248,7 +248,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -263,8 +263,8 @@ namespace SabreTools.DatFiles
if (parentItems.Count == 0)
{
copyFrom = new Rom();
copyFrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetName(cloneOf);
copyFrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, cloneOf);
copyFrom.GetMachine()!.SetName(cloneOf);
copyFrom.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, cloneOf);
}
else
{
@@ -325,7 +325,7 @@ namespace SabreTools.DatFiles
.Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey)))
{
if (subfolder)
rom.SetName($"{rom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()}\\{rom.GetName()}");
rom.SetName($"{rom.GetMachine()!.GetName()}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom);
AddItem(rom, statsOnly: false);
@@ -335,7 +335,7 @@ namespace SabreTools.DatFiles
else if (!GetItemsForBucket(cloneOf).Contains(item) || skipDedup)
{
if (subfolder)
rom.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()}\\{rom.GetName()}");
rom.SetName($"{item.GetMachine()!.GetName()}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom);
AddItem(rom, statsOnly: false);
@@ -346,7 +346,7 @@ namespace SabreTools.DatFiles
else if (!GetItemsForBucket(cloneOf).Contains(item))
{
if (subfolder)
item.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()}\\{item.GetName()}");
item.SetName($"{item.GetMachine()!.GetName()}\\{item.GetName()}");
item.CopyMachineInformation(copyFrom);
AddItem(item, statsOnly: false);
@@ -507,7 +507,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -532,10 +532,10 @@ namespace SabreTools.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].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
string? romof = GetItemsForBucket(cloneOf)[0].GetMachine()!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items)
{
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
item.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
}
}
}
@@ -626,7 +626,7 @@ namespace SabreTools.DatFiles
continue;
// If the machine (is/is not) a device, we want to continue
if (deviceOnly ^ (datItems[0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
if (deviceOnly ^ (datItems[0].GetMachine()!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
continue;
// Get all device reference names from the current machine
@@ -935,7 +935,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -1018,7 +1018,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -1085,7 +1085,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -1107,10 +1107,10 @@ namespace SabreTools.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].GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
string? romof = GetItemsForBucket(cloneOf)[0].GetMachine()!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (DatItem item in items)
{
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
item.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
}
}
}
@@ -1189,7 +1189,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine
var machine = items[0].GetFieldValue<Machine>(DatItem.MachineKey);
var machine = items[0].GetMachine();
if (machine == null)
continue;
@@ -1269,7 +1269,7 @@ namespace SabreTools.DatFiles
foreach (DatItem item in items)
{
// Get the machine
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = item.GetMachine();
if (machine == null)
continue;

View File

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

View File

@@ -1000,8 +1000,8 @@ namespace SabreTools.DatFiles
var nc = new NaturalComparer();
// If machine names don't match
string? xMachineName = x.GetFieldValue<Machine>(DatItem.MachineKey)?.GetName();
string? yMachineName = y.GetFieldValue<Machine>(DatItem.MachineKey)?.GetName();
string? xMachineName = x.GetMachine()?.GetName();
string? yMachineName = y.GetMachine()?.GetName();
if (xMachineName != yMachineName)
return nc.Compare(xMachineName, yMachineName);
@@ -1055,8 +1055,8 @@ namespace SabreTools.DatFiles
// TODO: Fix this since DB uses an external map for machines
// If machine names don't match
string? xMachineName = x.Value.GetFieldValue<Machine>(DatItem.MachineKey)?.GetName();
string? yMachineName = y.Value.GetFieldValue<Machine>(DatItem.MachineKey)?.GetName();
string? xMachineName = x.Value.GetMachine()?.GetName();
string? yMachineName = y.Value.GetMachine()?.GetName();
if (xMachineName != yMachineName)
return nc.Compare(xMachineName, yMachineName);

View File

@@ -82,7 +82,7 @@ namespace SabreTools.DatFiles.Formats
WriteDatItem(sw, datItem, lastgame);
// Set the new data to compare against
lastgame = datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName();
lastgame = datItem.GetMachine()!.GetName();
}
}
@@ -168,7 +168,7 @@ namespace SabreTools.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.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = datItem.GetMachine();
WriteDatItemImpl(sw, datItem, machine!, lastgame);
}

View File

@@ -412,11 +412,11 @@ namespace SabreTools.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 != null && !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName(), StringComparison.OrdinalIgnoreCase))
if (lastgame != null && !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName(), StringComparison.OrdinalIgnoreCase))
if (lastgame == null || !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteStartGame(jtw, datItem);
// Check for a "null" item
@@ -427,7 +427,7 @@ namespace SabreTools.DatFiles.Formats
WriteDatItem(jtw, datItem);
// Set the new data to compare against
lastgame = datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName();
lastgame = datItem.GetMachine()!.GetName();
}
}
@@ -555,8 +555,8 @@ namespace SabreTools.DatFiles.Formats
private static void WriteStartGame(JsonTextWriter jtw, DatItem datItem)
{
// No game should start with a path separator
if (!string.IsNullOrEmpty(datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()))
datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.SetName(datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()!.TrimStart(Path.DirectorySeparatorChar));
if (!string.IsNullOrEmpty(datItem.GetMachine()!.GetName()))
datItem.GetMachine()!.SetName(datItem.GetMachine()!.GetName()!.TrimStart(Path.DirectorySeparatorChar));
// Build the state
jtw.WriteStartObject();
@@ -564,7 +564,7 @@ namespace SabreTools.DatFiles.Formats
// Write the Machine
jtw.WritePropertyName("machine");
JsonSerializer js = new() { Formatting = Formatting.Indented };
js.Serialize(jtw, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!);
js.Serialize(jtw, datItem.GetMachine()!);
jtw.WritePropertyName("items");
jtw.WriteStartArray();
@@ -595,7 +595,7 @@ namespace SabreTools.DatFiles.Formats
private void WriteDatItem(JsonTextWriter jtw, DatItem datItem)
{
// Get the machine for the item
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = datItem.GetMachine();
// Pre-process the item name
ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false);

View File

@@ -240,11 +240,11 @@ namespace SabreTools.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 != null && !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName(), StringComparison.OrdinalIgnoreCase))
if (lastgame != null && !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName(), StringComparison.OrdinalIgnoreCase))
if (lastgame == null || !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteStartGame(xtw, datItem);
// Check for a "null" item
@@ -255,7 +255,7 @@ namespace SabreTools.DatFiles.Formats
WriteDatItem(xtw, datItem);
// Set the new data to compare against
lastgame = datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName();
lastgame = datItem.GetMachine()!.GetName();
}
}
@@ -387,14 +387,14 @@ namespace SabreTools.DatFiles.Formats
private static void WriteStartGame(XmlTextWriter xtw, DatItem datItem)
{
// No game should start with a path separator
datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.SetName(datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetName()?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty);
datItem.GetMachine()!.SetName(datItem.GetMachine()!.GetName()?.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.GetFieldValue<Machine>(DatItem.MachineKey), ns);
xs.Serialize(xtw, datItem.GetMachine(), ns);
xtw.WriteStartElement("files");
@@ -424,7 +424,7 @@ namespace SabreTools.DatFiles.Formats
private void WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{
// Get the machine for the item
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = datItem.GetMachine();
// Pre-process the item name
ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false);

View File

@@ -521,8 +521,8 @@ namespace SabreTools.DatFiles
var itemSource = datItem.GetFieldValue<Source?>(DatItem.SourceKey);
// Get the machines associated with the items
var savedMachine = savedItem.GetFieldValue<Machine>(DatItem.MachineKey);
var itemMachine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
var savedMachine = savedItem.GetMachine();
var itemMachine = datItem.GetMachine();
// If the current source has a lower ID than the saved, use the saved source
if (itemSource?.Index < savedSource?.Index)
@@ -690,7 +690,7 @@ namespace SabreTools.DatFiles
bucketBy = ItemKey.Machine;
// Get the machine and source
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = datItem.GetMachine();
var source = datItem.GetFieldValue<Source?>(DatItem.SourceKey);
// Get the bucket key
@@ -731,7 +731,7 @@ namespace SabreTools.DatFiles
continue;
// Get the machine and source
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
var machine = item.GetMachine();
var source = item.GetFieldValue<Source?>(DatItem.SourceKey);
// We want to get the key most appropriate for the given sorting type
@@ -803,8 +803,8 @@ namespace SabreTools.DatFiles
var nc = new NaturalComparer();
// Get the machines
Machine? xMachine = x.GetFieldValue<Machine>(DatItem.MachineKey);
Machine? yMachine = y.GetFieldValue<Machine>(DatItem.MachineKey);
Machine? xMachine = x.GetMachine();
Machine? yMachine = y.GetMachine();
// If machine names don't match
string? xMachineName = xMachine?.GetName();