diff --git a/SabreTools.Library/DatFiles/AttractMode.cs b/SabreTools.Library/DatFiles/AttractMode.cs index 3b47cf67..0582f7d9 100644 --- a/SabreTools.Library/DatFiles/AttractMode.cs +++ b/SabreTools.Library/DatFiles/AttractMode.cs @@ -29,7 +29,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -62,9 +63,7 @@ namespace SabreTools.Library.DatFiles catch (InvalidDataException ex) { Globals.Logger.Warning($"Malformed line found in '{filename}' at line {svr.LineNumber}"); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; continue; } @@ -117,8 +116,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -170,9 +170,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -183,46 +181,32 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// SeparatedValueWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(SeparatedValueWriter svw) + private void WriteHeader(SeparatedValueWriter svw) { - try + string[] headers = new string[] { - string[] headers = new string[] - { - "#Name", - "Title", - "Emulator", - "CloneOf", - "Year", - "Manufacturer", - "Category", - "Players", - "Rotation", - "Control", - "Status", - "DisplayCount", - "DisplayType", - "AltRomname", - "AltTitle", - "Extra", - "Buttons", - }; + "#Name", + "Title", + "Emulator", + "CloneOf", + "Year", + "Manufacturer", + "Category", + "Players", + "Rotation", + "Control", + "Status", + "DisplayCount", + "DisplayType", + "AltRomname", + "AltTitle", + "Extra", + "Buttons", + }; - svw.WriteHeader(headers); + svw.WriteHeader(headers); - svw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + svw.Flush(); } /// @@ -230,24 +214,22 @@ namespace SabreTools.Library.DatFiles /// /// SeparatedValueWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem) { - try + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - - // Pre-process the item name - ProcessItemName(datItem, true); - - // Build the state - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - string[] fields = new string[] - { + case ItemType.Rom: + var rom = datItem as Rom; + string[] fields = new string[] + { rom.Machine.Name, rom.Machine.Description, Header.FileName, @@ -265,24 +247,13 @@ namespace SabreTools.Library.DatFiles rom.AltTitle, rom.Machine.Comment, rom.Machine.Buttons, - }; + }; - svw.WriteValues(fields); - break; - } - - svw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + svw.WriteValues(fields); + break; } - return true; + svw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/ClrMamePro.cs b/SabreTools.Library/DatFiles/ClrMamePro.cs index 83bca57a..bcbb295c 100644 --- a/SabreTools.Library/DatFiles/ClrMamePro.cs +++ b/SabreTools.Library/DatFiles/ClrMamePro.cs @@ -31,7 +31,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -412,8 +413,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -480,9 +482,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -493,41 +493,27 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// ClrMameProWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(ClrMameProWriter cmpw) + private void WriteHeader(ClrMameProWriter cmpw) { - try - { - cmpw.WriteStartElement("clrmamepro"); + cmpw.WriteStartElement("clrmamepro"); - cmpw.WriteRequiredStandalone("name", Header.Name); - cmpw.WriteRequiredStandalone("description", Header.Description); - cmpw.WriteOptionalStandalone("category", Header.Category); - cmpw.WriteRequiredStandalone("version", Header.Version); - cmpw.WriteOptionalStandalone("date", Header.Date); - cmpw.WriteRequiredStandalone("author", Header.Author); - cmpw.WriteOptionalStandalone("email", Header.Email); - cmpw.WriteOptionalStandalone("homepage", Header.Homepage); - cmpw.WriteOptionalStandalone("url", Header.Url); - cmpw.WriteOptionalStandalone("comment", Header.Comment); - cmpw.WriteOptionalStandalone("forcezipping", Header.ForcePacking.FromPackingFlag(true), false); - cmpw.WriteOptionalStandalone("forcemerging", Header.ForceMerging.FromMergingFlag(false), false); + cmpw.WriteRequiredStandalone("name", Header.Name); + cmpw.WriteRequiredStandalone("description", Header.Description); + cmpw.WriteOptionalStandalone("category", Header.Category); + cmpw.WriteRequiredStandalone("version", Header.Version); + cmpw.WriteOptionalStandalone("date", Header.Date); + cmpw.WriteRequiredStandalone("author", Header.Author); + cmpw.WriteOptionalStandalone("email", Header.Email); + cmpw.WriteOptionalStandalone("homepage", Header.Homepage); + cmpw.WriteOptionalStandalone("url", Header.Url); + cmpw.WriteOptionalStandalone("comment", Header.Comment); + cmpw.WriteOptionalStandalone("forcezipping", Header.ForcePacking.FromPackingFlag(true), false); + cmpw.WriteOptionalStandalone("forcemerging", Header.ForceMerging.FromMergingFlag(false), false); - // End clrmamepro - cmpw.WriteEndElement(); + // End clrmamepro + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// @@ -535,37 +521,23 @@ namespace SabreTools.Library.DatFiles /// /// ClrMameProWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(ClrMameProWriter cmpw, DatItem datItem) + private void WriteStartGame(ClrMameProWriter cmpw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // Build the state - cmpw.WriteStartElement(datItem.Machine.MachineType == MachineType.Bios ? "resource" : "game"); + // Build the state + cmpw.WriteStartElement(datItem.Machine.MachineType == MachineType.Bios ? "resource" : "game"); - cmpw.WriteRequiredStandalone("name", datItem.Machine.Name); - cmpw.WriteOptionalStandalone("romof", datItem.Machine.RomOf); - cmpw.WriteOptionalStandalone("cloneof", datItem.Machine.CloneOf); - cmpw.WriteOptionalStandalone("description", datItem.Machine.Description ?? datItem.Machine.Name); - cmpw.WriteOptionalStandalone("year", datItem.Machine.Year); - cmpw.WriteOptionalStandalone("manufacturer", datItem.Machine.Manufacturer); - cmpw.WriteOptionalStandalone("category", datItem.Machine.Category); + cmpw.WriteRequiredStandalone("name", datItem.Machine.Name); + cmpw.WriteOptionalStandalone("romof", datItem.Machine.RomOf); + cmpw.WriteOptionalStandalone("cloneof", datItem.Machine.CloneOf); + cmpw.WriteOptionalStandalone("description", datItem.Machine.Description ?? datItem.Machine.Name); + cmpw.WriteOptionalStandalone("year", datItem.Machine.Year); + cmpw.WriteOptionalStandalone("manufacturer", datItem.Machine.Manufacturer); + cmpw.WriteOptionalStandalone("category", datItem.Machine.Category); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// @@ -573,29 +545,15 @@ namespace SabreTools.Library.DatFiles /// /// ClrMameProWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteEndGame(ClrMameProWriter cmpw, DatItem datItem) + private void WriteEndGame(ClrMameProWriter cmpw, DatItem datItem) { - try - { - // Build the state - cmpw.WriteOptionalStandalone("sampleof", datItem.Machine.SampleOf); + // Build the state + cmpw.WriteOptionalStandalone("sampleof", datItem.Machine.SampleOf); - // End game - cmpw.WriteEndElement(); + // End game + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// @@ -604,131 +562,103 @@ namespace SabreTools.Library.DatFiles /// DatFile to write out from /// ClrMameProWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem) + private void WriteDatItem(ClrMameProWriter cmpw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); + case ItemType.Archive: + var archive = datItem as Archive; + cmpw.WriteStartElement("archive"); + cmpw.WriteRequiredAttributeString("name", archive.Name); + cmpw.WriteEndElement(); + break; - // Build the state - switch (datItem.ItemType) - { - case ItemType.Archive: - var archive = datItem as Archive; - cmpw.WriteStartElement("archive"); - cmpw.WriteRequiredAttributeString("name", archive.Name); - cmpw.WriteEndElement(); - break; + case ItemType.BiosSet: + var biosSet = datItem as BiosSet; + cmpw.WriteStartElement("biosset"); + cmpw.WriteRequiredAttributeString("name", biosSet.Name); + cmpw.WriteOptionalAttributeString("description", biosSet.Description); + cmpw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant()); + cmpw.WriteEndElement(); + break; - case ItemType.BiosSet: - var biosSet = datItem as BiosSet; - cmpw.WriteStartElement("biosset"); - cmpw.WriteRequiredAttributeString("name", biosSet.Name); - cmpw.WriteOptionalAttributeString("description", biosSet.Description); - cmpw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant()); - cmpw.WriteEndElement(); - break; + case ItemType.Disk: + var disk = datItem as Disk; + cmpw.WriteStartElement("disk"); + cmpw.WriteRequiredAttributeString("name", disk.Name); + cmpw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("flags", disk.ItemStatus.FromItemStatus(false)); + cmpw.WriteEndElement(); + break; - case ItemType.Disk: - var disk = datItem as Disk; - cmpw.WriteStartElement("disk"); - cmpw.WriteRequiredAttributeString("name", disk.Name); - cmpw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("flags", disk.ItemStatus.FromItemStatus(false)); - cmpw.WriteEndElement(); - break; + case ItemType.Media: + var media = datItem as Media; + cmpw.WriteStartElement("media"); + cmpw.WriteRequiredAttributeString("name", media.Name); + cmpw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant()); + cmpw.WriteEndElement(); + break; - case ItemType.Media: - var media = datItem as Media; - cmpw.WriteStartElement("media"); - cmpw.WriteRequiredAttributeString("name", media.Name); - cmpw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant()); - cmpw.WriteEndElement(); - break; + case ItemType.Release: + var release = datItem as Release; + cmpw.WriteStartElement("release"); + cmpw.WriteRequiredAttributeString("name", release.Name); + cmpw.WriteOptionalAttributeString("region", release.Region); + cmpw.WriteOptionalAttributeString("language", release.Language); + cmpw.WriteOptionalAttributeString("date", release.Date); + cmpw.WriteOptionalAttributeString("default", release.Default?.ToString().ToLowerInvariant()); + cmpw.WriteEndElement(); + break; - case ItemType.Release: - var release = datItem as Release; - cmpw.WriteStartElement("release"); - cmpw.WriteRequiredAttributeString("name", release.Name); - cmpw.WriteOptionalAttributeString("region", release.Region); - cmpw.WriteOptionalAttributeString("language", release.Language); - cmpw.WriteOptionalAttributeString("date", release.Date); - cmpw.WriteOptionalAttributeString("default", release.Default?.ToString().ToLowerInvariant()); - cmpw.WriteEndElement(); - break; - - case ItemType.Rom: - var rom = datItem as Rom; - cmpw.WriteStartElement("rom"); - cmpw.WriteRequiredAttributeString("name", rom.Name); - cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString()); - cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant()); + case ItemType.Rom: + var rom = datItem as Rom; + cmpw.WriteStartElement("rom"); + cmpw.WriteRequiredAttributeString("name", rom.Name); + cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString()); + cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant()); #if NET_FRAMEWORK - cmpw.WriteOptionalAttributeString("ripemd160", rom.RIPEMD160?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("ripemd160", rom.RIPEMD160?.ToLowerInvariant()); #endif - cmpw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("spamsum", rom.SpamSum?.ToLowerInvariant()); - cmpw.WriteOptionalAttributeString("date", rom.Date); - cmpw.WriteOptionalAttributeString("flags", rom.ItemStatus.FromItemStatus(false)); - cmpw.WriteEndElement(); - break; + cmpw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("spamsum", rom.SpamSum?.ToLowerInvariant()); + cmpw.WriteOptionalAttributeString("date", rom.Date); + cmpw.WriteOptionalAttributeString("flags", rom.ItemStatus.FromItemStatus(false)); + cmpw.WriteEndElement(); + break; - case ItemType.Sample: - var sample = datItem as Sample; - cmpw.WriteStartElement("sample"); - cmpw.WriteRequiredAttributeString("name", sample.Name); - cmpw.WriteEndElement(); - break; - } - - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.Sample: + var sample = datItem as Sample; + cmpw.WriteStartElement("sample"); + cmpw.WriteRequiredAttributeString("name", sample.Name); + cmpw.WriteEndElement(); + break; } - return true; + cmpw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// ClrMameProWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(ClrMameProWriter cmpw) + private void WriteFooter(ClrMameProWriter cmpw) { - try - { - // End game - cmpw.WriteEndElement(); + // End game + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index 17132214..92fac963 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -1823,10 +1823,16 @@ namespace SabreTools.Library.DatFiles /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if original extension should be kept, false otherwise (default) - public void Parse(string filename, int indexId = 0, bool keep = false, bool keepext = false) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + public void Parse( + string filename, + int indexId = 0, + bool keep = false, + bool keepext = false, + bool throwOnError = false) { ParentablePath path = new ParentablePath(filename.Trim('"')); - Parse(path, indexId, keep, keepext); + Parse(path, indexId, keep, keepext, throwOnError); } /// @@ -1836,7 +1842,13 @@ namespace SabreTools.Library.DatFiles /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if original extension should be kept, false otherwise (default) - public void Parse(ParentablePath filename, int indexId = 0, bool keep = false, bool keepext = false) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + public void Parse( + ParentablePath filename, + int indexId = 0, + bool keep = false, + bool keepext = false, + bool throwOnError = false) { // Get the current path from the filename string currentPath = filename.CurrentPath; @@ -1855,13 +1867,12 @@ namespace SabreTools.Library.DatFiles // Now parse the correct type of DAT try { - Create(currentPath.GetDatFormat(), this)?.ParseFile(currentPath, indexId, keep); + Create(currentPath.GetDatFormat(), this)?.ParseFile(currentPath, indexId, keep, throwOnError); } catch (Exception ex) { Globals.Logger.Error($"Error with file '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; } } @@ -1976,7 +1987,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected abstract void ParseFile(string filename, int indexId, bool keep); + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected abstract void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false); #endregion @@ -3452,8 +3464,14 @@ namespace SabreTools.Library.DatFiles /// True if DAT statistics should be output on write, false otherwise (default) /// True if blank roms should be skipped on output, false otherwise (default) /// True if files should be overwritten (default), false if they should be renamed instead + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public bool Write(string outDir, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) + public bool Write(string outDir, + bool norename = true, + bool stats = false, + bool ignoreblanks = false, + bool overwrite = true, + bool throwOnError = false) { // If we have nothing writable, abort if (!HasWritable()) @@ -3504,7 +3522,7 @@ namespace SabreTools.Library.DatFiles string outfile = outfiles[datFormat]; try { - Create(datFormat, this)?.WriteToFile(outfile, ignoreblanks); + Create(datFormat, this)?.WriteToFile(outfile, ignoreblanks, throwOnError); } catch (Exception ex) { @@ -3532,8 +3550,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public abstract bool WriteToFile(string outfile, bool ignoreblanks = false); + public abstract bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false); /// /// Create a prefix or postfix from inputs diff --git a/SabreTools.Library/DatFiles/DosCenter.cs b/SabreTools.Library/DatFiles/DosCenter.cs index 8ce1ed30..def44683 100644 --- a/SabreTools.Library/DatFiles/DosCenter.cs +++ b/SabreTools.Library/DatFiles/DosCenter.cs @@ -31,7 +31,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -246,14 +247,14 @@ namespace SabreTools.Library.DatFiles } } - /// /// 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 error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -322,9 +323,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -335,36 +334,22 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// ClrMameProWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(ClrMameProWriter cmpw) + private void WriteHeader(ClrMameProWriter cmpw) { - try - { - // Build the state - cmpw.WriteStartElement("DOSCenter"); + // Build the state + cmpw.WriteStartElement("DOSCenter"); - cmpw.WriteRequiredStandalone("Name:", Header.Name, false); - cmpw.WriteRequiredStandalone("Description:", Header.Description, false); - cmpw.WriteRequiredStandalone("Version:", Header.Version, false); - cmpw.WriteRequiredStandalone("Date:", Header.Date, false); - cmpw.WriteRequiredStandalone("Author:", Header.Author, false); - cmpw.WriteRequiredStandalone("Homepage:", Header.Homepage, false); - cmpw.WriteRequiredStandalone("Comment:", Header.Comment, false); + cmpw.WriteRequiredStandalone("Name:", Header.Name, false); + cmpw.WriteRequiredStandalone("Description:", Header.Description, false); + cmpw.WriteRequiredStandalone("Version:", Header.Version, false); + cmpw.WriteRequiredStandalone("Date:", Header.Date, false); + cmpw.WriteRequiredStandalone("Author:", Header.Author, false); + cmpw.WriteRequiredStandalone("Homepage:", Header.Homepage, false); + cmpw.WriteRequiredStandalone("Comment:", Header.Comment, false); - cmpw.WriteEndElement(); + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// @@ -372,56 +357,28 @@ namespace SabreTools.Library.DatFiles /// /// ClrMameProWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(ClrMameProWriter cmpw, DatItem datItem) + private void WriteStartGame(ClrMameProWriter cmpw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // Build the state - cmpw.WriteStartElement("game"); - cmpw.WriteRequiredStandalone("name", $"{datItem.Machine.Name}.zip", true); + // Build the state + cmpw.WriteStartElement("game"); + cmpw.WriteRequiredStandalone("name", $"{datItem.Machine.Name}.zip", true); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// /// Write out Game end using the supplied StreamWriter /// /// ClrMameProWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(ClrMameProWriter cmpw) + private void WriteEndGame(ClrMameProWriter cmpw) { - try - { - // End game - cmpw.WriteEndElement(); + // End game + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } /// @@ -429,40 +386,26 @@ namespace SabreTools.Library.DatFiles /// /// ClrMameProWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem) + private void WriteDatItem(ClrMameProWriter cmpw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); - - // Build the state - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - cmpw.WriteStartElement("file"); - cmpw.WriteRequiredAttributeString("name", rom.Name); - cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString()); - cmpw.WriteOptionalAttributeString("date", rom.Date); - cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); - cmpw.WriteEndElement(); - break; - } - - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.Rom: + var rom = datItem as Rom; + cmpw.WriteStartElement("file"); + cmpw.WriteRequiredAttributeString("name", rom.Name); + cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString()); + cmpw.WriteOptionalAttributeString("date", rom.Date); + cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); + cmpw.WriteEndElement(); + break; } - return true; + cmpw.Flush(); } /// @@ -470,25 +413,12 @@ namespace SabreTools.Library.DatFiles /// /// ClrMameProWriter to output to /// True if the data was written, false on error - private bool WriteFooter(ClrMameProWriter cmpw) + private void WriteFooter(ClrMameProWriter cmpw) { - try - { - // End game - cmpw.WriteEndElement(); + // End game + cmpw.WriteEndElement(); - cmpw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + cmpw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/EverdriveSmdb.cs b/SabreTools.Library/DatFiles/EverdriveSmdb.cs index d56fd60a..36fde7a4 100644 --- a/SabreTools.Library/DatFiles/EverdriveSmdb.cs +++ b/SabreTools.Library/DatFiles/EverdriveSmdb.cs @@ -30,7 +30,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -87,8 +88,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { diff --git a/SabreTools.Library/DatFiles/Hashfile.cs b/SabreTools.Library/DatFiles/Hashfile.cs index 09b8077f..9df45571 100644 --- a/SabreTools.Library/DatFiles/Hashfile.cs +++ b/SabreTools.Library/DatFiles/Hashfile.cs @@ -34,7 +34,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -102,8 +103,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -152,9 +154,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -162,201 +162,187 @@ namespace SabreTools.Library.DatFiles } /// - /// Write out DatItem using the supplied StreamWriter + /// Write out DatItem using the supplied SeparatedValueWriter /// /// SeparatedValueWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem) + private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem) { - try + // Build the state + string[] fields = new string[2]; + + // Get the name field + string name = string.Empty; + switch (datItem.ItemType) { - // Build the state - string[] fields = new string[2]; + case ItemType.Disk: + var disk = datItem as Disk; + if (Header.GameName) + name = $"{disk.Machine.Name}{Path.DirectorySeparatorChar}"; - // Get the name field - string name = string.Empty; - switch (datItem.ItemType) - { - case ItemType.Disk: - var disk = datItem as Disk; - if (Header.GameName) - name = $"{disk.Machine.Name}{Path.DirectorySeparatorChar}"; + name += disk.Name; + break; - name += disk.Name; - break; + case ItemType.Media: + var media = datItem as Media; + if (Header.GameName) + name = $"{media.Machine.Name}{Path.DirectorySeparatorChar}"; - case ItemType.Media: - var media = datItem as Media; - if (Header.GameName) - name = $"{media.Machine.Name}{Path.DirectorySeparatorChar}"; + name += media.Name; + break; - name += media.Name; - break; + case ItemType.Rom: + var rom = datItem as Rom; + if (Header.GameName) + name = $"{rom.Machine.Name}{Path.DirectorySeparatorChar}"; - case ItemType.Rom: - var rom = datItem as Rom; - if (Header.GameName) - name = $"{rom.Machine.Name}{Path.DirectorySeparatorChar}"; + name += rom.Name; + break; + } - name += rom.Name; - break; - } + // Get the hash field and set final fields + string hash = string.Empty; + switch (_hash) + { + case Hash.CRC: + switch (datItem.ItemType) + { + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = name; + fields[1] = rom.CRC; + break; + } - // Get the hash field and set final fields - string hash = string.Empty; - switch (_hash) - { - case Hash.CRC: - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = name; - fields[1] = rom.CRC; - break; - } + break; - break; + case Hash.MD5: + switch (datItem.ItemType) + { + case ItemType.Disk: + var disk = datItem as Disk; + fields[0] = disk.MD5; + fields[1] = name; + break; - case Hash.MD5: - switch (datItem.ItemType) - { - case ItemType.Disk: - var disk = datItem as Disk; - fields[0] = disk.MD5; - fields[1] = name; - break; + case ItemType.Media: + var media = datItem as Media; + fields[0] = media.MD5; + fields[1] = name; + break; - case ItemType.Media: - var media = datItem as Media; - fields[0] = media.MD5; - fields[1] = name; - break; + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.MD5; + fields[1] = name; + break; + } - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.MD5; - fields[1] = name; - break; - } - - break; + break; #if NET_FRAMEWORK - case Hash.RIPEMD160: - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.RIPEMD160; - fields[1] = name; - break; - } + case Hash.RIPEMD160: + switch (datItem.ItemType) + { + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.RIPEMD160; + fields[1] = name; + break; + } - break; + break; #endif - case Hash.SHA1: - switch (datItem.ItemType) - { - case ItemType.Disk: - var disk = datItem as Disk; - fields[0] = disk.SHA1; - fields[1] = name; - break; + case Hash.SHA1: + switch (datItem.ItemType) + { + case ItemType.Disk: + var disk = datItem as Disk; + fields[0] = disk.SHA1; + fields[1] = name; + break; - case ItemType.Media: - var media = datItem as Media; - fields[0] = media.SHA1; - fields[1] = name; - break; + case ItemType.Media: + var media = datItem as Media; + fields[0] = media.SHA1; + fields[1] = name; + break; - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.SHA1; - fields[1] = name; - break; - } + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.SHA1; + fields[1] = name; + break; + } - break; + break; - case Hash.SHA256: - switch (datItem.ItemType) - { - case ItemType.Media: - var media = datItem as Media; - fields[0] = media.SHA256; - fields[1] = name; - break; + case Hash.SHA256: + switch (datItem.ItemType) + { + case ItemType.Media: + var media = datItem as Media; + fields[0] = media.SHA256; + fields[1] = name; + break; - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.SHA256; - fields[1] = name; - break; - } + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.SHA256; + fields[1] = name; + break; + } - break; + break; - case Hash.SHA384: - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.SHA384; - fields[1] = name; - break; - } + case Hash.SHA384: + switch (datItem.ItemType) + { + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.SHA384; + fields[1] = name; + break; + } - break; + break; - case Hash.SHA512: - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.SHA512; - fields[1] = name; - break; - } + case Hash.SHA512: + switch (datItem.ItemType) + { + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.SHA512; + fields[1] = name; + break; + } - break; + break; - case Hash.SpamSum: - switch (datItem.ItemType) - { - case ItemType.Media: - var media = datItem as Media; - fields[0] = media.SpamSum; - fields[1] = name; - break; + case Hash.SpamSum: + switch (datItem.ItemType) + { + case ItemType.Media: + var media = datItem as Media; + fields[0] = media.SpamSum; + fields[1] = name; + break; - case ItemType.Rom: - var rom = datItem as Rom; - fields[0] = rom.SpamSum; - fields[1] = name; - break; - } + case ItemType.Rom: + var rom = datItem as Rom; + fields[0] = rom.SpamSum; + fields[1] = name; + break; + } - break; - } - - // If we had at least one field filled in - if (!string.IsNullOrEmpty(fields[0]) || !string.IsNullOrEmpty(fields[1])) - svw.WriteValues(fields); - - svw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + break; } - return true; + // If we had at least one field filled in + if (!string.IsNullOrEmpty(fields[0]) || !string.IsNullOrEmpty(fields[1])) + svw.WriteValues(fields); + + svw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/Listrom.cs b/SabreTools.Library/DatFiles/Listrom.cs index f4c51f2d..9d8431ab 100644 --- a/SabreTools.Library/DatFiles/Listrom.cs +++ b/SabreTools.Library/DatFiles/Listrom.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -32,6 +31,7 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// /// In a new style MAME listrom DAT, each game has the following format: /// @@ -41,7 +41,7 @@ namespace SabreTools.Library.DatFiles /// 6331.sound-u8 32 BAD CRC(1d298cb0) SHA1(bb0bb62365402543e3154b9a77be9c75010e6abc) BAD_DUMP /// 16v8h-blue.u24 279 NO GOOD DUMP KNOWN /// - protected override void ParseFile(string filename, int indexId, bool keep) + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -255,8 +255,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -314,9 +315,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -328,56 +327,28 @@ namespace SabreTools.Library.DatFiles /// /// 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) + private void WriteStartGame(StreamWriter sw, DatItem rom) { - try - { - // No game should start with a path separator - rom.Machine.Name = rom.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + rom.Machine.Name = rom.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // Build the state - sw.Write($"ROMs required for driver \"{rom.Machine.Name}\".\n"); - sw.Write("Name Size Checksum\n"); + // Build the state + sw.Write($"ROMs required for driver \"{rom.Machine.Name}\".\n"); + sw.Write("Name Size Checksum\n"); - sw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + sw.Flush(); } /// /// Write out Game end using the supplied StreamWriter /// /// StreamWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(StreamWriter sw) + private void WriteEndGame(StreamWriter sw) { - try - { - // End driver - sw.Write("\n"); + // End driver + sw.Write("\n"); - sw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + sw.Flush(); } /// @@ -385,96 +356,82 @@ namespace SabreTools.Library.DatFiles /// /// StreamWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(StreamWriter sw, DatItem datItem) + private void WriteDatItem(StreamWriter sw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); + case ItemType.Disk: + var disk = datItem as Disk; - // Build the state - switch (datItem.ItemType) - { - case ItemType.Disk: - var disk = datItem as Disk; + // The name is padded out to a particular length + if (disk.Name.Length < 43) + sw.Write(disk.Name.PadRight(43, ' ')); + else + sw.Write($"{disk.Name} "); - // The name is padded out to a particular length - if (disk.Name.Length < 43) - sw.Write(disk.Name.PadRight(43, ' ')); - else - sw.Write($"{disk.Name} "); + // If we have a baddump, put the first indicator + if (disk.ItemStatus == ItemStatus.BadDump) + sw.Write(" BAD"); - // If we have a baddump, put the first indicator - if (disk.ItemStatus == ItemStatus.BadDump) - sw.Write(" BAD"); + // If we have a nodump, write out the indicator + if (disk.ItemStatus == ItemStatus.Nodump) + sw.Write(" NO GOOD DUMP KNOWN"); - // If we have a nodump, write out the indicator - if (disk.ItemStatus == ItemStatus.Nodump) - sw.Write(" NO GOOD DUMP KNOWN"); + // Otherwise, write out the SHA-1 hash + else if (!string.IsNullOrWhiteSpace(disk.SHA1)) + sw.Write($" SHA1({disk.SHA1 ?? string.Empty})"); - // Otherwise, write out the SHA-1 hash - else if (!string.IsNullOrWhiteSpace(disk.SHA1)) - sw.Write($" SHA1({disk.SHA1 ?? string.Empty})"); + // If we have a baddump, put the second indicator + if (disk.ItemStatus == ItemStatus.BadDump) + sw.Write(" BAD_DUMP"); - // If we have a baddump, put the second indicator - if (disk.ItemStatus == ItemStatus.BadDump) - sw.Write(" BAD_DUMP"); + sw.Write("\n"); + break; - sw.Write("\n"); - break; + case ItemType.Rom: + var rom = datItem as Rom; - case ItemType.Rom: - var rom = datItem as Rom; + // The name is padded out to a particular length + if (rom.Name.Length < 43) + sw.Write(rom.Name.PadRight(43 - rom.Size?.ToString().Length ?? 0, ' ')); + else + sw.Write($"{rom.Name} "); - // The name is padded out to a particular length - if (rom.Name.Length < 43) - sw.Write(rom.Name.PadRight(43 - rom.Size?.ToString().Length ?? 0, ' ')); - else - sw.Write($"{rom.Name} "); + // If we don't have a nodump, write out the size + if (rom.ItemStatus != ItemStatus.Nodump) + sw.Write(rom.Size?.ToString() ?? string.Empty); - // If we don't have a nodump, write out the size - if (rom.ItemStatus != ItemStatus.Nodump) - sw.Write(rom.Size?.ToString() ?? string.Empty); + // If we have a baddump, put the first indicator + if (rom.ItemStatus == ItemStatus.BadDump) + sw.Write(" BAD"); - // If we have a baddump, put the first indicator - if (rom.ItemStatus == ItemStatus.BadDump) - sw.Write(" BAD"); + // If we have a nodump, write out the indicator + if (rom.ItemStatus == ItemStatus.Nodump) + { + sw.Write(" NO GOOD DUMP KNOWN"); + } + // Otherwise, write out the CRC and SHA-1 hashes + else + { + if (!string.IsNullOrWhiteSpace(rom.CRC)) + sw.Write($" CRC({rom.CRC ?? string.Empty})"); + if (!string.IsNullOrWhiteSpace(rom.SHA1)) + sw.Write($" SHA1({rom.SHA1 ?? string.Empty})"); + } - // If we have a nodump, write out the indicator - if (rom.ItemStatus == ItemStatus.Nodump) - { - sw.Write(" NO GOOD DUMP KNOWN"); - } - // Otherwise, write out the CRC and SHA-1 hashes - else - { - if (!string.IsNullOrWhiteSpace(rom.CRC)) - sw.Write($" CRC({rom.CRC ?? string.Empty})"); - if (!string.IsNullOrWhiteSpace(rom.SHA1)) - sw.Write($" SHA1({rom.SHA1 ?? string.Empty})"); - } + // If we have a baddump, put the second indicator + if (rom.ItemStatus == ItemStatus.BadDump) + sw.Write(" BAD_DUMP"); - // If we have a baddump, put the second indicator - if (rom.ItemStatus == ItemStatus.BadDump) - sw.Write(" BAD_DUMP"); - - sw.Write("\n"); - break; - } - - sw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + sw.Write("\n"); + break; } - return true; + sw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/Listxml.cs b/SabreTools.Library/DatFiles/Listxml.cs index 4396e29a..3c26dbe1 100644 --- a/SabreTools.Library/DatFiles/Listxml.cs +++ b/SabreTools.Library/DatFiles/Listxml.cs @@ -32,9 +32,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - /// - /// - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables XmlReader xtr = filename.GetXmlTextReader(); @@ -92,8 +91,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; // For XML errors, just skip the affected node xtr?.Read(); @@ -1133,8 +1131,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -1203,9 +1202,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -1216,30 +1213,16 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try - { - xtw.WriteStartDocument(); + xtw.WriteStartDocument(); - xtw.WriteStartElement("mame"); - xtw.WriteRequiredAttributeString("build", Header.Name); - xtw.WriteOptionalAttributeString("debug", Header.Debug.FromYesNo()); - xtw.WriteOptionalAttributeString("mameconfig", Header.MameConfig); + xtw.WriteStartElement("mame"); + xtw.WriteRequiredAttributeString("build", Header.Name); + xtw.WriteOptionalAttributeString("debug", Header.Debug.FromYesNo()); + xtw.WriteOptionalAttributeString("mameconfig", Header.MameConfig); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -1248,76 +1231,49 @@ namespace SabreTools.Library.DatFiles /// XmlTextWriter to output to /// DatItem object to be output /// True if the data was written, false on error - private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem) + private void WriteStartGame(XmlTextWriter xtw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // Build the state - xtw.WriteStartElement("machine"); - xtw.WriteRequiredAttributeString("name", datItem.Machine.Name); - xtw.WriteOptionalAttributeString("sourcefile", datItem.Machine.SourceFile); + // Build the state + xtw.WriteStartElement("machine"); + xtw.WriteRequiredAttributeString("name", datItem.Machine.Name); + xtw.WriteOptionalAttributeString("sourcefile", datItem.Machine.SourceFile); - 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"); + 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"); - xtw.WriteOptionalAttributeString("runnable", datItem.Machine.Runnable.FromRunnable()); + xtw.WriteOptionalAttributeString("runnable", datItem.Machine.Runnable.FromRunnable()); - 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); + 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); - xtw.WriteOptionalElementString("manufacturer", datItem.Machine.Manufacturer); + xtw.WriteOptionalElementString("description", datItem.Machine.Description); + xtw.WriteOptionalElementString("year", datItem.Machine.Year); + xtw.WriteOptionalElementString("manufacturer", datItem.Machine.Manufacturer); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// /// Write out Game start using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(XmlTextWriter xtw) + private void WriteEndGame(XmlTextWriter xtw) { - try - { - // End machine - xtw.WriteEndElement(); + // End machine + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -1325,409 +1281,381 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); - - // Build the state - switch (datItem.ItemType) - { - case ItemType.Adjuster: - var adjuster = datItem as Adjuster; - xtw.WriteStartElement("adjuster"); - xtw.WriteRequiredAttributeString("name", adjuster.Name); - xtw.WriteOptionalAttributeString("default", adjuster.Default.FromYesNo()); - if (adjuster.Conditions != null) + case ItemType.Adjuster: + var adjuster = datItem as Adjuster; + xtw.WriteStartElement("adjuster"); + xtw.WriteRequiredAttributeString("name", adjuster.Name); + xtw.WriteOptionalAttributeString("default", adjuster.Default.FromYesNo()); + if (adjuster.Conditions != null) + { + foreach (var adjusterCondition in adjuster.Conditions) { - foreach (var adjusterCondition in adjuster.Conditions) - { - xtw.WriteStartElement("condition"); - xtw.WriteOptionalAttributeString("tag", adjusterCondition.Tag); - xtw.WriteOptionalAttributeString("mask", adjusterCondition.Mask); - xtw.WriteOptionalAttributeString("relation", adjusterCondition.Relation.FromRelation()); - xtw.WriteOptionalAttributeString("value", adjusterCondition.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("condition"); + xtw.WriteOptionalAttributeString("tag", adjusterCondition.Tag); + xtw.WriteOptionalAttributeString("mask", adjusterCondition.Mask); + xtw.WriteOptionalAttributeString("relation", adjusterCondition.Relation.FromRelation()); + xtw.WriteOptionalAttributeString("value", adjusterCondition.Value); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.BiosSet: - var biosSet = datItem as BiosSet; - xtw.WriteStartElement("biosset"); - xtw.WriteRequiredAttributeString("name", biosSet.Name); - xtw.WriteOptionalAttributeString("description", biosSet.Description); - xtw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant()); - xtw.WriteEndElement(); - break; + case ItemType.BiosSet: + var biosSet = datItem as BiosSet; + xtw.WriteStartElement("biosset"); + xtw.WriteRequiredAttributeString("name", biosSet.Name); + xtw.WriteOptionalAttributeString("description", biosSet.Description); + xtw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant()); + xtw.WriteEndElement(); + break; - 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()); - xtw.WriteOptionalAttributeString("clock", chip.Clock?.ToString()); - xtw.WriteEndElement(); - break; + 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()); + xtw.WriteOptionalAttributeString("clock", chip.Clock?.ToString()); + xtw.WriteEndElement(); + break; - case ItemType.Condition: - var condition = datItem as Condition; - xtw.WriteStartElement("condition"); - xtw.WriteOptionalAttributeString("tag", condition.Tag); - xtw.WriteOptionalAttributeString("mask", condition.Mask); - xtw.WriteOptionalAttributeString("relation", condition.Relation.FromRelation()); - xtw.WriteOptionalAttributeString("value", condition.Value); - xtw.WriteEndElement(); - break; + case ItemType.Condition: + var condition = datItem as Condition; + xtw.WriteStartElement("condition"); + xtw.WriteOptionalAttributeString("tag", condition.Tag); + xtw.WriteOptionalAttributeString("mask", condition.Mask); + xtw.WriteOptionalAttributeString("relation", condition.Relation.FromRelation()); + xtw.WriteOptionalAttributeString("value", condition.Value); + xtw.WriteEndElement(); + break; - 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); + 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) + if (configuration.Conditions != null) + { + foreach (var configurationCondition in configuration.Conditions) { - foreach (var configurationCondition in configuration.Conditions) - { - xtw.WriteStartElement("condition"); - xtw.WriteOptionalAttributeString("tag", configurationCondition.Tag); - xtw.WriteOptionalAttributeString("mask", configurationCondition.Mask); - xtw.WriteOptionalAttributeString("relation", configurationCondition.Relation.FromRelation()); - xtw.WriteOptionalAttributeString("value", configurationCondition.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("condition"); + xtw.WriteOptionalAttributeString("tag", configurationCondition.Tag); + xtw.WriteOptionalAttributeString("mask", configurationCondition.Mask); + xtw.WriteOptionalAttributeString("relation", configurationCondition.Relation.FromRelation()); + xtw.WriteOptionalAttributeString("value", configurationCondition.Value); + xtw.WriteEndElement(); } - if (configuration.Locations != null) + } + if (configuration.Locations != null) + { + foreach (var location in configuration.Locations) { - foreach (var location in configuration.Locations) - { - xtw.WriteStartElement("conflocation"); - xtw.WriteOptionalAttributeString("name", location.Name); - xtw.WriteOptionalAttributeString("number", location.Number?.ToString()); - xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo()); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("conflocation"); + xtw.WriteOptionalAttributeString("name", location.Name); + xtw.WriteOptionalAttributeString("number", location.Number?.ToString()); + xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo()); + xtw.WriteEndElement(); } - if (configuration.Settings != null) + } + if (configuration.Settings != null) + { + foreach (var setting in configuration.Settings) { - 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.WriteStartElement("confsetting"); + xtw.WriteOptionalAttributeString("name", setting.Name); + xtw.WriteOptionalAttributeString("value", setting.Value); + xtw.WriteOptionalAttributeString("default", setting.Default.FromYesNo()); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.Device: - var device = datItem as Device; - xtw.WriteStartElement("device"); - xtw.WriteOptionalAttributeString("type", device.DeviceType.FromDeviceType()); - xtw.WriteOptionalAttributeString("tag", device.Tag); - xtw.WriteOptionalAttributeString("fixed_image", device.FixedImage); - xtw.WriteOptionalAttributeString("mandatory", device.Mandatory?.ToString()); - xtw.WriteOptionalAttributeString("interface", device.Interface); - if (device.Instances != null) + case ItemType.Device: + var device = datItem as Device; + xtw.WriteStartElement("device"); + xtw.WriteOptionalAttributeString("type", device.DeviceType.FromDeviceType()); + xtw.WriteOptionalAttributeString("tag", device.Tag); + xtw.WriteOptionalAttributeString("fixed_image", device.FixedImage); + xtw.WriteOptionalAttributeString("mandatory", device.Mandatory?.ToString()); + xtw.WriteOptionalAttributeString("interface", device.Interface); + if (device.Instances != null) + { + foreach (var instance in device.Instances) { - foreach (var instance in device.Instances) - { - xtw.WriteStartElement("instance"); - xtw.WriteOptionalAttributeString("name", instance.Name); - xtw.WriteOptionalAttributeString("briefname", instance.BriefName); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("instance"); + xtw.WriteOptionalAttributeString("name", instance.Name); + xtw.WriteOptionalAttributeString("briefname", instance.BriefName); + xtw.WriteEndElement(); } - if (device.Extensions != null) + } + if (device.Extensions != null) + { + foreach (var extension in device.Extensions) { - foreach (var extension in device.Extensions) - { - xtw.WriteStartElement("extension"); - xtw.WriteOptionalAttributeString("name", extension.Name); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("extension"); + xtw.WriteOptionalAttributeString("name", extension.Name); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.DeviceReference: - var deviceRef = datItem as DeviceReference; - xtw.WriteStartElement("device_ref"); - xtw.WriteRequiredAttributeString("name", deviceRef.Name); - xtw.WriteEndElement(); - break; + case ItemType.DeviceReference: + var deviceRef = datItem as DeviceReference; + xtw.WriteStartElement("device_ref"); + xtw.WriteRequiredAttributeString("name", deviceRef.Name); + xtw.WriteEndElement(); + break; - 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) + 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) { - foreach (var dipSwitchCondition in dipSwitch.Conditions) - { - xtw.WriteStartElement("condition"); - xtw.WriteOptionalAttributeString("tag", dipSwitchCondition.Tag); - xtw.WriteOptionalAttributeString("mask", dipSwitchCondition.Mask); - xtw.WriteOptionalAttributeString("relation", dipSwitchCondition.Relation.FromRelation()); - xtw.WriteOptionalAttributeString("value", dipSwitchCondition.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("condition"); + xtw.WriteOptionalAttributeString("tag", dipSwitchCondition.Tag); + xtw.WriteOptionalAttributeString("mask", dipSwitchCondition.Mask); + xtw.WriteOptionalAttributeString("relation", dipSwitchCondition.Relation.FromRelation()); + xtw.WriteOptionalAttributeString("value", dipSwitchCondition.Value); + xtw.WriteEndElement(); } - if (dipSwitch.Locations != null) + } + if (dipSwitch.Locations != null) + { + foreach (var location in dipSwitch.Locations) { - foreach (var location in dipSwitch.Locations) - { - xtw.WriteStartElement("diplocation"); - xtw.WriteOptionalAttributeString("name", location.Name); - xtw.WriteOptionalAttributeString("number", location.Number?.ToString()); - xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo()); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("diplocation"); + xtw.WriteOptionalAttributeString("name", location.Name); + xtw.WriteOptionalAttributeString("number", location.Number?.ToString()); + xtw.WriteOptionalAttributeString("inverted", location.Inverted.FromYesNo()); + xtw.WriteEndElement(); } - if (dipSwitch.Values != null) + } + if (dipSwitch.Values != null) + { + foreach (var value in dipSwitch.Values) { - 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) { - 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) { - foreach (var dipValueCondition in value.Conditions) - { - xtw.WriteStartElement("condition"); - xtw.WriteOptionalAttributeString("tag", dipValueCondition.Tag); - xtw.WriteOptionalAttributeString("mask", dipValueCondition.Mask); - xtw.WriteOptionalAttributeString("relation", dipValueCondition.Relation.FromRelation()); - xtw.WriteOptionalAttributeString("value", dipValueCondition.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("condition"); + xtw.WriteOptionalAttributeString("tag", dipValueCondition.Tag); + xtw.WriteOptionalAttributeString("mask", dipValueCondition.Mask); + xtw.WriteOptionalAttributeString("relation", dipValueCondition.Relation.FromRelation()); + xtw.WriteOptionalAttributeString("value", dipValueCondition.Value); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); } + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.Disk: - var disk = datItem as Disk; - xtw.WriteStartElement("disk"); - xtw.WriteRequiredAttributeString("name", disk.Name); - xtw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("merge", disk.MergeTag); - xtw.WriteOptionalAttributeString("region", disk.Region); - xtw.WriteOptionalAttributeString("index", disk.Index); - xtw.WriteOptionalAttributeString("writable", disk.Writable.FromYesNo()); - xtw.WriteOptionalAttributeString("status", disk.ItemStatus.FromItemStatus(false)); - xtw.WriteOptionalAttributeString("optional", disk.Optional.FromYesNo()); - xtw.WriteEndElement(); - break; + case ItemType.Disk: + var disk = datItem as Disk; + xtw.WriteStartElement("disk"); + xtw.WriteRequiredAttributeString("name", disk.Name); + xtw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("merge", disk.MergeTag); + xtw.WriteOptionalAttributeString("region", disk.Region); + xtw.WriteOptionalAttributeString("index", disk.Index); + xtw.WriteOptionalAttributeString("writable", disk.Writable.FromYesNo()); + xtw.WriteOptionalAttributeString("status", disk.ItemStatus.FromItemStatus(false)); + xtw.WriteOptionalAttributeString("optional", disk.Optional.FromYesNo()); + xtw.WriteEndElement(); + break; - case ItemType.Display: - var display = datItem as Display; - xtw.WriteStartElement("display"); - xtw.WriteOptionalAttributeString("tag", display.Tag); - xtw.WriteOptionalAttributeString("type", display.DisplayType.FromDisplayType()); - xtw.WriteOptionalAttributeString("rotate", display.Rotate?.ToString()); - xtw.WriteOptionalAttributeString("flipx", display.FlipX.FromYesNo()); - xtw.WriteOptionalAttributeString("width", display.Width?.ToString()); - xtw.WriteOptionalAttributeString("height", display.Height?.ToString()); - xtw.WriteOptionalAttributeString("refresh", display.Refresh?.ToString("N6")); - xtw.WriteOptionalAttributeString("pixclock", display.PixClock?.ToString()); - 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()); - xtw.WriteEndElement(); - break; + case ItemType.Display: + var display = datItem as Display; + xtw.WriteStartElement("display"); + xtw.WriteOptionalAttributeString("tag", display.Tag); + xtw.WriteOptionalAttributeString("type", display.DisplayType.FromDisplayType()); + xtw.WriteOptionalAttributeString("rotate", display.Rotate?.ToString()); + xtw.WriteOptionalAttributeString("flipx", display.FlipX.FromYesNo()); + xtw.WriteOptionalAttributeString("width", display.Width?.ToString()); + xtw.WriteOptionalAttributeString("height", display.Height?.ToString()); + xtw.WriteOptionalAttributeString("refresh", display.Refresh?.ToString("N6")); + xtw.WriteOptionalAttributeString("pixclock", display.PixClock?.ToString()); + 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()); + xtw.WriteEndElement(); + break; - 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; + 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; - case ItemType.Feature: - var feature = datItem as Feature; - xtw.WriteStartElement("feature"); - xtw.WriteOptionalAttributeString("type", feature.Type.FromFeatureType()); - xtw.WriteOptionalAttributeString("status", feature.Status.FromFeatureStatus()); - xtw.WriteOptionalAttributeString("overall", feature.Overall.FromFeatureStatus()); - xtw.WriteEndElement(); - break; + case ItemType.Feature: + var feature = datItem as Feature; + xtw.WriteStartElement("feature"); + xtw.WriteOptionalAttributeString("type", feature.Type.FromFeatureType()); + xtw.WriteOptionalAttributeString("status", feature.Status.FromFeatureStatus()); + xtw.WriteOptionalAttributeString("overall", feature.Overall.FromFeatureStatus()); + xtw.WriteEndElement(); + break; - case ItemType.Input: - var input = datItem as Input; - xtw.WriteStartElement("input"); - xtw.WriteOptionalAttributeString("service", input.Service.FromYesNo()); - xtw.WriteOptionalAttributeString("tilt", input.Tilt.FromYesNo()); - xtw.WriteOptionalAttributeString("players", input.Players?.ToString()); - xtw.WriteOptionalAttributeString("coins", input.Coins?.ToString()); - if (input.Controls != null) + case ItemType.Input: + var input = datItem as Input; + xtw.WriteStartElement("input"); + xtw.WriteOptionalAttributeString("service", input.Service.FromYesNo()); + xtw.WriteOptionalAttributeString("tilt", input.Tilt.FromYesNo()); + xtw.WriteOptionalAttributeString("players", input.Players?.ToString()); + xtw.WriteOptionalAttributeString("coins", input.Coins?.ToString()); + if (input.Controls != null) + { + foreach (var control in input.Controls) { - 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()); - xtw.WriteOptionalAttributeString("reverse", control.Reverse.FromYesNo()); - xtw.WriteOptionalAttributeString("ways", control.Ways); - xtw.WriteOptionalAttributeString("ways2", control.Ways2); - xtw.WriteOptionalAttributeString("ways3", control.Ways3); - xtw.WriteEndElement(); - } + 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()); + 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; + } + xtw.WriteEndElement(); + break; - case ItemType.Port: - var port = datItem as Port; - xtw.WriteStartElement("port"); - xtw.WriteOptionalAttributeString("tag", port.Tag); - if (port.Analogs != null) + 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) { - foreach (var analog in port.Analogs) - { - xtw.WriteStartElement("analog"); - xtw.WriteOptionalAttributeString("mask", analog.Mask); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("analog"); + xtw.WriteOptionalAttributeString("mask", analog.Mask); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - 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; + 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; - case ItemType.Rom: - var rom = datItem as Rom; - xtw.WriteStartElement("rom"); - xtw.WriteRequiredAttributeString("name", rom.Name); - xtw.WriteOptionalAttributeString("size", rom.Size?.ToString()); - xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("bios", rom.Bios); - xtw.WriteOptionalAttributeString("merge", rom.MergeTag); - xtw.WriteOptionalAttributeString("region", rom.Region); - xtw.WriteOptionalAttributeString("offset", rom.Offset); - xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false)); - xtw.WriteOptionalAttributeString("optional", rom.Optional.FromYesNo()); - xtw.WriteEndElement(); - break; + case ItemType.Rom: + var rom = datItem as Rom; + xtw.WriteStartElement("rom"); + xtw.WriteRequiredAttributeString("name", rom.Name); + xtw.WriteOptionalAttributeString("size", rom.Size?.ToString()); + xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("bios", rom.Bios); + xtw.WriteOptionalAttributeString("merge", rom.MergeTag); + xtw.WriteOptionalAttributeString("region", rom.Region); + xtw.WriteOptionalAttributeString("offset", rom.Offset); + xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false)); + xtw.WriteOptionalAttributeString("optional", rom.Optional.FromYesNo()); + xtw.WriteEndElement(); + break; - case ItemType.Sample: - var sample = datItem as Sample; - xtw.WriteStartElement("sample"); - xtw.WriteRequiredAttributeString("name", sample.Name); - xtw.WriteEndElement(); - break; + case ItemType.Sample: + var sample = datItem as Sample; + xtw.WriteStartElement("sample"); + xtw.WriteRequiredAttributeString("name", sample.Name); + xtw.WriteEndElement(); + break; - case ItemType.Slot: - var slot = datItem as Slot; - xtw.WriteStartElement("slot"); - xtw.WriteOptionalAttributeString("name", slot.Name); - if (slot.SlotOptions != null) + 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) { - 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.WriteStartElement("slotoption"); + xtw.WriteOptionalAttributeString("name", slotOption.Name); + xtw.WriteOptionalAttributeString("devname", slotOption.DeviceName); + xtw.WriteOptionalAttributeString("default", slotOption.Default.FromYesNo()); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.SoftwareList: - var softwareList = datItem as DatItems.SoftwareList; - xtw.WriteStartElement("softwarelist"); - xtw.WriteRequiredAttributeString("name", softwareList.Name); - xtw.WriteOptionalAttributeString("status", softwareList.Status.FromSoftwareListStatus()); - xtw.WriteOptionalAttributeString("filter", softwareList.Filter); - xtw.WriteEndElement(); - break; + case ItemType.SoftwareList: + var softwareList = datItem as DatItems.SoftwareList; + xtw.WriteStartElement("softwarelist"); + xtw.WriteRequiredAttributeString("name", softwareList.Name); + xtw.WriteOptionalAttributeString("status", softwareList.Status.FromSoftwareListStatus()); + xtw.WriteOptionalAttributeString("filter", softwareList.Filter); + xtw.WriteEndElement(); + break; - case ItemType.Sound: - var sound = datItem as Sound; - xtw.WriteStartElement("sound"); - xtw.WriteOptionalAttributeString("channels", sound.Channels?.ToString()); - xtw.WriteEndElement(); - break; - } - - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.Sound: + var sound = datItem as Sound; + xtw.WriteStartElement("sound"); + xtw.WriteOptionalAttributeString("channels", sound.Channels?.ToString()); + xtw.WriteEndElement(); + break; } - return true; + xtw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End machine - xtw.WriteEndElement(); + // End machine + xtw.WriteEndElement(); - // End mame - xtw.WriteEndElement(); + // End mame + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/Logiqx.cs b/SabreTools.Library/DatFiles/Logiqx.cs index f42dfb2b..8fc97152 100644 --- a/SabreTools.Library/DatFiles/Logiqx.cs +++ b/SabreTools.Library/DatFiles/Logiqx.cs @@ -38,7 +38,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables XmlReader xtr = filename.GetXmlTextReader(); @@ -107,8 +108,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; // For XML errors, just skip the affected node xtr?.Read(); @@ -663,8 +663,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -733,9 +734,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -746,83 +745,69 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try + xtw.WriteStartDocument(); + xtw.WriteDocType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null); + + xtw.WriteStartElement("datafile"); + xtw.WriteOptionalAttributeString("build", Header.Build); + xtw.WriteOptionalAttributeString("debug", Header.Debug.FromYesNo()); + + xtw.WriteStartElement("header"); + + xtw.WriteRequiredElementString("name", Header.Name); + xtw.WriteRequiredElementString("description", Header.Description); + xtw.WriteOptionalElementString("rootdir", Header.RootDir); + xtw.WriteOptionalElementString("category", Header.Category); + xtw.WriteRequiredElementString("version", Header.Version); + xtw.WriteOptionalElementString("date", Header.Date); + xtw.WriteRequiredElementString("author", Header.Author); + xtw.WriteOptionalElementString("email", Header.Email); + xtw.WriteOptionalElementString("homepage", Header.Homepage); + xtw.WriteOptionalElementString("url", Header.Url); + xtw.WriteOptionalElementString("comment", Header.Comment); + xtw.WriteOptionalElementString("type", Header.Type); + + if (Header.ForcePacking != PackingFlag.None + || Header.ForceMerging != MergingFlag.None + || Header.ForceNodump != NodumpFlag.None + || !string.IsNullOrWhiteSpace(Header.HeaderSkipper)) { - xtw.WriteStartDocument(); - xtw.WriteDocType("datafile", "-//Logiqx//DTD ROM Management Datafile//EN", "http://www.logiqx.com/Dats/datafile.dtd", null); + xtw.WriteStartElement("clrmamepro"); - xtw.WriteStartElement("datafile"); - xtw.WriteOptionalAttributeString("build", Header.Build); - xtw.WriteOptionalAttributeString("debug", Header.Debug.FromYesNo()); + xtw.WriteOptionalAttributeString("forcepacking", Header.ForcePacking.FromPackingFlag(false)); + xtw.WriteOptionalAttributeString("forcemerging", Header.ForceMerging.FromMergingFlag(false)); + xtw.WriteOptionalAttributeString("forcenodump", Header.ForceNodump.FromNodumpFlag()); + xtw.WriteOptionalAttributeString("header", Header.HeaderSkipper); - xtw.WriteStartElement("header"); - - xtw.WriteRequiredElementString("name", Header.Name); - xtw.WriteRequiredElementString("description", Header.Description); - xtw.WriteOptionalElementString("rootdir", Header.RootDir); - xtw.WriteOptionalElementString("category", Header.Category); - xtw.WriteRequiredElementString("version", Header.Version); - xtw.WriteOptionalElementString("date", Header.Date); - xtw.WriteRequiredElementString("author", Header.Author); - xtw.WriteOptionalElementString("email", Header.Email); - xtw.WriteOptionalElementString("homepage", Header.Homepage); - xtw.WriteOptionalElementString("url", Header.Url); - xtw.WriteOptionalElementString("comment", Header.Comment); - xtw.WriteOptionalElementString("type", Header.Type); - - if (Header.ForcePacking != PackingFlag.None - || Header.ForceMerging != MergingFlag.None - || Header.ForceNodump != NodumpFlag.None - || !string.IsNullOrWhiteSpace(Header.HeaderSkipper)) - { - xtw.WriteStartElement("clrmamepro"); - - xtw.WriteOptionalAttributeString("forcepacking", Header.ForcePacking.FromPackingFlag(false)); - xtw.WriteOptionalAttributeString("forcemerging", Header.ForceMerging.FromMergingFlag(false)); - xtw.WriteOptionalAttributeString("forcenodump", Header.ForceNodump.FromNodumpFlag()); - xtw.WriteOptionalAttributeString("header", Header.HeaderSkipper); - - // End clrmamepro - xtw.WriteEndElement(); - } - - if (Header.System != null - || Header.RomMode != MergingFlag.None || Header.LockRomMode != null - || Header.BiosMode != MergingFlag.None || Header.LockBiosMode != null - || Header.SampleMode != MergingFlag.None || Header.LockSampleMode != null) - { - xtw.WriteStartElement("romcenter"); - - xtw.WriteOptionalAttributeString("plugin", Header.System); - xtw.WriteOptionalAttributeString("rommode", Header.RomMode.FromMergingFlag(true)); - xtw.WriteOptionalAttributeString("biosmode", Header.BiosMode.FromMergingFlag(true)); - xtw.WriteOptionalAttributeString("samplemode", Header.SampleMode.FromMergingFlag(true)); - xtw.WriteOptionalAttributeString("lockrommode", Header.LockRomMode.FromYesNo()); - xtw.WriteOptionalAttributeString("lockbiosmode", Header.LockBiosMode.FromYesNo()); - xtw.WriteOptionalAttributeString("locksamplemode", Header.LockSampleMode.FromYesNo()); - - // End romcenter - xtw.WriteEndElement(); - } - - // End header + // End clrmamepro xtw.WriteEndElement(); - - xtw.Flush(); } - catch (Exception ex) + + if (Header.System != null + || Header.RomMode != MergingFlag.None || Header.LockRomMode != null + || Header.BiosMode != MergingFlag.None || Header.LockBiosMode != null + || Header.SampleMode != MergingFlag.None || Header.LockSampleMode != null) { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; + xtw.WriteStartElement("romcenter"); - return false; + xtw.WriteOptionalAttributeString("plugin", Header.System); + xtw.WriteOptionalAttributeString("rommode", Header.RomMode.FromMergingFlag(true)); + xtw.WriteOptionalAttributeString("biosmode", Header.BiosMode.FromMergingFlag(true)); + xtw.WriteOptionalAttributeString("samplemode", Header.SampleMode.FromMergingFlag(true)); + xtw.WriteOptionalAttributeString("lockrommode", Header.LockRomMode.FromYesNo()); + xtw.WriteOptionalAttributeString("lockbiosmode", Header.LockBiosMode.FromYesNo()); + xtw.WriteOptionalAttributeString("locksamplemode", Header.LockSampleMode.FromYesNo()); + + // End romcenter + xtw.WriteEndElement(); } - return true; + // End header + xtw.WriteEndElement(); + + xtw.Flush(); } /// @@ -830,111 +815,83 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem) + private void WriteStartGame(XmlTextWriter xtw, DatItem datItem) { - try + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + + // Build the state + xtw.WriteStartElement(_deprecated ? "game" : "machine"); + xtw.WriteRequiredAttributeString("name", datItem.Machine.Name); + + 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"); + + xtw.WriteOptionalAttributeString("runnable", datItem.Machine.Runnable.FromRunnable()); + + 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("comment", datItem.Machine.Comment); + xtw.WriteOptionalElementString("description", datItem.Machine.Description); + xtw.WriteOptionalElementString("year", datItem.Machine.Year); + xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher); + xtw.WriteOptionalElementString("manufacturer", datItem.Machine.Manufacturer); + xtw.WriteOptionalElementString("category", datItem.Machine.Category); + + if (datItem.Machine.TitleID != null + || datItem.Machine.Developer != null + || datItem.Machine.Genre != null + || datItem.Machine.Subgenre != null + || datItem.Machine.Ratings != null + || datItem.Machine.Score != null + || datItem.Machine.Enabled != null + || datItem.Machine.Crc != null + || datItem.Machine.RelatedTo != null) { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + xtw.WriteStartElement("trurip"); - // Build the state - xtw.WriteStartElement(_deprecated ? "game" : "machine"); - xtw.WriteRequiredAttributeString("name", datItem.Machine.Name); - - 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"); - - xtw.WriteOptionalAttributeString("runnable", datItem.Machine.Runnable.FromRunnable()); - - 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("comment", datItem.Machine.Comment); - xtw.WriteOptionalElementString("description", datItem.Machine.Description); - xtw.WriteOptionalElementString("year", datItem.Machine.Year); + xtw.WriteOptionalElementString("titleid", datItem.Machine.TitleID); xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher); - xtw.WriteOptionalElementString("manufacturer", datItem.Machine.Manufacturer); - xtw.WriteOptionalElementString("category", datItem.Machine.Category); + xtw.WriteOptionalElementString("developer", datItem.Machine.Developer); + xtw.WriteOptionalElementString("year", datItem.Machine.Year); + xtw.WriteOptionalElementString("genre", datItem.Machine.Genre); + xtw.WriteOptionalElementString("subgenre", datItem.Machine.Subgenre); + xtw.WriteOptionalElementString("ratings", datItem.Machine.Ratings); + xtw.WriteOptionalElementString("score", datItem.Machine.Score); + xtw.WriteOptionalElementString("players", datItem.Machine.Players); + xtw.WriteOptionalElementString("enabled", datItem.Machine.Enabled); + xtw.WriteOptionalElementString("titleid", datItem.Machine.TitleID); + xtw.WriteOptionalElementString("crc", datItem.Machine.Crc.FromYesNo()); + xtw.WriteOptionalElementString("source", datItem.Machine.SourceFile); + xtw.WriteOptionalElementString("cloneof", datItem.Machine.CloneOf); + xtw.WriteOptionalElementString("relatedto", datItem.Machine.RelatedTo); - if (datItem.Machine.TitleID != null - || datItem.Machine.Developer != null - || datItem.Machine.Genre != null - || datItem.Machine.Subgenre != null - || datItem.Machine.Ratings != null - || datItem.Machine.Score != null - || datItem.Machine.Enabled != null - || datItem.Machine.Crc != null - || datItem.Machine.RelatedTo != null) - { - xtw.WriteStartElement("trurip"); - - xtw.WriteOptionalElementString("titleid", datItem.Machine.TitleID); - xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher); - xtw.WriteOptionalElementString("developer", datItem.Machine.Developer); - xtw.WriteOptionalElementString("year", datItem.Machine.Year); - xtw.WriteOptionalElementString("genre", datItem.Machine.Genre); - xtw.WriteOptionalElementString("subgenre", datItem.Machine.Subgenre); - xtw.WriteOptionalElementString("ratings", datItem.Machine.Ratings); - xtw.WriteOptionalElementString("score", datItem.Machine.Score); - xtw.WriteOptionalElementString("players", datItem.Machine.Players); - xtw.WriteOptionalElementString("enabled", datItem.Machine.Enabled); - xtw.WriteOptionalElementString("titleid", datItem.Machine.TitleID); - xtw.WriteOptionalElementString("crc", datItem.Machine.Crc.FromYesNo()); - xtw.WriteOptionalElementString("source", datItem.Machine.SourceFile); - xtw.WriteOptionalElementString("cloneof", datItem.Machine.CloneOf); - xtw.WriteOptionalElementString("relatedto", datItem.Machine.RelatedTo); - - // End trurip - xtw.WriteEndElement(); - } - - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + // End trurip + xtw.WriteEndElement(); } - return true; + xtw.Flush(); } /// /// Write out Game end using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(XmlTextWriter xtw) + private void WriteEndGame(XmlTextWriter xtw) { - try - { - // End machine - xtw.WriteEndElement(); + // End machine + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -942,135 +899,107 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); + case ItemType.Archive: + var archive = datItem as Archive; + xtw.WriteStartElement("archive"); + xtw.WriteRequiredAttributeString("name", archive.Name); + xtw.WriteEndElement(); + break; - // Build the state - switch (datItem.ItemType) - { - case ItemType.Archive: - var archive = datItem as Archive; - xtw.WriteStartElement("archive"); - xtw.WriteRequiredAttributeString("name", archive.Name); - xtw.WriteEndElement(); - break; + case ItemType.BiosSet: + var biosSet = datItem as BiosSet; + xtw.WriteStartElement("biosset"); + xtw.WriteRequiredAttributeString("name", biosSet.Name); + xtw.WriteOptionalAttributeString("description", biosSet.Description); + xtw.WriteOptionalAttributeString("default", biosSet.Default.FromYesNo()); + xtw.WriteEndElement(); + break; - case ItemType.BiosSet: - var biosSet = datItem as BiosSet; - xtw.WriteStartElement("biosset"); - xtw.WriteRequiredAttributeString("name", biosSet.Name); - xtw.WriteOptionalAttributeString("description", biosSet.Description); - xtw.WriteOptionalAttributeString("default", biosSet.Default.FromYesNo()); - xtw.WriteEndElement(); - break; + case ItemType.Disk: + var disk = datItem as Disk; + 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.WriteEndElement(); + break; - case ItemType.Disk: - var disk = datItem as Disk; - 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.WriteEndElement(); - break; + case ItemType.Media: + var media = datItem as Media; + xtw.WriteStartElement("media"); + xtw.WriteRequiredAttributeString("name", media.Name); + xtw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant()); + xtw.WriteEndElement(); + break; - case ItemType.Media: - var media = datItem as Media; - xtw.WriteStartElement("media"); - xtw.WriteRequiredAttributeString("name", media.Name); - xtw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant()); - xtw.WriteEndElement(); - break; + case ItemType.Release: + var release = datItem as Release; + xtw.WriteStartElement("release"); + xtw.WriteRequiredAttributeString("name", release.Name); + xtw.WriteOptionalAttributeString("region", release.Region); + xtw.WriteOptionalAttributeString("language", release.Language); + xtw.WriteOptionalAttributeString("date", release.Date); + xtw.WriteOptionalAttributeString("default", release.Default.FromYesNo()); + xtw.WriteEndElement(); + break; - case ItemType.Release: - var release = datItem as Release; - xtw.WriteStartElement("release"); - xtw.WriteRequiredAttributeString("name", release.Name); - xtw.WriteOptionalAttributeString("region", release.Region); - xtw.WriteOptionalAttributeString("language", release.Language); - xtw.WriteOptionalAttributeString("date", release.Date); - xtw.WriteOptionalAttributeString("default", release.Default.FromYesNo()); - xtw.WriteEndElement(); - break; - - case ItemType.Rom: - var rom = datItem as Rom; - xtw.WriteStartElement("rom"); - xtw.WriteRequiredAttributeString("name", rom.Name); - xtw.WriteAttributeString("size", rom.Size?.ToString()); - xtw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant()); + case ItemType.Rom: + var rom = datItem as Rom; + xtw.WriteStartElement("rom"); + xtw.WriteRequiredAttributeString("name", rom.Name); + xtw.WriteAttributeString("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()); + 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("spamsum", rom.SpamSum?.ToLowerInvariant()); - xtw.WriteOptionalAttributeString("date", rom.Date); - xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false)); - xtw.WriteOptionalAttributeString("inverted", rom.Inverted.FromYesNo()); - xtw.WriteEndElement(); - break; + 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("spamsum", rom.SpamSum?.ToLowerInvariant()); + xtw.WriteOptionalAttributeString("date", rom.Date); + xtw.WriteOptionalAttributeString("status", rom.ItemStatus.FromItemStatus(false)); + xtw.WriteOptionalAttributeString("inverted", rom.Inverted.FromYesNo()); + xtw.WriteEndElement(); + break; - case ItemType.Sample: - var sample = datItem as Sample; - xtw.WriteStartElement("sample"); - xtw.WriteRequiredAttributeString("name", sample.Name); - xtw.WriteEndElement(); - break; - } - - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.Sample: + var sample = datItem as Sample; + xtw.WriteStartElement("sample"); + xtw.WriteRequiredAttributeString("name", sample.Name); + xtw.WriteEndElement(); + break; } - return true; + xtw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End machine - xtw.WriteEndElement(); + // End machine + xtw.WriteEndElement(); - // End datafile - xtw.WriteEndElement(); + // End datafile + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/Missfile.cs b/SabreTools.Library/DatFiles/Missfile.cs index 2adf6ce7..e4832893 100644 --- a/SabreTools.Library/DatFiles/Missfile.cs +++ b/SabreTools.Library/DatFiles/Missfile.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using SabreTools.Library.Data; @@ -30,7 +29,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // There is no consistent way to parse a missfile... throw new NotImplementedException(); @@ -41,8 +41,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -92,9 +93,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -107,37 +106,23 @@ namespace SabreTools.Library.DatFiles /// StreamWriter to output to /// DatItem object to be output /// The name of the last game to be output - /// True if the data was written, false on error - private bool WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame) + private void WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame) { - try + // Process the item name + ProcessItemName(datItem, false, forceRomName: false); + + // Romba mode automatically uses item name + if (Header.OutputDepot?.IsActive == true || Header.UseRomName) { - // Process the item name - ProcessItemName(datItem, false, forceRomName: false); - - // Romba mode automatically uses item name - if (Header.OutputDepot?.IsActive == true || Header.UseRomName) - { - sw.Write($"{datItem.GetName() ?? string.Empty}\n"); - } - else if (!Header.UseRomName && datItem.Machine.Name != lastgame) - { - sw.Write($"{datItem.Machine.Name}\n"); - lastgame = datItem.Machine.Name; - } - - sw.Flush(); + sw.Write($"{datItem.GetName() ?? string.Empty}\n"); } - catch (Exception ex) + else if (!Header.UseRomName && datItem.Machine.Name != lastgame) { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + sw.Write($"{datItem.Machine.Name}\n"); + lastgame = datItem.Machine.Name; } - return true; + sw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/OfflineList.cs b/SabreTools.Library/DatFiles/OfflineList.cs index 64c3ef5b..e7af968e 100644 --- a/SabreTools.Library/DatFiles/OfflineList.cs +++ b/SabreTools.Library/DatFiles/OfflineList.cs @@ -32,7 +32,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { XmlReader xtr = filename.GetXmlTextReader(); @@ -78,8 +79,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; // For XML errors, just skip the affected node xtr?.Read(); @@ -659,8 +659,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -721,9 +722,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -734,127 +733,113 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try + xtw.WriteStartDocument(false); + + xtw.WriteStartElement("dat"); + xtw.WriteAttributeString("xsi", "xmlns", "http://www.w3.org/2001/XMLSchema-instance"); + xtw.WriteAttributeString("noNamespaceSchemaLocation", "xsi", "datas.xsd"); + + xtw.WriteStartElement("configuration"); + xtw.WriteRequiredElementString("datName", Header.Name); + xtw.WriteElementString("datVersion", Items.TotalCount.ToString()); + xtw.WriteRequiredElementString("system", Header.System); + xtw.WriteRequiredElementString("screenshotsWidth", Header.ScreenshotsWidth); + xtw.WriteRequiredElementString("screenshotsHeight", Header.ScreenshotsHeight); + + if (Header.Infos != null) { - xtw.WriteStartDocument(false); + xtw.WriteStartElement("infos"); - xtw.WriteStartElement("dat"); - xtw.WriteAttributeString("xsi", "xmlns", "http://www.w3.org/2001/XMLSchema-instance"); - xtw.WriteAttributeString("noNamespaceSchemaLocation", "xsi", "datas.xsd"); - - xtw.WriteStartElement("configuration"); - xtw.WriteRequiredElementString("datName", Header.Name); - xtw.WriteElementString("datVersion", Items.TotalCount.ToString()); - xtw.WriteRequiredElementString("system", Header.System); - xtw.WriteRequiredElementString("screenshotsWidth", Header.ScreenshotsWidth); - xtw.WriteRequiredElementString("screenshotsHeight", Header.ScreenshotsHeight); - - if (Header.Infos != null) + foreach (var info in Header.Infos) { - xtw.WriteStartElement("infos"); - - foreach (var info in Header.Infos) - { - xtw.WriteStartElement(info.Name); - xtw.WriteAttributeString("visible", info.Visible?.ToString()); - xtw.WriteAttributeString("inNamingOption", info.InNamingOption?.ToString()); - xtw.WriteAttributeString("default", info.Default?.ToString()); - xtw.WriteEndElement(); - } - - // End infos - xtw.WriteEndElement(); - } - - if (Header.CanOpen != null) - { - xtw.WriteStartElement("canOpen"); - - foreach (string extension in Header.CanOpen) - { - xtw.WriteElementString("extension", extension); - } - - // End canOpen + xtw.WriteStartElement(info.Name); + xtw.WriteAttributeString("visible", info.Visible?.ToString()); + xtw.WriteAttributeString("inNamingOption", info.InNamingOption?.ToString()); + xtw.WriteAttributeString("default", info.Default?.ToString()); xtw.WriteEndElement(); } - xtw.WriteStartElement("newDat"); - xtw.WriteRequiredElementString("datVersionURL", Header.Url); - - xtw.WriteStartElement("datUrl"); - xtw.WriteAttributeString("fileName", $"{Header.FileName ?? string.Empty}.zip"); - xtw.WriteString(Header.Url); + // End infos xtw.WriteEndElement(); - - xtw.WriteRequiredElementString("imURL", Header.Url); - - // End newDat - xtw.WriteEndElement(); - - xtw.WriteStartElement("search"); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "location"); - xtw.WriteAttributeString("default", "true"); - xtw.WriteAttributeString("auto", "true"); - xtw.WriteEndElement(); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "romSize"); - xtw.WriteAttributeString("default", "true"); - xtw.WriteAttributeString("auto", "false"); - xtw.WriteEndElement(); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "languages"); - xtw.WriteAttributeString("default", "true"); - xtw.WriteAttributeString("auto", "true"); - xtw.WriteEndElement(); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "saveType"); - xtw.WriteAttributeString("default", "false"); - xtw.WriteAttributeString("auto", "false"); - xtw.WriteEndElement(); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "publisher"); - xtw.WriteAttributeString("default", "false"); - xtw.WriteAttributeString("auto", "true"); - xtw.WriteEndElement(); - - xtw.WriteStartElement("to"); - xtw.WriteAttributeString("value", "sourceRom"); - xtw.WriteAttributeString("default", "false"); - xtw.WriteAttributeString("auto", "true"); - xtw.WriteEndElement(); - - // End search - xtw.WriteEndElement(); - - xtw.WriteRequiredElementString("romTitle", Header.RomTitle ?? "%u - %n"); - - // End configuration - xtw.WriteEndElement(); - - xtw.WriteStartElement("games"); - - xtw.Flush(); } - catch (Exception ex) + + if (Header.CanOpen != null) { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; + xtw.WriteStartElement("canOpen"); - return false; + foreach (string extension in Header.CanOpen) + { + xtw.WriteElementString("extension", extension); + } + + // End canOpen + xtw.WriteEndElement(); } - return true; + xtw.WriteStartElement("newDat"); + xtw.WriteRequiredElementString("datVersionURL", Header.Url); + + xtw.WriteStartElement("datUrl"); + xtw.WriteAttributeString("fileName", $"{Header.FileName ?? string.Empty}.zip"); + xtw.WriteString(Header.Url); + xtw.WriteEndElement(); + + xtw.WriteRequiredElementString("imURL", Header.Url); + + // End newDat + xtw.WriteEndElement(); + + xtw.WriteStartElement("search"); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "location"); + xtw.WriteAttributeString("default", "true"); + xtw.WriteAttributeString("auto", "true"); + xtw.WriteEndElement(); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "romSize"); + xtw.WriteAttributeString("default", "true"); + xtw.WriteAttributeString("auto", "false"); + xtw.WriteEndElement(); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "languages"); + xtw.WriteAttributeString("default", "true"); + xtw.WriteAttributeString("auto", "true"); + xtw.WriteEndElement(); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "saveType"); + xtw.WriteAttributeString("default", "false"); + xtw.WriteAttributeString("auto", "false"); + xtw.WriteEndElement(); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "publisher"); + xtw.WriteAttributeString("default", "false"); + xtw.WriteAttributeString("auto", "true"); + xtw.WriteEndElement(); + + xtw.WriteStartElement("to"); + xtw.WriteAttributeString("value", "sourceRom"); + xtw.WriteAttributeString("default", "false"); + xtw.WriteAttributeString("auto", "true"); + xtw.WriteEndElement(); + + // End search + xtw.WriteEndElement(); + + xtw.WriteRequiredElementString("romTitle", Header.RomTitle ?? "%u - %n"); + + // End configuration + xtw.WriteEndElement(); + + xtw.WriteStartElement("games"); + + xtw.Flush(); } /// @@ -863,69 +848,56 @@ namespace SabreTools.Library.DatFiles /// XmlTextWriter to output to /// DatItem object to be output /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + xtw.WriteStartElement("game"); + xtw.WriteElementString("imageNumber", "1"); + xtw.WriteElementString("releaseNumber", "1"); + xtw.WriteRequiredElementString("title", datItem.GetName() ?? string.Empty); + xtw.WriteElementString("saveType", "None"); + + if (datItem.ItemType == ItemType.Rom) { - // Pre-process the item name - ProcessItemName(datItem, true); + var rom = datItem as Rom; + xtw.WriteRequiredElementString("romSize", rom.Size?.ToString()); + } - // Build the state - xtw.WriteStartElement("game"); - xtw.WriteElementString("imageNumber", "1"); - xtw.WriteElementString("releaseNumber", "1"); - xtw.WriteRequiredElementString("title", datItem.GetName() ?? string.Empty); - xtw.WriteElementString("saveType", "None"); + xtw.WriteRequiredElementString("publisher", datItem.Machine.Publisher); + xtw.WriteElementString("location", "0"); + xtw.WriteElementString("sourceRom", "None"); + xtw.WriteElementString("language", "0"); - if (datItem.ItemType == ItemType.Rom) + if (datItem.ItemType == ItemType.Rom) + { + var rom = datItem as Rom; + string tempext = "." + PathExtensions.GetNormalizedExtension(rom.Name); + + xtw.WriteStartElement("files"); + if (!string.IsNullOrWhiteSpace(rom.CRC)) { - var rom = datItem as Rom; - xtw.WriteRequiredElementString("romSize", rom.Size?.ToString()); - } - - xtw.WriteRequiredElementString("publisher", datItem.Machine.Publisher); - xtw.WriteElementString("location", "0"); - xtw.WriteElementString("sourceRom", "None"); - xtw.WriteElementString("language", "0"); - - if (datItem.ItemType == ItemType.Rom) - { - var rom = datItem as Rom; - string tempext = "." + PathExtensions.GetNormalizedExtension(rom.Name); - - xtw.WriteStartElement("files"); - if (!string.IsNullOrWhiteSpace(rom.CRC)) - { - xtw.WriteStartElement("romCRC"); - xtw.WriteRequiredAttributeString("extension", tempext); - xtw.WriteString(rom.CRC?.ToUpperInvariant()); - xtw.WriteEndElement(); - } - - // End files + xtw.WriteStartElement("romCRC"); + xtw.WriteRequiredAttributeString("extension", tempext); + xtw.WriteString(rom.CRC?.ToUpperInvariant()); xtw.WriteEndElement(); } - xtw.WriteElementString("im1CRC", "00000000"); - xtw.WriteElementString("im2CRC", "00000000"); - xtw.WriteRequiredElementString("comment", datItem.Machine.Comment); - xtw.WriteRequiredElementString("duplicateID", datItem.Machine.CloneOf); - - // End game + // End files xtw.WriteEndElement(); - - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; } - return true; + xtw.WriteElementString("im1CRC", "00000000"); + xtw.WriteElementString("im2CRC", "00000000"); + xtw.WriteRequiredElementString("comment", datItem.Machine.Comment); + xtw.WriteRequiredElementString("duplicateID", datItem.Machine.CloneOf); + + // End game + xtw.WriteEndElement(); + + xtw.Flush(); } /// @@ -933,54 +905,41 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End games - xtw.WriteEndElement(); + // End games + xtw.WriteEndElement(); - xtw.WriteStartElement("gui"); + xtw.WriteStartElement("gui"); - xtw.WriteStartElement("images"); - xtw.WriteAttributeString("width", "487"); - xtw.WriteAttributeString("height", "162"); + xtw.WriteStartElement("images"); + xtw.WriteAttributeString("width", "487"); + xtw.WriteAttributeString("height", "162"); - xtw.WriteStartElement("image"); - xtw.WriteAttributeString("x", "0"); - xtw.WriteAttributeString("y", "0"); - xtw.WriteAttributeString("width", "240"); - xtw.WriteAttributeString("height", "160"); - xtw.WriteEndElement(); + xtw.WriteStartElement("image"); + xtw.WriteAttributeString("x", "0"); + xtw.WriteAttributeString("y", "0"); + xtw.WriteAttributeString("width", "240"); + xtw.WriteAttributeString("height", "160"); + xtw.WriteEndElement(); - xtw.WriteStartElement("image"); - xtw.WriteAttributeString("x", "245"); - xtw.WriteAttributeString("y", "0"); - xtw.WriteAttributeString("width", "240"); - xtw.WriteAttributeString("height", "160"); - xtw.WriteEndElement(); + xtw.WriteStartElement("image"); + xtw.WriteAttributeString("x", "245"); + xtw.WriteAttributeString("y", "0"); + xtw.WriteAttributeString("width", "240"); + xtw.WriteAttributeString("height", "160"); + xtw.WriteEndElement(); - // End images - xtw.WriteEndElement(); + // End images + xtw.WriteEndElement(); - // End gui - xtw.WriteEndElement(); + // End gui + xtw.WriteEndElement(); - // End dat - xtw.WriteEndElement(); + // End dat + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/OpenMSX.cs b/SabreTools.Library/DatFiles/OpenMSX.cs index cdbf4871..aee395e1 100644 --- a/SabreTools.Library/DatFiles/OpenMSX.cs +++ b/SabreTools.Library/DatFiles/OpenMSX.cs @@ -32,7 +32,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables XmlReader xtr = filename.GetXmlTextReader(); @@ -80,8 +81,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; // For XML errors, just skip the affected node xtr?.Read(); @@ -513,8 +513,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -583,9 +584,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -596,18 +595,15 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try - { - xtw.WriteStartDocument(); - xtw.WriteDocType("softwaredb", null, "softwaredb1.dtd", null); + xtw.WriteStartDocument(); + xtw.WriteDocType("softwaredb", null, "softwaredb1.dtd", null); - xtw.WriteStartElement("softwaredb"); - xtw.WriteRequiredAttributeString("timestamp", Header.Date); + xtw.WriteStartElement("softwaredb"); + xtw.WriteRequiredAttributeString("timestamp", Header.Date); - //TODO: Figure out how to fix the issue with removed formatting after this point + //TODO: Figure out how to fix the issue with removed formatting after this point // xtw.WriteComment("Credits"); // xtw.WriteCData(@"The softwaredb.xml file contains information about rom mapper types @@ -621,18 +617,7 @@ namespace SabreTools.Library.DatFiles //- p_gimeno and diedel for their help adding and valdiating ROM additions //- GDX for additional ROM info and validations and corrections"); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -640,61 +625,33 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem) + private void WriteStartGame(XmlTextWriter xtw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // Build the state - xtw.WriteStartElement("software"); - xtw.WriteRequiredElementString("title", datItem.Machine.Name); - xtw.WriteRequiredElementString("genmsxid", datItem.Machine.GenMSXID); - xtw.WriteRequiredElementString("system", datItem.Machine.System); - xtw.WriteRequiredElementString("company", datItem.Machine.Manufacturer); - xtw.WriteRequiredElementString("year", datItem.Machine.Year); - xtw.WriteRequiredElementString("country", datItem.Machine.Country); + // Build the state + xtw.WriteStartElement("software"); + xtw.WriteRequiredElementString("title", datItem.Machine.Name); + xtw.WriteRequiredElementString("genmsxid", datItem.Machine.GenMSXID); + xtw.WriteRequiredElementString("system", datItem.Machine.System); + xtw.WriteRequiredElementString("company", datItem.Machine.Manufacturer); + xtw.WriteRequiredElementString("year", datItem.Machine.Year); + xtw.WriteRequiredElementString("country", datItem.Machine.Country); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// /// Write out Game start using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(XmlTextWriter xtw) + private void WriteEndGame(XmlTextWriter xtw) { - try - { - // End software - xtw.WriteEndElement(); + // End software + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -702,106 +659,78 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); + case ItemType.Rom: + var rom = datItem as Rom; + xtw.WriteStartElement("dump"); - // Build the state - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - xtw.WriteStartElement("dump"); - - if (rom.Original != null) - { - xtw.WriteStartElement("original"); - xtw.WriteAttributeString("value", rom.Original.Value == true ? "true" : "false"); - xtw.WriteString(rom.Original.Content); - xtw.WriteEndElement(); - } - - switch (rom.OpenMSXSubType) - { - // Default to Rom for converting from other formats - case OpenMSXSubType.Rom: - case OpenMSXSubType.NULL: - xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); - xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalElementString("start", rom.Offset); - xtw.WriteOptionalElementString("type", rom.OpenMSXType); - xtw.WriteOptionalElementString("remark", rom.Remark); - xtw.WriteEndElement(); - break; - - case OpenMSXSubType.MegaRom: - xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); - xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalElementString("start", rom.Offset); - xtw.WriteOptionalElementString("type", rom.OpenMSXType); - xtw.WriteOptionalElementString("remark", rom.Remark); - xtw.WriteEndElement(); - break; - - case OpenMSXSubType.SCCPlusCart: - xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); - xtw.WriteOptionalElementString("boot", rom.Boot); - xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); - xtw.WriteOptionalElementString("remark", rom.Remark); - xtw.WriteEndElement(); - break; - } - - // End dump + if (rom.Original != null) + { + xtw.WriteStartElement("original"); + xtw.WriteAttributeString("value", rom.Original.Value == true ? "true" : "false"); + xtw.WriteString(rom.Original.Content); xtw.WriteEndElement(); - break; - } + } - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; + switch (rom.OpenMSXSubType) + { + // Default to Rom for converting from other formats + case OpenMSXSubType.Rom: + case OpenMSXSubType.NULL: + xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); + xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalElementString("start", rom.Offset); + xtw.WriteOptionalElementString("type", rom.OpenMSXType); + xtw.WriteOptionalElementString("remark", rom.Remark); + xtw.WriteEndElement(); + break; - return false; + case OpenMSXSubType.MegaRom: + xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); + xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalElementString("start", rom.Offset); + xtw.WriteOptionalElementString("type", rom.OpenMSXType); + xtw.WriteOptionalElementString("remark", rom.Remark); + xtw.WriteEndElement(); + break; + + case OpenMSXSubType.SCCPlusCart: + xtw.WriteStartElement(rom.OpenMSXSubType.FromOpenMSXSubType()); + xtw.WriteOptionalElementString("boot", rom.Boot); + xtw.WriteRequiredElementString("hash", rom.SHA1?.ToLowerInvariant()); + xtw.WriteOptionalElementString("remark", rom.Remark); + xtw.WriteEndElement(); + break; + } + + // End dump + xtw.WriteEndElement(); + break; } - return true; + xtw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End software - xtw.WriteEndElement(); + // End software + xtw.WriteEndElement(); - // End softwaredb - xtw.WriteEndElement(); + // End softwaredb + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/RomCenter.cs b/SabreTools.Library/DatFiles/RomCenter.cs index 530344c5..bc2d5fda 100644 --- a/SabreTools.Library/DatFiles/RomCenter.cs +++ b/SabreTools.Library/DatFiles/RomCenter.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using SabreTools.Library.Data; @@ -31,7 +30,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all intenral variables IniReader ir = filename.GetIniReader(false); @@ -85,8 +85,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; } ir.Dispose(); @@ -367,8 +366,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -422,9 +422,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -435,40 +433,26 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// IniWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(IniWriter iw) + private void WriteHeader(IniWriter iw) { - try - { - iw.WriteSection("CREDITS"); - iw.WriteKeyValuePair("author", Header.Author); - iw.WriteKeyValuePair("version", Header.Version); - iw.WriteKeyValuePair("comment", Header.Comment); + iw.WriteSection("CREDITS"); + iw.WriteKeyValuePair("author", Header.Author); + iw.WriteKeyValuePair("version", Header.Version); + iw.WriteKeyValuePair("comment", Header.Comment); - iw.WriteSection("DAT"); - iw.WriteKeyValuePair("version", Header.RomCenterVersion ?? "2.50"); - iw.WriteKeyValuePair("plugin", Header.System); - iw.WriteKeyValuePair("split", Header.ForceMerging == MergingFlag.Split ? "1" : "0"); - iw.WriteKeyValuePair("merge", Header.ForceMerging == MergingFlag.Full || Header.ForceMerging == MergingFlag.Merged ? "1" : "0"); + iw.WriteSection("DAT"); + iw.WriteKeyValuePair("version", Header.RomCenterVersion ?? "2.50"); + iw.WriteKeyValuePair("plugin", Header.System); + iw.WriteKeyValuePair("split", Header.ForceMerging == MergingFlag.Split ? "1" : "0"); + iw.WriteKeyValuePair("merge", Header.ForceMerging == MergingFlag.Full || Header.ForceMerging == MergingFlag.Merged ? "1" : "0"); - iw.WriteSection("EMULATOR"); - iw.WriteKeyValuePair("refname", Header.Name); - iw.WriteKeyValuePair("version", Header.Description); + iw.WriteSection("EMULATOR"); + iw.WriteKeyValuePair("refname", Header.Name); + iw.WriteKeyValuePair("version", Header.Description); - iw.WriteSection("GAMES"); + iw.WriteSection("GAMES"); - iw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + iw.Flush(); } /// @@ -476,8 +460,7 @@ namespace SabreTools.Library.DatFiles /// /// IniWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(IniWriter iw, DatItem datItem) + private void WriteDatItem(IniWriter iw, DatItem datItem) { /* The rominfo order is as follows: @@ -492,47 +475,34 @@ namespace SabreTools.Library.DatFiles 9 - merge name */ - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); + case ItemType.Rom: + var rom = datItem as Rom; - // Build the state - switch (datItem.ItemType) - { - case ItemType.Rom: - var rom = datItem as Rom; - - iw.WriteString($"¬{rom.Machine.CloneOf ?? string.Empty}"); - iw.WriteString($"¬{rom.Machine.CloneOf ?? string.Empty}"); + iw.WriteString($"¬{rom.Machine.CloneOf ?? string.Empty}"); + iw.WriteString($"¬{rom.Machine.CloneOf ?? string.Empty}"); + iw.WriteString($"¬{rom.Machine.Name ?? string.Empty}"); + if (string.IsNullOrWhiteSpace(rom.Machine.Description ?? string.Empty)) iw.WriteString($"¬{rom.Machine.Name ?? string.Empty}"); - if (string.IsNullOrWhiteSpace(rom.Machine.Description ?? string.Empty)) - iw.WriteString($"¬{rom.Machine.Name ?? string.Empty}"); - else - iw.WriteString($"¬{rom.Machine.Description ?? string.Empty}"); - iw.WriteString($"¬{rom.Name ?? string.Empty}"); - iw.WriteString($"¬{rom.CRC ?? string.Empty}"); - iw.WriteString($"¬{rom.Size?.ToString() ?? string.Empty}"); - iw.WriteString($"¬{rom.Machine.RomOf ?? string.Empty}"); - iw.WriteString($"¬{rom.MergeTag ?? string.Empty}"); - iw.WriteString("¬"); - iw.WriteLine(); + else + iw.WriteString($"¬{rom.Machine.Description ?? string.Empty}"); + iw.WriteString($"¬{rom.Name ?? string.Empty}"); + iw.WriteString($"¬{rom.CRC ?? string.Empty}"); + iw.WriteString($"¬{rom.Size?.ToString() ?? string.Empty}"); + iw.WriteString($"¬{rom.Machine.RomOf ?? string.Empty}"); + iw.WriteString($"¬{rom.MergeTag ?? string.Empty}"); + iw.WriteString("¬"); + iw.WriteLine(); - break; - } - - iw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + break; } - return true; + iw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/SabreJSON.cs b/SabreTools.Library/DatFiles/SabreJSON.cs index d2b9bc09..a241c61e 100644 --- a/SabreTools.Library/DatFiles/SabreJSON.cs +++ b/SabreTools.Library/DatFiles/SabreJSON.cs @@ -32,7 +32,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), new UTF8Encoding(false)); @@ -78,8 +79,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; } jtr.Close(); @@ -336,8 +336,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -407,9 +408,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -420,33 +419,19 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied JsonTextWriter /// /// JsonTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(JsonTextWriter jtw) + private void WriteHeader(JsonTextWriter jtw) { - try - { - jtw.WriteStartObject(); + jtw.WriteStartObject(); - // Write the DatHeader - jtw.WritePropertyName("header"); - JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented }; - js.Serialize(jtw, Header); + // Write the DatHeader + jtw.WritePropertyName("header"); + JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented }; + js.Serialize(jtw, Header); - jtw.WritePropertyName("machines"); - jtw.WriteStartArray(); + jtw.WritePropertyName("machines"); + jtw.WriteStartArray(); - jtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + jtw.Flush(); } /// @@ -454,66 +439,38 @@ namespace SabreTools.Library.DatFiles /// /// JsonTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(JsonTextWriter jtw, DatItem datItem) + private void WriteStartGame(JsonTextWriter jtw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty; + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty; - // Build the state - jtw.WriteStartObject(); + // Build the state + jtw.WriteStartObject(); - // Write the Machine - jtw.WritePropertyName("machine"); - JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented }; - js.Serialize(jtw, datItem.Machine); + // Write the Machine + jtw.WritePropertyName("machine"); + JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented }; + js.Serialize(jtw, datItem.Machine); - jtw.WritePropertyName("items"); - jtw.WriteStartArray(); + jtw.WritePropertyName("items"); + jtw.WriteStartArray(); - jtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + jtw.Flush(); } /// /// Write out Game end using the supplied JsonTextWriter /// /// JsonTextWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(JsonTextWriter jtw) + private void WriteEndGame(JsonTextWriter jtw) { - try - { - // End items - jtw.WriteEndArray(); + // End items + jtw.WriteEndArray(); - // End machine - jtw.WriteEndObject(); + // End machine + jtw.WriteEndObject(); - jtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + jtw.Flush(); } /// @@ -521,72 +478,44 @@ namespace SabreTools.Library.DatFiles /// /// JsonTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(JsonTextWriter jtw, DatItem datItem) + private void WriteDatItem(JsonTextWriter jtw, DatItem datItem) { - try - { - // Pre-process the item name - ProcessItemName(datItem, true); + // Pre-process the item name + ProcessItemName(datItem, true); - // Build the state - jtw.WriteStartObject(); + // Build the state + jtw.WriteStartObject(); - // Write the DatItem - jtw.WritePropertyName("datitem"); - JsonSerializer js = new JsonSerializer() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented }; - js.Serialize(jtw, datItem); + // Write the DatItem + jtw.WritePropertyName("datitem"); + JsonSerializer js = new JsonSerializer() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented }; + js.Serialize(jtw, datItem); - // End item - jtw.WriteEndObject(); + // End item + jtw.WriteEndObject(); - jtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + jtw.Flush(); } /// /// Write out DAT footer using the supplied JsonTextWriter /// /// JsonTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(JsonTextWriter jtw) + private void WriteFooter(JsonTextWriter jtw) { - try - { - // End items - jtw.WriteEndArray(); + // End items + jtw.WriteEndArray(); - // End machine - jtw.WriteEndObject(); + // End machine + jtw.WriteEndObject(); - // End machines - jtw.WriteEndArray(); + // End machines + jtw.WriteEndArray(); - // End file - jtw.WriteEndObject(); + // End file + jtw.WriteEndObject(); - jtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + jtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/SabreXML.cs b/SabreTools.Library/DatFiles/SabreXML.cs index d89e35cc..7ec1a676 100644 --- a/SabreTools.Library/DatFiles/SabreXML.cs +++ b/SabreTools.Library/DatFiles/SabreXML.cs @@ -31,7 +31,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables XmlReader xtr = filename.GetXmlTextReader(); @@ -77,9 +78,8 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; + // For XML errors, just skip the affected node xtr?.Read(); } @@ -103,46 +103,34 @@ namespace SabreTools.Library.DatFiles Machine machine = null; // Otherwise, read the directory - try + xtr.MoveToContent(); + while (!xtr.EOF) { - xtr.MoveToContent(); - while (!xtr.EOF) + // We only want elements + if (xtr.NodeType != XmlNodeType.Element) { - // We only want elements - if (xtr.NodeType != XmlNodeType.Element) - { - xtr.Read(); - continue; - } - - switch (xtr.Name) - { - case "machine": - XmlSerializer xs = new XmlSerializer(typeof(Machine)); - machine = xs.Deserialize(xtr.ReadSubtree()) as Machine; - xtr.Skip(); - break; - - case "files": - ReadFiles(xtr.ReadSubtree(), machine, filename, indexId); - - // Skip the directory node now that we've processed it - xtr.Read(); - break; - default: - xtr.Read(); - break; - } + xtr.Read(); + continue; } - } - catch (Exception ex) - { - Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; - // For XML errors, just skip the affected node - xtr?.Read(); + switch (xtr.Name) + { + case "machine": + XmlSerializer xs = new XmlSerializer(typeof(Machine)); + machine = xs.Deserialize(xtr.ReadSubtree()) as Machine; + xtr.Skip(); + break; + + case "files": + ReadFiles(xtr.ReadSubtree(), machine, filename, indexId); + + // Skip the directory node now that we've processed it + xtr.Read(); + break; + default: + xtr.Read(); + break; + } } } @@ -160,42 +148,30 @@ namespace SabreTools.Library.DatFiles return; // Otherwise, read the items - try + xtr.MoveToContent(); + while (!xtr.EOF) { - xtr.MoveToContent(); - while (!xtr.EOF) + // We only want elements + if (xtr.NodeType != XmlNodeType.Element) { - // We only want elements - if (xtr.NodeType != XmlNodeType.Element) - { - xtr.Read(); - continue; - } - - switch (xtr.Name) - { - case "datitem": - XmlSerializer xs = new XmlSerializer(typeof(DatItem)); - DatItem item = xs.Deserialize(xtr.ReadSubtree()) as DatItem; - item.CopyMachineInformation(machine); - item.Source = new Source { Name = filename, Index = indexId }; - ParseAddHelper(item); - xtr.Skip(); - break; - default: - xtr.Read(); - break; - } + xtr.Read(); + continue; } - } - catch (Exception ex) - { - Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; - // For XML errors, just skip the affected node - xtr?.Read(); + switch (xtr.Name) + { + case "datitem": + XmlSerializer xs = new XmlSerializer(typeof(DatItem)); + DatItem item = xs.Deserialize(xtr.ReadSubtree()) as DatItem; + item.CopyMachineInformation(machine); + item.Source = new Source { Name = filename, Index = indexId }; + ParseAddHelper(item); + xtr.Skip(); + break; + default: + xtr.Read(); + break; + } } } @@ -204,9 +180,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - /// TODO: Fix writing out files that have a path in the name - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -275,9 +251,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -288,34 +262,20 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try - { - xtw.WriteStartDocument(); + xtw.WriteStartDocument(); - xtw.WriteStartElement("datafile"); + xtw.WriteStartElement("datafile"); - XmlSerializer xs = new XmlSerializer(typeof(DatHeader)); - XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); - ns.Add("", ""); - xs.Serialize(xtw, Header, ns); + XmlSerializer xs = new XmlSerializer(typeof(DatHeader)); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add("", ""); + xs.Serialize(xtw, Header, ns); - xtw.WriteStartElement("data"); + xtw.WriteStartElement("data"); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -323,63 +283,36 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem) + private void WriteStartGame(XmlTextWriter xtw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty; + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty; - // Write the machine - xtw.WriteStartElement("directory"); - XmlSerializer xs = new XmlSerializer(typeof(Machine)); - XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); - ns.Add("", ""); - xs.Serialize(xtw, datItem.Machine, ns); + // Write the machine + xtw.WriteStartElement("directory"); + XmlSerializer xs = new XmlSerializer(typeof(Machine)); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add("", ""); + xs.Serialize(xtw, datItem.Machine, ns); - xtw.WriteStartElement("files"); + xtw.WriteStartElement("files"); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// /// Write out Game start using the supplied StreamWriter /// /// XmlTextWriter to output to - private bool WriteEndGame(XmlTextWriter xtw) + private void WriteEndGame(XmlTextWriter xtw) { - try - { - // End files - xtw.WriteEndElement(); + // End files + xtw.WriteEndElement(); - // End directory - xtw.WriteEndElement(); + // End directory + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -387,67 +320,39 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try - { - // Pre-process the item name - ProcessItemName(datItem, true); + // Pre-process the item name + ProcessItemName(datItem, true); - // Write the DatItem - XmlSerializer xs = new XmlSerializer(typeof(DatItem)); - XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); - ns.Add("", ""); - xs.Serialize(xtw, datItem, ns); + // Write the DatItem + XmlSerializer xs = new XmlSerializer(typeof(DatItem)); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add("", ""); + xs.Serialize(xtw, datItem, ns); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End files - xtw.WriteEndElement(); + // End files + xtw.WriteEndElement(); - // End directory - xtw.WriteEndElement(); + // End directory + xtw.WriteEndElement(); - // End data - xtw.WriteEndElement(); + // End data + xtw.WriteEndElement(); - // End datafile - xtw.WriteEndElement(); + // End datafile + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/SeparatedValue.cs b/SabreTools.Library/DatFiles/SeparatedValue.cs index 44e11d86..5c51a839 100644 --- a/SabreTools.Library/DatFiles/SeparatedValue.cs +++ b/SabreTools.Library/DatFiles/SeparatedValue.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using SabreTools.Library.Data; @@ -36,7 +35,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Open a file reader Encoding enc = FileExtensions.GetEncoding(filename); @@ -106,8 +106,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -159,9 +160,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -172,13 +171,10 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// SeparatedValueWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(SeparatedValueWriter svw) + private void WriteHeader(SeparatedValueWriter svw) { - try - { - string[] headers = new string[] - { + string[] headers = new string[] + { "File Name", "Internal Name", "Description", @@ -197,22 +193,11 @@ namespace SabreTools.Library.DatFiles //"SHA512", //"SpamSum", "Nodump", - }; + }; - svw.WriteHeader(headers); + svw.WriteHeader(headers); - svw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + svw.Flush(); } /// @@ -220,95 +205,81 @@ namespace SabreTools.Library.DatFiles /// /// SeparatedValueWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem) + private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem) { - try + // Separated values should only output Rom and Disk + if (datItem.ItemType != ItemType.Disk && datItem.ItemType != ItemType.Rom) + return; + + // Build the state + // TODO: Can we have some way of saying what fields to write out? Support for read extends to all fields now + string[] fields = new string[14]; // 18; + fields[0] = Header.FileName; + fields[1] = Header.Name; + fields[2] = Header.Description; + fields[3] = datItem.Machine.Name; + fields[4] = datItem.Machine.Description; + + switch (datItem.ItemType) { - // Separated values should only output Rom and Disk - if (datItem.ItemType != ItemType.Disk && datItem.ItemType != ItemType.Rom) - return true; + case ItemType.Disk: + var disk = datItem as Disk; + fields[5] = "disk"; + fields[6] = string.Empty; + fields[7] = disk.Name; + fields[8] = string.Empty; + fields[9] = string.Empty; + fields[10] = disk.MD5?.ToLowerInvariant(); + //fields[11] = string.Empty; + fields[11] = disk.SHA1?.ToLowerInvariant(); + fields[12] = string.Empty; + //fields[13] = string.Empty; + //fields[14] = string.Empty; + //fields[15] = string.Empty; + fields[13] = disk.ItemStatus.ToString(); + break; - // Build the state - // TODO: Can we have some way of saying what fields to write out? Support for read extends to all fields now - string[] fields = new string[14]; // 18; - fields[0] = Header.FileName; - fields[1] = Header.Name; - fields[2] = Header.Description; - fields[3] = datItem.Machine.Name; - fields[4] = datItem.Machine.Description; + case ItemType.Media: + var media = datItem as Media; + fields[5] = "media"; + fields[6] = string.Empty; + fields[7] = media.Name; + fields[8] = string.Empty; + fields[9] = string.Empty; + fields[10] = media.MD5?.ToLowerInvariant(); + //fields[11] = string.Empty; + fields[11] = media.SHA1?.ToLowerInvariant(); + fields[12] = media.SHA256?.ToLowerInvariant(); + //fields[13] = string.Empty; + //fields[14] = string.Empty; + //fields[15] = media.SpamSum?.ToLowerInvariant(); + fields[13] = string.Empty; + break; - switch (datItem.ItemType) - { - case ItemType.Disk: - var disk = datItem as Disk; - fields[5] = "disk"; - fields[6] = string.Empty; - fields[7] = disk.Name; - fields[8] = string.Empty; - fields[9] = string.Empty; - fields[10] = disk.MD5?.ToLowerInvariant(); - //fields[11] = string.Empty; - fields[11] = disk.SHA1?.ToLowerInvariant(); - fields[12] = string.Empty; - //fields[13] = string.Empty; - //fields[14] = string.Empty; - //fields[15] = string.Empty; - fields[13] = disk.ItemStatus.ToString(); - break; - - case ItemType.Media: - var media = datItem as Media; - fields[5] = "media"; - fields[6] = string.Empty; - fields[7] = media.Name; - fields[8] = string.Empty; - fields[9] = string.Empty; - fields[10] = media.MD5?.ToLowerInvariant(); - //fields[11] = string.Empty; - fields[11] = media.SHA1?.ToLowerInvariant(); - fields[12] = media.SHA256?.ToLowerInvariant(); - //fields[13] = string.Empty; - //fields[14] = string.Empty; - //fields[15] = media.SpamSum?.ToLowerInvariant(); - fields[13] = string.Empty; - break; - - case ItemType.Rom: - var rom = datItem as Rom; - fields[5] = "rom"; - fields[6] = rom.Name; - fields[7] = string.Empty; - fields[8] = rom.Size?.ToString(); - fields[9] = rom.CRC?.ToLowerInvariant(); - fields[10] = rom.MD5?.ToLowerInvariant(); - //fields[11] = rom.RIPEMD160?.ToLowerInvariant(); - fields[11] = rom.SHA1?.ToLowerInvariant(); - fields[12] = rom.SHA256?.ToLowerInvariant(); - //fields[13] = rom.SHA384?.ToLowerInvariant(); - //fields[14] = rom.SHA512?.ToLowerInvariant(); - //fields[15] = rom.SpamSum?.ToLowerInvariant(); - fields[13] = rom.ItemStatus.ToString(); - break; - } - - svw.WriteString(CreatePrefixPostfix(datItem, true)); - svw.WriteValues(fields, false); - svw.WriteString(CreatePrefixPostfix(datItem, false)); - svw.WriteLine(); - - svw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.Rom: + var rom = datItem as Rom; + fields[5] = "rom"; + fields[6] = rom.Name; + fields[7] = string.Empty; + fields[8] = rom.Size?.ToString(); + fields[9] = rom.CRC?.ToLowerInvariant(); + fields[10] = rom.MD5?.ToLowerInvariant(); + //fields[11] = rom.RIPEMD160?.ToLowerInvariant(); + fields[11] = rom.SHA1?.ToLowerInvariant(); + fields[12] = rom.SHA256?.ToLowerInvariant(); + //fields[13] = rom.SHA384?.ToLowerInvariant(); + //fields[14] = rom.SHA512?.ToLowerInvariant(); + //fields[15] = rom.SpamSum?.ToLowerInvariant(); + fields[13] = rom.ItemStatus.ToString(); + break; } - return true; + svw.WriteString(CreatePrefixPostfix(datItem, true)); + svw.WriteValues(fields, false); + svw.WriteString(CreatePrefixPostfix(datItem, false)); + svw.WriteLine(); + + svw.Flush(); } } } diff --git a/SabreTools.Library/DatFiles/SoftwareList.cs b/SabreTools.Library/DatFiles/SoftwareList.cs index 249d851f..b0146588 100644 --- a/SabreTools.Library/DatFiles/SoftwareList.cs +++ b/SabreTools.Library/DatFiles/SoftwareList.cs @@ -33,7 +33,8 @@ namespace SabreTools.Library.DatFiles /// Name of the file to be parsed /// Index ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) - protected override void ParseFile(string filename, int indexId, bool keep) + /// True if the error that is thrown should be thrown back to the caller, false otherwise + protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) { // Prepare all internal variables XmlReader xtr = filename.GetXmlTextReader(); @@ -81,8 +82,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); - if (Globals.ThrowOnError) - throw ex; + if (throwOnError) throw ex; // For XML errors, just skip the affected node xtr?.Read(); @@ -505,8 +505,9 @@ namespace SabreTools.Library.DatFiles /// /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) + /// True if the error that is thrown should be thrown back to the caller, false otherwise /// True if the DAT was written correctly, false otherwise - public override bool WriteToFile(string outfile, bool ignoreblanks = false) + public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false) { try { @@ -575,9 +576,7 @@ namespace SabreTools.Library.DatFiles catch (Exception ex) { Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - + if (throwOnError) throw ex; return false; } @@ -588,30 +587,16 @@ namespace SabreTools.Library.DatFiles /// Write out DAT header using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteHeader(XmlTextWriter xtw) + private void WriteHeader(XmlTextWriter xtw) { - try - { - xtw.WriteStartDocument(); - xtw.WriteDocType("softwarelist", null, "softwarelist.dtd", null); + xtw.WriteStartDocument(); + xtw.WriteDocType("softwarelist", null, "softwarelist.dtd", null); - xtw.WriteStartElement("softwarelist"); - xtw.WriteRequiredAttributeString("name", Header.Name); - xtw.WriteRequiredAttributeString("description", Header.Description); + xtw.WriteStartElement("softwarelist"); + xtw.WriteRequiredAttributeString("name", Header.Name); + xtw.WriteRequiredAttributeString("description", Header.Description); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -619,63 +604,35 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteStartGame(XmlTextWriter xtw, DatItem datItem) + private void WriteStartGame(XmlTextWriter xtw, DatItem datItem) { - try - { - // No game should start with a path separator - datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); + // No game should start with a path separator + datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar); - // 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)); + // 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)); - xtw.WriteOptionalElementString("description", datItem.Machine.Description); - xtw.WriteOptionalElementString("year", datItem.Machine.Year); - xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher); + xtw.WriteOptionalElementString("description", datItem.Machine.Description); + xtw.WriteOptionalElementString("year", datItem.Machine.Year); + xtw.WriteOptionalElementString("publisher", datItem.Machine.Publisher); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// /// Write out Game start using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteEndGame(XmlTextWriter xtw) + private void WriteEndGame(XmlTextWriter xtw) { - try - { - // End software - xtw.WriteEndElement(); + // End software + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } /// @@ -683,186 +640,158 @@ namespace SabreTools.Library.DatFiles /// /// XmlTextWriter to output to /// DatItem object to be output - /// True if the data was written, false on error - private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem) + private void WriteDatItem(XmlTextWriter xtw, DatItem datItem) { - try + // Pre-process the item name + ProcessItemName(datItem, true); + + // Build the state + switch (datItem.ItemType) { - // Pre-process the item name - ProcessItemName(datItem, true); - - // 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) + 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) { - foreach (Setting dipValue in dipSwitch.Values) - { - xtw.WriteStartElement("dipvalue"); - xtw.WriteRequiredAttributeString("name", dipValue.Name); - xtw.WriteOptionalAttributeString("value", dipValue.Value); - xtw.WriteOptionalAttributeString("default", dipValue.Default.FromYesNo()); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("dipvalue"); + xtw.WriteRequiredAttributeString("name", dipValue.Name); + xtw.WriteOptionalAttributeString("value", dipValue.Value); + xtw.WriteOptionalAttributeString("default", dipValue.Default.FromYesNo()); + xtw.WriteEndElement(); } - xtw.WriteEndElement(); - break; + } + xtw.WriteEndElement(); + break; - case ItemType.Disk: - var disk = datItem as Disk; - string diskAreaName = disk.DiskArea?.Name; - if (string.IsNullOrWhiteSpace(diskAreaName)) - diskAreaName = "cdrom"; + 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); + xtw.WriteStartElement("part"); + xtw.WriteRequiredAttributeString("name", disk.Part?.Name); + xtw.WriteRequiredAttributeString("interface", disk.Part?.Interface); - if (disk.Part?.Features != null && disk.Part?.Features.Count > 0) + if (disk.Part?.Features != null && disk.Part?.Features.Count > 0) + { + foreach (PartFeature partFeature in disk.Part.Features) { - foreach (PartFeature partFeature in disk.Part.Features) - { - xtw.WriteStartElement("feature"); - xtw.WriteRequiredAttributeString("name", partFeature.Name); - xtw.WriteRequiredAttributeString("value", partFeature.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("feature"); + xtw.WriteRequiredAttributeString("name", partFeature.Name); + xtw.WriteRequiredAttributeString("value", partFeature.Value); + xtw.WriteEndElement(); } + } - xtw.WriteStartElement("diskarea"); - xtw.WriteRequiredAttributeString("name", diskAreaName); + 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(); + 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 diskarea + xtw.WriteEndElement(); - // End part - xtw.WriteEndElement(); - break; + // 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.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"; + 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); + 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) + if (rom.Part?.Features != null && rom.Part?.Features.Count > 0) + { + foreach (PartFeature kvp in rom.Part.Features) { - foreach (PartFeature kvp in rom.Part.Features) - { - xtw.WriteStartElement("feature"); - xtw.WriteRequiredAttributeString("name", kvp.Name); - xtw.WriteRequiredAttributeString("value", kvp.Value); - xtw.WriteEndElement(); - } + xtw.WriteStartElement("feature"); + xtw.WriteRequiredAttributeString("name", kvp.Name); + xtw.WriteRequiredAttributeString("value", kvp.Value); + xtw.WriteEndElement(); } + } - 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("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()); + 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()); + 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(); + 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 dataarea + xtw.WriteEndElement(); - // End part - xtw.WriteEndElement(); - break; + // 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; - } - - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; + case ItemType.SharedFeature: + var sharedFeature = datItem as SharedFeature; + xtw.WriteStartElement("sharedfeat"); + xtw.WriteRequiredAttributeString("name", sharedFeature.Name); + xtw.WriteRequiredAttributeString("value", sharedFeature.Value); + xtw.WriteEndElement(); + break; } - return true; + xtw.Flush(); } /// /// Write out DAT footer using the supplied StreamWriter /// /// XmlTextWriter to output to - /// True if the data was written, false on error - private bool WriteFooter(XmlTextWriter xtw) + private void WriteFooter(XmlTextWriter xtw) { - try - { - // End software - xtw.WriteEndElement(); + // End software + xtw.WriteEndElement(); - // End softwarelist - xtw.WriteEndElement(); + // End softwarelist + xtw.WriteEndElement(); - xtw.Flush(); - } - catch (Exception ex) - { - Globals.Logger.Error(ex.ToString()); - if (Globals.ThrowOnError) - throw ex; - - return false; - } - - return true; + xtw.Flush(); } } } diff --git a/SabreTools.Library/IO/ClrMameProReader.cs b/SabreTools.Library/IO/ClrMameProReader.cs index 8ed150f2..85e4dfad 100644 --- a/SabreTools.Library/IO/ClrMameProReader.cs +++ b/SabreTools.Library/IO/ClrMameProReader.cs @@ -168,24 +168,8 @@ namespace SabreTools.Library.IO } else { - // Special case for non-quoted names (old DATs only) - if (key == "name" && !linegc[i + 1].Contains("\"")) - { - while (++i < linegc.Length - && linegc[i] != "size" - && linegc[i] != "crc" - && linegc[i] != "md5" - && linegc[i] != "sha1" - && linegc[i] != "status") - { - value += $" {linegc[i]}"; - } - - value = value.Trim(); - i--; - } // Special cases for standalone statuses - else if (key == "baddump" || key == "good" || key == "nodump" || key == "verified") + if (key == "baddump" || key == "good" || key == "nodump" || key == "verified") { value = key; key = "status"; diff --git a/SabreTools/Features/Batch.cs b/SabreTools/Features/Batch.cs index aa271323..fbb1221c 100644 --- a/SabreTools/Features/Batch.cs +++ b/SabreTools/Features/Batch.cs @@ -422,9 +422,6 @@ Reset the internal state: reset();"; catch (Exception ex) { Globals.Logger.Error($"There was an exception processing {path}: {ex}"); - if (Globals.ThrowOnError) - throw ex; - continue; } }