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

826 lines
37 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.Readers;
using SabreTools.Library.Tools;
2020-06-13 22:39:22 -07:00
using SabreTools.Library.Writers;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a ClrMamePro DAT
/// </summary>
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)
2019-01-11 13:43:15 -08:00
{
}
/// <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="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile(
2019-01-11 13:43:15 -08:00
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
// Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename);
ClrMameProReader cmpr = new ClrMameProReader(FileExtensions.TryOpenRead(filename), enc)
{
DosCenter = false
};
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
while (!cmpr.EndOfStream)
2019-01-11 13:43:15 -08:00
{
2020-06-15 12:41:39 -07:00
cmpr.ReadNextLine();
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
// Ignore everything not top-level
if (cmpr.RowType != CmpRowType.TopLevel)
2019-01-11 13:43:15 -08:00
continue;
2020-06-15 12:41:39 -07:00
// Switch on the top-level name
switch (cmpr.TopLevel.ToLowerInvariant())
2019-01-11 13:43:15 -08:00
{
2020-06-15 12:41:39 -07:00
// Header values
case "clrmamepro":
case "romvault":
ReadHeader(cmpr, keep);
break;
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
// Sets
case "set": // Used by the most ancient DATs
case "game": // Used by most CMP DATs
case "machine": // Possibly used by MAME CMP DATs
ReadSet(cmpr, false, filename, indexId);
2020-06-15 12:41:39 -07:00
break;
case "resource": // Used by some other DATs to denote a BIOS set
ReadSet(cmpr, true, filename, indexId);
2020-06-15 12:41:39 -07:00
break;
default:
break;
2019-01-11 13:43:15 -08:00
}
}
2020-06-15 12:41:39 -07:00
cmpr.Dispose();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read header information
/// </summary>
2020-06-15 12:41:39 -07:00
/// <param name="cmpr">ClrMameProReader to use to parse the header</param>
2019-01-11 13:43:15 -08:00
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
2020-06-15 12:41:39 -07:00
private void ReadHeader(ClrMameProReader cmpr, bool keep)
2019-01-11 13:43:15 -08:00
{
bool superdat = false;
// If there's no subtree to the header, skip it
2020-06-15 12:41:39 -07:00
if (cmpr == null || cmpr.EndOfStream)
2019-01-11 13:43:15 -08:00
return;
2020-06-15 12:41:39 -07:00
// While we don't hit an end element or end of stream
while (!cmpr.EndOfStream)
2019-01-11 13:43:15 -08:00
{
2020-06-15 12:41:39 -07:00
cmpr.ReadNextLine();
// Ignore comments, internal items, and nothingness
if (cmpr.RowType == CmpRowType.None || cmpr.RowType == CmpRowType.Comment || cmpr.RowType == CmpRowType.Internal)
continue;
// If we reached the end of a section, break
if (cmpr.RowType == CmpRowType.EndTopLevel)
break;
// If the standalone value is null, we skip
if (cmpr.Standalone == null)
2019-01-11 13:43:15 -08:00
continue;
2020-06-15 12:41:39 -07:00
string itemKey = cmpr.Standalone?.Key.ToLowerInvariant();
string itemVal = cmpr.Standalone?.Value;
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
// For all other cases
switch (itemKey)
2019-01-11 13:43:15 -08:00
{
case "name":
Header.Name = (string.IsNullOrWhiteSpace(Header.Name) ? itemVal : Header.Name);
2020-06-15 12:41:39 -07:00
superdat = superdat || itemVal.Contains(" - SuperDAT");
2019-01-11 13:43:15 -08:00
if (keep && superdat)
Header.Type = (string.IsNullOrWhiteSpace(Header.Type) ? "SuperDAT" : Header.Type);
2019-01-11 13:43:15 -08:00
break;
case "description":
Header.Description = (string.IsNullOrWhiteSpace(Header.Description) ? itemVal : Header.Description);
2019-01-11 13:43:15 -08:00
break;
case "rootdir":
Header.RootDir = (string.IsNullOrWhiteSpace(Header.RootDir) ? itemVal : Header.RootDir);
2019-01-11 13:43:15 -08:00
break;
case "category":
Header.Category = (string.IsNullOrWhiteSpace(Header.Category) ? itemVal : Header.Category);
2019-01-11 13:43:15 -08:00
break;
case "version":
Header.Version = (string.IsNullOrWhiteSpace(Header.Version) ? itemVal : Header.Version);
2019-01-11 13:43:15 -08:00
break;
case "date":
Header.Date = (string.IsNullOrWhiteSpace(Header.Date) ? itemVal : Header.Date);
2019-01-11 13:43:15 -08:00
break;
case "author":
Header.Author = (string.IsNullOrWhiteSpace(Header.Author) ? itemVal : Header.Author);
2019-01-11 13:43:15 -08:00
break;
case "email":
Header.Email = (string.IsNullOrWhiteSpace(Header.Email) ? itemVal : Header.Email);
2019-01-11 13:43:15 -08:00
break;
case "homepage":
Header.Homepage = (string.IsNullOrWhiteSpace(Header.Homepage) ? itemVal : Header.Homepage);
2019-01-11 13:43:15 -08:00
break;
case "url":
Header.Url = (string.IsNullOrWhiteSpace(Header.Url) ? itemVal : Header.Url);
2019-01-11 13:43:15 -08:00
break;
case "comment":
Header.Comment = (string.IsNullOrWhiteSpace(Header.Comment) ? itemVal : Header.Comment);
2019-01-11 13:43:15 -08:00
break;
case "header":
Header.Header = (string.IsNullOrWhiteSpace(Header.Header) ? itemVal : Header.Header);
2019-01-11 13:43:15 -08:00
break;
case "type":
Header.Type = (string.IsNullOrWhiteSpace(Header.Type) ? itemVal : Header.Type);
2020-06-15 12:41:39 -07:00
superdat = superdat || itemVal.Contains("SuperDAT");
2019-01-11 13:43:15 -08:00
break;
case "forcemerging":
if (Header.ForceMerging == ForceMerging.None)
Header.ForceMerging = itemVal.AsForceMerging();
2020-06-15 12:41:39 -07:00
2019-01-11 13:43:15 -08:00
break;
case "forcezipping":
if (Header.ForcePacking == ForcePacking.None)
Header.ForcePacking = itemVal.AsForcePacking();
2019-01-11 13:43:15 -08:00
break;
case "forcepacking":
if (Header.ForcePacking == ForcePacking.None)
Header.ForcePacking = itemVal.AsForcePacking();
2019-01-11 13:43:15 -08:00
break;
}
}
}
/// <summary>
/// Read set information
/// </summary>
2020-06-15 12:41:39 -07:00
/// <param name="cmpr">ClrMameProReader to use to parse the header</param>
2019-01-11 13:43:15 -08:00
/// <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="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
private void ReadSet(
2020-06-15 12:41:39 -07:00
ClrMameProReader cmpr,
2019-01-11 13:43:15 -08:00
bool resource,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
// 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
2020-06-15 12:41:39 -07:00
if (cmpr == null || cmpr.EndOfStream)
2019-01-11 13:43:15 -08:00
return;
2020-06-15 12:41:39 -07:00
// While we don't hit an end element or end of stream
while (!cmpr.EndOfStream)
2019-01-11 13:43:15 -08:00
{
2020-06-15 12:41:39 -07:00
cmpr.ReadNextLine();
// Ignore comments and nothingness
if (cmpr.RowType == CmpRowType.None || cmpr.RowType == CmpRowType.Comment)
2019-01-11 13:43:15 -08:00
continue;
2020-06-15 12:41:39 -07:00
// If we reached the end of a section, break
if (cmpr.RowType == CmpRowType.EndTopLevel)
break;
// Handle any standalone items
if (cmpr.RowType == CmpRowType.Standalone && cmpr.Standalone != null)
{
string itemKey = cmpr.Standalone?.Key.ToLowerInvariant();
string itemVal = cmpr.Standalone?.Value;
switch (itemKey)
{
case "name":
machine.Name = itemVal;
break;
case "description":
machine.Description = itemVal;
break;
case "year":
machine.Year = itemVal;
break;
case "manufacturer":
machine.Manufacturer = itemVal;
break;
case "category":
machine.Category = itemVal;
break;
2020-06-15 12:41:39 -07:00
case "cloneof":
machine.CloneOf = itemVal;
break;
case "romof":
machine.RomOf = itemVal;
break;
case "sampleof":
machine.SampleOf = itemVal;
break;
}
2019-01-11 13:43:15 -08:00
}
2020-06-15 12:41:39 -07:00
// Handle any internal items
else if (cmpr.RowType == CmpRowType.Internal
&& !string.IsNullOrWhiteSpace(cmpr.InternalName)
&& cmpr.Internal != null)
2019-01-11 13:43:15 -08:00
{
containsItems = true;
2020-06-15 12:41:39 -07:00
string itemKey = cmpr.InternalName;
ItemType itemType = ItemType.Rom;
switch (itemKey)
{
case "archive":
itemType = ItemType.Archive;
break;
case "biosset":
itemType = ItemType.BiosSet;
break;
case "disk":
itemType = ItemType.Disk;
break;
case "release":
itemType = ItemType.Release;
break;
case "rom":
itemType = ItemType.Rom;
break;
case "sample":
itemType = ItemType.Sample;
break;
}
2019-01-11 13:43:15 -08:00
// Create the proper DatItem based on the type
DatItem item = DatItem.Create(itemType);
2019-01-11 13:43:15 -08:00
// Then populate it with information
item.CopyMachineInformation(machine);
item.IndexId = indexId;
item.IndexSource = filename;
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
// Loop through all of the attributes
foreach (var kvp in cmpr.Internal)
2019-01-11 13:43:15 -08:00
{
2020-06-15 12:41:39 -07:00
string attrKey = kvp.Key;
string attrVal = kvp.Value;
2019-01-11 13:43:15 -08:00
2020-06-15 12:41:39 -07:00
switch (attrKey)
2019-01-11 13:43:15 -08:00
{
//If the item is empty, we automatically skip it because it's a fluke
case "":
continue;
// Regular attributes
case "name":
2020-06-15 12:41:39 -07:00
item.Name = attrVal;
2019-01-11 13:43:15 -08:00
break;
2020-06-15 12:41:39 -07:00
2019-01-11 13:43:15 -08:00
case "size":
if (item.ItemType == ItemType.Rom)
{
2020-06-15 12:41:39 -07:00
if (Int64.TryParse(attrVal, out long size))
2019-01-11 13:43:15 -08:00
((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)
(item as Rom).CRC = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "md5":
if (item.ItemType == ItemType.Rom)
(item as Rom).MD5 = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).MD5 = attrVal;
2019-01-11 13:43:15 -08:00
break;
#if NET_FRAMEWORK
2020-06-05 22:26:44 -07:00
case "ripemd160":
if (item.ItemType == ItemType.Rom)
(item as Rom).RIPEMD160 = attrVal;
2020-06-05 22:26:44 -07:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).RIPEMD160 = attrVal;
2020-06-05 22:26:44 -07:00
break;
#endif
2019-01-11 13:43:15 -08:00
case "sha1":
if (item.ItemType == ItemType.Rom)
(item as Rom).SHA1 = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).SHA1 = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "sha256":
if (item.ItemType == ItemType.Rom)
((Rom)item).SHA256 = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).SHA256 = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "sha384":
if (item.ItemType == ItemType.Rom)
((Rom)item).SHA384 = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).SHA384 = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "sha512":
if (item.ItemType == ItemType.Rom)
((Rom)item).SHA512 = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Disk)
((Disk)item).SHA512 = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "status":
ItemStatus tempFlagStatus = attrVal.AsItemStatus();
2019-01-11 13:43:15 -08:00
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)
2020-06-15 12:41:39 -07:00
((Rom)item).Date = attrVal;
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Release)
2020-06-15 12:41:39 -07:00
((Release)item).Date = attrVal;
2019-01-11 13:43:15 -08:00
break;
case "default":
if (item.ItemType == ItemType.BiosSet)
((BiosSet)item).Default = attrVal.ToLowerInvariant().AsYesNo();
2019-01-11 13:43:15 -08:00
else if (item.ItemType == ItemType.Release)
((Release)item).Default = attrVal.ToLowerInvariant().AsYesNo();
2019-01-11 13:43:15 -08:00
break;
case "description":
if (item.ItemType == ItemType.BiosSet)
2020-06-15 12:41:39 -07:00
((BiosSet)item).Description = attrVal.ToLowerInvariant();
2019-01-11 13:43:15 -08:00
break;
case "region":
if (item.ItemType == ItemType.Release)
2020-06-15 12:41:39 -07:00
((Release)item).Region = attrVal.ToLowerInvariant();
2019-01-11 13:43:15 -08:00
break;
case "language":
if (item.ItemType == ItemType.Release)
2020-06-15 12:41:39 -07:00
((Release)item).Language = attrVal.ToLowerInvariant();
2019-01-11 13:43:15 -08:00
break;
}
}
// Now process and add the rom
ParseAddHelper(item);
2019-01-11 13:43:15 -08:00
}
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
{
Blank blank = new Blank()
{
IndexId = indexId,
IndexSource = filename,
2019-01-11 13:43:15 -08:00
};
2019-01-11 13:43:15 -08:00
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank);
2019-01-11 13:43:15 -08: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>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
2019-01-11 13:43:15 -08:00
// 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;
}
ClrMameProWriter cmpw = new ClrMameProWriter(fs, new UTF8Encoding(false))
{
Quotes = true
};
2019-01-11 13:43:15 -08:00
// Write out the header
2020-06-13 13:54:04 -07:00
WriteHeader(cmpw);
2019-01-11 13:43:15 -08:00
// Write out each of the machines and roms
string lastgame = null;
2020-07-26 21:00:30 -07:00
// Use a sorted list of games to output
2020-07-26 22:34:45 -07:00
foreach (string key in Items.SortedKeys)
2019-01-11 13:43:15 -08:00
{
2020-07-26 22:34:45 -07:00
List<DatItem> roms = Items[key];
2019-01-11 13:43:15 -08: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;
}
// 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())
2020-06-13 13:54:04 -07:00
WriteEndGame(cmpw, rom);
2019-01-11 13:43:15 -08:00
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
2020-06-13 13:54:04 -07:00
WriteStartGame(cmpw, rom);
2019-01-11 13:43:15 -08:00
// 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;
#if NET_FRAMEWORK
2020-06-05 22:26:44 -07:00
((Rom)rom).RIPEMD160 = ((Rom)rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
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
2020-06-13 13:54:04 -07:00
WriteDatItem(cmpw, rom, ignoreblanks);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
lastgame = rom.MachineName;
}
}
// Write the file footer out
2020-06-13 13:54:04 -07:00
WriteFooter(cmpw);
2019-01-11 13:43:15 -08:00
Globals.Logger.Verbose($"File written!{Environment.NewLine}");
cmpw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
2020-06-13 13:54:04 -07:00
/// <param name="cmpw">ClrMameProWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
2020-06-13 13:54:04 -07:00
private bool WriteHeader(ClrMameProWriter cmpw)
2019-01-11 13:43:15 -08:00
{
try
{
2020-06-13 13:54:04 -07:00
cmpw.WriteStartElement("clrmamepro");
cmpw.WriteStandalone("name", Header.Name);
cmpw.WriteStandalone("description", Header.Description);
if (!string.IsNullOrWhiteSpace(Header.Category))
cmpw.WriteStandalone("category", Header.Category);
cmpw.WriteStandalone("version", Header.Version);
if (!string.IsNullOrWhiteSpace(Header.Date))
cmpw.WriteStandalone("date", Header.Date);
cmpw.WriteStandalone("author", Header.Author);
if (!string.IsNullOrWhiteSpace(Header.Email))
cmpw.WriteStandalone("email", Header.Email);
if (!string.IsNullOrWhiteSpace(Header.Homepage))
cmpw.WriteStandalone("homepage", Header.Homepage);
if (!string.IsNullOrWhiteSpace(Header.Url))
cmpw.WriteStandalone("url", Header.Url);
if (!string.IsNullOrWhiteSpace(Header.Comment))
cmpw.WriteStandalone("comment", Header.Comment);
switch (Header.ForcePacking)
{
case ForcePacking.Unzip:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcezipping", "no", false);
break;
case ForcePacking.Zip:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcezipping", "yes", false);
break;
}
2020-06-12 11:02:23 -07:00
switch (Header.ForceMerging)
{
case ForceMerging.Full:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcemerging", "full", false);
break;
case ForceMerging.Split:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcemerging", "split", false);
break;
case ForceMerging.Merged:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcemerging", "merged", false);
break;
case ForceMerging.NonMerged:
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("forcemerging", "nonmerged", false);
break;
}
2019-01-11 13:43:15 -08:00
2020-06-12 11:02:23 -07:00
// End clrmamepro
2020-06-13 13:54:04 -07:00
cmpw.WriteEndElement();
2020-06-12 11:02:23 -07:00
2020-06-13 13:54:04 -07:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
2020-06-13 13:54:04 -07:00
/// <param name="cmpw">ClrMameProWriter 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>
2020-06-13 13:54:04 -07:00
private bool WriteStartGame(ClrMameProWriter cmpw, 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-13 13:54:04 -07:00
cmpw.WriteStartElement(datItem.MachineType == MachineType.Bios ? "resource" : "game");
cmpw.WriteStandalone("name", datItem.GetField(Field.MachineName, Header.ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RomOf, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("romof", datItem.RomOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("cloneof", datItem.CloneOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("sampleof", datItem.SampleOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("description", datItem.MachineDescription);
else if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("description", datItem.MachineName);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("year", datItem.Year);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Manufacturer, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("manufacturer", datItem.Manufacturer);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Category, Header.ExcludeFields)))
cmpw.WriteStandalone("category", datItem.Category);
2019-01-11 13:43:15 -08:00
2020-06-13 13:54:04 -07:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game end using the supplied StreamWriter
/// </summary>
2020-06-13 13:54:04 -07:00
/// <param name="cmpw">ClrMameProWriter 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>
2020-06-13 13:54:04 -07:00
private bool WriteEndGame(ClrMameProWriter cmpw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
try
{
// Build the state based on excluded fields
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteStandalone("sampleof", datItem.SampleOf);
2020-06-12 11:02:23 -07:00
// End game
2020-06-13 13:54:04 -07:00
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
2020-06-13 13:54:04 -07:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
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>
2020-06-13 13:54:04 -07:00
/// <param name="cmpw">ClrMameProWriter 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>
2020-06-13 13:54:04 -07:00
private bool WriteDatItem(ClrMameProWriter cmpw, 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
{
// 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-13 13:54:04 -07:00
cmpw.WriteStartElement("archive");
cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, Header.ExcludeFields));
2020-06-13 13:54:04 -07:00
cmpw.WriteEndElement();
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-13 13:54:04 -07:00
cmpw.WriteStartElement("biosset");
cmpw.WriteAttributeString("name", biosSet.GetField(Field.Name, Header.ExcludeFields));
if (!string.IsNullOrWhiteSpace(biosSet.GetField(Field.BiosDescription, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("description", biosSet.Description);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Default) && biosSet.Default != null)
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("default", biosSet.Default.ToString().ToLowerInvariant());
cmpw.WriteEndElement();
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-13 13:54:04 -07:00
cmpw.WriteStartElement("disk");
cmpw.WriteAttributeString("name", disk.GetField(Field.Name, Header.ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("md5", disk.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("ripemd160", disk.RIPEMD160.ToLowerInvariant());
#endif
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha1", disk.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha256", disk.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha384", disk.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha512", disk.SHA512.ToLowerInvariant());
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Status) && disk.ItemStatus != ItemStatus.None)
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("flags", disk.ItemStatus.ToString().ToLowerInvariant());
cmpw.WriteEndElement();
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-13 13:54:04 -07:00
cmpw.WriteStartElement("release");
cmpw.WriteAttributeString("name", release.GetField(Field.Name, Header.ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("region", release.Region);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Language, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("language", release.Language);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("date", release.Date);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Default) && release.Default != null)
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("default", release.Default.ToString().ToLowerInvariant());
cmpw.WriteEndElement();
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-13 13:54:04 -07:00
cmpw.WriteStartElement("rom");
cmpw.WriteAttributeString("name", rom.GetField(Field.Name, Header.ExcludeFields));
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Size) && rom.Size != -1)
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("size", rom.Size.ToString());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("md5", rom.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("ripemd160", rom.RIPEMD160.ToLowerInvariant());
#endif
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha1", rom.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha256", rom.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha384", rom.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("sha512", rom.SHA512.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, Header.ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("date", rom.Date);
2020-07-27 15:21:59 -07:00
if (!Header.ExcludeFields.Contains(Field.Status) && rom.ItemStatus != ItemStatus.None)
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("flags", rom.ItemStatus.ToString().ToLowerInvariant());
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Sample:
2020-06-13 13:54:04 -07:00
cmpw.WriteStartElement("sample");
cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, Header.ExcludeFields));
2020-06-13 13:54:04 -07:00
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
}
2020-06-13 13:54:04 -07:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
2020-06-13 13:54:04 -07:00
/// <param name="cmpw">ClrMameProWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
2020-06-13 13:54:04 -07:00
private bool WriteFooter(ClrMameProWriter cmpw)
2019-01-11 13:43:15 -08:00
{
try
{
2020-06-12 11:02:23 -07:00
// End game
2020-06-13 13:54:04 -07:00
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
2020-06-13 13:54:04 -07:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}