2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Web;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
using System.Xml;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
using SabreTools.Library.Data;
|
2017-11-02 15:44:15 -07:00
|
|
|
|
using SabreTools.Library.DatItems;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
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 an SabreDat XML DAT
|
|
|
|
|
|
/// </summary>
|
2017-11-17 14:33:36 -08:00
|
|
|
|
internal class SabreDat : DatFile
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2017-10-16 13:52:56 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
public SabreDat(DatFile datFile)
|
2017-12-14 16:40:56 -08:00
|
|
|
|
: base(datFile, cloneHeader: false)
|
2017-10-16 13:52:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <summary>
|
2018-01-15 13:32:38 -08:00
|
|
|
|
/// Parse an SabreDat XML DAT and return all found directories and files within
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// </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>
|
2017-11-17 14:58:54 -08:00
|
|
|
|
public override void ParseFile(
|
2017-10-09 18:04:49 -07:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int sysid,
|
|
|
|
|
|
int srcid,
|
|
|
|
|
|
|
|
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
bool keep,
|
|
|
|
|
|
bool clean,
|
|
|
|
|
|
bool remUnicode)
|
|
|
|
|
|
{
|
|
|
|
|
|
// All XML-derived DATs share a lot in common so it just calls one implementation
|
2018-01-15 00:40:24 -08:00
|
|
|
|
// TODO: Use the following implementation instead of passing to Logiqx
|
2018-01-10 22:23:17 -08:00
|
|
|
|
new Logiqx(this, false).ParseFile(filename, sysid, srcid, keep, clean, remUnicode);
|
2018-01-15 00:40:24 -08:00
|
|
|
|
return;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
|
|
|
|
|
// Prepare all internal variables
|
2018-01-15 13:32:38 -08:00
|
|
|
|
bool empty = true;
|
|
|
|
|
|
string key = "";
|
2018-01-15 10:47:19 -08:00
|
|
|
|
List<string> parent = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we're ending a folder or game, take care of possibly empty games and removing from the parent
|
|
|
|
|
|
if (xtr.NodeType == XmlNodeType.EndElement && (xtr.Name == "directory" || xtr.Name == "dir"))
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we didn't find any items in the folder, make sure to add the blank rom
|
|
|
|
|
|
if (empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempgame = String.Join("\\", parent);
|
|
|
|
|
|
Rom rom = new Rom("null", tempgame, omitFromScan: Hash.DeepHashes); // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually
|
|
|
|
|
|
|
|
|
|
|
|
// Now process and add the rom
|
|
|
|
|
|
key = ParseAddHelper(rom, clean, remUnicode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Regardless, end the current folder
|
|
|
|
|
|
int parentcount = parent.Count;
|
|
|
|
|
|
if (parentcount == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Verbose("Empty parent '{0}' found in '{1}'", String.Join("\\", parent), filename);
|
|
|
|
|
|
empty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have an end folder element, remove one item from the parent, if possible
|
|
|
|
|
|
if (parentcount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
parent.RemoveAt(parent.Count - 1);
|
|
|
|
|
|
if (keep && parentcount > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = (String.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (xtr.NodeType != XmlNodeType.Element)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtr.Read();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (xtr.Name)
|
|
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// 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();
|
2018-01-15 10:47:19 -08:00
|
|
|
|
break;
|
2018-01-15 13:32:38 -08:00
|
|
|
|
case "dir":
|
|
|
|
|
|
case "directory":
|
|
|
|
|
|
empty = ReadDirectory(xtr.ReadSubtree(), parent, filename, sysid, srcid, keep, clean, remUnicode);
|
|
|
|
|
|
|
|
|
|
|
|
// Skip the directory node now that we've processed it
|
2018-01-15 10:47:19 -08:00
|
|
|
|
xtr.Read();
|
|
|
|
|
|
break;
|
2018-01-15 13:32:38 -08:00
|
|
|
|
default:
|
2018-01-15 10:47:19 -08:00
|
|
|
|
xtr.Read();
|
|
|
|
|
|
break;
|
2018-01-15 13:32:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Warning("Exception found while parsing '{0}': {1}", filename, ex);
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// For XML errors, just skip the affected node
|
|
|
|
|
|
xtr?.Read();
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
xtr.Dispose();
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
/// <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;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// If there's no subtree to the header, skip it
|
|
|
|
|
|
if (reader == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Otherwise, read what we can from the header
|
|
|
|
|
|
while (!reader.EOF)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (reader.NodeType != XmlNodeType.Element || reader.Name == "header")
|
|
|
|
|
|
{
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// 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":
|
|
|
|
|
|
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);
|
|
|
|
|
|
Email = (String.IsNullOrWhiteSpace(Email) ? reader.GetAttribute("email") : Email);
|
|
|
|
|
|
Homepage = (String.IsNullOrWhiteSpace(Homepage) ? reader.GetAttribute("homepage") : Homepage);
|
|
|
|
|
|
Url = (String.IsNullOrWhiteSpace(Url) ? reader.GetAttribute("url") : Url);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "comment":
|
|
|
|
|
|
content = reader.ReadElementContentAsString();
|
|
|
|
|
|
Comment = (String.IsNullOrWhiteSpace(Comment) ? content : Comment);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "flags":
|
|
|
|
|
|
ReadFlags(reader.ReadSubtree(), superdat);
|
|
|
|
|
|
|
|
|
|
|
|
// Skip the flags node now that we've processed it
|
|
|
|
|
|
reader.Skip();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read directory information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="reader">XmlReader to use to parse the header</param>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
|
/// <param name="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 bool ReadDirectory(XmlReader reader,
|
|
|
|
|
|
List<string> parent,
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int sysid,
|
|
|
|
|
|
int srcid,
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
bool keep,
|
|
|
|
|
|
bool clean,
|
|
|
|
|
|
bool remUnicode)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Prepare all internal variables
|
|
|
|
|
|
XmlReader flagreader;
|
|
|
|
|
|
bool empty = true;
|
|
|
|
|
|
string key = "", date = "";
|
|
|
|
|
|
long size = -1;
|
|
|
|
|
|
ItemStatus its = ItemStatus.None;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// If there's no subtree to the header, skip it
|
|
|
|
|
|
if (reader == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return empty;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
string foldername = (reader.GetAttribute("name") ?? "");
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(foldername))
|
|
|
|
|
|
{
|
|
|
|
|
|
parent.Add(foldername);
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Otherwise, read what we can from the directory
|
|
|
|
|
|
while (!reader.EOF)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we're ending a folder or game, take care of possibly empty games and removing from the parent
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "directory" || reader.Name == "dir"))
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we didn't find any items in the folder, make sure to add the blank rom
|
|
|
|
|
|
if (empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempgame = String.Join("\\", parent);
|
|
|
|
|
|
Rom rom = new Rom("null", tempgame, omitFromScan: Hash.DeepHashes); // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Now process and add the rom
|
|
|
|
|
|
key = ParseAddHelper(rom, clean, remUnicode);
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Regardless, end the current folder
|
|
|
|
|
|
int parentcount = parent.Count;
|
|
|
|
|
|
if (parentcount == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Verbose("Empty parent '{0}' found in '{1}'", String.Join("\\", parent), filename);
|
|
|
|
|
|
empty = true;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// If we have an end folder element, remove one item from the parent, if possible
|
|
|
|
|
|
if (parentcount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
parent.RemoveAt(parent.Count - 1);
|
|
|
|
|
|
if (keep && parentcount > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = (String.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (reader.NodeType != XmlNodeType.Element)
|
|
|
|
|
|
{
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Get all directory items
|
|
|
|
|
|
string content = "";
|
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Directories can contain directories
|
|
|
|
|
|
case "dir":
|
|
|
|
|
|
case "directory":
|
|
|
|
|
|
ReadDirectory(reader.ReadSubtree(), parent, filename, sysid, srcid, keep, clean, remUnicode);
|
|
|
|
|
|
|
|
|
|
|
|
// Skip the directory node now that we've processed it
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "file":
|
|
|
|
|
|
empty = false;
|
|
|
|
|
|
|
|
|
|
|
|
// If the rom is itemStatus, flag it
|
|
|
|
|
|
its = ItemStatus.None;
|
|
|
|
|
|
flagreader = reader.ReadSubtree();
|
|
|
|
|
|
|
|
|
|
|
|
// If the subtree is empty, skip it
|
|
|
|
|
|
if (flagreader == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
reader.Skip();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
while (!flagreader.EOF)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (flagreader.NodeType != XmlNodeType.Element || flagreader.Name == "flags")
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
flagreader.Read();
|
|
|
|
|
|
continue;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
switch (flagreader.Name)
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
case "flag":
|
|
|
|
|
|
if (flagreader.GetAttribute("name") != null && flagreader.GetAttribute("value") != null)
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
content = flagreader.GetAttribute("value");
|
|
|
|
|
|
its = Utilities.GetItemStatus(flagreader.GetAttribute("name"));
|
2018-01-15 10:47:19 -08:00
|
|
|
|
}
|
2018-01-15 13:32:38 -08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
flagreader.Read();
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// If the rom has a Date attached, read it in and then sanitize it
|
|
|
|
|
|
date = Utilities.GetDate(reader.GetAttribute("date"));
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Take care of hex-sized files
|
|
|
|
|
|
size = Utilities.GetSize(reader.GetAttribute("size"));
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
Machine dir = new Machine();
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Get the name of the game from the parent
|
|
|
|
|
|
dir.Name = String.Join("\\", parent);
|
|
|
|
|
|
dir.Description = dir.Name;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
DatItem datItem;
|
|
|
|
|
|
switch (reader.GetAttribute("type").ToLowerInvariant())
|
|
|
|
|
|
{
|
|
|
|
|
|
case "archive":
|
|
|
|
|
|
datItem = new Archive
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
Name = reader.GetAttribute("name"),
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
|
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
2018-01-15 13:32:38 -08:00
|
|
|
|
break;
|
|
|
|
|
|
case "biosset":
|
|
|
|
|
|
datItem = new BiosSet
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = reader.GetAttribute("name"),
|
|
|
|
|
|
Description = reader.GetAttribute("description"),
|
|
|
|
|
|
Default = Utilities.GetYesNo(reader.GetAttribute("default")),
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "disk":
|
|
|
|
|
|
datItem = 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(),
|
|
|
|
|
|
ItemStatus = its,
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "release":
|
|
|
|
|
|
datItem = new Release
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = reader.GetAttribute("name"),
|
|
|
|
|
|
Region = reader.GetAttribute("region"),
|
|
|
|
|
|
Language = reader.GetAttribute("language"),
|
|
|
|
|
|
Date = reader.GetAttribute("date"),
|
|
|
|
|
|
Default = Utilities.GetYesNo(reader.GetAttribute("default")),
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "rom":
|
|
|
|
|
|
datItem = new Rom
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
Name = reader.GetAttribute("name"),
|
|
|
|
|
|
Size = 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(),
|
|
|
|
|
|
ItemStatus = its,
|
|
|
|
|
|
Date = date,
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "sample":
|
|
|
|
|
|
datItem = new Sample
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
Name = reader.GetAttribute("name"),
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
System = filename,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
// By default, create a new Blank, just in case
|
|
|
|
|
|
datItem = new Blank();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
datItem.CopyMachineInformation(dir);
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// Now process and add the rom
|
|
|
|
|
|
key = ParseAddHelper(datItem, clean, remUnicode);
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
reader.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
return empty;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read flags information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="reader">XmlReader to use to parse the header</param>
|
|
|
|
|
|
/// <param name="superdat">True if superdat has already been set externally, false otherwise</param>
|
|
|
|
|
|
private void ReadFlags(XmlReader reader, bool superdat)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Prepare all internal variables
|
|
|
|
|
|
string content = "";
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
// If we somehow have a null flag section, skip it
|
|
|
|
|
|
if (reader == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (!reader.EOF)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We only want elements
|
|
|
|
|
|
if (reader.NodeType != XmlNodeType.Element || reader.Name == "flags")
|
|
|
|
|
|
{
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
|
2018-01-15 13:32:38 -08:00
|
|
|
|
switch (reader.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "flag":
|
|
|
|
|
|
if (reader.GetAttribute("name") != null && reader.GetAttribute("value") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
content = reader.GetAttribute("value");
|
|
|
|
|
|
switch (reader.GetAttribute("name").ToLowerInvariant())
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
case "type":
|
|
|
|
|
|
Type = (String.IsNullOrWhiteSpace(Type) ? content : Type);
|
|
|
|
|
|
superdat = superdat || content.Contains("SuperDAT");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "forcemerging":
|
|
|
|
|
|
if (ForceMerging == ForceMerging.None)
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
ForceMerging = Utilities.GetForceMerging(content);
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
break;
|
2018-01-15 13:32:38 -08:00
|
|
|
|
case "forcenodump":
|
|
|
|
|
|
if (ForceNodump == ForceNodump.None)
|
2018-01-15 10:47:19 -08:00
|
|
|
|
{
|
2018-01-15 13:32:38 -08:00
|
|
|
|
ForceNodump = Utilities.GetForceNodump(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "forcepacking":
|
|
|
|
|
|
if (ForcePacking == ForcePacking.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
ForcePacking = Utilities.GetForcePacking(content);
|
|
|
|
|
|
}
|
2018-01-15 10:47:19 -08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-01-15 13:32:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
break;
|
2018-01-15 10:47:19 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07: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>
|
2017-11-17 14:58:54 -08:00
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.User("Opening file for writing: {0}", outfile);
|
2017-11-08 00:27:00 -08:00
|
|
|
|
FileStream fs = Utilities.TryCreate(outfile);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 10:24:08 -08:00
|
|
|
|
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
2017-10-09 20:25:46 -07:00
|
|
|
|
WriteHeader(sw);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
|
|
|
|
|
int depth = 2, last = -1;
|
|
|
|
|
|
string lastgame = null;
|
|
|
|
|
|
List<string> splitpath = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
// Get a properly sorted set of keys
|
2017-10-30 21:15:37 -07:00
|
|
|
|
List<string> keys = Keys;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
keys.Sort(new NaturalComparer());
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string key in keys)
|
|
|
|
|
|
{
|
2017-10-09 20:25:46 -07:00
|
|
|
|
List<DatItem> roms = this[key];
|
2017-10-09 18:04:49 -07: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
|
|
|
|
|
|
if (rom.Name == null || rom.MachineName == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Warning("Null rom found!");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<string> newsplit = rom.MachineName.Split('\\').ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
|
|
{
|
|
|
|
|
|
depth = WriteEndGame(sw, splitpath, newsplit, depth, out last);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a new game, output the beginning of the new item
|
|
|
|
|
|
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
|
|
|
|
|
|
{
|
|
|
|
|
|
depth = WriteStartGame(sw, rom, newsplit, lastgame, depth, last);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
|
|
splitpath = newsplit;
|
|
|
|
|
|
lastgame = rom.MachineName;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now, output the rom data
|
2017-10-30 21:49:55 -07:00
|
|
|
|
WriteDatItem(sw, rom, depth, ignoreblanks);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
|
|
|
|
|
splitpath = newsplit;
|
|
|
|
|
|
lastgame = rom.MachineName;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Write the file footer out
|
|
|
|
|
|
WriteFooter(sw, depth);
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteHeader(StreamWriter sw)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
|
|
|
|
|
"<!DOCTYPE sabredat SYSTEM \"newdat.xsd\">\n\n" +
|
|
|
|
|
|
"<datafile>\n" +
|
|
|
|
|
|
"\t<header>\n" +
|
2017-10-09 20:25:46 -07:00
|
|
|
|
"\t\t<name>" + HttpUtility.HtmlEncode(Name) + "</name>\n" +
|
|
|
|
|
|
"\t\t<description>" + HttpUtility.HtmlEncode(Description) + "</description>\n" +
|
2017-11-08 13:15:44 -08:00
|
|
|
|
(!String.IsNullOrWhiteSpace(RootDir) ? "\t\t<rootdir>" + HttpUtility.HtmlEncode(RootDir) + "</rootdir>\n" : "") +
|
|
|
|
|
|
(!String.IsNullOrWhiteSpace(Category) ? "\t\t<category>" + HttpUtility.HtmlEncode(Category) + "</category>\n" : "") +
|
2017-10-09 20:25:46 -07:00
|
|
|
|
"\t\t<version>" + HttpUtility.HtmlEncode(Version) + "</version>\n" +
|
2017-11-08 13:15:44 -08:00
|
|
|
|
(!String.IsNullOrWhiteSpace(Date) ? "\t\t<date>" + HttpUtility.HtmlEncode(Date) + "</date>\n" : "") +
|
2017-10-09 20:25:46 -07:00
|
|
|
|
"\t\t<author>" + HttpUtility.HtmlEncode(Author) + "</author>\n" +
|
2017-11-08 13:15:44 -08:00
|
|
|
|
(!String.IsNullOrWhiteSpace(Comment) ? "\t\t<comment>" + HttpUtility.HtmlEncode(Comment) + "</comment>\n" : "") +
|
|
|
|
|
|
(!String.IsNullOrWhiteSpace(Type) || ForcePacking != ForcePacking.None || ForceMerging != ForceMerging.None || ForceNodump != ForceNodump.None ?
|
2017-10-09 18:04:49 -07:00
|
|
|
|
"\t\t<flags>\n" +
|
2017-11-08 13:15:44 -08:00
|
|
|
|
(!String.IsNullOrWhiteSpace(Type) ? "\t\t\t<flag name=\"type\" value=\"" + HttpUtility.HtmlEncode(Type) + "\"/>\n" : "") +
|
2017-10-09 20:25:46 -07:00
|
|
|
|
(ForcePacking == ForcePacking.Unzip ? "\t\t\t<flag name=\"forcepacking\" value=\"unzip\"/>\n" : "") +
|
|
|
|
|
|
(ForcePacking == ForcePacking.Zip ? "\t\t\t<flag name=\"forcepacking\" value=\"zip\"/>\n" : "") +
|
|
|
|
|
|
(ForceMerging == ForceMerging.Full ? "\t\t\t<flag name=\"forcemerging\" value=\"full\"/>\n" : "") +
|
|
|
|
|
|
(ForceMerging == ForceMerging.Split ? "\t\t\t<flag name=\"forcemerging\" value=\"split\"/>\n" : "") +
|
|
|
|
|
|
(ForceMerging == ForceMerging.Merged ? "\t\t\t<flag name=\"forcemerging\" value=\"merged\"/>\n" : "") +
|
|
|
|
|
|
(ForceMerging == ForceMerging.NonMerged ? "\t\t\t<flag name=\"forcemerging\" value=\"nonmerged\"/>\n" : "") +
|
|
|
|
|
|
(ForceNodump == ForceNodump.Ignore ? "\t\t\t<flag name=\"forcenodump\" value=\"ignore\"/>\n" : "") +
|
|
|
|
|
|
(ForceNodump == ForceNodump.Obsolete ? "\t\t\t<flag name=\"forcenodump\" value=\"obsolete\"/>\n" : "") +
|
|
|
|
|
|
(ForceNodump == ForceNodump.Required ? "\t\t\t<flag name=\"forcenodump\" value=\"required\"/>\n" : "") +
|
2017-10-09 18:04:49 -07:00
|
|
|
|
"\t\t</flags>\n"
|
|
|
|
|
|
: "") +
|
|
|
|
|
|
"\t</header>\n" +
|
|
|
|
|
|
"\t<data>\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>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// <param name="rom">DatItem object to be output</param>
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <param name="newsplit">Split path representing the parent game (SabreDAT only)</param>
|
|
|
|
|
|
/// <param name="lastgame">The name of the last game to be output</param>
|
|
|
|
|
|
/// <param name="depth">Current depth to output file at (SabreDAT only)</param>
|
|
|
|
|
|
/// <param name="last">Last known depth to cycle back from (SabreDAT only)</param>
|
|
|
|
|
|
/// <returns>The new depth of the tag</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private int WriteStartGame(StreamWriter sw, DatItem rom, List<string> newsplit, string lastgame, int depth, int last)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// No game should start with a path separator
|
|
|
|
|
|
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.MachineName = rom.MachineName.Substring(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string state = "";
|
|
|
|
|
|
for (int i = (last == -1 ? 0 : last); i < newsplit.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int j = 0; j < depth - last + i - (lastgame == null ? 1 : 0); j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
state += "\t";
|
|
|
|
|
|
}
|
|
|
|
|
|
state += "<directory name=\"" + HttpUtility.HtmlEncode(newsplit[i]) + "\" description=\"" +
|
|
|
|
|
|
HttpUtility.HtmlEncode(newsplit[i]) + "\">\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
depth = depth - (last == -1 ? 0 : last) + newsplit.Count;
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return depth;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game start using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
|
|
|
|
|
/// <param name="splitpath">Split path representing last kwown parent game (SabreDAT only)</param>
|
|
|
|
|
|
/// <param name="newsplit">Split path representing the parent game (SabreDAT only)</param>
|
|
|
|
|
|
/// <param name="depth">Current depth to output file at (SabreDAT only)</param>
|
|
|
|
|
|
/// <param name="last">Last known depth to cycle back from (SabreDAT only)</param>
|
|
|
|
|
|
/// <returns>The new depth of the tag</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private int WriteEndGame(StreamWriter sw, List<string> splitpath, List<string> newsplit, int depth, out int last)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
last = 0;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string state = "";
|
|
|
|
|
|
if (splitpath != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < newsplit.Count && i < splitpath.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Always keep track of the last seen item
|
|
|
|
|
|
last = i;
|
|
|
|
|
|
|
|
|
|
|
|
// If we find a difference, break
|
|
|
|
|
|
if (newsplit[i] != splitpath[i])
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now that we have the last known position, take down all open folders
|
|
|
|
|
|
for (int i = depth - 1; i > last + 1; i--)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Print out the number of tabs and the end folder
|
|
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
state += "\t";
|
|
|
|
|
|
}
|
|
|
|
|
|
state += "</directory>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reset the current depth
|
|
|
|
|
|
depth = 2 + last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return depth;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// <param name="rom">DatItem object to be output</param>
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <param name="depth">Current depth to output file at (SabreDAT only)</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>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
private bool WriteDatItem(StreamWriter sw, DatItem rom, int depth, bool ignoreblanks = false)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
// 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 = "", prefix = "";
|
|
|
|
|
|
for (int i = 0; i < depth; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
prefix += "\t";
|
|
|
|
|
|
}
|
|
|
|
|
|
state += prefix;
|
|
|
|
|
|
|
|
|
|
|
|
switch (rom.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ItemType.Archive:
|
|
|
|
|
|
state += "<file type=\"archive\" name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
|
|
|
|
|
|
+ "/>\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.BiosSet:
|
|
|
|
|
|
state += "<file type=\"biosset\" name\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
|
2017-11-08 13:15:44 -08:00
|
|
|
|
+ (!String.IsNullOrWhiteSpace(((BiosSet)rom).Description) ? " description=\"" + HttpUtility.HtmlEncode(((BiosSet)rom).Description) + "\"" : "")
|
2017-10-09 18:04:49 -07:00
|
|
|
|
+ (((BiosSet)rom).Default != null
|
|
|
|
|
|
? ((BiosSet)rom).Default.ToString().ToLowerInvariant()
|
|
|
|
|
|
: "")
|
|
|
|
|
|
+ "/>\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Disk:
|
|
|
|
|
|
state += "<file type=\"disk\" name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
|
2017-11-08 13:15:44 -08:00
|
|
|
|
+ (!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() + "\"" : "")
|
2017-10-09 18:04:49 -07:00
|
|
|
|
+ (((Disk)rom).ItemStatus != ItemStatus.None ? prefix + "/>\n" + prefix + "\t<flags>\n" +
|
|
|
|
|
|
prefix + "\t\t<flag name=\"status\" value=\"" + ((Disk)rom).ItemStatus.ToString().ToLowerInvariant() + "\"/>\n" +
|
|
|
|
|
|
prefix + "\t</flags>\n" +
|
|
|
|
|
|
prefix + "</file>\n" : "/>\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Release:
|
|
|
|
|
|
state += "<file type=\"release\" name\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
|
2017-11-08 13:15:44 -08:00
|
|
|
|
+ (!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) + "\"" : "")
|
2017-10-09 18:04:49 -07:00
|
|
|
|
+ (((Release)rom).Default != null
|
|
|
|
|
|
? ((Release)rom).Default.ToString().ToLowerInvariant()
|
|
|
|
|
|
: "")
|
|
|
|
|
|
+ "/>\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
state += "<file type=\"rom\" name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\""
|
|
|
|
|
|
+ (((Rom)rom).Size != -1 ? " size=\"" + ((Rom)rom).Size + "\"" : "")
|
2017-11-08 13:15:44 -08:00
|
|
|
|
+ (!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 + "\"" : "")
|
2017-10-09 18:04:49 -07:00
|
|
|
|
+ (((Rom)rom).ItemStatus != ItemStatus.None ? prefix + "/>\n" + prefix + "\t<flags>\n" +
|
|
|
|
|
|
prefix + "\t\t<flag name=\"status\" value=\"" + ((Rom)rom).ItemStatus.ToString().ToLowerInvariant() + "\"/>\n" +
|
|
|
|
|
|
prefix + "\t</flags>\n" +
|
|
|
|
|
|
prefix + "</file>\n" : "/>\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Sample:
|
|
|
|
|
|
state += "<file type=\"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>
|
|
|
|
|
|
/// <param name="depth">Current depth to output file at (SabreDAT only)</param>
|
|
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteFooter(StreamWriter sw, int depth)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string footer = "";
|
|
|
|
|
|
for (int i = depth - 1; i >= 2; i--)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Print out the number of tabs and the end folder
|
|
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
footer += "\t";
|
|
|
|
|
|
}
|
|
|
|
|
|
footer += "</directory>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
footer += "\t</data>\n</datafile>\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Write the footer out
|
|
|
|
|
|
sw.Write(footer);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|