-
Notifications
You must be signed in to change notification settings - Fork 29
/
compound.js
53 lines (48 loc) · 1.28 KB
/
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
module.exports = {
compound: [readCompound, writeCompound, sizeOfCompound]
}
function readCompound (buffer, offset, typeArgs, rootNode) {
const results = {
value: {},
size: 0
}
while (true) {
const typ = this.read(buffer, offset, 'i8', rootNode)
if (typ.value === 0) {
offset += typ.size
results.size += typ.size
break
}
const readResults = this.read(buffer, offset, 'nbt', rootNode)
offset += readResults.size
results.size += readResults.size
results.value[readResults.value.name] = {
type: readResults.value.type,
value: readResults.value.value
}
}
return results
}
function writeCompound (value, buffer, offset, typeArgs, rootNode) {
const self = this
Object.keys(value).forEach(function (key) {
offset = self.write({
name: key,
type: value[key].type,
value: value[key].value
}, buffer, offset, 'nbt', rootNode)
})
offset = this.write(0, buffer, offset, 'i8', rootNode)
return offset
}
function sizeOfCompound (value, typeArgs, rootNode) {
const self = this
const size = Object.keys(value).reduce(function (size, key) {
return size + self.sizeOf({
name: key,
type: value[key].type,
value: value[key].value
}, 'nbt', rootNode)
}, 0)
return 1 + size
}