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

1714 lines
71 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>
2020-08-28 15:06:07 -07:00
protected override void ParseFile(string filename, int indexId, 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>
2020-08-28 15:06:07 -07:00
private void ReadMachine(XmlReader reader, 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 = 0x0;
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"),
2020-08-23 21:10:29 -07:00
Description = reader.GetAttribute("name"),
2020-08-24 13:43:37 -07:00
CloneOf = reader.GetAttribute("cloneof"),
RomOf = reader.GetAttribute("romof"),
SampleOf = reader.GetAttribute("sampleof"),
MachineType = (machineType == 0x0 ? MachineType.NULL : machineType),
2020-08-23 21:10:29 -07:00
SourceFile = reader.GetAttribute("sourcefile"),
Runnable = reader.GetAttribute("runnable").AsRunnable(),
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;
2020-09-01 11:34:52 -07:00
case "adjuster":
var adjuster = new Adjuster
{
Name = reader.GetAttribute("name"),
Default = reader.GetAttribute("default").AsYesNo(),
2020-09-01 16:21:55 -07:00
Conditions = new List<Condition>(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 11:34:52 -07:00
};
// Now read the internal tags
ReadAdjuster(reader.ReadSubtree(), adjuster);
datItems.Add(adjuster);
// Skip the adjuster now that we've processed it
reader.Skip();
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;
2020-09-01 11:34:52 -07:00
case "chip":
2020-09-04 11:20:54 -07:00
var chip = new Chip
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
2020-09-01 11:34:52 -07:00
Tag = reader.GetAttribute("tag"),
ChipType = reader.GetAttribute("type").AsChipType(),
2020-09-04 23:03:27 -07:00
Clock = Sanitizer.CleanLong(reader.GetAttribute("clock")),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-04 11:20:54 -07:00
};
datItems.Add(chip);
2019-01-11 13:43:15 -08:00
reader.Read();
break;
case "condition":
datItems.Add(new Condition
{
Tag = reader.GetAttribute("tag"),
Mask = reader.GetAttribute("mask"),
2020-09-03 21:59:53 -07:00
Relation = reader.GetAttribute("relation").AsRelation(),
Value = reader.GetAttribute("value"),
Source = new Source
{
Index = indexId,
Name = filename,
},
});
reader.Read();
break;
2020-09-01 11:55:11 -07:00
case "configuration":
var configuration = new Configuration
{
Name = reader.GetAttribute("name"),
Tag = reader.GetAttribute("tag"),
Mask = reader.GetAttribute("mask"),
2020-09-01 16:21:55 -07:00
Conditions = new List<Condition>(),
Locations = new List<Location>(),
Settings = new List<Setting>(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 11:55:11 -07:00
};
// Now read the internal tags
ReadConfiguration(reader.ReadSubtree(), configuration);
datItems.Add(configuration);
// Skip the configuration now that we've processed it
reader.Skip();
break;
2020-09-02 17:09:19 -07:00
case "device":
var device = new Device
{
2020-09-07 00:39:59 -07:00
DeviceType = reader.GetAttribute("type").AsDeviceType(),
2020-09-02 17:09:19 -07:00
Tag = reader.GetAttribute("tag"),
FixedImage = reader.GetAttribute("fixed_image"),
Mandatory = reader.GetAttribute("mandatory"),
Interface = reader.GetAttribute("interface"),
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now read the internal tags
ReadDevice(reader.ReadSubtree(), device);
datItems.Add(device);
// Skip the device now that we've processed it
reader.Skip();
break;
2020-09-01 11:34:52 -07:00
case "device_ref":
datItems.Add(new DeviceReference
{
Name = reader.GetAttribute("name"),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 11:34:52 -07:00
});
reader.Read();
break;
2020-09-01 13:36:32 -07:00
case "dipswitch":
var dipSwitch = new DipSwitch
{
Name = reader.GetAttribute("name"),
Tag = reader.GetAttribute("tag"),
Mask = reader.GetAttribute("mask"),
2020-09-01 16:21:55 -07:00
Conditions = new List<Condition>(),
Locations = new List<Location>(),
Values = new List<Setting>(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 13:36:32 -07:00
};
// Now read the internal tags
ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
2020-09-01 16:21:55 -07:00
datItems.Add(dipSwitch);
2020-09-01 13:36:32 -07:00
// Skip the dipswitch now that we've processed it
reader.Skip();
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"),
SHA1 = reader.GetAttribute("sha1"),
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;
2020-09-02 21:36:14 -07:00
case "display":
2020-09-04 10:28:25 -07:00
var display = new Display
2020-09-02 21:36:14 -07:00
{
Tag = reader.GetAttribute("tag"),
DisplayType = reader.GetAttribute("type").AsDisplayType(),
2020-09-04 23:03:27 -07:00
Rotate = Sanitizer.CleanLong(reader.GetAttribute("rotate")),
2020-09-02 21:36:14 -07:00
FlipX = reader.GetAttribute("flipx").AsYesNo(),
2020-09-04 23:03:27 -07:00
Width = Sanitizer.CleanLong(reader.GetAttribute("width")),
Height = Sanitizer.CleanLong(reader.GetAttribute("height")),
PixClock = Sanitizer.CleanLong(reader.GetAttribute("pixclock")),
HTotal = Sanitizer.CleanLong(reader.GetAttribute("htotal")),
HBEnd = Sanitizer.CleanLong(reader.GetAttribute("hbend")),
HBStart = Sanitizer.CleanLong(reader.GetAttribute("hbstart")),
VTotal = Sanitizer.CleanLong(reader.GetAttribute("vtotal")),
VBEnd = Sanitizer.CleanLong(reader.GetAttribute("vbend")),
VBStart = Sanitizer.CleanLong(reader.GetAttribute("vbstart")),
2020-09-02 21:36:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-04 10:28:25 -07:00
};
2020-09-04 11:04:58 -07:00
// Set the refresh
if (reader.GetAttribute("refresh") != null)
{
if (Double.TryParse(reader.GetAttribute("refresh"), out double refresh))
display.Refresh = refresh;
}
2020-09-04 10:28:25 -07:00
datItems.Add(display);
2020-09-02 21:36:14 -07:00
reader.Read();
break;
2020-09-02 15:38:10 -07:00
case "driver":
datItems.Add(new Driver
{
Status = reader.GetAttribute("status").AsSupportStatus(),
Emulation = reader.GetAttribute("emulation").AsSupportStatus(),
Cocktail = reader.GetAttribute("cocktail").AsSupportStatus(),
SaveState = reader.GetAttribute("savestate").AsSupported(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-02 15:38:10 -07:00
});
reader.Read();
break;
2020-09-02 13:31:50 -07:00
case "feature":
datItems.Add(new Feature
{
2020-09-02 14:04:02 -07:00
Type = reader.GetAttribute("type").AsFeatureType(),
2020-09-02 14:34:41 -07:00
Status = reader.GetAttribute("status").AsFeatureStatus(),
Overall = reader.GetAttribute("overall").AsFeatureStatus(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-02 13:31:50 -07:00
});
reader.Read();
break;
2020-09-02 21:59:26 -07:00
case "input":
var input = new Input
{
Service = reader.GetAttribute("service").AsYesNo(),
Tilt = reader.GetAttribute("tilt").AsYesNo(),
2020-09-04 23:03:27 -07:00
Players = Sanitizer.CleanLong(reader.GetAttribute("players")),
Coins = Sanitizer.CleanLong(reader.GetAttribute("coins")),
2020-09-02 21:59:26 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now read the internal tags
ReadInput(reader.ReadSubtree(), input);
datItems.Add(input);
// Skip the input now that we've processed it
reader.Skip();
break;
2020-09-02 17:22:31 -07:00
case "port":
var port = new Port
{
Tag = reader.GetAttribute("tag"),
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now read the internal tags
ReadPort(reader.ReadSubtree(), port);
datItems.Add(port);
// Skip the port now that we've processed it
reader.Skip();
break;
2020-09-03 22:28:48 -07:00
case "ramoption":
datItems.Add(new RamOption
{
Name = reader.GetAttribute("name"),
2020-09-03 22:28:48 -07:00
Default = reader.GetAttribute("default").AsYesNo(),
Content = reader.ReadElementContentAsString(),
2020-09-01 11:34:52 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
});
2019-01-11 13:43:15 -08:00
break;
2020-09-03 22:28:48 -07:00
case "rom":
datItems.Add(new Rom
2020-09-01 11:34:52 -07:00
{
Name = reader.GetAttribute("name"),
2020-09-03 22:28:48 -07:00
Bios = reader.GetAttribute("bios"),
2020-09-04 23:03:27 -07:00
Size = Sanitizer.CleanLong(reader.GetAttribute("size")),
2020-09-03 22:28:48 -07:00
CRC = reader.GetAttribute("crc"),
SHA1 = reader.GetAttribute("sha1"),
MergeTag = reader.GetAttribute("merge"),
Region = reader.GetAttribute("region"),
Offset = reader.GetAttribute("offset"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
Optional = reader.GetAttribute("optional").AsYesNo(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 11:34:52 -07:00
});
2020-09-03 22:28:48 -07:00
reader.Read();
2020-09-01 11:34:52 -07:00
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;
2020-09-01 16:21:55 -07:00
case "slot":
var slot = new Slot
{
Name = reader.GetAttribute("name"),
SlotOptions = new List<SlotOption>(),
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-01 16:21:55 -07:00
};
// Now read the internal tags
ReadSlot(reader.ReadSubtree(), slot);
datItems.Add(slot);
// Skip the slot now that we've processed it
reader.Skip();
break;
2020-09-01 11:34:52 -07:00
case "softwarelist":
datItems.Add(new DatItems.SoftwareList
2020-08-25 22:48:46 -07:00
{
Name = reader.GetAttribute("name"),
2020-09-01 11:34:52 -07:00
Status = reader.GetAttribute("status").AsSoftwareListStatus(),
Filter = reader.GetAttribute("filter"),
2020-08-24 22:25:47 -07:00
2020-08-25 22:48:46 -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();
2020-09-02 12:51:21 -07:00
break;
2020-09-01 11:34:52 -07:00
2020-09-02 12:51:21 -07:00
case "sound":
2020-09-03 22:35:09 -07:00
var sound = new Sound
2020-09-02 12:51:21 -07:00
{
2020-09-04 23:03:27 -07:00
Channels = Sanitizer.CleanLong(reader.GetAttribute("channels")),
2020-09-02 12:51:21 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2020-09-03 22:35:09 -07:00
};
datItems.Add(sound);
2020-09-02 12:51:21 -07:00
reader.Read();
2019-01-11 13:43:15 -08:00
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>
2020-09-01 16:21:55 -07:00
private void ReadSlot(XmlReader reader, Slot slot)
2019-01-11 13:43:15 -08:00
{
// If we have an empty machine, skip it
if (reader == null)
return;
// Get list ready
2020-09-01 16:21:55 -07:00
slot.SlotOptions = new List<SlotOption>();
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":
2020-09-01 16:21:55 -07:00
var slotOption = new SlotOption();
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>
2020-09-01 16:21:55 -07:00
private void ReadInput(XmlReader reader, Input 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
2020-09-01 16:21:55 -07:00
input.Controls = new List<Control>();
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 Control
{
ControlType = reader.GetAttribute("type").AsControlType(),
Player = Sanitizer.CleanLong(reader.GetAttribute("player")),
Buttons = Sanitizer.CleanLong(reader.GetAttribute("buttons")),
RequiredButtons = Sanitizer.CleanLong(reader.GetAttribute("reqbuttons")),
Minimum = Sanitizer.CleanLong(reader.GetAttribute("minimum")),
Maximum = Sanitizer.CleanLong(reader.GetAttribute("maximum")),
Sensitivity = Sanitizer.CleanLong(reader.GetAttribute("sensitivity")),
KeyDelta = Sanitizer.CleanLong(reader.GetAttribute("keydelta")),
Reverse = reader.GetAttribute("reverse").AsYesNo(),
Ways = reader.GetAttribute("ways"),
Ways2 = reader.GetAttribute("ways2"),
Ways3 = reader.GetAttribute("ways3"),
};
2020-08-22 23:40:00 -07:00
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>
2020-09-01 13:36:32 -07:00
/// <param name="dipSwitch">DipSwitch to populate</param>
private void ReadDipSwitch(XmlReader reader, DipSwitch 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
2020-09-01 16:21:55 -07:00
dipSwitch.Conditions = new List<Condition>();
dipSwitch.Locations = new List<Location>();
dipSwitch.Values = new List<Setting>();
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)
{
2020-09-01 11:34:52 -07:00
case "condition":
2020-09-01 16:21:55 -07:00
var condition = new Condition();
2020-09-01 11:34:52 -07:00
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
2020-09-03 21:59:53 -07:00
condition.Relation = reader.GetAttribute("relation").AsRelation();
2020-09-01 11:34:52 -07:00
condition.Value = reader.GetAttribute("value");
dipSwitch.Conditions.Add(condition);
reader.Read();
break;
2020-08-22 13:31:13 -07:00
case "diplocation":
2020-09-01 16:21:55 -07:00
var dipLocation = new Location();
2020-08-22 23:40:00 -07:00
dipLocation.Name = reader.GetAttribute("name");
2020-09-06 23:08:50 -07:00
dipLocation.Number = Sanitizer.CleanLong(reader.GetAttribute("number"));
2020-08-22 23:40:00 -07:00
dipLocation.Inverted = reader.GetAttribute("inverted").AsYesNo();
dipSwitch.Locations.Add(dipLocation);
2020-08-22 13:31:13 -07:00
reader.Read();
break;
case "dipvalue":
2020-09-01 16:21:55 -07:00
var dipValue = new Setting();
2020-08-22 23:40:00 -07:00
dipValue.Name = reader.GetAttribute("name");
dipValue.Value = reader.GetAttribute("value");
dipValue.Default = reader.GetAttribute("default").AsYesNo();
2020-09-01 11:34:52 -07:00
// Now read the internal tags
ReadDipValue(reader, dipValue);
2020-08-22 23:40:00 -07:00
dipSwitch.Values.Add(dipValue);
2020-09-01 11:34:52 -07:00
// Skip the dipvalue now that we've processed it
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read DipValue information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
2020-09-01 16:21:55 -07:00
/// <param name="dipValue">Setting to populate</param>
private void ReadDipValue(XmlReader reader, Setting dipValue)
2020-09-01 11:34:52 -07:00
{
// If we have an empty dipvalue, skip it
if (reader == null)
return;
// Get list ready
2020-09-01 16:21:55 -07:00
dipValue.Conditions = new List<Condition>();
2020-09-01 11:34:52 -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 dipvalue
switch (reader.Name)
{
case "condition":
2020-09-01 16:21:55 -07:00
var condition = new Condition();
2020-09-01 11:34:52 -07:00
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
2020-09-03 21:59:53 -07:00
condition.Relation = reader.GetAttribute("relation").AsRelation();
2020-09-01 11:34:52 -07:00
condition.Value = reader.GetAttribute("value");
dipValue.Conditions.Add(condition);
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>
2020-09-01 11:55:11 -07:00
/// <param name="configuration">Configuration to populate</param>
private void ReadConfiguration(XmlReader reader, Configuration configuration)
{
// If we have an empty configuration, skip it
if (reader == null)
return;
// Get lists ready
2020-09-01 16:21:55 -07:00
configuration.Conditions = new List<Condition>();
configuration.Locations = new List<Location>();
configuration.Settings = new List<Setting>();
// 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)
{
2020-09-01 11:34:52 -07:00
case "condition":
2020-09-01 16:21:55 -07:00
var condition = new Condition();
2020-09-01 11:34:52 -07:00
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
2020-09-03 21:59:53 -07:00
condition.Relation = reader.GetAttribute("relation").AsRelation();
2020-09-01 11:34:52 -07:00
condition.Value = reader.GetAttribute("value");
configuration.Conditions.Add(condition);
reader.Read();
break;
case "conflocation":
2020-09-01 16:21:55 -07:00
var confLocation = new Location();
confLocation.Name = reader.GetAttribute("name");
2020-09-06 23:08:50 -07:00
confLocation.Number = Sanitizer.CleanLong(reader.GetAttribute("number"));
confLocation.Inverted = reader.GetAttribute("inverted").AsYesNo();
configuration.Locations.Add(confLocation);
reader.Read();
break;
case "confsetting":
2020-09-01 16:21:55 -07:00
var confSetting = new Setting();
confSetting.Name = reader.GetAttribute("name");
confSetting.Value = reader.GetAttribute("value");
confSetting.Default = reader.GetAttribute("default").AsYesNo();
2020-09-01 11:34:52 -07:00
// Now read the internal tags
ReadConfSetting(reader, confSetting);
configuration.Settings.Add(confSetting);
2020-09-01 11:34:52 -07:00
// Skip the dipvalue now that we've processed it
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary>
/// Read ConfSetting information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
2020-09-01 16:21:55 -07:00
/// <param name="confSetting">Setting to populate</param>
private void ReadConfSetting(XmlReader reader, Setting confSetting)
2020-09-01 11:34:52 -07:00
{
// If we have an empty confsetting, skip it
if (reader == null)
return;
// Get list ready
2020-09-01 16:21:55 -07:00
confSetting.Conditions = new List<Condition>();
2020-09-01 11:34:52 -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 confsetting
switch (reader.Name)
{
case "condition":
2020-09-01 16:21:55 -07:00
var condition = new Condition();
2020-09-01 11:34:52 -07:00
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
2020-09-03 21:59:53 -07:00
condition.Relation = reader.GetAttribute("relation").AsRelation();
2020-09-01 11:34:52 -07:00
condition.Value = reader.GetAttribute("value");
confSetting.Conditions.Add(condition);
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>
2020-09-01 16:21:55 -07:00
private void ReadPort(XmlReader reader, Port port)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get list ready
2020-09-01 16:21:55 -07:00
port.Analogs = new List<Analog>();
// 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":
2020-09-01 16:21:55 -07:00
var analog = new Analog();
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>
2020-09-01 11:34:52 -07:00
/// <param name="adjuster">Adjuster to populate</param>
private void ReadAdjuster(XmlReader reader, Adjuster adjuster)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get list ready
2020-09-01 16:21:55 -07:00
adjuster.Conditions = new List<Condition>();
// 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":
2020-09-01 16:21:55 -07:00
var condition = new Condition();
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
2020-09-03 21:59:53 -07:00
condition.Relation = reader.GetAttribute("relation").AsRelation();
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>
2020-09-01 16:21:55 -07:00
private void ReadDevice(XmlReader reader, Device device)
{
// If we have an empty port, skip it
if (reader == null)
return;
// Get lists ready
2020-09-01 16:21:55 -07:00
device.Instances = new List<Instance>();
device.Extensions = new List<Extension>();
// 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":
2020-09-02 16:37:01 -07:00
var instance = new Instance
{
Name = reader.GetAttribute("name"),
BriefName = reader.GetAttribute("briefname"),
};
device.Instances.Add(instance);
reader.Read();
break;
case "extension":
2020-09-02 16:37:01 -07:00
var extension = new 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-08-28 15:06:07 -07:00
List<DatItem> datItems = Items.FilteredItems(key);
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
2020-08-28 15:06:07 -07:00
datItems = DatItem.ResolveNames(datItems);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
for (int index = 0; index < datItems.Count; index++)
2019-01-11 13:43:15 -08:00
{
2020-08-28 15:06:07 -07:00
DatItem datItem = datItems[index];
2019-01-11 13:43:15 -08:00
// If we have a different game and we're not at the start of the list, output the end of last item
2020-08-28 15:06:07 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
2020-09-01 11:34:52 -07:00
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-28 15:06:07 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(xtw, datItem);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-28 15:06:07 -07:00
lastgame = datItem.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");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("build", Header.Name);
2020-08-24 00:25:23 -07:00
xtw.WriteOptionalAttributeString("debug", Header.Debug.FromYesNo());
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("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-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", datItem.Machine.Name);
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("sourcefile", datItem.Machine.SourceFile);
2020-08-23 22:54:09 -07:00
if (datItem.Machine.MachineType.HasFlag(MachineType.Bios))
xtw.WriteAttributeString("isbios", "yes");
if (datItem.Machine.MachineType.HasFlag(MachineType.Device))
xtw.WriteAttributeString("isdevice", "yes");
if (datItem.Machine.MachineType.HasFlag(MachineType.Mechanical))
xtw.WriteAttributeString("ismechanical", "yes");
2019-01-11 13:43:15 -08:00
2020-08-24 13:21:59 -07:00
xtw.WriteOptionalAttributeString("runnable", datItem.Machine.Runnable.FromRunnable());
2020-08-23 22:54:09 -07:00
if (!string.Equals(datItem.Machine.Name, datItem.Machine.CloneOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteOptionalAttributeString("cloneof", datItem.Machine.CloneOf);
if (!string.Equals(datItem.Machine.Name, datItem.Machine.RomOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteOptionalAttributeString("romof", datItem.Machine.RomOf);
if (!string.Equals(datItem.Machine.Name, datItem.Machine.SampleOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteOptionalAttributeString("sampleof", datItem.Machine.SampleOf);
xtw.WriteOptionalElementString("description", datItem.Machine.Description);
xtw.WriteOptionalElementString("year", datItem.Machine.Year);
2020-08-25 22:13:49 -07:00
xtw.WriteOptionalElementString("manufacturer", datItem.Machine.Manufacturer);
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>
2020-09-01 11:34:52 -07:00
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
/// <returns>True if the data was written, false on error</returns>
2020-08-28 15:06:07 -07:00
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
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
{
2020-09-01 11:34:52 -07:00
case ItemType.Adjuster:
var adjuster = datItem as Adjuster;
xtw.WriteStartElement("adjuster");
2020-09-02 12:32:10 -07:00
xtw.WriteRequiredAttributeString("name", adjuster.Name);
2020-09-01 11:34:52 -07:00
xtw.WriteOptionalAttributeString("default", adjuster.Default.FromYesNo());
if (adjuster.Conditions != null)
{
foreach (var adjusterCondition in adjuster.Conditions)
2020-09-01 11:34:52 -07:00
{
xtw.WriteStartElement("condition");
xtw.WriteOptionalAttributeString("tag", adjusterCondition.Tag);
xtw.WriteOptionalAttributeString("mask", adjusterCondition.Mask);
2020-09-03 21:59:53 -07:00
xtw.WriteOptionalAttributeString("relation", adjusterCondition.Relation.FromRelation());
xtw.WriteOptionalAttributeString("value", adjusterCondition.Value);
2020-09-01 11:34:52 -07:00
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
case ItemType.BiosSet:
var biosSet = datItem as BiosSet;
xtw.WriteStartElement("biosset");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", biosSet.Name);
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("description", biosSet.Description);
xtw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant());
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2020-08-25 22:48:46 -07:00
case ItemType.Chip:
var chip = datItem as Chip;
xtw.WriteStartElement("chip");
xtw.WriteRequiredAttributeString("name", chip.Name);
xtw.WriteOptionalAttributeString("tag", chip.Tag);
xtw.WriteOptionalAttributeString("type", chip.ChipType.FromChipType());
2020-09-04 11:20:54 -07:00
xtw.WriteOptionalAttributeString("clock", chip.Clock?.ToString());
2020-08-25 22:48:46 -07:00
xtw.WriteEndElement();
break;
case ItemType.Condition:
var condition = datItem as Condition;
xtw.WriteStartElement("condition");
xtw.WriteOptionalAttributeString("tag", condition.Tag);
xtw.WriteOptionalAttributeString("mask", condition.Mask);
2020-09-03 21:59:53 -07:00
xtw.WriteOptionalAttributeString("relation", condition.Relation.FromRelation());
xtw.WriteOptionalAttributeString("value", condition.Value);
xtw.WriteEndElement();
break;
2020-09-01 11:55:11 -07:00
case ItemType.Configuration:
var configuration = datItem as Configuration;
xtw.WriteStartElement("configuration");
xtw.WriteOptionalAttributeString("name", configuration.Name);
xtw.WriteOptionalAttributeString("tag", configuration.Tag);
xtw.WriteOptionalAttributeString("mask", configuration.Mask);
if (configuration.Conditions != null)
{
foreach (var configurationCondition in configuration.Conditions)
2020-09-01 11:55:11 -07:00
{
xtw.WriteStartElement("condition");
xtw.WriteOptionalAttributeString("tag", configurationCondition.Tag);
xtw.WriteOptionalAttributeString("mask", configurationCondition.Mask);
2020-09-03 21:59:53 -07:00
xtw.WriteOptionalAttributeString("relation", configurationCondition.Relation.FromRelation());
xtw.WriteOptionalAttributeString("value", configurationCondition.Value);
2020-09-01 11:55:11 -07:00
xtw.WriteEndElement();
}
}
if (configuration.Locations != null)
{
foreach (var location in configuration.Locations)
{
xtw.WriteStartElement("conflocation");
xtw.WriteOptionalAttributeString("name", location.Name);
2020-09-06 23:08:50 -07:00
xtw.WriteOptionalAttributeString("number", location.Number?.ToString());
2020-09-01 11:55:11 -07:00
xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo());
xtw.WriteEndElement();
}
}
if (configuration.Settings != null)
{
foreach (var setting in configuration.Settings)
{
xtw.WriteStartElement("confsetting");
xtw.WriteOptionalAttributeString("name", setting.Name);
xtw.WriteOptionalAttributeString("value", setting.Value);
xtw.WriteOptionalAttributeString("default", setting.Default.FromYesNo());
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
2020-09-02 17:09:19 -07:00
case ItemType.Device:
var device = datItem as Device;
xtw.WriteStartElement("device");
2020-09-07 00:39:59 -07:00
xtw.WriteOptionalAttributeString("type", device.DeviceType.FromDeviceType());
2020-09-02 17:09:19 -07:00
xtw.WriteOptionalAttributeString("tag", device.Tag);
xtw.WriteOptionalAttributeString("fixed_image", device.FixedImage);
xtw.WriteOptionalAttributeString("mandatory", device.Mandatory);
xtw.WriteOptionalAttributeString("interface", device.Interface);
if (device.Instances != null)
{
foreach (var instance in device.Instances)
{
xtw.WriteStartElement("instance");
xtw.WriteOptionalAttributeString("name", instance.Name);
xtw.WriteOptionalAttributeString("briefname", instance.BriefName);
xtw.WriteEndElement();
}
}
if (device.Extensions != null)
{
foreach (var extension in device.Extensions)
{
xtw.WriteStartElement("extension");
xtw.WriteOptionalAttributeString("name", extension.Name);
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
case ItemType.DeviceReference:
2020-09-02 12:32:10 -07:00
var deviceRef = datItem as DeviceReference;
xtw.WriteStartElement("device_ref");
2020-09-02 12:32:10 -07:00
xtw.WriteRequiredAttributeString("name", deviceRef.Name);
xtw.WriteEndElement();
break;
2020-09-01 13:36:32 -07:00
case ItemType.DipSwitch:
var dipSwitch = datItem as DipSwitch;
xtw.WriteStartElement("dipswitch");
xtw.WriteOptionalAttributeString("name", dipSwitch.Name);
xtw.WriteOptionalAttributeString("tag", dipSwitch.Tag);
xtw.WriteOptionalAttributeString("mask", dipSwitch.Mask);
if (dipSwitch.Conditions != null)
{
foreach (var dipSwitchCondition in dipSwitch.Conditions)
2020-09-01 13:36:32 -07:00
{
xtw.WriteStartElement("condition");
xtw.WriteOptionalAttributeString("tag", dipSwitchCondition.Tag);
xtw.WriteOptionalAttributeString("mask", dipSwitchCondition.Mask);
2020-09-03 21:59:53 -07:00
xtw.WriteOptionalAttributeString("relation", dipSwitchCondition.Relation.FromRelation());
xtw.WriteOptionalAttributeString("value", dipSwitchCondition.Value);
2020-09-01 13:36:32 -07:00
xtw.WriteEndElement();
}
}
if (dipSwitch.Locations != null)
{
foreach (var location in dipSwitch.Locations)
{
xtw.WriteStartElement("diplocation");
xtw.WriteOptionalAttributeString("name", location.Name);
2020-09-06 23:08:50 -07:00
xtw.WriteOptionalAttributeString("number", location.Number?.ToString());
2020-09-01 13:36:32 -07:00
xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo());
xtw.WriteEndElement();
}
}
if (dipSwitch.Values != null)
{
foreach (var value in dipSwitch.Values)
{
xtw.WriteStartElement("dipvalue");
xtw.WriteOptionalAttributeString("name", value.Name);
xtw.WriteOptionalAttributeString("value", value.Value);
xtw.WriteOptionalAttributeString("default", value.Default.FromYesNo());
if (value.Conditions != null)
{
foreach (var dipValueCondition in value.Conditions)
2020-09-01 13:36:32 -07:00
{
xtw.WriteStartElement("condition");
xtw.WriteOptionalAttributeString("tag", dipValueCondition.Tag);
xtw.WriteOptionalAttributeString("mask", dipValueCondition.Mask);
2020-09-03 21:59:53 -07:00
xtw.WriteOptionalAttributeString("relation", dipValueCondition.Relation.FromRelation());
xtw.WriteOptionalAttributeString("value", dipValueCondition.Value);
2020-09-01 13:36:32 -07:00
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
2019-01-11 13:43:15 -08:00
case ItemType.Disk:
var disk = datItem as Disk;
xtw.WriteStartElement("disk");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", disk.Name);
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant());
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("merge", disk.MergeTag);
xtw.WriteOptionalAttributeString("region", disk.Region);
xtw.WriteOptionalAttributeString("index", disk.Index);
xtw.WriteOptionalAttributeString("writable", disk.Writable.FromYesNo());
2020-08-24 13:21:59 -07:00
xtw.WriteOptionalAttributeString("status", disk.ItemStatus.FromItemStatus(false));
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("optional", disk.Optional.FromYesNo());
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2020-09-02 21:36:14 -07:00
case ItemType.Display:
var display = datItem as Display;
xtw.WriteStartElement("display");
xtw.WriteOptionalAttributeString("tag", display.Tag);
xtw.WriteOptionalAttributeString("type", display.DisplayType.FromDisplayType());
2020-09-04 10:28:25 -07:00
xtw.WriteOptionalAttributeString("rotate", display.Rotate?.ToString());
2020-09-02 21:36:14 -07:00
xtw.WriteOptionalAttributeString("flipx", display.FlipX.FromYesNo());
2020-09-04 10:39:37 -07:00
xtw.WriteOptionalAttributeString("width", display.Width?.ToString());
xtw.WriteOptionalAttributeString("height", display.Height?.ToString());
2020-09-04 11:04:58 -07:00
xtw.WriteOptionalAttributeString("refresh", display.Refresh?.ToString("N6"));
2020-09-04 10:50:08 -07:00
xtw.WriteOptionalAttributeString("pixclock", display.PixClock?.ToString());
2020-09-04 10:57:30 -07:00
xtw.WriteOptionalAttributeString("htotal", display.HTotal?.ToString());
xtw.WriteOptionalAttributeString("hbend", display.HBEnd?.ToString());
xtw.WriteOptionalAttributeString("hstart", display.HBStart?.ToString());
xtw.WriteOptionalAttributeString("vtotal", display.VTotal?.ToString());
xtw.WriteOptionalAttributeString("vbend", display.VBEnd?.ToString());
xtw.WriteOptionalAttributeString("vbstart", display.VBStart?.ToString());
2020-09-02 21:36:14 -07:00
xtw.WriteEndElement();
break;
2020-09-02 15:38:10 -07:00
case ItemType.Driver:
var driver = datItem as Driver;
xtw.WriteStartElement("driver");
xtw.WriteOptionalAttributeString("status", driver.Status.FromSupportStatus());
xtw.WriteOptionalAttributeString("emulation", driver.Emulation.FromSupportStatus());
xtw.WriteOptionalAttributeString("cocktail", driver.Cocktail.FromSupportStatus());
xtw.WriteOptionalAttributeString("savestate", driver.SaveState.FromSupported(true));
xtw.WriteEndElement();
break;
2020-09-02 13:31:50 -07:00
case ItemType.Feature:
var feature = datItem as Feature;
xtw.WriteStartElement("feature");
2020-09-02 14:04:02 -07:00
xtw.WriteOptionalAttributeString("type", feature.Type.FromFeatureType());
2020-09-02 14:34:41 -07:00
xtw.WriteOptionalAttributeString("status", feature.Status.FromFeatureStatus());
xtw.WriteOptionalAttributeString("overall", feature.Overall.FromFeatureStatus());
2020-09-02 13:31:50 -07:00
xtw.WriteEndElement();
break;
2020-09-02 21:59:26 -07:00
case ItemType.Input:
var input = datItem as Input;
xtw.WriteStartElement("input");
xtw.WriteOptionalAttributeString("service", input.Service.FromYesNo());
xtw.WriteOptionalAttributeString("tilt", input.Tilt.FromYesNo());
2020-09-03 22:28:48 -07:00
xtw.WriteOptionalAttributeString("players", input.Players?.ToString());
xtw.WriteOptionalAttributeString("coins", input.Coins?.ToString());
2020-09-02 21:59:26 -07:00
if (input.Controls != null)
{
foreach (var control in input.Controls)
{
xtw.WriteStartElement("control");
xtw.WriteOptionalAttributeString("type", control.ControlType.FromControlType());
xtw.WriteOptionalAttributeString("player", control.Player?.ToString());
xtw.WriteOptionalAttributeString("buttons", control.Buttons?.ToString());
xtw.WriteOptionalAttributeString("reqbuttons", control.RequiredButtons?.ToString());
xtw.WriteOptionalAttributeString("minimum", control.Minimum?.ToString());
xtw.WriteOptionalAttributeString("maximum", control.Maximum?.ToString());
xtw.WriteOptionalAttributeString("sensitivity", control.Sensitivity?.ToString());
xtw.WriteOptionalAttributeString("keydelta", control.KeyDelta?.ToString());
2020-09-02 21:59:26 -07:00
xtw.WriteOptionalAttributeString("reverse", control.Reverse.FromYesNo());
xtw.WriteOptionalAttributeString("ways", control.Ways);
xtw.WriteOptionalAttributeString("ways2", control.Ways2);
xtw.WriteOptionalAttributeString("ways3", control.Ways3);
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
2020-09-02 17:22:31 -07:00
case ItemType.Port:
var port = datItem as Port;
xtw.WriteStartElement("port");
xtw.WriteOptionalAttributeString("tag", port.Tag);
if (port.Analogs != null)
{
foreach (var analog in port.Analogs)
{
xtw.WriteStartElement("analog");
xtw.WriteOptionalAttributeString("mask", analog.Mask);
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
2020-09-01 11:34:52 -07:00
case ItemType.RamOption:
var ramOption = datItem as RamOption;
xtw.WriteStartElement("ramoption");
xtw.WriteRequiredAttributeString("name", ramOption.Name);
xtw.WriteOptionalAttributeString("default", ramOption.Default.FromYesNo());
xtw.WriteRaw(ramOption.Content ?? string.Empty);
xtw.WriteFullEndElement();
break;
2019-01-11 13:43:15 -08:00
case ItemType.Rom:
var rom = datItem as Rom;
xtw.WriteStartElement("rom");
2020-08-24 00:48:27 -07:00
xtw.WriteRequiredAttributeString("name", rom.Name);
2020-09-04 23:03:27 -07:00
xtw.WriteOptionalAttributeString("size", rom.Size?.ToString());
2020-08-24 11:56:49 -07:00
xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant());
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("bios", rom.Bios);
xtw.WriteOptionalAttributeString("merge", rom.MergeTag);
xtw.WriteOptionalAttributeString("region", rom.Region);
xtw.WriteOptionalAttributeString("offset", rom.Offset);
2020-08-24 13:21:59 -07:00
xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false));
2020-08-23 22:54:09 -07:00
xtw.WriteOptionalAttributeString("optional", rom.Optional.FromYesNo());
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Sample:
2020-09-02 12:32:10 -07:00
var sample = datItem as Sample;
xtw.WriteStartElement("sample");
2020-09-02 12:32:10 -07:00
xtw.WriteRequiredAttributeString("name", sample.Name);
xtw.WriteEndElement();
2020-08-31 23:26:07 -07:00
break;
2020-09-01 16:21:55 -07:00
case ItemType.Slot:
var slot = datItem as Slot;
xtw.WriteStartElement("slot");
xtw.WriteOptionalAttributeString("name", slot.Name);
if (slot.SlotOptions != null)
{
foreach (var slotOption in slot.SlotOptions)
{
xtw.WriteStartElement("slotoption");
xtw.WriteOptionalAttributeString("name", slotOption.Name);
xtw.WriteOptionalAttributeString("devname", slotOption.DeviceName);
xtw.WriteOptionalAttributeString("default", slotOption.Default.FromYesNo());
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
break;
2020-08-31 23:26:07 -07:00
case ItemType.SoftwareList:
var softwareList = datItem as DatItems.SoftwareList;
xtw.WriteStartElement("softwarelist");
2020-09-02 12:32:10 -07:00
xtw.WriteRequiredAttributeString("name", softwareList.Name);
2020-08-31 23:26:07 -07:00
xtw.WriteOptionalAttributeString("status", softwareList.Status.FromSoftwareListStatus());
2020-09-01 11:34:52 -07:00
xtw.WriteOptionalAttributeString("filter", softwareList.Filter);
2020-08-31 23:26:07 -07:00
xtw.WriteEndElement();
2020-09-02 12:51:21 -07:00
break;
case ItemType.Sound:
var sound = datItem as Sound;
xtw.WriteStartElement("sound");
2020-09-03 22:35:09 -07:00
xtw.WriteOptionalAttributeString("channels", sound.Channels?.ToString());
2020-09-02 12:51:21 -07:00
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;
}
}
}