PKHeX/PKHeX.Core/Saves/Blocks/BlockInfoRSBOX.cs
Kurt 3e8527f11a Minor tweaks
Reduce allocation in a few places, delete unused stuff, more concise expressions.
2022-05-07 21:25:26 -07:00

44 lines
1.3 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
/// <summary>
/// Gen3 <see cref="GameVersion.RSBOX"/> Block Info
/// </summary>
public sealed class BlockInfoRSBOX : BlockInfo
{
public readonly uint SaveCount;
private const int ChecksumRegionSize = 0x1FF8;
public BlockInfoRSBOX(ReadOnlySpan<byte> data, int offset)
{
Offset = offset;
Length = 4 + ChecksumRegionSize;
// Values stored in Big Endian format
ID = ReadUInt32BigEndian(data[(Offset + 4)..]);
SaveCount = ReadUInt32BigEndian(data[(Offset + 8)..]);
}
protected override bool ChecksumValid(Span<byte> data)
{
var chk = GetChecksum(data);
var old = ReadUInt32BigEndian(data[Offset..]);
return chk == old;
}
protected override void SetChecksum(Span<byte> data)
{
var chk = GetChecksum(data);
var span = data[Offset..];
WriteUInt32BigEndian(span, chk);
}
private uint GetChecksum(Span<byte> data)
{
var span = data.Slice(Offset + 4, ChecksumRegionSize);
return Checksums.CheckSum16BigInvert(span);
}
}
}