Files
SabreTools/SabreTools.DatFiles/Formats/OpenMSX.Writer.cs

184 lines
6.0 KiB
C#
Raw Normal View History

2023-07-31 13:14:42 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-07-31 13:14:42 -04:00
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents writing an openMSX softawre list XML DAT
/// </summary>
internal partial class OpenMSX : DatFile
{
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[]
{
ItemType.Rom
};
}
/// <inheritdoc/>
protected override List<DatItemField>? GetMissingRequiredFields(DatItem datItem)
2023-07-31 13:14:42 -04:00
{
var missingFields = new List<DatItemField>();
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;
2023-07-31 13:14:42 -04:00
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
logger.User($"Writing to '{outfile}'...");
// TODO: Write out comment prefix somehow
var softwaredb = CreateSoftwareDb(ignoreblanks);
2023-09-11 01:20:21 -04:00
if (!(new Serialization.Files.OpenMSX().SerializeToFileWithDocType(softwaredb, outfile)))
2023-07-31 13:14:42 -04:00
{
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
2023-07-31 13:14:42 -04:00
return false;
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
logger.User($"'{outfile}' written!{Environment.NewLine}");
2023-07-31 13:14:42 -04:00
return true;
}
#region Converters
2023-07-31 13:14:42 -04:00
/// <summary>
/// Create a SoftwareDb from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.OpenMSX.SoftwareDb CreateSoftwareDb(bool ignoreblanks)
2023-07-31 13:14:42 -04:00
{
var softwaredb = new Models.OpenMSX.SoftwareDb
{
Timestamp = Header.Date,
Software = CreateSoftwares(ignoreblanks)
};
return softwaredb;
2023-07-31 13:14:42 -04:00
}
/// <summary>
/// Create an array of Software from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.OpenMSX.Software[]? CreateSoftwares(bool ignoreblanks)
2023-07-31 13:14:42 -04:00
{
// If we don't have items, we can't do anything
if (this.Items == null || !this.Items.Any())
return null;
2023-07-31 13:14:42 -04:00
// Create a list of hold the games
var softwares = new List<Models.OpenMSX.Software>();
2023-07-31 13:14:42 -04:00
// Loop through the sorted items and create games for them
foreach (string key in Items.SortedKeys)
{
var items = Items.FilteredItems(key);
if (items == null || !items.Any())
continue;
2023-07-31 13:14:42 -04:00
// Get the first item for game information
var machine = items[0].Machine;
var software = new Models.OpenMSX.Software
{
Title = machine?.Name,
GenMSXID = machine?.GenMSXID,
System = machine?.System,
Company = machine?.Manufacturer,
Year = machine?.Year,
Country = machine?.Country,
};
2023-07-31 13:14:42 -04:00
// Create holder for dumps
var dumps = new List<Models.OpenMSX.Dump>();
2023-07-31 13:14:42 -04:00
// Loop through and convert the items to respective lists
for (int index = 0; index < items.Count; index++)
{
// Get the item
var item = items[index];
2023-07-31 13:14:42 -04:00
// Check for a "null" item
item = ProcessNullifiedItem(item);
2023-07-31 13:14:42 -04:00
// Skip if we're ignoring the item
if (ShouldIgnore(item, ignoreblanks))
continue;
2023-07-31 13:14:42 -04:00
switch (item)
2023-07-31 13:14:42 -04:00
{
case Rom rom:
dumps.Add(CreateDump(rom));
2023-07-31 13:14:42 -04:00
break;
}
}
2023-07-31 13:14:42 -04:00
software.Dump = dumps.ToArray();
softwares.Add(software);
2023-07-31 13:14:42 -04:00
}
return softwares.ToArray();
2023-07-31 13:14:42 -04:00
}
/// <summary>
/// Create a Dump from the current Rom DatItem
/// <summary>
private static Models.OpenMSX.Dump CreateDump(Rom item)
2023-07-31 13:14:42 -04:00
{
Models.OpenMSX.Original? original = null;
if (item.OriginalSpecified && item.Original != null)
{
original = new Models.OpenMSX.Original { Content = item.Original.Content };
if (item.Original.Value != null)
original.Value = item.Original.Value.ToString();
}
2023-07-31 13:14:42 -04:00
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;
2023-07-31 13:14:42 -04:00
}
#endregion
2023-07-31 13:14:42 -04:00
}
}