From d0a00cd9f897165bab2d45ead86946080f103635 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 5 Dec 2024 02:06:53 -0500 Subject: [PATCH 1/2] Remove protodef types --- src/datatypes/compiler-minecraft.js | 68 ----------------------------- src/datatypes/minecraft.js | 11 ++--- src/datatypes/varlong.js | 63 -------------------------- 3 files changed, 3 insertions(+), 139 deletions(-) delete mode 100644 src/datatypes/varlong.js diff --git a/src/datatypes/compiler-minecraft.js b/src/datatypes/compiler-minecraft.js index 3dd9483a..0118d4cb 100644 --- a/src/datatypes/compiler-minecraft.js +++ b/src/datatypes/compiler-minecraft.js @@ -116,74 +116,6 @@ Read.lnbt = ['native', minecraft.lnbt[0]] Write.lnbt = ['native', minecraft.lnbt[1]] SizeOf.lnbt = ['native', minecraft.lnbt[2]] -/** - * Bits - */ - -Read.bitflags = ['parametrizable', (compiler, { type, flags, shift, big }) => { - let fstr = JSON.stringify(flags) - if (Array.isArray(flags)) { - fstr = '{' - flags.map((v, k) => fstr += `"${v}": ${big ? 1n << BigInt(k) : 1 << k}` + (big ? 'n,' : ',')) - fstr += '}' - } else if (shift) { - fstr = '{' - for (const key in flags) fstr += `"${key}": ${1 << flags[key]},`; - fstr += '}' - } - return compiler.wrapCode(` - const { value: _value, size } = ${compiler.callType(type, 'offset')} - const value = { _value } - const flags = ${fstr} - for (const key in flags) { - value[key] = (_value & flags[key]) == flags[key] - } - return { value, size } - `.trim()) -}] - -Write.bitflags = ['parametrizable', (compiler, { type, flags, shift, big }) => { - let fstr = JSON.stringify(flags) - if (Array.isArray(flags)) { - fstr = '{' - flags.map((v, k) => fstr += `"${v}": ${big ? 1n << BigInt(k) : 1 << k}` + (big ? 'n,' : ',')) - fstr += '}' - } else if (shift) { - fstr = '{' - for (const key in flags) fstr += `"${key}": ${1 << flags[key]},`; - fstr += '}' - } - return compiler.wrapCode(` - const flags = ${fstr} - let val = value._value ${big ? '|| 0n' : ''} - for (const key in flags) { - if (value[key]) val |= flags[key] - } - return (ctx.${type})(val, buffer, offset) - `.trim()) -}] - -SizeOf.bitflags = ['parametrizable', (compiler, { type, flags, shift, big }) => { - let fstr = JSON.stringify(flags) - if (Array.isArray(flags)) { - fstr = '{' - flags.map((v, k) => fstr += `"${v}": ${big ? 1n << BigInt(k) : 1 << k}` + (big ? 'n,' : ',')) - fstr += '}' - } else if (shift) { - fstr = '{' - for (const key in flags) fstr += `"${key}": ${1 << flags[key]},`; - fstr += '}' - } - return compiler.wrapCode(` - const flags = ${fstr} - let val = value._value ${big ? '|| 0n' : ''} - for (const key in flags) { - if (value[key]) val |= flags[key] - } - return (ctx.${type})(val) - `.trim()) -}] - /** * Command Packet * - used for determining the size of the following enum diff --git a/src/datatypes/minecraft.js b/src/datatypes/minecraft.js index 1a9186d7..117e5ffb 100644 --- a/src/datatypes/minecraft.js +++ b/src/datatypes/minecraft.js @@ -1,14 +1,11 @@ -/* eslint-disable */ const nbt = require('prismarine-nbt') const UUID = require('uuid-1345') const protoLE = nbt.protos.little const protoLEV = nbt.protos.littleVarint -// TODO: deal with this: -const zigzag = require('prismarine-nbt/zigzag') function readUUID (buffer, offset) { - if (offset + 16 > buffer.length) { throw new PartialReadError() } + if (offset + 16 > buffer.length) { throw new Error('Reached end of buffer') } return { value: UUID.stringify(buffer.slice(offset, 16 + offset)), size: 16 @@ -65,7 +62,7 @@ function readEntityMetadata (buffer, offset, _ref) { const metadata = [] let item while (true) { - if (offset + 1 > buffer.length) throw new PartialReadError() + if (offset + 1 > buffer.length) throw new Error('Reached end of buffer') item = buffer.readUInt8(cursor) if (item === endVal) { return { @@ -159,7 +156,5 @@ module.exports = { lnbt: [readNbtLE, writeNbtLE, sizeOfNbtLE], entityMetadataLoop: [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata], ipAddress: [readIpAddress, writeIpAddress, 4], - endOfArray: [readEndOfArray, writeEndOfArray, sizeOfEndOfArray], - zigzag32: zigzag.interpret.zigzag32, - zigzag64: zigzag.interpret.zigzag64 + endOfArray: [readEndOfArray, writeEndOfArray, sizeOfEndOfArray] } diff --git a/src/datatypes/varlong.js b/src/datatypes/varlong.js deleted file mode 100644 index 5e57ad7a..00000000 --- a/src/datatypes/varlong.js +++ /dev/null @@ -1,63 +0,0 @@ -function sizeOfVarLong (value) { - if (typeof value.valueOf() === 'object') { - value = (BigInt(value[0]) << 32n) | BigInt(value[1]) - } else if (typeof value !== 'bigint') value = BigInt(value) - - let cursor = 0 - while (value > 127n) { - value >>= 7n - cursor++ - } - return cursor + 1 -} - -/** - * Reads a 64-bit VarInt as a BigInt - */ -function readVarLong (buffer, offset) { - let result = BigInt(0) - let shift = 0n - let cursor = offset - let size = 0 - - while (true) { - if (cursor + 1 > buffer.length) { throw new Error('unexpected buffer end') } - const b = buffer.readUInt8(cursor) - result |= (BigInt(b) & 0x7fn) << shift // Add the bits to our number, except MSB - cursor++ - if (!(b & 0x80)) { // If the MSB is not set, we return the number - size = cursor - offset - break - } - shift += 7n // we only have 7 bits, MSB being the return-trigger - if (shift > 63n) throw new Error(`varint is too big: ${shift}`) - } - - return { value: result, size } -} - -/** - * Writes a zigzag encoded 64-bit VarInt as a BigInt - */ -function writeVarLong (value, buffer, offset) { - // if an array, turn it into a BigInt - if (typeof value.valueOf() === 'object') { - value = BigInt.asIntN(64, (BigInt(value[0]) << 32n)) | BigInt(value[1]) - } else if (typeof value !== 'bigint') value = BigInt(value) - - let cursor = 0 - while (value > 127n) { // keep writing in 7 bit slices - const num = Number(value & 0xFFn) - buffer.writeUInt8(num | 0x80, offset + cursor) - cursor++ - value >>= 7n - } - buffer.writeUInt8(Number(value), offset + cursor) - return offset + cursor + 1 -} - -module.exports = { - Read: { varint64: ['native', readVarLong] }, - Write: { varint64: ['native', writeVarLong] }, - SizeOf: { varint64: ['native', sizeOfVarLong] } -} From e14d399593de355d2d64c4ddf8905db70ea573e4 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Mon, 9 Dec 2024 17:42:52 -0500 Subject: [PATCH 2/2] Update compiler-minecraft.js --- src/datatypes/compiler-minecraft.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datatypes/compiler-minecraft.js b/src/datatypes/compiler-minecraft.js index 0118d4cb..75fd0b9a 100644 --- a/src/datatypes/compiler-minecraft.js +++ b/src/datatypes/compiler-minecraft.js @@ -1,7 +1,7 @@ /* eslint-disable */ const UUID = require('uuid-1345') const minecraft = require('./minecraft') -const { Read, Write, SizeOf } = require('./varlong') +const [Read, Write, SizeOf] = [{}, {}, {}] /** * UUIDs