PKHeX/PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.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

86 lines
No EOL
2.4 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
public sealed class MyStatus7b : SaveBlock
{
public MyStatus7b(SAV7b sav, int offset) : base(sav) => Offset = offset;
// Player Information
// idb uint8 offset: 0x58
public int TID
{
get => ReadUInt16LittleEndian(Data.AsSpan(Offset + 0));
set => WriteUInt16LittleEndian(Data.AsSpan(Offset + 0), (ushort)value);
}
public int SID
{
get => ReadUInt16LittleEndian(Data.AsSpan(Offset + 2));
set => WriteUInt16LittleEndian(Data.AsSpan(Offset + 2), (ushort)value);
}
public int Game
{
get => Data[Offset + 4];
set => Data[Offset + 4] = (byte)value;
}
public int Gender
{
get => Data[Offset + 5];
set => Data[Offset + 5] = OverworldGender = (byte)value;
}
public const int GameSyncIDSize = 16; // 8 bytes
public string GameSyncID
{
get => Util.GetHexStringFromBytes(Data, Offset + 0x10, GameSyncIDSize / 2);
set
{
if (value.Length > 16)
throw new ArgumentException(nameof(value));
var data = Util.GetBytesFromHexString(value);
SAV.SetData(data, Offset + 0x10);
}
}
public int Language
{
get => Data[Offset + 0x35];
set => Data[Offset + 0x35] = (byte)value;
}
private Span<byte> OT_Trash => Data.AsSpan(Offset + 0x38, 0x1A);
public string OT
{
get => SAV.GetString(OT_Trash);
set => SAV.SetString(OT_Trash, value.AsSpan(), SAV.OTLength, StringConverterOption.ClearZero);
}
// The value here corresponds to a Trainer Class string (ranging from 000 to 383, use pkNX to get a full list).
public byte TrainerClassIndex
{
get => Data[Offset + 0x076];
set => Data[Offset + 0x076] = value;
}
public byte StarterGender
{
get => Data[Offset + 0x0B9];
set => Data[Offset + 0x0B9] = value;
}
public byte OverworldGender // Model
{
get => Data[Offset + 0x108];
set => Data[Offset + 0x108] = value;
}
}
}