using System; using System.IO; using System.Linq; using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.DatItems; using SabreTools.DatItems.Formats; namespace SabreTools.DatFiles.Formats { /// /// Represents parsing and writing of an Everdrive SMDB file /// internal partial class EverdriveSMDB : 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.EverdriveSMDB().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 /// /// Create a machine from the filename /// /// Filename to derive from /// Filled machine and new filename on success, null on error private static (Machine?, string?) DeriveMachine(string? filename) { // If the filename is missing, we can't do anything if (string.IsNullOrEmpty(filename)) return (null, null); string machineName = Path.GetFileNameWithoutExtension(filename); if (filename.Contains('/')) { string[] split = filename!.Split('/'); machineName = split[0]; filename = filename.Substring(machineName.Length + 1); } else if (filename.Contains('\\')) { string[] split = filename!.Split('\\'); machineName = split[0]; filename = filename.Substring(machineName.Length + 1); } var machine = new Machine { Name = machineName }; return (machine, filename); } /// /// 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.EverdriveSMDB.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 row 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.EverdriveSMDB.Row? row, string filename, int indexId, bool statsOnly) { // If the row is missing, we can't do anything if (row == null) return; (var machine, string? name) = DeriveMachine(row.Name); machine ??= new Machine { Name = Path.GetFileNameWithoutExtension(row.Name) }; var rom = new Rom() { Name = name, Size = NumberHelper.ConvertToInt64(row.Size), CRC = row.CRC32, MD5 = row.MD5, SHA1 = row.SHA1, SHA256 = row.SHA256, ItemStatus = ItemStatus.None, Source = new Source { Index = indexId, Name = filename, }, }; // Now process and add the rom rom.CopyMachineInformation(machine); ParseAddHelper(rom, statsOnly); } #endregion } }