using System;
using System.Linq;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
///
/// Represents parsing of an AttractMode DAT
///
internal partial class AttractMode : DatFile
{
///
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
try
{
// Deserialize the input file
var metadataFile = new Serialization.Files.AttractMode().Deserialize(filename);
// Convert the row data to the internal format
ConvertRows(metadataFile?.Row, filename, indexId, statsOnly);
}
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - An error occurred during parsing";
logger.Error(ex, message);
}
}
#region Converters
///
/// Convert rows information
///
/// Array of deserialized models to convert
/// Name of the file to be parsed
/// Index ID for the DAT
/// True to only add item statistics while parsing, false otherwise
private void ConvertRows(Models.AttractMode.Row?[]? rows, string filename, int indexId, bool statsOnly)
{
// If the rows array is missing, we can't do anything
if (rows == null || !rows.Any())
return;
// Loop through the rows and add
foreach (var row in rows)
{
ConvertRow(row, filename, indexId, statsOnly);
}
}
///
/// Convert rows information
///
/// Deserialized model to convert
/// Name of the file to be parsed
/// Index ID for the DAT
/// True to only add item statistics while parsing, false otherwise
private void ConvertRow(Models.AttractMode.Row? row, string filename, int indexId, bool statsOnly)
{
// If the row is missing, we can't do anything
if (row == null)
return;
var rom = new Rom()
{
Name = "-",
Size = Constants.SizeZero,
CRC = Constants.CRCZero,
MD5 = Constants.MD5Zero,
SHA1 = Constants.SHA1Zero,
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = row.Name,
Description = row.Title,
CloneOf = row.CloneOf,
Year = row.Year,
Manufacturer = row.Manufacturer,
Category = row.Category,
Players = row.Players,
Rotation = row.Rotation,
Control = row.Control,
Status = row.Status,
DisplayCount = row.DisplayCount,
DisplayType = row.DisplayType,
Comment = row.Extra,
Buttons = row.Buttons
},
AltName = row.AltRomname,
AltTitle = row.AltTitle,
// TODO: Add extended fields
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom, statsOnly);
}
#endregion
}
}