Decrypt savefile in-place (less allocation)

Each player and main.dat is ~11MB of allocation to read; old would read & decrypt into separate array = 22MB * (1 + playerCount). Cutting this in half reduces the amount of allocation and keeps stuff off gen2 LOH
This commit is contained in:
Kurt
2020-04-17 14:00:34 -07:00
parent 456575e64e
commit be0a456067
2 changed files with 7 additions and 10 deletions

View File

@@ -21,12 +21,11 @@ private static byte[] GetParam(uint[] data, in int index)
}
/// <summary>
/// Decrypts the <see cref="encData"/> using the <see cref="headerData"/>.
/// Decrypts the <see cref="encData"/> using the <see cref="headerData"/> in place.
/// </summary>
/// <param name="headerData">Header Data</param>
/// <param name="encData">Encrypted SaveData</param>
/// <returns>Decrypted SaveData</returns>
public static byte[] Decrypt(byte[] headerData, byte[] encData)
public static void Decrypt(byte[] headerData, byte[] encData)
{
// First 256 bytes go unused
var importantData = new uint[0x80];
@@ -41,10 +40,8 @@ public static byte[] Decrypt(byte[] headerData, byte[] encData)
// Do the AES
using var aesCtr = new Aes128CounterMode(counter);
var transform = aesCtr.CreateDecryptor(key, counter);
var decData = new byte[encData.Length];
transform.TransformBlock(encData, 0, encData.Length, decData, 0);
return decData;
transform.TransformBlock(encData, 0, encData.Length, encData, 0);
}
private static CryptoFile GenerateHeaderFile(uint seed, byte[] versionData)