mirror of
https://github.com/kwsch/NHSE.git
synced 2026-09-28 21:06:30 -05:00
Add automatic read/write items for all pocket items (1-40) (#51)
Decentralizes pocket injection logic from SysBotUI so that other auto-injectors can be passed in (future? like terrain/overworld items). Adds validation to not modify items if the pocket data doesn't have the expected layout. Closes #47 by implementing things in a more abstract & extendable way. Thanks @jfmherokiller for the example implementation which served as an inspiration!
This commit is contained in:
51
NHSE.Injection/SysBot/Decoder.cs
Normal file
51
NHSE.Injection/SysBot/Decoder.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
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(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);
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
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
|
||||
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