From 7228c56b6f955593fe29dbd57b723e802ea1a16d Mon Sep 17 00:00:00 2001 From: Unfamiliar <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Sun, 14 Jan 2024 18:20:51 +0100 Subject: [PATCH] Optimize packet reader --- lib/protocol/hpacket.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/protocol/hpacket.js b/lib/protocol/hpacket.js index 4562fe4..5f34eda 100644 --- a/lib/protocol/hpacket.js +++ b/lib/protocol/hpacket.js @@ -3,6 +3,8 @@ import { PacketInfoManager } from "../services/packetinfo/packetinfomanager.js"; import util from "util"; export class HPacket { + static TEXT_DECODER = new TextDecoder('utf8'); + #isEdited = false; #packetInBytes; #readIndex = 6; @@ -257,7 +259,7 @@ export class HPacket { } if(Number.isInteger(index) && index < this.#packetInBytes.length - 1) { - return Buffer.from(this.#packetInBytes).readInt16BE(index); + return (this.#packetInBytes[index + 1]) | (this.#packetInBytes[index] << 8); } throw new Error("Invalid argument(s) passed or read index out of bounds"); @@ -270,7 +272,7 @@ export class HPacket { } if(Number.isInteger(index) && index < this.#packetInBytes.length - 1) { - return Buffer.from(this.#packetInBytes).readUInt16BE(index); + return (this.#packetInBytes[index + 1]) | (this.#packetInBytes[index] << 8); } throw new Error("Invalid argument(s) passed or read index out of bounds"); @@ -287,7 +289,10 @@ export class HPacket { } if(Number.isInteger(index) && index < this.#packetInBytes.length - 3) { - return Buffer.from(this.#packetInBytes).readInt32BE(index); + return (this.#packetInBytes[index + 3]) | + (this.#packetInBytes[index + 2] << 8) | + (this.#packetInBytes[index + 1] << 16) | + (this.#packetInBytes[index] << 24); } throw new Error("Invalid argument(s) passed or read index out of bounds"); @@ -335,11 +340,7 @@ export class HPacket { } if(Number.isInteger(index) && index < this.#packetInBytes.length + 1 - length) { - let newBytes = new Uint8Array(length); - for(let i = 0; i < length; i++) { - newBytes[i] = this.#packetInBytes[i + index]; - } - return newBytes; + return new Uint8Array(length, index, length); } } @@ -353,7 +354,14 @@ export class HPacket { } if(Number.isInteger(index) && index < this.#packetInBytes.length - 7) { - return Number(Buffer.from(this.#packetInBytes).readBigInt64BE(index)); + return (this.#packetInBytes[index + 7]) | + (this.#packetInBytes[index + 6] << 8) | + (this.#packetInBytes[index + 5] << 16) | + (this.#packetInBytes[index + 4] << 24) + (this.#packetInBytes[index + 3] << 32) | + (this.#packetInBytes[index + 2] << 40) | + (this.#packetInBytes[index + 1] << 48) | + (this.#packetInBytes[index] << 52); } throw new Error("Invalid argument(s) passed or read index out of bounds"); @@ -378,7 +386,7 @@ export class HPacket { let length = this.readUShort(index); index += 2; if(index < this.#packetInBytes.length + 1 - length) { - return Buffer.from(this.#packetInBytes).toString(charset, index, index + length); + return HPacket.TEXT_DECODER.decode(new Uint8Array(this.#packetInBytes.buffer, index, length)); } } @@ -404,7 +412,7 @@ export class HPacket { let length = this.readInteger(index); index += 4; if(index < this.#packetInBytes.length + 1 - length) { - return Buffer.from(this.#packetInBytes).toString(charset, index, index + length); + return HPacket.TEXT_DECODER.decode(new Uint8Array(this.#packetInBytes.buffer, index, length)); } }