Files
SabreTools/SabreTools.Library/DatFiles/DosCenter.cs

526 lines
21 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2020-06-12 13:48:49 -07:00
using System.Text.RegularExpressions;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.Tools;
2020-06-13 22:39:22 -07:00
using SabreTools.Library.Writers;
using NaturalSort;
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)
: base(datFile, cloneHeader: false)
{
}
/// <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="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)
{
2020-06-12 13:48:49 -07:00
// Open a file reader
Encoding enc = Utilities.GetEncoding(filename);
StreamReader sr = new StreamReader(Utilities.TryOpenRead(filename), enc);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
// 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 == "doscenter")
ReadHeader(sr, keep);
2020-06-12 15:42:47 -07:00
// If we have a game
2020-06-12 13:48:49 -07:00
else if (normalizedValue == "game" )
ReadGame(sr, filename, sysid, srcid, clean, remUnicode);
}
}
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)
{
// 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))
{
// 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);
// Some dats don't have the space between "Name:" and the dat name
if (line.Trim().StartsWith("Name:"))
{
2020-06-15 10:56:47 -07:00
Name = (string.IsNullOrWhiteSpace(Name) ? line.Substring("Name:".Length).Trim() : Name);
2020-06-12 13:48:49 -07:00
line = reader.ReadLine();
continue;
}
switch (gc[1].Value)
{
case "Name:":
Name = (string.IsNullOrWhiteSpace(Name) ? itemval : Name);
break;
case "Description:":
Description = (string.IsNullOrWhiteSpace(Description) ? itemval : Description);
break;
case "Version:":
Version = (string.IsNullOrWhiteSpace(Version) ? itemval : Version);
break;
case "Date:":
Date = (string.IsNullOrWhiteSpace(Date) ? itemval : Date);
break;
case "Author:":
Author = (string.IsNullOrWhiteSpace(Author) ? itemval : Author);
break;
case "Homepage:":
Homepage = (string.IsNullOrWhiteSpace(Homepage) ? itemval : Homepage);
break;
case "Comment:":
Comment = (string.IsNullOrWhiteSpace(Comment) ? itemval : Comment);
break;
}
line = reader.ReadLine();
}
}
/// <summary>
/// Read set information
/// </summary>
/// <param name="reader">StreamReader to use to parse the header</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 ReadGame(
StreamReader reader,
// 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 = 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))
{
// Item-specific lines have a known pattern
string trimmedline = line.Trim();
if (trimmedline.StartsWith("file ("))
{
containsItems = true;
ItemType temptype = ItemType.Rom;
// 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;
// Get the line split by spaces and quotes
string[] linegc = Utilities.SplitLineAsCMP(line);
// 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")
{
2020-06-15 10:56:47 -07:00
item.Name += $"{linegc[i]}";
2020-06-12 13:48:49 -07:00
}
// Perform correction
item.Name = item.Name.TrimStart();
i--;
}
// Get the size from the next part
else if (linegc[i] == "size")
{
if (!Int64.TryParse(linegc[++i], out long tempsize))
tempsize = 0;
((Rom)item).Size = tempsize;
}
// Get the date from the next part
else if (linegc[i] == "date")
{
((Rom)item).Date = $"{linegc[++i].Replace("\"", string.Empty)} {linegc[++i].Replace("\"", string.Empty)}";
}
// Get the CRC from the next part
else if (linegc[i] == "crc")
{
((Rom)item).CRC = linegc[++i].Replace("\"", string.Empty).ToLowerInvariant();
}
}
// Now process and add the rom
ParseAddHelper(item, clean, remUnicode);
line = reader.ReadLine();
continue;
}
2020-06-12 15:42:47 -07:00
// Game-specific lines have a known pattern
GroupCollection setgc = Regex.Match(line, Constants.ItemPatternCMP).Groups;
string itemval = setgc[2].Value.Replace("\"", string.Empty);
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;
}
2020-06-12 13:48:49 -07:00
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,
};
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank, clean, remUnicode);
}
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}");
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;
}
2020-06-13 13:54:04 -07:00
ClrMameProWriter cmpw = new ClrMameProWriter(fs, new UTF8Encoding(false));
cmpw.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;
// 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;
}
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-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
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
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("DOSCenter");
cmpw.WriteStandalone("Name:", Name, false);
cmpw.WriteStandalone("Description:", Description, false);
cmpw.WriteStandalone("Version:", Version, false);
cmpw.WriteStandalone("Date:", Date, false);
cmpw.WriteStandalone("Author:", Author, false);
cmpw.WriteStandalone("Homepage:", Homepage, false);
cmpw.WriteStandalone("Comment:", Comment, false);
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>
/// <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);
2019-01-11 13:43:15 -08:00
// Build the state based on excluded fields
2020-06-13 13:54:04 -07:00
cmpw.WriteStartElement("game");
cmpw.WriteStandalone("name", $"{datItem.GetField(Field.MachineName, 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>
/// <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
{
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>
/// <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.Rom:
var rom = datItem as Rom;
2020-06-13 13:54:04 -07:00
cmpw.WriteStartElement("file");
cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, ExcludeFields));
if (!ExcludeFields[(int)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.Date, ExcludeFields)))
2020-06-13 13:54:04 -07:00
cmpw.WriteAttributeString("date", rom.Date);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, 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;
}
}
}