mirror of
https://github.com/kwsch/PKHeX
synced 2024-12-23 10:53:10 +00:00
47071b41f3
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.
63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
|
|
|
namespace PKHeX.Core
|
|
{
|
|
/// <summary>
|
|
/// Generation 5 Evolution Branch Entries
|
|
/// </summary>
|
|
public static class EvolutionSet5
|
|
{
|
|
private static EvolutionMethod GetMethod(ReadOnlySpan<byte> data)
|
|
{
|
|
var method = data[0]; // other byte unnecessary
|
|
int arg = ReadUInt16LittleEndian(data[2..]);
|
|
int species = ReadUInt16LittleEndian(data[4..]);
|
|
|
|
if (method == 0)
|
|
throw new ArgumentOutOfRangeException(nameof(method));
|
|
|
|
var lvl = EvolutionSet6.EvosWithArg.Contains(method) ? 0 : arg;
|
|
return new EvolutionMethod(method, species, argument: arg, level: lvl);
|
|
}
|
|
|
|
private const int bpe = 6; // bytes per evolution entry
|
|
private const int entries = 7; // amount of entries per species
|
|
private const int size = entries * bpe; // bytes per species entry
|
|
|
|
public static IReadOnlyList<EvolutionMethod[]> GetArray(ReadOnlySpan<byte> data)
|
|
{
|
|
var evos = new EvolutionMethod[data.Length / size][];
|
|
for (int i = 0; i < evos.Length; i++)
|
|
{
|
|
int offset = i * size;
|
|
var rawEntries = data.Slice(offset, size);
|
|
var count = ScanCountEvolutions(rawEntries);
|
|
if (count == 0)
|
|
{
|
|
evos[i] = Array.Empty<EvolutionMethod>();
|
|
continue;
|
|
}
|
|
|
|
var set = new EvolutionMethod[count];
|
|
for (int j = 0; j < set.Length; j++)
|
|
set[j] = GetMethod(rawEntries.Slice((j * bpe), bpe));
|
|
evos[i] = set;
|
|
}
|
|
return evos;
|
|
}
|
|
|
|
private static int ScanCountEvolutions(ReadOnlySpan<byte> data)
|
|
{
|
|
for (int count = 0; count < entries; count++)
|
|
{
|
|
var methodOffset = count * bpe;
|
|
var method = data[methodOffset];
|
|
if (method == 0)
|
|
return count;
|
|
}
|
|
return entries;
|
|
}
|
|
}
|
|
}
|