From c25dfbe2922789474b541a82d945fe6390e64aa4 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 24 Oct 2024 20:42:59 -0400 Subject: [PATCH] Add debug flag, fix description in help text --- Headerer/Extract.cs | 10 ++++++---- Headerer/Options.cs | 13 ++++++++++++- Headerer/Restore.cs | 10 ++++++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Headerer/Extract.cs b/Headerer/Extract.cs index 0863d7e9..78e44e94 100644 --- a/Headerer/Extract.cs +++ b/Headerer/Extract.cs @@ -15,8 +15,9 @@ namespace Headerer /// Name of the file to be parsed /// Output directory to write the file to, empty means the same directory as the input file /// True if headers should not be stored in the database, false otherwise + /// Enable additional log statements for debugging /// True if the output file was created, false otherwise - 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 /// String representing the header bytes /// SHA-1 of the deheadered file /// Name of the source skipper file - private static void AddHeaderToDatabase(string header, string SHA1, string source) + /// Enable additional log statements for debugging + 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 diff --git a/Headerer/Options.cs b/Headerer/Options.cs index 837dba1a..e6306658 100644 --- a/Headerer/Options.cs +++ b/Headerer/Options.cs @@ -17,6 +17,11 @@ namespace Headerer /// public Feature Feature { get; private set; } = Feature.NONE; + /// + /// Output debug statements to console + /// + public bool Debug { get; private set; } = false; + /// /// Optional output directory /// @@ -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"); } } } \ No newline at end of file diff --git a/Headerer/Restore.cs b/Headerer/Restore.cs index 28c07230..809b1110 100644 --- a/Headerer/Restore.cs +++ b/Headerer/Restore.cs @@ -14,8 +14,9 @@ namespace Headerer /// /// Name of the file to be parsed /// Output directory to write the file to, empty means the same directory as the input file + /// Enable additional log statements for debugging /// True if a header was found and appended, false otherwise - 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 headers = RetrieveHeadersFromDatabase(sha1); + List 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 /// /// SHA-1 of the deheadered file + /// Enable additional log statements for debugging /// List of strings representing the headers to add - private static List RetrieveHeadersFromDatabase(string SHA1) + private static List 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)); } }