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