Fix 2byte misalign on PH1

Closes #3882
This commit is contained in:
Kurt 2023-05-14 18:48:29 -07:00
parent 2bdee8c87d
commit 5acae160c6

View file

@ -15,14 +15,13 @@ public sealed class PKH : PKM, IHandlerLanguage, IFormArgument, IHomeTrack, IBat
public GameDataPB8? DataPB8 { get; private set; }
public GameDataPK9? DataPK9 { get; private set; }
private const int HeaderSize = 0x14;
public override EntityContext Context => EntityContext.None;
public PKH(byte[] data) : base(DecryptHome(data))
{
var mem = Data.AsMemory(HeaderSize);
var mem = Data.AsMemory(HomeCrypto.SIZE_1HEADER + 2);
var core = mem[..CoreDataSize];
var side = mem.Slice(core.Length, GameDataSize);
var side = mem.Slice(core.Length + 2, GameDataSize);
_coreData = new GameDataCore(core);
ReadGameData1(side);
@ -266,20 +265,22 @@ public sealed class PKH : PKM, IHandlerLanguage, IFormArgument, IHomeTrack, IBat
// Header and Core are already in the current byte array.
// Write each part, starting with header and core.
int ctr = HeaderSize;
int ctr = HomeCrypto.SIZE_1HEADER + 2;
ctr += _coreData.WriteTo(result.AsSpan(ctr));
var gameDataLengthSpan = result.AsSpan(ctr, 2); ctr += 2;
int gameDataStart = ctr;
if (DataPK8 is { } pk8) ctr += pk8.WriteTo(result.AsSpan(ctr));
if (DataPB7 is { } pb7) ctr += pb7.WriteTo(result.AsSpan(ctr));
if (DataPA8 is { } pa8) ctr += pa8.WriteTo(result.AsSpan(ctr));
if (DataPB8 is { } pb8) ctr += pb8.WriteTo(result.AsSpan(ctr));
if (DataPK9 is { } pk9) ctr += pk9.WriteTo(result.AsSpan(ctr));
WriteUInt16LittleEndian(gameDataLengthSpan, GameDataSize = (ushort)(ctr - gameDataStart));
// Update metadata to ensure we're a valid object.
DataVersion = HomeCrypto.Version1;
EncodedDataSize = (ushort)(result.Length - HomeCrypto.SIZE_1HEADER);
CoreDataSize = HomeCrypto.SIZE_1CORE;
GameDataSize = (ushort)(ctr - GameDataStart);
Data.AsSpan()[..HeaderSize].CopyTo(result); // Copy updated header.
Data.AsSpan()[..(HomeCrypto.SIZE_1HEADER + 2)].CopyTo(result); // Copy updated header & CoreData length.
return result;
}