PKHeX/PKHeX.Core/Saves/Substructures/Gen8/FashionUnlock8.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

43 lines
No EOL
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
public sealed class FashionUnlock8 : SaveBlock
{
private const int SIZE_ENTRY = 0x80;
private const int REGIONS = 15;
public FashionUnlock8(SaveFile sav, SCBlock block) : base(sav, block.Data) { }
public bool[] GetArrayOwned(int region) => ArrayUtil.GitBitFlagArray(Data.AsSpan(region * SIZE_ENTRY), SIZE_ENTRY * 8);
public bool[] GetArrayNew(int region) => ArrayUtil.GitBitFlagArray(Data.AsSpan((region + REGIONS) * SIZE_ENTRY), SIZE_ENTRY * 8);
public int[] GetIndexesOwned(int region) => GetIndexes(GetArrayOwned(region));
public int[] GetIndexesNew(int region) => GetIndexes(GetArrayNew(region));
public void SetArrayOwned(int region, bool[] value) => ArrayUtil.SetBitFlagArray(Data.AsSpan(region * SIZE_ENTRY), value);
public void SetArrayNew(int region, bool[] value) => ArrayUtil.SetBitFlagArray(Data.AsSpan((region + REGIONS) * SIZE_ENTRY), value);
public void SetIndexesOwned(int region, int[] value) => SetArrayOwned(region, SetIndexes(value));
public void SetIndexesNew(int region, int[] value) => SetArrayNew(region, SetIndexes(value));
public static int[] GetIndexes(bool[] arr)
{
var list = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i])
list.Add(i);
}
return list.ToArray();
}
public static bool[] SetIndexes(int[] arr)
{
var result = new bool[arr.Max()];
foreach (var index in arr)
result[index] = true;
return result;
}
}
}