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

983 lines
41 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"),
Supported = reader.GetAttribute("supported").AsYesNo(), // (yes|partial|no) "yes"
2019-01-11 13:43:15 -08:00
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
2020-06-14 23:07:31 -07:00
Infos = new List<KeyValuePair<string, string>>(),
2020-08-21 13:03:38 -07:00
SharedFeatures = new List<KeyValuePair<string, string>>(),
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":
2020-06-14 23:07:31 -07:00
machine.Infos.Add(new KeyValuePair<string, string>(reader.GetAttribute("name"), reader.GetAttribute("value")));
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "sharedfeat":
2020-08-21 13:03:38 -07:00
machine.SharedFeatures.Add(new KeyValuePair<string, string>(reader.GetAttribute("name"), reader.GetAttribute("value")));
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-06-14 23:07:31 -07:00
var features = new List<KeyValuePair<string, string>>();
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-06-14 23:07:31 -07:00
features = new List<KeyValuePair<string, string>>();
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-06-16 11:27:10 -07:00
features.Add(new KeyValuePair<string, string>(reader.GetAttribute("name"), reader.GetAttribute("value")));
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":
// string dipswitch_name = reader.GetAttribute("name");
// string dipswitch_tag = reader.GetAttribute("tag");
// string dipswitch_mask = reader.GetAttribute("mask");
// For every <dipvalue> element...
// string dipvalue_name = reader.GetAttribute("name");
// string dipvalue_value = reader.GetAttribute("value");
// bool? dipvalue_default = Utilities.GetYesNo(reader.GetAttribute("default")); // (yes|no) "no"
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-06-14 23:07:31 -07:00
List<KeyValuePair<string, string>> 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-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"),
// Value = reader.GetAttribute("value");
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
2019-01-11 13:43:15 -08:00
// LoadFlag = reader.GetAttribute("loadflag"), // (load16_byte|load16_word|load16_word_swap|load32_byte|load32_word|load32_word_swap|load32_dword|load64_word|load64_word_swap|reload|fill|continue|reload_plain|ignore)
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,
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-06-14 23:07:31 -07:00
List<KeyValuePair<string, string>> 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>
/// 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");
xtw.WriteAttributeString("name", Header.Name);
xtw.WriteAttributeString("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);
// Build the state based on excluded fields
xtw.WriteStartElement("software");
xtw.WriteAttributeString("name", datItem.GetField(Field.MachineName, Header.ExcludeFields));
2020-08-20 13:17:14 -07:00
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, Header.ExcludeFields)) && !string.Equals(datItem.Machine.Name, datItem.Machine.CloneOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteAttributeString("cloneof", datItem.Machine.CloneOf);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Supported))
2019-01-11 13:43:15 -08:00
{
2020-08-20 13:17:14 -07:00
if (datItem.Machine.Supported == true)
xtw.WriteAttributeString("supported", "yes");
2020-08-20 13:17:14 -07:00
else if (datItem.Machine.Supported == false)
xtw.WriteAttributeString("supported", "no");
else
xtw.WriteAttributeString("supported", "partial");
2019-01-11 13:43:15 -08:00
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, Header.ExcludeFields)))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("description", datItem.Machine.Description);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, Header.ExcludeFields)))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("year", datItem.Machine.Year);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Publisher, Header.ExcludeFields)))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("publisher", datItem.Machine.Publisher);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Category, Header.ExcludeFields)))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("category", datItem.Machine.Category);
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
if (!Header.ExcludeFields.Contains(Field.Infos) && datItem.Machine.Infos != null && datItem.Machine.Infos.Count > 0)
2019-01-11 13:43:15 -08:00
{
2020-08-20 13:17:14 -07:00
foreach (KeyValuePair<string, string> kvp in datItem.Machine.Infos)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartElement("info");
2020-06-14 23:07:31 -07:00
xtw.WriteAttributeString("name", kvp.Key);
xtw.WriteAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
}
2020-08-21 13:03:38 -07:00
if (!Header.ExcludeFields.Contains(Field.SharedFeatures) && datItem.Machine.SharedFeatures != null && datItem.Machine.SharedFeatures.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in datItem.Machine.SharedFeatures)
{
xtw.WriteStartElement("sharedfeat");
xtw.WriteAttributeString("name", kvp.Key);
xtw.WriteAttributeString("value", kvp.Value);
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
// Build the state based on excluded fields
xtw.WriteStartElement("part");
xtw.WriteAttributeString("name", datItem.GetField(Field.PartName, Header.ExcludeFields));
xtw.WriteAttributeString("interface", datItem.GetField(Field.PartInterface, Header.ExcludeFields));
2019-01-11 13:43:15 -08:00
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Features) && datItem.Features != null && datItem.Features.Count > 0)
2019-01-11 13:43:15 -08:00
{
2020-06-14 23:07:31 -07:00
foreach (KeyValuePair<string, string> kvp in datItem.Features)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartElement("feature");
2020-06-14 23:07:31 -07:00
xtw.WriteAttributeString("name", kvp.Key);
xtw.WriteAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
}
string areaName = datItem.GetField(Field.AreaName, Header.ExcludeFields);
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Disk:
var disk = datItem as Disk;
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.AreaName) && string.IsNullOrWhiteSpace(areaName))
areaName = "cdrom";
xtw.WriteStartElement("diskarea");
xtw.WriteAttributeString("name", areaName);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.AreaSize) && disk.AreaSize != null)
xtw.WriteAttributeString("size", disk.AreaSize.ToString());
xtw.WriteStartElement("disk");
xtw.WriteAttributeString("name", disk.GetField(Field.Name, Header.ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, Header.ExcludeFields)))
xtw.WriteAttributeString("md5", disk.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, Header.ExcludeFields)))
xtw.WriteAttributeString("ripemd160", disk.RIPEMD160.ToLowerInvariant());
#endif
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, Header.ExcludeFields)))
xtw.WriteAttributeString("sha1", disk.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, Header.ExcludeFields)))
xtw.WriteAttributeString("sha256", disk.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, Header.ExcludeFields)))
xtw.WriteAttributeString("sha384", disk.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, Header.ExcludeFields)))
xtw.WriteAttributeString("sha512", disk.SHA512.ToLowerInvariant());
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Status) && disk.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", disk.ItemStatus.ToString().ToLowerInvariant());
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Writable) && disk.Writable != null)
xtw.WriteAttributeString("writable", disk.Writable == true ? "yes" : "no");
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-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.AreaName) && string.IsNullOrWhiteSpace(areaName))
areaName = "rom";
xtw.WriteStartElement("dataarea");
xtw.WriteAttributeString("name", areaName);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.AreaSize) && rom.AreaSize != null)
xtw.WriteAttributeString("size", rom.AreaSize.ToString());
2020-08-21 13:31:22 -07:00
if (!Header.ExcludeFields.Contains(Field.AreaWidth) && rom.AreaWidth != null)
xtw.WriteAttributeString("width", rom.AreaWidth);
if (!Header.ExcludeFields.Contains(Field.AreaEndianness) && rom.AreaEndianness != null)
xtw.WriteAttributeString("endianness", rom.AreaEndianness);
xtw.WriteStartElement("rom");
xtw.WriteAttributeString("name", rom.GetField(Field.Name, Header.ExcludeFields));
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Size) && rom.Size != -1)
xtw.WriteAttributeString("size", rom.Size.ToString());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, Header.ExcludeFields)))
xtw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, Header.ExcludeFields)))
xtw.WriteAttributeString("md5", rom.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, Header.ExcludeFields)))
xtw.WriteAttributeString("ripemd160", rom.RIPEMD160.ToLowerInvariant());
#endif
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, Header.ExcludeFields)))
xtw.WriteAttributeString("sha1", rom.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, Header.ExcludeFields)))
xtw.WriteAttributeString("sha256", rom.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, Header.ExcludeFields)))
xtw.WriteAttributeString("sha384", rom.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, Header.ExcludeFields)))
xtw.WriteAttributeString("sha512", rom.SHA512.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Offset, Header.ExcludeFields)))
xtw.WriteAttributeString("offset", rom.Offset);
//if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Value, DatHeader.ExcludeFields)))
// xtw.WriteAttributeString("value", rom.Value);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Status) && rom.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", rom.ItemStatus.ToString().ToLowerInvariant());
//if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Loadflag, DatHeader.ExcludeFields)))
// xtw.WriteAttributeString("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;
}
}
}