Files
SabreTools/SabreTools.DatFiles/Formats/AttractMode.Reader.cs

105 lines
4.8 KiB
C#
Raw Normal View History

2023-07-29 00:06:31 -04:00
using System;
using System.Linq;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
2024-03-06 11:23:22 -05:00
using SabreTools.Hashing;
2023-07-29 00:06:31 -04:00
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents parsing of an AttractMode DAT
/// </summary>
internal partial class AttractMode : DatFile
{
/// <inheritdoc/>
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
try
{
// Deserialize the input file
2023-09-11 01:20:21 -04:00
var metadataFile = new Serialization.Files.AttractMode().Deserialize(filename);
2023-07-29 00:06:31 -04:00
// 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
/// <summary>
/// Convert rows information
/// </summary>
/// <param name="rows">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
2023-09-11 01:20:21 -04:00
private void ConvertRows(Models.AttractMode.Row?[]? rows, string filename, int indexId, bool statsOnly)
2023-07-29 00:06:31 -04:00
{
// 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);
}
}
/// <summary>
/// Convert rows information
/// </summary>
/// <param name="row">Deserialized model to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
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;
2024-03-09 23:43:43 -05:00
var machine = new Machine();
machine.SetFieldValue<string?>(Models.Metadata.Machine.ButtonsKey, row.Buttons);
machine.SetFieldValue<string?>(Models.Metadata.Machine.CategoryKey, row.Category);
machine.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, row.CloneOf);
machine.SetFieldValue<string?>(Models.Metadata.Machine.CommentKey, row.Extra);
machine.SetFieldValue<string?>(Models.Metadata.Machine.ControlKey, row.Control);
machine.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, row.Title);
machine.SetFieldValue<string?>(Models.Metadata.Machine.DisplayCountKey, row.DisplayCount);
machine.SetFieldValue<string?>(Models.Metadata.Machine.DisplayTypeKey, row.DisplayType);
machine.SetFieldValue<string?>(Models.Metadata.Machine.ManufacturerKey, row.Manufacturer);
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, row.Name);
machine.SetFieldValue<string?>(Models.Metadata.Machine.PlayersKey, row.Players);
machine.SetFieldValue<string?>(Models.Metadata.Machine.RotationKey, row.Rotation);
machine.SetFieldValue<string?>(Models.Metadata.Machine.StatusKey, row.Status);
machine.SetFieldValue<string?>(Models.Metadata.Machine.YearKey, row.Year);
2023-07-29 00:06:31 -04:00
2024-03-09 21:34:26 -05:00
var rom = new Rom()
{
2024-03-08 21:12:13 -05:00
Source = new Source { Index = indexId, Name = filename },
2023-07-29 00:06:31 -04:00
};
2024-03-08 20:42:24 -05:00
rom.SetName("-");
2024-03-09 21:34:26 -05:00
rom.SetFieldValue<string?>(Models.Metadata.Rom.AltRomnameKey, row.AltRomname);
rom.SetFieldValue<string?>(Models.Metadata.Rom.AltTitleKey, row.AltTitle);
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, Constants.CRCZero);
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, Constants.MD5Zero);
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, Constants.SHA1Zero);
rom.SetFieldValue<long?>(Models.Metadata.Rom.SizeKey, Constants.SizeZero);
rom.SetFieldValue<ItemStatus?>(Models.Metadata.Rom.StatusKey, ItemStatus.None);
2023-07-29 00:06:31 -04:00
// Now process and add the rom
2024-03-09 21:34:26 -05:00
rom.CopyMachineInformation(machine);
2023-07-29 00:06:31 -04:00
ParseAddHelper(rom, statsOnly);
}
#endregion
}
}