using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
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 StreamReader = System.IO.StreamReader;
using StreamWriter = System.IO.StreamWriter;
#endif
using NaturalSort;
namespace SabreTools.Library.DatFiles
{
///
/// Represents parsing and writing of a ClrMamePro DAT
///
/// TODO: Verify that all write for this DatFile type is correct
internal class ClrMamePro : DatFile
{
///
/// Constructor designed for casting a base DatFile
///
/// Parent DatFile to copy from
public ClrMamePro(DatFile datFile)
: base(datFile, cloneHeader: false)
{
}
///
/// Parse a ClrMamePro DAT and return all found games and roms within
///
/// Name of the file to be parsed
/// System ID for the DAT
/// Source ID for the DAT
/// True if full pathnames are to be kept, false otherwise (default)
/// True if game names are sanitized, false otherwise (default)
/// True if we should remove non-ASCII characters from output, false otherwise (default)
public override void ParseFile(
// Standard Dat parsing
string filename,
int sysid,
int srcid,
// Miscellaneous
bool keep,
bool clean,
bool remUnicode)
{
// Open a file reader
Encoding enc = Utilities.GetEncoding(filename);
StreamReader sr = new StreamReader(Utilities.TryOpenRead(filename), enc);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
// Comments in CMP DATs start with a #
if (line.Trim().StartsWith("#"))
{
continue;
}
// If the line is the header or a game
if (Regex.IsMatch(line, Constants.HeaderPatternCMP))
{
GroupCollection gc = Regex.Match(line, Constants.HeaderPatternCMP).Groups;
string normalizedValue = gc[1].Value.ToLowerInvariant();
// If we have a known header
if (normalizedValue == "clrmamepro"
|| normalizedValue == "romvault"
|| normalizedValue == "doscenter")
{
ReadHeader(sr, keep);
}
// If we have a known set type
else if (normalizedValue == "set" // Used by the most ancient DATs
|| normalizedValue == "game" // Used by most CMP DATs
|| normalizedValue == "machine") // Possibly used by MAME CMP DATs
{
ReadSet(sr, false, filename, sysid, srcid, keep, clean, remUnicode);
}
else if (normalizedValue == "resource") // Used by some other DATs to denote a BIOS set
{
ReadSet(sr, true, filename, sysid, srcid, keep, clean, remUnicode);
}
}
}
sr.Dispose();
}
///
/// Read header information
///
/// StreamReader to use to parse the header
/// True if full pathnames are to be kept, false otherwise (default)
private void ReadHeader(StreamReader reader, bool keep)
{
bool superdat = false;
// If there's no subtree to the header, skip it
if (reader == null || reader.EndOfStream)
{
return;
}
// Otherwise, add what is possible
string line = reader.ReadLine();
while (!Regex.IsMatch(line, Constants.EndPatternCMP))
{
// We only want elements
if (line.Trim().StartsWith("#"))
{
line = reader.ReadLine();
continue;
}
// Get all header items (ONLY OVERWRITE IF THERE'S NO DATA)
GroupCollection gc = Regex.Match(line, Constants.ItemPatternCMP).Groups;
string itemval = gc[2].Value.Replace("\"", "");
if (line.Trim().StartsWith("Name:"))
{
Name = (String.IsNullOrWhiteSpace(Name) ? line.Substring(6) : Name);
superdat = superdat || itemval.Contains(" - SuperDAT");
if (keep && superdat)
{
Type = (String.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
}
line = reader.ReadLine();
continue;
}
switch (gc[1].Value)
{
case "name":
case "Name:":
Name = (String.IsNullOrWhiteSpace(Name) ? itemval : Name);
superdat = superdat || itemval.Contains(" - SuperDAT");
if (keep && superdat)
{
Type = (String.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
}
break;
case "description":
case "Description:":
Description = (String.IsNullOrWhiteSpace(Description) ? itemval : Description);
break;
case "rootdir":
case "Rootdir:":
RootDir = (String.IsNullOrWhiteSpace(RootDir) ? itemval : RootDir);
break;
case "category":
case "Category:":
Category = (String.IsNullOrWhiteSpace(Category) ? itemval : Category);
break;
case "version":
case "Version:":
Version = (String.IsNullOrWhiteSpace(Version) ? itemval : Version);
break;
case "date":
case "Date:":
Date = (String.IsNullOrWhiteSpace(Date) ? itemval : Date);
break;
case "author":
case "Author:":
Author = (String.IsNullOrWhiteSpace(Author) ? itemval : Author);
break;
case "email":
case "Email:":
Email = (String.IsNullOrWhiteSpace(Email) ? itemval : Email);
break;
case "homepage":
case "Homepage:":
Homepage = (String.IsNullOrWhiteSpace(Homepage) ? itemval : Homepage);
break;
case "url":
case "Url:":
Url = (String.IsNullOrWhiteSpace(Url) ? itemval : Url);
break;
case "comment":
case "Comment:":
Comment = (String.IsNullOrWhiteSpace(Comment) ? itemval : Comment);
break;
case "header":
case "Header:":
Header = (String.IsNullOrWhiteSpace(Header) ? itemval : Header);
break;
case "type":
case "Type:":
Type = (String.IsNullOrWhiteSpace(Type) ? itemval : Type);
superdat = superdat || itemval.Contains("SuperDAT");
break;
case "forcemerging":
if (ForceMerging == ForceMerging.None)
{
ForceMerging = Utilities.GetForceMerging(itemval);
}
break;
case "forcezipping":
if (ForcePacking == ForcePacking.None)
{
ForcePacking = Utilities.GetForcePacking(itemval);
}
break;
case "forcepacking":
if (ForcePacking == ForcePacking.None)
{
ForcePacking = Utilities.GetForcePacking(itemval);
}
break;
}
line = reader.ReadLine();
}
}
///
/// Read set information
///
/// StreamReader to use to parse the header
/// True if the item is a resource (bios), false otherwise
/// Name of the file to be parsed
/// System ID for the DAT
/// Source ID for the DAT
/// True if full pathnames are to be kept, false otherwise (default)
/// True if game names are sanitized, false otherwise (default)
/// True if we should remove non-ASCII characters from output, false otherwise (default)
private void ReadSet(
StreamReader reader,
bool resource,
// Standard Dat parsing
string filename,
int sysid,
int srcid,
// Miscellaneous
bool keep,
bool clean,
bool remUnicode)
{
// Prepare all internal variables
bool containsItems = false;
Machine machine = new Machine()
{
MachineType = (resource ? MachineType.Bios : MachineType.None),
};
// If there's no subtree to the header, skip it
if (reader == null || reader.EndOfStream)
{
return;
}
// Otherwise, add what is possible
string line = reader.ReadLine();
while (!Regex.IsMatch(line, Constants.EndPatternCMP))
{
// We only want elements
if (line.Trim().StartsWith("#"))
{
line = reader.ReadLine();
continue;
}
// Item-specific lines have a known pattern
string trimmedline = line.Trim();
if (trimmedline.StartsWith("archive (")
|| trimmedline.StartsWith("biosset (")
|| trimmedline.StartsWith("disk (")
|| trimmedline.StartsWith("file (") // This is a DOSCenter file, not a SabreDAT file
|| trimmedline.StartsWith("release (")
|| trimmedline.StartsWith("rom (")
|| (trimmedline.StartsWith("sample") && !trimmedline.StartsWith("sampleof")))
{
containsItems = true;
ItemType temptype = ItemType.Rom;
if (line.Trim().StartsWith("rom ("))
{
temptype = ItemType.Rom;
}
else if (line.Trim().StartsWith("disk ("))
{
temptype = ItemType.Disk;
}
else if (line.Trim().StartsWith("file ("))
{
temptype = ItemType.Rom;
}
else if (line.Trim().StartsWith("sample"))
{
temptype = ItemType.Sample;
}
// Create the proper DatItem based on the type
DatItem item = Utilities.GetDatItem(temptype);
// Then populate it with information
item.CopyMachineInformation(machine);
item.SystemID = sysid;
item.System = filename;
item.SourceID = srcid;
// If we have a sample, treat it special
if (temptype == ItemType.Sample)
{
line = line.Trim().Remove(0, 6).Trim().Replace("\"", ""); // Remove "sample" from the input string
item.Name = line;
// Now process and add the sample
ParseAddHelper(item, clean, remUnicode);
line = reader.ReadLine();
continue;
}
// Get the line split by spaces and quotes
string[] linegc = Utilities.SplitLineAsCMP(line);
// Special cases for DOSCenter DATs only because of how the lines are arranged
if (line.Trim().StartsWith("file ("))
{
// Loop over the specifics
for (int i = 0; i < linegc.Length; i++)
{
// Names are not quoted, for some stupid reason
if (linegc[i] == "name")
{
// Get the name in order until we find the next flag
while (++i < linegc.Length && linegc[i] != "size"
&& linegc[i] != "date"
&& linegc[i] != "crc"
&& linegc[i] != "md5"
&& linegc[i] != "sha1"
&& linegc[i] != "sha256"
&& linegc[i] != "sha384"
&& linegc[i] != "sha512")
{
item.Name += " " + linegc[i];
}
// Perform correction
item.Name = item.Name.TrimStart();
i--;
}
// Get the size from the next part
else if (linegc[i] == "size")
{
long tempsize = -1;
if (!Int64.TryParse(linegc[++i], out tempsize))
{
tempsize = 0;
}
((Rom)item).Size = tempsize;
}
// Get the date from the next part
else if (linegc[i] == "date")
{
((Rom)item).Date = linegc[++i].Replace("\"", "") + " " + linegc[++i].Replace("\"", "");
}
// Get the CRC from the next part
else if (linegc[i] == "crc")
{
((Rom)item).CRC = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
// Get the MD5 from the next part
else if (linegc[i] == "md5")
{
((Rom)item).MD5 = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA1 from the next part
else if (linegc[i] == "sha1")
{
((Rom)item).SHA1 = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA256 from the next part
else if (linegc[i] == "sha256")
{
((Rom)item).SHA256 = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA384 from the next part
else if (linegc[i] == "sha384")
{
((Rom)item).SHA384 = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA512 from the next part
else if (linegc[i] == "sha512")
{
((Rom)item).SHA512 = linegc[++i].Replace("\"", "").ToLowerInvariant();
}
}
// Now process and add the rom
ParseAddHelper(item, clean, remUnicode);
line = reader.ReadLine();
continue;
}
// Loop over all attributes normally and add them if possible
for (int i = 0; i < linegc.Length; i++)
{
// Look at the current item and use it if possible
string quoteless = linegc[i].Replace("\"", "");
switch (quoteless)
{
//If the item is empty, we automatically skip it because it's a fluke
case "":
continue;
// Special cases for standalone item statuses
case "baddump":
case "good":
case "nodump":
case "verified":
ItemStatus tempStandaloneStatus = Utilities.GetItemStatus(quoteless);
if (item.Type == ItemType.Rom)
{
((Rom)item).ItemStatus = tempStandaloneStatus;
}
else if (item.Type == ItemType.Disk)
{
((Disk)item).ItemStatus = tempStandaloneStatus;
}
break;
// Regular attributes
case "name":
quoteless = linegc[++i].Replace("\"", "");
item.Name = quoteless;
break;
case "size":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
if (Int64.TryParse(quoteless, out long size))
{
((Rom)item).Size = size;
}
else
{
((Rom)item).Size = -1;
}
}
break;
case "crc":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).CRC = Utilities.CleanHashData(quoteless, Constants.CRCLength);
}
break;
case "md5":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).MD5 = Utilities.CleanHashData(quoteless, Constants.MD5Length);
}
else if (item.Type == ItemType.Disk)
{
i++;
quoteless = linegc[i].Replace("\"", "");
((Disk)item).MD5 = Utilities.CleanHashData(quoteless, Constants.MD5Length);
}
break;
case "sha1":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).SHA1 = Utilities.CleanHashData(quoteless, Constants.SHA1Length);
}
else if (item.Type == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", "");
((Disk)item).SHA1 = Utilities.CleanHashData(quoteless, Constants.SHA1Length);
}
break;
case "sha256":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).SHA256 = Utilities.CleanHashData(quoteless, Constants.SHA256Length);
}
else if (item.Type == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", "");
((Disk)item).SHA256 = Utilities.CleanHashData(quoteless, Constants.SHA256Length);
}
break;
case "sha384":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).SHA384 = Utilities.CleanHashData(quoteless, Constants.SHA384Length);
}
else if (item.Type == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", "");
((Disk)item).SHA384 = Utilities.CleanHashData(quoteless, Constants.SHA384Length);
}
break;
case "sha512":
if (item.Type == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", "");
((Rom)item).SHA512 = Utilities.CleanHashData(quoteless, Constants.SHA512Length);
}
else if (item.Type == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", "");
((Disk)item).SHA512 = Utilities.CleanHashData(quoteless, Constants.SHA512Length);
}
break;
case "status":
case "flags":
quoteless = linegc[++i].Replace("\"", "");
ItemStatus tempFlagStatus = Utilities.GetItemStatus(quoteless);
if (item.Type == ItemType.Rom)
{
((Rom)item).ItemStatus = tempFlagStatus;
}
else if (item.Type == ItemType.Disk)
{
((Disk)item).ItemStatus = tempFlagStatus;
}
break;
case "date":
if (item.Type == ItemType.Rom)
{
// If we have quotes in the next item, assume only one item
if (linegc[i + 1].Contains("\""))
{
quoteless = linegc[++i].Replace("\"", "");
}
// Otherwise, we assume we need to read the next two items
else
{
quoteless = linegc[++i].Replace("\"", "") + " " + linegc[++i].Replace("\"", "");
}
((Rom)item).Date = quoteless;
}
else if (item.Type == ItemType.Release)
{
// If we have quotes in the next item, assume only one item
if (linegc[i + 1].Contains("\""))
{
quoteless = linegc[++i].Replace("\"", "");
}
// Otherwise, we assume we need to read the next two items
else
{
quoteless = linegc[++i].Replace("\"", "") + " " + linegc[++i].Replace("\"", "");
}
((Release)item).Date = quoteless;
}
break;
case "default":
if (item.Type == ItemType.BiosSet)
{
quoteless = linegc[++i].Replace("\"", "");
((BiosSet)item).Default = Utilities.GetYesNo(quoteless.ToLowerInvariant());
}
else if (item.Type == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", "");
((Release)item).Default = Utilities.GetYesNo(quoteless.ToLowerInvariant());
}
break;
case "description":
if (item.Type == ItemType.BiosSet)
{
quoteless = linegc[++i].Replace("\"", "");
((BiosSet)item).Description = quoteless.ToLowerInvariant();
}
break;
case "region":
if (item.Type == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", "");
((Release)item).Region = quoteless.ToLowerInvariant();
}
break;
case "language":
if (item.Type == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", "");
((Release)item).Language = quoteless.ToLowerInvariant();
}
break;
}
}
// Now process and add the rom
ParseAddHelper(item, clean, remUnicode);
line = reader.ReadLine();
continue;
}
// Set-specific lines have a known pattern
GroupCollection setgc = Regex.Match(line, Constants.ItemPatternCMP).Groups;
string itemval = setgc[2].Value.Replace("\"", "");
switch (setgc[1].Value)
{
case "name":
machine.Name = (itemval.ToLowerInvariant().EndsWith(".zip") ? itemval.Remove(itemval.Length - 4) : itemval);
machine.Description = (itemval.ToLowerInvariant().EndsWith(".zip") ? itemval.Remove(itemval.Length - 4) : itemval);
break;
case "description":
machine.Description = itemval;
break;
case "year":
machine.Year = itemval;
break;
case "manufacturer":
machine.Manufacturer = itemval;
break;
case "cloneof":
machine.CloneOf = itemval;
break;
case "romof":
machine.RomOf = itemval;
break;
case "sampleof":
machine.SampleOf = itemval;
break;
}
line = reader.ReadLine();
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
{
if (this.KeepEmptyGames)
{
Blank blank = new Blank()
{
SystemID = sysid,
System = filename,
SourceID = srcid,
};
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank, clean, remUnicode);
}
}
}
///
/// Create and open an output file for writing direct from a dictionary
///
/// Name of the file to write to
/// True if blank roms should be skipped on output, false otherwise (default)
/// True if the DAT was written correctly, false otherwise
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User("Opening file for writing: {0}", 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);
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 keys = Keys;
keys.Sort(new NaturalComparer());
foreach (string key in keys)
{
List 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, rom);
}
// 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);
// If we're in a mode that doesn't allow for actual empty folders, add the blank info
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;
}
///
/// Write out DAT header using the supplied StreamWriter
///
/// StreamWriter to output to
/// True if the data was written, false on error
private bool WriteHeader(StreamWriter sw)
{
try
{
string header = "clrmamepro (\n" +
"\tname \"" + Name + "\"\n" +
"\tdescription \"" + Description + "\"\n" +
(!String.IsNullOrWhiteSpace(Category) ? "\tcategory \"" + Category + "\"\n" : "") +
"\tversion \"" + Version + "\"\n" +
(!String.IsNullOrWhiteSpace(Date) ? "\tdate \"" + Date + "\"\n" : "") +
"\tauthor \"" + Author + "\"\n" +
(!String.IsNullOrWhiteSpace(Email) ? "\temail \"" + Email + "\"\n" : "") +
(!String.IsNullOrWhiteSpace(Homepage) ? "\thomepage \"" + Homepage + "\"\n" : "") +
(!String.IsNullOrWhiteSpace(Url) ? "\turl \"" + Url + "\"\n" : "") +
(!String.IsNullOrWhiteSpace(Comment) ? "\tcomment \"" + Comment + "\"\n" : "") +
(ForcePacking == ForcePacking.Unzip ? "\tforcezipping no\n" : "") +
(ForcePacking == ForcePacking.Zip ? "\tforcezipping yes\n" : "") +
(ForceMerging == ForceMerging.Full ? "\tforcemerging full\n" : "") +
(ForceMerging == ForceMerging.Split ? "\tforcemerging split\n" : "") +
(ForceMerging == ForceMerging.Merged ? "\tforcemerging merged\n" : "") +
(ForceMerging == ForceMerging.NonMerged ? "\tforcemerging nonmerged\n" : "") +
")\n";
// Write the header out
sw.Write(header);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
///
/// Write out Game start using the supplied StreamWriter
///
/// StreamWriter to output to
/// DatItem object to be output
/// True if the data was written, false on error
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 = (rom.MachineType == MachineType.Bios ? "resource" : "game") + " (\n\tname \"" + rom.MachineName + "\"\n" +
(ExcludeOf ? "" :
(String.IsNullOrWhiteSpace(rom.RomOf) ? "" : "\tromof \"" + rom.RomOf + "\"\n") +
(String.IsNullOrWhiteSpace(rom.CloneOf) ? "" : "\tcloneof \"" + rom.CloneOf + "\"\n") +
(String.IsNullOrWhiteSpace(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n")
) +
"\tdescription \"" + (String.IsNullOrWhiteSpace(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription) + "\"\n" +
(String.IsNullOrWhiteSpace(rom.Year) ? "" : "\tyear " + rom.Year + "\n") +
(String.IsNullOrWhiteSpace(rom.Manufacturer) ? "" : "\tmanufacturer \"" + rom.Manufacturer + "\"\n");
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
///
/// Write out Game end using the supplied StreamWriter
///
/// StreamWriter to output to
/// DatItem object to be output
/// True if the data was written, false on error
private bool WriteEndGame(StreamWriter sw, DatItem rom)
{
try
{
string state = (String.IsNullOrWhiteSpace(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n") + ")\n";
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
///
/// Write out DatItem using the supplied StreamWriter
///
/// DatFile to write out from
/// StreamWriter to output to
/// DatItem object to be output
/// True if blank roms should be skipped on output, false otherwise (default)
/// True if the data was written, false on error
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 = "";
// Pre-process the item name
ProcessItemName(rom, true);
switch (rom.Type)
{
case ItemType.Archive:
state += "\tarchive ( name\"" + rom.Name + "\""
+ " )\n";
break;
case ItemType.BiosSet:
state += "\tbiosset ( name\"" + rom.Name + "\""
+ (!String.IsNullOrWhiteSpace(((BiosSet)rom).Description) ? " description \"" + ((BiosSet)rom).Description + "\"" : "")
+ (((BiosSet)rom).Default != null
? "default " + ((BiosSet)rom).Default.ToString().ToLowerInvariant()
: "")
+ " )\n";
break;
case ItemType.Disk:
state += "\tdisk ( name \"" + 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 ? " flags " + ((Disk)rom).ItemStatus.ToString().ToLowerInvariant() : "")
+ " )\n";
break;
case ItemType.Release:
state += "\trelease ( name\"" + rom.Name + "\""
+ (!String.IsNullOrWhiteSpace(((Release)rom).Region) ? " region \"" + ((Release)rom).Region + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Release)rom).Language) ? " language \"" + ((Release)rom).Language + "\"" : "")
+ (!String.IsNullOrWhiteSpace(((Release)rom).Date) ? " date \"" + ((Release)rom).Date + "\"" : "")
+ (((Release)rom).Default != null
? "default " + ((Release)rom).Default.ToString().ToLowerInvariant()
: "")
+ " )\n";
break;
case ItemType.Rom:
state += "\trom ( name \"" + 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 ? " flags " + ((Rom)rom).ItemStatus.ToString().ToLowerInvariant() : "")
+ " )\n";
break;
case ItemType.Sample:
state += "\tsample ( name\"" + rom.Name + "\""
+ " )\n";
break;
}
sw.Write(state);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
///
/// Write out DAT footer using the supplied StreamWriter
///
/// StreamWriter to output to
/// True if the data was written, false on error
private bool WriteFooter(StreamWriter sw)
{
try
{
string footer = footer = ")\n";
// Write the footer out
sw.Write(footer);
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}