PKHeX/PKHeX.Core/Saves/Substructures/Gen3/PokeBlock3.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

41 lines
No EOL
1.4 KiB
C#

using System;
namespace PKHeX.Core
{
public sealed class PokeBlock3
{
public const int SIZE = 7;
private readonly byte[] Data;
public PokeBlock3(byte[] data) => Data = data;
public PokeBlock3Color Color { get => (PokeBlock3Color)Data[0]; set => Data[0] = (byte)value; }
public int Spicy { get => Data[1]; set => Data[1] = (byte)value; }
public int Dry { get => Data[2]; set => Data[2] = (byte)value; }
public int Sweet { get => Data[3]; set => Data[3] = (byte)value; }
public int Bitter { get => Data[4]; set => Data[4] = (byte)value; }
public int Sour { get => Data[5]; set => Data[5] = (byte)value; }
public int Feel { get => Data[6]; set => Data[6] = (byte)value; }
public void Delete()
{
for (int i = 0; i < Data.Length; i++)
Data[i] = 0;
}
public void Maximize(bool create = false)
{
if (Color == 0 && !create)
return;
Spicy = Dry = Sweet = Bitter = Sour = Feel = 255;
Color = PokeBlock3Color.Gold;
}
public void SetBlock(byte[] data, int offset) => Data.CopyTo(data, offset);
public static PokeBlock3 GetBlock(ReadOnlySpan<byte> data, int offset)
{
byte[] result = data.Slice(offset, SIZE).ToArray();
return new PokeBlock3(result);
}
}
}