using System; using System.Linq; using SabreTools.Core; using SabreTools.DatItems; using SabreTools.DatItems.Formats; using SabreTools.Hashing; 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 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 }; var rom = new Rom() { Source = new Source { Index = indexId, Name = filename }, }; rom.SetName("-"); rom.SetFieldValue(Models.Metadata.Rom.AltRomnameKey, row.AltRomname); rom.SetFieldValue(Models.Metadata.Rom.AltTitleKey, row.AltTitle); rom.SetFieldValue(Models.Metadata.Rom.CRCKey, Constants.CRCZero); rom.SetFieldValue(Models.Metadata.Rom.MD5Key, Constants.MD5Zero); rom.SetFieldValue(Models.Metadata.Rom.SHA1Key, Constants.SHA1Zero); rom.SetFieldValue(Models.Metadata.Rom.SizeKey, Constants.SizeZero); rom.SetFieldValue(Models.Metadata.Rom.StatusKey, ItemStatus.None); // Now process and add the rom rom.CopyMachineInformation(machine); ParseAddHelper(rom, statsOnly); } #endregion } }