2022-08-27 06:43:36 +00:00
|
|
|
using System;
|
2022-03-22 01:53:24 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public sealed class Hall4
|
|
|
|
{
|
|
|
|
private const int SIZE = 0xBA0;
|
|
|
|
private const int SIZE_FOOTER = 0x10;
|
|
|
|
private const int SIZE_BLOCK = 0x1000;
|
|
|
|
|
|
|
|
private readonly byte[] Raw;
|
|
|
|
private readonly int Offset;
|
|
|
|
private Span<byte> Data => Raw.AsSpan(Offset, SIZE_BLOCK);
|
|
|
|
public Hall4(byte[] data, int offset) => (Raw, Offset) = (data, offset);
|
|
|
|
|
|
|
|
private const int SIZE_ARRAY = 0x3DE; // 493+(egg, bad-egg) species
|
|
|
|
|
|
|
|
// Structure:
|
|
|
|
// u32 key
|
|
|
|
// u16[495] single records
|
|
|
|
// u16[495] double records
|
|
|
|
// u16[495] multi records
|
|
|
|
// u16 alignment
|
|
|
|
// extdata 0x10 byte footer
|
|
|
|
|
|
|
|
public uint Key { get => ReadUInt32LittleEndian(Data); set => WriteUInt32LittleEndian(Data, value); }
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public ushort GetCount(int battleType, ushort species)
|
2022-03-22 01:53:24 +00:00
|
|
|
{
|
|
|
|
var offset = GetRecordOffset(battleType, species);
|
|
|
|
return ReadUInt16LittleEndian(Data[offset..]);
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public void SetCount(int battleType, ushort species, ushort value)
|
2022-03-22 01:53:24 +00:00
|
|
|
{
|
|
|
|
var offset = GetRecordOffset(battleType, species);
|
|
|
|
WriteUInt16LittleEndian(Data[offset..], value);
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static int GetRecordOffset(int battleType, ushort species)
|
2022-03-22 01:53:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (species > Legal.MaxSpeciesID_4)
|
2022-03-22 01:53:24 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(species));
|
|
|
|
if ((uint)battleType > 2)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(battleType));
|
|
|
|
|
|
|
|
return sizeof(uint) + (battleType * SIZE_ARRAY) + (species * sizeof(ushort));
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Footer
|
|
|
|
public bool SizeValid => BlockSize == SIZE;
|
|
|
|
public bool ChecksumValid => Checksum == GetChecksum();
|
|
|
|
public bool IsValid => SizeValid && ChecksumValid;
|
|
|
|
|
|
|
|
public uint Magic { get => ReadUInt32LittleEndian(Footer); set => WriteUInt32LittleEndian(Footer, value); }
|
|
|
|
public uint Revision { get => ReadUInt32LittleEndian(Footer[0x4..]); set => WriteUInt32LittleEndian(Footer[0x4..], value); }
|
|
|
|
public int BlockSize { get => ReadInt32LittleEndian (Footer[0x8..]); set => WriteInt32LittleEndian (Footer[0x8..], value); }
|
|
|
|
public ushort BlockID { get => ReadUInt16LittleEndian(Footer[0xC..]); set => WriteUInt16LittleEndian(Footer[0xC..], value); }
|
|
|
|
public ushort Checksum { get => ReadUInt16LittleEndian(Footer[0xE..]); set => WriteUInt16LittleEndian(Footer[0xE..], value); }
|
|
|
|
|
|
|
|
private ReadOnlySpan<byte> GetRegion() => Data[..(SIZE + SIZE_FOOTER)];
|
|
|
|
private Span<byte> Footer => Data.Slice(SIZE, SIZE_FOOTER);
|
|
|
|
private ushort GetChecksum() => Checksums.CRC16_CCITT(GetRegion()[..^2]);
|
|
|
|
public void RefreshChecksum() => Checksum = GetChecksum();
|
|
|
|
#endregion
|
|
|
|
}
|