-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcompiler-compound.js
59 lines (55 loc) · 1.31 KB
/
compiler-compound.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
/* global ctx */
module.exports = {
Read: {
compound: ['context', (buffer, offset) => {
const results = {
value: {},
size: 0
}
while (offset !== buffer.length) {
const typ = ctx.i8(buffer, offset)
if (typ.value === 0) {
results.size += typ.size
break
}
if (typ.value > 20) {
throw new Error(`Invalid tag: ${typ.value} > 20`)
}
const readResults = ctx.nbt(buffer, offset)
offset += readResults.size
results.size += readResults.size
results.value[readResults.value.name] = {
type: readResults.value.type,
value: readResults.value.value
}
}
return results
}]
},
Write: {
compound: ['context', (value, buffer, offset) => {
for (const key in value) {
offset = ctx.nbt({
name: key,
type: value[key].type,
value: value[key].value
}, buffer, offset)
}
offset = ctx.i8(0, buffer, offset)
return offset
}]
},
SizeOf: {
compound: ['context', (value) => {
let size = 1
for (const key in value) {
size += ctx.nbt({
name: key,
type: value[key].type,
value: value[key].value
})
}
return size
}]
}
}