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,14 +1,9 @@
using Mono.Data.Sqlite;
using System;
using System;
using System.IO;
using System.Collections.Generic;
using SabreTools.Library.Data;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
#endif
using Mono.Data.Sqlite;
namespace SabreTools.Library.Tools
{
@@ -34,19 +29,16 @@ namespace SabreTools.Library.Tools
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
dbc.Open();
string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'";
string query = $"SELECT * FROM data WHERE sha1='{SHA1}' AND header='{header}'";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
exists = sldr.HasRows;
if (!exists)
{
query = @"INSERT INTO data (sha1, header, type) VALUES ('" +
SHA1 + "', " +
"'" + header + "', " +
"'" + source + "')";
query = $"INSERT INTO data (sha1, header, type) VALUES ('{SHA1}', '{header}', '{source}')";
slc = new SqliteCommand(query, dbc);
Globals.Logger.Verbose("Result of inserting header: {0}", slc.ExecuteNonQuery());
Globals.Logger.Verbose($"Result of inserting header: {slc.ExecuteNonQuery()}");
}
// Dispose of database objects
@@ -68,9 +60,7 @@ namespace SabreTools.Library.Tools
// Make sure the file exists
if (!File.Exists(db))
{
SqliteConnection.CreateFile(db);
}
// Open the database connection
SqliteConnection dbc = new SqliteConnection(connectionString);
@@ -174,7 +164,7 @@ CREATE TABLE IF NOT EXISTS data (
// Create the output list of headers
List<string> headers = new List<string>();
string query = @"SELECT header, type FROM data WHERE sha1='" + SHA1 + "'";
string query = $"SELECT header, type FROM data WHERE sha1='{SHA1}'";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
@@ -182,7 +172,7 @@ CREATE TABLE IF NOT EXISTS data (
{
while (sldr.Read())
{
Globals.Logger.Verbose("Found match with rom type '{0}'", sldr.GetString(1));
Globals.Logger.Verbose($"Found match with rom type '{sldr.GetString(1)}'");
headers.Add(sldr.GetString(0));
}
}

View File

@@ -9,6 +9,9 @@ using SabreTools.Library.Data;
namespace SabreTools.Library.Tools
{
/// <summary>
/// Async hashing class wraper
/// </summary>
public class Hasher
{
public Hash HashType { get; private set; }
@@ -64,6 +67,9 @@ namespace SabreTools.Library.Tools
_hasher.Dispose();
}
/// <summary>
/// Process a buffer of some length with the internal hash algorithm
/// </summary>
public async Task Process(byte[] buffer, int size)
{
switch (HashType)
@@ -86,6 +92,9 @@ namespace SabreTools.Library.Tools
}
}
/// <summary>
/// Finalize the internal hash algorigthm
/// </summary>
public async Task Finalize()
{
byte[] emptyBuffer = new byte[0];
@@ -108,6 +117,9 @@ namespace SabreTools.Library.Tools
}
}
/// <summary>
/// Get internal hash as a byte array
/// </summary>
public byte[] GetHash()
{
switch (HashType)

View File

@@ -17,7 +17,7 @@ namespace SabreTools.Library.Tools
/// </summary>
public InternalStopwatch()
{
_subject = "";
_subject = string.Empty;
}
/// <summary>
@@ -30,24 +30,13 @@ namespace SabreTools.Library.Tools
Start();
}
/// <summary>
/// Constructor that initalizes the stopwatch with a subject and starts immediately
/// </summary>
/// <param name="subject">Subject of the stopwatch</param>
/// <param name="more">Parameters to format the string</param>
public InternalStopwatch(string subject, params object[] more)
{
_subject = string.Format(subject, more);
Start();
}
/// <summary>
/// Start the stopwatch and display subject text
/// </summary>
public void Start()
{
_startTime = DateTime.Now;
Globals.Logger.User("{0}...", _subject);
Globals.Logger.User($"{_subject}...");
}
/// <summary>
@@ -60,23 +49,12 @@ namespace SabreTools.Library.Tools
Start();
}
/// <summary>
/// Start the stopwatch and display subject text
/// </summary>
/// <param name="subject">Text to show on stopwatch start</param>
/// <param name="more">Parameters to format the string</param>
public void Start(string subject, params object[] more)
{
_subject = string.Format(subject, more);
Start();
}
/// <summary>
/// End the stopwatch and display subject text
/// </summary>
public void Stop()
{
Globals.Logger.User("{0} completed in {1}", _subject, DateTime.Now.Subtract(_startTime).ToString(@"hh\:mm\:ss\.fffff"));
Globals.Logger.User($"{_subject} completed in {DateTime.Now.Subtract(_startTime).ToString("hh:mm:ss.fffff")}");
}
}
}

View File

@@ -1,18 +1,10 @@
using System;
using System.IO;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using StreamWriter = System.IO.StreamWriter;
#endif
namespace SabreTools.Library.Tools
{
/// <summary>
@@ -58,13 +50,11 @@ namespace SabreTools.Library.Tools
_tofile = tofile;
_warnings = false;
_errors = false;
_filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")." + Utilities.GetExtension(filename);
_filename = $"{Path.GetFileNameWithoutExtension(filename)} ({DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")}).{Utilities.GetExtension(filename)}";
_filter = filter;
if (!Directory.Exists(_basepath))
{
Directory.CreateDirectory(_basepath);
}
Start();
}
@@ -77,9 +67,7 @@ namespace SabreTools.Library.Tools
{
_start = DateTime.Now;
if (!_tofile)
{
return true;
}
try
{
@@ -87,8 +75,8 @@ namespace SabreTools.Library.Tools
_log = new StreamWriter(logfile, Encoding.UTF8, (int)(4 * Constants.KibiByte), true);
_log.AutoFlush = true;
_log.WriteLine("Logging started " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
_log.WriteLine(string.Format("Command run: {0}", Globals.CommandLineArgs));
_log.WriteLine($"Logging started {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
_log.WriteLine($"Command run: {Globals.CommandLineArgs}");
}
catch
{
@@ -108,38 +96,31 @@ namespace SabreTools.Library.Tools
if (!suppress)
{
if (_warnings)
{
Console.WriteLine("There were warnings in the last run! Check the log for more details");
}
if (_errors)
{
Console.WriteLine("There were errors in the last run! Check the log for more details");
}
TimeSpan span = DateTime.Now.Subtract(_start);
// Special case for multi-day runs
string total = "";
string total = string.Empty;
if (span >= TimeSpan.FromDays(1))
{
total = span.ToString(@"d\:hh\:mm\:ss");
}
else
{
total = span.ToString(@"hh\:mm\:ss");
}
if (!_tofile)
{
Console.WriteLine("Total runtime: " + total);
Console.WriteLine($"Total runtime: {total}");
return true;
}
try
{
_log.WriteLine("Logging ended " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
_log.WriteLine("Total runtime: " + total);
Console.WriteLine("Total runtime: " + total);
_log.WriteLine($"Logging ended {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
_log.WriteLine($"Total runtime: {total}");
Console.WriteLine($"Total runtime: {total}");
_log.Close();
}
catch
@@ -173,15 +154,11 @@ namespace SabreTools.Library.Tools
{
// If the log level is less than the filter level, we skip it but claim we didn't
if (loglevel < _filter)
{
return true;
}
// USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
{
Console.WriteLine((loglevel == LogLevel.ERROR && appendPrefix ? loglevel.ToString() + " " : "") + output);
}
Console.WriteLine((loglevel == LogLevel.ERROR && appendPrefix ? loglevel.ToString() + " " : string.Empty) + output);
// If we're writing to file, use the existing stream
if (_tofile)
@@ -190,7 +167,7 @@ namespace SabreTools.Library.Tools
{
lock(_lock)
{
_log.WriteLine((appendPrefix ? loglevel.ToString() + " - " + DateTime.Now + " - " : "") + output);
_log.WriteLine((appendPrefix ? $"{loglevel} - {DateTime.Now} - " : string.Empty) + output);
}
}
catch (Exception ex)
@@ -230,7 +207,7 @@ namespace SabreTools.Library.Tools
{
lock (_lock)
{
_log.Write(DateTime.Now + " - " + output);
_log.Write($"{DateTime.Now} - {output}");
}
}
catch
@@ -243,38 +220,15 @@ namespace SabreTools.Library.Tools
return true;
}
/// <summary>
/// Write the given string as a verbose message to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>s
public bool Verbose(string output, params object[] args)
{
return Log(args.Length == 0 ? output: string.Format(output, args), LogLevel.VERBOSE, true);
}
/// <summary>
/// Write the given string as a verbose message to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="appendPrefix">True if the level and datetime should be prepended to each statement (default), false otherwise</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Verbose(string output, bool appendPrefix = true, params object[] args)
public bool Verbose(string output, bool appendPrefix = true)
{
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.VERBOSE, appendPrefix);
}
/// <summary>
/// Write the given string as a user message to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool User(string output, params object[] args)
{
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.USER, true);
return Log(output, LogLevel.VERBOSE, appendPrefix);
}
/// <summary>
@@ -282,23 +236,10 @@ namespace SabreTools.Library.Tools
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="appendPrefix">True if the level and datetime should be prepended to each statement (default), false otherwise</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool User(string output, bool appendPrefix = true, params object[] args)
public bool User(string output, bool appendPrefix = true)
{
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.USER, appendPrefix);
}
/// <summary>
/// Write the given string as a warning to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Warning(string output, params object[] args)
{
_warnings = true;
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.WARNING, true);
return Log(output, LogLevel.USER, appendPrefix);
}
/// <summary>
@@ -306,24 +247,11 @@ namespace SabreTools.Library.Tools
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="appendPrefix">True if the level and datetime should be prepended to each statement (default), false otherwise</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Warning(string output, bool appendPrefix = true, params object[] args)
public bool Warning(string output, bool appendPrefix = true)
{
_warnings = true;
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.WARNING, appendPrefix);
}
/// <summary>
/// Writes the given string as an error in the log
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Error(string output, params object[] args)
{
_errors = true;
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.ERROR, true);
return Log(output, LogLevel.WARNING, appendPrefix);
}
/// <summary>
@@ -331,12 +259,11 @@ namespace SabreTools.Library.Tools
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="appendPrefix">True if the level and datetime should be prepended to each statement (default), false otherwise</param>
/// <param name="args">Optional arguments for string formatting</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Error(string output, bool appendPrefix = true, params object[] args)
public bool Error(string output, bool appendPrefix = true)
{
_errors = true;
return Log(args.Length == 0 ? output : string.Format(output, args), LogLevel.ERROR, appendPrefix);
return Log(output, LogLevel.ERROR, appendPrefix);
}
/// <summary>

File diff suppressed because it is too large Load Diff