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 { /// /// Represents parsing a MAME XML DAT /// internal partial class Listxml : DatFile { /// public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false) { try { // Deserialize the input file // TODO: Support M1 DATs again var mame = new Serialization.Files.Listxml().Deserialize(filename); // Convert the header to the internal format ConvertHeader(mame, keep); // Convert the game data to the internal format ConvertGames(mame?.Game, filename, indexId, statsOnly); } catch (Exception ex) when (!throwOnError) { string message = $"'{filename}' - An error occurred during parsing"; logger.Error(ex, message); } } #region Converters /// /// Convert header information /// /// Deserialized model to convert /// True if full pathnames are to be kept, false otherwise (default) private void ConvertHeader(Models.Listxml.Mame? mame, bool keep) { // If the mame is missing, we can't do anything if (mame == null) return; Header.Name ??= mame.Build; Header.Description ??= mame.Build; Header.Build ??= mame.Build; Header.Debug ??= mame.Debug.AsYesNo(); Header.MameConfig ??= mame.MameConfig; // Handle implied SuperDAT if (Header.Name?.Contains(" - SuperDAT") == true && keep) 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.Listxml.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.Listxml.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.CloneOfKey, game.CloneOf); machine.SetFieldValue(Models.Metadata.Machine.DescriptionKey, game.Description); machine.SetFieldValue(Models.Metadata.Machine.HistoryKey, game.History); 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.RunnableKey, game.Runnable.AsEnumValue()); machine.SetFieldValue(Models.Metadata.Machine.RomOfKey, game.RomOf); machine.SetFieldValue(Models.Metadata.Machine.SampleOfKey, game.SampleOf); machine.SetFieldValue(Models.Metadata.Machine.SourceFileKey, game.SourceFile); machine.SetFieldValue(Models.Metadata.Machine.YearKey, game.Year); // Check if there are any items bool containsItems = false; // Loop through each type of item 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); ConvertDeviceRefs(game.DeviceRef, machine, filename, indexId, statsOnly, ref containsItems); ConvertSamples(game.Sample, machine, filename, indexId, statsOnly, ref containsItems); ConvertChips(game.Chip, machine, filename, indexId, statsOnly, ref containsItems); ConvertDisplays(game.Display, 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); ConvertConfigurations(game.Configuration, machine, filename, indexId, statsOnly, ref containsItems); ConvertPorts(game.Port, machine, filename, indexId, statsOnly, ref containsItems); ConvertAdjusters(game.Adjuster, machine, filename, indexId, statsOnly, ref containsItems); ConvertDriver(game.Driver, machine, filename, indexId, statsOnly, ref containsItems); ConvertFeatures(game.Feature, machine, filename, indexId, statsOnly, ref containsItems); ConvertDevices(game.Device, machine, filename, indexId, statsOnly, ref containsItems); ConvertSlots(game.Slot, machine, filename, indexId, statsOnly, ref containsItems); ConvertSoftwareLists(game.SoftwareList, machine, filename, indexId, statsOnly, ref containsItems); ConvertRamOptions(game.RamOption, 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 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.Listxml.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.Listxml.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.SizeKey, NumberHelper.ConvertToInt64(rom.Size)); item.SetFieldValue(Models.Metadata.Rom.BiosKey, rom.Bios); item.SetFieldValue(Models.Metadata.Rom.CRCKey, rom.CRC); item.SetFieldValue(Models.Metadata.Rom.DisposeKey, rom.Dispose.AsYesNo()); item.SetFieldValue(Models.Metadata.Rom.MergeKey, rom.Merge); item.SetFieldValue(Models.Metadata.Rom.OffsetKey, rom.Offset); item.SetFieldValue(Models.Metadata.Rom.OptionalKey, rom.Optional.AsYesNo()); item.SetFieldValue(Models.Metadata.Rom.RegionKey, rom.Region); item.SetFieldValue(Models.Metadata.Rom.SHA1Key, rom.SHA1); item.SetFieldValue(Models.Metadata.Rom.SoundOnlyKey, rom.SoundOnly.AsYesNo()); item.SetFieldValue(Models.Metadata.Rom.StatusKey, rom.Status.AsEnumValue()); 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.Listxml.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.IndexKey, disk.Index); 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.OptionalKey, disk.Optional.AsYesNo()); item.SetFieldValue(Models.Metadata.Disk.RegionKey, disk.Region); item.SetFieldValue(Models.Metadata.Disk.SHA1Key, disk.SHA1); item.SetFieldValue(Models.Metadata.Disk.WritableKey, disk.Writable.AsYesNo()); 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.Listxml.DeviceRef[]? devicerefs, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the DeviceRef 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 Sample 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.Listxml.Sample[]? samples, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Sample 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 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.Listxml.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()); item.SetFieldValue(Models.Metadata.Chip.ClockKey, NumberHelper.ConvertToInt64(chip.Clock)); item.SetFieldValue(Models.Metadata.Chip.SoundOnlyKey, chip.Type.AsYesNo()); item.SetFieldValue(Models.Metadata.Chip.TagKey, chip.Tag); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Display 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 ConvertDisplays(Models.Listxml.Display[]? displays, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Display array is missing, we can't do anything if (displays == null || !displays.Any()) return; containsItems = true; foreach (var display in displays) { var item = new Display { Source = new Source { Index = indexId, Name = filename }, }; item.SetFieldValue(Models.Metadata.Display.DisplayTypeKey, display.Type.AsEnumValue()); item.SetFieldValue(Models.Metadata.Display.FlipXKey, display.FlipX.AsYesNo()); item.SetFieldValue(Models.Metadata.Display.HBEndKey, NumberHelper.ConvertToInt64(display.HBEnd)); item.SetFieldValue(Models.Metadata.Display.HBStartKey, NumberHelper.ConvertToInt64(display.HBStart)); item.SetFieldValue(Models.Metadata.Display.HeightKey, NumberHelper.ConvertToInt64(display.Height)); item.SetFieldValue(Models.Metadata.Display.HTotalKey, NumberHelper.ConvertToInt64(display.HTotal)); item.SetFieldValue(Models.Metadata.Display.PixClockKey, NumberHelper.ConvertToInt64(display.PixClock)); item.SetFieldValue(Models.Metadata.Display.RefreshKey, NumberHelper.ConvertToDouble(display.Refresh)); item.SetFieldValue(Models.Metadata.Display.RotateKey, NumberHelper.ConvertToInt64(display.Rotate)); item.SetFieldValue(Models.Metadata.Display.TagKey, display.Tag); item.SetFieldValue(Models.Metadata.Display.VBEndKey, NumberHelper.ConvertToInt64(display.VBEnd)); item.SetFieldValue(Models.Metadata.Display.VBStartKey, NumberHelper.ConvertToInt64(display.VBStart)); item.SetFieldValue(Models.Metadata.Display.VTotalKey, NumberHelper.ConvertToInt64(display.VTotal)); item.SetFieldValue(Models.Metadata.Display.WidthKey, NumberHelper.ConvertToInt64(display.Width)); 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.Listxml.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.Height)); item.SetFieldValue(Models.Metadata.Display.RefreshKey, NumberHelper.ConvertToDouble(video.Refresh)); item.SetFieldValue(Models.Metadata.Display.WidthKey, NumberHelper.ConvertToInt64(video.Width)); 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.Listxml.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.Listxml.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.ButtonsKey, NumberHelper.ConvertToInt64(input.Buttons)); item.SetFieldValue(Models.Metadata.Input.CoinsKey, NumberHelper.ConvertToInt64(input.Coins)); //item.SetFieldValue(Models.Metadata.Input.ControlKey, input.ControlAttr); 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 controls = new List(); foreach (var control in input.Control ?? []) { var controlItem = new Control(); controlItem.SetFieldValue(Models.Metadata.Control.ButtonsKey, NumberHelper.ConvertToInt64(control.Buttons)); controlItem.SetFieldValue(Models.Metadata.Control.ControlTypeKey, control.Type.AsEnumValue()); controlItem.SetFieldValue(Models.Metadata.Control.KeyDeltaKey, NumberHelper.ConvertToInt64(control.KeyDelta)); controlItem.SetFieldValue(Models.Metadata.Control.MaximumKey, NumberHelper.ConvertToInt64(control.Maximum)); controlItem.SetFieldValue(Models.Metadata.Control.MinimumKey, NumberHelper.ConvertToInt64(control.Minimum)); controlItem.SetFieldValue(Models.Metadata.Control.PlayerKey, NumberHelper.ConvertToInt64(control.Player)); controlItem.SetFieldValue(Models.Metadata.Control.ReqButtonsKey, NumberHelper.ConvertToInt64(control.ReqButtons)); controlItem.SetFieldValue(Models.Metadata.Control.ReverseKey, control.Reverse.AsYesNo()); controlItem.SetFieldValue(Models.Metadata.Control.SensitivityKey, NumberHelper.ConvertToInt64(control.Sensitivity)); controlItem.SetFieldValue(Models.Metadata.Control.WaysKey, control.Ways); controlItem.SetFieldValue(Models.Metadata.Control.Ways2Key, control.Ways2); controlItem.SetFieldValue(Models.Metadata.Control.Ways3Key, control.Ways3); controls.Add(controlItem); } if (controls.Any()) item.SetFieldValue(Models.Metadata.Input.ControlKey, [.. controls]); 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.Listxml.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); item.SetFieldValue(Models.Metadata.DipSwitch.MaskKey, dipswitch.Mask); item.SetFieldValue(Models.Metadata.DipSwitch.TagKey, dipswitch.Tag); if (dipswitch.Condition != null) { var condition = new Condition(); condition.SetFieldValue(Models.Metadata.Condition.MaskKey, dipswitch.Condition.Mask); condition.SetFieldValue(Models.Metadata.Condition.RelationKey, dipswitch.Condition.Relation.AsEnumValue()); condition.SetFieldValue(Models.Metadata.Condition.TagKey, dipswitch.Condition.Tag); condition.SetFieldValue(Models.Metadata.Condition.ValueKey, dipswitch.Condition.Value); item.SetFieldValue(Models.Metadata.DipSwitch.ConditionKey, [condition]); } var locations = new List(); foreach (var diplocation in dipswitch.DipLocation ?? []) { var locationItem = new DipLocation(); locationItem.SetName(diplocation.Name); locationItem.SetFieldValue(Models.Metadata.DipLocation.InvertedKey, diplocation.Inverted.AsYesNo()); locationItem.SetFieldValue(Models.Metadata.DipLocation.NumberKey, NumberHelper.ConvertToInt64(diplocation.Number)); locations.Add(locationItem); } if (locations.Any()) item.SetFieldValue(Models.Metadata.DipSwitch.DipLocationKey, [.. locations]); var settings = new List(); foreach (var dipvalue in dipswitch.DipValue ?? []) { var dipValueItem = new DipValue(); dipValueItem.SetName(dipvalue.Name); dipValueItem.SetFieldValue(Models.Metadata.DipValue.DefaultKey, dipvalue.Default.AsYesNo()); dipValueItem.SetFieldValue(Models.Metadata.DipValue.ValueKey, dipvalue.Value); if (dipvalue.Condition != null) { var condition = new Condition(); condition.SetFieldValue(Models.Metadata.Condition.MaskKey, dipvalue.Condition.Mask); condition.SetFieldValue(Models.Metadata.Condition.RelationKey, dipvalue.Condition.Relation.AsEnumValue()); condition.SetFieldValue(Models.Metadata.Condition.TagKey, dipvalue.Condition.Tag); condition.SetFieldValue(Models.Metadata.Condition.ValueKey, dipvalue.Condition.Value); dipValueItem.SetFieldValue(Models.Metadata.DipValue.ConditionKey, [condition]); } settings.Add(dipValueItem); } if (settings.Any()) item.SetFieldValue(Models.Metadata.DipSwitch.DipValueKey, [.. settings]); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Configuration 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 ConvertConfigurations(Models.Listxml.Configuration[]? configurations, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Configuration array is missing, we can't do anything if (configurations == null || !configurations.Any()) return; containsItems = true; foreach (var configuration in configurations) { var item = new Configuration { Source = new Source { Index = indexId, Name = filename }, }; item.SetName(configuration.Name); item.SetFieldValue(Models.Metadata.Configuration.MaskKey, configuration.Mask); item.SetFieldValue(Models.Metadata.Configuration.TagKey, configuration.Tag); if (configuration.Condition != null) { var condition = new DatItems.Formats.Condition(); condition.SetFieldValue(Models.Metadata.Condition.MaskKey, configuration.Condition.Mask); condition.SetFieldValue(Models.Metadata.Condition.RelationKey, configuration.Condition.Relation.AsEnumValue()); condition.SetFieldValue(Models.Metadata.Condition.TagKey, configuration.Condition.Tag); condition.SetFieldValue(Models.Metadata.Condition.ValueKey, configuration.Condition.Value); item.SetFieldValue(Models.Metadata.Configuration.ConditionKey, [condition]); } var locations = new List(); foreach (var confLocation in configuration.ConfLocation ?? []) { var locationItem = new ConfLocation(); locationItem.SetName(confLocation.Name); locationItem.SetFieldValue(Models.Metadata.ConfLocation.InvertedKey, confLocation.Inverted.AsYesNo()); locationItem.SetFieldValue(Models.Metadata.ConfLocation.NumberKey, NumberHelper.ConvertToInt64(confLocation.Number)); locations.Add(locationItem); } if (locations.Any()) item.SetFieldValue(Models.Metadata.Configuration.ConfLocationKey, [.. locations]); var settings = new List(); foreach (var dipvalue in configuration.ConfSetting ?? []) { var settingItem = new ConfSetting(); settingItem.SetName(dipvalue.Name); settingItem.SetFieldValue(Models.Metadata.ConfSetting.DefaultKey, dipvalue.Default.AsYesNo()); settingItem.SetFieldValue(Models.Metadata.ConfSetting.ValueKey, dipvalue.Value); if (dipvalue.Condition != null) { var condition = new Condition(); condition.SetFieldValue(Models.Metadata.Condition.MaskKey, dipvalue.Condition.Mask); condition.SetFieldValue(Models.Metadata.Condition.RelationKey, dipvalue.Condition.Relation.AsEnumValue()); condition.SetFieldValue(Models.Metadata.Condition.TagKey, dipvalue.Condition.Tag); condition.SetFieldValue(Models.Metadata.Condition.ValueKey, dipvalue.Condition.Value); settingItem.SetFieldValue(Models.Metadata.ConfSetting.ConditionKey, [condition]); } settings.Add(settingItem); } if (settings.Any()) item.SetFieldValue(Models.Metadata.Configuration.ConfSettingKey, [.. settings]); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Port 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 ConvertPorts(Models.Listxml.Port[]? ports, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Port array is missing, we can't do anything if (ports == null || !ports.Any()) return; containsItems = true; foreach (var port in ports) { var item = new Port { Source = new Source { Index = indexId, Name = filename }, }; item.SetFieldValue(Models.Metadata.Port.TagKey, port.Tag); var analogs = new List(); foreach (var analog in port.Analog ?? []) { var analogItem = new Analog(); analogItem.SetFieldValue(Models.Metadata.Analog.MaskKey, analog.Mask); analogs.Add(analogItem); } if (analogs.Any()) item.SetFieldValue(Models.Metadata.Port.AnalogKey, [.. analogs]); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Adjuster 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 ConvertAdjusters(Models.Listxml.Adjuster[]? adjusters, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Adjuster array is missing, we can't do anything if (adjusters == null || !adjusters.Any()) return; containsItems = true; foreach (var adjuster in adjusters) { var item = new Adjuster { Source = new Source { Index = indexId, Name = filename } }; item.SetName(adjuster.Name); item.SetFieldValue(Models.Metadata.Adjuster.DefaultKey, adjuster.Default.AsYesNo()); if (adjuster.Condition != null) { var condition = new Condition(); condition.SetFieldValue(Models.Metadata.Condition.MaskKey, adjuster.Condition.Mask); condition.SetFieldValue(Models.Metadata.Condition.RelationKey, adjuster.Condition.Relation.AsEnumValue()); condition.SetFieldValue(Models.Metadata.Condition.TagKey, adjuster.Condition.Tag); condition.SetFieldValue(Models.Metadata.Condition.ValueKey, adjuster.Condition.Value); item.SetFieldValue(Models.Metadata.Adjuster.ConditionKey, [condition]); } 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.Listxml.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.ColorKey, driver.Color?.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.PaletteSizeKey, NumberHelper.ConvertToInt64(driver.PaletteSize)); 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.SoundKey, driver.Sound?.AsEnumValue() ?? SupportStatus.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 Feature 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 ConvertFeatures(Models.Listxml.Feature[]? features, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Feature array is missing, we can't do anything if (features == null || !features.Any()) return; containsItems = true; foreach (var feature in features) { var item = new Feature { Source = new Source { Index = indexId, Name = filename }, }; item.SetFieldValue(Models.Metadata.Feature.FeatureTypeKey, feature.Type.AsEnumValue()); item.SetFieldValue(Models.Metadata.Feature.OverallKey, feature.Overall.AsEnumValue()); item.SetFieldValue(Models.Metadata.Feature.StatusKey, feature.Status.AsEnumValue()); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Device 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 ConvertDevices(Models.Listxml.Device[]? devices, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Device array is missing, we can't do anything if (devices == null || !devices.Any()) return; containsItems = true; foreach (var device in devices) { var item = new Device { Source = new Source { Index = indexId, Name = filename }, }; item.SetFieldValue(Models.Metadata.Device.DeviceTypeKey, device.Type.AsEnumValue()); item.SetFieldValue(Models.Metadata.Device.FixedImageKey, device.FixedImage); item.SetFieldValue(Models.Metadata.Device.InterfaceKey, device.Interface); item.SetFieldValue(Models.Metadata.Device.MandatoryKey, NumberHelper.ConvertToInt64(device.Mandatory)); item.SetFieldValue(Models.Metadata.Device.TagKey, device.Tag); if (device.Instance != null) { var instance = new Instance(); instance.SetName(device.Instance.Name); instance.SetFieldValue(Models.Metadata.Instance.BriefNameKey, device.Instance.BriefName); item.SetFieldValue(Models.Metadata.Device.InstanceKey, [instance]); } var extensions = new List(); foreach (var extension in device.Extension ?? []) { var extensionItem = new Extension(); extensionItem.SetName(extension.Name); extensions.Add(extensionItem); } if (extensions.Any()) item.SetFieldValue(Models.Metadata.Device.ExtensionKey, extensions.ToArray()); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } /// /// Convert Slot 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 ConvertSlots(Models.Listxml.Slot[]? slots, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the Slot array is missing, we can't do anything if (slots == null || !slots.Any()) return; containsItems = true; foreach (var slot in slots) { var item = new Slot { Source = new Source { Index = indexId, Name = filename }, }; item.SetName(slot.Name); var slotoptions = new List(); foreach (var slotoption in slot.SlotOption ?? []) { var slotoptionItem = new SlotOption(); slotoptionItem.SetName(slotoption.Name); slotoptionItem.SetFieldValue(Models.Metadata.SlotOption.DefaultKey, slotoption.Default.AsYesNo()); slotoptionItem.SetFieldValue(Models.Metadata.SlotOption.DevNameKey, slotoption.DevName); slotoptions.Add(slotoptionItem); } if (slotoptions.Any()) item.SetFieldValue(Models.Metadata.Slot.SlotOptionKey, [.. slotoptions]); 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.Listxml.SoftwareList[]? softwarelists, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the SoftwareList 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); } } /// /// Convert RamOption 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 ConvertRamOptions(Models.Listxml.RamOption[]? ramoptions, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems) { // If the RamOption array is missing, we can't do anything if (ramoptions == null || !ramoptions.Any()) return; containsItems = true; foreach (var ramoption in ramoptions) { var item = new RamOption { Source = new Source { Index = indexId, Name = filename }, }; item.SetName(ramoption.Name); item.SetFieldValue(Models.Metadata.RamOption.ContentKey, ramoption.Content); item.SetFieldValue(Models.Metadata.RamOption.DefaultKey, ramoption.Default.AsYesNo()); item.CopyMachineInformation(machine); ParseAddHelper(item, statsOnly); } } #endregion } }