PKHeX/PKHeX.Core/Legality/Evolutions/EvolutionSets/EvolutionSet7.cs
Kurt 47071b41f3
Refactoring: Span-based value writes and method signatures (#3361)
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to.

`System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API.

The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder.

Other Fixes included:
- The Super Training UI for Gen6 has been reworked according to the latest block structure additions.
- Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list).
- Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
2022-01-02 21:35:59 -08:00

43 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
/// <summary>
/// Generation 7 Evolution Branch Entries
/// </summary>
public static class EvolutionSet7
{
private const int SIZE = 8;
private static EvolutionMethod[] GetMethods(ReadOnlySpan<byte> data)
{
var evos = new EvolutionMethod[data.Length / SIZE];
for (int i = 0; i < data.Length; i += SIZE)
{
var entry = data.Slice(i, SIZE);
evos[i / SIZE] = ReadEvolution(entry);
}
return evos;
}
private static EvolutionMethod ReadEvolution(ReadOnlySpan<byte> entry)
{
var method = ReadUInt16LittleEndian(entry);
var arg = ReadUInt16LittleEndian(entry[2..]);
var species = ReadUInt16LittleEndian(entry[4..]);
var form = (sbyte)entry[6];
var level = entry[7];
return new EvolutionMethod(method, species, argument: arg, level: level, form: form);
}
public static IReadOnlyList<EvolutionMethod[]> GetArray(IReadOnlyList<byte[]> data)
{
var evos = new EvolutionMethod[data.Count][];
for (int i = 0; i < evos.Length; i++)
evos[i] = GetMethods(data[i]);
return evos;
}
}
}