PKHeX/PKHeX.Core/Legality/Evolutions/EvolutionSets/EvolutionSet3.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

80 lines
3 KiB
C#

using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
/// <summary>
/// Generation 3 Evolution Branch Entries
/// </summary>
public static class EvolutionSet3
{
private static EvolutionMethod GetMethod(ReadOnlySpan<byte> data)
{
int method = ReadUInt16LittleEndian(data);
int arg = ReadUInt16LittleEndian(data[2..]);
int species = SpeciesConverter.GetG4Species(ReadUInt16LittleEndian(data[4..]));
//2 bytes padding
switch (method)
{
case 1: /* Friendship*/
case 2: /* Friendship day*/
case 3: /* Friendship night*/
case 5: /* Trade */
case 6: /* Trade while holding */
return new EvolutionMethod(method, species, argument: arg);
case 4: /* Level Up */
return new EvolutionMethod(4, species, argument: arg, level:arg);
case 7: /* Use item */
case 15: /* Beauty evolution*/
return new EvolutionMethod(method + 1, species, argument: arg);
case 8: /* Tyrogue -> Hitmonchan */
case 9: /* Tyrogue -> Hitmonlee */
case 10: /* Tyrogue -> Hitmontop*/
case 11: /* Wurmple -> Silcoon evolution */
case 12: /* Wurmple -> Cascoon evolution */
case 13: /* Nincada -> Ninjask evolution */
case 14: /* Shedinja spawn in Nincada -> Ninjask evolution */
return new EvolutionMethod(method + 1, species, argument: arg, level: arg);
default:
throw new ArgumentOutOfRangeException(nameof(method));
}
}
public static IReadOnlyList<EvolutionMethod[]> GetArray(ReadOnlySpan<byte> data)
{
var evos = new EvolutionMethod[Legal.MaxSpeciesID_3 + 1][];
evos[0] = Array.Empty<EvolutionMethod>();
for (int i = 1; i <= Legal.MaxSpeciesIndex_3; i++)
{
int g4species = SpeciesConverter.GetG4Species(i);
if (g4species == 0)
continue;
const int maxCount = 5;
const int size = 8;
int offset = i * (maxCount * size);
int count = 0;
for (; count < maxCount; count++)
{
if (data[offset + (count * size)] == 0)
break;
}
if (count == 0)
{
evos[g4species] = Array.Empty<EvolutionMethod>();
continue;
}
var set = new EvolutionMethod[count];
for (int j = 0; j < set.Length; j++)
set[j] = GetMethod(data.Slice(offset + (j * size), size));
evos[g4species] = set;
}
return evos;
}
}
}