Cleanup and overhaul (#21)

* Syntax cleanup

* More minor cleanup, use Linq

* Fix broken features by using correct values

* Feature flags the same

* Features are modular

* No AlphaFS, more .NET versions

* Fix appveyor

* Put back identifiers, for some reason

* String interpolation, modernization

* Better use of GetField

* XmlTextWriter to remove possible issues

* Fix header for OpenMSX
This commit is contained in:
Matt Nadareski
2020-06-10 22:37:19 -07:00
committed by GitHub
parent f01e47444c
commit 4ad77d6be6
75 changed files with 6945 additions and 7249 deletions

View File

@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using StreamWriter = System.IO.StreamWriter;
#endif
using NaturalSort;
namespace SabreTools.Library.DatFiles
@@ -62,9 +54,7 @@ namespace SabreTools.Library.DatFiles
// If we got a null reader, just return
if (xtr == null)
{
return;
}
// Otherwise, read the file to the end
try
@@ -82,27 +72,30 @@ namespace SabreTools.Library.DatFiles
switch (xtr.Name)
{
case "mame":
Name = (String.IsNullOrWhiteSpace(Name) ? xtr.GetAttribute("build") : Name);
Description = (String.IsNullOrWhiteSpace(Description) ? Name : Name);
Name = (string.IsNullOrWhiteSpace(Name) ? xtr.GetAttribute("build") : Name);
Description = (string.IsNullOrWhiteSpace(Description) ? Name : Name);
// string mame_debug = xtr.GetAttribute("debug"); // (yes|no) "no"
// string mame_mameconfig = xtr.GetAttribute("mameconfig"); CDATA
xtr.Read();
break;
// Handle M1 DATs since they're 99% the same as a SL DAT
case "m1":
Name = (String.IsNullOrWhiteSpace(Name) ? "M1" : Name);
Description = (String.IsNullOrWhiteSpace(Description) ? "M1" : Description);
Version = (String.IsNullOrWhiteSpace(Version) ? xtr.GetAttribute("version") ?? "" : Version);
Name = (string.IsNullOrWhiteSpace(Name) ? "M1" : Name);
Description = (string.IsNullOrWhiteSpace(Description) ? "M1" : Description);
Version = (string.IsNullOrWhiteSpace(Version) ? xtr.GetAttribute("version") ?? string.Empty : Version);
xtr.Read();
break;
// We want to process the entire subtree of the machine
case "game": // Some older DATs still use "game"
case "machine":
ReadMachine(xtr.ReadSubtree(), filename, sysid, srcid, keep, clean, remUnicode);
ReadMachine(xtr.ReadSubtree(), filename, sysid, srcid, clean, remUnicode);
// Skip the machine now that we've processed it
xtr.Skip();
break;
default:
xtr.Read();
break;
@@ -111,7 +104,7 @@ namespace SabreTools.Library.DatFiles
}
catch (Exception ex)
{
Globals.Logger.Warning("Exception found while parsing '{0}': {1}", filename, ex);
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}");
// For XML errors, just skip the affected node
xtr?.Read();
@@ -127,7 +120,6 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
private void ReadMachine(
@@ -139,37 +131,30 @@ namespace SabreTools.Library.DatFiles
int srcid,
// Miscellaneous
bool keep,
bool clean,
bool remUnicode)
{
// If we have an empty machine, skip it
if (reader == null)
{
return;
}
// Otherwise, add what is possible
reader.MoveToContent();
string key = "";
string key = string.Empty;
string temptype = reader.Name;
bool containsItems = false;
// Create a new machine
MachineType machineType = MachineType.NULL;
if (Utilities.GetYesNo(reader.GetAttribute("isbios")) == true)
{
machineType |= MachineType.Bios;
}
if (Utilities.GetYesNo(reader.GetAttribute("isdevice")) == true)
{
machineType |= MachineType.Device;
}
if (Utilities.GetYesNo(reader.GetAttribute("ismechanical")) == true)
{
machineType |= MachineType.Mechanical;
}
Machine machine = new Machine
{
@@ -178,11 +163,11 @@ namespace SabreTools.Library.DatFiles
SourceFile = reader.GetAttribute("sourcefile"),
Runnable = Utilities.GetYesNo(reader.GetAttribute("runnable")),
Comment = "",
Comment = string.Empty,
CloneOf = reader.GetAttribute("cloneof") ?? "",
RomOf = reader.GetAttribute("romof") ?? "",
SampleOf = reader.GetAttribute("sampleof") ?? "",
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
RomOf = reader.GetAttribute("romof") ?? string.Empty,
SampleOf = reader.GetAttribute("sampleof") ?? string.Empty,
Devices = new List<string>(),
SlotOptions = new List<string>(),
@@ -204,12 +189,15 @@ namespace SabreTools.Library.DatFiles
case "description":
machine.Description = reader.ReadElementContentAsString();
break;
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
case "manufacturer":
machine.Manufacturer = reader.ReadElementContentAsString();
break;
case "biosset":
containsItems = true;
@@ -231,6 +219,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "rom":
containsItems = true;
@@ -264,6 +253,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "disk":
containsItems = true;
@@ -295,15 +285,15 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "device_ref":
string device_ref_name = reader.GetAttribute("name");
if (!machine.Devices.Contains(device_ref_name))
{
machine.Devices.Add(device_ref_name);
}
reader.Read();
break;
case "sample":
containsItems = true;
@@ -323,6 +313,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "chip":
// string chip_name = reader.GetAttribute("name");
// string chip_tag = reader.GetAttribute("tag");
@@ -331,6 +322,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "display":
// string display_tag = reader.GetAttribute("tag");
// string display_type = reader.GetAttribute("type"); // (raster|vector|lcd|svg|unknown)
@@ -349,11 +341,13 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "sound":
// string sound_channels = reader.GetAttribute("channels");
reader.Read();
break;
case "condition":
// string condition_tag = reader.GetAttribute("tag");
// string condition_mask = reader.GetAttribute("mask");
@@ -362,6 +356,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "input":
// bool? input_service = Utilities.GetYesNo(reader.GetAttribute("service"));
// bool? input_tilt = Utilities.GetYesNo(reader.GetAttribute("tilt"));
@@ -384,6 +379,7 @@ namespace SabreTools.Library.DatFiles
reader.Skip();
break;
case "dipswitch":
// string dipswitch_name = reader.GetAttribute("name");
// string dipswitch_tag = reader.GetAttribute("tag");
@@ -401,6 +397,7 @@ namespace SabreTools.Library.DatFiles
reader.Skip();
break;
case "configuration":
// string configuration_name = reader.GetAttribute("name");
// string configuration_tag = reader.GetAttribute("tag");
@@ -418,6 +415,7 @@ namespace SabreTools.Library.DatFiles
reader.Skip();
break;
case "port":
// string port_tag = reader.GetAttribute("tag");
@@ -426,6 +424,7 @@ namespace SabreTools.Library.DatFiles
reader.Skip();
break;
case "adjuster":
// string adjuster_name = reader.GetAttribute("name");
// bool? adjuster_default = Utilities.GetYesNo(reader.GetAttribute("default"));
@@ -446,6 +445,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "feature":
// string feature_type = reader.GetAttribute("type"); // (protection|palette|graphics|sound|controls|keyboard|mouse|microphone|camera|disk|printer|lan|wan|timing)
// string feature_status = reader.GetAttribute("status"); // (unemulated|imperfect)
@@ -469,6 +469,7 @@ namespace SabreTools.Library.DatFiles
reader.Skip();
break;
case "slot":
// string slot_name = reader.GetAttribute("name");
ReadSlot(reader.ReadSubtree(), machine);
@@ -476,6 +477,7 @@ namespace SabreTools.Library.DatFiles
// Skip the slot now that we've processed it
reader.Skip();
break;
case "softwarelist":
// string softwarelist_name = reader.GetAttribute("name");
// string softwarelist_status = reader.GetAttribute("status"); // (original|compatible)
@@ -483,11 +485,13 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "ramoption":
// string ramoption_default = reader.GetAttribute("default");
reader.Read();
break;
default:
reader.Read();
break;
@@ -519,9 +523,7 @@ namespace SabreTools.Library.DatFiles
{
// If we have an empty machine, skip it
if (reader == null)
{
return;
}
// Otherwise, add what is possible
reader.MoveToContent();
@@ -548,6 +550,7 @@ namespace SabreTools.Library.DatFiles
// bool? slotoption_default = Utilities.GetYesNo(reader.GetAttribute("default"));
reader.Read();
break;
default:
reader.Read();
break;
@@ -565,20 +568,21 @@ namespace SabreTools.Library.DatFiles
{
try
{
Globals.Logger.User("Opening file for writing: {0}", outfile);
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = Utilities.TryCreate(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.Logger.Warning("File '{0}' could not be created for writing! Please check to see if the file is writable", outfile);
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
XmlTextWriter xtw = new XmlTextWriter(fs, new UTF8Encoding(false));
xtw.Formatting = Formatting.Indented;
// Write out the header
WriteHeader(sw);
WriteHeader(xtw);
// Write out each of the machines and roms
string lastgame = null;
@@ -607,29 +611,25 @@ namespace SabreTools.Library.DatFiles
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
{
WriteEndGame(sw);
}
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
{
WriteStartGame(sw, rom);
}
WriteStartGame(xtw, rom);
// 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")
{
Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName);
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
lastgame = rom.MachineName;
continue;
}
// Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks);
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against
lastgame = rom.MachineName;
@@ -637,10 +637,10 @@ namespace SabreTools.Library.DatFiles
}
// Write the file footer out
WriteFooter(sw);
WriteFooter(xtw);
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
@@ -655,21 +655,20 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(StreamWriter sw)
private bool WriteHeader(XmlTextWriter xtw)
{
try
{
string header = "<?xml version=\"1.0\"?>\n" +
"<mame build=\"" + WebUtility.HtmlEncode(Name) + "\"" +
// " debug=\"" + Debug + "\"" +
// " mameconfig=\"" + MameConfig + "\"" +
">\n\n";
xtw.WriteStartDocument();
// Write the header out
sw.Write(header);
sw.Flush();
xtw.WriteStartElement("mame");
xtw.WriteAttributeString("build", Name);
//xtw.WriteAttributeString("debug", Debug);
//xtw.WriteAttributeString("mameconfig", MameConfig);
xtw.Flush();
}
catch (Exception ex)
{
@@ -683,51 +682,66 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="rom">DatItem object to be output</param>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem rom)
private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem)
{
try
{
// No game should start with a path separator
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
// Build the state based on excluded fields
xtw.WriteStartElement("machine");
xtw.WriteAttributeString("name", datItem.GetField(Field.MachineName, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SourceFile, ExcludeFields)))
xtw.WriteElementString("sourcefile", datItem.SourceFile);
if (!ExcludeFields[(int)Field.MachineType])
{
rom.MachineName = rom.MachineName.Substring(1);
if ((datItem.MachineType & MachineType.Bios) != 0)
xtw.WriteAttributeString("isbios", "yes");
if ((datItem.MachineType & MachineType.Device) != 0)
xtw.WriteAttributeString("isdevice", "yes");
if ((datItem.MachineType & MachineType.Mechanical) != 0)
xtw.WriteAttributeString("ismechanical", "yes");
}
string state = "\t<machine name=\"" + (!ExcludeFields[(int)Field.MachineName] ? WebUtility.HtmlEncode(rom.MachineName) : "") + "\""
+ (!ExcludeFields[(int)Field.SourceFile] && rom.SourceFile != null ? " sourcefile=\"" + rom.SourceFile + "\"" : "")
+ (!ExcludeFields[(int)Field.MachineType] && (rom.MachineType & MachineType.Bios) != 0 ? " isbios=\"yes\"" : "")
+ (!ExcludeFields[(int)Field.MachineType] && (rom.MachineType & MachineType.Device) != 0 ? " isdevice=\"yes\"" : "")
+ (!ExcludeFields[(int)Field.MachineType] && (rom.MachineType & MachineType.Mechanical) != 0 ? " ismechanical=\"yes\"" : "")
+ (!ExcludeFields[(int)Field.Runnable] && rom.Runnable == true
? " runnable=\"yes\""
: (!ExcludeFields[(int)Field.Runnable] && rom.Runnable == false ? " runnable=\"no\"" : ""))
+ (!ExcludeFields[(int)Field.CloneOf] && String.IsNullOrWhiteSpace(rom.CloneOf) || (rom.MachineName.ToLowerInvariant() == rom.CloneOf.ToLowerInvariant())
? ""
: " cloneof=\"" + WebUtility.HtmlEncode(rom.CloneOf) + "\"")
+ (!ExcludeFields[(int)Field.RomOf] && String.IsNullOrWhiteSpace(rom.RomOf) || (rom.MachineName.ToLowerInvariant() == rom.RomOf.ToLowerInvariant())
? ""
: " romof=\"" + WebUtility.HtmlEncode(rom.RomOf) + "\"")
+ (!ExcludeFields[(int)Field.SampleOf] && String.IsNullOrWhiteSpace(rom.SampleOf) || (rom.MachineName.ToLowerInvariant() == rom.SampleOf.ToLowerInvariant())
? ""
: " sampleof=\"" + WebUtility.HtmlEncode(rom.SampleOf) + "\"")
+ ">\n"
+ (!ExcludeFields[(int)Field.Description] ? "\t\t<description>" + WebUtility.HtmlEncode(rom.MachineDescription) + "</description>\n" : "")
+ (!ExcludeFields[(int)Field.Year] && rom.Year != null ? "\t\t<year>" + WebUtility.HtmlEncode(rom.Year) + "</year>\n" : "")
+ (!ExcludeFields[(int)Field.Publisher] && rom.Publisher != null ? "\t\t<publisher>" + WebUtility.HtmlEncode(rom.Publisher) + "</publisher>\n" : "");
if (!ExcludeFields[(int)Field.Infos] && rom.Infos != null && rom.Infos.Count > 0)
if (!ExcludeFields[(int)Field.Runnable])
{
foreach (Tuple<string, string> kvp in rom.Infos)
if (datItem.Runnable == true)
xtw.WriteAttributeString("runnable", "yes");
else if (datItem.Runnable == false)
xtw.WriteAttributeString("runnable", "no");
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)) && !string.Equals(datItem.MachineName, datItem.CloneOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteAttributeString("cloneof", datItem.CloneOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RomOf, ExcludeFields)) && !string.Equals(datItem.MachineName, datItem.RomOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteAttributeString("romof", datItem.RomOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)) && !string.Equals(datItem.MachineName, datItem.SampleOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteAttributeString("sampleof", datItem.SampleOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields)))
xtw.WriteElementString("description", datItem.MachineDescription);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, ExcludeFields)))
xtw.WriteElementString("year", datItem.Year);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Publisher, ExcludeFields)))
xtw.WriteElementString("publisher", datItem.Publisher);
if (!ExcludeFields[(int)Field.Infos] && datItem.Infos != null && datItem.Infos.Count > 0)
{
foreach (Tuple<string, string> kvp in datItem.Infos)
{
state += "\t\t<info name=\"" + WebUtility.HtmlEncode(kvp.Item1) + "\" value=\"" + WebUtility.HtmlEncode(kvp.Item2) + "\" />\n";
xtw.WriteStartElement("info");
xtw.WriteAttributeString("name", kvp.Item1);
xtw.WriteAttributeString("value", kvp.Item2);
xtw.WriteEndElement();
}
}
sw.Write(state);
sw.Flush();
xtw.Flush();
}
catch (Exception ex)
{
@@ -741,16 +755,16 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw)
private bool WriteEndGame(XmlTextWriter xtw)
{
try
{
string state = "\t</machine>\n";
// End machine
xtw.WriteEndElement();
sw.Write(state);
sw.Flush();
xtw.Flush();
}
catch (Exception ex)
{
@@ -764,84 +778,109 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="rom">DatItem object to be output</param>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <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(StreamWriter sw, DatItem rom, bool ignoreblanks = false)
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false)
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks
&& (rom.ItemType == ItemType.Rom
&& (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1)))
{
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
}
try
{
string state = "";
// Pre-process the item name
ProcessItemName(rom, true);
ProcessItemName(datItem, true);
switch (rom.ItemType)
// Build the state based on excluded fields
switch (datItem.ItemType)
{
case ItemType.Archive:
break;
case ItemType.BiosSet: // TODO: Separate out MachineDescription from Description
state += "\t\t<biosset name\"" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") + "\""
+ (!ExcludeFields[(int)Field.BiosDescription] && !String.IsNullOrWhiteSpace(((BiosSet)rom).Description) ? " description=\"" + WebUtility.HtmlEncode(((BiosSet)rom).Description) + "\"" : "")
+ (!ExcludeFields[(int)Field.Default] && ((BiosSet)rom).Default != null
? ((BiosSet)rom).Default.ToString().ToLowerInvariant()
: "")
+ "/>\n";
case ItemType.BiosSet:
var biosSet = datItem as BiosSet;
xtw.WriteStartElement("biosset");
xtw.WriteAttributeString("name", biosSet.GetField(Field.Name, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.BiosDescription, ExcludeFields)))
xtw.WriteAttributeString("description", biosSet.Description);
if (!ExcludeFields[(int)Field.Default] && biosSet.Default != null)
xtw.WriteAttributeString("default", biosSet.Default.ToString().ToLowerInvariant());
xtw.WriteEndElement();
break;
case ItemType.Disk:
state += "\t\t<disk name=\"" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") + "\""
+ (!ExcludeFields[(int)Field.MD5] && !String.IsNullOrWhiteSpace(((Disk)rom).MD5) ? " md5=\"" + ((Disk)rom).MD5.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.RIPEMD160] && !String.IsNullOrWhiteSpace(((Disk)rom).RIPEMD160) ? " ripemd160=\"" + ((Disk)rom).RIPEMD160.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA1] && !String.IsNullOrWhiteSpace(((Disk)rom).SHA1) ? " sha1=\"" + ((Disk)rom).SHA1.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA256] && !String.IsNullOrWhiteSpace(((Disk)rom).SHA256) ? " sha256=\"" + ((Disk)rom).SHA256.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA384] && !String.IsNullOrWhiteSpace(((Disk)rom).SHA384) ? " sha384=\"" + ((Disk)rom).SHA384.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA512] && !String.IsNullOrWhiteSpace(((Disk)rom).SHA512) ? " sha512=\"" + ((Disk)rom).SHA512.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.Merge] && !String.IsNullOrWhiteSpace(((Disk)rom).MergeTag) ? " merge=\"" + ((Disk)rom).MergeTag + "\"" : "")
+ (!ExcludeFields[(int)Field.Region] && !String.IsNullOrWhiteSpace(((Disk)rom).Region) ? " region=\"" + ((Disk)rom).Region + "\"" : "")
+ (!ExcludeFields[(int)Field.Index] && !String.IsNullOrWhiteSpace(((Disk)rom).Index) ? " index=\"" + ((Disk)rom).Index + "\"" : "")
+ (!ExcludeFields[(int)Field.Writable] && ((Disk)rom).Writable != null ? " writable=\"" + (((Disk)rom).Writable == true ? "yes" : "no") + "\"" : "")
+ (!ExcludeFields[(int)Field.Status] && ((Disk)rom).ItemStatus != ItemStatus.None ? " status=\"" + ((Disk)rom).ItemStatus.ToString().ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.Optional] && ((Disk)rom).Optional != null ? " optional=\"" + (((Disk)rom).Optional == true ? "yes" : "no") + "\"" : "")
+ "/>\n";
break;
case ItemType.Release:
//TODO: Am I missing this?
var disk = datItem as Disk;
xtw.WriteStartElement("disk");
xtw.WriteAttributeString("name", disk.GetField(Field.Name, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
xtw.WriteAttributeString("md5", disk.MD5.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
xtw.WriteAttributeString("ripemd160", disk.RIPEMD160.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
xtw.WriteAttributeString("sha1", disk.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
xtw.WriteAttributeString("sha256", disk.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
xtw.WriteAttributeString("sha384", disk.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
xtw.WriteAttributeString("sha512", disk.SHA512.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Merge, ExcludeFields)))
xtw.WriteAttributeString("merge", disk.MergeTag);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, ExcludeFields)))
xtw.WriteAttributeString("region", disk.Region);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Index, ExcludeFields)))
xtw.WriteAttributeString("index", disk.Index);
if (!ExcludeFields[(int)Field.Writable] && disk.Writable != null)
xtw.WriteAttributeString("writable", disk.Writable == true ? "yes" : "no");
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", disk.ItemStatus.ToString());
if (!ExcludeFields[(int)Field.Optional] && disk.Optional != null)
xtw.WriteAttributeString("optional", disk.Optional == true ? "yes" : "no");
xtw.WriteEndElement();
break;
case ItemType.Rom:
state += "\t\t<rom name=\"" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") + "\""
+ (!ExcludeFields[(int)Field.Size] && ((Rom)rom).Size != -1 ? " size=\"" + ((Rom)rom).Size + "\"" : "")
+ (!ExcludeFields[(int)Field.CRC] && !String.IsNullOrWhiteSpace(((Rom)rom).CRC) ? " crc=\"" + ((Rom)rom).CRC.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.MD5] && !String.IsNullOrWhiteSpace(((Rom)rom).MD5) ? " md5=\"" + ((Rom)rom).MD5.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.RIPEMD160] && !String.IsNullOrWhiteSpace(((Rom)rom).RIPEMD160) ? " ripemd160=\"" + ((Rom)rom).RIPEMD160.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA1] && !String.IsNullOrWhiteSpace(((Rom)rom).SHA1) ? " sha1=\"" + ((Rom)rom).SHA1.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA256] && !String.IsNullOrWhiteSpace(((Rom)rom).SHA256) ? " sha256=\"" + ((Rom)rom).SHA256.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA384] && !String.IsNullOrWhiteSpace(((Rom)rom).SHA384) ? " sha384=\"" + ((Rom)rom).SHA384.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.SHA512] && !String.IsNullOrWhiteSpace(((Rom)rom).SHA512) ? " sha512=\"" + ((Rom)rom).SHA512.ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.Bios] && !String.IsNullOrWhiteSpace(((Rom)rom).Bios) ? " bios=\"" + ((Rom)rom).Bios + "\"" : "")
+ (!ExcludeFields[(int)Field.Merge] && !String.IsNullOrWhiteSpace(((Rom)rom).MergeTag) ? " merge=\"" + ((Rom)rom).MergeTag + "\"" : "")
+ (!ExcludeFields[(int)Field.Region] && !String.IsNullOrWhiteSpace(((Rom)rom).Region) ? " region=\"" + ((Rom)rom).Region + "\"" : "")
+ (!ExcludeFields[(int)Field.Offset] && !String.IsNullOrWhiteSpace(((Rom)rom).Offset) ? " offset=\"" + ((Rom)rom).Offset + "\"" : "")
+ (!ExcludeFields[(int)Field.Status] && ((Rom)rom).ItemStatus != ItemStatus.None ? " status=\"" + ((Rom)rom).ItemStatus.ToString().ToLowerInvariant() + "\"" : "")
+ (!ExcludeFields[(int)Field.Optional] && ((Rom)rom).Optional != null ? " optional=\"" + (((Rom)rom).Optional == true ? "yes" : "no") + "\"" : "")
+ "/>\n";
var rom = datItem as Rom;
xtw.WriteStartElement("rom");
xtw.WriteAttributeString("name", rom.GetField(Field.Name, ExcludeFields));
if (!ExcludeFields[(int)Field.Size] && rom.Size != -1)
xtw.WriteAttributeString("size", rom.Size.ToString());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
xtw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
xtw.WriteAttributeString("md5", rom.MD5.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
xtw.WriteAttributeString("ripemd160", rom.RIPEMD160.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
xtw.WriteAttributeString("sha1", rom.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
xtw.WriteAttributeString("sha256", rom.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
xtw.WriteAttributeString("sha384", rom.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
xtw.WriteAttributeString("sha512", rom.SHA512.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Bios, ExcludeFields)))
xtw.WriteAttributeString("bios", rom.Bios);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Merge, ExcludeFields)))
xtw.WriteAttributeString("merge", rom.MergeTag);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, ExcludeFields)))
xtw.WriteAttributeString("region", rom.Region);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Offset, ExcludeFields)))
xtw.WriteAttributeString("offset", rom.Offset);
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", rom.ItemStatus.ToString().ToLowerInvariant());
if (!ExcludeFields[(int)Field.Optional] && rom.Optional != null)
xtw.WriteAttributeString("optional", rom.Optional == true ? "yes" : "no");
xtw.WriteEndElement();
break;
case ItemType.Sample:
state += "\t\t<sample name=\"" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") + "\""
+ "/>\n";
xtw.WriteStartElement("sample");
xtw.WriteAttributeString("name", datItem.GetField(Field.Name, ExcludeFields));
xtw.WriteEndElement();
break;
}
sw.Write(state);
sw.Flush();
xtw.Flush();
}
catch (Exception ex)
{
@@ -855,17 +894,19 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(StreamWriter sw)
private bool WriteFooter(XmlTextWriter xtw)
{
try
{
string footer = "\t</machine>\n</mame>\n";
// End machine
xtw.WriteEndElement();
// Write the footer out
sw.Write(footer);
sw.Flush();
// End mame
xtw.WriteEndElement();
xtw.Flush();
}
catch (Exception ex)
{