2018-05-19 19:07:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2018-05-19 19:07:50 +00:00
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 5 Evolution Branch Entries
|
|
|
|
/// </summary>
|
2019-03-23 17:36:28 +00:00
|
|
|
public static class EvolutionSet5
|
2018-05-19 19:07:50 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
private static EvolutionMethod GetMethod(ReadOnlySpan<byte> data)
|
2018-05-19 19:07:50 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
var method = data[0]; // other byte unnecessary
|
|
|
|
int arg = ReadUInt16LittleEndian(data[2..]);
|
|
|
|
int species = ReadUInt16LittleEndian(data[4..]);
|
2018-05-19 19:07:50 +00:00
|
|
|
|
|
|
|
if (method == 0)
|
2021-08-21 23:51:50 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(method));
|
2018-05-19 19:07:50 +00:00
|
|
|
|
2020-01-25 23:42:17 +00:00
|
|
|
var lvl = EvolutionSet6.EvosWithArg.Contains(method) ? 0 : arg;
|
|
|
|
return new EvolutionMethod(method, species, argument: arg, level: lvl);
|
2018-05-19 19:07:50 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
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
|
2019-03-23 08:01:04 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
public static IReadOnlyList<EvolutionMethod[]> GetArray(ReadOnlySpan<byte> data)
|
|
|
|
{
|
2019-03-23 17:36:28 +00:00
|
|
|
var evos = new EvolutionMethod[data.Length / size][];
|
2019-03-23 08:01:04 +00:00
|
|
|
for (int i = 0; i < evos.Length; i++)
|
2018-05-19 19:07:50 +00:00
|
|
|
{
|
2019-03-23 08:01:04 +00:00
|
|
|
int offset = i * size;
|
2022-01-03 05:35:59 +00:00
|
|
|
var rawEntries = data.Slice(offset, size);
|
|
|
|
var count = ScanCountEvolutions(rawEntries);
|
2019-03-23 08:01:04 +00:00
|
|
|
if (count == 0)
|
|
|
|
{
|
2019-03-23 17:36:28 +00:00
|
|
|
evos[i] = Array.Empty<EvolutionMethod>();
|
2019-03-23 08:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var set = new EvolutionMethod[count];
|
|
|
|
for (int j = 0; j < set.Length; j++)
|
2022-01-03 05:35:59 +00:00
|
|
|
set[j] = GetMethod(rawEntries.Slice((j * bpe), bpe));
|
2019-03-23 17:36:28 +00:00
|
|
|
evos[i] = set;
|
2018-05-19 19:07:50 +00:00
|
|
|
}
|
|
|
|
return evos;
|
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-05-19 19:07:50 +00:00
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
}
|