diff --git a/SabreTools.DatFiles/DatFile.MetadataConverter.cs b/SabreTools.DatFiles/DatFile.MetadataConverter.cs index e8e1d2f3..931ef3a3 100644 --- a/SabreTools.DatFiles/DatFile.MetadataConverter.cs +++ b/SabreTools.DatFiles/DatFile.MetadataConverter.cs @@ -2,7 +2,6 @@ using System.Linq; namespace SabreTools.DatFiles { - // TODO: Have the converters return a value OR port functionality to DatFile // TODO: Figure out if there's a way to condense the various processing methods // TODO: Convert nested items (e.g. Configuration, DipLocation) // TODO: Determine which items need to have their values flipped (e.g. Part, DiskArea, DataArea) @@ -23,6 +22,8 @@ namespace SabreTools.DatFiles if (item == null || !item.Any()) return; + // TODO: Add header parsing + // Get the machines from the metadata var machines = item.Read(Models.Metadata.MetadataFile.MachineKey); if (machines == null) diff --git a/SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs b/SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs index 9ee956bc..fcf364ec 100644 --- a/SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs +++ b/SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; using SabreTools.Core; using SabreTools.Core.Tools; -using SabreTools.DatItems; -using SabreTools.DatItems.Formats; namespace SabreTools.DatFiles.Formats { @@ -20,12 +16,13 @@ namespace SabreTools.DatFiles.Formats { // Deserialize the input file var metadataFile = new Serialization.Files.ClrMamePro().Deserialize(filename, this.Quotes); + var metadata = new Serialization.CrossModel.ClrMamePro().Serialize(metadataFile); // Convert the header to the internal format ConvertHeader(metadataFile?.ClrMamePro, keep); - // Convert the game data to the internal format - ConvertGames(metadataFile?.Game, filename, indexId, statsOnly); + // Convert to the internal format + ConvertMetadata(metadata, filename, indexId, statsOnly); } catch (Exception ex) when (!throwOnError) { @@ -71,500 +68,6 @@ namespace SabreTools.DatFiles.Formats Header.Type ??= "SuperDAT"; } - /// - /// Convert games 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 ConvertGames(Models.ClrMamePro.GameBase?[]? games, string filename, int indexId, bool statsOnly) - { - // If the game array is missing, we can't do anything - if (games == null || !games.Any()) - return; - - // Loop through the games and add - foreach (var game in games) - { - ConvertGame(game, filename, indexId, statsOnly); - } - } - - /// - /// Convert game 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 ConvertGame(Models.ClrMamePro.GameBase? game, string filename, int indexId, bool statsOnly) - { - // If the game is missing, we can't do anything - if (game == null) - return; - - // Create the machine for copying information - var machine = new Machine(); - machine.SetFieldValue(Models.Metadata.Machine.CategoryKey, game.Category); - machine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, game.CloneOf); - machine.SetFieldValue(Models.Metadata.Machine.DescriptionKey, game.Description); - machine.SetFieldValue(Models.Metadata.Machine.ManufacturerKey, game.Manufacturer); - machine.SetFieldValue(Models.Metadata.Machine.NameKey, game.Name); - machine.SetFieldValue(Models.Metadata.Machine.RomOfKey, game.RomOf); - machine.SetFieldValue(Models.Metadata.Machine.SampleOfKey, game.SampleOf); - machine.SetFieldValue(Models.Metadata.Machine.YearKey, game.Year); - if (game is Models.ClrMamePro.Resource) - machine.SetFieldValue(Models.Metadata.Machine.IsBiosKey, true); - - // Check if there are any items - bool containsItems = false; - - // Loop through each type of item - ConvertReleases(game.Release, machine, filename, indexId, statsOnly, ref containsItems); - ConvertBiosSets(game.BiosSet, machine, filename, indexId, statsOnly, ref containsItems); - ConvertRoms(game.Rom, machine, filename, indexId, statsOnly, ref containsItems); - ConvertDisks(game.Disk, machine, filename, indexId, statsOnly, ref containsItems); - ConvertMedia(game.Media, machine, filename, indexId, statsOnly, ref containsItems); - ConvertArchives(game.Archive, machine, filename, indexId, statsOnly, ref containsItems); - ConvertChips(game.Chip, machine, filename, indexId, statsOnly, ref containsItems); - ConvertVideos(game.Video, machine, filename, indexId, statsOnly, ref containsItems); - ConvertSound(game.Sound, machine, filename, indexId, statsOnly, ref containsItems); - ConvertInput(game.Input, machine, filename, indexId, statsOnly, ref containsItems); - ConvertDipSwitches(game.DipSwitch, machine, filename, indexId, statsOnly, ref containsItems); - ConvertDriver(game.Driver, machine, filename, indexId, statsOnly, ref containsItems); - - // If we had no items, create a Blank placeholder - if (!containsItems) - { - var blank = new Blank - { - Source = new Source { Index = indexId, Name = filename }, - }; - - blank.CopyMachineInformation(machine); - ParseAddHelper(blank, statsOnly); - } - } - - /// - /// Convert Release information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertReleases(Models.ClrMamePro.Release[]? releases, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the release array is missing, we can't do anything - if (releases == null || !releases.Any()) - return; - - containsItems = true; - foreach (var release in releases) - { - var item = new Release - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(release.Name); - item.SetFieldValue(Models.Metadata.Release.DateKey, release.Date); - item.SetFieldValue(Models.Metadata.Release.DefaultKey, release.Default?.AsYesNo()); - item.SetFieldValue(Models.Metadata.Release.LanguageKey, release.Language); - item.SetFieldValue(Models.Metadata.Release.RegionKey, release.Region); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert BiosSet information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertBiosSets(Models.ClrMamePro.BiosSet[]? biossets, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the biosset array is missing, we can't do anything - if (biossets == null || !biossets.Any()) - return; - - containsItems = true; - foreach (var biosset in biossets) - { - var item = new BiosSet - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(biosset.Name); - item.SetFieldValue(Models.Metadata.BiosSet.DefaultKey, biosset.Default?.AsYesNo()); - item.SetFieldValue(Models.Metadata.BiosSet.DescriptionKey, biosset.Description); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Rom information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertRoms(Models.ClrMamePro.Rom[]? roms, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the rom array is missing, we can't do anything - if (roms == null || !roms.Any()) - return; - - containsItems = true; - foreach (var rom in roms) - { - var item = new Rom - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(rom.Name); - item.SetFieldValue(Models.Metadata.Rom.CRCKey, rom.CRC); - item.SetFieldValue(Models.Metadata.Rom.DateKey, rom.Date); - item.SetFieldValue(Models.Metadata.Rom.FlagsKey, rom.Flags); - item.SetFieldValue(Models.Metadata.Rom.HeaderKey, rom.Header); - item.SetFieldValue(Models.Metadata.Rom.InvertedKey, rom.Inverted?.AsYesNo()); - item.SetFieldValue(Models.Metadata.Rom.MIAKey, rom.MIA?.AsYesNo()); - item.SetFieldValue(Models.Metadata.Rom.MD5Key, rom.MD5); - item.SetFieldValue(Models.Metadata.Rom.MergeKey, rom.Merge); - item.SetFieldValue(Models.Metadata.Rom.OffsetKey, rom.Offs); - item.SetFieldValue(Models.Metadata.Rom.RegionKey, rom.Region); - item.SetFieldValue(Models.Metadata.Rom.SerialKey, rom.Serial); - item.SetFieldValue(Models.Metadata.Rom.SHA1Key, rom.SHA1); - item.SetFieldValue(Models.Metadata.Rom.SHA256Key, rom.SHA256); - item.SetFieldValue(Models.Metadata.Rom.SHA384Key, rom.SHA384); - item.SetFieldValue(Models.Metadata.Rom.SHA512Key, rom.SHA512); - item.SetFieldValue(Models.Metadata.Rom.SizeKey, NumberHelper.ConvertToInt64(rom.Size)); - item.SetFieldValue(Models.Metadata.Rom.SpamSumKey, rom.SpamSum); - item.SetFieldValue(Models.Metadata.Rom.StatusKey, rom.Status?.AsEnumValue() ?? ItemStatus.NULL); - item.SetFieldValue(Models.Metadata.Rom.xxHash364Key, rom.xxHash364); - item.SetFieldValue(Models.Metadata.Rom.xxHash3128Key, rom.xxHash3128); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Disk information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertDisks(Models.ClrMamePro.Disk[]? disks, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the disk array is missing, we can't do anything - if (disks == null || !disks.Any()) - return; - - containsItems = true; - foreach (var disk in disks) - { - var item = new Disk - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(disk.Name); - item.SetFieldValue(Models.Metadata.Disk.FlagsKey, disk.Flags); - item.SetFieldValue(Models.Metadata.Disk.MD5Key, disk.MD5); - item.SetFieldValue(Models.Metadata.Disk.MergeKey, disk.Merge); - item.SetFieldValue(Models.Metadata.Disk.SHA1Key, disk.SHA1); - item.SetFieldValue(Models.Metadata.Disk.StatusKey, disk.Status?.AsEnumValue() ?? ItemStatus.NULL); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Media information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertMedia(Models.ClrMamePro.Media[]? media, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the media array is missing, we can't do anything - if (media == null || !media.Any()) - return; - - containsItems = true; - foreach (var medium in media) - { - var item = new Media - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(medium.Name); - item.SetFieldValue(Models.Metadata.Media.MD5Key, medium.MD5); - item.SetFieldValue(Models.Metadata.Media.SHA1Key, medium.SHA1); - item.SetFieldValue(Models.Metadata.Media.SHA256Key, medium.SHA256); - item.SetFieldValue(Models.Metadata.Media.SpamSumKey, medium.SpamSum); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Archive information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertArchives(Models.ClrMamePro.Archive[]? archives, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the archive array is missing, we can't do anything - if (archives == null || !archives.Any()) - return; - - containsItems = true; - foreach (var archive in archives) - { - var item = new Archive - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(archive.Name); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Chip information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertChips(Models.ClrMamePro.Chip[]? chips, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the chip array is missing, we can't do anything - if (chips == null || !chips.Any()) - return; - - containsItems = true; - foreach (var chip in chips) - { - var item = new Chip - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(chip.Name); - item.SetFieldValue(Models.Metadata.Chip.ChipTypeKey, chip.Type?.AsEnumValue() ?? ChipType.NULL); - item.SetFieldValue(Models.Metadata.Chip.ClockKey, NumberHelper.ConvertToInt64(chip.Clock)); - item.SetFieldValue(Models.Metadata.Chip.FlagsKey, chip.Flags); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Video information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertVideos(Models.ClrMamePro.Video[]? videos, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the video array is missing, we can't do anything - if (videos == null || !videos.Any()) - return; - - containsItems = true; - foreach (var video in videos) - { - var item = new Display - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetFieldValue("ASPECTX", NumberHelper.ConvertToInt64(video.AspectX)); - item.SetFieldValue("ASPECTY", NumberHelper.ConvertToInt64(video.AspectY)); - item.SetFieldValue(Models.Metadata.Display.DisplayTypeKey, video.Screen?.AsEnumValue() ?? DisplayType.NULL); - item.SetFieldValue(Models.Metadata.Display.HeightKey, NumberHelper.ConvertToInt64(video.Y)); - item.SetFieldValue(Models.Metadata.Display.RefreshKey, NumberHelper.ConvertToDouble(video.Freq)); - item.SetFieldValue(Models.Metadata.Display.WidthKey, NumberHelper.ConvertToInt64(video.X)); - - switch (video.Orientation) - { - case "horizontal": - item.SetFieldValue(Models.Metadata.Display.RotateKey, 0); - break; - case "vertical": - item.SetFieldValue(Models.Metadata.Display.RotateKey, 90); - break; - } - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Sound information - /// - /// Deserialized model to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertSound(Models.ClrMamePro.Sound? sound, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the sound is missing, we can't do anything - if (sound == null) - return; - - containsItems = true; - var item = new Sound - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetFieldValue(Models.Metadata.Sound.ChannelsKey, NumberHelper.ConvertToInt64(sound.Channels)); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - - /// - /// Convert Input information - /// - /// Deserialized model to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertInput(Models.ClrMamePro.Input? input, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the input is missing, we can't do anything - if (input == null) - return; - - containsItems = true; - var item = new Input - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetFieldValue(Models.Metadata.Input.CoinsKey, NumberHelper.ConvertToInt64(input.Coins)); - //item.SetFieldValue(Models.Metadata.Input.ControlKey, input.Control); - item.SetFieldValue(Models.Metadata.Input.PlayersKey, NumberHelper.ConvertToInt64(input.Players)); - item.SetFieldValue(Models.Metadata.Input.ServiceKey, input.Service?.AsYesNo()); - item.SetFieldValue(Models.Metadata.Input.TiltKey, input.Tilt?.AsYesNo()); - - var control = new Control(); - control.SetFieldValue(Models.Metadata.Control.ButtonsKey, NumberHelper.ConvertToInt64(input.Buttons)); - - item.SetFieldValue(Models.Metadata.Input.ControlKey, [control]); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - - /// - /// Convert DipSwitch information - /// - /// Array of deserialized models to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertDipSwitches(Models.ClrMamePro.DipSwitch[]? dipswitches, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the dipswitch array is missing, we can't do anything - if (dipswitches == null || !dipswitches.Any()) - return; - - containsItems = true; - foreach (var dipswitch in dipswitches) - { - var item = new DipSwitch - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(dipswitch.Name); - - var values = new List(); - foreach (string entry in dipswitch.Entry ?? []) - { - var dipValue = new DipValue(); - dipValue.SetName(dipswitch.Name); - dipValue.SetFieldValue(Models.Metadata.DipValue.DefaultKey, entry == dipswitch.Default); - dipValue.SetFieldValue(Models.Metadata.DipValue.ValueKey, entry); - - values.Add(dipValue); - } - - item.SetFieldValue(Models.Metadata.DipSwitch.DipValueKey, [.. values]); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert Driver information - /// - /// Deserialized model to convert - /// Prefilled machine to use - /// Name of the file to be parsed - /// Index ID for the DAT - /// True to only add item statistics while parsing, false otherwise - /// True if there were any items in the array, false otherwise - private void ConvertDriver(Models.ClrMamePro.Driver? driver, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the driver is missing, we can't do anything - if (driver == null) - return; - - containsItems = true; - var item = new Driver - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetFieldValue(Models.Metadata.Driver.BlitKey, driver.Blit); - item.SetFieldValue(Models.Metadata.Driver.ColorKey, driver.Color?.AsEnumValue() ?? SupportStatus.NULL); - item.SetFieldValue(Models.Metadata.Driver.PaletteSizeKey, NumberHelper.ConvertToInt64(driver.PaletteSize)); - item.SetFieldValue(Models.Metadata.Driver.SoundKey, driver.Sound?.AsEnumValue() ?? SupportStatus.NULL); - item.SetFieldValue(Models.Metadata.Driver.StatusKey, driver.Status?.AsEnumValue() ?? SupportStatus.NULL); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - #endregion } }