PKHeX/PKHeX.Core/PKM/HOME/HomeOptional1.cs
Kurt 1174e354b1
Initial support for Pokémon HOME 3.0.0 (#3899)
* Heavily rewrites the `PKH` abstractions.
* Uses HOME's core-side classes as the transfer middlemen instead of direct A->B transfers.
* Revises logic to account for most of HOME's quirks (scale/height copying, safe refuge PLA)

Future revisions hinge on better handling of evotree (need better metadata about existing as specific evolutions in each game).

---------

Co-authored-by: sora10pls <17801814+sora10pls@users.noreply.github.com>
Co-authored-by: Lusamine <30205550+Lusamine@users.noreply.github.com>
2023-06-03 18:19:16 -07:00

42 lines
1.3 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <summary>
/// Side game data base class for <see cref="PKM"/> data transferred into HOME.
/// </summary>
public abstract class HomeOptional1
{
// Internal Attributes set on creation
private readonly Memory<byte> Buffer; // Raw Storage
protected Span<byte> Data => Buffer.Span;
public int SerializedSize => HeaderSize + Buffer.Length;
public const int HeaderSize = 3; // u8 format, u16 length(data[u8])
protected abstract HomeGameDataFormat Format { get; }
protected HomeOptional1(ushort size) => Buffer = new byte[size];
protected HomeOptional1(Memory<byte> buffer) => Buffer = buffer;
protected void EnsureSize(int size)
{
if (Buffer.Length != size)
throw new ArgumentOutOfRangeException(nameof(size), size, $"Expected size {Buffer.Length} but received {size}.");
}
protected byte[] ToArray() => Data.ToArray();
protected int WriteWithHeader(Span<byte> result)
{
result[0] = (byte)Format;
WriteUInt16LittleEndian(result[1..], (ushort)Data.Length);
return HeaderSize + WriteWithoutHeader(result[HeaderSize..]);
}
private int WriteWithoutHeader(Span<byte> result)
{
var span = Data;
span.CopyTo(result);
return span.Length;
}
}