Skip to content

Commit a73d021

Browse files
authored
Add ToArray method for Lua Tables
1 parent ddca8f1 commit a73d021

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

HashifyNETCLI/Core/ScriptHelpers.cs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static byte[] ToByteArray(NLua.LuaTable table)
100100
throw new ArgumentNullException(nameof(table));
101101

102102
if (table.Values.Count < 1)
103-
return Array.Empty<byte>();
103+
return System.Array.Empty<byte>();
104104

105105
byte[] result = new byte[table.Values.Count];
106106
int ndx = 0;
@@ -116,5 +116,94 @@ public static byte[] ToByteArray(NLua.LuaTable table)
116116

117117
return result;
118118
}
119+
120+
public static Array ToArray(NLua.LuaTable table)
121+
{
122+
if (table == null)
123+
throw new ArgumentNullException(nameof(table));
124+
125+
if (table.Values.Count < 1)
126+
return System.Array.Empty<byte>();
127+
128+
System.Collections.IEnumerator enumerator = table.Values.GetEnumerator();
129+
if (!enumerator.MoveNext())
130+
return System.Array.Empty<byte>();
131+
132+
object o = enumerator.Current;
133+
if (o == null)
134+
{
135+
return System.Array.Empty<byte>();
136+
}
137+
138+
Type type = o.GetType();
139+
Type originalType = type;
140+
141+
// We prefer byte arrays over long arrays if all values are in the range of byte.MinValue and byte.MaxValue
142+
// TODO: This results in shorter scripts but with implicit conversions. Review it to ensure it has no harm.
143+
if (type == typeof(long))
144+
{
145+
bool fits = true;
146+
foreach (object v in table.Values)
147+
{
148+
long l = (long)v;
149+
if (l < byte.MinValue || l > byte.MaxValue)
150+
{
151+
fits = false;
152+
break;
153+
}
154+
}
155+
156+
if (fits)
157+
type = typeof(byte);
158+
}
159+
160+
Array array;
161+
if (type == typeof(byte))
162+
{
163+
array = new byte[table.Values.Count];
164+
}
165+
else if (type == typeof(string))
166+
{
167+
array = new string[table.Values.Count];
168+
}
169+
else if (type == typeof(int))
170+
{
171+
array = new int[table.Values.Count];
172+
}
173+
else if (type == typeof(long))
174+
{
175+
array = new long[table.Values.Count];
176+
}
177+
else if (type == typeof(double))
178+
{
179+
array = new double[table.Values.Count];
180+
}
181+
else if (type == typeof(float))
182+
{
183+
array = new float[table.Values.Count];
184+
}
185+
else if (type == typeof(bool))
186+
{
187+
array = new bool[table.Values.Count];
188+
}
189+
else
190+
{
191+
array = new object[table.Values.Count];
192+
}
193+
194+
int ndx = 0;
195+
foreach (var value in table.Values)
196+
{
197+
if (value == null || value.GetType() != originalType)
198+
{
199+
throw new ArgumentException("Lua table contains mixed or null types which cannot be converted to a C# array directly.");
200+
}
201+
202+
array.SetValue(type == originalType ? value : Convert.ChangeType(value, type), ndx);
203+
ndx++;
204+
}
205+
206+
return array;
207+
}
119208
}
120209
}

0 commit comments

Comments
 (0)