mirror of
https://github.com/kwsch/PKHeX
synced 2024-12-22 18:33:14 +00:00
47071b41f3
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.
59 lines
No EOL
1.9 KiB
C#
59 lines
No EOL
1.9 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
|
|
namespace PKHeX.Core
|
|
{
|
|
internal sealed class OPowerFlagSet
|
|
{
|
|
public readonly OPower6Type Identifier;
|
|
public readonly int Count;
|
|
public int Offset { get; set; }
|
|
public int BaseCount => Math.Min(3, Count);
|
|
public bool HasOPowerS => Count > 3;
|
|
public bool HasOPowerMAX => Count == 5;
|
|
|
|
public OPowerFlagSet(int count, OPower6Type ident)
|
|
{
|
|
Identifier = ident;
|
|
Count = count;
|
|
}
|
|
|
|
public int GetOPowerLevel(ReadOnlySpan<byte> data)
|
|
{
|
|
for (int i = 0; i < BaseCount; i++)
|
|
{
|
|
if (GetFlag(data, i))
|
|
continue;
|
|
Debug.WriteLine($"Fetched {Identifier}: {i}");
|
|
return i;
|
|
}
|
|
|
|
Debug.WriteLine($"Fetched {Identifier}: {BaseCount}");
|
|
return BaseCount;
|
|
}
|
|
|
|
public void SetOPowerLevel(Span<byte> data, int value)
|
|
{
|
|
Debug.WriteLine($"Setting {Identifier}: {value}");
|
|
for (int i = 0; i < BaseCount; i++)
|
|
SetFlag(data, i, i + 1 <= value);
|
|
Debug.Assert(value == GetOPowerLevel(data));
|
|
}
|
|
|
|
public bool GetOPowerS(ReadOnlySpan<byte> data) => HasOPowerS && GetFlag(data, 3);
|
|
public bool GetOPowerMAX(ReadOnlySpan<byte> data) => HasOPowerMAX && GetFlag(data, 4);
|
|
public void SetOPowerS(Span<byte> data, bool value) => SetFlag(data, 3, value);
|
|
public void SetOPowerMAX(Span<byte> data, bool value) => SetFlag(data, 4, value);
|
|
|
|
private bool GetFlag(ReadOnlySpan<byte> data, int index)
|
|
{
|
|
return data[Offset + index] == (byte)OPowerFlagState.Unlocked;
|
|
}
|
|
|
|
private void SetFlag(Span<byte> data, int index, bool value)
|
|
{
|
|
if (index < Count)
|
|
data[Offset + index] = (byte)(value ? OPowerFlagState.Unlocked : OPowerFlagState.Locked);
|
|
}
|
|
}
|
|
} |