Files
SabreTools/SabreTools.DatFiles/Formats/OfflineList.cs

970 lines
34 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-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
2020-12-07 15:08:57 -08:00
using SabreTools.IO;
namespace SabreTools.DatFiles.Formats
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of an OfflineList XML DAT
/// </summary>
internal class OfflineList : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public OfflineList(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse an OfflineList XML DAT and return all found games and roms within
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
2020-12-08 00:13:22 -08:00
XmlReader xtr = XmlReader.Create(filename, new XmlReaderSettings
{
CheckCharacters = false,
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
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 "configuration":
ReadConfiguration(xtr.ReadSubtree(), keep);
// Skip the configuration node now that we've processed it
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
case "games":
ReadGames(xtr.ReadSubtree(), filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the games node now that we've processed it
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
xtr.Read();
break;
}
}
}
catch (Exception ex)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read configuration information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
private void ReadConfiguration(XmlReader reader, bool keep)
{
bool superdat = false;
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all configuration items (ONLY OVERWRITE IF THERE'S NO DATA)
string content;
2019-01-11 13:43:15 -08:00
switch (reader.Name.ToLowerInvariant())
{
case "datname":
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.Name = Header.Name ?? content;
2019-01-11 13:43:15 -08:00
superdat = superdat || content.Contains(" - SuperDAT");
if (keep && superdat)
{
2020-10-07 16:11:05 -07:00
Header.Type = Header.Type ?? "SuperDAT";
2019-01-11 13:43:15 -08:00
}
break;
2019-01-11 13:43:15 -08:00
case "datversion":
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.Version = Header.Version ?? content;
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "system":
2020-08-20 15:51:13 -07:00
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.System = Header.System ?? content;
2019-01-11 13:43:15 -08:00
break;
2020-08-20 15:51:13 -07:00
// TODO: Int32?
2019-01-11 13:43:15 -08:00
case "screenshotswidth":
2020-08-20 15:51:13 -07:00
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.ScreenshotsWidth = Header.ScreenshotsWidth ?? content;
2019-01-11 13:43:15 -08:00
break;
2020-08-20 15:51:13 -07:00
// TODO: Int32?
2019-01-11 13:43:15 -08:00
case "screenshotsheight":
2020-08-20 15:51:13 -07:00
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.ScreenshotsHeight = Header.ScreenshotsHeight ?? content;
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "infos":
ReadInfos(reader.ReadSubtree());
// Skip the infos node now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "canopen":
ReadCanOpen(reader.ReadSubtree());
// Skip the canopen node now that we've processed it
reader.Skip();
break;
2020-08-20 16:30:16 -07:00
// TODO: Use all header values
2019-01-11 13:43:15 -08:00
case "newdat":
ReadNewDat(reader.ReadSubtree());
// Skip the newdat node now that we've processed it
reader.Skip();
break;
2020-08-20 15:51:13 -07:00
// TODO: Use header values
2019-01-11 13:43:15 -08:00
case "search":
ReadSearch(reader.ReadSubtree());
// Skip the search node now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "romtitle":
2020-08-20 15:51:13 -07:00
content = reader.ReadElementContentAsString();
2020-10-07 16:11:05 -07:00
Header.RomTitle = Header.RomTitle ?? content;
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read infos information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
private void ReadInfos(XmlReader reader)
{
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
2020-08-20 16:20:22 -07:00
// Setup the infos object
2020-08-21 15:31:19 -07:00
Header.Infos = new List<OfflineListInfo>();
2020-08-20 16:20:22 -07:00
2019-01-11 13:43:15 -08:00
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
2020-08-21 15:31:19 -07:00
// Add all infos to the info list
2020-08-24 22:25:47 -07:00
switch (reader.Name.ToLowerInvariant())
{
case "info":
2020-10-07 16:11:05 -07:00
var info = new OfflineListInfo
{
Name = reader.Name.ToLowerInvariant(),
Visible = reader.GetAttribute("visible").AsYesNo(),
InNamingOption = reader.GetAttribute("inNamingOption").AsYesNo(),
Default = reader.GetAttribute("default").AsYesNo()
};
2020-08-24 22:25:47 -07:00
Header.Infos.Add(info);
reader.Read();
break;
default:
reader.Read();
break;
}
2019-01-11 13:43:15 -08:00
}
}
/// <summary>
/// Read canopen information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
private void ReadCanOpen(XmlReader reader)
{
// Prepare all internal variables
2020-08-20 16:30:16 -07:00
Header.CanOpen = new List<string>();
2019-01-11 13:43:15 -08:00
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all canopen items
switch (reader.Name.ToLowerInvariant())
{
case "extension":
2020-08-20 16:30:16 -07:00
Header.CanOpen.Add(reader.ReadElementContentAsString());
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read newdat information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
private void ReadNewDat(XmlReader reader)
{
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all newdat items
string content;
2019-01-11 13:43:15 -08:00
switch (reader.Name.ToLowerInvariant())
{
case "datversionurl":
2020-08-22 12:56:38 -07:00
// TODO: Read this into an appropriate field
2019-01-11 13:43:15 -08:00
content = reader.ReadElementContentAsString();
Header.Url = (string.IsNullOrWhiteSpace(Header.Url) ? content : Header.Url);
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "daturl":
2020-08-22 12:56:38 -07:00
// TODO: Read this into an appropriate structure
reader.GetAttribute("fileName");
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "imurl":
2020-08-22 12:56:38 -07:00
// TODO: Read this into an appropriate field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read search information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
private void ReadSearch(XmlReader reader)
{
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all search items
switch (reader.Name.ToLowerInvariant())
{
case "to":
2020-08-22 12:56:38 -07:00
// TODO: Read this into an appropriate structure
reader.GetAttribute("value");
reader.GetAttribute("default"); // (true|false)
reader.GetAttribute("auto"); // (true|false)
2019-01-11 13:43:15 -08:00
ReadTo(reader.ReadSubtree());
// Skip the to node now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read to information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
private void ReadTo(XmlReader reader)
{
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all search items
switch (reader.Name.ToLowerInvariant())
{
case "find":
2020-08-22 12:56:38 -07:00
// TODO: Read this into an appropriate structure
reader.GetAttribute("operation");
reader.GetAttribute("value"); // Int32?
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read games information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2020-08-28 15:06:07 -07:00
private void ReadGames(XmlReader reader, string filename, int indexId)
2019-01-11 13:43:15 -08:00
{
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all games items (ONLY OVERWRITE IF THERE'S NO DATA)
switch (reader.Name.ToLowerInvariant())
{
case "game":
ReadGame(reader.ReadSubtree(), filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the game node now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read game information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2020-08-28 15:06:07 -07:00
private void ReadGame(XmlReader reader, string filename, int indexId)
2019-01-11 13:43:15 -08:00
{
// Prepare all internal variables
2020-08-22 12:56:38 -07:00
string releaseNumber = string.Empty, duplicateid;
long? size = null;
2020-08-22 12:56:38 -07:00
List<Rom> datItems = new List<Rom>();
2019-01-11 13:43:15 -08:00
Machine machine = new Machine();
// If there's no subtree to the configuration, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all games items
switch (reader.Name.ToLowerInvariant())
{
case "imagenumber":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "releasenumber":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
2019-01-11 13:43:15 -08:00
releaseNumber = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "title":
machine.Name = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "savetype":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "romsize":
size = Utilities.CleanLong(reader.ReadElementContentAsString());
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "publisher":
2020-08-22 12:56:38 -07:00
machine.Publisher = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "location":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "sourcerom":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "language":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "files":
2020-08-22 12:56:38 -07:00
datItems = ReadFiles(reader.ReadSubtree(), releaseNumber, machine.Name, filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the files node now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "im1crc":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "im2crc":
2020-08-22 12:56:38 -07:00
// TODO: Read this into a field
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "comment":
machine.Comment = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "duplicateid":
duplicateid = reader.ReadElementContentAsString();
if (duplicateid != "0")
machine.CloneOf = duplicateid;
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// Add information accordingly for each rom
2020-08-22 12:56:38 -07:00
for (int i = 0; i < datItems.Count; i++)
2019-01-11 13:43:15 -08:00
{
2020-08-22 12:56:38 -07:00
datItems[i].Size = size;
datItems[i].CopyMachineInformation(machine);
2019-01-11 13:43:15 -08:00
// Now process and add the rom
2020-08-22 12:56:38 -07:00
ParseAddHelper(datItems[i]);
2019-01-11 13:43:15 -08:00
}
}
/// <summary>
/// Read files information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="releaseNumber">Release number from the parent game</param>
/// <param name="machineName">Name of the parent game to use</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private List<Rom> ReadFiles(
XmlReader reader,
2019-01-11 13:43:15 -08:00
string releaseNumber,
string machineName,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
// Prepare all internal variables
2020-06-14 23:07:31 -07:00
var extensionToCrc = new List<KeyValuePair<string, string>>();
var roms = new List<Rom>();
2019-01-11 13:43:15 -08:00
// If there's no subtree to the configuration, skip it
if (reader == null)
return roms;
// Otherwise, add what is possible
reader.MoveToContent();
// Otherwise, read what we can from the header
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get all romCRC items
switch (reader.Name.ToLowerInvariant())
{
case "romcrc":
extensionToCrc.Add(
2020-06-14 23:07:31 -07:00
new KeyValuePair<string, string>(
reader.GetAttribute("extension") ?? string.Empty,
2019-01-11 13:43:15 -08:00
reader.ReadElementContentAsString().ToLowerInvariant()));
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// Now process the roms with the proper information
2020-06-14 23:07:31 -07:00
foreach (KeyValuePair<string, string> pair in extensionToCrc)
2019-01-11 13:43:15 -08:00
{
roms.Add(new Rom()
{
2020-06-14 23:07:31 -07:00
Name = (releaseNumber != "0" ? releaseNumber + " - " : string.Empty) + machineName + pair.Key,
CRC = pair.Value,
2019-01-11 13:43:15 -08:00
ItemStatus = ItemStatus.None,
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
});
}
return roms;
}
2020-09-18 17:12:31 -07:00
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[] { ItemType.Rom };
}
2019-01-11 13:43:15 -08:00
/// <summary>
/// Create and open an output file for writing direct from a dictionary
/// </summary>
/// <param name="outfile">Name of the file to write to</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
try
{
logger.User($"Opening file for writing: {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)
{
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 XmlTextWriter(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
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;
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
{
2020-08-28 15:06:07 -07:00
List<DatItem> datItems = Items.FilteredItems(key);
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
2020-08-28 15:06:07 -07:00
datItems = DatItem.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
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))
WriteDatItem(xtw, datItem);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-28 15:06:07 -07:00
lastgame = datItem.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
// Write the file footer out
WriteFooter(xtw);
2019-01-11 13:43:15 -08:00
logger.Verbose("File written!" + Environment.NewLine);
xtw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
logger.Error(ex);
if (throwOnError) throw ex;
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
/// <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(false);
xtw.WriteStartElement("dat");
xtw.WriteAttributeString("xsi", "xmlns", "http://www.w3.org/2001/XMLSchema-instance");
xtw.WriteAttributeString("noNamespaceSchemaLocation", "xsi", "datas.xsd");
xtw.WriteStartElement("configuration");
xtw.WriteRequiredElementString("datName", Header.Name);
xtw.WriteElementString("datVersion", Items.TotalCount.ToString());
xtw.WriteRequiredElementString("system", Header.System);
xtw.WriteRequiredElementString("screenshotsWidth", Header.ScreenshotsWidth);
xtw.WriteRequiredElementString("screenshotsHeight", Header.ScreenshotsHeight);
if (Header.Infos != null)
{
xtw.WriteStartElement("infos");
foreach (var info in Header.Infos)
{
xtw.WriteStartElement(info.Name);
xtw.WriteAttributeString("visible", info.Visible?.ToString());
xtw.WriteAttributeString("inNamingOption", info.InNamingOption?.ToString());
xtw.WriteAttributeString("default", info.Default?.ToString());
2020-08-20 16:20:22 -07:00
xtw.WriteEndElement();
}
2020-08-20 16:30:16 -07:00
// End infos
xtw.WriteEndElement();
}
2020-08-20 16:30:16 -07:00
if (Header.CanOpen != null)
{
xtw.WriteStartElement("canOpen");
foreach (string extension in Header.CanOpen)
{
xtw.WriteElementString("extension", extension);
}
// End canOpen
xtw.WriteEndElement();
}
xtw.WriteStartElement("newDat");
xtw.WriteRequiredElementString("datVersionURL", Header.Url);
xtw.WriteStartElement("datUrl");
xtw.WriteAttributeString("fileName", $"{Header.FileName ?? string.Empty}.zip");
xtw.WriteString(Header.Url);
xtw.WriteEndElement();
xtw.WriteRequiredElementString("imURL", Header.Url);
// End newDat
xtw.WriteEndElement();
xtw.WriteStartElement("search");
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "location");
xtw.WriteAttributeString("default", "true");
xtw.WriteAttributeString("auto", "true");
xtw.WriteEndElement();
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "romSize");
xtw.WriteAttributeString("default", "true");
xtw.WriteAttributeString("auto", "false");
xtw.WriteEndElement();
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "languages");
xtw.WriteAttributeString("default", "true");
xtw.WriteAttributeString("auto", "true");
xtw.WriteEndElement();
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "saveType");
xtw.WriteAttributeString("default", "false");
xtw.WriteAttributeString("auto", "false");
xtw.WriteEndElement();
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "publisher");
xtw.WriteAttributeString("default", "false");
xtw.WriteAttributeString("auto", "true");
xtw.WriteEndElement();
xtw.WriteStartElement("to");
xtw.WriteAttributeString("value", "sourceRom");
xtw.WriteAttributeString("default", "false");
xtw.WriteAttributeString("auto", "true");
xtw.WriteEndElement();
// End search
xtw.WriteEndElement();
xtw.WriteRequiredElementString("romTitle", Header.RomTitle ?? "%u - %n");
// End configuration
xtw.WriteEndElement();
2020-09-15 12:12:13 -07:00
xtw.WriteStartElement("games");
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>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private void WriteDatItem(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
// Build the state
xtw.WriteStartElement("game");
xtw.WriteElementString("imageNumber", "1");
xtw.WriteElementString("releaseNumber", "1");
xtw.WriteRequiredElementString("title", datItem.GetName() ?? string.Empty);
xtw.WriteElementString("saveType", "None");
2019-01-11 13:43:15 -08:00
if (datItem.ItemType == ItemType.Rom)
{
var rom = datItem as Rom;
xtw.WriteRequiredElementString("romSize", rom.Size?.ToString());
}
2019-01-11 13:43:15 -08:00
xtw.WriteRequiredElementString("publisher", datItem.Machine.Publisher);
xtw.WriteElementString("location", "0");
xtw.WriteElementString("sourceRom", "None");
xtw.WriteElementString("language", "0");
if (datItem.ItemType == ItemType.Rom)
{
var rom = datItem as Rom;
string tempext = "." + PathExtensions.GetNormalizedExtension(rom.Name);
xtw.WriteStartElement("files");
if (!string.IsNullOrWhiteSpace(rom.CRC))
{
xtw.WriteStartElement("romCRC");
xtw.WriteRequiredAttributeString("extension", tempext);
xtw.WriteString(rom.CRC?.ToUpperInvariant());
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
// End files
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
2020-09-15 12:12:13 -07:00
xtw.WriteElementString("im1CRC", "00000000");
xtw.WriteElementString("im2CRC", "00000000");
xtw.WriteRequiredElementString("comment", datItem.Machine.Comment);
xtw.WriteRequiredElementString("duplicateID", datItem.Machine.CloneOf);
2019-01-11 13:43:15 -08:00
// End game
xtw.WriteEndElement();
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>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private void WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End games
xtw.WriteEndElement();
xtw.WriteStartElement("gui");
xtw.WriteStartElement("images");
xtw.WriteAttributeString("width", "487");
xtw.WriteAttributeString("height", "162");
xtw.WriteStartElement("image");
xtw.WriteAttributeString("x", "0");
xtw.WriteAttributeString("y", "0");
xtw.WriteAttributeString("width", "240");
xtw.WriteAttributeString("height", "160");
xtw.WriteEndElement();
xtw.WriteStartElement("image");
xtw.WriteAttributeString("x", "245");
xtw.WriteAttributeString("y", "0");
xtw.WriteAttributeString("width", "240");
xtw.WriteAttributeString("height", "160");
xtw.WriteEndElement();
// End images
xtw.WriteEndElement();
// End gui
xtw.WriteEndElement();
2020-09-15 12:12:13 -07:00
// End dat
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}