Repository navigation
Expand file tree
/
Copy pathnbt.lua
More file actions
1219 lines (1059 loc) · 29.8 KB
/
Copy pathnbt.lua
File metadata and controls
1219 lines (1059 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[---------------------------------------------------------------------------
-- Copyright (c) 2040 Dark Energy Processor
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely, subject to the following restrictions:
--
-- 1. The origin of this software must not be misrepresented; you must not
-- claim that you wrote the original software. If you use this software
-- in a product, an acknowledgment in the product documentation would be
-- appreciated but is not required.
-- 2. Altered source versions must be plainly marked as such, and must not
-- be misrepresented as being the original software.
-- 3. This notice may not be removed or altered from any source
-- distribution.
--]]---------------------------------------------------------------------------
-- Lua 5.1 or later is required
assert(_VERSION >= "Lua 5.1", "At least Lua 5.1 is required for this library")
local nbt = {
_VERSION = "1.0.1",
_DESCRIPTION = "Named Binary Tag library for Lua",
_URL = "https://github.com/MikuAuahDark/lua-nbt",
_LICENSE = "zLib"
}
-- Tag constants
local TAG_END = 0 nbt.TAG_END = 0
local TAG_BYTE = 1 nbt.TAG_BYTE = 1
local TAG_SHORT = 2 nbt.TAG_SHORT = 2
local TAG_INT = 3 nbt.TAG_INT = 3
local TAG_LONG = 4 nbt.TAG_LONG = 4
local TAG_FLOAT = 5 nbt.TAG_FLOAT = 5
local TAG_DOUBLE = 6 nbt.TAG_DOUBLE = 6
local TAG_BYTE_ARRAY = 7 nbt.TAG_BYTE_ARRAY = 7
local TAG_STRING = 8 nbt.TAG_STRING = 8
local TAG_LIST = 9 nbt.TAG_LIST = 9
local TAG_COMPOUND = 10 nbt.TAG_COMPOUND = 10
local TAG_INT_ARRAY = 11 nbt.TAG_INT_ARRAY = 11
local TAG_LONG_ARRAY = 12 nbt.TAG_LONG_ARRAY = 12
-- Internal uses
local isLuaJIT = pcall(require, "jit")
local isLua53 = _VERSION >= "Lua 5.3" -- Should work for now
-- Internal functions
local getLongInt, longIntTostring, toInteger, encodeLongInt, decodeLongInt
local encodeShort, encodeInt, decodeShort, decodeInt
local encodeFloat, encodeDouble, decodeFloat, decodeDouble
if isLuaJIT then
-- Use LuaJIT boxed 64-bit int
local ffi = require("ffi")
local i64 = ffi.new("union {int64_t a; uint8_t b[8]; double c;}")
local i32f = ffi.new("union {float a; uint8_t b[4];}")
local zeroLL = loadstring("return 0LL")()
function getLongInt(self)
return self._value + zeroLL
end
function longIntTostring(v)
return tostring(v + zeroLL):sub(1, -2) -- remove additional "L"
end
function toInteger(v)
return v < 0 and math.ceil(v) or math.floor(v)
end
if ffi.abi("be") then
function encodeLongInt(v)
i64.a = v
return ffi.string(i64.b, 8)
end
function decodeLongInt(str)
ffi.copy(i64.b, str, 8)
return i64.a
end
function encodeDouble(v)
i64.c = v
return ffi.string(i64.b, 8)
end
function decodeDouble(str)
ffi.copy(i64.b, str, 8)
return i64.c
end
function encodeFloat(v)
i32f.a = v
return ffi.string(i32f.b, 4)
end
function decodeFloat(str)
ffi.copy(i32f.b, str, 4)
return i32f.a
end
else
function encodeLongInt(v)
i64.a = v
return string.char(
i64.b[7],
i64.b[6],
i64.b[5],
i64.b[4],
i64.b[3],
i64.b[2],
i64.b[1],
i64.b[0]
)
end
function decodeLongInt(str)
local a, b, c, d, e, f, g, h = str:byte(1, 8)
i64.b[0], i64.b[1], i64.b[2], i64.b[3] = h, g, f, e
i64.b[4], i64.b[5], i64.b[6], i64.b[7] = d, c, b, a
return i64.a
end
function encodeDouble(v)
i64.c = v
return string.char(
i64.b[7],
i64.b[6],
i64.b[5],
i64.b[4],
i64.b[3],
i64.b[2],
i64.b[1],
i64.b[0]
)
end
function decodeDouble(str)
local a, b, c, d, e, f, g, h = str:byte(1, 8)
i64.b[0], i64.b[1], i64.b[2], i64.b[3] = h, g, f, e
i64.b[4], i64.b[5], i64.b[6], i64.b[7] = d, c, b, a
return i64.c
end
function encodeFloat(v)
i32f.a = v
return string.char(
i32f.b[3],
i32f.b[2],
i32f.b[1],
i32f.b[0]
)
end
function decodeFloat(str)
local a, b, c, d = str:byte(1, 4)
i32f.b[0], i32f.b[1], i32f.b[2], i32f.b[3] = d, c, b, a
return i32f.a
end
end
elseif isLua53 then
-- Use Lua 5.3 integer
function toInteger(v)
return math.tointeger(v < 0 and math.ceil(v) or math.floor(v))
end
function getLongInt(self)
return toInteger(self._value)
end
function longIntTostring(v)
return string.format("%dL", v)
end
function encodeLongInt(v)
return string.pack(">i8", v)
end
function decodeLongInt(str)
return string.unpack(">i8", str)
end
function encodeDouble(v)
return string.pack(">d", v)
end
function decodeDouble(str)
return string.unpack(">d", str)
end
function encodeFloat(v)
return string.pack(">f", v)
end
function decodeFloat(str)
return string.unpack(">f", str)
end
else
local log2 = math.log(2)
local frexp = not(isLuaJIT) and math.frexp or function(x)
-- Stolen't from cpml/utils
if x == 0 then return 0, 0 end
local e = math.floor(math.log(math.abs(x)) / log2 + 1)
return x / 2 ^ e, e
end
-- Default for Lua 5.1/5.2
function getLongInt(self)
return self._value < 0 and math.ceil(self._value) or math.floor(self._value)
end
function longIntTostring(v)
-- May imprecise but we have no choice
return string.format("%.0fL", v)
end
function toInteger(v)
return v < 0 and math.ceil(v) or math.floor(v)
end
function encodeLongInt(v)
-- This was shamelessly stolen from lua-MessagePack
return string.char(
v < 0 and 255 or 0, -- only 53 bits from double
math.floor(v / 0x1000000000000) % 0x100,
math.floor(v / 0x10000000000) % 0x100,
math.floor(v / 0x100000000) % 0x100,
math.floor(v / 0x1000000) % 0x100,
math.floor(v / 0x10000) % 0x100,
math.floor(v / 0x100) % 0x100,
v % 0x100
)
end
function decodeLongInt(str)
-- This was shamelessly stolen from lua-MessagePack
local a, b, c, d, e, f, g, h = str:byte(1, 8)
if a > 128 then
a, b, c, d = a - 255, b - 255, c - 255, d - 255
e, f, g, h = e - 255, f - 255, g - 255, h - 255
end
return ((((((a * 256 + b) * 256 + c) * 256 + d) * 256 + e) * 256 + f) * 256 + g) * 256 + h
end
function encodeDouble(v)
-- This was shamelessly stolen from lua-MessagePack
local sign = v < 0 and 128 or 0
local mant, expo = frexp(v)
if mant ~= mant then
-- NaN
return "\255\248\0\0\0\0\0\0"
elseif mant == math.huge or expo > 1024 then
if sign == 0 then
-- +inf
return "\127\240\0\0\0\0\0\0"
else
-- -inf
return "\255\240\0\0\0\0\0\0"
end
elseif (mant == 0 and expo == 0) or expo < -1022 then
-- zero
return string.char(sign).."\0\0\0\0\0\0\0"
else
expo = expo + 1022
mant = math.floor((math.abs(mant) * 2.0 - 1.0) * 4503599627370496)
return string.char(
sign + math.floor(expo / 16),
(expo % 16) * 16 + math.floor(mant / 0x1000000000000),
math.floor(mant / 0x10000000000) % 256,
math.floor(mant / 4294967296) % 256,
math.floor(mant / 16777216) % 256,
math.floor(mant / 65536) % 256,
math.floor(mant / 256) % 256,
mant % 256
)
end
end
function decodeDouble(str)
-- This was shamelessly stolen from lua-MessagePack
local a, b, c, d, e, f, g, h = str:byte(1, 8)
local sign = a > 127 and -1 or 1
local expo = (a % 128) * 16 + math.floor(b / 16)
local mant = ((((((b % 16) * 256 + c) * 256 + d) * 256 + e) * 256 + f) * 256 + g) * 256 + h
if mant == 0 and expo == 0 then
return sign * 0.0
elseif expo == 2047 then
if mant == 0 then
return sign * math.huge
else
return 0.0/0.0
end
else
return sign * ((1.0 + mant / 4503599627370496.0) * 2 ^ (expo - 1023))
end
end
function encodeFloat(v)
-- This was shamelessly stolen from lua-MessagePack
local sign = v < 0 and 128 or 0
local mant, expo = frexp(v)
if mant ~= mant then
-- NaN
return "\255\136\0\0"
elseif mant == math.huge or expo > 128 then
if sign then
-- +inf
return "\127\128\0\0"
else
-- -inf
return "\255\128\0\0"
end
elseif (mant == 0 and expo == 0) or expo < -126 then
-- zero
return string.byte(sign).."\0\0\0"
else
expo = expo + 126
mant = math.floor((math.abs(mant) * 2.0 - 1.0) * 8388608)
return string.char(
sign + math.floor(expo / 2),
(expo % 2) * 128 + math.floor(mant / 65536),
math.floor(mant / 256) % 256,
mant % 256
)
end
end
function decodeFloat(str)
-- This was shamelessly stolen from lua-MessagePack
local a, b, c, d = str:byte(1, 4)
local sign = a > 127 and -1 or 1
local expo = (a % 128) * 2 + math.floor(b / 128)
local mant = ((b % 128) * 256 + c) * 256 + d
if mant == 0 and expo == 0 then
return sign * 0.0
elseif expo == 255 then
if mant == 0 then
return sign * math.huge
else
return 0.0/0.0
end
else
return sign * ((1.0 + mant / 8388608) * 2 ^ (expo - 127))
end
end
end
local function isCorrectTypeID(typeID)
return
type(typeID) == "number" and (
typeID == TAG_BYTE or
typeID == TAG_SHORT or
typeID == TAG_INT or
typeID == TAG_LONG or
typeID == TAG_FLOAT or
typeID == TAG_DOUBLE or
typeID == TAG_BYTE_ARRAY or
typeID == TAG_STRING or
typeID == TAG_LIST or
typeID == TAG_COMPOUND or
typeID == TAG_INT_ARRAY or
typeID == TAG_LONG_ARRAY)
end
if isLua53 then
function encodeShort(v)
return string.pack(">i2", v)
end
function decodeShort(str)
return string.unpack(">i2", str:sub(1, 2))
end
function encodeInt(v)
return string.pack(">i4", v)
end
function decodeInt(str)
return string.unpack(">i4", str:sub(1, 4))
end
else
function encodeShort(v)
v = v % 65536
return string.char(math.floor(v / 256), math.floor(v) % 256)
end
function decodeShort(str)
local a, b = str:byte(1, 2)
return a * 256 + b
end
function encodeInt(v)
v = v % 4294967296
return string.char(
math.floor(v / 16777216),
math.floor(v / 65536) % 256,
math.floor(v / 256) % 256,
math.floor(v) % 256
)
end
function decodeInt(str)
local a, b, c, d = str:byte(1, 4)
return a * 16777216 + b * 65536 + c * 256 + d
end
end
local function limitString(v)
v = v:gsub("%z", "\192\128") -- Java modified UTF-8
return v:sub(1, math.min(#v, 32767))
end
-- NBT TAG_* class
local TagClass = {
-- Names, no need to use separate table
"TAG_Byte",
"TAG_Short",
"TAG_Int",
"TAG_Long",
"TAG_Float",
"TAG_Double",
"TAG_Byte_Array",
"TAG_String",
"TAG_List",
"TAG_Compound",
"TAG_Int_Array",
"TAG_Long_Array"
}
TagClass.__index = TagClass
function TagClass.new(type, val, name)
local t = setmetatable({
-- No sanity check because this class is used internally
_value = val,
_name = name or "",
_type = assert(tonumber(type), "invalid type id")
}, TagClass)
return t
end
-- This actually pretty print and
-- may result in very long string
function TagClass:__tostring(indent, noname)
indent = indent or 0
local prefix
local ind = string.rep(" ", indent)
if noname then
prefix = string.format("%s%s(None): ", ind, TagClass[self._type])
else
prefix = string.format("%s%s(%q): ", ind, TagClass[self._type], self._name)
end
if
self._type == TAG_BYTE or
self._type == TAG_SHORT or
self._type == TAG_INT
then
return string.format("%s%d", prefix, self._value)
elseif
self._type == TAG_FLOAT or
self._type == TAG_DOUBLE
then
return string.format("%s%.14g", prefix, self._value)
elseif self._type == TAG_LONG then
return string.format("%s%s", prefix, longIntTostring(self._value))
elseif self._type == TAG_STRING then
return string.format("%s%q", prefix, self._value)
elseif
self._type == TAG_LIST or
self._type == TAG_COMPOUND
then
local isList = self._type == TAG_LIST
local strb = {}
local j = indent + 1
for i, v in ipairs(self._value) do
strb[i] = v:__tostring(j, isList)
end
local len = #strb
return string.format(
"%s%d entr%s\n%s{\n%s\n%s}", -- uh crazy string formats
prefix, len,
len == 1 and "y" or "ies",
ind,
table.concat(strb, "\n"),
ind
)
elseif
self._type == TAG_BYTE_ARRAY or
self._type == TAG_INT_ARRAY
then
-- value is table in this case
local strb = {}
for i, v in ipairs(self._value) do
strb[i] = tostring(v)
end
return string.format("%s[%s]", prefix, table.concat(strb, ", "))
elseif self._type == TAG_LONG_ARRAY then
-- same as above but have to call longIntTostring
local strb = {}
for i, v in ipairs(self._value) do
strb[i] = longIntTostring(v)
end
return string.format("%s[%s]", prefix, table.concat(strb, ", "))
end
end
function TagClass:getTypeID()
return self._type
end
function TagClass:getName()
return self._name
end
function TagClass:getString()
if
self._type == TAG_BYTE_ARRAY or
self._type == TAG_LIST or
self._type == TAG_COMPOUND or
self._type == TAG_INT_ARRAY or
self._type == TAG_LONG_ARRAY
then
error("attempt to get string of array value")
end
return tostring(self._value)
end
function TagClass:getNumber()
if
self._type == TAG_BYTE or
self._type == TAG_SHORT or
self._type == TAG_INT or
self._type == TAG_LONG or
self._type == TAG_FLOAT or
self._type == TAG_DOUBLE
then
return tonumber(self._value) + 0.0 -- Lua 5.3 to float
elseif self._type == TAG_STRING then
-- Try tonumber it
local num = tonumber(self._value)
if num then
return num + 0.0 -- Lua 5.3 to float
end
end
error("attempt to get number of invalid type")
end
function TagClass:getInteger()
if
self._type == TAG_BYTE or
self._type == TAG_SHORT or
self._type == TAG_INT or
self._type == TAG_FLOAT or
self._type == TAG_DOUBLE
then
return toInteger(self._value < 0 and math.ceil(self._value) or math.floor(self._value))
elseif self._type == TAG_LONG then
return getLongInt(self)
elseif self._type == TAG_STRING then
-- Try tonumber first
local num = tonumber(self._value)
if num then
return toInteger(num < 0 and math.ceil(num) or math.floor(num))
end
end
error("attempt to get integer of invalid type")
end
function TagClass:getValue()
return self._value
end
function TagClass:copy(shallow)
if self._type == TAG_LIST or self._type == TAG_COMPOUND then
if shallow then
-- Only copy top element
local new = {}
for k, v in pairs(self._value) do
new[k] = v
end
return TagClass.new(self._type, new, self._name)
else
-- Copy recursively
local new = {}
for k, v in pairs(self._value) do
new[k] = v:copy()
end
return TagClass.new(self._type, new, self._name)
end
elseif
self._type == TAG_BYTE_ARRAY or
self._type == TAG_INT_ARRAY or
self._type == TAG_LONG_ARRAY
then
-- Copy elements
local new = {}
for i, v in ipairs(self._value) do
new[i] = v
end
return TagClass.new(self._type, new, self._name)
else
return TagClass.new(self._type, self._value, self._name)
end
end
function TagClass:encode(noprefix)
local name
if not(noprefix) then
name = limitString(self._name)
end
if self._type == TAG_BYTE then
if noprefix then
return string.char(self._value % 256)
else
-- May strip UTF-8 encoding
return "\1"..encodeShort(#name)..name..string.char(self._value % 256)
end
elseif self._type == TAG_BYTE_ARRAY then
local strb = {}
-- Convert all bytes to string
for i, v in ipairs(self._value) do
strb[i] = string.char(v % 256)
end
if noprefix then
return encodeInt(#strb)..table.concat(strb)
else
return "\7"..encodeShort(#name)..name..encodeInt(#strb)..table.concat(strb)
end
elseif self._type == TAG_SHORT then
if noprefix then
return encodeShort(self._value)
else
return "\2"..encodeShort(#name)..name..encodeShort(self._value)
end
elseif self._type == TAG_INT then
if noprefix then
return encodeInt(self._value)
else
return "\3"..encodeShort(#name)..name..encodeInt(self._value)
end
elseif self._type == TAG_INT_ARRAY then
local strb = {}
-- Convert all values to int
for i, v in ipairs(self._value) do
strb[i] = encodeInt(v)
end
if noprefix then
return encodeInt(#strb)..table.concat(strb)
else
return "\11"..encodeShort(#name)..name..encodeInt(#strb)..table.concat(strb)
end
elseif self._type == TAG_LONG then
if noprefix then
return encodeLongInt(self._value)
else
return "\4"..encodeShort(#name)..name..encodeLongInt(self._value)
end
elseif self._type == TAG_LONG_ARRAY then
local strb = {}
-- Convert all values to long
for i, v in ipairs(self._value) do
strb[i] = encodeLongInt(v)
end
if noprefix then
return encodeInt(#strb)..table.concat(strb)
else
return "\12"..encodeShort(#name)..name..encodeInt(#strb)..table.concat(strb)
end
elseif self._type == TAG_FLOAT then
if noprefix then
return encodeFloat(self._value)
else
return "\5"..encodeShort(#name)..name..encodeFloat(self._value)
end
elseif self._type == TAG_DOUBLE then
if noprefix then
return encodeDouble(self._value)
else
return "\6"..encodeShort(#name)..name..encodeDouble(self._value)
end
elseif self._type == TAG_STRING then
local valueTrunc = limitString(self._value)
if noprefix then
return encodeShort(#valueTrunc)..valueTrunc
else
return "\8"..encodeShort(#name)..name..encodeShort(#valueTrunc)..valueTrunc
end
elseif self._type == TAG_LIST then
-- Special case if length is 0
if #self._value == 0 then
if noprefix then
return "\0\0\0\0\0"
else
return "\9"..encodeShort(#name)..name.."\0\0\0\0\0"
end
else
local typeID = 0
local strb = {}
for i, v in ipairs(self._value) do
local id = v:getTypeID()
if typeID == 0 then
typeID = id
elseif typeID ~= id then
error("TAG_List type inconsistent (old "..typeID.." new "..id..")")
end
strb[i] = v:encode(true)
end
if noprefix then
return string.char(typeID)..encodeInt(#strb)..table.concat(strb)
else
return "\9"..encodeShort(#name)..name..string.char(typeID)..encodeInt(#strb)..table.concat(strb)
end
end
elseif self._type == TAG_COMPOUND then
local strb = {}
for i, v in ipairs(self._value) do
strb[i] = v:encode()
end
if noprefix then
return table.concat(strb).."\0"
else
return "\10"..encodeShort(#name)..name..table.concat(strb).."\0"
end
else
return ""
end
end
-- Public NBT functions
--- Create new `TAG_Byte` object.
-- @tparam number v Byte value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_Byte` type.
-- @warning Value will be converted integer and
-- clamped to -128...127 if outside range
function nbt.newByte(v, name)
v = assert(tonumber(v), "invalid number passed")
return TagClass.new(TAG_BYTE, toInteger(math.min(math.max(v, -128), 127)), name)
end
--- Create new `TAG_Short` object.
-- @tparam number v Short value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_Short` type.
-- @warning Value will be converted integer and
-- clamped to -32768...32767 if outside range
function nbt.newShort(v, name)
v = assert(tonumber(v), "invalid number passed")
return TagClass.new(TAG_SHORT, toInteger(math.min(math.max(v, -32768), 32767)), name)
end
--- Create new `TAG_Int` object.
-- @tparam number v Int value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_Int` type.
-- @warning Value will be converted integer and
-- clamped to -2147483648...2147483647 if outside range
function nbt.newInt(v, name)
v = assert(tonumber(v), "invalid number passed")
return TagClass.new(TAG_INT, toInteger(math.min(math.max(v, -2147483648), 2147483647)), name)
end
if isLuaJIT then
-- As much as I want to avoid FFI
local ffi = require("ffi")
function nbt.newLong(v, name)
v = ffi.cast("int64_t", v)
return TagClass.new(TAG_LONG, v, name)
end
function nbt.newLongArray(value, name)
local list = {}
for i, v in ipairs(value) do
if getmetatable(v) == TagClass then
v = v:getInteger()
elseif type(v) ~= "number" then
v = tonumber(v)
end
if v == nil then
error("invalid value at #"..i)
else
v = ffi.cast("int64_t", v)
end
list[i] = v
end
return TagClass.new(TAG_LONG_ARRAY, list, name)
end
else
function nbt.newLong(v, name)
v = assert(tonumber(v), "invalid number passed")
return TagClass.new(TAG_INT, toInteger(math.min(math.max(v, -9223372036854775808), 9223372036854775807)), name)
end
function nbt.newLongArray(value, name)
local list = {}
for i, v in ipairs(value) do
if getmetatable(v) == TagClass then
v = v:getInteger()
elseif type(v) ~= "number" then
v = tonumber(v)
end
if v == nil then
error("invalid value at #"..i)
else
v = toInteger(math.min(math.max(v, -9223372036854775808), 9223372036854775807))
end
list[i] = v
end
return TagClass.new(TAG_LONG_ARRAY, list, name)
end
end
--- Create new `TAG_Float` object.
-- @tparam number v Float value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_Float` type.
function nbt.newFloat(v, name)
return TagClass.new(TAG_FLOAT, v, name)
end
--- Create new `TAG_Double` object.
-- @tparam number v Double float value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_Double` type.
function nbt.newDouble(v, name)
return TagClass.new(TAG_DOUBLE, v, name)
end
--- Create new `TAG_String` object.
-- @tparam string v String value
-- @tparam[opt] string name Tag name
-- @return Tag class with `TAG_String` type.
function nbt.newString(v, name)
return TagClass.new(TAG_STRING, tostring(v), name)
end
--- Create new `TAG_Compound` object.
-- @tparam table value List of key-value pairs in table
-- @tparam[opt] string name description
-- @return Tag class with `TAG_List` type.
-- @warning If `TAG_*` class object is passed, this function will take
-- the ownership of that object and modify it, so make sure to clone the
-- object if needed.
function nbt.newCompound(value, name)
local compound = {}
for k, v in pairs(value) do
if type(k) ~= "string" then
error(string.format("invalid key %q", k))
end
if getmetatable(v) == TagClass then
v._name = k
compound[#compound + 1] = v
compound[k] = v
else
local datatype = type(v)
-- We have to do some guessing here
-- "cdata" must also be checked because LuaJIT boxed 64-bit int
if datatype == "number" or datatype == "cdata" then
-- if value is integer, do integer guessing
if v % 1 == 0 then
local y
if v >= -128 and v <= 127 then
y = nbt.newByte(tonumber(v), k)
elseif v >= -32768 and v <= 32767 then
y = nbt.newShort(tonumber(v), k)
elseif v >= -2147483648 and v <= 2147483647 then
y = nbt.newInt(tonumber(v), k)
else
y = nbt.newLong(v, k)
end
y._name = k
compound[#compound + 1] = y
compound[k] = y
else
-- assume double, default Lua
local y = nbt.newDouble(v, k)
compound[#compound + 1] = y
compound[k] = y
end
elseif datatype == "string" then
local y = nbt.newString(v, k)
y._name = k
compound[#compound + 1] = y
compound[k] = y
else
-- Should've implement type checking but
-- in Lua it's too complex and may slow
error(string.format("invalid value for key %q", k))
end
end
end
return TagClass.new(TAG_COMPOUND, compound, name)
end
--- Create new `TAG_List` object.
-- @tparam number typeID Valid NBT type ID (`nbt.TAG_*` constants) excluding `TAG_END`
-- @tparam table value List of values in table
-- @tparam[opt] string name description
-- @return Tag class with `TAG_List` type.
-- @warning If `TAG_*` class object is passed, this function will take
-- the ownership of that object, so make sure to clone the object if needed.
function nbt.newList(typeID, value, name)
assert(isCorrectTypeID(typeID), "invalid type ID passed")
local constructorFunction
local list = {}
if typeID == TAG_BYTE then
constructorFunction = nbt.newByte
elseif typeID == TAG_SHORT then
constructorFunction = nbt.newShort
elseif typeID == TAG_INT then
constructorFunction = nbt.newInt
elseif typeID == TAG_LONG then
constructorFunction = nbt.newLong
elseif typeID == TAG_STRING then
constructorFunction = nbt.newString
end
for i, v in ipairs(value) do
if getmetatable(v) == TagClass then
if v:getTypeID() == typeID then
list[i] = v
else
error("type ID mismatch at table index #"..i)
end
elseif constructorFunction == nil then
error("cannot type deduce value at index #"..i)
end
list[i] = constructorFunction(v)
end
return TagClass.new(TAG_LIST, list, name)
end
--- Create new `TAG_Byte_Array` object.
-- @tparam table value List of values in table
-- @tparam[opt] string name description
-- @return Tag class with `TAG_Byte_Array` type.
function nbt.newByteArray(value, name)
local list = {}
for i, v in ipairs(value) do
if getmetatable(v) == TagClass then
v = v:getInteger()
elseif type(v) ~= "number" then
v = tonumber(v)
end
if v == nil then
error("invalid value at #"..i)