diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index a7a29885..435679a8 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -195,55 +195,17 @@ namespace SabreTools.DatFiles /// /// DatItem to update /// True if the Quotes flag should be ignored, false otherwise - /// True if the UseRomName should be always on (default), false otherwise - protected void ProcessItemName(DatItem item, bool forceRemoveQuotes, bool forceRomName = true) + /// True if the UseRomName should be always on, false otherwise + protected void ProcessItemName(DatItem item, Machine? machine, bool forceRemoveQuotes, bool forceRomName) { // Get the relevant processing values bool quotes = forceRemoveQuotes ? false : Header.GetBoolFieldValue(DatHeader.QuotesKey) ?? false; bool useRomName = forceRomName ? true : Header.GetBoolFieldValue(DatHeader.UseRomNameKey) ?? false; - // Get the machine - var machine = item.GetFieldValue(DatItem.MachineKey); - if (machine == null) - return; - - // Process the item name with common rules - ProcessItemNameImpl(item, machine, quotes, useRomName); - } - - /// - /// Process an item and correctly set the item name - /// - /// DatItem to update - /// True if the Quotes flag should be ignored, false otherwise - /// True if the UseRomName should be always on (default), false otherwise - protected void ProcessItemNameDB(KeyValuePair item, bool forceRemoveQuotes, bool forceRomName = true) - { - // Get the relevant processing values - bool quotes = forceRemoveQuotes ? false : Header.GetBoolFieldValue(DatHeader.QuotesKey) ?? false; - bool useRomName = forceRomName ? true : Header.GetBoolFieldValue(DatHeader.UseRomNameKey) ?? false; - - // Get machine for the item - var machine = ItemsDB.GetMachineForItem(item.Key); - if (machine.Value == null) - return; - - // Process the item name with common rules - ProcessItemNameImpl(item.Value, machine.Value, quotes, useRomName); - } - - /// - /// Process an item and correctly set the item name - /// - /// DatItem to update - /// True if the Quotes flag should be ignored, false otherwise - /// True if the UseRomName should be always on (default), false otherwise - private void ProcessItemNameImpl(DatItem item, Machine machine, bool quotes, bool useRomName) - { // Get the name to update string? name = (useRomName == true ? item.GetName() - : machine.GetStringFieldValue(Models.Metadata.Machine.NameKey)) ?? string.Empty; + : machine?.GetStringFieldValue(Models.Metadata.Machine.NameKey)) ?? string.Empty; // Create the proper Prefix and Postfix string pre = CreatePrefix(item, machine, quotes); @@ -307,7 +269,7 @@ namespace SabreTools.DatFiles name += addExtension; if (useRomName && Header.GetBoolFieldValue(DatHeader.GameNameKey) == true) - name = Path.Combine(machine.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty, name); + name = Path.Combine(machine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty, name); // Now assign back the formatted name name = $"{pre}{name}{post}"; diff --git a/SabreTools.DatFiles/Formats/Missfile.cs b/SabreTools.DatFiles/Formats/Missfile.cs index 961ab20a..6e32ba88 100644 --- a/SabreTools.DatFiles/Formats/Missfile.cs +++ b/SabreTools.DatFiles/Formats/Missfile.cs @@ -170,8 +170,11 @@ namespace SabreTools.DatFiles.Formats /// The name of the last game to be output private void WriteDatItem(StreamWriter sw, DatItem datItem, string? lastgame) { + // Get the machine for the item + var machine = datItem.GetFieldValue(DatItem.MachineKey); + // Process the item name - ProcessItemName(datItem, false, forceRomName: false); + ProcessItemName(datItem, machine, false, forceRomName: false); // Romba mode automatically uses item name if (Header.GetFieldValue(DatHeader.OutputDepotKey)?.IsActive == true || Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true) @@ -194,7 +197,7 @@ namespace SabreTools.DatFiles.Formats var machine = ItemsDB.GetMachineForItem(datItem.Key); // Process the item name - ProcessItemNameDB(datItem, false, forceRomName: false); + ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: false, forceRomName: false); // Romba mode automatically uses item name if (Header.GetFieldValue(DatHeader.OutputDepotKey)?.IsActive == true diff --git a/SabreTools.DatFiles/Formats/SabreJSON.cs b/SabreTools.DatFiles/Formats/SabreJSON.cs index 6b00947e..61ba3b32 100644 --- a/SabreTools.DatFiles/Formats/SabreJSON.cs +++ b/SabreTools.DatFiles/Formats/SabreJSON.cs @@ -592,8 +592,11 @@ namespace SabreTools.DatFiles.Formats /// DatItem object to be output private void WriteDatItem(JsonTextWriter jtw, DatItem datItem) { + // Get the machine for the item + var machine = datItem.GetFieldValue(DatItem.MachineKey); + // Pre-process the item name - ProcessItemName(datItem, true); + ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false); // Build the state jtw.WriteStartObject(); @@ -616,8 +619,11 @@ namespace SabreTools.DatFiles.Formats /// DatItem object to be output private void WriteDatItemDB(JsonTextWriter jtw, KeyValuePair datItem) { + // Get the machine for the item + var machine = ItemsDB.GetMachineForItem(datItem.Key); + // Pre-process the item name - ProcessItemNameDB(datItem, true); + ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: true, forceRomName: false); // Build the state jtw.WriteStartObject(); diff --git a/SabreTools.DatFiles/Formats/SabreXML.cs b/SabreTools.DatFiles/Formats/SabreXML.cs index d49d5487..68b00687 100644 --- a/SabreTools.DatFiles/Formats/SabreXML.cs +++ b/SabreTools.DatFiles/Formats/SabreXML.cs @@ -421,8 +421,11 @@ namespace SabreTools.DatFiles.Formats /// DatItem object to be output private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { + // Get the machine for the item + var machine = datItem.GetFieldValue(DatItem.MachineKey); + // Pre-process the item name - ProcessItemName(datItem, true); + ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false); // Write the DatItem XmlSerializer xs = new(typeof(DatItem)); @@ -440,8 +443,11 @@ namespace SabreTools.DatFiles.Formats /// DatItem object to be output private void WriteDatItemDB(XmlTextWriter xtw, KeyValuePair datItem) { + // Get the machine for the item + var machine = ItemsDB.GetMachineForItem(datItem.Key); + // Pre-process the item name - ProcessItemNameDB(datItem, true); + ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: true, forceRomName: false); // Write the DatItem XmlSerializer xs = new(typeof(DatItem));