2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
2024-10-30 10:59:04 -04:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Text;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
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;
|
2025-05-02 22:34:38 -04:00
|
|
|
|
using SabreTools.Core.Filter;
|
2020-12-08 15:15:41 -08:00
|
|
|
|
using SabreTools.DatItems;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
2020-12-09 22:11:35 -08:00
|
|
|
|
namespace SabreTools.DatFiles.Formats
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
2020-09-07 22:57:44 -07:00
|
|
|
|
/// 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
|
2025-02-13 14:22:51 -05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-18 12:09:09 -08:00
|
|
|
|
/// <inheritdoc/>
|
2025-05-02 22:34:38 -04:00
|
|
|
|
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
|
2023-08-10 23:22:14 -04:00
|
|
|
|
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);
|
2025-01-12 22:10:48 -05:00
|
|
|
|
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":
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(DatHeader));
|
2023-08-10 23:22:14 -04:00
|
|
|
|
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;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2019-01-11 13:43:15 -08:00
|
|
|
|
case "directory":
|
2025-05-02 22:34:38 -04:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-12 15:54:14 -08:00
|
|
|
|
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}'");
|
2023-08-10 23:22:14 -04:00
|
|
|
|
|
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
|
2023-08-10 23:22:14 -04:00
|
|
|
|
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>
|
2020-12-23 13:55:09 -08:00
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
2024-03-20 01:15:07 -04:00
|
|
|
|
/// <param name="source">Source representing the DAT</param>
|
|
|
|
|
|
/// <param name="sourceIndex">Index of the Source representing the DAT</param>
|
2025-05-02 22:34:38 -04:00
|
|
|
|
/// <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
|
2023-08-10 23:22:14 -04:00
|
|
|
|
Machine? machine = null;
|
2024-03-19 15:21:01 -04:00
|
|
|
|
long machineIndex = -1;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-07 22:21:02 -07:00
|
|
|
|
// Otherwise, read the directory
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.MoveToContent();
|
|
|
|
|
|
while (!xtr.EOF)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (xtr.NodeType != XmlNodeType.Element)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.Read();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
switch (xtr.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "machine":
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(Machine));
|
2023-08-10 23:22:14 -04:00
|
|
|
|
machine = xs?.Deserialize(xtr.ReadSubtree()) as Machine;
|
2025-05-02 22:34:38 -04:00
|
|
|
|
|
|
|
|
|
|
// If the machine doesn't pass the filter
|
|
|
|
|
|
if (machine != null && filterRunner != null && !machine.PassesFilter(filterRunner))
|
|
|
|
|
|
machine = null;
|
|
|
|
|
|
|
2024-03-19 15:21:01 -04:00
|
|
|
|
if (machine != null)
|
2025-01-12 22:10:48 -05:00
|
|
|
|
machineIndex = AddMachineDB(machine);
|
2024-03-19 15:21:01 -04:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.Skip();
|
|
|
|
|
|
break;
|
2020-09-01 13:36:32 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
case "files":
|
2025-05-02 22:34:38 -04:00
|
|
|
|
ReadFiles(xtr.ReadSubtree(),
|
|
|
|
|
|
machine,
|
|
|
|
|
|
machineIndex,
|
|
|
|
|
|
statsOnly,
|
|
|
|
|
|
source,
|
|
|
|
|
|
sourceIndex,
|
|
|
|
|
|
filterRunner);
|
2020-09-01 13:36:32 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -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>
|
2024-03-19 15:21:01 -04:00
|
|
|
|
/// <param name="machineIndex">Index of the Machine to add to the parsed items</param>
|
2020-12-23 13:55:09 -08:00
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
2024-03-20 01:15:07 -04:00
|
|
|
|
/// <param name="source">Source representing the DAT</param>
|
|
|
|
|
|
/// <param name="sourceIndex">Index of the Source representing the DAT</param>
|
2025-05-02 22:34:38 -04:00
|
|
|
|
/// <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
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.MoveToContent();
|
|
|
|
|
|
while (!xtr.EOF)
|
2020-09-01 13:36:32 -07:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (xtr.NodeType != XmlNodeType.Element)
|
2020-09-01 13:36:32 -07:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.Read();
|
|
|
|
|
|
continue;
|
2020-09-01 13:36:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
switch (xtr.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "datitem":
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(DatItem));
|
2023-08-10 23:22:14 -04:00
|
|
|
|
if (xs.Deserialize(xtr.ReadSubtree()) is DatItem item)
|
|
|
|
|
|
{
|
2025-05-02 22:34:38 -04:00
|
|
|
|
// If the item doesn't pass the filter
|
|
|
|
|
|
if (filterRunner != null && !item.PassesFilter(filterRunner))
|
|
|
|
|
|
{
|
|
|
|
|
|
xtr.Skip();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-10 23:22:14 -04:00
|
|
|
|
item.CopyMachineInformation(machine);
|
2024-03-20 01:15:07 -04:00
|
|
|
|
item.SetFieldValue<Source?>(DatItem.SourceKey, source);
|
2025-01-12 22:10:48 -05:00
|
|
|
|
AddItem(item, statsOnly);
|
2025-05-02 00:28:22 -04:00
|
|
|
|
// AddItemDB(item, machineIndex, sourceIndex, statsOnly);
|
2023-08-10 23:22:14 -04:00
|
|
|
|
}
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtr.Skip();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
xtr.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2020-09-01 13:36:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-18 12:09:09 -08:00
|
|
|
|
/// <inheritdoc/>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlTextWriter xtw = new(fs, new UTF8Encoding(false))
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
IndentChar = '\t',
|
2020-09-07 22:21:02 -07:00
|
|
|
|
Indentation = 1,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
};
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
2020-06-10 22:37:19 -07:00
|
|
|
|
WriteHeader(xtw);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
2023-08-10 23:22:14 -04:00
|
|
|
|
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
|
|
|
|
{
|
2025-01-12 23:15:30 -05: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
|
2025-01-07 15:28:01 -05:00
|
|
|
|
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
|
2020-06-10 22:37:19 -07:00
|
|
|
|
xtw.Dispose();
|
2024-02-28 22:54:56 -05:00
|
|
|
|
#endif
|
2019-01-11 13:43:15 -08:00
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
2021-01-12 15:54:14 -08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 11:22:33 -04:00
|
|
|
|
/// <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}'...");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
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");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
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
|
2025-01-12 23:15:30 -05:00
|
|
|
|
var itemsDict = GetItemsForBucketDB(key, filter: true);
|
2025-01-09 10:16:39 -05:00
|
|
|
|
if (itemsDict == null || !ContainsWritable([.. itemsDict.Values]))
|
2024-03-20 11:22:33 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve the names in the block
|
2025-01-07 15:28:01 -05:00
|
|
|
|
var items = ResolveNamesDB([.. itemsDict]);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var kvp in items)
|
2024-03-20 11:22:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Get the machine for the item
|
2025-02-24 09:20:46 -05:00
|
|
|
|
var machine = GetMachineForItemDB(kvp.Key);
|
2024-03-20 11:22:33 -04: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:05:08 -04:00
|
|
|
|
if (lastgame != null && !string.Equals(lastgame, machine.Value!.GetName(), StringComparison.OrdinalIgnoreCase))
|
2024-03-20 11:22:33 -04:00
|
|
|
|
WriteEndGame(xtw);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a new game, output the beginning of the new item
|
2025-05-02 16:05:08 -04:00
|
|
|
|
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetName(), StringComparison.OrdinalIgnoreCase))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
WriteStartGame(xtw, kvp.Value);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Check for a "null" item
|
2025-01-09 10:01:56 -05:00
|
|
|
|
var datItem = new KeyValuePair<long, DatItem>(kvp.Key, ProcessNullifiedItem(kvp.Value));
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Write out the item if we're not ignoring
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!ShouldIgnore(datItem.Value, ignoreblanks))
|
2025-01-09 10:05:16 -05:00
|
|
|
|
WriteDatItemDB(xtw, datItem);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
2025-05-02 16:05:08 -04:00
|
|
|
|
lastgame = machine.Value!.GetName();
|
2024-03-20 11:22:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Write the file footer out
|
|
|
|
|
|
WriteFooter(xtw);
|
|
|
|
|
|
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.User($"'{outfile}' written!{Environment.NewLine}");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
#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);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DAT header using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <param name="xtw">XmlTextWriter to output to</param>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
private void WriteHeader(XmlTextWriter xtw)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.WriteStartDocument();
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.WriteStartElement("datafile");
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(DatHeader));
|
|
|
|
|
|
XmlSerializerNamespaces ns = new();
|
2020-09-15 14:23:40 -07:00
|
|
|
|
ns.Add("", "");
|
|
|
|
|
|
xs.Serialize(xtw, Header, ns);
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.WriteStartElement("data");
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game start using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
|
|
|
{
|
2020-09-15 14:23:40 -07: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
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// Write the machine
|
|
|
|
|
|
xtw.WriteStartElement("directory");
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(Machine));
|
|
|
|
|
|
XmlSerializerNamespaces ns = new();
|
2020-09-15 14:23:40 -07:00
|
|
|
|
ns.Add("", "");
|
2025-05-02 16:46:20 -04:00
|
|
|
|
xs.Serialize(xtw, datItem.GetMachine(), ns);
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.WriteStartElement("files");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game start using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End files
|
|
|
|
|
|
xtw.WriteEndElement();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End directory
|
|
|
|
|
|
xtw.WriteEndElement();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <param name="xtw">XmlTextWriter to output to</param>
|
|
|
|
|
|
/// <param name="datItem">DatItem object to be output</param>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
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
|
|
|
|
|
2020-09-15 14:23:40 -07: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
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// Write the DatItem
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlSerializer xs = new(typeof(DatItem));
|
|
|
|
|
|
XmlSerializerNamespaces ns = new();
|
2020-09-15 14:23:40 -07:00
|
|
|
|
ns.Add("", "");
|
|
|
|
|
|
xs.Serialize(xtw, datItem, ns);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07: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
|
2025-02-24 09:20:46 -05:00
|
|
|
|
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>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End files
|
|
|
|
|
|
xtw.WriteEndElement();
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End directory
|
|
|
|
|
|
xtw.WriteEndElement();
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End data
|
|
|
|
|
|
xtw.WriteEndElement();
|
2020-09-15 12:12:13 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// End datafile
|
|
|
|
|
|
xtw.WriteEndElement();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
xtw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|