PKHeX/PKHeX.Core/Saves/Storage/BulkStorage.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

63 lines
3.0 KiB
C#

using System;
namespace PKHeX.Core
{
/// <summary>
/// Simple Storage Binary wrapper for a concatenated list of <see cref="PKM"/> data.
/// </summary>
public abstract class BulkStorage : SaveFile
{
protected BulkStorage(byte[] data, Type t, int start, int slotsPerBox = 30) : base(data)
{
Box = start;
SlotsPerBox = slotsPerBox;
blank = PKMConverter.GetBlank(t);
var slots = (Data.Length - Box) / blank.SIZE_STORED;
BoxCount = slots / SlotsPerBox;
}
protected readonly int SlotsPerBox;
protected internal override string ShortSummary => $"{Checksums.CRC16Invert(new ReadOnlySpan<byte>(Data, Box, Data.Length - Box)):X4}";
public override string Extension => ".bin";
public sealed override bool ChecksumsValid => true;
public sealed override string ChecksumInfo => "No Info.";
private readonly PKM blank;
public sealed override Type PKMType => blank.GetType();
public sealed override PKM BlankPKM => blank.Clone();
protected override PKM GetPKM(byte[] data) => PKMConverter.GetPKMfromBytes(data, prefer: Generation) ?? blank;
protected override byte[] DecryptPKM(byte[] data) => GetPKM(data).Data;
protected override int SIZE_STORED => blank.SIZE_STORED;
protected override int SIZE_PARTY => blank.SIZE_PARTY;
public sealed override int MaxEV => blank.MaxEV;
public sealed override int Generation => blank.Format;
public sealed override int MaxMoveID => blank.MaxMoveID;
public sealed override int MaxSpeciesID => blank.MaxSpeciesID;
public sealed override int MaxAbilityID => blank.MaxAbilityID;
public sealed override int MaxItemID => blank.MaxItemID;
public sealed override int MaxBallID => blank.MaxBallID;
public sealed override int MaxGameID => blank.MaxGameID;
public sealed override int OTLength => blank.OTLength;
public sealed override int NickLength => blank.NickLength;
public bool IsBigEndian => blank is BK4 or XK3 or CK3;
public override int BoxCount { get; }
protected override void SetChecksums() { }
public override int GetBoxOffset(int box) => Box + (box * (SlotsPerBox * SIZE_STORED));
public override string GetBoxName(int box) => $"Box {box + 1:d2}";
public sealed override void SetBoxName(int box, string value) { }
public sealed override int GetPartyOffset(int slot) => int.MinValue;
public override string GetString(ReadOnlySpan<byte> data)
=> StringConverter.GetString(data, Generation, blank.Japanese, IsBigEndian);
public override int SetString(Span<byte> destBuffer, ReadOnlySpan<char> value, int maxLength, StringConverterOption option)
=> StringConverter.SetString(destBuffer, value, maxLength, option: option, generation: Generation, jp: blank.Japanese, isBigEndian: IsBigEndian, language: Language);
}
}