diff --git a/SabreTools.DatFiles/Formats/Logiqx.Reader.cs b/SabreTools.DatFiles/Formats/Logiqx.Reader.cs index 1f9df85c..126468f7 100644 --- a/SabreTools.DatFiles/Formats/Logiqx.Reader.cs +++ b/SabreTools.DatFiles/Formats/Logiqx.Reader.cs @@ -1,9 +1,6 @@ using System; -using System.Linq; using SabreTools.Core; using SabreTools.Core.Tools; -using SabreTools.DatItems; -using SabreTools.DatItems.Formats; namespace SabreTools.DatFiles.Formats { @@ -19,15 +16,13 @@ namespace SabreTools.DatFiles.Formats { // Deserialize the input file var metadataFile = new Serialization.Files.Logiqx().Deserialize(filename); + var metadata = new Serialization.CrossModel.Logiqx().Serialize(metadataFile); // Convert the header to the internal format ConvertHeader(metadataFile, keep); - // Convert the game data to the internal format - ConvertGames(metadataFile?.Game, filename, indexId, statsOnly); - - // Convert the dir data to the internal format - ConvertDirs(metadataFile?.Dir, filename, indexId, statsOnly); + // Convert to the internal format + ConvertMetadata(metadata, filename, indexId, statsOnly); } catch (Exception ex) when (!throwOnError) { @@ -133,491 +128,6 @@ namespace SabreTools.DatFiles.Formats Header.LockSampleMode ??= romCenter.LockSampleMode.AsYesNo(); } - /// - /// Convert dirs 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 ConvertDirs(Models.Logiqx.Dir[]? dirs, string filename, int indexId, bool statsOnly) - { - // If the dir array is missing, we can't do anything - if (dirs == null || !dirs.Any()) - return; - - // Loop through the dirs and add - foreach (var dir in dirs) - { - ConvertDir(dir, filename, indexId, statsOnly); - } - } - - /// - /// Convert dir 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 ConvertDir(Models.Logiqx.Dir dir, string filename, int indexId, bool statsOnly) - { - // If the game array is missing, we can't do anything - if (dir.Game == null || !dir.Game.Any()) - return; - - // Loop through the games and add - foreach (var game in dir.Game) - { - ConvertGame(game, filename, indexId, statsOnly, dir.Name); - } - } - - /// - /// 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.Logiqx.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.Logiqx.GameBase game, string filename, int indexId, bool statsOnly, string? dirname = null) - { - // 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.BoardKey, game.Board); - machine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, game.CloneOf); - machine.SetFieldValue(Models.Metadata.Machine.CloneOfIdKey, game.CloneOfId); - machine.SetFieldValue(Models.Metadata.Machine.DescriptionKey, game.Description); - machine.SetFieldValue(Models.Metadata.Machine.IdKey, game.Id); - machine.SetFieldValue(Models.Metadata.Machine.IsBiosKey, game.IsBios.AsYesNo()); - machine.SetFieldValue(Models.Metadata.Machine.IsDeviceKey, game.IsDevice.AsYesNo()); - machine.SetFieldValue(Models.Metadata.Machine.IsMechanicalKey, game.IsMechanical.AsYesNo()); - machine.SetFieldValue(Models.Metadata.Machine.ManufacturerKey, game.Manufacturer); - machine.SetFieldValue(Models.Metadata.Machine.NameKey, game.Name); - machine.SetFieldValue(Models.Metadata.Machine.PublisherKey, game.Publisher); - machine.SetFieldValue(Models.Metadata.Machine.RebuildToKey, game.RebuildTo); - machine.SetFieldValue(Models.Metadata.Machine.RomKey, game.RomOf); - machine.SetFieldValue(Models.Metadata.Machine.RunnableKey, game.Runnable.AsEnumValue()); - machine.SetFieldValue(Models.Metadata.Machine.SampleOfKey, game.SampleOf); - machine.SetFieldValue(Models.Metadata.Machine.SourceFileKey, game.SourceFile); - machine.SetFieldValue(Models.Metadata.Machine.YearKey, game.Year); - - if (!string.IsNullOrEmpty(dirname)) - machine.SetFieldValue(Models.Metadata.Machine.NameKey, $"{dirname}/{machine.GetFieldValue(Models.Metadata.Machine.NameKey)}"); - -#if NETFRAMEWORK - if (game.Comment != null && game.Comment.Any()) - machine.SetFieldValue(Models.Metadata.Machine.CommentKey, string.Join(";", game.Comment)); - if (game.Category != null && game.Category.Any()) - machine.SetFieldValue(Models.Metadata.Machine.CategoryKey, string.Join(";", game.Category)); -#else - if (game.Comment != null && game.Comment.Any()) - machine.SetFieldValue(Models.Metadata.Machine.CommentKey, string.Join(';', game.Comment)); - if (game.Category != null && game.Category.Any()) - machine.SetFieldValue(Models.Metadata.Machine.CategoryKey, string.Join(';', game.Category)); -#endif - - if (game.Trurip != null) - { - var trurip = game.Trurip; - - machine.TitleID = trurip.TitleID; - machine.SetFieldValue(Models.Metadata.Machine.PublisherKey, trurip.Publisher); - machine.Developer = trurip.Developer; - machine.SetFieldValue(Models.Metadata.Machine.YearKey, value: trurip.Year); - machine.Genre = trurip.Genre; - machine.Subgenre = trurip.Subgenre; - machine.Ratings = trurip.Ratings; - machine.Score = trurip.Score; - machine.SetFieldValue(Models.Metadata.Machine.PlayersKey, trurip.Players); - machine.Enabled = trurip.Enabled; - machine.Crc = trurip.CRC.AsYesNo(); - machine.SetFieldValue(Models.Metadata.Machine.SourceFileKey, trurip.Source); - machine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, trurip.CloneOf); - machine.RelatedTo = trurip.RelatedTo; - } - - // 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); - ConvertDeviceRefs(game.DeviceRef, machine, filename, indexId, statsOnly, ref containsItems); - ConvertSamples(game.Sample, machine, filename, indexId, statsOnly, ref containsItems); - ConvertArchives(game.Archive, machine, filename, indexId, statsOnly, ref containsItems); - ConvertDriver(game.Driver, machine, filename, indexId, statsOnly, ref containsItems); - ConvertSoftwareLists(game.SoftwareList, 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.Logiqx.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.Logiqx.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.Logiqx.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.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.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.Logiqx.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.StatusKey, disk.Status?.AsEnumValue() ?? ItemStatus.NULL); - item.SetFieldValue(Models.Metadata.Disk.MD5Key, disk.MD5); - item.SetFieldValue(Models.Metadata.Disk.MergeKey, disk.Merge); - item.SetFieldValue(Models.Metadata.Disk.RegionKey, disk.Region); - item.SetFieldValue(Models.Metadata.Disk.SHA1Key, disk.SHA1); - - 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.Logiqx.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 DeviceRef 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 ConvertDeviceRefs(Models.Logiqx.DeviceRef[]? devicerefs, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the devicerefs array is missing, we can't do anything - if (devicerefs == null || !devicerefs.Any()) - return; - - containsItems = true; - foreach (var deviceref in devicerefs) - { - var item = new DeviceReference - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(deviceref.Name); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - - /// - /// Convert DeviceRef 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 ConvertSamples(Models.Logiqx.Sample[]? samples, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the samples array is missing, we can't do anything - if (samples == null || !samples.Any()) - return; - - containsItems = true; - foreach (var sample in samples) - { - var item = new Sample - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(sample.Name); - - 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.Logiqx.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 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.Logiqx.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.CocktailKey, driver.Cocktail?.AsEnumValue() ?? SupportStatus.NULL); - item.SetFieldValue(Models.Metadata.Driver.EmulationKey, driver.Emulation?.AsEnumValue() ?? SupportStatus.NULL); - item.SetFieldValue(Models.Metadata.Driver.IncompleteKey, driver.Incomplete.AsYesNo()); - item.SetFieldValue(Models.Metadata.Driver.NoSoundHardwareKey, driver.NoSoundHardware.AsYesNo()); - item.SetFieldValue(Models.Metadata.Driver.RequiresArtworkKey, driver.RequiresArtwork.AsYesNo()); - item.SetFieldValue(Models.Metadata.Driver.SaveStateKey, driver.SaveState?.AsEnumValue() ?? Supported.NULL); - item.SetFieldValue(Models.Metadata.Driver.StatusKey, driver.Status?.AsEnumValue() ?? SupportStatus.NULL); - item.SetFieldValue(Models.Metadata.Driver.UnofficialKey, driver.Unofficial.AsYesNo()); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - - /// - /// Convert SoftwareList 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 ConvertSoftwareLists(Models.Logiqx.SoftwareList[]? softwarelists, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) - { - // If the softwarelists array is missing, we can't do anything - if (softwarelists == null || !softwarelists.Any()) - return; - - containsItems = true; - foreach (var softwarelist in softwarelists) - { - var item = new DatItems.Formats.SoftwareList - { - Source = new Source { Index = indexId, Name = filename }, - }; - item.SetName(softwarelist.Name); - item.SetFieldValue(Models.Metadata.SoftwareList.FilterKey, softwarelist.Filter); - item.SetFieldValue(Models.Metadata.SoftwareList.StatusKey, softwarelist.Status.AsEnumValue()); - item.SetFieldValue(Models.Metadata.SoftwareList.TagKey, softwarelist.Tag); - - item.CopyMachineInformation(machine); - ParseAddHelper(item, statsOnly); - } - } - #endregion } }