Reduce unnecessary round-trip conversions

This commit is contained in:
Matt Nadareski
2024-12-06 23:16:09 -05:00
parent d78ff5eb67
commit c8c10659b1
34 changed files with 597 additions and 765 deletions

View File

@@ -28,7 +28,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -73,7 +73,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case Release release:

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -55,7 +55,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -30,7 +30,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -244,7 +244,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case BiosSet biosset:

View File

@@ -248,7 +248,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case Release release:

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.DatItems;
@@ -118,28 +119,30 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
// Check for a "null" item
var datItem = items[index];
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(datItem.Key);
// Write out the item if we're using machine names or we're not ignoring
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true || !ShouldIgnore(datItem, ignoreblanks))
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
|| !ShouldIgnore(datItem.Value, ignoreblanks))
{
WriteDatItemDB(sw, datItem, lastgame);
}
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
@@ -182,19 +185,25 @@ namespace SabreTools.DatFiles.Formats
/// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <param name="lastgame">The name of the last game to be output</param>
private void WriteDatItemDB(StreamWriter sw, (long, DatItem) datItem, string? lastgame)
private void WriteDatItemDB(StreamWriter sw, KeyValuePair<long, DatItem> datItem, string? lastgame)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(datItem.Key);
// Process the item name
ProcessItemNameDB(datItem, false, forceRomName: false);
// Romba mode automatically uses item name
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true || Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
sw.Write($"{datItem.Item2.GetName() ?? string.Empty}\n");
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true && machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
sw.Write($"{machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty}\n");
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true
|| Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
{
sw.Write($"{datItem.Value.GetName() ?? string.Empty}\n");
}
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
&& machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
{
sw.Write($"{machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty}\n");
}
sw.Flush();
}

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{

View File

@@ -60,7 +60,7 @@ The softwaredb.xml file contains information about rom mapper types
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -406,11 +406,11 @@ namespace SabreTools.DatFiles.Formats
// 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)!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteEndGame(jtw);
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)!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteStartGame(jtw, datItem);
WriteStartGame(jtw, datItem);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
@@ -425,7 +425,7 @@ namespace SabreTools.DatFiles.Formats
}
// Write the file footer out
SabreJSON.WriteFooter(jtw);
WriteFooter(jtw);
logger.User($"'{outfile}' written!{Environment.NewLine}");
jtw.Close();
@@ -473,42 +473,40 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
var datItem = items[index];
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(kvp.Key);
// 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, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteEndGame(jtw);
if (lastgame != null && !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGameDB(jtw, datItem);
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGame(jtw, kvp.Value);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItemDB(jtw, datItem);
if (!ShouldIgnore(datItem.Value, ignoreblanks))
WriteDatItem(jtw, datItem.Value);
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
// Write the file footer out
SabreJSON.WriteFooter(jtw);
WriteFooter(jtw);
logger.User($"'{outfile}' written!{Environment.NewLine}");
jtw.Close();
@@ -567,34 +565,6 @@ namespace SabreTools.DatFiles.Formats
jtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied JsonTextWriter
/// </summary>
/// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteStartGameDB(JsonTextWriter jtw, (long, DatItem) datItem)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
// No game should start with a path separator
if (!string.IsNullOrEmpty(machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)))
machine.Item2!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!.TrimStart(Path.DirectorySeparatorChar));
// Build the state
jtw.WriteStartObject();
// Write the Machine
jtw.WritePropertyName("machine");
JsonSerializer js = new() { Formatting = Formatting.Indented };
js.Serialize(jtw, machine.Item2!);
jtw.WritePropertyName("items");
jtw.WriteStartArray();
jtw.Flush();
}
/// <summary>
/// Write out Game end using the supplied JsonTextWriter
/// </summary>
@@ -634,30 +604,6 @@ namespace SabreTools.DatFiles.Formats
jtw.Flush();
}
/// <summary>
/// Write out DatItem using the supplied JsonTextWriter
/// </summary>
/// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItemDB(JsonTextWriter jtw, (long, DatItem) datItem)
{
// Pre-process the item name
ProcessItemNameDB(datItem, true);
// Build the state
jtw.WriteStartObject();
// Write the DatItem
jtw.WritePropertyName("datitem");
JsonSerializer js = new() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented };
js.Serialize(jtw, datItem.Item2);
// End item
jtw.WriteEndObject();
jtw.Flush();
}
/// <summary>
/// Write out DAT footer using the supplied JsonTextWriter
/// </summary>

View File

@@ -303,37 +303,35 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
var datItem = items[index];
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(kvp.Key);
// 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, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
if (lastgame != null && !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGameDB(xtw, datItem);
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGame(xtw, kvp.Value);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItemDB(xtw, datItem);
if (!ShouldIgnore(datItem.Value, ignoreblanks))
WriteDatItem(xtw, datItem.Value);
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
@@ -397,31 +395,6 @@ namespace SabreTools.DatFiles.Formats
xtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteStartGameDB(XmlTextWriter xtw, (long, DatItem) datItem)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
// No game should start with a path separator
machine.Item2!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty);
// Write the machine
xtw.WriteStartElement("directory");
XmlSerializer xs = new(typeof(Machine));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, machine.Item2, ns);
xtw.WriteStartElement("files");
xtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
@@ -456,25 +429,6 @@ namespace SabreTools.DatFiles.Formats
xtw.Flush();
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItemDB(XmlTextWriter xtw, (long, DatItem) datItem)
{
// Pre-process the item name
ProcessItemNameDB(datItem, true);
// Write the DatItem
XmlSerializer xs = new(typeof(DatItem));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, datItem.Item2, ns);
xtw.Flush();
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>

View File

@@ -54,7 +54,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -96,7 +96,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{