PKHeX/PKHeX.Core/Util/FlagUtil.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

59 lines
2 KiB
C#

using System;
namespace PKHeX.Core;
/// <summary>
/// Utility logic for dealing with bitflags in a byte array.
/// </summary>
public static class FlagUtil
{
/// <summary>
/// Gets the requested <see cref="bitIndex"/> from the byte at <see cref="offset"/>.
/// </summary>
/// <param name="arr">Buffer to read</param>
/// <param name="offset">Offset of the byte</param>
/// <param name="bitIndex">Bit to read</param>
public static bool GetFlag(ReadOnlySpan<byte> arr, int offset, int bitIndex)
{
bitIndex &= 7; // ensure bit access is 0-7
return ((arr[offset] >> bitIndex) & 1) != 0;
}
/// <summary>
/// Sets the requested <see cref="bitIndex"/> value to the byte at <see cref="offset"/>.
/// </summary>
/// <param name="arr">Buffer to modify</param>
/// <param name="offset">Offset of the byte</param>
/// <param name="bitIndex">Bit to write</param>
/// <param name="value">Bit flag value to set</param>
public static void SetFlag(Span<byte> arr, int offset, int bitIndex, bool value)
{
bitIndex &= 7; // ensure bit access is 0-7
var current = arr[offset] & ~(1 << bitIndex);
var newValue = current | ((value ? 1 : 0) << bitIndex);
arr[offset] = (byte)newValue;
}
public static bool[] GitBitFlagArray(ReadOnlySpan<byte> data, int count)
{
var result = new bool[count];
for (int i = 0; i < count; i++)
result[i] = (data[i >> 3] & (1 << (i & 7))) != 0;
return result;
}
public static bool[] GitBitFlagArray(ReadOnlySpan<byte> data) => GitBitFlagArray(data, data.Length << 3);
public static void SetBitFlagArray(Span<byte> data, ReadOnlySpan<bool> value)
{
for (int i = 0; i < value.Length; i++)
{
var ofs = i >> 3;
var mask = (1 << (i & 7));
if (value[i])
data[ofs] |= (byte)mask;
else
data[ofs] &= (byte)~mask;
}
}
}