PKHeX/PKHeX.Core/Saves/Substructures/Gen5/EntreeSlot.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

87 lines
No EOL
2.5 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
/// <summary>
/// Generation 5 <see cref="EntreeForest"/> slot
/// </summary>
public sealed class EntreeSlot : ISpeciesForm
{
/// <summary>
/// <see cref="PKM.Species"/> index
/// </summary>
public int Species // bits 0-10
{
get => (int)(RawValue & 0x3FF) >> 0;
set => RawValue = (RawValue & 0xFFFF_F800) | ((uint)(value & 0x3FF) << 0);
}
/// <summary>
/// Special Move
/// </summary>
public int Move // bits 11-20
{
get => (int)(RawValue & 0x001F_F800) >> 11;
set => RawValue = (RawValue & 0xFFE0_07FF) | ((uint)(value & 0x3FF) << 11);
}
/// <summary>
/// <see cref="PKM.Gender"/> index
/// </summary>
public int Gender // bits 21-22
{
get => (int)(RawValue & 0x0060_0000) >> 21;
set => RawValue = (RawValue & 0xFF9F_FFFF) | ((uint)(value & 0x3) << 21);
}
/// <summary>
/// <see cref="PKM.Form"/> index
/// </summary>
public int Form // bits 23-27
{
get => (int)(RawValue & 0x0F80_0000) >> 23;
set => RawValue = (RawValue & 0xF07F_FFFF) | ((uint)(value & 0x1F) << 23);
}
/// <summary>
/// Visibility Flag
/// </summary>
public bool Invisible // bit 28
{
get => ((RawValue >> 28) & 1) == 1;
set => RawValue = (RawValue & 0xEFFFFFFF) | (value ? 0 : 1u << 28);
}
/// <summary>
/// Animation Leash (How many steps it can deviate from its spawn location).
/// </summary>
public int Animation // bits 29-31
{
get => (int)(RawValue >> 29);
set => RawValue = (RawValue << 3) >> 3 | (uint)((value & 0x7) << 29);
}
private readonly byte[] Data;
private readonly int Offset;
/// <summary>
/// Raw Data Value
/// </summary>
public uint RawValue
{
get => ReadUInt32LittleEndian(Data.AsSpan(Offset));
set => WriteUInt32LittleEndian(Data.AsSpan(Offset), value);
}
public void Delete() => RawValue = 0;
public EntreeForestArea Area { get; init; }
public EntreeSlot(byte[] data, int ofs)
{
Data = data;
Offset = ofs;
}
}
}