PKHeX/PKHeX.Core/Saves/Substructures/Gen4/Hall4.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

67 lines
2.6 KiB
C#

using System;
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); }
public ushort GetCount(int battleType, ushort species)
{
var offset = GetRecordOffset(battleType, species);
return ReadUInt16LittleEndian(Data[offset..]);
}
public void SetCount(int battleType, ushort species, ushort value)
{
var offset = GetRecordOffset(battleType, species);
WriteUInt16LittleEndian(Data[offset..], value);
}
private static int GetRecordOffset(int battleType, ushort species)
{
if (species > Legal.MaxSpeciesID_4)
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
}