PKHeX/PKHeX.Core/PKM/HOME/HomeOptional1.cs
Kurt fd02b97ce1 Misc tweaks
Allow nickname->species with span
add ConstantExpected annotations
Correct Gen8b ability set calling CommonEdits instead of direct setter
Slightly better perf on gen5/gen8+ location fetch
Misc pkm ctor fixes for b2w2 trade & wc3 eggs
wurmple evo now has an enum to be more explicit
no recursion for gen5 generator
fix showdown line length check allowing 86 instead of 80
2023-08-27 23:05:50 -07:00

43 lines
1.4 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
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([ConstantExpected] 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;
}
}