mirror of
https://github.com/kwsch/PKHeX
synced 2024-12-18 08:23:12 +00:00
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
|
using System;
|
|||
|
using static System.Buffers.Binary.BinaryPrimitives;
|
|||
|
|
|||
|
namespace PKHeX.Core;
|
|||
|
|
|||
|
public abstract class HomeOptional1
|
|||
|
{
|
|||
|
// Internal Attributes set on creation
|
|||
|
public readonly byte[] Data; // Raw Storage
|
|||
|
public readonly int Offset;
|
|||
|
|
|||
|
private const int HeaderBias = 3;
|
|||
|
|
|||
|
protected HomeOptional1(HomeGameDataFormat format, ushort size)
|
|||
|
{
|
|||
|
Data = new byte[size + HeaderBias];
|
|||
|
Offset = HeaderBias;
|
|||
|
|
|||
|
Data[0] = (byte)format;
|
|||
|
WriteUInt16LittleEndian(Data.AsSpan(1, 2), size);
|
|||
|
}
|
|||
|
|
|||
|
protected HomeOptional1(HomeGameDataFormat format, ushort size, byte[] data, int offset = 0)
|
|||
|
{
|
|||
|
// Sanity check input format value with backing data value.
|
|||
|
if ((HomeGameDataFormat)data[offset] != format)
|
|||
|
throw new ArgumentOutOfRangeException(nameof(format), format, $"Invalid {nameof(HomeGameDataFormat)} for {format}");
|
|||
|
|
|||
|
// Sanity check input structure size with backing data value.
|
|||
|
var length = ReadUInt16LittleEndian(data.AsSpan(offset + 1));
|
|||
|
if (length != size)
|
|||
|
throw new ArgumentOutOfRangeException(nameof(size), length, $"Invalid structure size for {format}");
|
|||
|
|
|||
|
Data = data;
|
|||
|
Offset = HeaderBias + offset;
|
|||
|
}
|
|||
|
|
|||
|
protected Span<byte> ToSpan(int size) => Data.AsSpan(Offset - HeaderBias, HeaderBias + size);
|
|||
|
protected byte[] ToArray(int size) => ToSpan(size).ToArray();
|
|||
|
protected int CopyTo(Span<byte> result, int size)
|
|||
|
{
|
|||
|
var span = ToSpan(size);
|
|||
|
span.CopyTo(result);
|
|||
|
return span.Length;
|
|||
|
}
|
|||
|
}
|