2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using SabreTools.Library.Data;
|
2017-11-02 15:44:15 -07:00
|
|
|
|
using SabreTools.Library.DatItems;
|
2020-08-01 22:46:28 -07:00
|
|
|
|
using SabreTools.Library.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using SabreTools.Library.Tools;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatFiles
|
|
|
|
|
|
{
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents parsing and writing of a DosCenter DAT
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class DosCenter : DatFile
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
public DosCenter(DatFile datFile)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
: base(datFile)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-12 13:48:49 -07:00
|
|
|
|
/// Parse a DOSCenter DAT and return all found games and roms within
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <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>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
protected override void ParseFile(
|
2019-01-11 13:43:15 -08:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
int indexId,
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// Miscellaneous
|
2020-07-15 09:41:59 -07:00
|
|
|
|
bool keep)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-06-12 13:48:49 -07:00
|
|
|
|
// Open a file reader
|
2020-07-15 09:41:59 -07:00
|
|
|
|
Encoding enc = FileExtensions.GetEncoding(filename);
|
|
|
|
|
|
ClrMameProReader cmpr = new ClrMameProReader(FileExtensions.TryOpenRead(filename), enc)
|
|
|
|
|
|
{
|
|
|
|
|
|
DosCenter = true
|
|
|
|
|
|
};
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
while (!cmpr.EndOfStream)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
cmpr.ReadNextLine();
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
// Ignore everything not top-level
|
|
|
|
|
|
if (cmpr.RowType != CmpRowType.TopLevel)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Switch on the top-level name
|
|
|
|
|
|
switch (cmpr.TopLevel.ToLowerInvariant())
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
// Header values
|
|
|
|
|
|
case "doscenter":
|
|
|
|
|
|
ReadHeader(cmpr);
|
|
|
|
|
|
break;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
// Sets
|
|
|
|
|
|
case "game":
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ReadGame(cmpr, filename, indexId);
|
2020-06-15 12:41:39 -07:00
|
|
|
|
break;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
cmpr.Dispose();
|
2020-06-12 13:48:49 -07: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>
|
|
|
|
|
|
private void ReadHeader(ClrMameProReader cmpr)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If there's no subtree to the header, skip it
|
2020-06-15 12:41:39 -07:00
|
|
|
|
if (cmpr == null || cmpr.EndOfStream)
|
2020-06-12 13:48:49 -07: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)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
cmpr.ReadNextLine();
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
// 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)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
string itemKey = cmpr.Standalone?.Key.ToLowerInvariant().TrimEnd(':');
|
|
|
|
|
|
string itemVal = cmpr.Standalone?.Value;
|
|
|
|
|
|
|
|
|
|
|
|
// For all other cases
|
|
|
|
|
|
switch (itemKey)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "name":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Name = (string.IsNullOrWhiteSpace(Header.Name) ? itemVal : Header.Name);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "description":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Description = (string.IsNullOrWhiteSpace(Header.Description) ? itemVal : Header.Description);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "dersion":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Version = (string.IsNullOrWhiteSpace(Header.Version) ? itemVal : Header.Version);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "date":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Date = (string.IsNullOrWhiteSpace(Header.Date) ? itemVal : Header.Date);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "author":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Author = (string.IsNullOrWhiteSpace(Header.Author) ? itemVal : Header.Author);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "homepage":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Homepage = (string.IsNullOrWhiteSpace(Header.Homepage) ? itemVal : Header.Homepage);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
break;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
case "comment":
|
2020-07-27 10:26:08 -07:00
|
|
|
|
Header.Comment = (string.IsNullOrWhiteSpace(Header.Comment) ? itemVal : Header.Comment);
|
2020-06-12 13:48:49 -07: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>
|
2020-06-12 13:48:49 -07:00
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2020-06-12 13:48:49 -07:00
|
|
|
|
private void ReadGame(
|
2020-06-15 12:41:39 -07:00
|
|
|
|
ClrMameProReader cmpr,
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
int indexId)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Prepare all internal variables
|
|
|
|
|
|
bool containsItems = false;
|
|
|
|
|
|
Machine machine = new Machine()
|
|
|
|
|
|
{
|
|
|
|
|
|
MachineType = 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)
|
2020-06-12 13:48:49 -07: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)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
cmpr.ReadNextLine();
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore comments and nothingness
|
|
|
|
|
|
if (cmpr.RowType == CmpRowType.None || cmpr.RowType == CmpRowType.Comment)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// 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.ToLowerInvariant().EndsWith(".zip") ? itemVal.Remove(itemVal.Length - 4) : itemVal);
|
|
|
|
|
|
machine.Description = (itemVal.ToLowerInvariant().EndsWith(".zip") ? itemVal.Remove(itemVal.Length - 4) : itemVal);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle any internal items
|
|
|
|
|
|
else if (cmpr.RowType == CmpRowType.Internal
|
|
|
|
|
|
&& string.Equals(cmpr.InternalName, "file", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
&& cmpr.Internal != null)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
containsItems = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Create the proper DatItem based on the type
|
2020-07-15 09:41:59 -07:00
|
|
|
|
Rom item = DatItem.Create(ItemType.Rom) as Rom;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Then populate it with information
|
|
|
|
|
|
item.CopyMachineInformation(machine);
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
item.IndexId = indexId;
|
|
|
|
|
|
item.IndexSource = filename;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
// Loop through all of the attributes
|
|
|
|
|
|
foreach (var kvp in cmpr.Internal)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
string attrKey = kvp.Key;
|
|
|
|
|
|
string attrVal = kvp.Value;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
|
2020-06-15 12:41:39 -07:00
|
|
|
|
switch (attrKey)
|
2020-06-12 13:48:49 -07:00
|
|
|
|
{
|
2020-06-15 12:41:39 -07:00
|
|
|
|
//If the item is empty, we automatically skip it because it's a fluke
|
|
|
|
|
|
case "":
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Regular attributes
|
|
|
|
|
|
case "name":
|
|
|
|
|
|
item.Name = attrVal;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case "size":
|
|
|
|
|
|
if (Int64.TryParse(attrVal, out long size))
|
|
|
|
|
|
item.Size = size;
|
|
|
|
|
|
else
|
|
|
|
|
|
item.Size = -1;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case "crc":
|
2020-07-15 09:41:59 -07:00
|
|
|
|
item.CRC = attrVal;
|
2020-06-15 12:41:39 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case "date":
|
|
|
|
|
|
item.Date = attrVal;
|
|
|
|
|
|
break;
|
2020-06-12 13:48:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now process and add the rom
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ParseAddHelper(item);
|
2020-06-12 15:42:47 -07:00
|
|
|
|
}
|
2020-06-12 13:48:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If no items were found for this machine, add a Blank placeholder
|
|
|
|
|
|
if (!containsItems)
|
|
|
|
|
|
{
|
|
|
|
|
|
Blank blank = new Blank()
|
|
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
IndexId = indexId,
|
|
|
|
|
|
IndexSource = filename,
|
2020-06-12 13:48:49 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
blank.CopyMachineInformation(machine);
|
|
|
|
|
|
|
|
|
|
|
|
// Now process and add the rom
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ParseAddHelper(blank);
|
2020-06-12 13:48:49 -07:00
|
|
|
|
}
|
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
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.User($"Opening file for writing: {outfile}");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ClrMameProWriter cmpw = new ClrMameProWriter(fs, new UTF8Encoding(false))
|
|
|
|
|
|
{
|
|
|
|
|
|
Quotes = false
|
|
|
|
|
|
};
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
2020-07-15 09:41:59 -07:00
|
|
|
|
WriteEndGame(cmpw);
|
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")
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
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-07-15 09:41:59 -07:00
|
|
|
|
#if NET_FRAMEWORK
|
2020-06-05 22:26:44 -07:00
|
|
|
|
((Rom)rom).RIPEMD160 = ((Rom)rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#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
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"File written!{Environment.NewLine}");
|
2020-06-14 14:16:03 -07:00
|
|
|
|
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("DOSCenter");
|
2020-07-27 10:26:08 -07:00
|
|
|
|
cmpw.WriteStandalone("Name:", Header.Name, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Description:", Header.Description, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Version:", Header.Version, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Date:", Header.Date, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Author:", Header.Author, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Homepage:", Header.Homepage, false);
|
|
|
|
|
|
cmpw.WriteStandalone("Comment:", Header.Comment, false);
|
2020-06-13 13:54:04 -07:00
|
|
|
|
cmpw.WriteEndElement();
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
2020-06-10 22:37:19 -07:00
|
|
|
|
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
// Build the state based on excluded fields
|
2020-06-13 13:54:04 -07:00
|
|
|
|
cmpw.WriteStartElement("game");
|
2020-07-27 10:26:08 -07:00
|
|
|
|
cmpw.WriteStandalone("name", $"{datItem.GetField(Field.MachineName, Header.ExcludeFields)}.zip", true);
|
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>
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
private bool WriteEndGame(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-13 13:54:04 -07:00
|
|
|
|
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
2020-06-10 22:37:19 -07:00
|
|
|
|
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
|
2020-06-10 22:37:19 -07:00
|
|
|
|
ProcessItemName(datItem, true);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
// Build the state based on excluded fields
|
|
|
|
|
|
switch (datItem.ItemType)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
case ItemType.Rom:
|
2020-06-10 22:37:19 -07:00
|
|
|
|
var rom = datItem as Rom;
|
2020-06-13 13:54:04 -07:00
|
|
|
|
cmpw.WriteStartElement("file");
|
2020-07-27 10:26:08 -07:00
|
|
|
|
cmpw.WriteAttributeString("name", datItem.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());
|
2020-07-27 10:26:08 -07:00
|
|
|
|
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 10:26:08 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, Header.ExcludeFields)))
|
2020-06-13 13:54:04 -07:00
|
|
|
|
cmpw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|