UNPKG

@bedrock-apis/nbt

Version:

Solid NBT package, with multiple formats

38 lines (37 loc) 1.36 kB
//#region main/shared.ts const UTF8_DECODER = new TextDecoder(); const UTF8_ENCODER = new TextEncoder(); const UTF8_BUFFER_HELPER = new Uint8Array(32768); function readVarInt32(cursor) { for (let i = 0, shift = 0, num = 0; i < 5; i++, shift += 7) { const byte = cursor.buffer[cursor.pointer++]; num |= (byte & 127) << shift; if ((byte & 128) === 0) return num; } throw new Error("VarInt32 too long: exceeds 5 bytes"); } function readVarInt64(cursor) { for (let i = 0, shift = 0n, num = 0n; i < 10; i++, shift += 7n) { const byte = BigInt(cursor.buffer[cursor.pointer++]); num |= (byte & 127n) << shift; if ((byte & 128n) === 0n) return num; } throw new Error("VarInt64 too long: exceeds 10 bytes"); } function writeVarInt32(cursor, n) { for (let i = 0; i < 5; i++) { if ((n & -128) === 0) return void (cursor.buffer[cursor.pointer++] = n); cursor.buffer[cursor.pointer++] = n & 127 | 128; n >>>= 7; } } function writeVarInt64(cursor, n) { for (let i = 0; i < 10; i++) { if ((n & -128n) === 0n) return void (cursor.buffer[cursor.pointer++] = Number(n)); cursor.buffer[cursor.pointer++] = Number(n & 127n | 128n); n >>= 7n; } throw new ReferenceError("Exceeded size for VarInt64 max up to 10bytes"); } //#endregion export { UTF8_BUFFER_HELPER, UTF8_DECODER, UTF8_ENCODER, readVarInt32, readVarInt64, writeVarInt32, writeVarInt64 };