Skip to content

Commit

Permalink
Remove protodef varint types (#552)
Browse files Browse the repository at this point in the history
Remove protodef types which are now natively in ProtoDef
  • Loading branch information
extremeheat authored Dec 9, 2024
1 parent f48b508 commit 347e303
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 140 deletions.
70 changes: 1 addition & 69 deletions src/datatypes/compiler-minecraft.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/datatypes/minecraft.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
63 changes: 0 additions & 63 deletions src/datatypes/varlong.js

This file was deleted.

0 comments on commit 347e303

Please sign in to comment.