-
Notifications
You must be signed in to change notification settings - Fork 28
/
optional.js
54 lines (50 loc) · 1.52 KB
/
optional.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
function readOptionalNbt (buffer, offset, { tagType }, rootNode) {
if (offset + 1 > buffer.length) { throw new Error('Read out of bounds') }
if (buffer.readInt8(offset) === 0) return { size: 1 }
return this.read(buffer, offset, tagType, rootNode)
}
function writeOptionalNbt (value, buffer, offset, { tagType }, rootNode) {
if (value === undefined) {
buffer.writeInt8(0, offset)
return offset + 1
}
return this.write(value, buffer, offset, tagType, rootNode)
}
function sizeOfOptionalNbt (value, { tagType }, rootNode) {
if (value === undefined) { return 1 }
return this.sizeOf(value, tagType, tagType, rootNode)
}
const compiler = {
Read: {
optionalNbtType: ['parametrizable', (compiler, { tagType }) => {
return compiler.wrapCode(`
if (offset + 1 > buffer.length) { throw new PartialReadError() }
if (buffer.readInt8(offset) === 0) return { size: 1 }
return ${compiler.callType(tagType)}
`)
}]
},
Write: {
optionalNbtType: ['parametrizable', (compiler, { tagType }) => {
return compiler.wrapCode(`
if (value === undefined) {
buffer.writeInt8(0, offset)
return offset + 1
}
return ${compiler.callType('value', tagType)}
`)
}]
},
SizeOf: {
optionalNbtType: ['parametrizable', (compiler, { tagType }) => {
return compiler.wrapCode(`
if (value === undefined) { return 1 }
return ${compiler.callType('value', tagType)}
`)
}]
}
}
module.exports = {
compiler,
interpret: { optionalNbtType: [readOptionalNbt, writeOptionalNbt, sizeOfOptionalNbt] }
}