Files
SabreTools/SabreTools.DatFiles/Formats/SabreXML.cs

521 lines
18 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
2020-12-08 00:13:22 -08:00
using System.Xml.Schema;
2020-09-07 22:21:02 -07:00
using System.Xml.Serialization;
using SabreTools.Core.Filter;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
namespace SabreTools.DatFiles.Formats
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a SabreDAT XML
2019-01-11 13:43:15 -08:00
/// </summary>
2025-02-14 14:49:48 -05:00
/// TODO: Transform this into direct serialization and deserialization of the Metadata type
public sealed class SabreXML : DatFile
2019-01-11 13:43:15 -08:00
{
2025-01-09 06:14:01 -05:00
/// <inheritdoc/>
public override ItemType[] SupportedTypes
=> Enum.GetValues(typeof(ItemType)) as ItemType[] ?? [];
2019-01-11 13:43:15 -08:00
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
2024-10-24 04:48:21 -04:00
public SabreXML(DatFile? datFile) : base(datFile)
2019-01-11 13:43:15 -08:00
{
2025-04-14 12:10:09 -04:00
Header.SetFieldValue<DatFormat>(DatHeader.DatFormatKey, DatFormat.SabreXML);
2019-01-11 13:43:15 -08:00
}
/// <inheritdoc/>
public override void ParseFile(string filename,
int indexId,
bool keep,
bool statsOnly = false,
FilterRunner? filterRunner = null,
bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
// Prepare all internal variables
XmlReader? xtr = XmlReader.Create(filename, new XmlReaderSettings
2020-12-08 00:13:22 -08:00
{
CheckCharacters = false,
2024-02-28 22:54:56 -05:00
#if NET40_OR_GREATER
2020-12-08 00:13:22 -08:00
DtdProcessing = DtdProcessing.Ignore,
2024-02-28 22:54:56 -05:00
#endif
2020-12-08 00:13:22 -08:00
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
2024-10-19 23:17:37 -04:00
var source = new Source(indexId, filename);
long sourceIndex = AddSourceDB(source);
2019-01-11 13:43:15 -08:00
// If we got a null reader, just return
if (xtr == null)
return;
// Otherwise, read the file to the end
try
{
xtr.MoveToContent();
while (!xtr.EOF)
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
{
xtr.Read();
continue;
}
switch (xtr.Name)
{
case "header":
XmlSerializer xs = new(typeof(DatHeader));
DatHeader? header = xs.Deserialize(xtr.ReadSubtree()) as DatHeader;
2025-01-30 10:22:20 -05:00
SetHeader(header);
2019-01-11 13:43:15 -08:00
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
case "directory":
ReadDirectory(xtr.ReadSubtree(), statsOnly, source, sourceIndex, filterRunner);
2019-01-11 13:43:15 -08:00
// Skip the directory node now that we've processed it
xtr.Read();
break;
default:
xtr.Read();
break;
}
}
}
catch (Exception ex) when (!throwOnError)
2019-01-11 13:43:15 -08:00
{
2025-01-08 16:59:44 -05:00
_logger.Warning(ex, $"Exception found while parsing '{filename}'");
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
2024-02-28 22:54:56 -05:00
#if NET452_OR_GREATER
xtr?.Dispose();
2024-02-28 22:54:56 -05:00
#endif
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read directory information
/// </summary>
2020-09-07 22:21:02 -07:00
/// <param name="xtr">XmlReader to use to parse the header</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="source">Source representing the DAT</param>
/// <param name="sourceIndex">Index of the Source representing the DAT</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param>
private void ReadDirectory(XmlReader xtr,
bool statsOnly,
Source source,
long sourceIndex,
FilterRunner? filterRunner)
2019-01-11 13:43:15 -08:00
{
2020-09-07 22:21:02 -07:00
// If the reader is invalid, skip
if (xtr == null)
return;
2019-01-11 13:43:15 -08:00
2020-09-07 22:21:02 -07:00
// Prepare internal variables
Machine? machine = null;
long machineIndex = -1;
2019-01-11 13:43:15 -08:00
2020-09-07 22:21:02 -07:00
// Otherwise, read the directory
xtr.MoveToContent();
while (!xtr.EOF)
2019-01-11 13:43:15 -08:00
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
2019-01-11 13:43:15 -08:00
{
xtr.Read();
continue;
}
2019-01-11 13:43:15 -08:00
switch (xtr.Name)
{
case "machine":
XmlSerializer xs = new(typeof(Machine));
machine = xs?.Deserialize(xtr.ReadSubtree()) as Machine;
// If the machine doesn't pass the filter
if (machine != null && filterRunner != null && !machine.PassesFilter(filterRunner))
machine = null;
if (machine != null)
machineIndex = AddMachineDB(machine);
xtr.Skip();
break;
2020-09-01 13:36:32 -07:00
case "files":
ReadFiles(xtr.ReadSubtree(),
machine,
machineIndex,
statsOnly,
source,
sourceIndex,
filterRunner);
2020-09-01 13:36:32 -07:00
// Skip the directory node now that we've processed it
xtr.Read();
break;
default:
xtr.Read();
break;
2020-09-01 13:36:32 -07:00
}
}
}
/// <summary>
2020-09-07 22:21:02 -07:00
/// Read Files information
2020-09-01 13:36:32 -07:00
/// </summary>
2020-09-07 22:21:02 -07:00
/// <param name="xtr">XmlReader to use to parse the header</param>
/// <param name="machine">Machine to copy information from</param>
/// <param name="machineIndex">Index of the Machine to add to the parsed items</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="source">Source representing the DAT</param>
/// <param name="sourceIndex">Index of the Source representing the DAT</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param>
private void ReadFiles(XmlReader xtr,
Machine? machine,
long machineIndex,
bool statsOnly,
Source source,
long sourceIndex,
FilterRunner? filterRunner)
2020-09-01 13:36:32 -07:00
{
2020-09-07 22:21:02 -07:00
// If the reader is invalid, skip
if (xtr == null)
2020-09-01 13:36:32 -07:00
return;
2020-09-07 22:21:02 -07:00
// Otherwise, read the items
xtr.MoveToContent();
while (!xtr.EOF)
2020-09-01 13:36:32 -07:00
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
2020-09-01 13:36:32 -07:00
{
xtr.Read();
continue;
2020-09-01 13:36:32 -07:00
}
switch (xtr.Name)
{
case "datitem":
XmlSerializer xs = new(typeof(DatItem));
if (xs.Deserialize(xtr.ReadSubtree()) is DatItem item)
{
// If the item doesn't pass the filter
if (filterRunner != null && !item.PassesFilter(filterRunner))
{
xtr.Skip();
break;
}
item.CopyMachineInformation(machine);
item.SetFieldValue<Source?>(DatItem.SourceKey, source);
AddItem(item, statsOnly);
2025-05-02 00:28:22 -04:00
// AddItemDB(item, machineIndex, sourceIndex, statsOnly);
}
xtr.Skip();
break;
default:
xtr.Read();
break;
}
2020-09-01 13:36:32 -07:00
}
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
try
{
2025-01-08 16:59:44 -05:00
_logger.User($"Writing to '{outfile}'...");
2020-12-08 00:13:22 -08:00
FileStream fs = File.Create(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
2025-01-08 16:59:44 -05:00
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
2019-01-11 13:43:15 -08:00
return false;
}
XmlTextWriter xtw = new(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
2020-09-07 22:21:02 -07:00
Indentation = 1,
};
2019-01-11 13:43:15 -08:00
// Write out the header
WriteHeader(xtw);
2019-01-11 13:43:15 -08:00
// Write out each of the machines and roms
string? lastgame = null;
2019-01-11 13:43:15 -08:00
2020-07-26 21:00:30 -07:00
// Use a sorted list of games to output
2020-07-26 22:34:45 -07:00
foreach (string key in Items.SortedKeys)
2019-01-11 13:43:15 -08:00
{
List<DatItem> datItems = GetItemsForBucket(key, filter: true);
2019-01-11 13:43:15 -08:00
2020-09-25 20:25:29 -07:00
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
datItems = ResolveNames(datItems);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
for (int index = 0; index < datItems.Count; index++)
2019-01-11 13:43:15 -08:00
{
2020-08-28 15:06:07 -07:00
DatItem datItem = datItems[index];
2019-01-11 13:43:15 -08:00
// If we have a different game and we're not at the start of the list, output the end of last item
2025-05-02 16:46:20 -04:00
if (lastgame != null && !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
2020-09-07 22:21:02 -07:00
WriteEndGame(xtw);
2019-01-11 13:43:15 -08:00
// If we have a new game, output the beginning of the new item
2025-05-02 16:46:20 -04:00
if (lastgame == null || !string.Equals(lastgame, datItem.GetMachine()!.GetName(), StringComparison.OrdinalIgnoreCase))
2020-09-07 22:21:02 -07:00
WriteStartGame(xtw, datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
2020-09-07 22:21:02 -07:00
WriteDatItem(xtw, datItem);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2025-05-02 16:46:20 -04:00
lastgame = datItem.GetMachine()!.GetName();
2019-01-11 13:43:15 -08:00
}
}
// Write the file footer out
2020-09-07 22:21:02 -07:00
WriteFooter(xtw);
2019-01-11 13:43:15 -08:00
2025-01-08 16:59:44 -05:00
_logger.User($"'{outfile}' written!{Environment.NewLine}");
2024-02-28 22:54:56 -05:00
#if NET452_OR_GREATER
xtw.Dispose();
2024-02-28 22:54:56 -05:00
#endif
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex) when (!throwOnError)
2019-01-11 13:43:15 -08:00
{
2025-01-08 16:59:44 -05:00
_logger.Error(ex);
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
/// <inheritdoc/>
public override bool WriteToFileDB(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
2025-01-08 16:59:44 -05:00
_logger.User($"Writing to '{outfile}'...");
FileStream fs = File.Create(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
2025-01-08 16:59:44 -05:00
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
XmlTextWriter xtw = new(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1,
};
// Write out the header
WriteHeader(xtw);
// Write out each of the machines and roms
string? lastgame = null;
// Use a sorted list of games to output
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var itemsDict = GetItemsForBucketDB(key, filter: true);
2025-01-09 10:16:39 -05:00
if (itemsDict == null || !ContainsWritable([.. itemsDict.Values]))
continue;
// Resolve the names in the block
var items = ResolveNamesDB([.. itemsDict]);
foreach (var kvp in items)
{
// Get the machine for the item
var machine = GetMachineForItemDB(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.Value!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetName(), StringComparison.OrdinalIgnoreCase))
WriteStartGame(xtw, kvp.Value);
// Check for a "null" item
var datItem = new KeyValuePair<long, DatItem>(kvp.Key, ProcessNullifiedItem(kvp.Value));
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem.Value, ignoreblanks))
2025-01-09 10:05:16 -05:00
WriteDatItemDB(xtw, datItem);
// Set the new data to compare against
lastgame = machine.Value!.GetName();
}
}
// Write the file footer out
WriteFooter(xtw);
2025-01-08 16:59:44 -05:00
_logger.User($"'{outfile}' written!{Environment.NewLine}");
#if NET452_OR_GREATER
xtw.Dispose();
#endif
fs.Dispose();
}
catch (Exception ex) when (!throwOnError)
{
2025-01-08 16:59:44 -05:00
_logger.Error(ex);
return false;
}
return true;
}
2019-01-11 13:43:15 -08:00
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteHeader(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartDocument();
xtw.WriteStartElement("datafile");
XmlSerializer xs = new(typeof(DatHeader));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, Header, ns);
xtw.WriteStartElement("data");
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
2024-02-28 19:19:50 -05:00
private static void WriteStartGame(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// No game should start with a path separator
2025-05-02 16:46:20 -04:00
datItem.GetMachine()!.SetName(datItem.GetMachine()!.GetName()?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty);
2019-01-11 13:43:15 -08:00
// Write the machine
xtw.WriteStartElement("directory");
XmlSerializer xs = new(typeof(Machine));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
2025-05-02 16:46:20 -04:00
xs.Serialize(xtw, datItem.GetMachine(), ns);
xtw.WriteStartElement("files");
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
2024-02-28 19:19:50 -05:00
private static void WriteEndGame(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End files
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
// End directory
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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 WriteDatItem(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
2025-01-09 10:13:47 -05:00
// Get the machine for the item
2025-05-02 16:46:20 -04:00
var machine = datItem.GetMachine();
2025-01-09 10:13:47 -05:00
// Pre-process the item name
2025-01-09 10:13:47 -05:00
ProcessItemName(datItem, machine, forceRemoveQuotes: true, forceRomName: false);
2020-09-15 12:12:13 -07:00
// Write the DatItem
XmlSerializer xs = new(typeof(DatItem));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, datItem, ns);
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
2025-01-09 10:05:16 -05:00
/// <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, KeyValuePair<long, DatItem> datItem)
{
2025-01-09 10:13:47 -05:00
// Get the machine for the item
var machine = GetMachineForItemDB(datItem.Key);
2025-01-09 10:13:47 -05:00
2025-01-09 10:05:16 -05:00
// Pre-process the item name
2025-01-09 10:13:47 -05:00
ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: true, forceRomName: false);
2025-01-09 10:05:16 -05:00
// Write the DatItem
XmlSerializer xs = new(typeof(DatItem));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, datItem, ns);
xtw.Flush();
}
2019-01-11 13:43:15 -08:00
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
2024-02-28 19:19:50 -05:00
private static void WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End files
xtw.WriteEndElement();
// End directory
xtw.WriteEndElement();
// End data
xtw.WriteEndElement();
2020-09-15 12:12:13 -07:00
// End datafile
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}