Files
SabreTools/SabreTools.DatFiles/Formats/Listxml.Writer.cs

965 lines
35 KiB
C#
Raw Normal View History

2023-08-01 10:31:38 -04:00
using System;
using System.Collections.Generic;
2023-08-01 13:30:34 -04:00
using System.Linq;
2023-08-01 10:31:38 -04:00
using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents writing a MAME XML DAT
/// </summary>
internal partial class Listxml : DatFile
{
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
2024-02-28 19:19:50 -05:00
return
[
2023-08-01 10:31:38 -04:00
ItemType.Adjuster,
ItemType.BiosSet,
ItemType.Chip,
ItemType.Condition,
ItemType.Configuration,
ItemType.Device,
ItemType.DeviceReference,
ItemType.DipSwitch,
ItemType.Disk,
ItemType.Display,
ItemType.Driver,
ItemType.Feature,
ItemType.Input,
ItemType.Port,
ItemType.RamOption,
ItemType.Rom,
ItemType.Sample,
ItemType.Slot,
ItemType.SoftwareList,
ItemType.Sound,
2024-02-28 19:19:50 -05:00
];
2023-08-01 10:31:38 -04:00
}
/// <inheritdoc/>
2024-03-05 23:41:00 -05:00
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
2023-08-01 10:31:38 -04:00
{
2024-03-05 23:41:00 -05:00
var missingFields = new List<string>();
2023-08-01 12:03:12 -04:00
switch (datItem)
{
case BiosSet biosset:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(biosset.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.BiosSet.NameKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(biosset.Description))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.BiosSet.DescriptionKey);
2023-08-01 12:03:12 -04:00
break;
case Rom rom:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(rom.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Rom.NameKey);
2023-08-01 12:03:12 -04:00
if (rom.Size == null || rom.Size < 0)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Rom.SizeKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(rom.CRC)
&& string.IsNullOrEmpty(rom.SHA1))
2023-08-01 12:03:12 -04:00
{
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Rom.SHA1Key);
2023-08-01 12:03:12 -04:00
}
break;
case Disk disk:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(disk.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Disk.NameKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(disk.MD5)
&& string.IsNullOrEmpty(disk.SHA1))
2023-08-01 12:03:12 -04:00
{
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Disk.SHA1Key);
2023-08-01 12:03:12 -04:00
}
break;
case DeviceReference deviceref:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(deviceref.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.DeviceRef.NameKey);
2023-08-01 12:03:12 -04:00
break;
case Sample sample:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(sample.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Sample.NameKey);
2023-08-01 12:03:12 -04:00
break;
case Chip chip:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(chip.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Chip.NameKey);
2023-08-01 12:03:12 -04:00
if (!chip.ChipTypeSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Chip.ChipTypeKey);
2023-08-01 12:03:12 -04:00
break;
case Display display:
if (!display.DisplayTypeSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Display.DisplayTypeKey);
2023-08-01 12:03:12 -04:00
if (display.Refresh == null)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Display.RefreshKey);
2023-08-01 12:03:12 -04:00
break;
case Sound sound:
if (sound.Channels == null)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Sound.ChannelsKey);
2023-08-01 12:03:12 -04:00
break;
case Input input:
if (input.Players == null)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Input.PlayersKey);
2023-08-01 12:03:12 -04:00
break;
case DipSwitch dipswitch:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(dipswitch.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.DipSwitch.NameKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(dipswitch.Tag))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.DipSwitch.TagKey);
2023-08-01 12:03:12 -04:00
break;
case Configuration configuration:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(configuration.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Configuration.NameKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(configuration.Tag))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Configuration.TagKey);
2023-08-01 12:03:12 -04:00
break;
case Port port:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(port.Tag))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Port.TagKey);
2023-08-01 12:03:12 -04:00
break;
case Adjuster adjuster:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(adjuster.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Adjuster.NameKey);
2023-08-01 12:03:12 -04:00
break;
case Driver driver:
if (!driver.StatusSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Driver.StatusKey);
2023-08-01 12:03:12 -04:00
if (!driver.EmulationSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Driver.EmulationKey);
2023-08-01 12:03:12 -04:00
if (!driver.CocktailSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Driver.CocktailKey);
2023-08-01 12:03:12 -04:00
if (!driver.SaveStateSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Driver.SaveStateKey);
2023-08-01 12:03:12 -04:00
break;
case Feature feature:
if (!feature.TypeSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Feature.FeatureTypeKey);
2023-08-01 12:03:12 -04:00
break;
case Device device:
if (!device.DeviceTypeSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Device.DeviceTypeKey);
2023-08-01 12:03:12 -04:00
break;
case Slot slot:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(slot.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Slot.NameKey);
2023-08-01 12:03:12 -04:00
break;
case DatItems.Formats.SoftwareList softwarelist:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(softwarelist.Tag))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.SoftwareList.TagKey);
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(softwarelist.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.SoftwareList.NameKey);
2023-08-01 12:03:12 -04:00
if (!softwarelist.StatusSpecified)
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.SoftwareList.StatusKey);
2023-08-01 12:03:12 -04:00
break;
case RamOption ramoption:
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(ramoption.Name))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.RamOption.NameKey);
2023-08-01 12:03:12 -04:00
break;
}
return missingFields;
2023-08-01 10:31:38 -04:00
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
logger.User($"Writing to '{outfile}'...");
2023-08-01 13:30:34 -04:00
var mame = CreateMame(ignoreblanks);
2023-09-11 01:20:21 -04:00
if (!(new Serialization.Files.Listxml().Serialize(mame, outfile)))
2023-08-01 10:31:38 -04:00
{
2023-08-01 13:30:34 -04:00
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
2023-08-01 10:31:38 -04:00
return false;
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
2023-08-01 13:30:34 -04:00
logger.User($"'{outfile}' written!{Environment.NewLine}");
2023-08-01 10:31:38 -04:00
return true;
}
2023-08-01 13:30:34 -04:00
#region Converters
/// <summary>
/// Create a Mame from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.Listxml.Mame CreateMame(bool ignoreblanks)
{
var datafile = new Models.Listxml.Mame
{
Build = Header.Name ?? Header.Description ?? Header.Build,
Debug = Header.Debug.FromYesNo(),
MameConfig = Header.MameConfig,
Game = CreateGames(ignoreblanks)
};
return datafile;
}
/// <summary>
/// Create an array of GameBase from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.Listxml.GameBase[]? 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<Models.Listxml.GameBase>();
// 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;
2024-02-28 19:19:50 -05:00
var game = Listxml.CreateGame(machine!);
2023-08-01 13:30:34 -04:00
// Create holders for all item types
var biosSets = new List<Models.Listxml.BiosSet>();
var roms = new List<Models.Listxml.Rom>();
var disks = new List<Models.Listxml.Disk>();
var deviceRefs = new List<Models.Listxml.DeviceRef>();
var samples = new List<Models.Listxml.Sample>();
var chips = new List<Models.Listxml.Chip>();
var displays = new List<Models.Listxml.Display>();
var dipSwitches = new List<Models.Listxml.DipSwitch>();
var configurations = new List<Models.Listxml.Configuration>();
var ports = new List<Models.Listxml.Port>();
var adjusters = new List<Models.Listxml.Adjuster>();
var features = new List<Models.Listxml.Feature>();
var devices = new List<Models.Listxml.Device>();
var slots = new List<Models.Listxml.Slot>();
var softwareLists = new List<Models.Listxml.SoftwareList>();
var ramOptions = new List<Models.Listxml.RamOption>();
// 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 BiosSet biosset:
biosSets.Add(CreateBiosSet(biosset));
break;
case Rom rom:
roms.Add(CreateRom(rom));
break;
case Disk disk:
disks.Add(CreateDisk(disk));
break;
case DeviceReference deviceref:
deviceRefs.Add(CreateDeviceRef(deviceref));
break;
case Sample sample:
samples.Add(CreateSample(sample));
break;
case Chip chip:
chips.Add(CreateChip(chip));
break;
case Display display:
displays.Add(CreateDisplay(display));
break;
case Sound sound:
game.Sound = CreateSound(sound);
break;
case Input input:
game.Input = CreateInput(input);
break;
case DipSwitch dipswitch:
dipSwitches.Add(CreateDipSwitch(dipswitch));
break;
case Configuration configuration:
configurations.Add(CreateConfiguration(configuration));
break;
case Port port:
ports.Add(CreatePort(port));
break;
case Adjuster adjuster:
adjusters.Add(CreateAdjuster(adjuster));
break;
case Driver driver:
game.Driver = CreateDriver(driver);
break;
case Feature feature:
features.Add(CreateFeature(feature));
break;
case Device device:
devices.Add(CreateDevice(device));
break;
case Slot slot:
slots.Add(CreateSlot(slot));
break;
case DatItems.Formats.SoftwareList softwarelist:
softwareLists.Add(CreateSoftwareList(softwarelist));
break;
case RamOption ramoption:
ramOptions.Add(CreateRamOption(ramoption));
break;
}
}
// Assign the values to the game
2024-02-28 19:19:50 -05:00
game.BiosSet = [.. biosSets];
game.Rom = [.. roms];
game.Disk = [.. disks];
game.DeviceRef = [.. deviceRefs];
game.Sample = [.. samples];
game.Chip = [.. chips];
game.Display = [.. displays];
2023-08-01 13:30:34 -04:00
game.Video = null;
2024-02-28 19:19:50 -05:00
game.DipSwitch = [.. dipSwitches];
game.Configuration = [.. configurations];
game.Port = [.. ports];
game.Adjuster = [.. adjusters];
game.Feature = [.. features];
game.Device = [.. devices];
game.Slot = [.. slots];
game.SoftwareList = [.. softwareLists];
game.RamOption = [.. ramOptions];
2023-08-01 13:30:34 -04:00
// Add the game to the list
games.Add(game);
}
2024-02-28 19:19:50 -05:00
return [.. games];
2023-08-01 13:30:34 -04:00
}
/// <summary>
/// Create a GameBase from the current internal information
/// <summary>
2024-02-28 19:19:50 -05:00
private static Models.Listxml.GameBase CreateGame(Machine machine)
2023-08-01 13:30:34 -04:00
{
var game = new Models.Listxml.Machine
{
Name = machine.Name,
SourceFile = machine.SourceFile,
2024-03-05 15:24:11 -05:00
Runnable = machine.Runnable.AsStringValue<Runnable>(),
2023-08-01 13:30:34 -04:00
CloneOf = machine.CloneOf,
RomOf = machine.RomOf,
SampleOf = machine.SampleOf,
Description = machine.Description,
Year = machine.Year,
Manufacturer = machine.Manufacturer,
History = machine.History,
};
2024-02-28 22:54:56 -05:00
#if NETFRAMEWORK
if ((machine.MachineType & MachineType.Bios) != 0)
game.IsBios = "yes";
if ((machine.MachineType & MachineType.Device) != 0)
game.IsDevice = "yes";
if ((machine.MachineType & MachineType.Mechanical) != 0)
game.IsMechanical = "yes";
#else
2023-08-01 13:30:34 -04:00
if (machine.MachineType.HasFlag(MachineType.Bios))
game.IsBios = "yes";
if (machine.MachineType.HasFlag(MachineType.Device))
game.IsDevice = "yes";
if (machine.MachineType.HasFlag(MachineType.Mechanical))
game.IsMechanical = "yes";
2024-02-28 22:54:56 -05:00
#endif
2023-08-01 13:30:34 -04:00
return game;
}
/// <summary>
/// Create a BiosSet from the current BiosSet DatItem
/// <summary>
private static Models.Listxml.BiosSet CreateBiosSet(BiosSet item)
{
var biosset = new Models.Listxml.BiosSet
{
Name = item.Name,
Description = item.Description,
};
if (item.DefaultSpecified)
biosset.Default = item.Default.FromYesNo();
return biosset;
}
/// <summary>
/// Create a Rom from the current Rom DatItem
/// <summary>
private static Models.Listxml.Rom CreateRom(Rom item)
{
var rom = new Models.Listxml.Rom
{
Name = item.Name,
Bios = item.Bios,
Size = item.Size?.ToString(),
CRC = item.CRC,
SHA1 = item.SHA1,
Merge = item.MergeTag,
Region = item.Region,
Offset = item.Offset,
2024-03-05 15:24:11 -05:00
Status = item.ItemStatus.AsStringValue<ItemStatus>(useSecond: false),
2023-08-01 13:30:34 -04:00
Optional = item.Optional.FromYesNo(),
//Dispose = item.Dispose.FromYesNo(), // TODO: Add to internal model
//SoundOnly = item.SoundOnly.FromYesNo(), // TODO: Add to internal model
};
return rom;
}
/// <summary>
/// Create a Disk from the current Disk DatItem
/// <summary>
private static Models.Listxml.Disk CreateDisk(Disk item)
{
var disk = new Models.Listxml.Disk
{
Name = item.Name,
MD5 = item.MD5,
SHA1 = item.SHA1,
Merge = item.MergeTag,
Region = item.Region,
Index = item.Index,
Writable = item.Writable.FromYesNo(),
2024-03-05 15:24:11 -05:00
Status = item.ItemStatus.AsStringValue<ItemStatus>(useSecond: false),
2023-08-01 13:30:34 -04:00
Optional = item.Optional.FromYesNo(),
};
return disk;
}
/// <summary>
/// Create a DeviceRef from the current DeviceReference DatItem
/// <summary>
private static Models.Listxml.DeviceRef CreateDeviceRef(DeviceReference item)
{
var deviceref = new Models.Listxml.DeviceRef
{
Name = item.Name,
};
return deviceref;
}
/// <summary>
/// Create a Sample from the current Sample DatItem
/// <summary>
private static Models.Listxml.Sample CreateSample(Sample item)
{
var sample = new Models.Listxml.Sample
{
Name = item.Name,
};
return sample;
}
/// <summary>
/// Create a Chip from the current Chip DatItem
/// <summary>
private static Models.Listxml.Chip CreateChip(Chip item)
{
var chip = new Models.Listxml.Chip
{
Name = item.Name,
Tag = item.Tag,
2024-03-05 15:24:11 -05:00
Type = item.ChipType.AsStringValue<ChipType>(),
2023-08-01 13:30:34 -04:00
//SoundOnly = item.SoundOnly, // TODO: Add to internal model
Clock = item.Clock?.ToString(),
};
return chip;
}
/// <summary>
/// Create a Display from the current Display DatItem
/// <summary>
private static Models.Listxml.Display CreateDisplay(Display item)
{
var display = new Models.Listxml.Display
{
Tag = item.Tag,
2024-03-05 15:24:11 -05:00
Type = item.DisplayType.AsStringValue<DisplayType>(),
2023-08-01 13:30:34 -04:00
Rotate = item.Rotate?.ToString(),
FlipX = item.FlipX.FromYesNo(),
Width = item.Width?.ToString(),
Height = item.Height?.ToString(),
Refresh = item.Refresh?.ToString(),
PixClock = item.PixClock?.ToString(),
HTotal = item.HTotal?.ToString(),
HBEnd = item.HBEnd?.ToString(),
HBStart = item.HBStart?.ToString(),
VTotal = item.VTotal?.ToString(),
VBEnd = item.VBEnd?.ToString(),
VBStart = item.VBStart?.ToString(),
};
return display;
}
/// <summary>
/// Create a Sound from the current Sound DatItem
/// <summary>
private static Models.Listxml.Sound CreateSound(Sound item)
{
var sound = new Models.Listxml.Sound
{
Channels = item.Channels?.ToString(),
};
return sound;
}
/// <summary>
/// Create an Input from the current Input DatItem
/// <summary>
private static Models.Listxml.Input CreateInput(Input item)
{
var input = new Models.Listxml.Input
{
Service = item.Service.FromYesNo(),
Tilt = item.Tilt.FromYesNo(),
Players = item.Players?.ToString(),
//ControlAttr = item.ControlAttr, // TODO: Add to internal model
//Buttons = item.Buttons, // TODO: Add to internal model
Coins = item.Coins?.ToString(),
};
var controls = new List<Models.Listxml.Control>();
2024-02-28 19:19:50 -05:00
foreach (var controlItem in item.Controls ?? [])
2023-08-01 13:30:34 -04:00
{
var control = CreateControl(controlItem);
controls.Add(control);
}
if (controls.Any())
2024-02-28 19:19:50 -05:00
input.Control = [.. controls];
2023-08-01 13:30:34 -04:00
return input;
}
/// <summary>
/// Create an Control from the current Input DatItem
/// <summary>
private static Models.Listxml.Control CreateControl(Control item)
{
var control = new Models.Listxml.Control
{
2024-03-05 15:24:11 -05:00
Type = item.ControlType.AsStringValue<ControlType>(),
2023-08-01 13:30:34 -04:00
Player = item.Player?.ToString(),
Buttons = item.Buttons?.ToString(),
ReqButtons = item.RequiredButtons?.ToString(),
Minimum = item.Minimum?.ToString(),
Maximum = item.Maximum?.ToString(),
Sensitivity = item.Sensitivity?.ToString(),
KeyDelta = item.KeyDelta?.ToString(),
Reverse = item.Reverse.FromYesNo(),
Ways = item.Ways,
Ways2 = item.Ways2,
Ways3 = item.Ways3,
};
return control;
}
/// <summary>
/// Create an DipSwitch from the current DipSwitch DatItem
/// <summary>
private static Models.Listxml.DipSwitch CreateDipSwitch(DipSwitch item)
{
var dipswitch = new Models.Listxml.DipSwitch
{
Name = item.Name,
Tag = item.Tag,
Mask = item.Mask,
};
if (item.ConditionsSpecified)
{
var conditionItem = item.Conditions?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var condition = new Models.Listxml.Condition
{
Tag = conditionItem?.Tag,
Mask = conditionItem?.Mask,
2024-03-05 15:24:11 -05:00
Relation = conditionItem?.Relation.AsStringValue<Relation>(),
Value = conditionItem?.Value,
2023-08-01 13:30:34 -04:00
};
dipswitch.Condition = condition;
}
var diplocations = new List<Models.Listxml.DipLocation>();
2024-02-28 19:19:50 -05:00
foreach (var locationItem in item.Locations ?? [])
2023-08-01 13:30:34 -04:00
{
var control = CreateDipLocation(locationItem);
diplocations.Add(control);
}
if (diplocations.Any())
2024-02-28 19:19:50 -05:00
dipswitch.DipLocation = [.. diplocations];
2023-08-01 13:30:34 -04:00
var dipvalues = new List<Models.Listxml.DipValue>();
2024-02-28 19:19:50 -05:00
foreach (var dipValueItem in item.Values ?? [])
2023-08-01 13:30:34 -04:00
{
var dipvalue = CreateDipValue(dipValueItem);
2023-08-01 13:30:34 -04:00
dipvalues.Add(dipvalue);
}
if (dipvalues.Any())
2024-02-28 19:19:50 -05:00
dipswitch.DipValue = [.. dipvalues];
2023-08-01 13:30:34 -04:00
return dipswitch;
}
/// <summary>
/// Create a DipLocation from the current DipLocation DatItem
2023-08-01 13:30:34 -04:00
/// <summary>
private static Models.Listxml.DipLocation CreateDipLocation(DipLocation item)
2023-08-01 13:30:34 -04:00
{
var diplocation = new Models.Listxml.DipLocation
{
Name = item.Name,
Number = item.Number?.ToString(),
Inverted = item.Inverted.FromYesNo(),
};
return diplocation;
}
/// <summary>
/// Create a DipValue from the current DipValue DatItem
2023-08-01 13:30:34 -04:00
/// <summary>
private static Models.Listxml.DipValue CreateDipValue(DipValue item)
2023-08-01 13:30:34 -04:00
{
var dipvalue = new Models.Listxml.DipValue
{
Name = item.Name,
Value = item.Value,
Default = item.Default.FromYesNo(),
};
if (item.ConditionsSpecified)
{
var conditionItem = item.Conditions?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var condition = new Models.Listxml.Condition
{
Tag = conditionItem?.Tag,
Mask = conditionItem?.Mask,
2024-03-05 15:24:11 -05:00
Relation = conditionItem?.Relation.AsStringValue<Relation>(),
Value = conditionItem?.Value,
2023-08-01 13:30:34 -04:00
};
dipvalue.Condition = condition;
}
return dipvalue;
}
/// <summary>
/// Create an Configuration from the current Configuration DatItem
/// <summary>
private static Models.Listxml.Configuration CreateConfiguration(Configuration item)
{
var configuration = new Models.Listxml.Configuration
{
Name = item.Name,
Tag = item.Tag,
Mask = item.Mask,
};
if (item.ConditionsSpecified)
{
var conditionItem = item.Conditions?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var condition = new Models.Listxml.Condition
{
Tag = conditionItem?.Tag,
Mask = conditionItem?.Mask,
2024-03-05 15:24:11 -05:00
Relation = conditionItem?.Relation.AsStringValue<Relation>(),
Value = conditionItem?.Value,
2023-08-01 13:30:34 -04:00
};
configuration.Condition = condition;
}
var confLocations = new List<Models.Listxml.ConfLocation>();
2024-02-28 19:19:50 -05:00
foreach (var location in item.Locations ?? [])
2023-08-01 13:30:34 -04:00
{
var control = CreateConfLocation(location);
confLocations.Add(control);
}
if (confLocations.Any())
2024-02-28 19:19:50 -05:00
configuration.ConfLocation = [.. confLocations];
2023-08-01 13:30:34 -04:00
var confsettings = new List<Models.Listxml.ConfSetting>();
2024-02-28 19:19:50 -05:00
foreach (var confSettingItem in item.Settings ?? [])
2023-08-01 13:30:34 -04:00
{
var dipvalue = CreateConfSetting(confSettingItem);
2023-08-01 13:30:34 -04:00
confsettings.Add(dipvalue);
}
if (confsettings.Any())
2024-02-28 19:19:50 -05:00
configuration.ConfSetting = [.. confsettings];
2023-08-01 13:30:34 -04:00
return configuration;
}
/// <summary>
/// Create a ConfLocation from the current ConfLocation DatItem
2023-08-01 13:30:34 -04:00
/// <summary>
private static Models.Listxml.ConfLocation CreateConfLocation(ConfLocation item)
2023-08-01 13:30:34 -04:00
{
var conflocation = new Models.Listxml.ConfLocation
{
Name = item.Name,
Number = item.Number?.ToString(),
Inverted = item.Inverted.FromYesNo(),
};
return conflocation;
}
/// <summary>
/// Create a ConfSetting from the current ConfSetting DatItem
2023-08-01 13:30:34 -04:00
/// <summary>
private static Models.Listxml.ConfSetting CreateConfSetting(ConfSetting item)
2023-08-01 13:30:34 -04:00
{
var confsetting = new Models.Listxml.ConfSetting
{
Name = item.Name,
Value = item.Value,
Default = item.Default.FromYesNo(),
};
if (item.ConditionsSpecified)
{
var conditionItem = item.Conditions?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var condition = new Models.Listxml.Condition
{
Tag = conditionItem?.Tag,
Mask = conditionItem?.Mask,
2024-03-05 15:24:11 -05:00
Relation = conditionItem?.Relation.AsStringValue<Relation>(),
Value = conditionItem?.Value,
2023-08-01 13:30:34 -04:00
};
confsetting.Condition = condition;
}
return confsetting;
}
/// <summary>
/// Create a Port from the current Port DatItem
/// <summary>
private static Models.Listxml.Port CreatePort(Port item)
{
var port = new Models.Listxml.Port
{
Tag = item.Tag,
};
return port;
}
/// <summary>
/// Create a Adjuster from the current Adjuster DatItem
/// <summary>
private static Models.Listxml.Adjuster CreateAdjuster(Adjuster item)
{
var adjuster = new Models.Listxml.Adjuster
{
Name = item.Name,
Default = item.Default.FromYesNo(),
};
if (item.ConditionsSpecified)
{
var conditionItem = item.Conditions?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var condition = new Models.Listxml.Condition
{
Tag = conditionItem?.Tag,
Mask = conditionItem?.Mask,
2024-03-05 15:24:11 -05:00
Relation = conditionItem?.Relation.AsStringValue<Relation>(),
Value = conditionItem?.Value,
2023-08-01 13:30:34 -04:00
};
adjuster.Condition = condition;
}
return adjuster;
}
/// <summary>
/// Create a Driver from the current Driver DatItem
/// <summary>
private static Models.Listxml.Driver CreateDriver(Driver item)
{
var driver = new Models.Listxml.Driver
{
2024-03-05 15:24:11 -05:00
Status = item.Status.AsStringValue<SupportStatus>(),
//Color = item.Color.AsStringValue<SupportStatus>(), // TODO: Add to internal model
//Sound = item.Sound.AsStringValue<SupportStatus>(), // TODO: Add to internal model
2023-08-01 13:30:34 -04:00
//PaletteSize = driver.PaletteSize?.ToString(), // TODO: Add to internal model
2024-03-05 15:24:11 -05:00
Emulation = item.Emulation.AsStringValue<SupportStatus>(),
Cocktail = item.Cocktail.AsStringValue<SupportStatus>(),
SaveState = item.SaveState.AsStringValue<Supported>(useSecond: true),
2023-08-01 13:30:34 -04:00
RequiresArtwork = item.RequiresArtwork.FromYesNo(),
Unofficial = item.Unofficial.FromYesNo(),
NoSoundHardware = item.NoSoundHardware.FromYesNo(),
Incomplete = item.Incomplete.FromYesNo(),
};
return driver;
}
/// <summary>
/// Create a Feature from the current Feature DatItem
/// <summary>
private static Models.Listxml.Feature CreateFeature(Feature item)
{
var feature = new Models.Listxml.Feature
{
2024-03-05 15:24:11 -05:00
Type = item.Type.AsStringValue<FeatureType>(),
Status = item.Status.AsStringValue<FeatureStatus>(),
Overall = item.Overall.AsStringValue<FeatureStatus>(),
2023-08-01 13:30:34 -04:00
};
return feature;
}
/// <summary>
/// Create a Device from the current Device DatItem
/// <summary>
private static Models.Listxml.Device CreateDevice(Device item)
{
var device = new Models.Listxml.Device
{
2024-03-05 15:24:11 -05:00
Type = item.DeviceType.AsStringValue<DeviceType>(),
2023-08-01 13:30:34 -04:00
Tag = item.Tag,
FixedImage = item.FixedImage,
Mandatory = item.Mandatory?.ToString(),
Interface = item.Interface,
};
if (item.InstancesSpecified)
{
var instanceItem = item.Instances?.FirstOrDefault();
2023-08-01 13:30:34 -04:00
var instance = new Models.Listxml.Instance
{
Name = instanceItem?.Name,
BriefName = instanceItem?.BriefName,
2023-08-01 13:30:34 -04:00
};
device.Instance = instance;
}
var extensions = new List<Models.Listxml.Extension>();
2024-02-28 19:19:50 -05:00
foreach (var extensionItem in item.Extensions ?? [])
2023-08-01 13:30:34 -04:00
{
var extension = new Models.Listxml.Extension
{
Name = extensionItem.Name,
};
extensions.Add(extension);
}
if (extensions.Any())
2024-02-28 19:19:50 -05:00
device.Extension = [.. extensions];
2023-08-01 13:30:34 -04:00
return device;
}
/// <summary>
/// Create a Slot from the current Slot DatItem
/// <summary>
private static Models.Listxml.Slot CreateSlot(Slot item)
{
var slot = new Models.Listxml.Slot
{
Name = item.Name,
};
var slotoptions = new List<Models.Listxml.SlotOption>();
2024-02-28 19:19:50 -05:00
foreach (var slotoptionItem in item.SlotOptions ?? [])
2023-08-01 13:30:34 -04:00
{
var slotoption = new Models.Listxml.SlotOption
{
Name = slotoptionItem.Name,
DevName = slotoptionItem.DeviceName,
Default = slotoptionItem.Default.FromYesNo(),
};
slotoptions.Add(slotoption);
}
if (slotoptions.Any())
2024-02-28 19:19:50 -05:00
slot.SlotOption = [.. slotoptions];
2023-08-01 13:30:34 -04:00
return slot;
}
/// <summary>
/// Create a SoftwareList from the current SoftwareList DatItem
/// <summary>
private static Models.Listxml.SoftwareList CreateSoftwareList(DatItems.Formats.SoftwareList item)
{
var softwarelist = new Models.Listxml.SoftwareList
{
Tag = item.Tag,
Name = item.Name,
2024-03-05 15:24:11 -05:00
Status = item.Status.AsStringValue<SoftwareListStatus>(),
2023-08-01 13:30:34 -04:00
Filter = item.Filter,
};
return softwarelist;
}
/// <summary>
/// Create a RamOption from the current RamOption DatItem
/// <summary>
private static Models.Listxml.RamOption CreateRamOption(RamOption item)
{
var softwarelist = new Models.Listxml.RamOption
{
Name = item.Name,
Default = item.Default.FromYesNo(),
Content = item.Content,
2023-08-01 13:30:34 -04:00
};
return softwarelist;
}
#endregion
2023-08-01 10:31:38 -04:00
}
}