UNPKG

pw-js-world

Version:

An optional package for PW-JS-Api, aims to serve world purposes.

331 lines (330 loc) 7.29 kB
/** * CREDIT: Anatoly for making this Buffer Reader so I don't have to! * Source: https://github.com/Anatoly03/pixelwalker.js/blob/9bb3c7e39a45006086a2abae8c515599bd3db835/src/util/buffer-reader.ts * License: ISC */ /** * Data during the communication in the process is dynamic * typed. Every entry is followed by a byte identifying the * type, followed by data. The type header is noted by its' * 7-bit notation. */ export declare enum ComponentTypeHeader { String = 0, Byte = 1, Int16 = 2, Int32 = 3, Int64 = 4, Float = 5, Double = 6, Boolean = 7, ByteArray = 8, UInt32 = 9 } /** * A Buffer reader is a special buffer extension made to perform * game-specific tasks in the field of communication. * * @implements Buffer */ export default class BufferReader { private buffer; private offset; /** * */ private constructor(); /** * */ static from(from: Uint8Array | Buffer): BufferReader; /** * */ static alloc(amount: number): BufferReader; /** * @param {string} value * @returns {Buffer} */ static String(value?: string): Buffer; /** * @param {number} value * @returns {Buffer} */ static Byte(value?: number): Buffer; /** * @param {number} value * @returns {Buffer} */ static Int16(value?: number): Buffer; /** * @param {number} value * @returns {Buffer} */ static Int32(value?: number): Buffer; /** * @param {bigint} value * @returns {Buffer} */ static Int64(value?: bigint): Buffer; /** * @param {number} value * @returns {Buffer} */ static Float(value?: number): Buffer; /** * @param {number} value * @returns {Buffer} */ static Double(value?: number): Buffer; /** * @param {boolean} value * @returns {Buffer} */ static Boolean(value?: boolean): Buffer; /** * @param {Uint8Array} buffer * @returns {Buffer} */ static ByteArray(buffer?: Buffer): Buffer; /** * @param {number} value * @returns {Buffer} */ static UInt32(value?: number): Buffer; /** * @param {number} value * @returns {Buffer} */ static Magic(value: number): Buffer; /** * @param {number} value * @returns {Buffer} */ static Bit7(value?: number): Buffer; /** * @param tt * @param value */ static Dynamic(tt: ComponentTypeHeader, value: boolean | number | bigint | string | Buffer): Buffer; /** * */ get length(): number; /** * */ subarray(start?: number, end?: number): BufferReader; /** * */ write(value: string): number; /** * */ writeBigInt64BE(value: bigint): number; /** * */ writeBigInt64LE(value: bigint): number; /** * */ writeUInt8(value: number): number; /** * */ writeUInt16LE(value: number): number; /** * */ writeUInt16BE(value: number): number; /** * */ writeUInt32LE(value: number): number; /** * */ writeUInt32BE(value: number): number; /** * */ writeInt8(value: number): number; /** * */ writeInt16LE(value: number): number; /** * */ writeInt16BE(value: number): number; /** * */ writeInt32LE(value: number): number; /** * */ writeInt32BE(value: number): number; /** * */ writeFloatLE(value: number): number; /** * */ writeFloatBE(value: number): number; /** * */ writeDoubleLE(value: number): number; /** * */ writeDoubleBE(value: number): number; /** * */ readBigUInt64BE(): bigint; /** * */ readBigUInt64LE(): bigint; /** * */ readBigInt64BE(): bigint; /** * */ readBigInt64LE(): bigint; /** * */ expectUInt8(value: number): number; /** * */ readUInt8(): number; /** * */ readUInt16LE(): number; /** * */ readUInt16BE(): number; /** * */ readUInt32LE(): number; /** * */ readUInt32BE(): number; /** * */ readInt8(): number; /** * */ readInt16LE(): number; /** * */ readInt16BE(): number; /** * */ readInt32LE(): number; /** * */ readInt32BE(): number; /** * */ readFloatLE(): number; /** * */ readFloatBE(): number; /** * */ readDoubleLE(): number; /** * */ readDoubleBE(): number; read(tt: ComponentTypeHeader, littleEndian?: boolean): string | number | bigint | boolean | Buffer; read(tt: ComponentTypeHeader.String, littleEndian?: boolean): string; read(tt: ComponentTypeHeader.Byte, littleEndian?: boolean): number; read(tt: ComponentTypeHeader.Int16, littleEndian?: boolean): number; read(tt: ComponentTypeHeader.Int32, littleEndian?: boolean): number; read(tt: ComponentTypeHeader.Int64, littleEndian?: boolean): bigint; read(tt: ComponentTypeHeader.Float, littleEndian?: boolean): number; read(tt: ComponentTypeHeader.Double, littleEndian?: boolean): number; read(tt: ComponentTypeHeader.Boolean, littleEndian?: boolean): boolean; read(tt: ComponentTypeHeader.ByteArray, littleEndian?: boolean): Buffer; /** * */ toBuffer(): Buffer; /** * https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer */ toArrayBuffer(): ArrayBuffer; /** * */ at(idx: number): number; /** * Advanced the buffer reader by pffset. */ advanceOffset(relativeOffset?: number): this; /** * This function reads how many bytes a normal integer would take * as a 7-bit number * * 1(000 0001) 0(111 1110) */ static length7BitInt(value: number): number; /** * Reads in an integer in 7-bit notation. A 7-bit integer * encoding splits a number into a variable size of bits, * in which the first bit is set while bytes are following. * * @example * * ``` * 1111 0000 1010 1010 1000 0000 0000 0001 Reading In * ^--- ---- ^--- ---- ^--- ---- ^--- ---- * 111 0000 010 1010 000 0000 000 0001 Writing Out * ``` */ read7BitInt(): number; /** * Write a normal integer value into buffer at offset. */ write7BitInt(value: number): void; /** * Reads a dynamic buffer which is prepended by its' length * in 7-bit encoding. */ readDynamicBuffer(): Buffer<ArrayBufferLike>; /** * Append a buffer to the current buffer. Asserts the cursor * to be at the end of the current buffer. */ append(buffer: Buffer): this; /** * Keep Deserializing the buffer for typed data until * you reach the end of the buffer. Typed data consists * of a type indicator in 7-bit-encoding and data following * accordingly. */ deserialize(): (string | number | bigint | boolean | Buffer<ArrayBufferLike>)[]; }