From dd478db70aba3e0a16fd66f4bd9e162ddedd6cf4 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Tue, 17 Oct 2023 22:17:40 -0700 Subject: [PATCH] hactoolnet: Don't output unknown key warnings by default --- src/hactoolnet/CliParser.cs | 2 ++ src/hactoolnet/Options.cs | 1 + src/hactoolnet/Program.cs | 30 ++++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/hactoolnet/CliParser.cs b/src/hactoolnet/CliParser.cs index f8ae51c6..525ca19f 100644 --- a/src/hactoolnet/CliParser.cs +++ b/src/hactoolnet/CliParser.cs @@ -18,6 +18,7 @@ internal static class CliParser new CliOption("dev", 'd', 0, (o, _) => o.UseDevKeys = true), new CliOption("enablehash", 'h', 0, (o, _) => o.EnableHash = true), new CliOption("disablekeywarns", 0, (o, _) => o.DisableKeyWarns = true), + new CliOption("enableallkeywarns", 0, (o, _) => o.EnableAllKeyWarns = true), new CliOption("keyset", 'k', 1, (o, a) => o.Keyfile = a[0]), new CliOption("titlekeys", 1, (o, a) => o.TitleKeyFile = a[0]), new CliOption("consolekeys", 1, (o, a) => o.ConsoleKeyFile = a[0]), @@ -254,6 +255,7 @@ internal static class CliParser sb.AppendLine(" --titlekeys Load title keys from an external file."); sb.AppendLine(" --accesslog Specify the access log file path."); sb.AppendLine(" --disablekeywarns Disables warning output when loading external keys."); + sb.AppendLine(" --enableallkeywarns Enables warning output when loading unknown external keys."); sb.AppendLine(" --version Display version information and exit."); sb.AppendLine(" --help Display this help and exit."); sb.AppendLine("NCA options:"); diff --git a/src/hactoolnet/Options.cs b/src/hactoolnet/Options.cs index a0ab9032..77d1a458 100644 --- a/src/hactoolnet/Options.cs +++ b/src/hactoolnet/Options.cs @@ -17,6 +17,7 @@ internal class Options public bool UseDevKeys; public bool EnableHash; public bool DisableKeyWarns; + public bool EnableAllKeyWarns; public string Keyfile; public string TitleKeyFile; public string ConsoleKeyFile; diff --git a/src/hactoolnet/Program.cs b/src/hactoolnet/Program.cs index 75cd16b3..e4e49369 100644 --- a/src/hactoolnet/Program.cs +++ b/src/hactoolnet/Program.cs @@ -190,7 +190,7 @@ public static class Program private static void OpenKeySet(Context ctx) { #if NATIVEAOT_NO_REFLECTION - string home = HomeFolder.GetFolderPath(Environment.SpecialFolder.UserProfile); + string home = HomeFolder.GetFolderPath(Environment.SpecialFolder.UserProfile); #else string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); #endif @@ -224,7 +224,7 @@ public static class Program var keySet = KeySet.CreateDefaultKeySet(); - IProgressReport logger = ctx.Options.DisableKeyWarns ? null : ctx.Logger; + IProgressReport logger = GetKeySetReaderLogger(ctx); // If the user specifies a key file then only load that file into the mode they specified, // otherwise load both prod.keys and dev.keys. @@ -283,6 +283,32 @@ public static class Program } } + private static IProgressReport GetKeySetReaderLogger(Context ctx) + { + if (ctx.Options.DisableKeyWarns) return null; + if (ctx.Options.EnableAllKeyWarns) return ctx.Logger; + return new UnknownKeyFilteringLogger(ctx.Logger); + } + + // Key dumpers output keys LibHac doesn't read. This can cause a lot of noise in the CLI output, + // so we'll remove those messages. + private class UnknownKeyFilteringLogger : IProgressReport + { + private IProgressReport _baseLogger; + public UnknownKeyFilteringLogger(IProgressReport baseLogger) => _baseLogger = baseLogger; + + public void Report(long value) => _baseLogger.Report(value); + public void ReportAdd(long value) => _baseLogger.ReportAdd(value); + public void SetTotal(long value) => _baseLogger.SetTotal(value); + + public void LogMessage(string message) + { + if (!message.StartsWith("Failed to match key")) + _baseLogger.LogMessage(message); + } + } + + // For running random stuff // ReSharper disable once UnusedParameter.Local private static void CustomTask(Context ctx)