using System; using System.Collections.Generic; using System.Linq; using SabreTools.Core; using SabreTools.DatItems; using SabreTools.DatItems.Formats; namespace SabreTools.DatFiles.Formats { /// /// Represents parsing and writing of a DosCenter DAT /// internal partial class DosCenter : DatFile { /// protected override ItemType[] GetSupportedTypes() { return new ItemType[] { ItemType.Rom }; } /// protected override List? GetMissingRequiredFields(DatItem datItem) { List missingFields = new(); // Check item name if (string.IsNullOrWhiteSpace(datItem.GetName())) missingFields.Add(DatItemField.Name); switch (datItem) { case Rom rom: if (!rom.SizeSpecified) missingFields.Add(DatItemField.Size); // if (string.IsNullOrWhiteSpace(rom.Date)) // missingFields.Add(DatItemField.Date); if (string.IsNullOrWhiteSpace(rom.CRC)) missingFields.Add(DatItemField.CRC); break; } return missingFields; } /// public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { logger.User($"Writing to '{outfile}'..."); var metadataFile = CreateMetadataFile(ignoreblanks); if (!Serialization.DosCenter.SerializeToFile(metadataFile, outfile)) { logger.Warning($"File '{outfile}' could not be written! See the log for more details."); return false; } } catch (Exception ex) when (!throwOnError) { logger.Error(ex); return false; } logger.User($"'{outfile}' written!{Environment.NewLine}"); return true; } #region Converters /// /// Create a MetadataFile from the current internal information /// /// True if blank roms should be skipped on output, false otherwise private Models.DosCenter.MetadataFile CreateMetadataFile(bool ignoreblanks) { var metadataFile = new Models.DosCenter.MetadataFile { DosCenter = CreateDosCenter(), Game = CreateGames(ignoreblanks) }; return metadataFile; } /// /// Create a DosCenter from the current internal information /// private Models.DosCenter.DosCenter? CreateDosCenter() { // If we don't have a header, we can't do anything if (this.Header == null) return null; var clrMamePro = new Models.DosCenter.DosCenter { Name = Header.Name, Description = Header.Description, Version = Header.Version, Date = Header.Date, Author = Header.Author, Homepage = Header.Homepage, Comment = Header.Comment, }; return clrMamePro; } /// /// Create an array of GameBase from the current internal information /// /// True if blank roms should be skipped on output, false otherwise private Models.DosCenter.Game[]? CreateGames(bool ignoreblanks) { // If we don't have items, we can't do anything if (this.Items == null || !this.Items.Any()) return null; // Create a list of hold the games var games = new List(); // 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; // Get the first item for game information var machine = items[0].Machine; // We re-add the missing parts of the game name var game = new Models.DosCenter.Game { Name = $"\"{machine.Name}.zip\"" }; // Create holders for all item types var files = new List(); // Loop through and convert the items to respective lists for (int index = 0; index < items.Count; index++) { // Get the item var item = items[index]; // Check for a "null" item item = ProcessNullifiedItem(item); // Skip if we're ignoring the item if (ShouldIgnore(item, ignoreblanks)) continue; switch (item) { case Rom rom: files.Add(CreateFile(rom)); break; } } // Assign the values to the game game.File = files.ToArray(); // Add the game to the list games.Add(game); } return games.ToArray(); } /// /// Create a File from the current Rom DatItem /// private static Models.DosCenter.File CreateFile(Rom item) { var rom = new Models.DosCenter.File { Name = item.Name, Size = item.Size?.ToString(), CRC = item.CRC, Date = item.Date, }; return rom; } #endregion } }