Files
SabreTools/SabreTools.Library/DatFiles/SoftwareList.cs

1046 lines
40 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
2020-08-01 23:04:11 -07:00
using SabreTools.Library.IO;
using SabreTools.Library.Tools;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a SofwareList, M1, or MAME XML DAT
/// </summary>
internal class SoftwareList : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public SoftwareList(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse an SofwareList 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>
protected override void ParseFile(
2019-01-11 13:43:15 -08:00
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
// Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader();
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 "softwarelist":
Header.Name = (Header.Name == null ? xtr.GetAttribute("name") ?? string.Empty : Header.Name);
Header.Description = (Header.Description == null ? xtr.GetAttribute("description") ?? string.Empty : Header.Description);
2020-08-20 20:38:29 -07:00
if (Header.ForceMerging == MergingFlag.None)
Header.ForceMerging = xtr.GetAttribute("forcemerging").AsMergingFlag();
2020-08-20 20:38:29 -07:00
if (Header.ForceNodump == NodumpFlag.None)
Header.ForceNodump = xtr.GetAttribute("forcenodump").AsNodumpFlag();
2020-08-20 20:38:29 -07:00
if (Header.ForcePacking == PackingFlag.None)
Header.ForcePacking = xtr.GetAttribute("forcepacking").AsPackingFlag();
2019-01-11 13:43:15 -08:00
xtr.Read();
break;
2019-01-11 13:43:15 -08:00
// We want to process the entire subtree of the machine
case "software":
ReadSoftware(xtr.ReadSubtree(), filename, indexId, keep);
2019-01-11 13:43:15 -08:00
// Skip the software now that we've processed it
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
xtr.Read();
break;
}
}
}
catch (Exception ex)
{
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}");
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read software information
/// </summary>
/// <param name="reader">XmlReader representing a software block</param>
/// <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>
private void ReadSoftware(
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
// If we have an empty software, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
bool containsItems = false;
// Create a new machine
MachineType machineType = MachineType.NULL;
if (reader.GetAttribute("isbios").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Bios;
if (reader.GetAttribute("isdevice").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Device;
if (reader.GetAttribute("ismechanical").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Mechanical;
Machine machine = new Machine
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("name"),
2020-08-22 13:31:13 -07:00
Supported = reader.GetAttribute("supported").AsSupported(),
2019-01-11 13:43:15 -08:00
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
2020-08-21 15:31:19 -07:00
Infos = new List<ListXmlInfo>(),
SharedFeatures = new List<SoftwareListSharedFeature>(),
DipSwitches = new List<ListXmlDipSwitch>(),
2019-01-11 13:43:15 -08:00
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
};
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the software
switch (reader.Name)
{
case "description":
machine.Description = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "publisher":
machine.Publisher = reader.ReadElementContentAsString();
break;
case "category":
machine.Category = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "info":
var info = new ListXmlInfo();
info.Name = reader.GetAttribute("name");
info.Value = reader.GetAttribute("value");
machine.Infos.Add(info);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "sharedfeat":
2020-08-23 21:10:29 -07:00
var sharedFeature = new SoftwareListSharedFeature();
sharedFeature.Name = reader.GetAttribute("name");
sharedFeature.Value = reader.GetAttribute("value");
machine.SharedFeatures.Add(sharedFeature);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "part": // Contains all rom and disk information
containsItems = ReadPart(reader.ReadSubtree(), machine, filename, indexId, keep);
2019-01-11 13:43:15 -08:00
// Skip the part now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
{
Blank blank = new Blank()
{
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
2020-08-20 13:17:14 -07:00
2019-01-11 13:43:15 -08:00
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank);
2019-01-11 13:43:15 -08:00
}
}
/// <summary>
/// Read part information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <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>
private bool ReadPart(
XmlReader reader,
Machine machine,
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
2020-08-21 13:31:22 -07:00
string areaname,
partname = string.Empty,
partinterface = string.Empty,
2020-08-21 13:33:17 -07:00
areaWidth,
areaEndinaness;
2019-01-11 13:43:15 -08:00
long? areasize = null;
2020-08-21 15:31:19 -07:00
var features = new List<SoftwareListFeature>();
2019-01-11 13:43:15 -08:00
bool containsItems = false;
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "part")
{
partname = string.Empty;
partinterface = string.Empty;
2020-08-21 15:31:19 -07:00
features = new List<SoftwareListFeature>();
2019-01-11 13:43:15 -08:00
}
2019-01-11 13:43:15 -08:00
if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "dataarea" || reader.Name == "diskarea"))
areasize = null;
reader.Read();
continue;
}
// Get the elements from the software
switch (reader.Name)
{
case "part":
partname = reader.GetAttribute("name");
partinterface = reader.GetAttribute("interface");
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "feature":
2020-08-23 21:10:29 -07:00
var feature = new SoftwareListFeature();
feature.Name = reader.GetAttribute("name");
feature.Value = reader.GetAttribute("value");
features.Add(feature);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "dataarea":
areaname = reader.GetAttribute("name");
if (reader.GetAttribute("size") != null)
{
if (Int64.TryParse(reader.GetAttribute("size"), out long tempas))
areasize = tempas;
}
2020-08-21 13:31:22 -07:00
areaWidth = reader.GetAttribute("width");
areaEndinaness = reader.GetAttribute("endianness");
containsItems = ReadDataArea(
reader.ReadSubtree(),
machine,
partname,
partinterface,
features,
areaname,
areasize,
areaWidth,
areaEndinaness,
filename,
indexId,
keep);
2019-01-11 13:43:15 -08:00
// Skip the dataarea now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "diskarea":
areaname = reader.GetAttribute("name");
2020-08-21 13:31:22 -07:00
containsItems = ReadDiskArea(
reader.ReadSubtree(),
machine,
partname,
partinterface,
features,
areaname,
areasize,
filename,
indexId,
keep);
2019-01-11 13:43:15 -08:00
// Skip the diskarea now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "dipswitch":
2020-08-22 13:05:58 -07:00
// TODO: Use these dipswitches
var dipSwitch = new ListXmlDipSwitch();
2020-08-22 23:40:00 -07:00
dipSwitch.Name = reader.GetAttribute("name");
dipSwitch.Tag = reader.GetAttribute("tag");
dipSwitch.Mask = reader.GetAttribute("mask");
// Now read the internal tags
2020-08-22 13:31:13 -07:00
ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
// Skip the dipswitch now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
return containsItems;
}
/// <summary>
/// Read dataarea information
/// </summary>
/// <param name="reader">XmlReader representing a dataarea block</param>
/// <param name="machine">Machine information to pass to contained items</param>
2020-08-21 13:31:22 -07:00
/// <param name="partName">Name of the containing part</param>
/// <param name="partInterface">Interface of the containing part</param>
2019-01-11 13:43:15 -08:00
/// <param name="features">List of features from the parent part</param>
2020-08-21 13:31:22 -07:00
/// <param name="areaName">Name of the containing area</param>
/// <param name="areaSize">Size of the containing area</param>
/// <param name="areaWidth">Byte width of the containing area</param>
/// <param name="areaEndianness">Endianness of the containing area</param>
2019-01-11 13:43:15 -08:00
/// <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>
private bool ReadDataArea(
XmlReader reader,
Machine machine,
2020-08-21 13:31:22 -07:00
string partName,
string partInterface,
2020-08-21 15:31:19 -07:00
List<SoftwareListFeature> features,
2020-08-21 13:31:22 -07:00
string areaName,
long? areaSize,
string areaWidth,
string areaEndianness,
2019-01-11 13:43:15 -08:00
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
string key = string.Empty;
2019-01-11 13:43:15 -08:00
string temptype = reader.Name;
bool containsItems = false;
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the software
switch (reader.Name)
{
case "rom":
containsItems = true;
// If the rom is continue or ignore, add the size to the previous rom
if (reader.GetAttribute("loadflag") == "continue" || reader.GetAttribute("loadflag") == "ignore")
{
2020-07-26 22:34:45 -07:00
int index = Items[key].Count - 1;
DatItem lastrom = Items[key][index];
2019-01-11 13:43:15 -08:00
if (lastrom.ItemType == ItemType.Rom)
{
((Rom)lastrom).Size += Sanitizer.CleanSize(reader.GetAttribute("size"));
2019-01-11 13:43:15 -08:00
}
2020-08-21 14:20:17 -07:00
2020-07-26 22:34:45 -07:00
Items[key].RemoveAt(index);
Items[key].Add(lastrom);
2019-01-11 13:43:15 -08:00
reader.Read();
continue;
}
DatItem rom = new Rom
{
Name = reader.GetAttribute("name"),
Size = Sanitizer.CleanSize(reader.GetAttribute("size")),
CRC = reader.GetAttribute("crc"),
MD5 = reader.GetAttribute("md5"),
#if NET_FRAMEWORK
RIPEMD160 = reader.GetAttribute("ripemd160"),
#endif
SHA1 = reader.GetAttribute("sha1"),
SHA256 = reader.GetAttribute("sha256"),
SHA384 = reader.GetAttribute("sha384"),
SHA512 = reader.GetAttribute("sha512"),
2019-01-11 13:43:15 -08:00
Offset = reader.GetAttribute("offset"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
2019-01-11 13:43:15 -08:00
2020-08-21 13:31:22 -07:00
PartName = partName,
PartInterface = partInterface,
2019-01-11 13:43:15 -08:00
Features = features,
2020-08-21 13:31:22 -07:00
AreaName = areaName,
AreaSize = areaSize,
AreaWidth = areaWidth,
AreaEndianness = areaEndianness,
2020-08-21 14:20:17 -07:00
Value = reader.GetAttribute("value"),
LoadFlag = reader.GetAttribute("loadflag"),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
rom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(rom);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
return containsItems;
}
/// <summary>
/// Read diskarea information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="machine">Machine information to pass to contained items</param>
2020-08-21 13:31:22 -07:00
/// <param name="partname">Name of the containing part</param>
/// <param name="partinterface">Interface of the containing part</param>
2019-01-11 13:43:15 -08:00
/// <param name="features">List of features from the parent part</param>
/// <param name="areaname">Name of the containing area</param>
/// <param name="areasize">Size of the containing area</param>
/// <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>
private bool ReadDiskArea(
XmlReader reader,
Machine machine,
2020-08-21 13:31:22 -07:00
string partname,
string partinterface,
2020-08-21 15:31:19 -07:00
List<SoftwareListFeature> features,
2019-01-11 13:43:15 -08:00
string areaname,
long? areasize,
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
string key = string.Empty;
2019-01-11 13:43:15 -08:00
string temptype = reader.Name;
bool containsItems = false;
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the software
switch (reader.Name)
{
case "disk":
containsItems = true;
DatItem disk = new Disk
{
Name = reader.GetAttribute("name"),
MD5 = reader.GetAttribute("md5"),
#if NET_FRAMEWORK
RIPEMD160 = reader.GetAttribute("ripemd160"),
#endif
SHA1 = reader.GetAttribute("sha1"),
SHA256 = reader.GetAttribute("sha256"),
SHA384 = reader.GetAttribute("sha384"),
SHA512 = reader.GetAttribute("sha512"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
Writable = reader.GetAttribute("writable").AsYesNo(),
2019-01-11 13:43:15 -08:00
PartName = partname,
PartInterface = partinterface,
2020-08-21 13:31:22 -07:00
Features = features,
AreaName = areaname,
AreaSize = areasize,
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
disk.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(disk);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
return containsItems;
}
/// <summary>
/// Read DipSwitch DipValues information
/// </summary>
2020-08-22 13:31:13 -07:00
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="dipSwitch">ListXMLDipSwitch to populate</param>
private void ReadDipSwitch(XmlReader reader, ListXmlDipSwitch dipSwitch)
{
// If we have an empty dipswitch, skip it
if (reader == null)
2020-08-22 13:31:13 -07:00
return;
// Get list ready
dipSwitch.Values = new List<ListXmlDipValue>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "dipvalue":
var dipValue = new ListXmlDipValue();
2020-08-22 23:40:00 -07:00
dipValue.Name = reader.GetAttribute("name");
dipValue.Value = reader.GetAttribute("value");
dipValue.Default = reader.GetAttribute("default").AsYesNo();
dipSwitch.Values.Add(dipValue);
2020-08-22 13:31:13 -07:00
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
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>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.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-07-26 22:34:45 -07:00
List<DatItem> roms = Items[key];
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
roms = DatItem.ResolveNames(roms);
for (int index = 0; index < roms.Count; index++)
{
DatItem rom = roms[index];
// There are apparently times when a null rom can skip by, skip them
2020-08-20 13:17:14 -07:00
if (rom.Name == null || rom.Machine.Name == null)
2019-01-11 13:43:15 -08:00
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item
2020-08-20 13:17:14 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw);
2019-01-11 13:43:15 -08:00
// If we have a new game, output the beginning of the new item
2020-08-20 13:17:14 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom);
2019-01-11 13:43:15 -08:00
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
2020-08-20 13:17:14 -07:00
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
lastgame = rom.Machine.Name;
2019-01-11 13:43:15 -08:00
continue;
}
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-20 13:17:14 -07:00
lastgame = rom.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
Globals.Logger.Verbose("File written!" + Environment.NewLine);
xtw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT header 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 bool WriteHeader(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
xtw.WriteStartDocument();
xtw.WriteDocType("softwarelist", null, "softwarelist.dtd", null);
xtw.WriteStartElement("softwarelist");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", Header.Name);
xtw.WriteRequiredAttributeString("description", Header.Description);
switch (Header.ForcePacking)
{
2020-08-20 20:38:29 -07:00
case PackingFlag.Unzip:
xtw.WriteAttributeString("forcepacking", "unzip");
break;
2020-08-20 20:38:29 -07:00
case PackingFlag.Zip:
xtw.WriteAttributeString("forcepacking", "zip");
break;
}
switch (Header.ForceMerging)
{
2020-08-20 20:38:29 -07:00
case MergingFlag.Full:
xtw.WriteAttributeString("forcemerging", "full");
break;
2020-08-20 20:38:29 -07:00
case MergingFlag.Split:
xtw.WriteAttributeString("forcemerging", "split");
break;
2020-08-20 20:38:29 -07:00
case MergingFlag.Merged:
xtw.WriteAttributeString("forcemerging", "merged");
break;
2020-08-20 20:38:29 -07:00
case MergingFlag.NonMerged:
xtw.WriteAttributeString("forcemerging", "nonmerged");
break;
}
switch (Header.ForceNodump)
{
2020-08-20 20:38:29 -07:00
case NodumpFlag.Ignore:
xtw.WriteAttributeString("forcenodump", "ignore");
break;
2020-08-20 20:38:29 -07:00
case NodumpFlag.Obsolete:
xtw.WriteAttributeString("forcenodump", "obsolete");
break;
2020-08-20 20:38:29 -07:00
case NodumpFlag.Required:
xtw.WriteAttributeString("forcenodump", "required");
break;
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <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>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
try
{
// No game should start with a path separator
2020-08-20 13:17:14 -07:00
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2020-08-23 22:23:55 -07:00
// Build the state
xtw.WriteStartElement("software");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", datItem.Machine.Name);
2020-08-23 22:54:09 -07:00
if (!string.Equals(datItem.Machine.Name, datItem.Machine.CloneOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteOptionalAttributeString("cloneof", datItem.Machine.CloneOf);
2020-08-23 22:54:09 -07:00
switch (datItem.Machine.Supported)
2019-01-11 13:43:15 -08:00
{
2020-08-23 22:54:09 -07:00
case Supported.No:
xtw.WriteAttributeString("supported", "no");
break;
case Supported.Partial:
xtw.WriteAttributeString("supported", "partial");
break;
case Supported.Yes:
xtw.WriteAttributeString("supported", "yes");
break;
2019-01-11 13:43:15 -08:00
}
2020-08-24 00:25:23 -07:00
xtw.WriteOptionalElementString("description", datItem.Machine.Description);
xtw.WriteOptionalElementString("year", datItem.Machine.Year);
xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher);
xtw.WriteOptionalElementString("category", datItem.Machine.Category);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
if (datItem.Machine.Infos != null && datItem.Machine.Infos.Count > 0)
2019-01-11 13:43:15 -08:00
{
2020-08-21 15:31:19 -07:00
foreach (ListXmlInfo kvp in datItem.Machine.Infos)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartElement("info");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", kvp.Name);
xtw.WriteRequiredAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
}
2020-08-23 22:23:55 -07:00
if (datItem.Machine.SharedFeatures != null && datItem.Machine.SharedFeatures.Count > 0)
2020-08-21 13:03:38 -07:00
{
2020-08-21 15:31:19 -07:00
foreach (SoftwareListSharedFeature kvp in datItem.Machine.SharedFeatures)
2020-08-21 13:03:38 -07:00
{
xtw.WriteStartElement("sharedfeat");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", kvp.Name);
xtw.WriteRequiredAttributeString("value", kvp.Value);
2020-08-21 13:03:38 -07:00
xtw.WriteEndElement();
}
}
2020-08-23 22:23:55 -07:00
if (datItem.Machine.DipSwitches != null && datItem.Machine.DipSwitches.Count > 0)
{
foreach (ListXmlDipSwitch dip in datItem.Machine.DipSwitches)
{
xtw.WriteStartElement("dipswitch");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", dip.Name);
xtw.WriteRequiredAttributeString("tag", dip.Tag);
xtw.WriteRequiredAttributeString("mask", dip.Mask);
foreach (ListXmlDipValue dipval in dip.Values)
{
xtw.WriteStartElement("dipvalue");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", dipval.Name);
xtw.WriteRequiredAttributeString("value", dipval.Value);
xtw.WriteRequiredAttributeString("default", dipval.Default == true ? "yes" : "no");
xtw.WriteEndElement();
}
// End dipswitch
xtw.WriteEndElement();
}
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game start 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 bool WriteEndGame(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
// End software
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <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
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false)
2019-01-11 13:43:15 -08:00
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
2019-01-11 13:43:15 -08:00
return true;
try
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
// Build the state
xtw.WriteStartElement("part");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", datItem.PartName);
xtw.WriteRequiredAttributeString("interface", datItem.PartInterface);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
if (datItem.Features != null && datItem.Features.Count > 0)
2019-01-11 13:43:15 -08:00
{
2020-08-21 15:31:19 -07:00
foreach (SoftwareListFeature kvp in datItem.Features)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartElement("feature");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", kvp.Name);
xtw.WriteRequiredAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
}
2020-08-23 22:23:55 -07:00
string areaName = datItem.AreaName;
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Disk:
var disk = datItem as Disk;
2020-08-23 22:23:55 -07:00
if (string.IsNullOrWhiteSpace(areaName))
areaName = "cdrom";
xtw.WriteStartElement("diskarea");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", areaName);
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("size", disk.AreaSize.ToString());
xtw.WriteStartElement("disk");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", disk.Name);
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant());
#if NET_FRAMEWORK
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("ripemd160", disk.RIPEMD160?.ToLowerInvariant());
#endif
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha256", disk.SHA256?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha384", disk.SHA384?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha512", disk.SHA512?.ToLowerInvariant());
2020-08-23 22:54:09 -07:00
if (disk.ItemStatus != ItemStatus.None) xtw.WriteAttributeString("status", disk.ItemStatus.ToString().ToLowerInvariant());
xtw.WriteOptionalAttributeString("writable", disk.Writable.FromYesNo());
xtw.WriteEndElement();
// End diskarea
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Rom:
var rom = datItem as Rom;
2020-08-23 22:23:55 -07:00
if (string.IsNullOrWhiteSpace(areaName))
areaName = "rom";
xtw.WriteStartElement("dataarea");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", areaName);
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("size", rom.AreaSize.ToString());
xtw.WriteOptionalAttributeString("width", rom.AreaWidth);
xtw.WriteOptionalAttributeString("endianness", rom.AreaEndianness);
xtw.WriteStartElement("rom");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", rom.Name);
2020-08-23 22:54:09 -07:00
if (rom.Size != -1) xtw.WriteAttributeString("size", rom.Size.ToString());
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant());
#if NET_FRAMEWORK
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("ripemd160", rom.RIPEMD160?.ToLowerInvariant());
#endif
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant());
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("offset", rom.Offset);
xtw.WriteOptionalAttributeString("value", rom.Value);
if (rom.ItemStatus != ItemStatus.None) xtw.WriteAttributeString("status", rom.ItemStatus.ToString().ToLowerInvariant());
xtw.WriteOptionalAttributeString("loadflag", rom.LoadFlag);
xtw.WriteEndElement();
// End dataarea
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
}
// End part
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <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 bool WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
// End software
xtw.WriteEndElement();
// End softwarelist
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}