Use new writer, remove string state

This commit is contained in:
Matt Nadareski
2020-06-13 13:54:04 -07:00
parent 46cc8e806e
commit 8c97497987
8 changed files with 143 additions and 167 deletions

View File

@@ -552,10 +552,11 @@ namespace SabreTools.Library.DatFiles
return false; return false;
} }
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false)); ClrMameProWriter cmpw = new ClrMameProWriter(fs, new UTF8Encoding(false));
cmpw.Quotes = true;
// Write out the header // Write out the header
WriteHeader(sw); WriteHeader(cmpw);
// Write out each of the machines and roms // Write out each of the machines and roms
string lastgame = null; string lastgame = null;
@@ -584,11 +585,11 @@ namespace SabreTools.Library.DatFiles
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
WriteEndGame(sw, rom); WriteEndGame(cmpw, rom);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
WriteStartGame(sw, rom); WriteStartGame(cmpw, rom);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom if (rom.ItemType == ItemType.Rom
@@ -610,7 +611,7 @@ namespace SabreTools.Library.DatFiles
} }
// Now, output the rom data // Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks); WriteDatItem(cmpw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.MachineName; lastgame = rom.MachineName;
@@ -618,10 +619,10 @@ namespace SabreTools.Library.DatFiles
} }
// Write the file footer out // Write the file footer out
WriteFooter(sw); WriteFooter(cmpw);
Globals.Logger.Verbose($"File written!{Environment.NewLine}"); Globals.Logger.Verbose($"File written!{Environment.NewLine}");
sw.Dispose(); cmpw.Close();
fs.Dispose(); fs.Dispose();
} }
catch (Exception ex) catch (Exception ex)
@@ -636,60 +637,61 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out DAT header using the supplied StreamWriter /// Write out DAT header using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(StreamWriter sw) private bool WriteHeader(ClrMameProWriter cmpw)
{ {
try try
{ {
sw.Write("clrmamepro (\n"); cmpw.WriteStartElement("clrmamepro");
sw.Write($"\tname \"{Name}\"\n");
sw.Write($"\tdescription \"{Description}\"\n"); cmpw.WriteStandalone("name", Name);
cmpw.WriteStandalone("description", Description);
if (!string.IsNullOrWhiteSpace(Category)) if (!string.IsNullOrWhiteSpace(Category))
sw.Write($"\tcategory \"{Category}\"\n"); cmpw.WriteStandalone("category", Category);
sw.Write($"\tversion \"{Version}\"\n"); cmpw.WriteStandalone("version", Version);
if (!string.IsNullOrWhiteSpace(Date)) if (!string.IsNullOrWhiteSpace(Date))
sw.Write($"\tdate \"{Date}\"\n"); cmpw.WriteStandalone("date", Date);
sw.Write($"\tauthor \"{Author}\"\n"); cmpw.WriteStandalone("author", Author);
if (!string.IsNullOrWhiteSpace(Email)) if (!string.IsNullOrWhiteSpace(Email))
sw.Write($"\temail \"{Email}\"\n"); cmpw.WriteStandalone("email", Email);
if (!string.IsNullOrWhiteSpace(Homepage)) if (!string.IsNullOrWhiteSpace(Homepage))
sw.Write($"\thomepage \"{Homepage}\"\n"); cmpw.WriteStandalone("homepage", Homepage);
if (!string.IsNullOrWhiteSpace(Url)) if (!string.IsNullOrWhiteSpace(Url))
sw.Write($"\turl \"{Url}\"\n"); cmpw.WriteStandalone("url", Url);
if (!string.IsNullOrWhiteSpace(Comment)) if (!string.IsNullOrWhiteSpace(Comment))
sw.Write($"\tcomment \"{Comment}\"\n"); cmpw.WriteStandalone("comment", Comment);
switch (ForcePacking) switch (ForcePacking)
{ {
case ForcePacking.Unzip: case ForcePacking.Unzip:
sw.Write($"\tforcezipping no\n"); cmpw.WriteStandalone("forcezipping", "no", false);
break; break;
case ForcePacking.Zip: case ForcePacking.Zip:
sw.Write($"\tforcezipping yes\n"); cmpw.WriteStandalone("forcezipping", "yes", false);
break; break;
} }
switch (ForceMerging) switch (ForceMerging)
{ {
case ForceMerging.Full: case ForceMerging.Full:
sw.Write($"\tforcemerging full\n"); cmpw.WriteStandalone("forcemerging", "full", false);
break; break;
case ForceMerging.Split: case ForceMerging.Split:
sw.Write($"\tforcemerging split\n"); cmpw.WriteStandalone("forcemerging", "split", false);
break; break;
case ForceMerging.Merged: case ForceMerging.Merged:
sw.Write($"\tforcemerging merged\n"); cmpw.WriteStandalone("forcemerging", "merged", false);
break; break;
case ForceMerging.NonMerged: case ForceMerging.NonMerged:
sw.Write($"\tforcemerging nonmerged\n"); cmpw.WriteStandalone("forcemerging", "nonmerged", false);
break; break;
} }
// End clrmamepro // End clrmamepro
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -703,10 +705,10 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out Game start using the supplied StreamWriter /// Write out Game start using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem datItem) private bool WriteStartGame(ClrMameProWriter cmpw, DatItem datItem)
{ {
try try
{ {
@@ -714,24 +716,24 @@ namespace SabreTools.Library.DatFiles
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar); datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
// Build the state based on excluded fields // Build the state based on excluded fields
sw.Write($"{(datItem.MachineType == MachineType.Bios ? "resource" : "game")} (\n"); cmpw.WriteStartElement(datItem.MachineType == MachineType.Bios ? "resource" : "game");
sw.Write($"\tname \"{datItem.MachineName}\"\n"); cmpw.WriteStandalone("name", datItem.GetField(Field.MachineName, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RomOf, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RomOf, ExcludeFields)))
sw.Write($"\tromof \"{datItem.RomOf}\"\n"); cmpw.WriteStandalone("romof", datItem.RomOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
sw.Write($"\tcloneof \"{datItem.CloneOf}\"\n"); cmpw.WriteStandalone("cloneof", datItem.CloneOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
sw.Write($"\tsampleof \"{datItem.SampleOf}\"\n"); cmpw.WriteStandalone("sampleof", datItem.SampleOf);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields)))
sw.Write($"\tdescription \"{datItem.MachineDescription}\"\n"); cmpw.WriteStandalone("description", datItem.MachineDescription);
else if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields))) else if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, ExcludeFields)))
sw.Write($"\tdescription \"{datItem.MachineName}\"\n"); cmpw.WriteStandalone("description", datItem.MachineName);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Year, ExcludeFields)))
sw.Write($"\tyear \"{datItem.Year}\"\n"); cmpw.WriteStandalone("year", datItem.Year);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Manufacturer, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Manufacturer, ExcludeFields)))
sw.Write($"\tmanufacturer \"{datItem.Manufacturer}\"\n"); cmpw.WriteStandalone("manufacturer", datItem.Manufacturer);
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -745,23 +747,21 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out Game end using the supplied StreamWriter /// Write out Game end using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw, DatItem datItem) private bool WriteEndGame(ClrMameProWriter cmpw, DatItem datItem)
{ {
try try
{ {
string state = string.Empty;
// Build the state based on excluded fields // Build the state based on excluded fields
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
sw.Write($"\tsampleof \"{datItem.SampleOf}\"\n"); cmpw.WriteStandalone("sampleof", datItem.SampleOf);
// End game // End game
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -776,11 +776,11 @@ namespace SabreTools.Library.DatFiles
/// Write out DatItem using the supplied StreamWriter /// Write out DatItem using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="datFile">DatFile to write out from</param> /// <param name="datFile">DatFile to write out from</param>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param> /// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem, bool ignoreblanks = false)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1))) if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
@@ -788,8 +788,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Pre-process the item name // Pre-process the item name
ProcessItemName(datItem, true); ProcessItemName(datItem, true);
@@ -797,94 +795,93 @@ namespace SabreTools.Library.DatFiles
switch (datItem.ItemType) switch (datItem.ItemType)
{ {
case ItemType.Archive: case ItemType.Archive:
sw.Write("\tarchive ("); cmpw.WriteStartElement("archive");
sw.Write($" name\"{datItem.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, ExcludeFields));
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
case ItemType.BiosSet: case ItemType.BiosSet:
var biosSet = datItem as BiosSet; var biosSet = datItem as BiosSet;
sw.Write("\tbiosset ("); cmpw.WriteStartElement("biosset");
sw.Write($" name\"{biosSet.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", biosSet.GetField(Field.Name, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.BiosDescription, ExcludeFields))) if (!string.IsNullOrWhiteSpace(biosSet.GetField(Field.BiosDescription, ExcludeFields)))
sw.Write($" description \"{biosSet.Description}\""); cmpw.WriteAttributeString("description", biosSet.Description);
if (!ExcludeFields[(int)Field.Default] && biosSet.Default != null) if (!ExcludeFields[(int)Field.Default] && biosSet.Default != null)
sw.Write($" default \"{biosSet.Default.ToString().ToLowerInvariant()}\""); cmpw.WriteAttributeString("default", biosSet.Default.ToString().ToLowerInvariant());
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
case ItemType.Disk: case ItemType.Disk:
var disk = datItem as Disk; var disk = datItem as Disk;
sw.Write("\tdisk ("); cmpw.WriteStartElement("disk");
sw.Write($" name\"{disk.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", disk.GetField(Field.Name, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
sw.Write($" md5 \"{disk.MD5.ToLowerInvariant()}\""); cmpw.WriteAttributeString("md5", disk.MD5.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
sw.Write($" ripemd160 \"{disk.RIPEMD160.ToLowerInvariant()}\""); cmpw.WriteAttributeString("ripemd160", disk.RIPEMD160.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
sw.Write($" sha1 \"{disk.SHA1.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha1", disk.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
sw.Write($" sha256 \"{disk.SHA256.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha256", disk.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
sw.Write($" sha384 \"{disk.SHA384.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha384", disk.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
sw.Write($" sha512 \"{disk.SHA512.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha512", disk.SHA512.ToLowerInvariant());
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus != ItemStatus.None) if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus != ItemStatus.None)
sw.Write($" flags \"{disk.ItemStatus.ToString().ToLowerInvariant()}\""); cmpw.WriteAttributeString("flags", disk.ItemStatus.ToString().ToLowerInvariant());
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
case ItemType.Release: case ItemType.Release:
var release = datItem as Release; var release = datItem as Release;
sw.Write("\trelease ("); cmpw.WriteStartElement("release");
sw.Write($" name\"{release.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", release.GetField(Field.Name, ExcludeFields));
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Region, ExcludeFields)))
sw.Write($" region \"{release.Region}\""); cmpw.WriteAttributeString("region", release.Region);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Language, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Language, ExcludeFields)))
sw.Write($" language \"{release.Language}\""); cmpw.WriteAttributeString("language", release.Language);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
sw.Write($" date \"{release.Date}\""); cmpw.WriteAttributeString("date", release.Date);
if (!ExcludeFields[(int)Field.Default] && release.Default != null) if (!ExcludeFields[(int)Field.Default] && release.Default != null)
sw.Write($" default \"{release.Default.ToString().ToLowerInvariant()}\""); cmpw.WriteAttributeString("default", release.Default.ToString().ToLowerInvariant());
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
case ItemType.Rom: case ItemType.Rom:
var rom = datItem as Rom; var rom = datItem as Rom;
sw.Write("\trom ("); cmpw.WriteStartElement("rom");
sw.Write($" name\"{rom.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", rom.GetField(Field.Name, ExcludeFields));
if (!ExcludeFields[(int)Field.Size] && rom.Size != -1) if (!ExcludeFields[(int)Field.Size] && rom.Size != -1)
sw.Write($" size \"{rom.Size}\""); cmpw.WriteAttributeString("size", rom.Size.ToString());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields)))
sw.Write($" crc \"{rom.CRC.ToLowerInvariant()}\""); cmpw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.MD5, ExcludeFields)))
sw.Write($" md5 \"{rom.MD5.ToLowerInvariant()}\""); cmpw.WriteAttributeString("md5", rom.MD5.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.RIPEMD160, ExcludeFields)))
sw.Write($" ripemd160 \"{rom.RIPEMD160.ToLowerInvariant()}\""); cmpw.WriteAttributeString("ripemd160", rom.RIPEMD160.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
sw.Write($" sha1 \"{rom.SHA1.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha1", rom.SHA1.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA256, ExcludeFields)))
sw.Write($" sha256 \"{rom.SHA256.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha256", rom.SHA256.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA384, ExcludeFields)))
sw.Write($" sha384 \"{rom.SHA384.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha384", rom.SHA384.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA512, ExcludeFields)))
sw.Write($" sha512 \"{rom.SHA512.ToLowerInvariant()}\""); cmpw.WriteAttributeString("sha512", rom.SHA512.ToLowerInvariant());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
sw.Write($" date \"{rom.Date}\""); cmpw.WriteAttributeString("date", rom.Date);
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus != ItemStatus.None) if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus != ItemStatus.None)
sw.Write($" flags \"{rom.ItemStatus.ToString().ToLowerInvariant()}\""); cmpw.WriteAttributeString("flags", rom.ItemStatus.ToString().ToLowerInvariant());
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
case ItemType.Sample: case ItemType.Sample:
sw.Write("\tsample ("); cmpw.WriteStartElement("sample");
sw.Write($" name\"{datItem.GetField(Field.Name, ExcludeFields)}\""); cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, ExcludeFields));
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
} }
sw.Write(state); cmpw.Flush();
sw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -898,16 +895,16 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out DAT footer using the supplied StreamWriter /// Write out DAT footer using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(StreamWriter sw) private bool WriteFooter(ClrMameProWriter cmpw)
{ {
try try
{ {
// End game // End game
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -197,7 +197,7 @@ namespace SabreTools.Library.DatFiles
&& linegc[i] != "date" && linegc[i] != "date"
&& linegc[i] != "crc") && linegc[i] != "crc")
{ {
item.Name += $" {linegc[i]}"; item.Name += "{linegc[i]}";
} }
// Perform correction // Perform correction
@@ -287,10 +287,11 @@ namespace SabreTools.Library.DatFiles
return false; return false;
} }
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false)); ClrMameProWriter cmpw = new ClrMameProWriter(fs, new UTF8Encoding(false));
cmpw.Quotes = false;
// Write out the header // Write out the header
WriteHeader(sw); WriteHeader(cmpw);
// Write out each of the machines and roms // Write out each of the machines and roms
string lastgame = null; string lastgame = null;
@@ -321,11 +322,11 @@ namespace SabreTools.Library.DatFiles
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
WriteEndGame(sw, rom); WriteEndGame(cmpw, rom);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
WriteStartGame(sw, rom); WriteStartGame(cmpw, rom);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom if (rom.ItemType == ItemType.Rom
@@ -346,7 +347,7 @@ namespace SabreTools.Library.DatFiles
} }
// Now, output the rom data // Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks); WriteDatItem(cmpw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.MachineName; lastgame = rom.MachineName;
@@ -354,10 +355,10 @@ namespace SabreTools.Library.DatFiles
} }
// Write the file footer out // Write the file footer out
WriteFooter(sw); WriteFooter(cmpw);
Globals.Logger.Verbose($"File written!{Environment.NewLine}"); Globals.Logger.Verbose($"File written!{Environment.NewLine}");
sw.Dispose(); cmpw.Close();
fs.Dispose(); fs.Dispose();
} }
catch (Exception ex) catch (Exception ex)
@@ -372,23 +373,23 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out DAT header using the supplied StreamWriter /// Write out DAT header using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(StreamWriter sw) private bool WriteHeader(ClrMameProWriter cmpw)
{ {
try try
{ {
sw.Write("DOSCenter (\n"); cmpw.WriteStartElement("DOSCenter");
sw.Write($"\tName: {Name}\n"); cmpw.WriteStandalone("Name:", Name, false);
sw.Write($"\tDescription: {Description}\n"); cmpw.WriteStandalone("Description:", Description, false);
sw.Write($"\tVersion: {Version}\n"); cmpw.WriteStandalone("Version:", Version, false);
sw.Write($"\tDate: {Date}\n"); cmpw.WriteStandalone("Date:", Date, false);
sw.Write($"\tAuthor: {Author}\n"); cmpw.WriteStandalone("Author:", Author, false);
sw.Write($"\tHomepage: {Homepage}\n"); cmpw.WriteStandalone("Homepage:", Homepage, false);
sw.Write($"\tComment: {Comment}\n"); cmpw.WriteStandalone("Comment:", Comment, false);
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -402,10 +403,10 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out Game start using the supplied StreamWriter /// Write out Game start using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem datItem) private bool WriteStartGame(ClrMameProWriter cmpw, DatItem datItem)
{ {
try try
{ {
@@ -413,10 +414,10 @@ namespace SabreTools.Library.DatFiles
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar); datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
// Build the state based on excluded fields // Build the state based on excluded fields
sw.Write("game (\n"); cmpw.WriteStartElement("game");
sw.Write($"\tname \"{datItem.GetField(Field.MachineName, ExcludeFields)}.zip\"\n"); cmpw.WriteStandalone("name", $"{datItem.GetField(Field.MachineName, ExcludeFields)}.zip", true);
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -430,21 +431,17 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out Game end using the supplied StreamWriter /// Write out Game end using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw, DatItem datItem) private bool WriteEndGame(ClrMameProWriter cmpw, DatItem datItem)
{ {
try try
{ {
// Build the state based on excluded fields
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
sw.Write($"\tsampleof \"{datItem.SampleOf}\"\n");
// End game // End game
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -458,11 +455,11 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out DatItem using the supplied StreamWriter /// Write out DatItem using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param> /// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem, bool ignoreblanks = false)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1))) if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
@@ -470,8 +467,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Pre-process the item name // Pre-process the item name
ProcessItemName(datItem, true); ProcessItemName(datItem, true);
@@ -480,20 +475,19 @@ namespace SabreTools.Library.DatFiles
{ {
case ItemType.Rom: case ItemType.Rom:
var rom = datItem as Rom; var rom = datItem as Rom;
sw.Write("\tfile ("); cmpw.WriteStartElement("file");
sw.Write($" name {datItem.GetField(Field.Name, ExcludeFields)}"); cmpw.WriteAttributeString("name", datItem.GetField(Field.Name, ExcludeFields));
if (!ExcludeFields[(int)Field.Size] && rom.Size != -1) if (!ExcludeFields[(int)Field.Size] && rom.Size != -1)
sw.Write($" size \"{rom.Size}\""); cmpw.WriteAttributeString("size", rom.Size.ToString());
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
sw.Write($" date \"{rom.Date}\""); cmpw.WriteAttributeString("date", rom.Date);
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields)))
sw.Write($" crc \"{rom.CRC.ToLowerInvariant()}\""); cmpw.WriteAttributeString("crc", rom.CRC.ToLowerInvariant());
sw.Write(" )\n"); cmpw.WriteEndElement();
break; break;
} }
sw.Write(state); cmpw.Flush();
sw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -507,16 +501,16 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Write out DAT footer using the supplied StreamWriter /// Write out DAT footer using the supplied StreamWriter
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(StreamWriter sw) private bool WriteFooter(ClrMameProWriter cmpw)
{ {
try try
{ {
// End game // End game
sw.Write(")\n"); cmpw.WriteEndElement();
sw.Flush(); cmpw.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -186,8 +186,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Pre-process the item name // Pre-process the item name
ProcessItemName(datItem, true); ProcessItemName(datItem, true);
@@ -239,7 +237,6 @@ namespace SabreTools.Library.DatFiles
break; break;
} }
sw.Write(state);
sw.Flush(); sw.Flush();
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -142,8 +142,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Process the item name // Process the item name
ProcessItemName(datItem, false, forceRomName: false); ProcessItemName(datItem, false, forceRomName: false);
@@ -166,7 +164,6 @@ namespace SabreTools.Library.DatFiles
} }
} }
sw.Write(state);
sw.Flush(); sw.Flush();
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -1041,8 +1041,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Pre-process the item name // Pre-process the item name
ProcessItemName(datItem, true); ProcessItemName(datItem, true);

View File

@@ -266,8 +266,6 @@ namespace SabreTools.Library.DatFiles
try try
{ {
string state = string.Empty;
// Pre-process the item name // Pre-process the item name
ProcessItemName(datItem, true); ProcessItemName(datItem, true);
@@ -310,7 +308,6 @@ namespace SabreTools.Library.DatFiles
break; break;
} }
sw.Write(state);
sw.Flush(); sw.Flush();
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -1,8 +0,0 @@
{
"profiles": {
"SabreTools": {
"commandName": "Project",
"commandLineArgs": "--update --output-dir=B:\\_TEMP --output-type=xml \"C:\\Users\\Matt\\Downloads\\DC_DAT.DAT\""
}
}
}

View File

@@ -16,4 +16,8 @@
<ProjectReference Include="..\SabreTools.Library\SabreTools.Library.csproj" /> <ProjectReference Include="..\SabreTools.Library\SabreTools.Library.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project> </Project>