Decrypt sav8 blocks in-place

Removes 1.5MB of allocation; we don't need to preserve the encrypted state.

Revise savefile clone operation to use its own constructor, as we need to call the base constructor first (backup file clone...) prior to decrypting.

Expose CryptStaticXorpadBytes as GetDecryptedRawData no longer exists.
This commit is contained in:
Kurt 2021-06-04 13:16:00 -07:00
parent 3764e60b37
commit 041074f26c
3 changed files with 18 additions and 19 deletions

View file

@ -45,7 +45,7 @@ namespace PKHeX.Core
0xA4, 0x48, 0xB3, 0x50, 0x9E, 0x14, 0xA0, 0x52, 0xDE, 0x7E, 0x10, 0x2B, 0x1B, 0x77, 0x6E,
};
private static void CryptStaticXorpadBytes(byte[] data)
public static void CryptStaticXorpadBytes(byte[] data)
{
var xp = StaticXorpad;
for (var i = 0; i < data.Length - SIZE_HASH; i++)
@ -94,7 +94,7 @@ namespace PKHeX.Core
}
/// <summary>
/// Decrypts the save data.
/// Decrypts the save data in-place, then unpacks the blocks.
/// </summary>
/// <param name="data">Encrypted save data</param>
/// <returns>Decrypted blocks.</returns>
@ -103,19 +103,8 @@ namespace PKHeX.Core
/// </remarks>
public static IReadOnlyList<SCBlock> Decrypt(byte[] data)
{
var temp = GetDecryptedRawData(data);
return ReadBlocks(temp);
}
/// <summary>
/// Decrypts the save data, with raw block data concatenated together.
/// </summary>
public static byte[] GetDecryptedRawData(byte[] data)
{
// de-ref from input data, since we're going to modify the contents in-place
var temp = (byte[])data.Clone();
CryptStaticXorpadBytes(temp);
return temp;
CryptStaticXorpadBytes(data);
return ReadBlocks(data);
}
private const int BlockDataRatioEstimate1 = 777; // bytes per block, on average (generous)

View file

@ -16,7 +16,7 @@ namespace PKHeX.Core
public override IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return gen <= 8; // future: change to <= when HOME released
return gen <= 8;
}).ToArray();
protected SAV8(byte[] data) : base(data) { }

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
@ -9,8 +8,13 @@ namespace PKHeX.Core
/// </summary>
public sealed class SAV8SWSH : SAV8, ISaveBlock8SWSH, ITrainerStatRecord, ISaveFileRevision
{
public SAV8SWSH(byte[] data) : this(data, SwishCrypto.Decrypt(data))
public SAV8SWSH(byte[] data) : base(data)
{
Data = Array.Empty<byte>();
AllBlocks = SwishCrypto.Decrypt(data);
Blocks = new SaveBlockAccessor8SWSH(this);
SaveRevision = Zukan.GetRevision();
Initialize();
}
private SAV8SWSH(byte[] data, IReadOnlyList<SCBlock> blocks) : base(data)
@ -99,7 +103,13 @@ namespace PKHeX.Core
}
#endregion
protected override SaveFile CloneInternal() => new SAV8SWSH(State.BAK, AllBlocks.Select(z => z.Clone()).ToArray());
protected override SaveFile CloneInternal()
{
var blockCopy = new SCBlock[AllBlocks.Count];
for (int i = 0; i < AllBlocks.Count; i++)
blockCopy[i] = AllBlocks[i].Clone();
return new SAV8SWSH(State.BAK, blockCopy);
}
private int m_spec, m_item, m_move, m_abil;
public override int MaxMoveID => m_move;