Files
SabreTools/SabreTools.Library/DatFiles/ClrMamePro.cs

1044 lines
48 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.Tools;
using NaturalSort;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a ClrMamePro DAT
/// </summary>
2020-06-12 11:02:23 -07:00
/// TODO: Can there be a writer like XmlTextWriter for this? Or too inconsistent?
2019-01-11 13:43:15 -08:00
internal class ClrMamePro : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public ClrMamePro(DatFile datFile)
: base(datFile, cloneHeader: false)
{
}
/// <summary>
/// Parse a ClrMamePro 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)
{
// 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, clean, remUnicode);
2019-01-11 13:43:15 -08:00
}
else if (normalizedValue == "resource") // Used by some other DATs to denote a BIOS set
{
ReadSet(sr, true, filename, sysid, srcid, clean, remUnicode);
2019-01-11 13:43:15 -08:00
}
}
}
sr.Dispose();
}
/// <summary>
/// Read header information
/// </summary>
/// <param name="reader">StreamReader 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(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("\"", string.Empty);
2019-01-11 13:43:15 -08:00
if (line.Trim().StartsWith("Name:"))
{
Name = (string.IsNullOrWhiteSpace(Name) ? line.Substring(6) : Name);
2019-01-11 13:43:15 -08:00
superdat = superdat || itemval.Contains(" - SuperDAT");
2019-01-11 13:43:15 -08:00
if (keep && superdat)
Type = (string.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
2019-01-11 13:43:15 -08:00
line = reader.ReadLine();
continue;
}
switch (gc[1].Value)
{
case "name":
case "Name:":
Name = (string.IsNullOrWhiteSpace(Name) ? itemval : Name);
2019-01-11 13:43:15 -08:00
superdat = superdat || itemval.Contains(" - SuperDAT");
2019-01-11 13:43:15 -08:00
if (keep && superdat)
Type = (string.IsNullOrWhiteSpace(Type) ? "SuperDAT" : Type);
2019-01-11 13:43:15 -08:00
break;
case "description":
case "Description:":
Description = (string.IsNullOrWhiteSpace(Description) ? itemval : Description);
2019-01-11 13:43:15 -08:00
break;
case "rootdir":
case "Rootdir:":
RootDir = (string.IsNullOrWhiteSpace(RootDir) ? itemval : RootDir);
2019-01-11 13:43:15 -08:00
break;
case "category":
case "Category:":
Category = (string.IsNullOrWhiteSpace(Category) ? itemval : Category);
2019-01-11 13:43:15 -08:00
break;
case "version":
case "Version:":
Version = (string.IsNullOrWhiteSpace(Version) ? itemval : Version);
2019-01-11 13:43:15 -08:00
break;
case "date":
case "Date:":
Date = (string.IsNullOrWhiteSpace(Date) ? itemval : Date);
2019-01-11 13:43:15 -08:00
break;
case "author":
case "Author:":
Author = (string.IsNullOrWhiteSpace(Author) ? itemval : Author);
2019-01-11 13:43:15 -08:00
break;
case "email":
case "Email:":
Email = (string.IsNullOrWhiteSpace(Email) ? itemval : Email);
2019-01-11 13:43:15 -08:00
break;
case "homepage":
case "Homepage:":
Homepage = (string.IsNullOrWhiteSpace(Homepage) ? itemval : Homepage);
2019-01-11 13:43:15 -08:00
break;
case "url":
case "Url:":
Url = (string.IsNullOrWhiteSpace(Url) ? itemval : Url);
2019-01-11 13:43:15 -08:00
break;
case "comment":
case "Comment:":
Comment = (string.IsNullOrWhiteSpace(Comment) ? itemval : Comment);
2019-01-11 13:43:15 -08:00
break;
case "header":
case "Header:":
Header = (string.IsNullOrWhiteSpace(Header) ? itemval : Header);
2019-01-11 13:43:15 -08:00
break;
case "type":
case "Type:":
Type = (string.IsNullOrWhiteSpace(Type) ? itemval : Type);
2019-01-11 13:43:15 -08:00
superdat = superdat || itemval.Contains("SuperDAT");
break;
case "forcemerging":
if (ForceMerging == ForceMerging.None)
ForceMerging = Utilities.GetForceMerging(itemval);
2019-01-11 13:43:15 -08:00
break;
case "forcezipping":
if (ForcePacking == ForcePacking.None)
ForcePacking = Utilities.GetForcePacking(itemval);
2019-01-11 13:43:15 -08:00
break;
case "forcepacking":
if (ForcePacking == ForcePacking.None)
ForcePacking = Utilities.GetForcePacking(itemval);
2019-01-11 13:43:15 -08:00
break;
}
line = reader.ReadLine();
}
}
/// <summary>
/// Read set information
/// </summary>
/// <param name="reader">StreamReader to use to parse the header</param>
/// <param name="resource">True if the item is a resource (bios), false otherwise</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="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 ReadSet(
StreamReader reader,
bool resource,
// Standard Dat parsing
string filename,
int sysid,
int srcid,
// Miscellaneous
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("\"", string.Empty); // Remove "sample" from the input string
2019-01-11 13:43:15 -08:00
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"
2020-06-05 22:26:44 -07:00
&& linegc[i] != "ripemd160"
2019-01-11 13:43:15 -08:00
&& linegc[i] != "sha1"
&& linegc[i] != "sha256"
&& linegc[i] != "sha384"
&& linegc[i] != "sha512")
{
item.Name += $" {linegc[i]}";
2019-01-11 13:43:15 -08:00
}
// 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;
2019-01-11 13:43:15 -08:00
((Rom)item).Size = tempsize;
}
// Get the date from the next part
else if (linegc[i] == "date")
{
((Rom)item).Date = $"{linegc[++i].Replace("\"", string.Empty)} {linegc[++i].Replace("\"", string.Empty)}";
2019-01-11 13:43:15 -08:00
}
// Get the CRC from the next part
else if (linegc[i] == "crc")
{
((Rom)item).CRC = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
// Get the MD5 from the next part
else if (linegc[i] == "md5")
{
((Rom)item).MD5 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
2020-06-05 22:26:44 -07:00
// Get the RIPEMD160 from the next part
else if (linegc[i] == "ripemd160")
{
((Rom)item).RIPEMD160 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2020-06-05 22:26:44 -07:00
}
2019-01-11 13:43:15 -08:00
// Get the SHA1 from the next part
else if (linegc[i] == "sha1")
{
((Rom)item).SHA1 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
// Get the SHA256 from the next part
else if (linegc[i] == "sha256")
{
((Rom)item).SHA256 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
// Get the SHA384 from the next part
else if (linegc[i] == "sha384")
{
((Rom)item).SHA384 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
// Get the SHA512 from the next part
else if (linegc[i] == "sha512")
{
((Rom)item).SHA512 = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
2019-01-11 13:43:15 -08:00
}
}
// 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("\"", string.Empty);
2019-01-11 13:43:15 -08:00
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.ItemType == ItemType.Rom)
((Rom)item).ItemStatus = tempStandaloneStatus;
else if (item.ItemType == ItemType.Disk)
((Disk)item).ItemStatus = tempStandaloneStatus;
2019-01-11 13:43:15 -08:00
break;
// Regular attributes
case "name":
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
item.Name = quoteless;
break;
case "size":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
if (Int64.TryParse(quoteless, out long size))
((Rom)item).Size = size;
else
((Rom)item).Size = -1;
}
2019-01-11 13:43:15 -08:00
break;
case "crc":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).CRC = Utilities.CleanHashData(quoteless, Constants.CRCLength);
}
2019-01-11 13:43:15 -08:00
break;
case "md5":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).MD5 = Utilities.CleanHashData(quoteless, Constants.MD5Length);
}
else if (item.ItemType == ItemType.Disk)
{
i++;
quoteless = linegc[i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Disk)item).MD5 = Utilities.CleanHashData(quoteless, Constants.MD5Length);
}
2019-01-11 13:43:15 -08:00
break;
2020-06-05 22:26:44 -07:00
case "ripemd160":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2020-06-05 22:26:44 -07:00
((Rom)item).RIPEMD160 = Utilities.CleanHashData(quoteless, Constants.RIPEMD160Length);
}
else if (item.ItemType == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2020-06-05 22:26:44 -07:00
((Disk)item).RIPEMD160 = Utilities.CleanHashData(quoteless, Constants.RIPEMD160Length);
}
2020-06-05 22:26:44 -07:00
break;
2019-01-11 13:43:15 -08:00
case "sha1":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).SHA1 = Utilities.CleanHashData(quoteless, Constants.SHA1Length);
}
else if (item.ItemType == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Disk)item).SHA1 = Utilities.CleanHashData(quoteless, Constants.SHA1Length);
}
2019-01-11 13:43:15 -08:00
break;
case "sha256":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).SHA256 = Utilities.CleanHashData(quoteless, Constants.SHA256Length);
}
else if (item.ItemType == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Disk)item).SHA256 = Utilities.CleanHashData(quoteless, Constants.SHA256Length);
}
2019-01-11 13:43:15 -08:00
break;
case "sha384":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).SHA384 = Utilities.CleanHashData(quoteless, Constants.SHA384Length);
}
else if (item.ItemType == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Disk)item).SHA384 = Utilities.CleanHashData(quoteless, Constants.SHA384Length);
}
2019-01-11 13:43:15 -08:00
break;
case "sha512":
if (item.ItemType == ItemType.Rom)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Rom)item).SHA512 = Utilities.CleanHashData(quoteless, Constants.SHA512Length);
}
else if (item.ItemType == ItemType.Disk)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Disk)item).SHA512 = Utilities.CleanHashData(quoteless, Constants.SHA512Length);
}
2019-01-11 13:43:15 -08:00
break;
case "status":
case "flags":
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
ItemStatus tempFlagStatus = Utilities.GetItemStatus(quoteless);
if (item.ItemType == ItemType.Rom)
((Rom)item).ItemStatus = tempFlagStatus;
else if (item.ItemType == ItemType.Disk)
((Disk)item).ItemStatus = tempFlagStatus;
2019-01-11 13:43:15 -08:00
break;
case "date":
if (item.ItemType == ItemType.Rom)
{
// If we have quotes in the next item, assume only one item
if (linegc[i + 1].Contains("\""))
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
// Otherwise, we assume we need to read the next two items
else
quoteless = $"{linegc[++i].Replace("\"", string.Empty)} {linegc[++i].Replace("\"", string.Empty)}";
2019-01-11 13:43:15 -08:00
((Rom)item).Date = quoteless;
}
else if (item.ItemType == ItemType.Release)
{
// If we have quotes in the next item, assume only one item
if (linegc[i + 1].Contains("\""))
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
// Otherwise, we assume we need to read the next two items
else
quoteless = $"{linegc[++i].Replace("\"", string.Empty)} {linegc[++i].Replace("\"", string.Empty)}";
2019-01-11 13:43:15 -08:00
((Release)item).Date = quoteless;
}
2019-01-11 13:43:15 -08:00
break;
case "default":
if (item.ItemType == ItemType.BiosSet)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((BiosSet)item).Default = Utilities.GetYesNo(quoteless.ToLowerInvariant());
}
else if (item.ItemType == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Release)item).Default = Utilities.GetYesNo(quoteless.ToLowerInvariant());
}
2019-01-11 13:43:15 -08:00
break;
case "description":
if (item.ItemType == ItemType.BiosSet)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((BiosSet)item).Description = quoteless.ToLowerInvariant();
}
2019-01-11 13:43:15 -08:00
break;
case "region":
if (item.ItemType == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Release)item).Region = quoteless.ToLowerInvariant();
}
2019-01-11 13:43:15 -08:00
break;
case "language":
if (item.ItemType == ItemType.Release)
{
quoteless = linegc[++i].Replace("\"", string.Empty);
2019-01-11 13:43:15 -08:00
((Release)item).Language = quoteless.ToLowerInvariant();
}
2019-01-11 13:43:15 -08:00
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("\"", string.Empty);
2019-01-11 13:43:15 -08:00
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)
{
Blank blank = new Blank()
{
SystemID = sysid,
System = filename,
SourceID = srcid,
};
2019-01-11 13:43:15 -08:00
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank, clean, remUnicode);
}
}
/// <summary>
/// Create and open an output file for writing direct from a dictionary
/// </summary>
/// <param name="outfile">Name of the file to write to</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
2019-01-11 13:43:15 -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 '{outfile}' could not be created for writing! Please check to see if the file is writable");
2019-01-11 13:43:15 -08:00
return false;
}
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, 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.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
2019-01-11 13:43:15 -08:00
// 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;
2020-06-05 22:26:44 -07:00
((Rom)rom).RIPEMD160 = ((Rom)rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
2019-01-11 13:43:15 -08:00
((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}");
2019-01-11 13:43:15 -08:00
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
{
2020-06-12 11:02:23 -07:00
sw.Write("clrmamepro (\n");
sw.Write($"\tname \"{Name}\"\n");
sw.Write($"\tdescription \"{Description}\"\n");
if (!string.IsNullOrWhiteSpace(Category))
2020-06-12 11:02:23 -07:00
sw.Write($"\tcategory \"{Category}\"\n");
sw.Write($"\tversion \"{Version}\"\n");
if (!string.IsNullOrWhiteSpace(Date))
2020-06-12 11:02:23 -07:00
sw.Write($"\tdate \"{Date}\"\n");
sw.Write($"\tauthor \"{Author}\"\n");
if (!string.IsNullOrWhiteSpace(Email))
2020-06-12 11:02:23 -07:00
sw.Write($"\temail \"{Email}\"\n");
if (!string.IsNullOrWhiteSpace(Homepage))
2020-06-12 11:02:23 -07:00
sw.Write($"\thomepage \"{Homepage}\"\n");
if (!string.IsNullOrWhiteSpace(Url))
2020-06-12 11:02:23 -07:00
sw.Write($"\turl \"{Url}\"\n");
if (!string.IsNullOrWhiteSpace(Comment))
2020-06-12 11:02:23 -07:00
sw.Write($"\tcomment \"{Comment}\"\n");
switch (ForcePacking)
{
case ForcePacking.Unzip:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcezipping no\n");
break;
case ForcePacking.Zip:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcezipping yes\n");
break;
}
2020-06-12 11:02:23 -07:00
switch (ForceMerging)
{
case ForceMerging.Full:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcemerging full\n");
break;
case ForceMerging.Split:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcemerging split\n");
break;
case ForceMerging.Merged:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcemerging merged\n");
break;
case ForceMerging.NonMerged:
2020-06-12 11:02:23 -07:00
sw.Write($"\tforcemerging nonmerged\n");
break;
}
2019-01-11 13:43:15 -08:00
2020-06-12 11:02:23 -07:00
// End clrmamepro
sw.Write(")\n");
2019-01-11 13:43:15 -08:00
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="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
try
{
// No game should start with a path separator
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
// Build the state based on excluded fields
2020-06-12 11:02:23 -07:00
sw.Write($"{(datItem.MachineType == MachineType.Bios ? "resource" : "game")} (\n");
sw.Write($"\tname \"{datItem.MachineName}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RomOf, ExcludeFields)))
sw.Write($"\tromof \"{datItem.RomOf}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tcloneof \"{datItem.CloneOf}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tsampleof \"{datItem.SampleOf}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tdescription \"{datItem.MachineDescription}\"\n");
else if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tdescription \"{datItem.MachineName}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tyear \"{datItem.Year}\"\n");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Manufacturer, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tmanufacturer \"{datItem.Manufacturer}\"\n");
2019-01-11 13:43:15 -08:00
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>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
try
{
string state = string.Empty;
// Build the state based on excluded fields
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($"\tsampleof \"{datItem.SampleOf}\"\n");
2020-06-12 11:02:23 -07:00
// End game
sw.Write(")\n");
2019-01-11 13:43:15 -08:00
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="datFile">DatFile to write out from</param>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false)
2019-01-11 13:43:15 -08:00
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
2019-01-11 13:43:15 -08:00
return true;
try
{
string state = string.Empty;
2019-01-11 13:43:15 -08:00
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
// Build the state based on excluded fields
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Archive:
2020-06-12 11:02:23 -07:00
sw.Write("\tarchive (");
sw.Write($" name\"{datItem.GetField(Field.Name, ExcludeFields)}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.BiosSet:
var biosSet = datItem as BiosSet;
2020-06-12 11:02:23 -07:00
sw.Write("\tbiosset (");
sw.Write($" name\"{biosSet.GetField(Field.Name, ExcludeFields)}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.BiosDescription, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" description \"{biosSet.Description}\"");
if (!ExcludeFields[(int)Field.Default] && biosSet.Default != null)
2020-06-12 11:02:23 -07:00
sw.Write($" default \"{biosSet.Default.ToString().ToLowerInvariant()}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Disk:
var disk = datItem as Disk;
2020-06-12 11:02:23 -07:00
sw.Write("\tdisk (");
sw.Write($" name\"{disk.GetField(Field.Name, ExcludeFields)}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" md5 \"{disk.MD5.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" ripemd160 \"{disk.RIPEMD160.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha1 \"{disk.SHA1.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha256 \"{disk.SHA256.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha384 \"{disk.SHA384.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha512 \"{disk.SHA512.ToLowerInvariant()}\"");
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus != ItemStatus.None)
2020-06-12 11:02:23 -07:00
sw.Write($" flags \"{disk.ItemStatus.ToString().ToLowerInvariant()}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Release:
var release = datItem as Release;
2020-06-12 11:02:23 -07:00
sw.Write("\trelease (");
sw.Write($" name\"{release.GetField(Field.Name, ExcludeFields)}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" region \"{release.Region}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Language, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" language \"{release.Language}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" date \"{release.Date}\"");
if (!ExcludeFields[(int)Field.Default] && release.Default != null)
2020-06-12 11:02:23 -07:00
sw.Write($" default \"{release.Default.ToString().ToLowerInvariant()}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Rom:
var rom = datItem as Rom;
2020-06-12 11:02:23 -07:00
sw.Write("\trom (");
sw.Write($" name\"{rom.GetField(Field.Name, ExcludeFields)}\"");
if (!ExcludeFields[(int)Field.Size] && rom.Size != -1)
2020-06-12 11:02:23 -07:00
sw.Write($" size \"{rom.Size}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" crc \"{rom.CRC.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" md5 \"{rom.MD5.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" ripemd160 \"{rom.RIPEMD160.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha1 \"{rom.SHA1.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha256 \"{rom.SHA256.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha384 \"{rom.SHA384.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" sha512 \"{rom.SHA512.ToLowerInvariant()}\"");
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
2020-06-12 11:02:23 -07:00
sw.Write($" date \"{rom.Date}\"");
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus != ItemStatus.None)
2020-06-12 11:02:23 -07:00
sw.Write($" flags \"{rom.ItemStatus.ToString().ToLowerInvariant()}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Sample:
2020-06-12 11:02:23 -07:00
sw.Write("\tsample (");
sw.Write($" name\"{datItem.GetField(Field.Name, ExcludeFields)}\"");
sw.Write(" )\n");
2019-01-11 13:43:15 -08:00
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
{
2020-06-12 11:02:23 -07:00
// End game
sw.Write(")\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}