2018-02-19 19:33:37 +00:00
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gen5 Block Info
|
|
|
|
/// </summary>
|
|
|
|
public sealed class BlockInfoNDS : BlockInfo
|
2018-02-19 19:33:37 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private readonly int ChecksumOffset;
|
|
|
|
private readonly int ChecksumMirror;
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public BlockInfoNDS(int offset, int length, int chkOffset, int chkMirror)
|
|
|
|
{
|
|
|
|
Offset = offset;
|
|
|
|
Length = length;
|
|
|
|
ChecksumOffset = chkOffset;
|
|
|
|
ChecksumMirror = chkMirror;
|
|
|
|
}
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private ushort GetChecksum(ReadOnlySpan<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length));
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool ChecksumValid(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
ushort chk = GetChecksum(data);
|
|
|
|
if (chk != ReadUInt16LittleEndian(data[ChecksumOffset..]))
|
|
|
|
return false;
|
|
|
|
if (chk != ReadUInt16LittleEndian(data[ChecksumMirror..]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetChecksum(Span<byte> data)
|
|
|
|
{
|
|
|
|
ushort chk = GetChecksum(data);
|
|
|
|
WriteUInt16LittleEndian(data[ChecksumOffset..], chk);
|
|
|
|
WriteUInt16LittleEndian(data[ChecksumMirror..], chk);
|
2018-02-19 19:33:37 +00:00
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
}
|