Files
SabreTools/SabreTools.Library/DatFiles/Listxml.cs

1278 lines
50 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2020-08-23 21:10:29 -07:00
using System.Linq;
using System.Text;
using System.Xml;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
2020-08-01 23:04:11 -07:00
using SabreTools.Library.IO;
using SabreTools.Library.Tools;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a MAME XML DAT
/// </summary>
internal class Listxml : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public Listxml(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse a MAME XML DAT and return all found games and roms within
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <remarks>
/// </remarks>
protected override void ParseFile(
2019-01-11 13:43:15 -08:00
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
// Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader();
2019-01-11 13:43:15 -08:00
// If we got a null reader, just return
if (xtr == null)
return;
// Otherwise, read the file to the end
try
{
xtr.MoveToContent();
while (!xtr.EOF)
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
{
xtr.Read();
continue;
}
switch (xtr.Name)
{
case "mame":
Header.Name = (Header.Name == null ? xtr.GetAttribute("build") : Header.Name);
Header.Description = (Header.Description == null ? Header.Name : Header.Description);
2020-08-20 15:13:57 -07:00
Header.Debug = (Header.Debug == null ? xtr.GetAttribute("debug").AsYesNo() : Header.Debug);
Header.MameConfig = (Header.MameConfig == null ? xtr.GetAttribute("mameconfig") : Header.MameConfig);
2019-01-11 13:43:15 -08:00
xtr.Read();
break;
2019-01-11 13:43:15 -08:00
// Handle M1 DATs since they're 99% the same as a SL DAT
case "m1":
Header.Name = (Header.Name == null ? "M1" : Header.Name);
Header.Description = (Header.Description == null ? "M1" : Header.Description);
Header.Version = (Header.Version == null ? xtr.GetAttribute("version") ?? string.Empty : Header.Version);
2019-01-11 13:43:15 -08:00
xtr.Read();
break;
2019-01-11 13:43:15 -08:00
// We want to process the entire subtree of the machine
case "game": // Some older DATs still use "game"
case "machine":
ReadMachine(xtr.ReadSubtree(), filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the machine now that we've processed it
xtr.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
xtr.Read();
break;
}
}
}
catch (Exception ex)
{
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}");
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read machine information
/// </summary>
/// <param name="reader">XmlReader representing a machine block</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
private void ReadMachine(
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
2019-01-11 13:43:15 -08:00
{
// If we have an empty machine, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
// Create a new machine
MachineType machineType = MachineType.NULL;
if (reader.GetAttribute("isbios").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Bios;
if (reader.GetAttribute("isdevice").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Device;
if (reader.GetAttribute("ismechanical").AsYesNo() == true)
2019-01-11 13:43:15 -08:00
machineType |= MachineType.Mechanical;
Machine machine = new Machine
{
Name = reader.GetAttribute("name"),
Comment = string.Empty,
2020-08-23 21:10:29 -07:00
Description = reader.GetAttribute("name"),
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
RomOf = reader.GetAttribute("romof") ?? string.Empty,
SampleOf = reader.GetAttribute("sampleof") ?? string.Empty,
2019-01-11 13:43:15 -08:00
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
2020-08-23 21:10:29 -07:00
SourceFile = reader.GetAttribute("sourcefile"),
Runnable = reader.GetAttribute("runnable").AsRunnable(),
DeviceReferences = new List<ListXmlDeviceReference>(),
Chips = new List<ListXmlChip>(),
Displays = new List<ListXmlDisplay>(),
Sounds = new List<ListXmlSound>(),
Conditions = new List<ListXmlCondition>(),
Inputs = new List<ListXmlInput>(),
DipSwitches = new List<ListXmlDipSwitch>(),
Configurations = new List<ListXmlConfiguration>(),
Slots = new List<ListXmlSlot>(),
2019-01-11 13:43:15 -08:00
};
2020-08-23 21:10:29 -07:00
// Get list for new DatItems
List<DatItem> datItems = new List<DatItem>();
2019-01-11 13:43:15 -08:00
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the roms from the machine
switch (reader.Name)
{
case "description":
machine.Description = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "year":
machine.Year = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "manufacturer":
machine.Manufacturer = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "biosset":
2020-08-23 21:10:29 -07:00
datItems.Add(new BiosSet
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("description"),
Default = reader.GetAttribute("default").AsYesNo(),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-08-23 21:10:29 -07:00
});
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "rom":
2020-08-23 21:10:29 -07:00
datItems.Add(new Rom
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
Bios = reader.GetAttribute("bios"),
Size = Sanitizer.CleanSize(reader.GetAttribute("size")),
CRC = reader.GetAttribute("crc"),
MD5 = reader.GetAttribute("md5"),
#if NET_FRAMEWORK
RIPEMD160 = reader.GetAttribute("ripemd160"),
#endif
SHA1 = reader.GetAttribute("sha1"),
SHA256 = reader.GetAttribute("sha256"),
SHA384 = reader.GetAttribute("sha384"),
SHA512 = reader.GetAttribute("sha512"),
2019-01-11 13:43:15 -08:00
MergeTag = reader.GetAttribute("merge"),
Region = reader.GetAttribute("region"),
Offset = reader.GetAttribute("offset"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
Optional = reader.GetAttribute("optional").AsYesNo(),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-08-23 21:10:29 -07:00
});
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "disk":
2020-08-23 21:10:29 -07:00
datItems.Add(new Disk
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
MD5 = reader.GetAttribute("md5"),
#if NET_FRAMEWORK
RIPEMD160 = reader.GetAttribute("ripemd160"),
#endif
SHA1 = reader.GetAttribute("sha1"),
SHA256 = reader.GetAttribute("sha256"),
SHA384 = reader.GetAttribute("sha384"),
SHA512 = reader.GetAttribute("sha512"),
2019-01-11 13:43:15 -08:00
MergeTag = reader.GetAttribute("merge"),
Region = reader.GetAttribute("region"),
Index = reader.GetAttribute("index"),
Writable = reader.GetAttribute("writable").AsYesNo(),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
Optional = reader.GetAttribute("optional").AsYesNo(),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-08-23 21:10:29 -07:00
});
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "device_ref":
var deviceReference = new ListXmlDeviceReference();
deviceReference.Name = reader.GetAttribute("name");
2020-08-23 15:42:58 -07:00
machine.DeviceReferences.Add(deviceReference);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "sample":
2020-08-23 21:10:29 -07:00
datItems.Add(new Sample
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-08-23 21:10:29 -07:00
});
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "chip":
var chip = new ListXmlChip();
2020-08-22 23:40:00 -07:00
chip.Name = reader.GetAttribute("name");
chip.Tag = reader.GetAttribute("tag");
chip.Type = reader.GetAttribute("type");
chip.Clock = reader.GetAttribute("clock");
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Chips.Add(chip);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "display":
var display = new ListXmlDisplay();
2020-08-22 23:40:00 -07:00
display.Tag = reader.GetAttribute("tag");
display.Type = reader.GetAttribute("type");
display.Rotate = reader.GetAttribute("rotate");
display.FlipX = reader.GetAttribute("flipx").AsYesNo();
display.Width = reader.GetAttribute("width");
display.Height = reader.GetAttribute("height");
display.Refresh = reader.GetAttribute("refresh");
display.PixClock = reader.GetAttribute("pixclock");
display.HTotal = reader.GetAttribute("htotal");
display.HBend = reader.GetAttribute("hbend");
display.HStart = reader.GetAttribute("hstart");
display.VTotal = reader.GetAttribute("vtotal");
display.VBend = reader.GetAttribute("vbend");
display.VStart = reader.GetAttribute("vstart");
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Displays.Add(display);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "sound":
var sound = new ListXmlSound();
2020-08-22 23:40:00 -07:00
sound.Channels = reader.GetAttribute("channels");
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Sounds.Add(sound);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "condition":
var condition = new ListXmlCondition();
2020-08-22 23:40:00 -07:00
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
condition.Relation = reader.GetAttribute("relation");
condition.Value = reader.GetAttribute("value");
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Conditions.Add(condition);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "input":
var input = new ListXmlInput();
2020-08-22 23:40:00 -07:00
input.Service = reader.GetAttribute("service").AsYesNo();
input.Tilt = reader.GetAttribute("tilt").AsYesNo();
input.Players = reader.GetAttribute("players");
input.Coins = reader.GetAttribute("coins");
2019-01-11 13:43:15 -08:00
2020-08-22 23:40:00 -07:00
// Now read the internal tags
ReadInput(reader.ReadSubtree(), input);
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Inputs.Add(input);
2020-08-22 23:40:00 -07:00
// Skip the input now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "dipswitch":
var dipSwitch = new ListXmlDipSwitch();
2020-08-22 23:40:00 -07:00
dipSwitch.Name = reader.GetAttribute("name");
dipSwitch.Tag = reader.GetAttribute("tag");
dipSwitch.Mask = reader.GetAttribute("mask");
// Now read the internal tags
2020-08-22 13:31:13 -07:00
ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.DipSwitches.Add(dipSwitch);
2020-08-22 13:31:13 -07:00
// Skip the dipswitch now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "configuration":
var configuration = new ListXmlConfiguration();
configuration.Name = reader.GetAttribute("name");
configuration.Tag = reader.GetAttribute("tag");
configuration.Mask = reader.GetAttribute("mask");
// Now read the internal tags
ReadConfiguration(reader.ReadSubtree(), configuration);
2019-01-11 13:43:15 -08:00
2020-08-23 21:10:29 -07:00
machine.Configurations.Add(configuration);
// Skip the configuration now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "port":
// TODO: Use these ports
var port = new ListXmlPort();
port.Tag = reader.GetAttribute("tag");
2019-01-11 13:43:15 -08:00
// Now read the internal tags
ReadPort(reader.ReadSubtree(), port);
2019-01-11 13:43:15 -08:00
// Skip the port now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "adjuster":
// TODO: Use these adjusters
var adjuster = new ListXmlAdjuster();
adjuster.Name = reader.GetAttribute("name");
adjuster.Default = reader.GetAttribute("default").AsYesNo();
2019-01-11 13:43:15 -08:00
// Now read the internal tags
ReadAdjuster(reader.ReadSubtree(), adjuster);
2019-01-11 13:43:15 -08:00
// Skip the adjuster now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2020-08-22 13:05:58 -07:00
2019-01-11 13:43:15 -08:00
case "driver":
// TODO: Use these drivers
var driver = new ListXmlDriver();
driver.Status = reader.GetAttribute("status");
driver.Emulation = reader.GetAttribute("emulation");
driver.Cocktail = reader.GetAttribute("cocktail");
driver.SaveState = reader.GetAttribute("savestate");
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "feature":
// TODO: Use these features
var feature = new ListXmlFeature();
feature.Type = reader.GetAttribute("type");
feature.Status = reader.GetAttribute("status");
feature.Overall = reader.GetAttribute("overall");
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2020-08-23 21:10:29 -07:00
2019-01-11 13:43:15 -08:00
case "device":
// TODO: Use these devices
var device = new ListXmlDevice();
device.Type = reader.GetAttribute("type");
device.Tag = reader.GetAttribute("tag");
device.FixedImage = reader.GetAttribute("fixed_image");
device.Mandatory = reader.GetAttribute("mandatory");
device.Interface = reader.GetAttribute("interface");
2019-01-11 13:43:15 -08:00
// Now read the internal tags
ReadDevice(reader.ReadSubtree(), device);
// Skip the device now that we've processed it
2019-01-11 13:43:15 -08:00
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "slot":
var slot = new ListXmlSlot();
slot.Name = reader.GetAttribute("name");
// Now read the internal tags
ReadSlot(reader.ReadSubtree(), slot, machine);
2020-08-23 21:10:29 -07:00
machine.Slots.Add(slot);
2019-01-11 13:43:15 -08:00
// Skip the slot now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "softwarelist":
// TODO: Use these softwarelists
var softwareList = new ListXmlSoftwareList();
softwareList.Name = reader.GetAttribute("name");
softwareList.Status = reader.GetAttribute("status");
softwareList.Filter = reader.GetAttribute("filter");
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "ramoption":
// TODO: Use these ramoptions
var ramOption = new ListXmlRamOption();
ramOption.Default = reader.GetAttribute("default").AsYesNo();
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
2020-08-23 21:10:29 -07:00
// If we found items, copy the machine info and add them
if (datItems.Any())
{
foreach (DatItem datItem in datItems)
{
datItem.CopyMachineInformation(machine);
ParseAddHelper(datItem);
}
}
2019-01-11 13:43:15 -08:00
// If no items were found for this machine, add a Blank placeholder
2020-08-23 21:10:29 -07:00
else
2019-01-11 13:43:15 -08:00
{
Blank blank = new Blank()
{
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
2020-08-20 13:17:14 -07:00
2019-01-11 13:43:15 -08:00
blank.CopyMachineInformation(machine);
// Now process and add the rom
ParseAddHelper(blank);
2019-01-11 13:43:15 -08:00
}
}
/// <summary>
/// Read slot information
/// </summary>
/// <param name="reader">XmlReader representing a machine block</param>
/// <param name="slot">ListXmlSlot to populate</param>
2019-01-11 13:43:15 -08:00
/// <param name="machine">Machine information to pass to contained items</param>
private void ReadSlot(XmlReader reader, ListXmlSlot slot, Machine machine)
2019-01-11 13:43:15 -08:00
{
// If we have an empty machine, skip it
if (reader == null)
return;
// Get list ready
slot.SlotOptions = new List<ListXmlSlotOption>();
2019-01-11 13:43:15 -08:00
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the roms from the machine
switch (reader.Name)
{
case "slotoption":
var slotOption = new ListXmlSlotOption();
slotOption.Name = reader.GetAttribute("name");
slotOption.DeviceName = reader.GetAttribute("devname");
slotOption.Default = reader.GetAttribute("default").AsYesNo();
slot.SlotOptions.Add(slotOption);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
}
2020-08-22 13:31:13 -07:00
/// <summary>
2020-08-22 23:40:00 -07:00
/// Read Input information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="input">ListXmlInput to populate</param>
private void ReadInput(XmlReader reader, ListXmlInput input)
2020-08-22 23:40:00 -07:00
{
// If we have an empty input, skip it
2020-08-22 23:40:00 -07:00
if (reader == null)
return;
// Get list ready
input.Controls = new List<ListXmlControl>();
2020-08-22 23:40:00 -07:00
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "control":
var control = new ListXmlControl();
2020-08-22 23:40:00 -07:00
control.Type = reader.GetAttribute("type");
control.Player = reader.GetAttribute("player");
control.Buttons = reader.GetAttribute("buttons");
control.RegButtons = reader.GetAttribute("regbuttons");
control.Minimum = reader.GetAttribute("minimum");
control.Maximum = reader.GetAttribute("maximum");
control.Sensitivity = reader.GetAttribute("sensitivity");
control.KeyDelta = reader.GetAttribute("keydelta");
control.Reverse = reader.GetAttribute("reverse").AsYesNo();
control.Ways = reader.GetAttribute("ways");
control.Ways2 = reader.GetAttribute("ways2");
control.Ways3 = reader.GetAttribute("ways3");
input.Controls.Add(control);
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read DipSwitch information
2020-08-22 13:31:13 -07:00
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="dipSwitch">ListXmlDipSwitch to populate</param>
private void ReadDipSwitch(XmlReader reader, ListXmlDipSwitch dipSwitch)
2020-08-22 13:31:13 -07:00
{
// If we have an empty dipswitch, skip it
2020-08-22 13:31:13 -07:00
if (reader == null)
return;
// Get lists ready
dipSwitch.Locations = new List<ListXmlDipLocation>();
dipSwitch.Values = new List<ListXmlDipValue>();
2020-08-22 13:31:13 -07:00
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "diplocation":
var dipLocation = new ListXmlDipLocation();
2020-08-22 23:40:00 -07:00
dipLocation.Name = reader.GetAttribute("name");
dipLocation.Number = reader.GetAttribute("number");
dipLocation.Inverted = reader.GetAttribute("inverted").AsYesNo();
dipSwitch.Locations.Add(dipLocation);
2020-08-22 13:31:13 -07:00
reader.Read();
break;
case "dipvalue":
var dipValue = new ListXmlDipValue();
2020-08-22 23:40:00 -07:00
dipValue.Name = reader.GetAttribute("name");
dipValue.Value = reader.GetAttribute("value");
dipValue.Default = reader.GetAttribute("default").AsYesNo();
dipSwitch.Values.Add(dipValue);
2020-08-22 13:31:13 -07:00
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read Configuration information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="configuration">ListXmlConfiguration to populate</param>
private void ReadConfiguration(XmlReader reader, ListXmlConfiguration configuration)
{
// If we have an empty configuration, skip it
if (reader == null)
return;
// Get lists ready
configuration.Locations = new List<ListXmlConfLocation>();
configuration.Settings = new List<ListXmlConfSetting>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "conflocation":
var confLocation = new ListXmlConfLocation();
confLocation.Name = reader.GetAttribute("name");
confLocation.Number = reader.GetAttribute("number");
confLocation.Inverted = reader.GetAttribute("inverted").AsYesNo();
configuration.Locations.Add(confLocation);
reader.Read();
break;
case "confsetting":
var confSetting = new ListXmlConfSetting();
confSetting.Name = reader.GetAttribute("name");
confSetting.Value = reader.GetAttribute("value");
confSetting.Default = reader.GetAttribute("default").AsYesNo();
configuration.Settings.Add(confSetting);
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read Port information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="port">ListXmlPort to populate</param>
private void ReadPort(XmlReader reader, ListXmlPort port)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get list ready
port.Analogs = new List<ListXmlAnalog>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the port
switch (reader.Name)
{
case "analog":
var analog = new ListXmlAnalog();
analog.Mask = reader.GetAttribute("mask");
port.Analogs.Add(analog);
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read Adjuster information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="adjuster">ListXmlAdjuster to populate</param>
private void ReadAdjuster(XmlReader reader, ListXmlAdjuster adjuster)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get list ready
adjuster.Conditions = new List<ListXmlCondition>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the adjuster
switch (reader.Name)
{
case "condition":
var condition = new ListXmlCondition();
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
condition.Relation = reader.GetAttribute("relation");
condition.Value = reader.GetAttribute("value");
adjuster.Conditions.Add(condition);
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read Device information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="device">ListXmlDevice to populate</param>
private void ReadDevice(XmlReader reader, ListXmlDevice device)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get lists ready
device.Instances = new List<ListXmlInstance>();
device.Extensions = new List<ListXmlExtension>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the adjuster
switch (reader.Name)
{
case "instance":
var instance = new ListXmlInstance();
instance.Name = reader.GetAttribute("name");
instance.BriefName = reader.GetAttribute("briefname");
device.Instances.Add(instance);
reader.Read();
break;
case "extension":
var extension = new ListXmlExtension();
extension.Name = reader.GetAttribute("name");
device.Extensions.Add(extension);
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
2019-01-11 13:43:15 -08:00
/// <summary>
/// Create and open an output file for writing direct from a dictionary
/// </summary>
/// <param name="outfile">Name of the file to write to</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
2019-01-11 13:43:15 -08:00
return false;
}
XmlTextWriter xtw = new XmlTextWriter(fs, new UTF8Encoding(false))
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1
};
2019-01-11 13:43:15 -08:00
// Write out the header
WriteHeader(xtw);
2019-01-11 13:43:15 -08:00
// Write out each of the machines and roms
string lastgame = null;
2020-07-26 21:00:30 -07:00
// Use a sorted list of games to output
2020-07-26 22:34:45 -07:00
foreach (string key in Items.SortedKeys)
2019-01-11 13:43:15 -08:00
{
2020-07-26 22:34:45 -07:00
List<DatItem> roms = Items[key];
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
roms = DatItem.ResolveNames(roms);
for (int index = 0; index < roms.Count; index++)
{
DatItem rom = roms[index];
// There are apparently times when a null rom can skip by, skip them
2020-08-20 13:17:14 -07:00
if (rom.Name == null || rom.Machine.Name == null)
2019-01-11 13:43:15 -08:00
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item
2020-08-20 13:17:14 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw);
2019-01-11 13:43:15 -08:00
// If we have a new game, output the beginning of the new item
2020-08-20 13:17:14 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom);
2019-01-11 13:43:15 -08:00
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
2020-08-20 13:17:14 -07:00
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
lastgame = rom.Machine.Name;
2019-01-11 13:43:15 -08:00
continue;
}
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-20 13:17:14 -07:00
lastgame = rom.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
// Write the file footer out
WriteFooter(xtw);
2019-01-11 13:43:15 -08:00
Globals.Logger.Verbose("File written!" + Environment.NewLine);
xtw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
xtw.WriteStartDocument();
xtw.WriteStartElement("mame");
xtw.WriteAttributeString("build", Header.Name);
2020-08-20 15:13:57 -07:00
if (Header.Debug != null)
{
switch (Header.Debug)
{
case true:
xtw.WriteAttributeString("debug", "yes");
break;
case false:
xtw.WriteAttributeString("debug", "no");
break;
}
}
if (!string.IsNullOrEmpty(Header.MameConfig))
xtw.WriteAttributeString("mameconfig", Header.MameConfig);
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
try
{
// No game should start with a path separator
2020-08-20 13:17:14 -07:00
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2020-08-23 22:23:55 -07:00
// Build the state
xtw.WriteStartElement("machine");
2020-08-23 22:23:55 -07:00
xtw.WriteAttributeString("name", datItem.Machine.Name);
if (!string.IsNullOrWhiteSpace(datItem.Machine.SourceFile))
2020-08-20 13:17:14 -07:00
xtw.WriteAttributeString("sourcefile", datItem.Machine.SourceFile);
2020-08-23 22:23:55 -07:00
if (datItem.Machine.MachineType != MachineType.NULL)
2019-01-11 13:43:15 -08:00
{
2020-08-20 13:17:14 -07:00
if (datItem.Machine.MachineType.HasFlag(MachineType.Bios))
xtw.WriteAttributeString("isbios", "yes");
2020-08-20 13:17:14 -07:00
if (datItem.Machine.MachineType.HasFlag(MachineType.Device))
xtw.WriteAttributeString("isdevice", "yes");
2020-08-20 13:17:14 -07:00
if (datItem.Machine.MachineType.HasFlag(MachineType.Mechanical))
xtw.WriteAttributeString("ismechanical", "yes");
2019-01-11 13:43:15 -08:00
}
2020-08-23 22:23:55 -07:00
if (datItem.Machine.Runnable != Runnable.NULL)
2019-01-11 13:43:15 -08:00
{
switch (datItem.Machine.Runnable)
{
case Runnable.No:
xtw.WriteAttributeString("runnable", "no");
break;
case Runnable.Partial:
xtw.WriteAttributeString("runnable", "partial");
break;
case Runnable.Yes:
xtw.WriteAttributeString("runnable", "yes");
break;
}
}
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.CloneOf) && !string.Equals(datItem.Machine.Name, datItem.Machine.CloneOf, StringComparison.OrdinalIgnoreCase))
2020-08-20 13:17:14 -07:00
xtw.WriteAttributeString("cloneof", datItem.Machine.CloneOf);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.RomOf) && !string.Equals(datItem.Machine.Name, datItem.Machine.RomOf, StringComparison.OrdinalIgnoreCase))
2020-08-20 13:17:14 -07:00
xtw.WriteAttributeString("romof", datItem.Machine.RomOf);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.SampleOf) && !string.Equals(datItem.Machine.Name, datItem.Machine.SampleOf, StringComparison.OrdinalIgnoreCase))
2020-08-20 13:17:14 -07:00
xtw.WriteAttributeString("sampleof", datItem.Machine.SampleOf);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.Description))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("description", datItem.Machine.Description);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.Year))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("year", datItem.Machine.Year);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.Publisher))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("publisher", datItem.Machine.Publisher);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(datItem.Machine.Category))
2020-08-20 13:17:14 -07:00
xtw.WriteElementString("category", datItem.Machine.Category);
2020-08-23 22:23:55 -07:00
if (datItem.Machine.Infos != null && datItem.Machine.Infos.Count > 0)
{
2020-08-21 15:31:19 -07:00
foreach (ListXmlInfo kvp in datItem.Machine.Infos)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartElement("info");
2020-08-21 15:31:19 -07:00
xtw.WriteAttributeString("name", kvp.Name);
2020-06-14 23:07:31 -07:00
xtw.WriteAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
}
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
// End machine
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false)
2019-01-11 13:43:15 -08:00
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
2019-01-11 13:43:15 -08:00
return true;
try
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
// Build the state
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.BiosSet:
var biosSet = datItem as BiosSet;
xtw.WriteStartElement("biosset");
2020-08-23 22:23:55 -07:00
xtw.WriteAttributeString("name", biosSet.Name);
if (!string.IsNullOrWhiteSpace(biosSet.Description))
xtw.WriteAttributeString("description", biosSet.Description);
2020-08-23 22:23:55 -07:00
if (biosSet.Default != null)
xtw.WriteAttributeString("default", biosSet.Default.ToString().ToLowerInvariant());
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Disk:
var disk = datItem as Disk;
xtw.WriteStartElement("disk");
2020-08-23 22:23:55 -07:00
xtw.WriteAttributeString("name", disk.Name);
if (!string.IsNullOrWhiteSpace(disk.MD5))
xtw.WriteAttributeString("md5", disk.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.RIPEMD160))
xtw.WriteAttributeString("ripemd160", disk.RIPEMD160.ToLowerInvariant());
#endif
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.SHA1))
xtw.WriteAttributeString("sha1", disk.SHA1.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.SHA256))
xtw.WriteAttributeString("sha256", disk.SHA256.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.SHA384))
xtw.WriteAttributeString("sha384", disk.SHA384.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.SHA512))
xtw.WriteAttributeString("sha512", disk.SHA512.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.MergeTag))
xtw.WriteAttributeString("merge", disk.MergeTag);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.Region))
xtw.WriteAttributeString("region", disk.Region);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(disk.Index))
xtw.WriteAttributeString("index", disk.Index);
2020-08-23 22:23:55 -07:00
if (disk.Writable != null)
xtw.WriteAttributeString("writable", disk.Writable == true ? "yes" : "no");
2020-08-23 22:23:55 -07:00
if (disk.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", disk.ItemStatus.ToString());
2020-08-23 22:23:55 -07:00
if (disk.Optional != null)
xtw.WriteAttributeString("optional", disk.Optional == true ? "yes" : "no");
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Rom:
var rom = datItem as Rom;
xtw.WriteStartElement("rom");
2020-08-23 22:23:55 -07:00
xtw.WriteAttributeString("name", rom.Name);
if (rom.Size != -1)
xtw.WriteAttributeString("size", rom.Size.ToString());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.CRC))
xtw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.MD5))
xtw.WriteAttributeString("md5", rom.MD5.ToLowerInvariant());
#if NET_FRAMEWORK
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.RIPEMD160))
xtw.WriteAttributeString("ripemd160", rom.RIPEMD160.ToLowerInvariant());
#endif
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.SHA1))
xtw.WriteAttributeString("sha1", rom.SHA1.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.SHA256))
xtw.WriteAttributeString("sha256", rom.SHA256.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.SHA384))
xtw.WriteAttributeString("sha384", rom.SHA384.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.SHA512))
xtw.WriteAttributeString("sha512", rom.SHA512.ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.Bios))
xtw.WriteAttributeString("bios", rom.Bios);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.MergeTag))
xtw.WriteAttributeString("merge", rom.MergeTag);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.Region))
xtw.WriteAttributeString("region", rom.Region);
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.Offset))
xtw.WriteAttributeString("offset", rom.Offset);
2020-08-23 22:23:55 -07:00
if (rom.ItemStatus != ItemStatus.None)
xtw.WriteAttributeString("status", rom.ItemStatus.ToString().ToLowerInvariant());
2020-08-23 22:23:55 -07:00
if (rom.Optional != null)
xtw.WriteAttributeString("optional", rom.Optional == true ? "yes" : "no");
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Sample:
xtw.WriteStartElement("sample");
2020-08-23 22:23:55 -07:00
xtw.WriteAttributeString("name", datItem.Name);
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
try
{
// End machine
xtw.WriteEndElement();
// End mame
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}