PKHeX/PKHeX.Core/Legality/Learnset/LearnsetReader.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

84 lines
2.7 KiB
C#

using System;
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(Array.Empty<ushort>(), Array.Empty<byte>());
/// <summary>
/// Loads a learnset using the 8-bit-per-move storage structure used by Generation 1 &amp; 2 games.
/// </summary>
/// <param name="input">Raw ROM data containing the contiguous moves</param>
/// <param name="maxSpecies">Highest species ID for the input game.</param>
public static Learnset[] GetArray(ReadOnlySpan<byte> input, ushort maxSpecies)
{
int offset = 0;
var result = new Learnset[maxSpecies + 1];
for (int i = 0; i < result.Length; i++)
result[i] = ReadLearnset8(input, ref offset);
return result;
}
/// <summary>
/// Loads a learnset by reading 16-bit move,level pairs.
/// </summary>
/// <param name="entries">Entry data</param>
public static Learnset[] GetArray(BinLinkerAccessor entries)
{
var result = new Learnset[entries.Length];
for (int i = 0; i < result.Length; i++)
result[i] = ReadLearnset16(entries[i]);
return result;
}
/// <summary>
/// Reads a Level up move pool definition from a contiguous chunk of GB era ROM data.
/// </summary>
/// <remarks>Moves and Levels are 8-bit</remarks>
private static Learnset ReadLearnset8(ReadOnlySpan<byte> data, ref int offset)
{
int end = offset; // scan for count
if (data[end] == 0)
{
++offset;
return EMPTY;
}
do { end += 2; } while (data[end] != 0);
var count = (end - offset) / 2;
var moves = new ushort[count];
var levels = new byte[count];
for (int i = 0; i < moves.Length; i++)
{
levels[i] = data[offset++];
moves[i] = data[offset++];
}
++offset;
return new Learnset(moves, levels);
}
/// <summary>
/// Reads a Level up move pool definition from a single move pool definition.
/// </summary>
/// <remarks>Count of moves, followed by Moves and Levels which are 16-bit</remarks>
private static Learnset ReadLearnset16(ReadOnlySpan<byte> data)
{
if (data.Length <= 4)
return EMPTY;
var count = (data.Length / 4) - 1;
var moves = new ushort[count];
var levels = new byte[count];
for (int i = 0; i < count; i++)
{
var move = data.Slice(i * 4, 4);
levels[i] = move[2];
moves[i] = ReadUInt16LittleEndian(move);
}
return new Learnset(moves, levels);
}
}