PKHeX/PKHeX.Core/Legality/Learnset/LearnsetReader.cs
Kurt c4199b26ec Minor startup optimization (resource sizes)
-288 KB (-31%) across lvlmove/eggmove/evolve binaries
redesign the levelup bins:
- be moves_levels rather than the "official" -1 stop of the past era.
- gen1/2 reformatted from byte,byte[] to ^ to skip initialization work
redesign the eggmove bins:
- be simply moves[], rather than the "official" -1 stop of the past era; now is just a struct to keep the array readonly with no further allocation.
- same for gen2 skipping initialization byte[]->ushort[]
- for gen7/8 formtable indexed, just use the personal table indexing style of SV.
added a 16-bit version of BinLinkerAccessor as start/end offsets of <65KB files are always 16bit. Saves a fair bit of space in eggmoves/evo where there's often 0 entries for a species-form.

Obviously binlinker16 is an unofficial format, but there's no need to replicate official serialization formats if we instead use a universal & maintainable alternative. Plus they don't even use BinLinker across all their games.

Adds a debug BinLinkerWriter because I'm tired of digging up the zipping implementation :)
2025-05-25 16:27:05 -05:00

45 lines
1.4 KiB
C#

using System;
using System.Runtime.InteropServices;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <summary>
/// Unpacks <see cref="Learnset"/> data from legality binary inputs.
/// </summary>
public static class LearnsetReader
{
private static readonly Learnset EMPTY = new([], []);
/// <summary>
/// Loads a learnset by reading 16-bit move,level pairs.
/// </summary>
/// <param name="entries">Entry data</param>
public static Learnset[] GetArray(BinLinkerAccessor16 entries)
{
var result = new Learnset[entries.Length];
result[0] = EMPTY; // empty entry
for (int i = 1; i < result.Length; i++)
result[i] = ReadLearnset16(entries[i]);
return result;
}
/// <summary>
/// Reads a Level up move pool definition from a single move pool definition.
/// </summary>
private static Learnset ReadLearnset16(ReadOnlySpan<byte> data)
{
if (data.Length == 0)
return EMPTY;
// move[], .. level[]
var count = data.Length / 3;
var size = count << 1; // 2 bytes per move
var moves = MemoryMarshal.Cast<byte, ushort>(data[..size]).ToArray();
if (!BitConverter.IsLittleEndian)
ReverseEndianness(moves, moves);
var levels = data.Slice(size, count).ToArray();
return new Learnset(moves, levels);
}
}