21 lines
969 B
C#
21 lines
969 B
C#
|
using Newtonsoft.Json;
|
|||
|
|
|||
|
namespace MinecraftDiscordBot.Models;
|
|||
|
|
|||
|
public class LuaPackedArray {
|
|||
|
public ref object? this[int i] => ref _items[i];
|
|||
|
private readonly object?[] _items;
|
|||
|
public LuaPackedArray(IDictionary<string, object> packedTable) {
|
|||
|
if (packedTable["n"] is not long n) throw new ArgumentException("No length in packed array!");
|
|||
|
_items = new object?[n];
|
|||
|
for (var i = 0; i < _items.Length; i++)
|
|||
|
_items[i] = packedTable.TryGetValue((i + 1).ToString(), out var val) ? val : null;
|
|||
|
}
|
|||
|
public static LuaPackedArray Deserialize(string value) {
|
|||
|
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
|
|||
|
return new LuaPackedArray(dict ?? throw new Exception("Not a packed table (empty object)!"));
|
|||
|
}
|
|||
|
public override string ToString() => _items is { Length: 0 }
|
|||
|
? "Empty Array"
|
|||
|
: string.Join(", ", _items.Select(i => i is null ? "nil" : i.ToString()));
|
|||
|
}
|