Use OpenMSX serializer for writing, fix issues

This commit is contained in:
Matt Nadareski
2023-07-31 14:11:26 -04:00
parent 745cac1427
commit 2435c5ed24
5 changed files with 138 additions and 192 deletions

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using SabreTools.Core; using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
@@ -159,7 +160,7 @@ namespace SabreTools.DatFiles.Formats
{ {
item.Original = new Original item.Original = new Original
{ {
Value = dump.Original.Value, Value = dump.Original.Value.AsYesNo(),
Content = dump.Original.Content, Content = dump.Original.Content,
}; };
} }

View File

@@ -1,13 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.Linq;
using System.Text;
using System.Xml;
using SabreTools.Core; using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
using SabreTools.IO;
namespace SabreTools.DatFiles.Formats namespace SabreTools.DatFiles.Formats
{ {
@@ -28,8 +24,20 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/> /// <inheritdoc/>
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem) protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
{ {
// TODO: Check required fields var missingFields = new List<DatItemField>();
return null;
if (string.IsNullOrWhiteSpace(datItem.GetName()))
missingFields.Add(DatItemField.Name);
switch (datItem)
{
case Rom rom:
if (string.IsNullOrWhiteSpace(rom.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
}
return missingFields;
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -38,70 +46,14 @@ namespace SabreTools.DatFiles.Formats
try try
{ {
logger.User($"Writing to '{outfile}'..."); logger.User($"Writing to '{outfile}'...");
FileStream fs = System.IO.File.Create(outfile);
// If we get back null for some reason, just log and return // TODO: Write out comment prefix somehow
if (fs == null) var softwaredb = CreateSoftwareDb(ignoreblanks);
if (!Serialization.OpenMSX.SerializeToFileWithDocType(softwaredb, outfile))
{ {
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable"); logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
return false; return false;
} }
XmlTextWriter xtw = new(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1
};
// Write out the header
WriteHeader(xtw);
// Write out each of the machines and roms
string lastgame = null;
// Use a sorted list of games to output
foreach (string key in Items.SortedKeys)
{
ConcurrentList<DatItem> datItems = Items.FilteredItems(key);
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
// Resolve the names in the block
datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < datItems.Count; index++)
{
DatItem datItem = datItems[index];
// 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() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, datItem);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(xtw, datItem);
// Set the new data to compare against
lastgame = datItem.Machine.Name;
}
}
// Write the file footer out
WriteFooter(xtw);
logger.User($"'{outfile}' written!{Environment.NewLine}");
xtw.Dispose();
fs.Dispose();
} }
catch (Exception ex) when (!throwOnError) catch (Exception ex) when (!throwOnError)
{ {
@@ -109,149 +61,124 @@ namespace SabreTools.DatFiles.Formats
return false; return false;
} }
logger.User($"'{outfile}' written!{Environment.NewLine}");
return true; return true;
} }
#region Converters
/// <summary> /// <summary>
/// Write out DAT header using the supplied StreamWriter /// Create a SoftwareDb from the current internal information
/// </summary> /// <summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private void WriteHeader(XmlTextWriter xtw) private Models.OpenMSX.SoftwareDb CreateSoftwareDb(bool ignoreblanks)
{ {
xtw.WriteStartDocument(); var softwaredb = new Models.OpenMSX.SoftwareDb
xtw.WriteDocType("softwaredb", null, "softwaredb1.dtd", null); {
Timestamp = Header.Date,
xtw.WriteStartElement("softwaredb"); Software = CreateSoftwares(ignoreblanks)
xtw.WriteRequiredAttributeString("timestamp", Header.Date); };
return softwaredb;
//TODO: Figure out how to fix the issue with removed formatting after this point
// 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();
} }
/// <summary> /// <summary>
/// Write out Game start using the supplied StreamWriter /// Create an array of Software from the current internal information
/// </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)
{
// 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();
}
/// <summary> /// <summary>
/// Write out Game start using the supplied StreamWriter /// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
/// </summary> private Models.OpenMSX.Software[]? CreateSoftwares(bool ignoreblanks)
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteEndGame(XmlTextWriter xtw)
{ {
// End software // If we don't have items, we can't do anything
xtw.WriteEndElement(); if (this.Items == null || !this.Items.Any())
return null;
xtw.Flush(); // Create a list of hold the games
} var softwares = new List<Models.OpenMSX.Software>();
/// <summary> // Loop through the sorted items and create games for them
/// Write out DatItem using the supplied StreamWriter foreach (string key in Items.SortedKeys)
/// </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)
{ {
// Pre-process the item name var items = Items.FilteredItems(key);
ProcessItemName(datItem, true); if (items == null || !items.Any())
continue;
// Build the state // Get the first item for game information
switch (datItem.ItemType) var machine = items[0].Machine;
var software = new Models.OpenMSX.Software
{ {
case ItemType.Rom: Title = machine.Name,
var rom = datItem as Rom; GenMSXID = machine.GenMSXID,
xtw.WriteStartElement("dump"); System = machine.System,
Company = machine.Manufacturer,
Year = machine.Year,
Country = machine.Country,
};
if (rom.Original != null) // Create holder for dumps
var dumps = new List<Models.OpenMSX.Dump>();
// Loop through and convert the items to respective lists
for (int index = 0; index < items.Count; index++)
{ {
xtw.WriteStartElement("original"); // Get the item
xtw.WriteAttributeString("value", rom.Original.Value == true ? "true" : "false"); var item = items[index];
xtw.WriteString(rom.Original.Content);
xtw.WriteEndElement();
}
switch (rom.OpenMSXSubType) // Check for a "null" item
item = ProcessNullifiedItem(item);
// Skip if we're ignoring the item
if (ShouldIgnore(item, ignoreblanks))
continue;
switch (item)
{ {
// Default to Rom for converting from other formats case Rom rom:
case OpenMSXSubType.Rom: dumps.Add(CreateDump(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;
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; break;
} }
// End dump
xtw.WriteEndElement();
break;
} }
xtw.Flush(); software.Dump = dumps.ToArray();
softwares.Add(software);
}
return softwares.ToArray();
} }
/// <summary> /// <summary>
/// Write out DAT footer using the supplied StreamWriter /// Create a Dump from the current Rom DatItem
/// </summary> /// <summary>
/// <param name="xtw">XmlTextWriter to output to</param> private static Models.OpenMSX.Dump CreateDump(Rom item)
private void WriteFooter(XmlTextWriter xtw)
{ {
// End software
xtw.WriteEndElement();
// End softwaredb Models.OpenMSX.Original original = null;
xtw.WriteEndElement(); if (item.OriginalSpecified)
{
original = new Models.OpenMSX.Original { Content = item.Original.Content };
if (item.Original.Value != null)
original.Value = item.Original.Value.ToString();
}
xtw.Flush(); Models.OpenMSX.RomBase rom = item.OpenMSXSubType switch
} {
OpenMSXSubType.MegaRom => new Models.OpenMSX.MegaRom(),
OpenMSXSubType.SCCPlusCart => new Models.OpenMSX.SCCPlusCart(),
_ => new Models.OpenMSX.Rom(),
};
rom.Start = item.Offset;
rom.Type = item.OpenMSXType;
rom.Hash = item.SHA1;
rom.Remark = item.Remark;
var dump = new Models.OpenMSX.Dump
{
Original = original,
Rom = rom,
};
return dump;
}
#endregion
} }
} }

View File

@@ -19,6 +19,23 @@
<!ELEMENT dump (#PCDATA)> <!ELEMENT dump (#PCDATA)>
"; ";
private const string OpenMSXCredits = @"<!-- Credits -->
<![CDATA[
The softwaredb.xml file contains information about rom mapper types
- Copyright 2003 Nicolas Beyaert (Initial Database)
- Copyright 2004-2013 BlueMSX Team
- Copyright 2005-2023 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
]]>";
/// <summary> /// <summary>
/// Constructor designed for casting a base DatFile /// Constructor designed for casting a base DatFile
/// </summary> /// </summary>

View File

@@ -6,8 +6,9 @@ namespace SabreTools.Models.OpenMSX
[XmlRoot("original")] [XmlRoot("original")]
public class Original public class Original
{ {
/// <remarks>Boolean?</remarks>
[XmlAttribute("value")] [XmlAttribute("value")]
public bool Value { get; set; } public string? Value { get; set; }
[XmlText] [XmlText]
public string? Content { get; set; } public string? Content { get; set; }

View File

@@ -30,7 +30,7 @@ namespace SabreTools.Test.Parser
{ {
var original = new Models.OpenMSX.Original var original = new Models.OpenMSX.Original
{ {
Value = false, Value = "false",
Content = "Original Name", Content = "Original Name",
}; };