NHSE/NHSE.Injection/SysBot/Decoder.cs
Kurt b88c518d5c
Update FieldItemEditor for 3.0.0 (#716)
Updates the Field Item Editor to render layers based on the entire map, and the per-patch positioning of each layer.
Import/export will gracefully handle upgrade/downgrade, and viewport import/export will gracefully update tiles rather than a per-acre basis.

Performance has also been slightly improved; no allocation is done anymore when updating the image.
2026-01-25 16:55:38 -06:00

41 lines
1.4 KiB
C#

using System;
namespace NHSE.Injection;
public static class Decoder
{
private static bool IsNum(char c) => (uint)(c - '0') <= 9;
private static bool IsHexUpper(char c) => (uint)(c - 'A') <= 5;
public static byte[] ConvertHexByteStringToBytes(ReadOnlySpan<byte> bytes)
{
var dest = new byte[bytes.Length / 2];
LoadHexBytesTo(bytes, dest, 2);
return dest;
}
public static void LoadHexBytesTo(ReadOnlySpan<byte> str, Span<byte> dest, int tupleSize)
{
// The input string is 2-char hex values optionally separated.
// The destination array should always be larger or equal than the bytes written. Let the runtime bounds check us.
// Iterate through the string without allocating.
for (int i = 0, j = 0; i < str.Length; i += tupleSize)
dest[j++] = DecodeTuple((char)str[i + 0], (char)str[i + 1]);
}
private static byte DecodeTuple(char _0, char _1)
{
return (byte)(DecodeChar(_0) << 4 | DecodeChar(_1));
static int DecodeChar(char x)
{
if (char.IsAsciiDigit(x))
return (byte)(x - '0');
if (char.IsAsciiHexDigitUpper(x))
return (byte)(x - 'A' + 10);
if (char.IsAsciiHexDigitLower(x))
return (byte)(x - 'a' + 10);
throw new ArgumentOutOfRangeException(nameof(_0));
}
}
}