diff --git a/src/LibHac/Keyset.cs b/src/LibHac/Keyset.cs index 5fe2064e..724daf8a 100644 --- a/src/LibHac/Keyset.cs +++ b/src/LibHac/Keyset.cs @@ -491,26 +491,15 @@ namespace LibHac public static class ExternalKeyReader { private const int TitleKeySize = 0x10; - - public static readonly Dictionary CommonKeyDict; - public static readonly Dictionary UniqueKeyDict; - public static readonly Dictionary AllKeyDict; - - static ExternalKeyReader() - { - List commonKeys = CreateCommonKeyList(); - List uniqueKeys = CreateUniqueKeyList(); - - CommonKeyDict = commonKeys.ToDictionary(k => k.Name, k => k); - UniqueKeyDict = uniqueKeys.ToDictionary(k => k.Name, k => k); - AllKeyDict = uniqueKeys.Concat(commonKeys).ToDictionary(k => k.Name, k => k); - } - + public static void ReadKeyFile(Keyset keyset, string filename, string titleKeysFilename = null, string consoleKeysFilename = null, IProgressReport logger = null) { - if (filename != null) ReadMainKeys(keyset, filename, AllKeyDict, logger); - if (consoleKeysFilename != null) ReadMainKeys(keyset, consoleKeysFilename, AllKeyDict, logger); + Dictionary keyDictionary = CreateFullKeyDictionary(); + + if (filename != null) ReadMainKeys(keyset, filename, keyDictionary, logger); + if (consoleKeysFilename != null) ReadMainKeys(keyset, consoleKeysFilename, keyDictionary, logger); if (titleKeysFilename != null) ReadTitleKeys(keyset, titleKeysFilename, logger); + keyset.ExternalKeySet.TrimExcess(); keyset.DeriveKeys(logger); } @@ -526,13 +515,15 @@ namespace LibHac public static void LoadConsoleKeys(this Keyset keyset, string filename, IProgressReport logger = null) { - foreach (KeyValue key in UniqueKeyDict.Values) + Dictionary uniqueKeyDictionary = CreateUniqueKeyDictionary(); + + foreach (KeyValue key in uniqueKeyDictionary.Values) { byte[] keyBytes = key.GetKey(keyset); Array.Clear(keyBytes, 0, keyBytes.Length); } - ReadMainKeys(keyset, filename, UniqueKeyDict, logger); + ReadMainKeys(keyset, filename, uniqueKeyDictionary, logger); keyset.DeriveKeys(); } @@ -650,17 +641,17 @@ namespace LibHac public static string PrintCommonKeys(Keyset keyset) { - return PrintKeys(keyset, CommonKeyDict); + return PrintKeys(keyset, CreateCommonKeyDictionary()); } public static string PrintUniqueKeys(Keyset keyset) { - return PrintKeys(keyset, UniqueKeyDict); + return PrintKeys(keyset, CreateUniqueKeyDictionary()); } public static string PrintAllKeys(Keyset keyset) { - return PrintKeys(keyset, AllKeyDict); + return PrintKeys(keyset, CreateFullKeyDictionary()); } public static string PrintTitleKeys(Keyset keyset) @@ -676,6 +667,24 @@ namespace LibHac return sb.ToString(); } + public static Dictionary CreateCommonKeyDictionary() + { + return CreateCommonKeyList().ToDictionary(k => k.Name, k => k); + } + + public static Dictionary CreateUniqueKeyDictionary() + { + return CreateUniqueKeyList().ToDictionary(k => k.Name, k => k); + } + + public static Dictionary CreateFullKeyDictionary() + { + List commonKeys = CreateCommonKeyList(); + List uniqueKeys = CreateUniqueKeyList(); + + return uniqueKeys.Concat(commonKeys).ToDictionary(k => k.Name, k => k); + } + private static List CreateCommonKeyList() { var keys = new List diff --git a/src/hactoolnet/CliParser.cs b/src/hactoolnet/CliParser.cs index c03d6785..11e44030 100644 --- a/src/hactoolnet/CliParser.cs +++ b/src/hactoolnet/CliParser.cs @@ -7,7 +7,7 @@ namespace hactoolnet { internal static class CliParser { - private static readonly CliOption[] CliOptions = + private static CliOption[] GetCliOptions() => new[] { new CliOption("custom", 0, (o, a) => o.RunCustom = true), new CliOption("intype", 't', 1, (o, a) => o.InFileType = ParseFileType(a[0])), @@ -73,6 +73,8 @@ namespace hactoolnet var options = new Options(); bool inputSpecified = false; + CliOption[] cliOptions = GetCliOptions(); + for (int i = 0; i < args.Length; i++) { string arg; @@ -98,7 +100,7 @@ namespace hactoolnet continue; } - CliOption option = CliOptions.FirstOrDefault(x => x.Long == arg || x.Short == arg); + CliOption option = cliOptions.FirstOrDefault(x => x.Long == arg || x.Short == arg); if (option == null) { PrintWithUsage($"Unknown option {args[i]}"); @@ -260,6 +262,7 @@ namespace hactoolnet ArgsNeeded = argsNeeded; Assigner = assigner; } + public CliOption(string longName, int argsNeeded, Action assigner) { Long = longName;