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:
@@ -1,20 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
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
|
||||
@@ -66,13 +58,13 @@ namespace SabreTools.Library.DatFiles
|
||||
Encoding enc = Utilities.GetEncoding(filename);
|
||||
StreamReader sr = new StreamReader(Utilities.TryOpenRead(filename), enc);
|
||||
|
||||
string gamename = "";
|
||||
string gamename = string.Empty;
|
||||
while (!sr.EndOfStream)
|
||||
{
|
||||
string line = sr.ReadLine().Trim();
|
||||
|
||||
// If we have a blank line, we just skip it
|
||||
if (String.IsNullOrWhiteSpace(line))
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -86,7 +78,7 @@ namespace SabreTools.Library.DatFiles
|
||||
// If we have the beginning of a game, set the name of the game
|
||||
else if (line.StartsWith("ROMs required for"))
|
||||
{
|
||||
gamename = Regex.Match(line, @"^ROMs required for \S*? ""(.*?)""\.").Groups[1].Value;
|
||||
gamename = Regex.Match(line, @"^ROMs required for \S*? string.Empty(.*?)string.Empty\.").Groups[1].Value;
|
||||
}
|
||||
|
||||
// If we have a machine with no required roms (usually internal devices), skip it
|
||||
@@ -99,20 +91,16 @@ namespace SabreTools.Library.DatFiles
|
||||
else
|
||||
{
|
||||
// First, we preprocess the line so that the rom name is consistently correct
|
||||
string romname = "";
|
||||
string romname = string.Empty;
|
||||
string[] split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// If the line doesn't have the 4 spaces of padding, check for 3
|
||||
if (split.Length == 1)
|
||||
{
|
||||
split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
|
||||
// If the split is still unsuccessful, log it and skip
|
||||
if (split.Length == 1)
|
||||
{
|
||||
Globals.Logger.Warning("Possibly malformed line: '{0}'", line);
|
||||
}
|
||||
Globals.Logger.Warning($"Possibly malformed line: '{line}'");
|
||||
|
||||
romname = split[0];
|
||||
line = line.Substring(romname.Length);
|
||||
@@ -153,9 +141,7 @@ namespace SabreTools.Library.DatFiles
|
||||
else if (split.Length == 3)
|
||||
{
|
||||
if (!Int64.TryParse(split[0], out long size))
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
Rom rom = new Rom()
|
||||
{
|
||||
@@ -188,9 +174,7 @@ namespace SabreTools.Library.DatFiles
|
||||
else if (split.Length == 5 && line.EndsWith("BAD_DUMP"))
|
||||
{
|
||||
if (!Int64.TryParse(split[0], out long size))
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
Rom rom = new Rom()
|
||||
{
|
||||
@@ -210,9 +194,7 @@ namespace SabreTools.Library.DatFiles
|
||||
else if (split.Length == 5 && line.EndsWith("NO GOOD DUMP KNOWN"))
|
||||
{
|
||||
if (!Int64.TryParse(split[0], out long size))
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
Rom rom = new Rom()
|
||||
{
|
||||
@@ -229,7 +211,7 @@ namespace SabreTools.Library.DatFiles
|
||||
// If we have something else, it's invalid
|
||||
else
|
||||
{
|
||||
Globals.Logger.Warning("Invalid line detected: '{0} {1}'", romname, line);
|
||||
Globals.Logger.Warning($"Invalid line detected: '{romname} {line}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,13 +228,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;
|
||||
}
|
||||
|
||||
@@ -285,22 +267,18 @@ 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 (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
|
||||
{
|
||||
WriteEndGame(sw);
|
||||
}
|
||||
|
||||
// If we have a new game, output the beginning of the new item
|
||||
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
|
||||
{
|
||||
WriteStartGame(sw, rom);
|
||||
}
|
||||
|
||||
// If we have a "null" game (created by DATFromDir or something similar), log it to file
|
||||
if (rom.ItemType == ItemType.Rom
|
||||
&& ((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;
|
||||
@@ -344,13 +322,11 @@ namespace SabreTools.Library.DatFiles
|
||||
try
|
||||
{
|
||||
// No game should start with a path separator
|
||||
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
rom.MachineName = rom.MachineName.Substring(1);
|
||||
}
|
||||
rom.MachineName = rom.MachineName.TrimStart(Path.DirectorySeparatorChar);
|
||||
|
||||
string state = "ROMs required for driver \"" + (!ExcludeFields[(int)Field.MachineName] ? rom.MachineName : "") + "\".\n" +
|
||||
"Name Size Checksum\n";
|
||||
// Build the state based on excluded fields
|
||||
string state = $"ROMs required for driver \"{rom.GetField(Field.MachineName, ExcludeFields)}\".\n";
|
||||
state += "Name Size Checksum\n";
|
||||
|
||||
sw.Write(state);
|
||||
sw.Flush();
|
||||
@@ -391,110 +367,94 @@ 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);
|
||||
|
||||
switch (rom.ItemType)
|
||||
// Build the state based on excluded fields
|
||||
switch (datItem.ItemType)
|
||||
{
|
||||
case ItemType.Archive:
|
||||
case ItemType.BiosSet:
|
||||
case ItemType.Release:
|
||||
case ItemType.Sample:
|
||||
// We don't output these at all
|
||||
// We don't output these at all for Listrom
|
||||
break;
|
||||
|
||||
case ItemType.Disk:
|
||||
var disk = datItem as Disk;
|
||||
|
||||
// The name is padded out to a particular length
|
||||
if (rom.Name.Length < 43)
|
||||
{
|
||||
state += rom.Name.PadRight(43, ' ');
|
||||
}
|
||||
if (disk.Name.Length < 43)
|
||||
state += disk.Name.PadRight(43, ' ');
|
||||
else
|
||||
{
|
||||
state += rom.Name + " ";
|
||||
}
|
||||
state += disk.Name + " ";
|
||||
|
||||
// If we have a baddump, put the first indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Disk)rom).ItemStatus == ItemStatus.BadDump)
|
||||
{
|
||||
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus == ItemStatus.BadDump)
|
||||
state += " BAD";
|
||||
}
|
||||
|
||||
// If we have a nodump, write out the indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Disk)rom).ItemStatus == ItemStatus.Nodump)
|
||||
{
|
||||
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus == ItemStatus.Nodump)
|
||||
state += " NO GOOD DUMP KNOWN";
|
||||
}
|
||||
|
||||
// Otherwise, write out the SHA-1 hash
|
||||
else if (!ExcludeFields[(int)Field.SHA1])
|
||||
{
|
||||
state += " SHA1(" + ((Disk)rom).SHA1 + ")";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
|
||||
state += $" SHA1({disk.SHA1})";
|
||||
|
||||
// If we have a baddump, put the second indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Disk)rom).ItemStatus == ItemStatus.BadDump)
|
||||
{
|
||||
if (!ExcludeFields[(int)Field.Status] && disk.ItemStatus == ItemStatus.BadDump)
|
||||
state += " BAD_DUMP";
|
||||
}
|
||||
|
||||
state += "\n";
|
||||
break;
|
||||
|
||||
case ItemType.Rom:
|
||||
var rom = datItem as Rom;
|
||||
|
||||
// The name is padded out to a particular length
|
||||
if (rom.Name.Length < 40)
|
||||
{
|
||||
state += rom.Name.PadRight(43 - (((Rom)rom).Size.ToString().Length), ' ');
|
||||
}
|
||||
state += rom.Name.PadRight(43 - rom.Size.ToString().Length, ' ');
|
||||
else
|
||||
{
|
||||
state += rom.Name + " ";
|
||||
}
|
||||
|
||||
// If we don't have a nodump, write out the size
|
||||
if (((Rom)rom).ItemStatus != ItemStatus.Nodump)
|
||||
{
|
||||
state += ((Rom)rom).Size;
|
||||
}
|
||||
if (rom.ItemStatus != ItemStatus.Nodump)
|
||||
state += rom.Size;
|
||||
|
||||
// If we have a baddump, put the first indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Rom)rom).ItemStatus == ItemStatus.BadDump)
|
||||
{
|
||||
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus == ItemStatus.BadDump)
|
||||
state += " BAD";
|
||||
}
|
||||
|
||||
// If we have a nodump, write out the indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Rom)rom).ItemStatus == ItemStatus.Nodump)
|
||||
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus == ItemStatus.Nodump)
|
||||
{
|
||||
state += " NO GOOD DUMP KNOWN";
|
||||
}
|
||||
// Otherwise, write out the CRC and SHA-1 hashes
|
||||
else
|
||||
{
|
||||
state += (!ExcludeFields[(int)Field.CRC] ? " CRC(" + ((Rom)rom).CRC + ")" : "");
|
||||
state += (!ExcludeFields[(int)Field.SHA1] ? " SHA1(" + ((Rom)rom).SHA1 + ")" : "");
|
||||
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields)))
|
||||
state += $" CRC({rom.CRC})";
|
||||
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SHA1, ExcludeFields)))
|
||||
state += $" SHA1({rom.SHA1})";
|
||||
}
|
||||
|
||||
// If we have a baddump, put the second indicator
|
||||
if (!ExcludeFields[(int)Field.Status] && ((Rom)rom).ItemStatus == ItemStatus.BadDump)
|
||||
{
|
||||
if (!ExcludeFields[(int)Field.Status] && rom.ItemStatus == ItemStatus.BadDump)
|
||||
state += " BAD_DUMP";
|
||||
}
|
||||
|
||||
state += "\n";
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user