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

48 lines
1.5 KiB
C#

using System;
namespace PKHeX.Core
{
public sealed class BoxLayout5 : SaveBlock
{
public BoxLayout5(SAV5BW sav, int offset) : base(sav) => Offset = offset;
public BoxLayout5(SAV5B2W2 sav, int offset) : base(sav) => Offset = offset;
public int CurrentBox { get => Data[Offset]; set => Data[Offset] = (byte)value; }
public int GetBoxNameOffset(int box) => Offset + (0x28 * box) + 4;
public int GetBoxWallpaperOffset(int box) => Offset + 0x3C4 + box;
public int GetBoxWallpaper(int box)
{
if ((uint)box > SAV.BoxCount)
return 0;
return Data[GetBoxWallpaperOffset(box)];
}
public void SetBoxWallpaper(int box, int value)
{
if ((uint)box > SAV.BoxCount)
return;
Data[GetBoxWallpaperOffset(box)] = (byte)value;
}
private Span<byte> GetBoxNameSpan(int box) => Data.AsSpan(GetBoxNameOffset(box), 0x14);
public string GetBoxName(int box)
{
if (box >= SAV.BoxCount)
return string.Empty;
return SAV.GetString(GetBoxNameSpan(box));
}
public void SetBoxName(int box, string value)
{
SAV.SetString(GetBoxNameSpan(box), value.AsSpan(), 13, StringConverterOption.ClearZero);
}
public string this[int i]
{
get => GetBoxName(i);
set => SetBoxName(i, value);
}
}
}