mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
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:
@@ -22,7 +22,7 @@ namespace SabreTools.Library.DatFiles
|
||||
internal class SeparatedValue : DatFile
|
||||
{
|
||||
// Private instance variables specific to Separated Value DATs
|
||||
char _delim;
|
||||
private readonly char _delim;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor designed for casting a base DatFile
|
||||
@@ -81,85 +81,103 @@ namespace SabreTools.Library.DatFiles
|
||||
case "file name":
|
||||
columns.Add("DatFile.FileName");
|
||||
break;
|
||||
|
||||
case "internal name":
|
||||
columns.Add("DatFile.Name");
|
||||
break;
|
||||
|
||||
case "description":
|
||||
case "dat description":
|
||||
columns.Add("DatFile.Description");
|
||||
break;
|
||||
|
||||
case "game name":
|
||||
case "game":
|
||||
case "machine":
|
||||
columns.Add("Machine.Name");
|
||||
break;
|
||||
|
||||
case "game description":
|
||||
columns.Add("Description");
|
||||
break;
|
||||
|
||||
case "type":
|
||||
columns.Add("DatItem.Type");
|
||||
break;
|
||||
|
||||
case "rom":
|
||||
case "romname":
|
||||
case "rom name":
|
||||
case "name":
|
||||
columns.Add("Rom.Name");
|
||||
break;
|
||||
|
||||
case "disk":
|
||||
case "diskname":
|
||||
case "disk name":
|
||||
columns.Add("Disk.Name");
|
||||
break;
|
||||
|
||||
case "size":
|
||||
columns.Add("DatItem.Size");
|
||||
break;
|
||||
|
||||
case "crc":
|
||||
case "crc hash":
|
||||
columns.Add("DatItem.CRC");
|
||||
break;
|
||||
|
||||
case "md5":
|
||||
case "md5 hash":
|
||||
columns.Add("DatItem.MD5");
|
||||
break;
|
||||
|
||||
case "ripemd":
|
||||
case "ripemd160":
|
||||
case "ripemd hash":
|
||||
case "ripemd160 hash":
|
||||
columns.Add("DatItem.RIPEMD160");
|
||||
break;
|
||||
|
||||
case "sha1":
|
||||
case "sha-1":
|
||||
case "sha1 hash":
|
||||
case "sha-1 hash":
|
||||
columns.Add("DatItem.SHA1");
|
||||
break;
|
||||
|
||||
case "sha256":
|
||||
case "sha-256":
|
||||
case "sha256 hash":
|
||||
case "sha-256 hash":
|
||||
columns.Add("DatItem.SHA256");
|
||||
break;
|
||||
|
||||
case "sha384":
|
||||
case "sha-384":
|
||||
case "sha384 hash":
|
||||
case "sha-384 hash":
|
||||
columns.Add("DatItem.SHA384");
|
||||
break;
|
||||
|
||||
case "sha512":
|
||||
case "sha-512":
|
||||
case "sha512 hash":
|
||||
case "sha-512 hash":
|
||||
columns.Add("DatItem.SHA512");
|
||||
break;
|
||||
|
||||
case "nodump":
|
||||
case "no dump":
|
||||
case "status":
|
||||
case "item status":
|
||||
columns.Add("DatItem.Nodump");
|
||||
break;
|
||||
|
||||
case "date":
|
||||
columns.Add("DatItem.Date");
|
||||
break;
|
||||
|
||||
default:
|
||||
columns.Add("INVALID");
|
||||
break;
|
||||
@@ -175,7 +193,7 @@ namespace SabreTools.Library.DatFiles
|
||||
// If the line doesn't have the correct number of columns, we log and skip
|
||||
if (parsedLine.Length != columns.Count)
|
||||
{
|
||||
Globals.Logger.Warning("Malformed line found in '{0}' at line {1}", filename, linenum);
|
||||
Globals.Logger.Warning($"Malformed line found in '{filename}' at line {linenum}");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -193,57 +211,72 @@ namespace SabreTools.Library.DatFiles
|
||||
switch (columns[i])
|
||||
{
|
||||
case "DatFile.FileName":
|
||||
FileName = (String.IsNullOrWhiteSpace(FileName) ? value : FileName);
|
||||
FileName = (string.IsNullOrWhiteSpace(FileName) ? value : FileName);
|
||||
break;
|
||||
|
||||
case "DatFile.Name":
|
||||
Name = (String.IsNullOrWhiteSpace(Name) ? value : Name);
|
||||
Name = (string.IsNullOrWhiteSpace(Name) ? value : Name);
|
||||
break;
|
||||
|
||||
case "DatFile.Description":
|
||||
Description = (String.IsNullOrWhiteSpace(Description) ? value : Description);
|
||||
Description = (string.IsNullOrWhiteSpace(Description) ? value : Description);
|
||||
break;
|
||||
|
||||
case "Machine.Name":
|
||||
machineName = value;
|
||||
break;
|
||||
|
||||
case "Description":
|
||||
machineDesc = value;
|
||||
break;
|
||||
|
||||
case "DatItem.Type":
|
||||
itemType = Utilities.GetItemType(value) ?? ItemType.Rom;
|
||||
break;
|
||||
|
||||
case "Rom.Name":
|
||||
case "Disk.Name":
|
||||
name = String.IsNullOrWhiteSpace(value) ? name : value;
|
||||
name = string.IsNullOrWhiteSpace(value) ? name : value;
|
||||
break;
|
||||
|
||||
case "DatItem.Size":
|
||||
if (!Int64.TryParse(value, out size))
|
||||
{
|
||||
size = -1;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "DatItem.CRC":
|
||||
crc = Utilities.CleanHashData(value, Constants.CRCLength);
|
||||
break;
|
||||
|
||||
case "DatItem.MD5":
|
||||
md5 = Utilities.CleanHashData(value, Constants.MD5Length);
|
||||
break;
|
||||
|
||||
case "DatItem.RIPEMD160":
|
||||
ripemd160 = Utilities.CleanHashData(value, Constants.RIPEMD160Length);
|
||||
break;
|
||||
|
||||
case "DatItem.SHA1":
|
||||
sha1 = Utilities.CleanHashData(value, Constants.SHA1Length);
|
||||
break;
|
||||
|
||||
case "DatItem.SHA256":
|
||||
sha256 = Utilities.CleanHashData(value, Constants.SHA256Length);
|
||||
break;
|
||||
|
||||
case "DatItem.SHA384":
|
||||
sha384 = Utilities.CleanHashData(value, Constants.SHA384Length);
|
||||
break;
|
||||
|
||||
case "DatItem.SHA512":
|
||||
sha512 = Utilities.CleanHashData(value, Constants.SHA512Length);
|
||||
break;
|
||||
|
||||
case "DatItem.Nodump":
|
||||
status = Utilities.GetItemStatus(value);
|
||||
break;
|
||||
|
||||
case "DatItem.Date":
|
||||
date = value;
|
||||
break;
|
||||
@@ -264,6 +297,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
ParseAddHelper(archive, clean, remUnicode);
|
||||
break;
|
||||
|
||||
case ItemType.BiosSet:
|
||||
BiosSet biosset = new BiosSet()
|
||||
{
|
||||
@@ -275,6 +309,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
ParseAddHelper(biosset, clean, remUnicode);
|
||||
break;
|
||||
|
||||
case ItemType.Disk:
|
||||
Disk disk = new Disk()
|
||||
{
|
||||
@@ -294,6 +329,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
ParseAddHelper(disk, clean, remUnicode);
|
||||
break;
|
||||
|
||||
case ItemType.Release:
|
||||
Release release = new Release()
|
||||
{
|
||||
@@ -305,6 +341,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
ParseAddHelper(release, clean, remUnicode);
|
||||
break;
|
||||
|
||||
case ItemType.Rom:
|
||||
Rom rom = new Rom()
|
||||
{
|
||||
@@ -327,6 +364,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
ParseAddHelper(rom, clean, remUnicode);
|
||||
break;
|
||||
|
||||
case ItemType.Sample:
|
||||
Sample sample = new Sample()
|
||||
{
|
||||
@@ -352,13 +390,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;
|
||||
}
|
||||
|
||||
@@ -394,7 +432,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}");
|
||||
}
|
||||
|
||||
// Now, output the rom data
|
||||
@@ -444,91 +482,91 @@ 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;
|
||||
}
|
||||
|
||||
// TODO: Clean up this mess and make it more like the other DatFile types
|
||||
// TODO: Specifically, make it so that each ItemType has its own block, if possible
|
||||
try
|
||||
{
|
||||
// Initialize all strings
|
||||
string state = "",
|
||||
pre = "",
|
||||
post = "",
|
||||
type = "",
|
||||
romname = "",
|
||||
diskname = "",
|
||||
size = "",
|
||||
crc = "",
|
||||
md5 = "",
|
||||
ripemd160 = "",
|
||||
sha1 = "",
|
||||
sha256 = "",
|
||||
sha384 = "",
|
||||
sha512 = "",
|
||||
status = "";
|
||||
string state = string.Empty,
|
||||
pre = string.Empty,
|
||||
post = string.Empty,
|
||||
type = string.Empty,
|
||||
romname = string.Empty,
|
||||
diskname = string.Empty,
|
||||
size = string.Empty,
|
||||
crc = string.Empty,
|
||||
md5 = string.Empty,
|
||||
ripemd160 = string.Empty,
|
||||
sha1 = string.Empty,
|
||||
sha256 = string.Empty,
|
||||
sha384 = string.Empty,
|
||||
sha512 = string.Empty,
|
||||
status = string.Empty;
|
||||
|
||||
// Separated values should only output Rom and Disk
|
||||
if (rom.ItemType != ItemType.Disk && rom.ItemType != ItemType.Rom)
|
||||
{
|
||||
if (datItem.ItemType != ItemType.Disk && datItem.ItemType != ItemType.Rom)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (rom.ItemType == ItemType.Rom)
|
||||
{
|
||||
type = "rom";
|
||||
romname = rom.Name;
|
||||
size = ((Rom)rom).Size.ToString();
|
||||
crc = ((Rom)rom).CRC;
|
||||
md5 = ((Rom)rom).MD5;
|
||||
ripemd160 = ((Rom)rom).RIPEMD160;
|
||||
sha1 = ((Rom)rom).SHA1;
|
||||
sha256 = ((Rom)rom).SHA256;
|
||||
sha384 = ((Rom)rom).SHA384;
|
||||
sha512 = ((Rom)rom).SHA512;
|
||||
status = (((Rom)rom).ItemStatus != ItemStatus.None ? "\"" + ((Rom)rom).ItemStatus.ToString() + "\"" : "\"\"");
|
||||
}
|
||||
else if (rom.ItemType == ItemType.Disk)
|
||||
if (datItem.ItemType == ItemType.Disk)
|
||||
{
|
||||
var disk = datItem as Disk;
|
||||
type = "disk";
|
||||
diskname = rom.Name;
|
||||
md5 = ((Disk)rom).MD5;
|
||||
ripemd160 = ((Disk)rom).RIPEMD160;
|
||||
sha1 = ((Disk)rom).SHA1;
|
||||
sha256 = ((Disk)rom).SHA256;
|
||||
sha384 = ((Disk)rom).SHA384;
|
||||
sha512 = ((Disk)rom).SHA512;
|
||||
status = (((Disk)rom).ItemStatus != ItemStatus.None ? "\"" + ((Disk)rom).ItemStatus.ToString() + "\"" : "\"\"");
|
||||
diskname = datItem.Name;
|
||||
md5 = disk.MD5;
|
||||
ripemd160 = disk.RIPEMD160;
|
||||
sha1 = disk.SHA1;
|
||||
sha256 = disk.SHA256;
|
||||
sha384 = disk.SHA384;
|
||||
sha512 = disk.SHA512;
|
||||
status = (disk.ItemStatus != ItemStatus.None ? $"\"{disk.ItemStatus}\"" : "\"\"");
|
||||
}
|
||||
else if (datItem.ItemType == ItemType.Rom)
|
||||
{
|
||||
var rom = datItem as Rom;
|
||||
type = "rom";
|
||||
romname = datItem.Name;
|
||||
size = rom.Size.ToString();
|
||||
crc = rom.CRC;
|
||||
md5 = rom.MD5;
|
||||
ripemd160 = rom.RIPEMD160;
|
||||
sha1 = rom.SHA1;
|
||||
sha256 = rom.SHA256;
|
||||
sha384 = rom.SHA384;
|
||||
sha512 = rom.SHA512;
|
||||
status = (rom.ItemStatus != ItemStatus.None ? $"\"{rom.ItemStatus}\"" : "\"\"");
|
||||
}
|
||||
|
||||
pre = CreatePrefixPostfix(rom, true);
|
||||
post = CreatePrefixPostfix(rom, false);
|
||||
string inline = string.Format("\"" + FileName + "\""
|
||||
+ "{0}\"" + Name + "\""
|
||||
+ "{0}\"" + Description + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.MachineName] ? rom.MachineName : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.Description] ? rom.MachineDescription : "") + "\""
|
||||
+ "{0}\"" + type + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.Name] ? romname : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.Name] ? diskname : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.Size] ? size : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.CRC] ? crc : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.MD5] ? md5 : "") + "\""
|
||||
// + "{0}\"" + (!ExcludeFields[(int)Field.RIPEMD160] ? ripemd160 : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.SHA1] ? sha1 : "") + "\""
|
||||
+ "{0}\"" + (!ExcludeFields[(int)Field.SHA256] ? sha256 : "") + "\""
|
||||
// + "{0}\"" + (!ExcludeFields[(int)Field.SHA384] ? sha384 : "") + "\""
|
||||
// + "{0}\"" + (!ExcludeFields[(int)Field.SHA512] ? sha512 : "") + "\""
|
||||
+ "{0}" + status, _delim);
|
||||
state += pre + inline + post + "\n";
|
||||
pre = CreatePrefixPostfix(datItem, true);
|
||||
post = CreatePrefixPostfix(datItem, false);
|
||||
string inline = string.Format($"\"{FileName}\""
|
||||
+ $"{0}\"{Name}\""
|
||||
+ $"{0}\"{Description}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.MachineName] ? datItem.MachineName : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.Description] ? datItem.MachineDescription : string.Empty)}\""
|
||||
+ $"{0}\"{type}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.Name] ? romname : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.Name] ? diskname : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.Size] ? size : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.CRC] ? crc : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.MD5] ? md5 : string.Empty)}\""
|
||||
// + $"{0}\"{(!ExcludeFields[(int)Field.RIPEMD160] ? ripemd160 : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.SHA1] ? sha1 : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.SHA256] ? sha256 : string.Empty)}\""
|
||||
// + $"{0}\"{(!ExcludeFields[(int)Field.SHA384] ? sha384 : string.Empty)}\""
|
||||
// + $"{0}\"{(!ExcludeFields[(int)Field.SHA512] ? sha512 : string.Empty)}\""
|
||||
+ $"{0}\"{(!ExcludeFields[(int)Field.Status] ? status : string.Empty)}\"",
|
||||
_delim);
|
||||
|
||||
state += $"{pre}{inline}{post}\n";
|
||||
|
||||
sw.Write(state);
|
||||
sw.Flush();
|
||||
|
||||
Reference in New Issue
Block a user