Files
SabreTools/SabreTools.Library/DatFiles/OpenMSX.cs

760 lines
27 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
2020-12-08 00:13:22 -08:00
using System.Xml.Schema;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
2020-12-07 15:08:57 -08:00
using SabreTools.IO;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a openMSX softawre list XML DAT
/// </summary>
internal class OpenMSX : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public OpenMSX(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse a openMSX softawre list XML 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>
/// <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
{
// Prepare all internal variables
2020-12-08 00:13:22 -08:00
XmlReader xtr = XmlReader.Create(filename, new XmlReaderSettings
{
CheckCharacters = false,
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
2019-01-11 13:43:15 -08:00
// If we got a null reader, just return
if (xtr == null)
return;
// Otherwise, read the file to the end
try
{
xtr.MoveToContent();
while (!xtr.EOF)
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
{
xtr.Read();
continue;
}
switch (xtr.Name)
{
case "softwaredb":
2020-10-07 16:11:05 -07:00
Header.Name = Header.Name ?? "openMSX Software List";
Header.Description = Header.Description ?? Header.Name;
Header.Date = Header.Date ?? xtr.GetAttribute("timestamp");
2019-01-11 13:43:15 -08:00
xtr.Read();
break;
2019-01-11 13:43:15 -08:00
// We want to process the entire subtree of the software
case "software":
ReadSoftware(xtr.ReadSubtree(), filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the software now that we've processed it
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
xtr.Read();
break;
}
}
}
catch (Exception ex)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read software information
/// </summary>
/// <param name="reader">XmlReader representing a machine block</param>
/// <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 ReadSoftware(XmlReader reader, string filename, int indexId)
2019-01-11 13:43:15 -08:00
{
// If we have an empty machine, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
int diskno = 0;
bool containsItems = false;
// Create a new machine
Machine machine = new Machine();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the roms from the machine
switch (reader.Name)
{
case "title":
machine.Name = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "genmsxid":
machine.GenMSXID = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "system":
machine.System = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "company":
machine.Manufacturer = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "country":
machine.Country = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "dump":
containsItems = ReadDump(reader.ReadSubtree(), machine, diskno, filename, indexId);
2019-01-11 13:43:15 -08:00
diskno++;
// Skip the dump now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// 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,
},
2019-01-11 13:43:15 -08:00
};
2020-08-20 13:17:14 -07: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>
/// Read dump information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</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 bool ReadDump(
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
List<DatItem> items = new List<DatItem>();
2020-09-02 23:02:06 -07:00
Original original = null;
2019-01-11 13:43:15 -08:00
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the dump
switch (reader.Name)
{
case "rom":
DatItem rom = ReadRom(reader.ReadSubtree(), machine, diskno, filename, indexId);
if (rom != null)
items.Add(rom);
2019-01-11 13:43:15 -08:00
// Skip the rom now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "megarom":
DatItem megarom = ReadMegaRom(reader.ReadSubtree(), machine, diskno, filename, indexId);
if (megarom != null)
items.Add(megarom);
2019-01-11 13:43:15 -08:00
// Skip the megarom now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "sccpluscart":
DatItem sccpluscart = ReadSccPlusCart(reader.ReadSubtree(), machine, diskno, filename, indexId);
if (sccpluscart != null)
items.Add(sccpluscart);
2019-01-11 13:43:15 -08:00
// Skip the sccpluscart now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "original":
2020-10-07 16:11:05 -07:00
original = new Original
{
Value = reader.GetAttribute("value").AsYesNo(),
Content = reader.ReadElementContentAsString()
};
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If we have any items, loop through and add them
foreach (DatItem item in items)
{
switch (item.ItemType)
{
case ItemType.Rom:
(item as Rom).Original = original;
break;
}
item.CopyMachineInformation(machine);
ParseAddHelper(item);
}
return items.Count > 0;
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read rom information
/// </summary>
/// <param name="reader">XmlReader representing a rom block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private DatItem ReadRom(
2019-01-11 13:43:15 -08:00
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
string hash = string.Empty,
offset = string.Empty,
type = string.Empty,
remark = string.Empty;
2019-01-11 13:43:15 -08:00
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the rom
switch (reader.Name)
{
case "hash":
hash = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "start":
offset = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "type":
type = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "remark":
remark = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If we got a hash, then create and return the item
if (!string.IsNullOrWhiteSpace(hash))
2019-01-11 13:43:15 -08:00
{
return new Rom
2020-08-20 13:17:14 -07:00
{
Name = machine.Name + "_" + diskno + (!string.IsNullOrWhiteSpace(remark) ? " " + remark : string.Empty),
Offset = offset,
2020-09-04 23:03:27 -07:00
Size = null,
SHA1 = hash,
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
OpenMSXSubType = OpenMSXSubType.Rom,
OpenMSXType = type,
Remark = remark,
};
}
2019-01-11 13:43:15 -08:00
// No valid item means returning null
return null;
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read megarom information
/// </summary>
/// <param name="reader">XmlReader representing a megarom block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private DatItem ReadMegaRom(
2019-01-11 13:43:15 -08:00
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
string hash = string.Empty,
offset = string.Empty,
type = string.Empty,
remark = string.Empty;
2019-01-11 13:43:15 -08:00
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the dump
switch (reader.Name)
{
case "hash":
hash = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "start":
offset = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "type":
reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "remark":
remark = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If we got a hash, then create and return the item
if (!string.IsNullOrWhiteSpace(hash))
2019-01-11 13:43:15 -08:00
{
return new Rom
2020-08-20 13:17:14 -07:00
{
Name = machine.Name + "_" + diskno + (!string.IsNullOrWhiteSpace(remark) ? " " + remark : string.Empty),
Offset = offset,
2020-09-04 23:03:27 -07:00
Size = null,
SHA1 = hash,
2019-01-11 13:43:15 -08:00
Source = new Source
{
Index = indexId,
Name = filename,
},
OpenMSXSubType = OpenMSXSubType.MegaRom,
OpenMSXType = type,
Remark = remark,
};
}
2019-01-11 13:43:15 -08:00
// No valid item means returning null
return null;
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read sccpluscart information
/// </summary>
/// <param name="reader">XmlReader representing a sccpluscart block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private DatItem ReadSccPlusCart(
2019-01-11 13:43:15 -08:00
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
string boot = string.Empty,
hash = string.Empty,
remark = string.Empty;
2019-01-11 13:43:15 -08:00
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the elements from the dump
switch (reader.Name)
{
case "boot":
boot = reader.ReadElementContentAsString();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case "hash":
hash = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "remark":
remark = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If we got a hash, then create and return the item
if (!string.IsNullOrWhiteSpace(hash))
2019-01-11 13:43:15 -08:00
{
return new Rom
2020-08-20 13:17:14 -07:00
{
Name = machine.Name + "_" + diskno + (!string.IsNullOrWhiteSpace(remark) ? " " + remark : string.Empty),
2020-09-04 23:03:27 -07:00
Size = null,
SHA1 = hash,
2019-01-11 13:43:15 -08:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
OpenMSXSubType = OpenMSXSubType.SCCPlusCart,
Boot = boot,
Remark = remark,
};
}
// No valid item means returning null
return null;
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;
}
XmlTextWriter xtw = new XmlTextWriter(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1
};
2019-01-11 13:43:15 -08:00
// Write out the header
WriteHeader(xtw);
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
// 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(xtw);
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(xtw, 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(xtw, 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
WriteFooter(xtw);
2019-01-11 13:43:15 -08:00
logger.Verbose("File written!" + Environment.NewLine);
xtw.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>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteHeader(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartDocument();
xtw.WriteDocType("softwaredb", null, "softwaredb1.dtd", null);
xtw.WriteStartElement("softwaredb");
xtw.WriteRequiredAttributeString("timestamp", Header.Date);
//TODO: Figure out how to fix the issue with removed formatting after this point
2020-06-11 11:44:46 -07:00
// xtw.WriteComment("Credits");
// xtw.WriteCData(@"The softwaredb.xml file contains information about rom mapper types
//-Copyright 2003 Nicolas Beyaert(Initial Database)
//-Copyright 2004 - 2013 BlueMSX Team
//-Copyright 2005 - 2020 openMSX Team
//-Generation MSXIDs by www.generation - msx.nl
//- Thanks go out to:
//-Generation MSX / Sylvester for the incredible source of information
//- p_gimeno and diedel for their help adding and valdiating ROM additions
//- GDX for additional ROM info and validations and corrections");
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteStartGame(XmlTextWriter xtw, 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);
// Build the state
xtw.WriteStartElement("software");
xtw.WriteRequiredElementString("title", datItem.Machine.Name);
xtw.WriteRequiredElementString("genmsxid", datItem.Machine.GenMSXID);
xtw.WriteRequiredElementString("system", datItem.Machine.System);
xtw.WriteRequiredElementString("company", datItem.Machine.Manufacturer);
xtw.WriteRequiredElementString("year", datItem.Machine.Year);
xtw.WriteRequiredElementString("country", datItem.Machine.Country);
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteEndGame(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End software
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItem(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// Pre-process the item name
ProcessItemName(datItem, true);
// Build the state
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Rom:
var rom = datItem as Rom;
xtw.WriteStartElement("dump");
2019-01-11 13:43:15 -08:00
if (rom.Original != null)
{
xtw.WriteStartElement("original");
xtw.WriteAttributeString("value", rom.Original.Value == true ? "true" : "false");
xtw.WriteString(rom.Original.Content);
xtw.WriteEndElement();
}
2019-01-11 13:43:15 -08:00
switch (rom.OpenMSXSubType)
{
// Default to Rom for converting from other formats
case OpenMSXSubType.Rom:
case OpenMSXSubType.NULL:
xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType());
xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant());
xtw.WriteOptionalElementString("start", rom.Offset);
xtw.WriteOptionalElementString("type", rom.OpenMSXType);
xtw.WriteOptionalElementString("remark", rom.Remark);
xtw.WriteEndElement();
break;
2020-09-15 12:12:13 -07:00
case OpenMSXSubType.MegaRom:
xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType());
xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant());
xtw.WriteOptionalElementString("start", rom.Offset);
xtw.WriteOptionalElementString("type", rom.OpenMSXType);
xtw.WriteOptionalElementString("remark", rom.Remark);
xtw.WriteEndElement();
break;
case OpenMSXSubType.SCCPlusCart:
xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType());
xtw.WriteOptionalElementString("boot", rom.Boot);
xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant());
xtw.WriteOptionalElementString("remark", rom.Remark);
xtw.WriteEndElement();
break;
}
// End dump
xtw.WriteEndElement();
break;
2019-01-11 13:43:15 -08:00
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End software
xtw.WriteEndElement();
// End softwaredb
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}