Add debug flag, fix description in help text

This commit is contained in:
Matt Nadareski
2024-10-24 20:42:59 -04:00
parent 5bd6ecd88c
commit c25dfbe292
3 changed files with 24 additions and 9 deletions

View File

@@ -15,8 +15,9 @@ namespace Headerer
/// <param name="file">Name of the file to be parsed</param>
/// <param name="outDir">Output directory to write the file to, empty means the same directory as the input file</param>
/// <param name="nostore">True if headers should not be stored in the database, false otherwise</param>
/// <param name="debug">Enable additional log statements for debugging</param>
/// <returns>True if the output file was created, false otherwise</returns>
public static bool DetectTransformStore(string file, string? outDir, bool nostore)
public static bool DetectTransformStore(string file, string? outDir, bool nostore, bool debug = false)
{
// Create the output directory if it doesn't exist
if (!string.IsNullOrWhiteSpace(outDir) && !Directory.Exists(outDir))
@@ -62,7 +63,7 @@ namespace Headerer
if (!nostore)
{
string sha1 = HashTool.GetFileHash(newfile, HashType.SHA1) ?? string.Empty;
AddHeaderToDatabase(hstr, sha1, rule.SourceFile!);
AddHeaderToDatabase(hstr, sha1, rule.SourceFile!, debug);
}
return true;
@@ -74,7 +75,8 @@ namespace Headerer
/// <param name="header">String representing the header bytes</param>
/// <param name="SHA1">SHA-1 of the deheadered file</param>
/// <param name="type">Name of the source skipper file</param>
private static void AddHeaderToDatabase(string header, string SHA1, string source)
/// <param name="debug">Enable additional log statements for debugging</param>
private static void AddHeaderToDatabase(string header, string SHA1, string source, bool debug)
{
// Ensure the database exists
Database.EnsureDatabase();
@@ -92,7 +94,7 @@ namespace Headerer
{
query = $"INSERT INTO data (sha1, header, type) VALUES ('{SHA1}', '{header}', '{source}')";
slc = new SqliteCommand(query, dbc);
Console.WriteLine($"Result of inserting header: {slc.ExecuteNonQuery()}"); // TODO: Gate behind debug flag
if (debug) Console.WriteLine($"Result of inserting header: {slc.ExecuteNonQuery()}");
}
// Dispose of database objects

View File

@@ -17,6 +17,11 @@ namespace Headerer
/// </summary>
public Feature Feature { get; private set; } = Feature.NONE;
/// <summary>
/// Output debug statements to console
/// </summary>
public bool Debug { get; private set; } = false;
/// <summary>
/// Optional output directory
/// </summary>
@@ -71,6 +76,11 @@ namespace Headerer
string arg = args[index];
switch (arg)
{
case "-dbg":
case "--debug":
options.Debug = true;
break;
case "-o":
case "--outdir":
options.OutputDir = index + 1 < args.Length ? args[++index] : string.Empty;
@@ -120,10 +130,11 @@ namespace Headerer
Console.WriteLine();
Console.WriteLine("Common options:");
Console.WriteLine("-?, -h, --help Display this help text and quit");
Console.WriteLine("-dbg, --debug Enable debug logging statements");
Console.WriteLine("-o, --outdir [PATH] Set output directory");
Console.WriteLine();
Console.WriteLine("Extraction options:");
Console.WriteLine("-nsh, --no-store-header Set output path for extraction (required)");
Console.WriteLine("-nsh, --no-store-header Don't store the extracted header");
}
}
}

View File

@@ -14,8 +14,9 @@ namespace Headerer
/// </summary>
/// <param name="file">Name of the file to be parsed</param>
/// <param name="outDir">Output directory to write the file to, empty means the same directory as the input file</param>
/// <param name="debug">Enable additional log statements for debugging</param>
/// <returns>True if a header was found and appended, false otherwise</returns>
public static bool RestoreHeader(string file, string? outDir)
public static bool RestoreHeader(string file, string? outDir, bool debug = false)
{
// Create the output directory if it doesn't exist
if (!string.IsNullOrWhiteSpace(outDir) && !Directory.Exists(outDir))
@@ -25,7 +26,7 @@ namespace Headerer
string sha1 = HashTool.GetFileHash(file, HashType.SHA1) ?? string.Empty;
// Retrieve a list of all related headers from the database
List<string> headers = RetrieveHeadersFromDatabase(sha1);
List<string> headers = RetrieveHeadersFromDatabase(sha1, debug);
// If we have nothing retrieved, we return false
if (headers.Count == 0)
@@ -47,8 +48,9 @@ namespace Headerer
/// Retrieve headers from the database
/// </summary>
/// <param name="SHA1">SHA-1 of the deheadered file</param>
/// <param name="debug">Enable additional log statements for debugging</param>
/// <returns>List of strings representing the headers to add</returns>
private static List<string> RetrieveHeadersFromDatabase(string SHA1)
private static List<string> RetrieveHeadersFromDatabase(string SHA1, bool debug)
{
// Ensure the database exists
Database.EnsureDatabase();
@@ -68,7 +70,7 @@ namespace Headerer
{
while (sldr.Read())
{
Console.WriteLine($"Found match with rom type '{sldr.GetString(1)}'"); // TODO: Gate behind debug flag
if (debug) Console.WriteLine($"Found match with rom type '{sldr.GetString(1)}'");
headers.Add(sldr.GetString(0));
}
}