Skip to content

Commit ddca8f1

Browse files
authored
Implement ToByteArray method for NLua.LuaTable
1 parent 2430e21 commit ddca8f1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

HashifyNETCLI/Core/ScriptHelpers.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,28 @@ public static byte[] HexToArray(string hex)
9393

9494
return bytes;
9595
}
96+
97+
public static byte[] ToByteArray(NLua.LuaTable table)
98+
{
99+
if (table == null)
100+
throw new ArgumentNullException(nameof(table));
101+
102+
if (table.Values.Count < 1)
103+
return Array.Empty<byte>();
104+
105+
byte[] result = new byte[table.Values.Count];
106+
int ndx = 0;
107+
foreach (var value in table.Values)
108+
{
109+
if (value is not long && value is not int && value is not byte)
110+
throw new ArgumentException("Lua table contains non-integer values.");
111+
112+
byte b = Convert.ToByte(value);
113+
result[ndx] = b;
114+
ndx++;
115+
}
116+
117+
return result;
118+
}
96119
}
97120
}

0 commit comments

Comments
 (0)