Skip to content

Commit

Permalink
hactoolnet: Don't output unknown key warnings by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Thealexbarney committed Oct 18, 2023
1 parent 33446b4 commit cd4004a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/hactoolnet/CliParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private static CliOption[] GetCliOptions() => new[]
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]),
Expand Down Expand Up @@ -254,6 +255,7 @@ private static string GetUsage()
sb.AppendLine(" --titlekeys <file> Load title keys from an external file.");
sb.AppendLine(" --accesslog <file> 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:");
Expand Down
1 change: 1 addition & 0 deletions src/hactoolnet/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 deletions src/hactoolnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static void RunTask(Context ctx)
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
Expand Down Expand Up @@ -224,7 +224,7 @@ private static void OpenKeySet(Context ctx)

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.
Expand Down Expand Up @@ -283,6 +283,32 @@ private static void ProcessKeygen(Context ctx)
}
}

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)
Expand Down

0 comments on commit cd4004a

Please sign in to comment.