PKHeX/PKHeX.Core/Saves/Substructures/Gen4/Dendou4.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

119 lines
5.2 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
public sealed class Dendou4
{
private const int SIZE = 0x2AB0;
private const int SIZE_FOOTER = 0x10;
private const int SIZE_BLOCK = 3 * 0x1000;
public const int MaxClears = 9999;
public const int MaxRecords = 30;
private readonly byte[] Raw;
private readonly int Offset;
private Span<byte> Data => Raw.AsSpan(Offset, SIZE_BLOCK);
public Dendou4(byte[] data, int offset) => (Raw, Offset) = (data, offset);
// Structure:
// record[30] records
// u32 NextIndexToOverwrite
// u32 ClearCount
public Dendou4Record this[int index] => GetRecord(index);
private Dendou4Record GetRecord(int index)
{
if ((uint)index >= MaxRecords)
throw new ArgumentOutOfRangeException(nameof(index));
var slice = Data.Slice(index * Dendou4Record.SIZE, Dendou4Record.SIZE);
return new Dendou4Record(slice);
}
private const int EndDataOffset = MaxRecords * Dendou4Record.SIZE; // 0x2AA8
public uint IndexNextOverwrite { get => ReadUInt32LittleEndian(Data[EndDataOffset..]); set => WriteUInt32LittleEndian(Data[EndDataOffset..], value % MaxRecords); }
public uint ClearCount { get => Math.Min(MaxClears, ReadUInt32LittleEndian(Data[(EndDataOffset + 4)..])); set => WriteUInt32LittleEndian(Data[(EndDataOffset + 4)..], Math.Min(MaxClears, value)); }
#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
}
public readonly ref struct Dendou4Record
{
public const int Count = 6;
public const int SIZE = (Dendou4Entity.SIZE * Count) + sizeof(uint); // 0x16C
// structure:
// Dendou4Entity[6]
// u16 Year
// u8 Month
// u8 Day
private readonly Span<byte> Data;
public Dendou4Record(Span<byte> data) => Data = data;
public Dendou4Entity this[int index] => GetEntity(index);
private const int DateOffset = SIZE - 4;
public ushort Year { get => ReadUInt16LittleEndian(Data[DateOffset..]); set => WriteUInt16LittleEndian(Data[DateOffset..], value); }
public byte Month { get => Data[DateOffset + 2]; set => Data[DateOffset + 2] = value; }
public byte Day { get => Data[DateOffset + 3]; set => Data[DateOffset + 3] = value; }
private Dendou4Entity GetEntity(int index)
{
if ((uint)index >= Count)
throw new ArgumentOutOfRangeException(nameof(index));
var slice = Data.Slice(index * Dendou4Entity.SIZE, Dendou4Entity.SIZE);
return new Dendou4Entity(slice);
}
}
public readonly ref struct Dendou4Entity
{
public const int SIZE = 0x3C;
private readonly Span<byte> Data;
public Dendou4Entity(Span<byte> data) => Data = data;
public ushort Species { get => ReadUInt16LittleEndian(Data); set => WriteUInt16LittleEndian(Data, value); }
public byte Level { get => Data[2]; set => Data[2] = value; }
public byte Form { get => Data[3]; set => Data[3] = value; }
public uint PID { get => ReadUInt32LittleEndian(Data[4..]); set => WriteUInt32LittleEndian(Data[4..], value); }
public uint ID32 { get => ReadUInt32LittleEndian(Data[8..]); set => WriteUInt32LittleEndian(Data[8..], value); }
public Span<byte> Nickname_Trash => Data.Slice(0x0C, 22);
public Span<byte> OT_Trash => Data.Slice(0x22, 16);
public string Nickname
{
get => StringConverter4.GetString(Nickname_Trash);
set => StringConverter4.SetString(Nickname_Trash, value, 10, StringConverterOption.None);
}
public string OT_Name
{
get => StringConverter4.GetString(OT_Trash);
set => StringConverter4.SetString(OT_Trash, value, 7, StringConverterOption.None);
}
public ushort Move1 { get => ReadUInt16LittleEndian(Data[0x32..]); set => WriteUInt16LittleEndian(Data[0x32..], value); }
public ushort Move2 { get => ReadUInt16LittleEndian(Data[0x34..]); set => WriteUInt16LittleEndian(Data[0x34..], value); }
public ushort Move3 { get => ReadUInt16LittleEndian(Data[0x36..]); set => WriteUInt16LittleEndian(Data[0x36..], value); }
public ushort Move4 { get => ReadUInt16LittleEndian(Data[0x38..]); set => WriteUInt16LittleEndian(Data[0x38..], value); }
// u16 alignment 0x3A
}