Cleanup and overhaul (#21)

* Syntax cleanup

* More minor cleanup, use Linq

* Fix broken features by using correct values

* Feature flags the same

* Features are modular

* No AlphaFS, more .NET versions

* Fix appveyor

* Put back identifiers, for some reason

* String interpolation, modernization

* Better use of GetField

* XmlTextWriter to remove possible issues

* Fix header for OpenMSX
This commit is contained in:
Matt Nadareski
2020-06-10 22:37:19 -07:00
committed by GitHub
parent f01e47444c
commit 4ad77d6be6
75 changed files with 6945 additions and 7249 deletions

View File

@@ -1,20 +1,11 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using StreamReader = System.IO.StreamReader;
using StreamWriter = System.IO.StreamWriter;
#endif
using NaturalSort;
namespace SabreTools.Library.DatFiles
@@ -22,6 +13,7 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Represents parsing and writing of a RomCenter DAT
/// </summary>
/// TODO: Port over the INI parser from DICUI to make this flow a little better
internal class RomCenter : DatFile
{
/// <summary>
@@ -57,7 +49,7 @@ namespace SabreTools.Library.DatFiles
Encoding enc = Utilities.GetEncoding(filename);
StreamReader sr = new StreamReader(Utilities.TryOpenRead(filename), enc);
string blocktype = "";
string blocktype = string.Empty;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
@@ -67,87 +59,93 @@ namespace SabreTools.Library.DatFiles
{
blocktype = "credits";
}
// If the line is the start of the dat section
else if (line.ToLowerInvariant().StartsWith("[dat]"))
{
blocktype = "dat";
}
// If the line is the start of the emulator section
else if (line.ToLowerInvariant().StartsWith("[emulator]"))
{
blocktype = "emulator";
}
// If the line is the start of the game section
else if (line.ToLowerInvariant().StartsWith("[games]"))
{
blocktype = "games";
}
// Otherwise, it's not a section and it's data, so get out all data
else
{
// If we have an author
if (line.ToLowerInvariant().StartsWith("author="))
{
Author = (String.IsNullOrWhiteSpace(Author) ? line.Split('=')[1] : Author);
Author = (string.IsNullOrWhiteSpace(Author) ? line.Split('=')[1] : Author);
}
// If we have one of the three version tags
else if (line.ToLowerInvariant().StartsWith("version="))
{
switch (blocktype)
{
case "credits":
Version = (String.IsNullOrWhiteSpace(Version) ? line.Split('=')[1] : Version);
Version = (string.IsNullOrWhiteSpace(Version) ? line.Split('=')[1] : Version);
break;
case "emulator":
Description = (String.IsNullOrWhiteSpace(Description) ? line.Split('=')[1] : Description);
Description = (string.IsNullOrWhiteSpace(Description) ? line.Split('=')[1] : Description);
break;
}
}
// If we have a URL
else if (line.ToLowerInvariant().StartsWith("url="))
{
Url = (String.IsNullOrWhiteSpace(Url) ? line.Split('=')[1] : Url);
Url = (string.IsNullOrWhiteSpace(Url) ? line.Split('=')[1] : Url);
}
// If we have a comment
else if (line.ToLowerInvariant().StartsWith("comment="))
{
Comment = (String.IsNullOrWhiteSpace(Comment) ? line.Split('=')[1] : Comment);
Comment = (string.IsNullOrWhiteSpace(Comment) ? line.Split('=')[1] : Comment);
}
// If we have the split flag
else if (line.ToLowerInvariant().StartsWith("split="))
{
if (Int32.TryParse(line.Split('=')[1], out int split))
{
if (split == 1 && ForceMerging == ForceMerging.None)
{
ForceMerging = ForceMerging.Split;
}
}
}
// If we have the merge tag
else if (line.ToLowerInvariant().StartsWith("merge="))
{
if (Int32.TryParse(line.Split('=')[1], out int merge))
{
if (merge == 1 && ForceMerging == ForceMerging.None)
{
ForceMerging = ForceMerging.Full;
}
}
}
// If we have the refname tag
else if (line.ToLowerInvariant().StartsWith("refname="))
{
Name = (String.IsNullOrWhiteSpace(Name) ? line.Split('=')[1] : Name);
Name = (string.IsNullOrWhiteSpace(Name) ? line.Split('=')[1] : Name);
}
// If we have a rom
else if (line.StartsWith("¬"))
{
// Some old RC DATs have this behavior
if (line.Contains("¬N¬O"))
{
line = line.Replace("¬N¬O", "") + "¬¬";
}
line = line.Replace("¬N¬O", string.Empty) + "¬¬";
/*
The rominfo order is as follows:
@@ -165,9 +163,7 @@ namespace SabreTools.Library.DatFiles
// Try getting the size separately
if (!Int64.TryParse(rominfo[7], out long size))
{
size = 0;
}
Rom rom = new Rom
{
@@ -204,13 +200,13 @@ namespace SabreTools.Library.DatFiles
{
try
{
Globals.Logger.User("Opening file for writing: {0}", outfile);
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = Utilities.TryCreate(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.Logger.Warning("File '{0}' could not be created for writing! Please check to see if the file is writable", outfile);
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
@@ -250,7 +246,7 @@ namespace SabreTools.Library.DatFiles
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName);
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
((Rom)rom).Size = Constants.SizeZero;
@@ -293,18 +289,18 @@ namespace SabreTools.Library.DatFiles
{
try
{
string header = header = "[CREDITS]\n" +
"author=" + Author + "\n" +
"version=" + Version + "\n" +
"comment=" + Comment + "\n" +
"[DAT]\n" +
"version=2.50\n" +
"split=" + (ForceMerging == ForceMerging.Split ? "1" : "0") + "\n" +
"merge=" + (ForceMerging == ForceMerging.Full || ForceMerging == ForceMerging.Merged ? "1" : "0") + "\n" +
"[EMULATOR]\n" +
"refname=" + Name + "\n" +
"version=" + Description + "\n" +
"[GAMES]\n";
string header = "[CREDITS]\n";
header += $"author={Author}\n";
header += $"version={Version}\n";
header += $"comment={Comment}\n";
header += "[DAT]\n";
header += "version=2.50\n";
header += $"split={(ForceMerging == ForceMerging.Split ? "1" : "0")}\n";
header += $"merge={(ForceMerging == ForceMerging.Full || ForceMerging == ForceMerging.Merged ? "1" : "0")}\n";
header += "[EMULATOR]\n";
header += $"refname={Name}\n";
header += $"version={Description}\n";
header += "[GAMES]\n";
// Write the header out
sw.Write(header);
@@ -323,44 +319,59 @@ namespace SabreTools.Library.DatFiles
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="rom">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>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem rom, bool ignoreblanks = false)
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false)
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks
&& (rom.ItemType == ItemType.Rom
&& (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1)))
{
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
}
try
{
string state = "";
string state = string.Empty;
// Pre-process the item name
ProcessItemName(rom, true);
ProcessItemName(datItem, true);
if (rom.ItemType == ItemType.Rom)
// Build the state based on excluded fields
switch (datItem.ItemType)
{
state += "¬" + (!ExcludeFields[(int)Field.CloneOf] && String.IsNullOrWhiteSpace(rom.CloneOf) ? WebUtility.HtmlEncode(rom.CloneOf) : "") +
"¬" + (!ExcludeFields[(int)Field.CloneOf] && String.IsNullOrWhiteSpace(rom.CloneOf) ? WebUtility.HtmlEncode(rom.CloneOf) : "") +
"¬" + (!ExcludeFields[(int)Field.MachineName] ? WebUtility.HtmlEncode(rom.MachineName) : "") +
"¬" + (!ExcludeFields[(int)Field.Description] ? WebUtility.HtmlEncode((String.IsNullOrWhiteSpace(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) : "") +
"¬" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") +
"¬" + (!ExcludeFields[(int)Field.CRC] ? ((Rom)rom).CRC.ToLowerInvariant() : "") +
"¬" + (!ExcludeFields[(int)Field.Size] && ((Rom)rom).Size != -1 ? ((Rom)rom).Size.ToString() : "") + "¬¬¬\n";
}
else if (rom.ItemType == ItemType.Disk)
{
state += "¬" + (!ExcludeFields[(int)Field.CloneOf] && String.IsNullOrWhiteSpace(rom.CloneOf) ? WebUtility.HtmlEncode(rom.CloneOf) : "") +
"¬" + (!ExcludeFields[(int)Field.CloneOf] && String.IsNullOrWhiteSpace(rom.CloneOf) ? WebUtility.HtmlEncode(rom.CloneOf) : "") +
"¬" + (!ExcludeFields[(int)Field.MachineName] ? WebUtility.HtmlEncode(rom.MachineName) : "") +
"¬" + (!ExcludeFields[(int)Field.Description] ? WebUtility.HtmlEncode((String.IsNullOrWhiteSpace(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) : "") +
"¬" + (!ExcludeFields[(int)Field.Name] ? WebUtility.HtmlEncode(rom.Name) : "") +
"¬¬¬¬¬\n";
case ItemType.Disk:
state += "¬";
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
state += datItem.CloneOf;
state += "¬";
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
state += datItem.CloneOf;
state += $"¬{datItem.GetField(Field.MachineName, ExcludeFields)}";
if (string.IsNullOrWhiteSpace(datItem.MachineDescription))
state += $"¬{datItem.GetField(Field.MachineName, ExcludeFields)}";
else
state += $"¬{datItem.GetField(Field.Description, ExcludeFields)}";
state += $"¬{datItem.GetField(Field.Name, ExcludeFields)}";
state += "¬¬¬¬¬\n";
break;
case ItemType.Rom:
var rom = datItem as Rom;
state += "¬";
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
state += datItem.CloneOf;
state += "¬";
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CloneOf, ExcludeFields)))
state += datItem.CloneOf;
state += $"¬{datItem.GetField(Field.MachineName, ExcludeFields)}";
if (string.IsNullOrWhiteSpace(datItem.MachineDescription))
state += $"¬{datItem.GetField(Field.MachineName, ExcludeFields)}";
else
state += $"¬{datItem.GetField(Field.Description, ExcludeFields)}";
state += $"¬{datItem.GetField(Field.Name, ExcludeFields)}";
state += $"¬{datItem.GetField(Field.CRC, ExcludeFields)}";
state += $"¬{datItem.GetField(Field.Size, ExcludeFields)}";
state += "¬¬¬\n";
break;
}
sw.Write(state);