Files
SabreTools/SabreTools.Library/DatFiles/SoftwareList.cs

821 lines
31 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2020-08-24 22:25:47 -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;
2020-09-03 09:57:16 -07:00
// TODO: Use softwarelist.dtd and *try* to make this write more correctly
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
2020-09-03 10:03:04 -07:00
/// Represents parsing and writing of a SoftwareList
2019-01-11 13:43:15 -08:00
/// </summary>
internal class SoftwareList : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public SoftwareList(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse an SofwareList 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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
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 "softwarelist":
Header.Name = (Header.Name == null ? xtr.GetAttribute("name") ?? string.Empty : Header.Name);
Header.Description = (Header.Description == null ? xtr.GetAttribute("description") ?? string.Empty : Header.Description);
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 "software":
ReadSoftware(xtr.ReadSubtree(), filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the software 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(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
2019-01-11 13:43:15 -08:00
// For XML errors, just skip the affected node
xtr?.Read();
}
xtr.Dispose();
}
/// <summary>
/// Read software information
/// </summary>
/// <param name="reader">XmlReader representing a software block</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private void ReadSoftware(XmlReader reader, string filename, int indexId)
2019-01-11 13:43:15 -08:00
{
// If we have an empty software, skip it
if (reader == null)
return;
// Otherwise, add what is possible
reader.MoveToContent();
bool containsItems = false;
// Create a new machine
Machine machine = new Machine
{
Name = reader.GetAttribute("name"),
2020-08-24 13:43:37 -07:00
CloneOf = reader.GetAttribute("cloneof"),
Supported = reader.GetAttribute("supported").AsSupported(),
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 elements from the software
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 "publisher":
machine.Publisher = reader.ReadElementContentAsString();
break;
2019-01-11 13:43:15 -08:00
case "info":
2020-09-02 23:31:35 -07:00
ParseAddHelper(new Info
{
Name = reader.GetAttribute("name"),
Value = reader.GetAttribute("value"),
2020-08-24 22:25:47 -07:00
2020-09-02 23:31:35 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
});
2019-01-11 13:43:15 -08:00
reader.Read();
break;
2019-01-11 13:43:15 -08:00
case "sharedfeat":
2020-09-03 00:48:07 -07:00
ParseAddHelper(new SharedFeature
2020-09-02 23:31:35 -07:00
{
Name = reader.GetAttribute("name"),
Value = reader.GetAttribute("value"),
2020-08-24 22:25:47 -07:00
2020-09-03 00:48:07 -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 "part": // Contains all rom and disk information
var part = new Part()
{
Name = reader.GetAttribute("name"),
Interface = reader.GetAttribute("interface"),
};
// Now read the internal tags
containsItems = ReadPart(reader.ReadSubtree(), machine, part, filename, indexId);
2019-01-11 13:43:15 -08:00
// Skip the part now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
default:
reader.Read();
break;
}
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
{
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 part information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="part">Part information to pass to contained items</param>
2019-01-11 13:43:15 -08:00
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private bool ReadPart(XmlReader reader, Machine machine, Part part, string filename, int indexId)
2019-01-11 13:43:15 -08:00
{
// If we have an empty port, skip it
if (reader == null)
return false;
// Get lists ready
part.Features = new List<PartFeature>();
2020-08-24 22:25:47 -07:00
List<DatItem> items = 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 elements from the software
switch (reader.Name)
{
case "feature":
var feature = new PartFeature()
{
Name = reader.GetAttribute("name"),
Value = reader.GetAttribute("value"),
};
2020-08-24 22:25:47 -07:00
part.Features.Add(feature);
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 "dataarea":
var dataArea = new DataArea
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
Size = Sanitizer.CleanLong(reader.GetAttribute("size")),
Width = Sanitizer.CleanLong(reader.GetAttribute("width")),
2020-09-03 21:39:16 -07:00
Endianness = reader.GetAttribute("endianness").AsEndianness(),
};
2020-08-21 13:31:22 -07:00
List<DatItem> roms = ReadDataArea(reader.ReadSubtree(), dataArea);
2019-01-11 13:43:15 -08:00
2020-08-24 22:25:47 -07:00
// If we got valid roms, add them to the list
if (roms != null)
items.AddRange(roms);
2019-01-11 13:43:15 -08:00
// Skip the dataarea now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "diskarea":
var diskArea = new DiskArea
{
Name = reader.GetAttribute("name"),
};
2019-01-11 13:43:15 -08:00
List<DatItem> disks = ReadDiskArea(reader.ReadSubtree(), diskArea);
2019-01-11 13:43:15 -08:00
2020-08-24 22:25:47 -07:00
// If we got valid disks, add them to the list
if (disks != null)
items.AddRange(disks);
2019-01-11 13:43:15 -08:00
// Skip the diskarea now that we've processed it
reader.Skip();
break;
2019-01-11 13:43:15 -08:00
case "dipswitch":
2020-09-01 13:36:32 -07:00
var dipSwitch = new DipSwitch
{
Name = reader.GetAttribute("name"),
Tag = reader.GetAttribute("tag"),
Mask = reader.GetAttribute("mask"),
};
2020-08-22 23:40:00 -07:00
// Now read the internal tags
2020-08-22 13:31:13 -07:00
ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
2020-09-01 13:36:32 -07:00
items.Add(dipSwitch);
2020-08-24 22:25:47 -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
default:
reader.Read();
break;
}
}
2020-08-24 22:25:47 -07:00
// Loop over all of the items, if they exist
string key = string.Empty;
2020-08-24 22:25:47 -07:00
foreach (DatItem item in items)
{
// Add all missing information
switch (item.ItemType)
{
case ItemType.DipSwitch:
(item as DipSwitch).Part = part;
break;
case ItemType.Disk:
(item as Disk).Part = part;
break;
case ItemType.Rom:
(item as Rom).Part = part;
// If the rom is continue or ignore, add the size to the previous rom
// TODO: Can this be done on write? We technically lose information this way.
// Order is not guaranteed, and since these don't tend to have any way
// of determining what the "previous" item was after this, that info would
// have to be stored *with* the item somehow
if ((item as Rom).LoadFlag == LoadFlag.Continue || (item as Rom).LoadFlag == LoadFlag.Ignore)
{
int index = Items[key].Count - 1;
DatItem lastrom = Items[key][index];
if (lastrom.ItemType == ItemType.Rom)
{
(lastrom as Rom).Size += (item as Rom).Size;
Items[key].RemoveAt(index);
Items[key].Add(lastrom);
}
continue;
}
break;
}
2020-08-24 22:25:47 -07:00
item.Source = new Source(indexId, filename);
item.CopyMachineInformation(machine);
// Finally add each item
key = ParseAddHelper(item);
2020-08-24 22:25:47 -07:00
}
return items.Any();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read dataarea information
/// </summary>
/// <param name="reader">XmlReader representing a dataarea block</param>
/// <param name="dataArea">DataArea representing the enclosing area</param>
private List<DatItem> ReadDataArea(XmlReader reader, DataArea dataArea)
2019-01-11 13:43:15 -08:00
{
2020-08-24 22:25:47 -07:00
List<DatItem> items = 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 elements from the software
switch (reader.Name)
{
case "rom":
2020-09-04 10:28:25 -07:00
var rom = new Rom
2019-01-11 13:43:15 -08:00
{
Name = reader.GetAttribute("name"),
2020-09-04 23:03:27 -07:00
Size = Sanitizer.CleanLong(reader.GetAttribute("size")),
CRC = reader.GetAttribute("crc"),
SHA1 = reader.GetAttribute("sha1"),
2019-01-11 13:43:15 -08:00
Offset = reader.GetAttribute("offset"),
2020-08-21 14:20:17 -07:00
Value = reader.GetAttribute("value"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
LoadFlag = reader.GetAttribute("loadflag").AsLoadFlag(),
DataArea = dataArea,
2019-01-11 13:43:15 -08:00
};
2020-08-24 22:25:47 -07:00
items.Add(rom);
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-24 22:25:47 -07:00
return items;
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read diskarea information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="diskArea">DiskArea representing the enclosing area</param>
private List<DatItem> ReadDiskArea(XmlReader reader, DiskArea diskArea)
2019-01-11 13:43:15 -08:00
{
2020-08-24 22:25:47 -07:00
List<DatItem> items = 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 elements from the software
switch (reader.Name)
{
case "disk":
DatItem disk = new Disk
{
Name = reader.GetAttribute("name"),
SHA1 = reader.GetAttribute("sha1"),
ItemStatus = reader.GetAttribute("status").AsItemStatus(),
Writable = reader.GetAttribute("writable").AsYesNo(),
2019-01-11 13:43:15 -08:00
DiskArea = diskArea,
2019-01-11 13:43:15 -08:00
};
2020-08-24 22:25:47 -07:00
items.Add(disk);
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-24 22:25:47 -07:00
return items;
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Read DipSwitch DipValues information
/// </summary>
2020-08-22 13:31:13 -07:00
/// <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)
{
// If we have an empty dipswitch, skip it
if (reader == null)
2020-08-22 13:31:13 -07:00
return;
// Get list ready
2020-09-01 16:21:55 -07:00
dipSwitch.Values = 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)
{
case "dipvalue":
var dipValue = new Setting
{
Name = reader.GetAttribute("name"),
Value = reader.GetAttribute("value"),
Default = reader.GetAttribute("default").AsYesNo(),
};
2020-08-22 23:40:00 -07:00
dipSwitch.Values.Add(dipValue);
2020-08-22 13:31:13 -07:00
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
2020-09-18 17:12:31 -07:00
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[]
{
ItemType.DipSwitch,
ItemType.Disk,
ItemType.Info,
ItemType.Rom,
ItemType.SharedFeature,
};
}
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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
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
2020-09-25 20:25:29 -07:00
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
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())
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)
{
2020-09-15 14:38:37 -07:00
Globals.Logger.Error(ex);
if (throwOnError) throw ex;
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteHeader(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
xtw.WriteStartDocument();
xtw.WriteDocType("softwarelist", null, "softwarelist.dtd", null);
xtw.WriteStartElement("softwarelist");
xtw.WriteRequiredAttributeString("name", Header.Name);
xtw.WriteRequiredAttributeString("description", Header.Description);
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteStartGame(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// No game should start with a path separator
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2019-01-11 13:43:15 -08:00
// Build the state
xtw.WriteStartElement("software");
xtw.WriteRequiredAttributeString("name", datItem.Machine.Name);
if (!string.Equals(datItem.Machine.Name, datItem.Machine.CloneOf, StringComparison.OrdinalIgnoreCase))
xtw.WriteOptionalAttributeString("cloneof", datItem.Machine.CloneOf);
xtw.WriteOptionalAttributeString("supported", datItem.Machine.Supported.FromSupported(false));
2020-09-15 12:12:13 -07:00
xtw.WriteOptionalElementString("description", datItem.Machine.Description);
xtw.WriteOptionalElementString("year", datItem.Machine.Year);
xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher);
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteEndGame(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End software
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteDatItem(XmlTextWriter xtw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
// Build the state
switch (datItem.ItemType)
{
case ItemType.DipSwitch:
var dipSwitch = datItem as DipSwitch;
xtw.WriteStartElement("dipswitch");
xtw.WriteRequiredAttributeString("name", dipSwitch.Name);
xtw.WriteRequiredAttributeString("tag", dipSwitch.Tag);
xtw.WriteRequiredAttributeString("mask", dipSwitch.Mask);
if (dipSwitch.Values != null)
{
foreach (Setting dipValue in dipSwitch.Values)
2020-09-01 13:36:32 -07:00
{
xtw.WriteStartElement("dipvalue");
xtw.WriteRequiredAttributeString("name", dipValue.Name);
xtw.WriteOptionalAttributeString("value", dipValue.Value);
xtw.WriteOptionalAttributeString("default", dipValue.Default.FromYesNo());
xtw.WriteEndElement();
2020-09-01 13:36:32 -07:00
}
}
xtw.WriteEndElement();
break;
2020-09-01 13:36:32 -07:00
case ItemType.Disk:
var disk = datItem as Disk;
string diskAreaName = disk.DiskArea?.Name;
if (string.IsNullOrWhiteSpace(diskAreaName))
diskAreaName = "cdrom";
xtw.WriteStartElement("part");
xtw.WriteRequiredAttributeString("name", disk.Part?.Name);
xtw.WriteRequiredAttributeString("interface", disk.Part?.Interface);
2020-09-03 09:57:16 -07:00
if (disk.Part?.Features != null && disk.Part?.Features.Count > 0)
{
foreach (PartFeature partFeature in disk.Part.Features)
2020-09-03 09:57:16 -07:00
{
xtw.WriteStartElement("feature");
xtw.WriteRequiredAttributeString("name", partFeature.Name);
xtw.WriteRequiredAttributeString("value", partFeature.Value);
xtw.WriteEndElement();
2020-09-03 09:57:16 -07:00
}
}
2020-09-03 09:57:16 -07:00
xtw.WriteStartElement("diskarea");
xtw.WriteRequiredAttributeString("name", diskAreaName);
xtw.WriteStartElement("disk");
xtw.WriteRequiredAttributeString("name", disk.Name);
xtw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("status", disk.ItemStatus.FromItemStatus(false));
xtw.WriteOptionalAttributeString("writable", disk.Writable.FromYesNo());
xtw.WriteEndElement();
// End diskarea
xtw.WriteEndElement();
// End part
xtw.WriteEndElement();
break;
case ItemType.Info:
var info = datItem as Info;
xtw.WriteStartElement("info");
xtw.WriteRequiredAttributeString("name", info.Name);
xtw.WriteRequiredAttributeString("value", info.Value);
xtw.WriteEndElement();
break;
case ItemType.Rom:
var rom = datItem as Rom;
string dataAreaName = rom.DataArea?.Name;
if (string.IsNullOrWhiteSpace(dataAreaName))
dataAreaName = "rom";
xtw.WriteStartElement("part");
xtw.WriteRequiredAttributeString("name", rom.Part?.Name);
xtw.WriteRequiredAttributeString("interface", rom.Part?.Interface);
if (rom.Part?.Features != null && rom.Part?.Features.Count > 0)
{
foreach (PartFeature kvp in rom.Part.Features)
2020-09-03 09:57:16 -07:00
{
xtw.WriteStartElement("feature");
xtw.WriteRequiredAttributeString("name", kvp.Name);
xtw.WriteRequiredAttributeString("value", kvp.Value);
xtw.WriteEndElement();
2020-09-03 09:57:16 -07:00
}
}
2020-09-03 09:57:16 -07:00
xtw.WriteStartElement("dataarea");
xtw.WriteRequiredAttributeString("name", dataAreaName);
xtw.WriteOptionalAttributeString("size", rom.DataArea?.Size.ToString());
xtw.WriteOptionalAttributeString("width", rom.DataArea?.Width?.ToString());
xtw.WriteOptionalAttributeString("endianness", rom.DataArea?.Endianness.FromEndianness());
xtw.WriteStartElement("rom");
xtw.WriteRequiredAttributeString("name", rom.Name);
xtw.WriteOptionalAttributeString("size", rom.Size?.ToString());
xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant());
#if NET_FRAMEWORK
xtw.WriteOptionalAttributeString("ripemd160", rom.RIPEMD160?.ToLowerInvariant());
#endif
xtw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant());
xtw.WriteOptionalAttributeString("offset", rom.Offset);
xtw.WriteOptionalAttributeString("value", rom.Value);
xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false));
xtw.WriteOptionalAttributeString("loadflag", rom.LoadFlag.FromLoadFlag());
xtw.WriteEndElement();
// End dataarea
xtw.WriteEndElement();
// End part
xtw.WriteEndElement();
break;
case ItemType.SharedFeature:
var sharedFeature = datItem as SharedFeature;
xtw.WriteStartElement("sharedfeat");
xtw.WriteRequiredAttributeString("name", sharedFeature.Name);
xtw.WriteRequiredAttributeString("value", sharedFeature.Value);
xtw.WriteEndElement();
break;
2019-01-11 13:43:15 -08:00
}
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
private void WriteFooter(XmlTextWriter xtw)
2019-01-11 13:43:15 -08:00
{
// End software
xtw.WriteEndElement();
2020-09-15 12:12:13 -07:00
// End softwarelist
xtw.WriteEndElement();
2019-01-11 13:43:15 -08:00
xtw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}