mirror of
https://github.com/kwsch/PKHeX
synced 2024-12-22 18:33:14 +00:00
47071b41f3
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to. `System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API. The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder. Other Fixes included: - The Super Training UI for Gen6 has been reworked according to the latest block structure additions. - Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list). - Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
79 lines
No EOL
2.8 KiB
C#
79 lines
No EOL
2.8 KiB
C#
using System;
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
|
|
|
namespace PKHeX.Core
|
|
{
|
|
public sealed class MysteryBlock5 : SaveBlock
|
|
{
|
|
private const int FlagStart = 0;
|
|
private const int MaxReceivedFlag = 2048;
|
|
private const int MaxCardsPresent = 12;
|
|
private const int FlagRegionSize = (MaxReceivedFlag / 8); // 0x100
|
|
private const int CardStart = FlagStart + FlagRegionSize;
|
|
|
|
private const int DataSize = 0xA90;
|
|
private int SeedOffset => Offset + DataSize;
|
|
|
|
// Everything is stored encrypted, and only decrypted on demand. Only crypt on object fetch...
|
|
public MysteryBlock5(SAV5BW sav, int offset) : base(sav) => Offset = offset;
|
|
public MysteryBlock5(SAV5B2W2 sav, int offset) : base(sav) => Offset = offset;
|
|
|
|
public EncryptedMysteryGiftAlbum GiftAlbum
|
|
{
|
|
get
|
|
{
|
|
uint seed = ReadUInt32LittleEndian(Data.AsSpan(SeedOffset));
|
|
byte[] wcData = SAV.GetData(Offset + FlagStart, 0xA90); // Encrypted, Decrypt
|
|
return GetAlbum(seed, wcData);
|
|
}
|
|
set
|
|
{
|
|
var wcData = SetAlbum(value);
|
|
// Write Back
|
|
wcData.CopyTo(Data, Offset + FlagStart);
|
|
WriteUInt32LittleEndian(Data.AsSpan(SeedOffset), value.Seed);
|
|
}
|
|
}
|
|
|
|
private static EncryptedMysteryGiftAlbum GetAlbum(uint seed, byte[] wcData)
|
|
{
|
|
PokeCrypto.CryptArray(wcData, seed);
|
|
|
|
var flags = new bool[MaxReceivedFlag];
|
|
var gifts = new DataMysteryGift[MaxCardsPresent];
|
|
var Info = new EncryptedMysteryGiftAlbum(gifts, flags, seed);
|
|
|
|
// 0x100 Bytes for Used Flags
|
|
for (int i = 0; i < Info.Flags.Length; i++)
|
|
Info.Flags[i] = (wcData[i / 8] >> i % 8 & 0x1) == 1;
|
|
// 12 PGFs
|
|
for (int i = 0; i < Info.Gifts.Length; i++)
|
|
{
|
|
var data = wcData.AsSpan(CardStart + (i * PGF.Size), PGF.Size).ToArray();
|
|
Info.Gifts[i] = new PGF(data);
|
|
}
|
|
|
|
return Info;
|
|
}
|
|
|
|
private static byte[] SetAlbum(EncryptedMysteryGiftAlbum value)
|
|
{
|
|
byte[] wcData = new byte[0xA90];
|
|
|
|
// Toss back into byte[]
|
|
for (int i = 0; i < value.Flags.Length; i++)
|
|
{
|
|
if (value.Flags[i])
|
|
wcData[i / 8] |= (byte) (1 << (i & 7));
|
|
}
|
|
|
|
var span = wcData.AsSpan(CardStart);
|
|
for (int i = 0; i < value.Gifts.Length; i++)
|
|
value.Gifts[i].Data.CopyTo(span[(i * PGF.Size)..]);
|
|
|
|
// Decrypted, Encrypt
|
|
PokeCrypto.CryptArray(wcData, value.Seed);
|
|
return wcData;
|
|
}
|
|
}
|
|
} |