Files
SabreTools/SabreTools.DatFiles/DosCenter.cs

448 lines
16 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
2020-12-07 15:08:57 -08:00
using SabreTools.IO;
namespace SabreTools.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)
: 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>
/// <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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
2020-06-12 13:48:49 -07:00
// Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename);
2020-12-08 00:13:22 -08:00
ClrMameProReader cmpr = new ClrMameProReader(File.OpenRead(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
{
try
{
cmpr.ReadNextLine();
2020-06-12 13:48:49 -07:00
// Ignore everything not top-level
if (cmpr.RowType != CmpRowType.TopLevel)
continue;
2020-06-15 12:41:39 -07:00
// Switch on the top-level name
switch (cmpr.TopLevel.ToLowerInvariant())
{
// Header values
case "doscenter":
ReadHeader(cmpr);
break;
2020-06-12 13:48:49 -07:00
// Sets
case "game":
ReadGame(cmpr, filename, indexId);
break;
2020-06-12 13:48:49 -07:00
default:
break;
}
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
cmpr.Dispose();
throw new Exception(message, ex);
}
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-10-07 16:11:05 -07:00
Header.Name = Header.Name ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "description":
2020-10-07 16:11:05 -07:00
Header.Description = Header.Description ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "dersion":
2020-10-07 16:11:05 -07:00
Header.Version = Header.Version ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "date":
2020-10-07 16:11:05 -07:00
Header.Date = Header.Date ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "author":
2020-10-07 16:11:05 -07:00
Header.Author = Header.Author ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "homepage":
2020-10-07 16:11:05 -07:00
Header.Homepage = Header.Homepage ?? itemVal;
2020-06-12 13:48:49 -07:00
break;
2020-06-15 12:41:39 -07:00
case "comment":
2020-10-07 16:11:05 -07:00
Header.Comment = Header.Comment ?? itemVal;
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>
/// <param name="indexId">Index ID for the DAT</param>
2020-08-28 15:06:07 -07:00
private void ReadGame(ClrMameProReader cmpr, string filename, int indexId)
2020-06-12 13:48:49 -07:00
{
// Prepare all internal variables
bool containsItems = false;
Machine machine = new Machine()
{
2020-08-24 13:53:53 -07:00
MachineType = MachineType.NULL,
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();
// 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
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-08-20 13:17:14 -07:00
item.Source = new Source
{
Index = indexId,
Name = 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":
2020-09-04 23:03:27 -07:00
item.Size = Sanitizer.CleanLong(attrVal);
2020-06-15 12:41:39 -07:00
break;
case "crc":
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
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-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-06-12 13:48:49 -07:00
};
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank);
2020-06-12 13:48:49 -07:00
}
2019-01-11 13:43:15 -08:00
}
2020-09-18 17:12:31 -07:00
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[] { ItemType.Rom };
}
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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
try
{
logger.User($"Opening file for writing: {outfile}");
2020-12-08 00:13:22 -08:00
FileStream fs = File.Create(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
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 = 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-08-28 15:06:07 -07:00
List<DatItem> datItems = Items.FilteredItems(key);
2019-01-11 13:43:15 -08:00
2020-09-25 20:25:29 -07:00
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
2020-08-28 15:06:07 -07:00
datItems = DatItem.ResolveNames(datItems);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
for (int index = 0; index < datItems.Count; index++)
2019-01-11 13:43:15 -08:00
{
2020-08-28 15:06:07 -07:00
DatItem datItem = datItems[index];
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
List<string> newsplit = datItem.Machine.Name.Split('\\').ToList();
2019-01-11 13:43:15 -08:00
// If we have a different game and we're not at the start of the list, output the end of last item
2020-08-28 15:06:07 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(cmpw);
2019-01-11 13:43:15 -08:00
// If we have a new game, output the beginning of the new item
2020-08-28 15:06:07 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(cmpw, datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(cmpw, datItem);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-28 15:06:07 -07:00
lastgame = datItem.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
// Write the file footer out
2020-06-13 13:54:04 -07:00
WriteFooter(cmpw);
2019-01-11 13:43:15 -08:00
logger.Verbose($"File written!{Environment.NewLine}");
cmpw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
logger.Error(ex);
if (throwOnError) throw ex;
2019-01-11 13:43:15 -08:00
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>
private void WriteHeader(ClrMameProWriter cmpw)
2019-01-11 13:43:15 -08:00
{
// Build the state
cmpw.WriteStartElement("DOSCenter");
2020-08-24 13:43:37 -07:00
cmpw.WriteRequiredStandalone("Name:", Header.Name, false);
cmpw.WriteRequiredStandalone("Description:", Header.Description, false);
cmpw.WriteRequiredStandalone("Version:", Header.Version, false);
cmpw.WriteRequiredStandalone("Date:", Header.Date, false);
cmpw.WriteRequiredStandalone("Author:", Header.Author, false);
cmpw.WriteRequiredStandalone("Homepage:", Header.Homepage, false);
cmpw.WriteRequiredStandalone("Comment:", Header.Comment, false);
2020-09-15 12:12:13 -07:00
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteStartGame(ClrMameProWriter cmpw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// No game should start with a path separator
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2020-09-15 12:12:13 -07:00
// Build the state
cmpw.WriteStartElement("game");
cmpw.WriteRequiredStandalone("name", $"{datItem.Machine.Name}.zip", true);
2019-01-11 13:43:15 -08:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteEndGame(ClrMameProWriter cmpw)
2019-01-11 13:43:15 -08:00
{
// End game
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItem(ClrMameProWriter cmpw, DatItem datItem)
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
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Rom:
var rom = datItem as Rom;
cmpw.WriteStartElement("file");
cmpw.WriteRequiredAttributeString("name", rom.Name);
cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString());
cmpw.WriteOptionalAttributeString("date", rom.Date);
cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant());
cmpw.WriteEndElement();
break;
2019-01-11 13:43:15 -08:00
}
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteFooter(ClrMameProWriter cmpw)
2019-01-11 13:43:15 -08:00
{
// End game
cmpw.WriteEndElement();
2019-01-11 13:43:15 -08:00
cmpw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}