PKHeX/PKHeX.Core/Saves/Substructures/Gen8/BS/PoffinSaveData8b.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
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
/// <summary>
/// Details about the Poketch corner app.
/// </summary>
/// <remarks>size: 0x644</remarks>
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class PoffinSaveData8b : SaveBlock
{
public PoffinSaveData8b(SAV8BS sav, int offset) : base(sav) => Offset = offset;
// structure:
// PoffinData[] Poffins;
// int CookingCount;
// 0x640 bytes of data is for poffins
public const int COUNT_POFFIN = 100;
private int GetPoffinOffset(int index)
{
if ((uint)index >= COUNT_POFFIN)
throw new ArgumentOutOfRangeException(nameof(index));
return Offset + (index * Poffin8b.SIZE);
}
public Poffin8b GetPoffin(int index) => new(Data, GetPoffinOffset(index));
public void SetPoffin(int index, Poffin8b poffin) => poffin.CopyTo(Data, GetPoffinOffset(index));
public int CookingCount { get => ReadInt32LittleEndian(Data.AsSpan(Offset + 0x640)); set => WriteInt32LittleEndian(Data.AsSpan(Offset + 0x640), value); }
public Poffin8b[] GetPoffins()
{
var result = new Poffin8b[COUNT_POFFIN];
for (int i = 0; i < result.Length; i++)
result[i] = GetPoffin(i);
return result;
}
public void SetPoffins(IReadOnlyCollection<Poffin8b> value)
{
if (value.Count != COUNT_POFFIN)
throw new ArgumentOutOfRangeException(nameof(value.Count));
var ordered = value.OrderBy(z => z.IsNull).ThenBy(z => z.IsNew);
int ctr = 0;
foreach (var p in ordered)
SetPoffin(ctr++, p);
}
public int CountPoffins() => GetPoffins().Count(z => z.IsNull);
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class Poffin8b
{
public const int SIZE = 0x10;
private readonly byte[] Data = new byte[SIZE];
public Poffin8b(byte[] data, int offset)
{
data.AsSpan(offset, SIZE).CopyTo(Data);
}
public void Clear() => Data.AsSpan().Clear();
public void CopyTo(byte[] data, int offset) => Data.CopyTo(data, offset);
public void ToNull() => MstID = 0xFF;
public bool IsNull => MstID == 0xFF;
public byte MstID { get => Data[0x00]; set => Data[0x00] = value; }
public byte Level { get => Data[0x01]; set => Data[0x01] = value; }
public byte Taste { get => Data[0x02]; set => Data[0x02] = value; } // Smoothness/feel of the Poffin => +sheen
public bool IsNew { get => ReadUInt32LittleEndian(Data.AsSpan(0x04)) == 1; set => WriteUInt32LittleEndian(Data.AsSpan(0x04), value ? 1u : 0u); }
public byte FlavorSpicy { get => Data[0x08]; set => Data[0x08] = value; }
public byte FlavorDry { get => Data[0x09]; set => Data[0x09] = value; }
public byte FlavorSweet { get => Data[0x0A]; set => Data[0x0A] = value; }
public byte FlavorBitter { get => Data[0x0B]; set => Data[0x0B] = value; }
public byte FlavorSour { get => Data[0x0C]; set => Data[0x0C] = value; }
}
}