mirror of
https://github.com/kwsch/NHSE.git
synced 2026-09-28 21:06:30 -05:00
Stash
Code now compiles, but will need to test and bugfix extensively. Need to pre-fill the pixel buffers with sea first unless the acre fill actually works (maybe?) Items need to be relative'd to apply 1 row lower, and skip the bottom row. Need to check that terrain is the same as well. Note to self: For Map - probably better to get a starting x,y,width and iterate off that, fetching tile from relative, converting to absolute... idk. For View - need to check if tile is in layer.
This commit is contained in:
@@ -7,44 +7,35 @@ 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(byte[] bytes)
|
||||
public static byte[] ConvertHexByteStringToBytes(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var dest = new byte[bytes.Length / 2];
|
||||
for (int i = 0; i < dest.Length; i++)
|
||||
{
|
||||
int ofs = i * 2;
|
||||
var _0 = (char)bytes[ofs + 0];
|
||||
var _1 = (char)bytes[ofs + 1];
|
||||
dest[i] = DecodeTuple(_0, _1);
|
||||
}
|
||||
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)
|
||||
{
|
||||
byte result;
|
||||
if (IsNum(_0))
|
||||
result = (byte)((_0 - '0') << 4);
|
||||
else if (IsHexUpper(_0))
|
||||
result = (byte)((_0 - 'A' + 10) << 4);
|
||||
else
|
||||
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));
|
||||
|
||||
if (IsNum(_1))
|
||||
result |= (byte)(_1 - '0');
|
||||
else if (IsHexUpper(_1))
|
||||
result |= (byte)(_1 - 'A' + 10);
|
||||
else
|
||||
throw new ArgumentOutOfRangeException(nameof(_1));
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] StringToByteArray(string hex)
|
||||
{
|
||||
int NumberChars = hex.Length;
|
||||
byte[] bytes = new byte[NumberChars / 2];
|
||||
for (int i = 0; i < NumberChars; i += 2)
|
||||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user