using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Xml; using SabreTools.Library.Data; using SabreTools.Library.DatItems; using SabreTools.Library.Tools; #if MONO using System.IO; #else using Alphaleonis.Win32.Filesystem; using FileStream = System.IO.FileStream; using StreamWriter = System.IO.StreamWriter; #endif using NaturalSort; namespace SabreTools.Library.DatFiles { /// /// Represents parsing and writing of a SofwareList, M1, or MAME XML DAT /// /// TODO: Verify that all write for this DatFile type is correct internal class SoftwareList : DatFile { /// /// Constructor designed for casting a base DatFile /// /// Parent DatFile to copy from public SoftwareList(DatFile datFile) : base(datFile, cloneHeader: false) { } /// /// Parse an SofwareList XML DAT and return all found games and roms within /// /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) public override void ParseFile( // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { // Prepare all internal variables Encoding enc = Utilities.GetEncoding(filename); XmlReader xtr = Utilities.GetXmlTextReader(filename); // 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": Name = (String.IsNullOrWhiteSpace(Name) ? xtr.GetAttribute("name") ?? "" : Name); Description = (String.IsNullOrWhiteSpace(Description) ? xtr.GetAttribute("description") ?? "" : Description); if (ForceMerging == ForceMerging.None) { ForceMerging = Utilities.GetForceMerging(xtr.GetAttribute("forcemerging")); } if (ForceNodump == ForceNodump.None) { ForceNodump = Utilities.GetForceNodump(xtr.GetAttribute("forcenodump")); } if (ForcePacking == ForcePacking.None) { ForcePacking = Utilities.GetForcePacking(xtr.GetAttribute("forcepacking")); } xtr.Read(); break; // We want to process the entire subtree of the machine case "software": ReadSoftware(xtr.ReadSubtree(), filename, sysid, srcid, keep, clean, remUnicode); // Skip the software now that we've processed it xtr.Skip(); break; default: xtr.Read(); break; } } } catch (Exception ex) { Globals.Logger.Warning("Exception found while parsing '{0}': {1}", filename, ex); // For XML errors, just skip the affected node xtr?.Read(); } xtr.Dispose(); } /// /// Read software information /// /// XmlReader representing a software block /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) private void ReadSoftware( XmlReader reader, // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { // If we have an empty software, skip it if (reader == null) { return; } // Otherwise, add what is possible reader.MoveToContent(); string key = ""; string temptype = reader.Name; bool containsItems = false; // Create a new machine MachineType machineType = MachineType.NULL; if (Utilities.GetYesNo(reader.GetAttribute("isbios")) == true) { machineType |= MachineType.Bios; } if (Utilities.GetYesNo(reader.GetAttribute("isdevice")) == true) { machineType |= MachineType.Device; } if (Utilities.GetYesNo(reader.GetAttribute("ismechanical")) == true) { machineType |= MachineType.Mechanical; } Machine machine = new Machine { Name = reader.GetAttribute("name"), Description = reader.GetAttribute("name"), Supported = Utilities.GetYesNo(reader.GetAttribute("supported")), // (yes|partial|no) "yes" CloneOf = reader.GetAttribute("cloneof") ?? "", Infos = new List>(), MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType), }; 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; case "year": machine.Year = reader.ReadElementContentAsString(); break; case "publisher": machine.Publisher = reader.ReadElementContentAsString(); break; case "info": machine.Infos.Add(new Tuple(reader.GetAttribute("name"), reader.GetAttribute("value"))); reader.Read(); break; case "sharedfeat": // string sharedfeat_name = reader.GetAttribute("name"); // string sharedfeat_value = reader.GetAttribute("value"); reader.Read(); break; case "part": // Contains all rom and disk information containsItems = ReadPart(reader.ReadSubtree(), machine, filename, sysid, srcid, keep, clean, remUnicode); // Skip the part now that we've processed it reader.Skip(); break; default: reader.Read(); break; } } // If no items were found for this machine, add a Blank placeholder if (!containsItems) { Blank blank = new Blank() { SystemID = sysid, System = filename, SourceID = srcid, }; blank.CopyMachineInformation(machine); // Now process and add the rom ParseAddHelper(blank, clean, remUnicode); } } /// /// Read part information /// /// XmlReader representing a part block /// Machine information to pass to contained items /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) private bool ReadPart( XmlReader reader, Machine machine, // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { string key = "", areaname = "", partname = "", partinterface = ""; string temptype = reader.Name; long? areasize = null; List> features = new List>(); bool containsItems = false; while (!reader.EOF) { // We only want elements if (reader.NodeType != XmlNodeType.Element) { if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "part") { partname = ""; partinterface = ""; features = new List>(); } if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "dataarea" || reader.Name == "diskarea")) { areaname = ""; areasize = null; } reader.Read(); continue; } // Get the elements from the software switch (reader.Name) { case "part": partname = reader.GetAttribute("name"); partinterface = reader.GetAttribute("interface"); reader.Read(); break; case "feature": features.Add(new Tuple(reader.GetAttribute("name"), reader.GetAttribute("feature"))); reader.Read(); break; case "dataarea": areaname = reader.GetAttribute("name"); if (reader.GetAttribute("size") != null) { if (Int64.TryParse(reader.GetAttribute("size"), out long tempas)) { areasize = tempas; } } // string dataarea_width = reader.GetAttribute("width"); // (8|16|32|64) "8" // string dataarea_endianness = reader.GetAttribute("endianness"); // endianness (big|little) "little" containsItems = ReadDataArea(reader.ReadSubtree(), machine, features, areaname, areasize, partname, partinterface, filename, sysid, srcid, keep, clean, remUnicode); // Skip the dataarea now that we've processed it reader.Skip(); break; case "diskarea": areaname = reader.GetAttribute("name"); containsItems = ReadDiskArea(reader.ReadSubtree(), machine, features, areaname, areasize, partname, partinterface, filename, sysid, srcid, keep, clean, remUnicode); // Skip the diskarea now that we've processed it reader.Skip(); break; case "dipswitch": // string dipswitch_name = reader.GetAttribute("name"); // string dipswitch_tag = reader.GetAttribute("tag"); // string dipswitch_mask = reader.GetAttribute("mask"); // For every element... // string dipvalue_name = reader.GetAttribute("name"); // string dipvalue_value = reader.GetAttribute("value"); // bool? dipvalue_default = Utilities.GetYesNo(reader.GetAttribute("default")); // (yes|no) "no" reader.Skip(); break; default: reader.Read(); break; } } return containsItems; } /// /// Read dataarea information /// /// XmlReader representing a dataarea block /// Machine information to pass to contained items /// List of features from the parent part /// Name of the containing area /// Size of the containing area /// Name of the containing part /// Interface of the containing part /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) private bool ReadDataArea( XmlReader reader, Machine machine, List> features, string areaname, long? areasize, string partname, string partinterface, // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { string key = ""; string temptype = reader.Name; bool containsItems = false; 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": containsItems = true; // If the rom is continue or ignore, add the size to the previous rom if (reader.GetAttribute("loadflag") == "continue" || reader.GetAttribute("loadflag") == "ignore") { int index = this[key].Count - 1; DatItem lastrom = this[key][index]; if (lastrom.Type == ItemType.Rom) { ((Rom)lastrom).Size += Utilities.GetSize(reader.GetAttribute("size")); } this[key].RemoveAt(index); this[key].Add(lastrom); reader.Read(); continue; } DatItem rom = new Rom { Name = reader.GetAttribute("name"), Size = Utilities.GetSize(reader.GetAttribute("size")), CRC = Utilities.CleanHashData(reader.GetAttribute("crc"), Constants.CRCLength), MD5 = Utilities.CleanHashData(reader.GetAttribute("md5"), Constants.MD5Length), SHA1 = Utilities.CleanHashData(reader.GetAttribute("sha1"), Constants.SHA1Length), SHA256 = Utilities.CleanHashData(reader.GetAttribute("sha256"), Constants.SHA256Length), SHA384 = Utilities.CleanHashData(reader.GetAttribute("sha384"), Constants.SHA384Length), SHA512 = Utilities.CleanHashData(reader.GetAttribute("sha512"), Constants.SHA512Length), Offset = reader.GetAttribute("offset"), // Value = reader.GetAttribute("value"); ItemStatus = Utilities.GetItemStatus(reader.GetAttribute("status")), // LoadFlag = reader.GetAttribute("loadflag"), // (load16_byte|load16_word|load16_word_swap|load32_byte|load32_word|load32_word_swap|load32_dword|load64_word|load64_word_swap|reload|fill|continue|reload_plain|ignore) AreaName = areaname, AreaSize = areasize, Features = features, PartName = partname, PartInterface = partinterface, SystemID = sysid, System = filename, SourceID = srcid, }; rom.CopyMachineInformation(machine); // Now process and add the rom key = ParseAddHelper(rom, clean, remUnicode); reader.Read(); break; default: reader.Read(); break; } } return containsItems; } /// /// Read diskarea information /// /// XmlReader representing a diskarea block /// Machine information to pass to contained items /// List of features from the parent part /// Name of the containing area /// Size of the containing area /// Name of the containing part /// Interface of the containing part /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) private bool ReadDiskArea( XmlReader reader, Machine machine, List> features, string areaname, long? areasize, string partname, string partinterface, // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { string key = ""; string temptype = reader.Name; bool containsItems = false; 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": containsItems = true; DatItem disk = new Disk { Name = reader.GetAttribute("name"), MD5 = Utilities.CleanHashData(reader.GetAttribute("md5"), Constants.MD5Length), SHA1 = Utilities.CleanHashData(reader.GetAttribute("sha1"), Constants.SHA1Length), SHA256 = Utilities.CleanHashData(reader.GetAttribute("sha256"), Constants.SHA256Length), SHA384 = Utilities.CleanHashData(reader.GetAttribute("sha384"), Constants.SHA384Length), SHA512 = Utilities.CleanHashData(reader.GetAttribute("sha512"), Constants.SHA512Length), ItemStatus = Utilities.GetItemStatus(reader.GetAttribute("status")), Writable = Utilities.GetYesNo(reader.GetAttribute("writable")), AreaName = areaname, AreaSize = areasize, Features = features, PartName = partname, PartInterface = partinterface, SystemID = sysid, System = filename, SourceID = srcid, }; disk.CopyMachineInformation(machine); // Now process and add the rom key = ParseAddHelper(disk, clean, remUnicode); reader.Read(); break; default: reader.Read(); break; } } return containsItems; } /// /// Create and open an output file for writing direct from a dictionary /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) /// True if the DAT was written correctly, false otherwise public override bool WriteToFile(string outfile, bool ignoreblanks = false) { try { Globals.Logger.User("Opening file for writing: {0}", outfile); FileStream fs = Utilities.TryCreate(outfile); // If we get back null for some reason, just log and return if (fs == null) { Globals.Logger.Warning("File '{0}' could not be created for writing! Please check to see if the file is writable", outfile); return false; } StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false)); // Write out the header WriteHeader(sw); // Write out each of the machines and roms string lastgame = null; // Get a properly sorted set of keys List keys = Keys; keys.Sort(new NaturalComparer()); foreach (string key in keys) { List roms = this[key]; // 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 if (rom.Name == null || rom.MachineName == null) { 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 if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) { WriteEndGame(sw); } // If we have a new game, output the beginning of the new item if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) { WriteStartGame(sw, rom); } // If we have a "null" game (created by DATFromDir or something similar), log it to file if (rom.Type == ItemType.Rom && ((Rom)rom).Size == -1 && ((Rom)rom).CRC == "null") { Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName); lastgame = rom.MachineName; continue; } // Now, output the rom data WriteDatItem(sw, rom, ignoreblanks); // Set the new data to compare against lastgame = rom.MachineName; } } // Write the file footer out WriteFooter(sw); Globals.Logger.Verbose("File written!" + Environment.NewLine); sw.Dispose(); fs.Dispose(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out DAT header using the supplied StreamWriter /// /// StreamWriter to output to /// True if the data was written, false on error private bool WriteHeader(StreamWriter sw) { try { string header = "\n" + "\n\n" + "\n\n"; // Write the header out sw.Write(header); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out Game start using the supplied StreamWriter /// /// StreamWriter to output to /// DatItem object to be output /// True if the data was written, false on error private bool WriteStartGame(StreamWriter sw, DatItem rom) { try { // No game should start with a path separator if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString())) { rom.MachineName = rom.MachineName.Substring(1); } string state = "\t\n" : "") + (!ExcludeFields[(int)Field.Description] ? "\t\t" + HttpUtility.HtmlEncode(rom.MachineDescription) + "\n" : "") + (!ExcludeFields[(int)Field.Year] && rom.Year != null ? "\t\t" + HttpUtility.HtmlEncode(rom.Year) + "\n" : "") + (!ExcludeFields[(int)Field.Publisher] && rom.Publisher != null ? "\t\t" + HttpUtility.HtmlEncode(rom.Publisher) + "\n" : ""); if (!ExcludeFields[(int)Field.Infos]) { foreach (Tuple kvp in rom.Infos) { state += "\t\t\n"; } } sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out Game start using the supplied StreamWriter /// /// StreamWriter to output to /// True if the data was written, false on error private bool WriteEndGame(StreamWriter sw) { try { string state = "\t\n\n"; sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out DatItem using the supplied StreamWriter /// /// StreamWriter to output to /// DatItem object to be output /// True if blank roms should be skipped on output, false otherwise (default) /// True if the data was written, false on error private bool WriteDatItem(StreamWriter sw, DatItem rom, bool ignoreblanks = false) { // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip if (ignoreblanks && (rom.Type == ItemType.Rom && (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1))) { return true; } try { string state = ""; // Pre-process the item name ProcessItemName(rom, true); state += "\t\t\n"; if (!ExcludeFields[(int)Field.Features]) { foreach (Tuple kvp in rom.Features) { state += "\t\t\t\n"; } } switch (rom.Type) { case ItemType.Disk: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.Rom: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; } state += "\t\t\n"; sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out DAT footer using the supplied StreamWriter /// /// StreamWriter to output to /// True if the data was written, false on error private bool WriteFooter(StreamWriter sw) { try { string footer = "\t\n\n\n"; // Write the footer out sw.Write(footer); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } } }