-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
796 lines (708 loc) · 20.4 KB
/
main.js
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
if (typeof ArrayBuffer == "undefined")
throw new Error("Missing ArrayBuffer");
if (typeof DataView == "undefined")
throw new Error("Missing DataView");
if (typeof Uint8Array == "undefined")
throw new Error("Missing Uint8Array");
if (typeof WeakSet == "undefined")
throw new Error("Missing WeakSet");
if (typeof TextEncoder == "undefined")
throw new Error("Missing TextEncoder");
if (typeof TextDecoder == "undefined")
throw new Error("Missing TextDecoder");
const NBTObjectProto = {}
, TYPER = { 0: "null", 1: "i8", 2: "i16", 3: "i32", 4: "i64", 5: "f32", 6: "f64", 7: "a8", 8: "str", 9: "list", 10: "comp", 11: "a32", 12: "a64" }
, TYPEW = { "null": 0, "i8": 1, "i16": 2, "i32": 3, "i64": 4, "f32": 5, "f64": 6, "a8": 7, "str": 8, "list": 9, "comp": 10, "a32": 11, "a64": 12 }
, PROXIED_NBT = Symbol("NBT_PROXIED");
Object.freeze(NBTObjectProto);
function detectCircularReference(obj) {
var cache = new WeakSet();
function recurse(obj) {
obj = obj[PROXIED_NBT] || obj;
for (var k of Object.getOwnPropertyNames(obj)) {
// Write values
var f = splitTK(k)
, g = TYPEW[f[0]];
if (!g)
continue;
var value = obj[k];
if (typeof value == 'object' && value !== null && !ArrayBuffer.isView(value)) {
if (cache.has(value))
throw new Error("Cannot serialize circular reference to NBT.");
cache.add(value);
if (Array.isArray(value))
for (var im = value.length, i = 0, v = value[0]; i < im; i++, v = value[i])
typeof v == "object" && v !== null && recurse(value);
else
recurse(value);
}
}
return obj
}
cache.add(obj);
return recurse(obj);
}
function getTypeOfArray(l) {
var constructors = new Map();
constructors.set(Int8Array, 1);
constructors.set(Uint8Array, 1);
constructors.set(Uint8ClampedArray, 1);
constructors.set(Int16Array, 2);
constructors.set(Uint16Array, 2);
constructors.set(Int32Array, 3);
constructors.set(Uint32Array, 3);
constructors.set(BigInt64Array, 4);
constructors.set(BigUint64Array, 4);
constructors.set(Float32Array, 5);
constructors.set(Float64Array, 6);
for (var c of constructors)
if (l instanceof c[0])
return c[1];
return false
}
function toTypedArray(t) {
return {
1: Int8Array,
2: Int16Array,
3: Int32Array,
4: BigInt64Array,
5: Float32Array,
6: Float64Array
}[t];
}
function typeCheck(v, t) {
switch (t) {
case "i8":
return typeof v == "number" ? v < -128 ? -128 : v > 127 ? 127 : v : 0;
case "i16":
return typeof v == "number" ? v < -32768 ? -32768 : v > 32767 ? 32767 : v : 0;
case "i32":
return typeof v == "number" ? v < -2147483648 ? -2147483648 : v > 2147483647 ? 2147483647 : v : 0;
case "i64":
if (typeof v == "object") {
typeof v.high != "number" && (v.high = 0);
typeof v.low != "number" && (v.low = 0);
return v
} else if (typeof v == "bigint")
return v < -0x8000000000000000n ? -0x8000000000000000n : v > 0x7FFFFFFFFFFFFFFFn ? 0x7FFFFFFFFFFFFFFFn : v
else
return {
high: 0,
low: typeof v == "number" ? v | 0 : 0
};
case "f32": case "f64":
return typeof v == "number" ? v : 0
case "str":
return typeof v == "undefined" || v === null ? "" : v + "";
case "comp":
return typeof v == "object" ? v : NBT.create(true);
case "list": case "a8": case "a32": case "a64":
return typeof v == "object" ? v : [];
}
}
function splitTK(s) {
var i = s.indexOf(">");
if (i == -1)
return [null, s];
return [
s.slice(0, i),
s.slice(i + 1)
]
}
function ReaderProto(buf, option, isSerial) {
function g(b, c) {
return dtv["get" + b](offset, (offset += c, isBedrock))
}
option = typeof option == "object" ? option : {};
var offset = 0
, dtv = new DataView(buf)
, u8a = new Uint8Array(buf)
, isBedrock = false
, func = {};
if (option.littleEndian) {
// Detect MCBE NBT header
buf.length > 8 && dtv.getUint32(4, true) == u8a.byteLength - 8 && (offset = 8);
isBedrock = true;
}
// Unsigned 16 bit integer
// Only used in length of array-like
func["Uint16"] = g.bind(func, "Uint16", 2);
// 8 bit signed integer
func[1] = g.bind(func, "Int8", 1);
// 16 bit signed integer
func[2] = g.bind(func, "Int16", 2);
// 32 bit signed integer
func[3] = g.bind(func, "Int32", 4);
// 64 bit signed integer
func[4] = option.asBigInt ? g.bind(func, "BigInt64", 8) : function () {
var a = this[3]()
, b = this[3]();
return isBedrock ? { high: b, low: a } : { high: a, low: b }
}.bind(func);
// Single precision float
func[5] = g.bind(func, "Float32", 4);
// Double precision float
func[6] = g.bind(func, "Float64", 8);
// Array of 8 bit signed integer
func[7] = function () {
var a = this[3]()
, r, i;
if (option.asTypedArray) {
r = new (toTypedArray(1))(a);
r.set(u8a.slice(offset, offset += a))
} else {
r = new Array(a);
for (i = 0; i < a; i++)
r[i] = this[1]();
}
return r
}.bind(func);
// String
func[8] = function () {
var l = this["Uint16"]();
return new TextDecoder().decode(u8a.slice(offset, offset += l))
}.bind(func);
// List tag
func[9] = function () {
var r, c, l, i;
// Type of elements in the list
c = this[1]();
// Length of the list
l = this[3]();
r = (option.asTypedArray && toTypedArray(c)) ? new (toTypedArray(c))(l) : new Array(l);
if (this[c]) {
for (i = 0; i < l; i++)
r[i] = this[c]();
Array.isArray(r) && r.unshift(TYPER[c])
} else if (c == 0)
// Null type list, always empty
;
else
throw new Error(`Invalid tag ID at Byte${offset - 1} : ${u8a[offset - 1]}`);
return r;
}.bind(func);
// Compound tag
func[10] = function () {
var r = NBT.create(option.asProxy)
, o = r[PROXIED_NBT] || r
, c, d;
while ((c = u8a[offset]) > 0x00)
if (this[c]) {
offset++;
d = this[8]();
o[TYPER[c] + ">" + d] = this[c]();
} else
throw new Error('Invalid tag ID at Byte' + offset + ' : ' + u8a[offset]);
return offset++, r;
}.bind(func);
// Array of 32 bit signed integer
func[11] = function () {
var l = this[3]()
, r = (option.asTypedArray && option.asBigInt) ? new Int32Array(l) : new Array(l)
, i;
for (i = 0; i < l; i++)
r[i] = this[3]();
return r
}.bind(func);
// Array of 64 bit signed integer
func[12] = function () {
var l = this[3]()
, r = (option.asTypedArray && option.asBigInt) ? new BigInt64Array(l) : new Array(l)
, i;
for (i = 0; i < l; i++)
r[i] = this[4]();
return r
}.bind(func);
func["root"] = function () {
var r = NBT.create(option.asProxy)
, c = u8a[offset]
, o = r[PROXIED_NBT] || r
, d;
if (this[c]) {
offset++;
d = this[8]();
o[TYPER[c] + ">" + d] = this[c]();
} else
throw new Error('Invalid tag ID at Byte' + offset + ' : ' + u8a[offset]);
return r
}.bind(func);
var result = [];
if (isSerial)
while (1) {
if (func[u8a[offset]])
result.push(func["root"]());
else if (offset < buf.byteLength)
offset++;
else
return result
}
else
return {
value: func["root"](),
length: offset
}
}
function WriterProto(obj, option) {
// Write a single value
function g(a, b, c) {
if (offset + b > abuf.byteLength) {
var l = abuf.byteLength;
while (l < offset + b)
l *= 2;
var t1 = new ArrayBuffer(l)
, t2 = new DataView(t1)
, t3 = new Uint8Array(t1);
t3.set(port);
abuf = t1, dtv = t2, port = t3;
}
dtv["set" + a](offset, (offset += b, c), isBedrock);
}
// Write a typed array
function h(a) {
var t = getTypeOfArray(a);
if (!t)
return;
if (offset + a.byteLength > abuf.byteLength) {
var l = abuf.byteLength;
while (l < offset + a.byteLength)
l *= 2;
var t1 = new ArrayBuffer(l)
, t2 = new DataView(t1)
, t3 = new Uint8Array(t1);
t3.set(port);
abuf = t1, dtv = t2, port = t3;
}
if (t == 1)
port.set(a, offset), offset += a.byteLength;
else
for (var i = 0, im = a.length; i < im; i++)
func[t](a[i]);
}
option = typeof option == "object" ? option : {};
var c = option.noCheck ? obj : detectCircularReference(obj)
, isBedrock = !!option.littleEndian
, func = {}
, abuf = new ArrayBuffer(128)
, dtv = new DataView(abuf)
, port = new Uint8Array(abuf)
, offset = 0;
func["Uint16"] = g.bind(func, "Uint16", 2);
func["BigInt64"] = g.bind(func, "BigInt64", 8);
func[1] = g.bind(func, "Int8", 1);
func[2] = g.bind(func, "Int16", 2);
func[3] = g.bind(func, "Int32", 4);
func[4] = function (o) {
if (typeof o == 'bigint')
func["BigInt64"](o);
else if (typeof o != "object")
func[3](0), func[3](0)
else
isBedrock ? (func[3](0 | o.low || 0), func[3](0 | o.high || 0)) : (func[3](0 | o.high || 0), func[3](0 | o.low || 0));
}.bind(func);
func[5] = g.bind(func, "Float32", 4);
func[6] = g.bind(func, "Float64", 8);
// Array of 8 bit signed integer
func[7] = function (o) {
this[3](o.length);
if (getTypeOfArray(o) == 1)
h(o);
else
for (var im = o.length, i = 0; i < im; i++)
this[1](o[i])
}.bind(func);
// String tag
func[8] = function (s) {
var a = new TextEncoder().encode(s);
this["Uint16"](a.length);
h(a);
}.bind(func);
// List tag
// Allows any object with type, length and integer keys
func[9] = function (l) {
var t, m = l, n;
if (l.type && typeof TYPEW[l.type] != 'undefined')
// Specified type
t = TYPEW[l.type], n = l.type;
else if (ArrayBuffer.isView(l))
// Typed array
t = getTypeOfArray(l), n = "Invalid TypedArray";
else {
// Legacy NBT list with type on the first element
t = TYPEW[l[0]];
m = l.slice(1);
n = l[0];
}
// Write as empty list when m.length is falsy or null type
if (t === 0 || !m.length) {
this[1](0);
this[3](0);
} else if (t) {
// Write type of the list
this[1](t);
// Write length
this[3](m.length);
for (var i = 0, im = m.length; i < im; i++)
this[t](m[i])
} else
throw new Error("Invalid type: " + n);
}.bind(func);
// Compound tag
func[10] = function (o, root) {
o = o[PROXIED_NBT] || o;
// Optimize performance
// Reduce traversal times
for (var k of Object.getOwnPropertyNames(o)) {
// Write values
var f = splitTK(k)
, g = TYPEW[f[0]];
// Ignore non-NBT keys
if (!g)
continue;
this[1](g);
this[8](f[1]);
this[g](o[k]);
}
root || this[1](0)
}.bind(func);
// Array of 32 bit signed integer
func[11] = function (o) {
// Write length
this[3](o.length);
for (var im = o.length, i = 0; i < im; i++)
// Write elements
this[3](o[i])
}.bind(func);
// Array of 64 bit signed integer
func[12] = function (o) {
this[3](o.length);
for (var im = o.length, i = 0; i < im; i++)
this[4](o[i])
}.bind(func);
func["root"] = function (o) {
o = o[PROXIED_NBT] || o;
var keys = NBT.keys(o);
if (keys.length != 1 || keys[0] != "comp>")
o = { "comp>": o };
this[10](o, true)
}.bind(func);
func["root"](c);
return abuf.slice(0, offset)
}
class NBT {
/**
* A symbol to get the original object of a proxied NBT object.
*
* Only for debug use.
*/
static get PROXIED_NBT() {
return PROXIED_NBT
}
/**
* Create a new empty NBT object.
* @param {Boolean} [isProxy] - Create a new empty NBT object with proxy if true.
* @returns {Object}
*/
static create(isProxy) {
var result = {
__proto__: NBTObjectProto
};
if (!isProxy)
return result;
return new Proxy(result, {
get: function (target, property) {
if (property === PROXIED_NBT)
return result;
if (typeof property == "symbol")
return void 0;
var tk = splitTK(property);
if (tk && TYPEW[tk[0]])
// Key with type
return target[property];
// Key without type
for (var k of NBT.keys(target))
if (splitTK(k)[1] == property)
return target[k];
return void 0
},
set: function (target, property, value) {
if (typeof property == "symbol")
return void 0;
var tk = splitTK(property);
if (tk && TYPEW[tk[0]]) {
// Key with type
// Directly return existing propertys
if (typeof target[property] != "undefined") {
target[property] = typeCheck(value, tk[0]);
return true
}
// Type override
for (var k of NBT.keys(target))
if (splitTK(k)[1] == tk[1])
delete target[k];
target[property] = typeCheck(value, tk[0]);
return true
}
// Key without type
for (var k of NBT.keys(target)) {
var tk = splitTK(k);
if (tk[1] == property) {
target[k] = typeCheck(value, tk[0]);
return true
}
}
return false
},
deleteProperty: function (target, property) {
if (typeof property == "symbol")
return true;
var tk = splitTK(property);
if (tk && TYPEW[tk[0]]) {
// Key with type
// Directly return existing propertys
if (typeof target[property] != "undefined")
return delete target[property];
// Type override
for (var k of NBT.keys(target))
if (splitTK(k)[1] == tk[1])
return delete target[k];
}
// Key without type
for (var k of NBT.keys(target))
if (splitTK(k)[1] == property)
return delete target[k];
},
setPrototypeOf: function () {
return false
},
defineProperty: function () {
return false
},
preventExtensions: function () {
return false
},
getOwnPropertyDescriptor: function () {
return void 0
},
ownKeys: function (target) {
return NBT.keys(target)
},
has(target, property) {
return NBT.keys(target).indexOf(property) !== -1
}
})
}
/**
* Returns a boolean value that indicates whether a value is a object created by NBT.create().
* @returns {Boolean}
*/
static isNBT(obj) {
function $() { }
$.prototype = NBTObjectProto;
return obj instanceof $;
}
/**
* Returns the names with valid type-value pair of an NBT object.
* @param {Object} obj
* @returns {String[]}
*/
static keys(obj) {
var result = [];
if (obj[PROXIED_NBT])
obj = obj[PROXIED_NBT];
for (var k of Object.getOwnPropertyNames(obj)) {
var l = splitTK(k);
if (TYPEW[l[0]])
result.push(k)
}
return result
}
/**
* Copy the values of all of the NBT properties from one or more source objects
* to a target object.
*
* Returns the target object.
* @param {Object} target - The target object to copy to.
* @param {Object} source - The source object from which to copy properties.
*/
static assign(target, ...source) {
var t = target[PROXIED_NBT] || target
, j = {};
if (!source.length || typeof target != "object" || target === null)
return target;
for (var k of Object.getOwnPropertyNames(t)) {
var l = splitTK(k);
if (TYPEW[l[0]])
j[l[1]] = k
}
for (var s of source) {
if (typeof s != "object" || s === null)
continue;
s = s[PROXIED_NBT] || s;
for (var k of Object.getOwnPropertyNames(s)) {
var l = splitTK(k);
if (TYPEW[l[0]]) {
// Type override
if (j[l[1]])
delete t[j[l[1]]];
t[k] = s[k]
}
}
}
return target
}
/**
* Recursively detect whether objects are compvarely equal.
* @param {Object} a
* @param {Object} b
* @returns {Boolean}
*/
static equal(a, b) {
var visited = new WeakSet();
function recursive(a, b) {
if (a[PROXIED_NBT])
a = a[PROXIED_NBT];
if (b[PROXIED_NBT])
b = b[PROXIED_NBT];
if (visited.has(a) && visited.has(b) && a === b)
return true;
if (a === null && b === null)
return true;
if (a === void 0 && b === void 0)
return true;
if (a === null || b === null || a === void 0 || b === void 0)
return false;
if (typeof a !== typeof b)
return false;
if (typeof a !== 'object')
return a === b;
visited.add(a);
visited.add(b);
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length)
return false;
for (var i = 0; i < a.length; i++)
if (!recursive(a[i], b[i]))
return false;
return true;
}
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
if (getTypeOfArray(a) !== getTypeOfArray(b))
return false;
if (a.length !== b.length)
return false;
for (var i = 0; i < a.length; i++)
if (!recursive(a[i], b[i]))
return false;
return true;
}
var keys1 = NBT.keys(a)
, keys2 = NBT.keys(b);
if (keys1.length !== keys2.length)
return false;
for (var key of keys1)
if (!keys2.includes(key) || !recursive(a[key], b[key], visited))
return false;
return true;
}
return recursive(a, b);
}
/**
* Read NBT data in buffer.
* @param {ArrayBuffer} buf - Input buffer.
* @param {Object} option - Options.
* @param {Boolean} option.littleEndian - Read as little endian if true.
* @param {Boolean} option.asBigInt - Read i64 as BigInt if true.
* @param {Boolean} option.asTypedArray - Read array and list as TypedArray if true.
* @param {Boolean} option.asProxy - Create proxied NBT object.
* @returns {Object}
*/
static Reader(buf, option) {
return ReaderProto(buf, option, !1).value
}
/**
* Read concatenated root label sequence.
* @param {ArrayBuffer} buf - Input buffer.
* @param {Object} option - Options.
* @param {Boolean} option.littleEndian - Read as little endian if true.
* @param {Boolean} option.asBigInt - Read i64 as BigInt if true.
* @param {Boolean} option.asTypedArray - Read array and list as TypedArray if true.
* @param {Boolean} option.asProxy - Create proxied NBT object.
* @returns {Array} Array of NBT root tags.
*/
static ReadSerial(buf, option) {
return ReaderProto(buf, option, !0)
}
/**
* Serialize NBT object.
* @param {Object} obj - Input object.
* @param {Object} option - Options.
* @param {Boolean} option.littleEndian - Write as little endian if true.
* @param {Boolean} option.noCheck - Disable circular reference detect for faster operation.
* @returns {ArrayBuffer}
*/
static Writer(obj, option) {
return WriterProto(obj, option)
}
/**
* Creates a reader.
* @param {ArrayBuffer} buf - Input buffer.
* @param {Object} option - Options.
* @param {Boolean} option.littleEndian - Read as little endian if true.
* @param {Boolean} option.asBigInt - Read i64 as BigInt if true.
* @param {Boolean} option.asTypedArray - Read array and list as TypedArray if true.
* @param {Boolean} option.asProxy - Create proxied NBT object.
* @returns {Object}
*/
constructor(buf, option) {
this.buf = buf;
this.offset = 0;
this.option = option || {};
}
/**
* Get input buffer.
* @returns {ArrayBuffer}
*/
getBuffer() {
return this.buf
}
/**
* Get offset.
* @returns {Number}
*/
getOffset() {
return this.offset
}
/**
* Detect whether reached the end.
* @returns {Boolean}
*/
canRead() {
return this.offset < this.buf.byteLength
}
/**
* Read a single NBT root tag.
*
* Returns null when read to the end.
* @returns {Object|null}
*/
read() {
if (!this.canRead())
return null;
var t = ReaderProto(this.buf.slice(this.offset), this.option, !1);
this.offset += t.length;
return t.value
}
[Symbol.iterator]() {
var t = new NBT(this.buf, this.option);
return {
next: function () {
var s = !t.canRead();
return {
done: s,
value: t.read()
}
}
}
}
}
module.exports = NBT;