2021-05-14 19:30:40 +00:00
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2021-05-14 19:30:40 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gen3 <see cref="GameVersion.RSBOX"/> Block Info
|
|
|
|
/// </summary>
|
|
|
|
public sealed class BlockInfoRSBOX : BlockInfo
|
2018-02-19 19:33:37 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public readonly uint SaveCount;
|
|
|
|
private const int ChecksumRegionSize = 0x1FF8;
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public BlockInfoRSBOX(ReadOnlySpan<byte> data, int offset)
|
|
|
|
{
|
|
|
|
Offset = offset;
|
|
|
|
Length = 4 + ChecksumRegionSize;
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Values stored in Big Endian format
|
|
|
|
ID = ReadUInt32BigEndian(data[(Offset + 4)..]);
|
|
|
|
SaveCount = ReadUInt32BigEndian(data[(Offset + 8)..]);
|
|
|
|
}
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool ChecksumValid(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
var chk = GetChecksum(data);
|
|
|
|
var old = ReadUInt32BigEndian(data[Offset..]);
|
|
|
|
return chk == old;
|
|
|
|
}
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetChecksum(Span<byte> data)
|
|
|
|
{
|
|
|
|
var chk = GetChecksum(data);
|
|
|
|
var span = data[Offset..];
|
|
|
|
WriteUInt32BigEndian(span, chk);
|
|
|
|
}
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private uint GetChecksum(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
var span = data.Slice(Offset + 4, ChecksumRegionSize);
|
|
|
|
return Checksums.CheckSum16BigInvert(span);
|
2018-02-19 19:33:37 +00:00
|
|
|
}
|
2021-03-08 19:39:15 +00:00
|
|
|
}
|