mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Reduce memory used by parsing CLI arguments and keys
These actions are usually only performed once per program run, but the objects they used were stored as static properties where they couldn't be cleared from memory. Free up a small bit of memory by only storing them locally. Reduces memory used by ~150 KB and reduces object count by over 1500
This commit is contained in:
parent
cccb811293
commit
fcf46b1822
2 changed files with 36 additions and 24 deletions
|
@ -491,26 +491,15 @@ namespace LibHac
|
|||
public static class ExternalKeyReader
|
||||
{
|
||||
private const int TitleKeySize = 0x10;
|
||||
|
||||
public static readonly Dictionary<string, KeyValue> CommonKeyDict;
|
||||
public static readonly Dictionary<string, KeyValue> UniqueKeyDict;
|
||||
public static readonly Dictionary<string, KeyValue> AllKeyDict;
|
||||
|
||||
static ExternalKeyReader()
|
||||
{
|
||||
List<KeyValue> commonKeys = CreateCommonKeyList();
|
||||
List<KeyValue> 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<string, KeyValue> 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<string, KeyValue> 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<string, KeyValue> CreateCommonKeyDictionary()
|
||||
{
|
||||
return CreateCommonKeyList().ToDictionary(k => k.Name, k => k);
|
||||
}
|
||||
|
||||
public static Dictionary<string, KeyValue> CreateUniqueKeyDictionary()
|
||||
{
|
||||
return CreateUniqueKeyList().ToDictionary(k => k.Name, k => k);
|
||||
}
|
||||
|
||||
public static Dictionary<string, KeyValue> CreateFullKeyDictionary()
|
||||
{
|
||||
List<KeyValue> commonKeys = CreateCommonKeyList();
|
||||
List<KeyValue> uniqueKeys = CreateUniqueKeyList();
|
||||
|
||||
return uniqueKeys.Concat(commonKeys).ToDictionary(k => k.Name, k => k);
|
||||
}
|
||||
|
||||
private static List<KeyValue> CreateCommonKeyList()
|
||||
{
|
||||
var keys = new List<KeyValue>
|
||||
|
|
|
@ -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<Options, string[]> assigner)
|
||||
{
|
||||
Long = longName;
|
||||
|
|
Loading…
Reference in a new issue