mirror of
https://github.com/kwsch/NHSE.git
synced 2026-09-07 18:55:18 -05:00
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:
@@ -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)
|
||||
|
||||
@@ -29,10 +29,10 @@ protected EncryptedFilePair(string folder, string name)
|
||||
var hd = File.ReadAllBytes(hdr);
|
||||
var md = File.ReadAllBytes(dat);
|
||||
|
||||
var decrypted = Encryption.Decrypt(hd, md);
|
||||
Encryption.Decrypt(hd, md);
|
||||
|
||||
Header = hd;
|
||||
Data = decrypted;
|
||||
Data = md;
|
||||
DataPath = dat;
|
||||
HeaderPath = hdr;
|
||||
|
||||
@@ -76,7 +76,7 @@ public IEnumerable<FileHashRegion> InvalidHashes()
|
||||
}
|
||||
}
|
||||
|
||||
public string GetString(int offset, int maxLength) => StringUtil.GetString(Data, offset, maxLength);
|
||||
public static byte[] GetBytes(string value, int maxLength) => StringUtil.GetBytes(value, maxLength);
|
||||
protected string GetString(int offset, int maxLength) => StringUtil.GetString(Data, offset, maxLength);
|
||||
protected static byte[] GetBytes(string value, int maxLength) => StringUtil.GetBytes(value, maxLength);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user