Files
SabreTools/SabreTools.Library/DatFiles/Logiqx.cs

967 lines
31 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
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
{
/// <summary>
/// Represents parsing and writing of a Logiqx-derived DAT
/// </summary>
/// TODO: Add XSD validation for all XML DAT types (maybe?)
2018-01-17 11:59:42 -08:00
/// TODO: Verify that all write for this DatFile type is correct
internal class Logiqx : DatFile
{
// Private instance variables specific to Logiqx DATs
bool _depreciated;
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
/// <param name="depreciated">True if the output uses "game", false if the output uses "machine"</param>
public Logiqx(DatFile datFile, bool depreciated)
2017-12-14 16:40:56 -08:00
: base(datFile, cloneHeader: false)
{
_depreciated = depreciated;
}
/// <summary>
/// Parse a Logiqx XML DAT and return all found games and roms within
/// </summary>
/// <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>
public override void ParseFile(
// Standard Dat parsing
string filename,
int sysid,
int srcid,
// Miscellaneous
bool keep,
bool clean,
bool remUnicode)
{
// Prepare all internal variables
Encoding enc = Utilities.GetEncoding(filename);
XmlReader xtr = Utilities.GetXmlTextReader(filename);
// 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)
{
// The datafile tag can have some attributes
case "datafile":
// string build = xtr.GetAttribute("build");
// string debug = xtr.GetAttribute("debug"); // (yes|no) "no"
xtr.Read();
break;
// We want to process the entire subtree of the header
case "header":
ReadHeader(xtr.ReadSubtree(), keep);
// Skip the header node now that we've processed it
xtr.Skip();
break;
// We want to process the entire subtree of the game
case "machine": // New-style Logiqx
case "game": // Old-style Logiqx
ReadMachine(xtr.ReadSubtree(), filename, sysid, srcid, keep, clean, remUnicode);
// Skip the machine now that we've processed it
xtr.Skip();
break;
default:
xtr.Read();
break;
}
}
}
catch (Exception ex)
{
Globals.Logger.Warning("Exception found while parsing '{0}': {1}", filename, ex);
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read header information
/// </summary>
/// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
private void ReadHeader(XmlReader reader, bool keep)
{
bool superdat = false;
// If there's no subtree to the header, skip it
if (reader == null)
{
return;
}
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element || reader.Name == "header")
{
reader.Read();
continue;
}
// Get all header items (ONLY OVERWRITE IF THERE'S NO DATA)
string content = "";
switch (reader.Name)
{
case "name":
content = reader.ReadElementContentAsString(); ;
Name = (String.IsNullOrWhiteSpace(Name) ? content : Name);
superdat = superdat || content.Contains(" - SuperDAT");
if (keep && superdat)
{
Type = (String.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
}
break;
case "description":
content = reader.ReadElementContentAsString();
Description = (String.IsNullOrWhiteSpace(Description) ? content : Description);
break;
case "rootdir": // This is exclusive to TruRip XML
content = reader.ReadElementContentAsString();
RootDir = (String.IsNullOrWhiteSpace(RootDir) ? content : RootDir);
break;
case "category":
content = reader.ReadElementContentAsString();
Category = (String.IsNullOrWhiteSpace(Category) ? content : Category);
break;
case "version":
content = reader.ReadElementContentAsString();
Version = (String.IsNullOrWhiteSpace(Version) ? content : Version);
break;
case "date":
content = reader.ReadElementContentAsString();
Date = (String.IsNullOrWhiteSpace(Date) ? content.Replace(".", "/") : Date);
break;
case "author":
content = reader.ReadElementContentAsString();
Author = (String.IsNullOrWhiteSpace(Author) ? content : Author);
break;
case "email":
content = reader.ReadElementContentAsString();
Email = (String.IsNullOrWhiteSpace(Email) ? content : Email);
break;
case "homepage":
content = reader.ReadElementContentAsString();
Homepage = (String.IsNullOrWhiteSpace(Homepage) ? content : Homepage);
break;
case "url":
content = reader.ReadElementContentAsString();
Url = (String.IsNullOrWhiteSpace(Url) ? content : Url);
break;
case "comment":
content = reader.ReadElementContentAsString();
Comment = (String.IsNullOrWhiteSpace(Comment) ? content : Comment);
break;
case "type": // This is exclusive to TruRip XML
content = reader.ReadElementContentAsString();
Type = (String.IsNullOrWhiteSpace(Type) ? content : Type);
superdat = superdat || content.Contains("SuperDAT");
break;
case "clrmamepro":
if (String.IsNullOrWhiteSpace(Header))
{
Header = reader.GetAttribute("header");
}
if (ForceMerging == ForceMerging.None)
{
ForceMerging = Utilities.GetForceMerging(reader.GetAttribute("forcemerging"));
}
if (ForceNodump == ForceNodump.None)
{
ForceNodump = Utilities.GetForceNodump(reader.GetAttribute("forcenodump"));
}
if (ForcePacking == ForcePacking.None)
{
ForcePacking = Utilities.GetForcePacking(reader.GetAttribute("forcepacking"));
}
2018-01-17 11:59:42 -08:00
reader.Read();
break;
case "romcenter":
if (reader.GetAttribute("plugin") != null)
{
// CDATA
}
if (reader.GetAttribute("rommode") != null)
{
// (merged|split|unmerged) "split"
}
if (reader.GetAttribute("biosmode") != null)
{
// merged|split|unmerged) "split"
}
if (reader.GetAttribute("samplemode") != null)
{
// (merged|unmerged) "merged"
}
if (reader.GetAttribute("lockrommode") != null)
{
// (yes|no) "no"
}
if (reader.GetAttribute("lockbiosmode") != null)
{
// (yes|no) "no"
}
if (reader.GetAttribute("locksamplemode") != null)
{
// (yes|no) "no"
}
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read game/machine information
/// </summary>
/// <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(
XmlReader reader,
// Standard Dat parsing
string filename,
int sysid,
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 temptype = reader.Name;
bool containsItems = false;
List<string> parent = new List<string>();
// Create a new machine
MachineType machineType = MachineType.NULL;
if (Utilities.GetYesNo(reader.GetAttribute("isbios")) == true)
{
machineType |= MachineType.Bios;
}
2018-01-17 11:59:42 -08:00
if (Utilities.GetYesNo(reader.GetAttribute("isdevice")) == true) // Listxml-specific, used by older DATs
{
machineType |= MachineType.Device;
}
2018-01-17 11:59:42 -08:00
if (Utilities.GetYesNo(reader.GetAttribute("ismechanical")) == true) // Listxml-specific, used by older DATs
{
machineType |= MachineType.Mechanical;
}
Machine machine = new Machine
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("name"),
SourceFile = reader.GetAttribute("sourcefile"),
Board = reader.GetAttribute("board"),
RebuildTo = reader.GetAttribute("rebuildto"),
2018-01-17 11:59:42 -08:00
Runnable = Utilities.GetYesNo(reader.GetAttribute("runnable")), // Listxml-specific, used by older DATs
Comment = "",
CloneOf = reader.GetAttribute("cloneof") ?? "",
RomOf = reader.GetAttribute("romof") ?? "",
SampleOf = reader.GetAttribute("sampleof") ?? "",
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
};
if (Type == "SuperDAT" && !keep)
{
string tempout = Regex.Match(machine.Name, @".*?\\(.*)").Groups[1].Value;
if (!String.IsNullOrWhiteSpace(tempout))
{
machine.Name = tempout;
}
}
// Get the name of the game from the parent
else if (Type == "SuperDAT" && keep && parent.Count > 0)
{
machine.Name = String.Join("\\", parent) + "\\" + machine.Name;
}
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the roms from the machine
switch (reader.Name)
{
case "comment": // There can be multiple comments by spec
machine.Comment += reader.ReadElementContentAsString();
break;
case "description":
machine.Description = reader.ReadElementContentAsString();
break;
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
case "manufacturer":
machine.Manufacturer = reader.ReadElementContentAsString();
break;
case "trurip": // This is special metadata unique to TruRip
ReadTruRip(reader.ReadSubtree(), machine);
// Skip the trurip node now that we've processed it
reader.Skip();
break;
case "release":
containsItems = true;
DatItem release = new Release
{
Name = reader.GetAttribute("name"),
Region = reader.GetAttribute("region"),
Language = reader.GetAttribute("language"),
Date = reader.GetAttribute("date"),
Default = Utilities.GetYesNo(reader.GetAttribute("default")),
};
release.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(release, clean, remUnicode);
reader.Read();
break;
case "biosset":
containsItems = true;
DatItem biosset = new BiosSet
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("description"),
Default = Utilities.GetYesNo(reader.GetAttribute("default")),
SystemID = sysid,
System = filename,
SourceID = srcid,
};
biosset.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(biosset, clean, remUnicode);
reader.Read();
break;
case "rom":
containsItems = true;
DatItem rom = new Rom
{
Name = reader.GetAttribute("name"),
Size = Utilities.GetSize(reader.GetAttribute("size")),
CRC = reader.GetAttribute("crc")?.ToLowerInvariant(),
MD5 = reader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = reader.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = reader.GetAttribute("sha256")?.ToLowerInvariant(),
SHA384 = reader.GetAttribute("sha384")?.ToLowerInvariant(),
SHA512 = reader.GetAttribute("sha512")?.ToLowerInvariant(),
MergeTag = reader.GetAttribute("merge"),
ItemStatus = Utilities.GetItemStatus(reader.GetAttribute("status")),
Date = Utilities.GetDate(reader.GetAttribute("date")),
SystemID = sysid,
System = filename,
SourceID = srcid,
};
rom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(rom, clean, remUnicode);
reader.Read();
break;
case "disk":
containsItems = true;
DatItem disk = new Disk
{
Name = reader.GetAttribute("name"),
MD5 = reader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = reader.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = reader.GetAttribute("sha256")?.ToLowerInvariant(),
SHA384 = reader.GetAttribute("sha384")?.ToLowerInvariant(),
SHA512 = reader.GetAttribute("sha512")?.ToLowerInvariant(),
MergeTag = reader.GetAttribute("merge"),
ItemStatus = Utilities.GetItemStatus(reader.GetAttribute("status")),
SystemID = sysid,
System = filename,
SourceID = srcid,
};
disk.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(disk, clean, remUnicode);
reader.Read();
break;
case "sample":
containsItems = true;
DatItem samplerom = new Sample
{
Name = reader.GetAttribute("name"),
SystemID = sysid,
System = filename,
SourceID = srcid,
};
samplerom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(samplerom, clean, remUnicode);
reader.Read();
break;
case "archive":
containsItems = true;
DatItem archiverom = new Archive
{
Name = reader.GetAttribute("name"),
SystemID = sysid,
System = filename,
SourceID = srcid,
};
archiverom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(archiverom, clean, remUnicode);
reader.Read();
break;
default:
reader.Read();
break;
}
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
{
Blank blank = new Blank()
{
SystemID = sysid,
System = filename,
SourceID = srcid,
};
blank.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(blank, clean, remUnicode);
}
}
/// <summary>
/// Read TruRip information
/// </summary>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="machine">Machine information to pass to contained items</param>
private void ReadTruRip(XmlReader reader, Machine machine)
{
// If we have an empty trurip, skip it
if (reader == null)
{
return;
}
// 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 trurip
string content = "";
switch (reader.Name)
{
case "titleid":
content = reader.ReadElementContentAsString();
// string titleid = content;
break;
case "publisher":
machine.Publisher = reader.ReadElementContentAsString();
break;
case "developer": // Manufacturer is as close as this gets
machine.Manufacturer = reader.ReadElementContentAsString();
break;
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
case "genre":
content = reader.ReadElementContentAsString();
// string genre = content;
break;
case "subgenre":
content = reader.ReadElementContentAsString();
// string subgenre = content;
break;
case "ratings":
content = reader.ReadElementContentAsString();
// string ratings = content;
break;
case "score":
content = reader.ReadElementContentAsString();
// string score = content;
break;
case "players":
content = reader.ReadElementContentAsString();
// string players = content;
break;
case "enabled":
content = reader.ReadElementContentAsString();
// string enabled = content;
break;
case "crc":
content = reader.ReadElementContentAsString();
// string crc = Utilities.GetYesNo(content);
break;
case "source":
content = reader.ReadElementContentAsString();
// string source = content;
break;
case "cloneof":
machine.CloneOf = reader.ReadElementContentAsString();
break;
case "relatedto":
content = reader.ReadElementContentAsString();
// string relatedto = content;
break;
default:
reader.Read();
break;
}
}
}
/// <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: {0}", outfile);
2017-11-08 00:27:00 -08:00
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);
return false;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
// Write out the header
WriteHeader(sw);
// Write out each of the machines and roms
string lastgame = null;
// Get a properly sorted set of keys
List<string> keys = Keys;
keys.Sort(new NaturalComparer());
foreach (string key in keys)
{
List<DatItem> roms = this[key];
// 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
if (rom.Name == null || rom.MachineName == null)
{
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
if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
{
WriteEndGame(sw);
}
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
{
WriteStartGame(sw, rom);
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.Type == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName);
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
((Rom)rom).Size = Constants.SizeZero;
((Rom)rom).CRC = ((Rom)rom).CRC == "null" ? Constants.CRCZero : null;
((Rom)rom).MD5 = ((Rom)rom).MD5 == "null" ? Constants.MD5Zero : null;
((Rom)rom).SHA1 = ((Rom)rom).SHA1 == "null" ? Constants.SHA1Zero : null;
((Rom)rom).SHA256 = ((Rom)rom).SHA256 == "null" ? Constants.SHA256Zero : null;
((Rom)rom).SHA384 = ((Rom)rom).SHA384 == "null" ? Constants.SHA384Zero : null;
((Rom)rom).SHA512 = ((Rom)rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks);
// Set the new data to compare against
lastgame = rom.MachineName;
}
}
// Write the file footer out
WriteFooter(sw);
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
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="sw">StreamWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(StreamWriter sw)
{
try
{
string header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">\n\n" +
"<datafile>\n" +
"\t<header>\n" +
"\t\t<name>" + HttpUtility.HtmlEncode(Name) + "</name>\n" +
"\t\t<description>" + HttpUtility.HtmlEncode(Description) + "</description>\n" +
(!String.IsNullOrWhiteSpace(RootDir) ? "\t\t<rootdir>" + HttpUtility.HtmlEncode(RootDir) + "</rootdir>\n" : "") +
(!String.IsNullOrWhiteSpace(Category) ? "\t\t<category>" + HttpUtility.HtmlEncode(Category) + "</category>\n" : "") +
"\t\t<version>" + HttpUtility.HtmlEncode(Version) + "</version>\n" +
(!String.IsNullOrWhiteSpace(Date) ? "\t\t<date>" + HttpUtility.HtmlEncode(Date) + "</date>\n" : "") +
"\t\t<author>" + HttpUtility.HtmlEncode(Author) + "</author>\n" +
(!String.IsNullOrWhiteSpace(Email) ? "\t\t<email>" + HttpUtility.HtmlEncode(Email) + "</email>\n" : "") +
(!String.IsNullOrWhiteSpace(Homepage) ? "\t\t<homepage>" + HttpUtility.HtmlEncode(Homepage) + "</homepage>\n" : "") +
(!String.IsNullOrWhiteSpace(Url) ? "\t\t<url>" + HttpUtility.HtmlEncode(Url) + "</url>\n" : "") +
(!String.IsNullOrWhiteSpace(Comment) ? "\t\t<comment>" + HttpUtility.HtmlEncode(Comment) + "</comment>\n" : "") +
(!String.IsNullOrWhiteSpace(Type) ? "\t\t<type>" + HttpUtility.HtmlEncode(Type) + "</type>\n" : "") +
(ForcePacking != ForcePacking.None || ForceMerging != ForceMerging.None || ForceNodump != ForceNodump.None ?
"\t\t<clrmamepro" +
(ForcePacking == ForcePacking.Unzip ? " forcepacking=\"unzip\"" : "") +
(ForcePacking == ForcePacking.Zip ? " forcepacking=\"zip\"" : "") +
(ForceMerging == ForceMerging.Full ? " forcemerging=\"full\"" : "") +
(ForceMerging == ForceMerging.Split ? " forcemerging=\"split\"" : "") +
(ForceMerging == ForceMerging.Merged ? " forcemerging=\"merged\"" : "") +
(ForceMerging == ForceMerging.NonMerged ? " forcemerging=\"nonmerged\"" : "") +
(ForceNodump == ForceNodump.Ignore ? " forcenodump=\"ignore\"" : "") +
(ForceNodump == ForceNodump.Obsolete ? " forcenodump=\"obsolete\"" : "") +
(ForceNodump == ForceNodump.Required ? " forcenodump=\"required\"" : "") +
" />\n"
: "") +
"\t</header>\n";
// Write the header out
sw.Write(header);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <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>
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem rom)
{
try
{
// No game should start with a path separator
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
rom.MachineName = rom.MachineName.Substring(1);
}
string state = "\t<" + (_depreciated ? "game" : "machine") + " name=\"" + HttpUtility.HtmlEncode(rom.MachineName) + "\"" +
(ExcludeOf ? "" :
((rom.MachineType & MachineType.Bios) != 0 ? " isbios=\"yes\"" : "") +
((rom.MachineType & MachineType.Device) != 0 ? " isdevice=\"yes\"" : "") +
((rom.MachineType & MachineType.Mechanical) != 0 ? " ismechanical=\"yes\"" : "") +
(rom.Runnable == true ? " runnable=\"yes\"" : "") +
(String.IsNullOrWhiteSpace(rom.CloneOf) || (rom.MachineName.ToLowerInvariant() == rom.CloneOf.ToLowerInvariant())
? ""
: " cloneof=\"" + HttpUtility.HtmlEncode(rom.CloneOf) + "\"") +
(String.IsNullOrWhiteSpace(rom.RomOf) || (rom.MachineName.ToLowerInvariant() == rom.RomOf.ToLowerInvariant())
? ""
: " romof=\"" + HttpUtility.HtmlEncode(rom.RomOf) + "\"") +
(String.IsNullOrWhiteSpace(rom.SampleOf) || (rom.MachineName.ToLowerInvariant() == rom.SampleOf.ToLowerInvariant())
? ""
: " sampleof=\"" + HttpUtility.HtmlEncode(rom.SampleOf) + "\"")
) +
">\n" +
(String.IsNullOrWhiteSpace(rom.Comment) ? "" : "\t\t<comment>" + HttpUtility.HtmlEncode(rom.Comment) + "</comment>\n") +
"\t\t<description>" + HttpUtility.HtmlEncode((String.IsNullOrWhiteSpace(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + "</description>\n" +
(String.IsNullOrWhiteSpace(rom.Year) ? "" : "\t\t<year>" + HttpUtility.HtmlEncode(rom.Year) + "</year>\n") +
(String.IsNullOrWhiteSpace(rom.Manufacturer) ? "" : "\t\t<manufacturer>" + HttpUtility.HtmlEncode(rom.Manufacturer) + "</manufacturer>\n");
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game end using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw)
{
try
{
string state = "\t</" + (_depreciated ? "game" : "machine") + ">\n";
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <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="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)
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks
&& (rom.Type == ItemType.Rom
&& (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1)))
{
return true;
}
try
{
string state = "";
switch (rom.Type)
{
case ItemType.Archive:
state += "\t\t<archive name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ "/>\n";
break;
case ItemType.BiosSet:
state += "\t\t<biosset name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ (!String.IsNullOrWhiteSpace(((BiosSet)rom).Description) ? " description=\"" + HttpUtility.HtmlEncode(((BiosSet)rom).Description) + "\"" : "")
+ (((BiosSet)rom).Default != null
? ((BiosSet)rom).Default.ToString().ToLowerInvariant()
: "")
+ "/>\n";
break;
case ItemType.Disk:
state += "\t\t<disk name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ (!String.IsNullOrWhiteSpace(((Disk)rom).MD5) ? " md5=\"" + ((Disk)rom).MD5.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Disk)rom).SHA1) ? " sha1=\"" + ((Disk)rom).SHA1.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Disk)rom).SHA256) ? " sha256=\"" + ((Disk)rom).SHA256.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Disk)rom).SHA384) ? " sha384=\"" + ((Disk)rom).SHA384.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Disk)rom).SHA512) ? " sha512=\"" + ((Disk)rom).SHA512.ToLowerInvariant() + "\"" : "")
+ (((Disk)rom).ItemStatus != ItemStatus.None ? " status=\"" + ((Disk)rom).ItemStatus.ToString().ToLowerInvariant() + "\"" : "")
+ "/>\n";
break;
case ItemType.Release:
state += "\t\t<release name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ (!String.IsNullOrWhiteSpace(((Release)rom).Region) ? " region=\"" + HttpUtility.HtmlEncode(((Release)rom).Region) + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Release)rom).Language) ? " language=\"" + HttpUtility.HtmlEncode(((Release)rom).Language) + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Release)rom).Date) ? " date=\"" + HttpUtility.HtmlEncode(((Release)rom).Date) + "\"" : "")
+ (((Release)rom).Default != null
? ((Release)rom).Default.ToString().ToLowerInvariant()
: "")
+ "/>\n";
break;
case ItemType.Rom:
state += "\t\t<rom name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ (((Rom)rom).Size != -1 ? " size=\"" + ((Rom)rom).Size + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).CRC) ? " crc=\"" + ((Rom)rom).CRC.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).MD5) ? " md5=\"" + ((Rom)rom).MD5.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).SHA1) ? " sha1=\"" + ((Rom)rom).SHA1.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).SHA256) ? " sha256=\"" + ((Rom)rom).SHA256.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).SHA384) ? " sha384=\"" + ((Rom)rom).SHA384.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).SHA512) ? " sha512=\"" + ((Rom)rom).SHA512.ToLowerInvariant() + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Rom)rom).Date) ? " date=\"" + ((Rom)rom).Date + "\"" : "")
+ (((Rom)rom).ItemStatus != ItemStatus.None ? " status=\"" + ((Rom)rom).ItemStatus.ToString().ToLowerInvariant() + "\"" : "")
+ "/>\n";
break;
case ItemType.Sample:
state += "\t\t<sample name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
+ "/>\n";
break;
}
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(StreamWriter sw)
{
try
{
string footer = "\t</machine>\n</datafile>\n";
// Write the footer out
sw.Write(footer);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}