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
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
{
|
2018-07-29 20:27:48 +00:00
|
|
|
/// <summary>
|
2019-06-09 02:56:11 +00:00
|
|
|
/// Gen6+ Block Info (inside BEEF chunk)
|
2018-07-29 20:27:48 +00:00
|
|
|
/// </summary>
|
2019-06-09 02:56:11 +00:00
|
|
|
public abstract class BlockInfo3DS : BlockInfo
|
2018-02-19 19:33:37 +00:00
|
|
|
{
|
2019-06-09 02:56:11 +00:00
|
|
|
private readonly int BlockInfoOffset;
|
|
|
|
|
|
|
|
// ==chunk def== @ BlockInfoOffset
|
|
|
|
// u64 timestamp1
|
|
|
|
// u64 timestamp2
|
|
|
|
// u8[4] BEEF magic
|
|
|
|
// n*{blockInfo}, where n varies per sav type
|
|
|
|
|
|
|
|
// ==block info def==
|
|
|
|
// u32 length
|
|
|
|
// u16 id
|
|
|
|
// u16 checksum
|
|
|
|
|
|
|
|
// when stored, each block size is rounded up to nearest 0x200, and the next chunk is immediately after
|
|
|
|
|
|
|
|
protected BlockInfo3DS(int bo, uint id, int ofs, int len)
|
|
|
|
{
|
|
|
|
BlockInfoOffset = bo;
|
|
|
|
ID = id;
|
|
|
|
Offset = ofs;
|
|
|
|
Length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int ChecksumOffset => BlockInfoOffset + 0x14 + ((int)ID * 8) + 6;
|
2022-01-03 05:35:59 +00:00
|
|
|
protected abstract ushort GetChecksum(Span<byte> data);
|
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
|
|
|
{
|
|
|
|
ushort chk = GetChecksum(data);
|
2022-01-03 05:35:59 +00:00
|
|
|
var old = ReadUInt16LittleEndian(data[ChecksumOffset..]);
|
2018-02-19 19:33:37 +00:00
|
|
|
return chk == old;
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
protected override void SetChecksum(Span<byte> data)
|
2018-02-19 19:33:37 +00:00
|
|
|
{
|
|
|
|
ushort chk = GetChecksum(data);
|
2022-01-03 05:35:59 +00:00
|
|
|
WriteUInt16LittleEndian(data[ChecksumOffset..], chk);
|
2018-02-19 19:33:37 +00:00
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2018-02-19 19:33:37 +00:00
|
|
|
|
2019-06-09 02:56:11 +00:00
|
|
|
public sealed class BlockInfo6 : BlockInfo3DS
|
|
|
|
{
|
|
|
|
public BlockInfo6(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
|
2022-01-03 05:35:59 +00:00
|
|
|
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length));
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2018-11-14 03:18:29 +00:00
|
|
|
|
2019-06-09 02:56:11 +00:00
|
|
|
public sealed class BlockInfo7 : BlockInfo3DS
|
|
|
|
{
|
|
|
|
public BlockInfo7(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
|
2022-01-03 05:35:59 +00:00
|
|
|
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16Invert(data.Slice(Offset, Length));
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public sealed class BlockInfo7b : BlockInfo3DS
|
|
|
|
{
|
|
|
|
public BlockInfo7b(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
|
2022-01-03 05:35:59 +00:00
|
|
|
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16NoInvert(data.Slice(Offset, Length));
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2021-06-16 03:00:51 +00:00
|
|
|
}
|