2023-07-31 13:14:42 -04:00
|
|
|
using System;
|
2023-07-31 13:44:15 -04:00
|
|
|
using System.Linq;
|
2023-07-31 13:14:42 -04:00
|
|
|
using SabreTools.Core;
|
2023-07-31 14:11:26 -04:00
|
|
|
using SabreTools.Core.Tools;
|
2023-07-31 13:14:42 -04:00
|
|
|
using SabreTools.DatItems;
|
|
|
|
|
using SabreTools.DatItems.Formats;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.DatFiles.Formats
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents parsing an openMSX softawre list XML DAT
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal partial class OpenMSX : DatFile
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// Deserialize the input file
|
|
|
|
|
var softwareDb = Serialization.OpenMSX.Deserialize(filename);
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
// Convert the header to the internal format
|
|
|
|
|
ConvertHeader(softwareDb);
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
// Convert the software data to the internal format
|
|
|
|
|
ConvertSoftwares(softwareDb?.Software, filename, indexId, statsOnly);
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
string message = $"'{filename}' - An error occurred during parsing";
|
|
|
|
|
logger.Error(ex, message);
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
#region Converters
|
|
|
|
|
|
2023-07-31 13:14:42 -04:00
|
|
|
/// <summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// Convert header information
|
2023-07-31 13:14:42 -04:00
|
|
|
/// </summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="datafile">Deserialized model to convert</param>
|
|
|
|
|
private void ConvertHeader(Models.OpenMSX.SoftwareDb? datafile)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// If the datafile is missing, we can't do anything
|
|
|
|
|
if (datafile == null)
|
2023-07-31 13:14:42 -04:00
|
|
|
return;
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
Header.Name ??= "openMSX Software List";
|
|
|
|
|
Header.Description ??= Header.Name;
|
|
|
|
|
Header.Date ??= datafile.Timestamp;
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// Convert softwares information
|
2023-07-31 13:14:42 -04:00
|
|
|
/// </summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="softwares">Array of deserialized models to convert</param>
|
2023-07-31 13:14:42 -04:00
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
private void ConvertSoftwares(Models.OpenMSX.Software[]? softwares, string filename, int indexId, bool statsOnly)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// If the software array is missing, we can't do anything
|
|
|
|
|
if (softwares == null || !softwares.Any())
|
|
|
|
|
return;
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
// Loop through the software and add
|
|
|
|
|
foreach (var software in softwares)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
ConvertSoftware(software, filename, indexId, statsOnly);
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// Convert software information
|
2023-07-31 13:14:42 -04:00
|
|
|
/// </summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="software">Deserialized model to convert</param>
|
2023-07-31 13:14:42 -04:00
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
private void ConvertSoftware(Models.OpenMSX.Software software, string filename, int indexId, bool statsOnly, string dirname = null)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// If the software is missing, we can't do anything
|
|
|
|
|
if (software == null)
|
|
|
|
|
return;
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
// Create the machine for copying information
|
|
|
|
|
var machine = new Machine
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
Name = software.Title,
|
|
|
|
|
GenMSXID = software.GenMSXID,
|
|
|
|
|
System = software.System,
|
|
|
|
|
Manufacturer = software.Company,
|
|
|
|
|
Year = software.Year,
|
|
|
|
|
Country = software.Country,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Check if there are any items
|
|
|
|
|
bool containsItems = false;
|
|
|
|
|
ConvertDumps(software.Dump, machine, filename, indexId, statsOnly, ref containsItems);
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
// If we had no items, create a Blank placeholder
|
|
|
|
|
if (!containsItems)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
var blank = new Blank
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
|
|
|
|
Source = new Source
|
|
|
|
|
{
|
|
|
|
|
Index = indexId,
|
|
|
|
|
Name = filename,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
blank.CopyMachineInformation(machine);
|
|
|
|
|
ParseAddHelper(blank, statsOnly);
|
|
|
|
|
}
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// Convert Dump information
|
2023-07-31 13:14:42 -04:00
|
|
|
/// </summary>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="dumps">Array of deserialized models to convert</param>
|
|
|
|
|
/// <param name="machine">Prefilled machine to use</param>
|
2023-07-31 13:14:42 -04:00
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2023-07-31 13:44:15 -04:00
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
|
|
|
|
private void ConvertDumps(Models.OpenMSX.Dump[]? dumps, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// If the dumps array is missing, we can't do anything
|
|
|
|
|
if (dumps == null || !dumps.Any())
|
|
|
|
|
return;
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
containsItems = true;
|
|
|
|
|
int index = 0;
|
|
|
|
|
foreach (var dump in dumps)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
// If we don't have rom data, we can't do anything
|
|
|
|
|
if (dump?.Rom == null)
|
2023-07-31 13:14:42 -04:00
|
|
|
continue;
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
var rom = dump.Rom;
|
2023-07-31 13:14:42 -04:00
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
string name = $"{machine.Name}_{index++}{(!string.IsNullOrWhiteSpace(rom.Remark) ? $" {rom.Remark}" : string.Empty)}";
|
|
|
|
|
var item = new Rom
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
Name = name,
|
|
|
|
|
Offset = dump.Rom?.Start,
|
|
|
|
|
OpenMSXType = rom.Type,
|
|
|
|
|
SHA1 = rom.Hash,
|
|
|
|
|
Remark = rom.Remark,
|
2023-07-31 13:14:42 -04:00
|
|
|
|
|
|
|
|
Source = new Source
|
|
|
|
|
{
|
|
|
|
|
Index = indexId,
|
|
|
|
|
Name = filename,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
if (dump.Original != null)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
item.Original = new Original
|
|
|
|
|
{
|
2023-07-31 14:11:26 -04:00
|
|
|
Value = dump.Original.Value.AsYesNo(),
|
2023-07-31 13:44:15 -04:00
|
|
|
Content = dump.Original.Content,
|
|
|
|
|
};
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
switch (dump.Rom)
|
2023-07-31 13:14:42 -04:00
|
|
|
{
|
2023-07-31 13:44:15 -04:00
|
|
|
case Models.OpenMSX.Rom:
|
|
|
|
|
item.OpenMSXSubType = OpenMSXSubType.Rom;
|
2023-07-31 13:14:42 -04:00
|
|
|
break;
|
2023-07-31 13:44:15 -04:00
|
|
|
case Models.OpenMSX.MegaRom:
|
|
|
|
|
item.OpenMSXSubType = OpenMSXSubType.MegaRom;
|
2023-07-31 13:14:42 -04:00
|
|
|
break;
|
2023-07-31 13:44:15 -04:00
|
|
|
case Models.OpenMSX.SCCPlusCart:
|
|
|
|
|
item.OpenMSXSubType = OpenMSXSubType.SCCPlusCart;
|
2023-07-31 13:14:42 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 13:44:15 -04:00
|
|
|
item.CopyMachineInformation(machine);
|
|
|
|
|
ParseAddHelper(item, statsOnly);
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 13:44:15 -04:00
|
|
|
|
|
|
|
|
#endregion
|
2023-07-31 13:14:42 -04:00
|
|
|
}
|
|
|
|
|
}
|