diff --git a/base/ReaderBase.ts b/base/ReaderBase.ts index e425152..45d814f 100644 --- a/base/ReaderBase.ts +++ b/base/ReaderBase.ts @@ -82,4 +82,26 @@ export class ReaderBase { return text; } + + public readVarint() { + let total = 0; + let shift = 0; + let byte = this.readUByte(); + + if (!(byte & 0x80)) { + return (byte & 0x7F); + } else { + let end = false; + while (!end) { + if (shift) { + byte = this.readUByte(); + } + total |= ((byte & 0x7F) << shift); + end = !(byte & 0x80); + shift += 7; + } + } + + return total; + } } \ No newline at end of file diff --git a/base/WriterBase.ts b/base/WriterBase.ts index ffad28f..38f87cd 100644 --- a/base/WriterBase.ts +++ b/base/WriterBase.ts @@ -112,4 +112,17 @@ export class WriterBase { return this; } + + public writeVarint(value: number) { + let temp: number; + while (value > 0) { + temp = value & 0x7F; + if (!!(value >>= 7)) { + temp |= 0xB4; + } + this.writeUByte(temp); + } + + return this; + } } \ No newline at end of file diff --git a/readers/IReader.ts b/readers/IReader.ts index 99abd77..88af944 100644 --- a/readers/IReader.ts +++ b/readers/IReader.ts @@ -12,6 +12,7 @@ export interface IReader { readUShortString(): string, readShortString(): string, readBytesAsString(bytesToRead:number): string, + readVarint(): number, readShort(): number, readUShort(): number, readInt(): number, @@ -25,4 +26,6 @@ export interface IReader { readShortsAsString(shortsToRead:number): string, readUString16(): string, readString16(): string, + readArbInt(length: number): number, + readArbUInt(length: number): number, } \ No newline at end of file diff --git a/readers/ReaderBE.ts b/readers/ReaderBE.ts index a26e760..ef6c2c7 100644 --- a/readers/ReaderBE.ts +++ b/readers/ReaderBE.ts @@ -106,4 +106,16 @@ export class ReaderBE extends ReaderBase implements IReader { return text; } + + public readArbInt(length: number) { + const value = this.buffer.readIntBE(this.offset, length); + this.offset += length; + return value; + } + + public readArbUInt(length: number) { + const value = this.buffer.readUIntBE(this.offset, length); + this.offset += length; + return value; + } } \ No newline at end of file diff --git a/readers/ReaderLE.ts b/readers/ReaderLE.ts index e3a3e5e..3ca085e 100644 --- a/readers/ReaderLE.ts +++ b/readers/ReaderLE.ts @@ -106,4 +106,16 @@ export class ReaderLE extends ReaderBase implements IReader { return text; } + + public readArbInt(length: number) { + const value = this.buffer.readIntLE(this.offset, length); + this.offset += length; + return value; + } + + public readArbUInt(length: number) { + const value = this.buffer.readUIntLE(this.offset, length); + this.offset += length; + return value; + } } \ No newline at end of file diff --git a/writers/IWriter.ts b/writers/IWriter.ts index a4a62b8..2eaf653 100644 --- a/writers/IWriter.ts +++ b/writers/IWriter.ts @@ -12,6 +12,7 @@ export interface IWriter { writeUByte(value:number): IWriter, writeBool(value:boolean|number): IWriter, writeStringAsBytes(text:string): IWriter, + writeVarint(value: number): IWriter, writeShort(value:number): IWriter, writeUShort(value:number): IWriter, writeInt(value:number): IWriter, diff --git a/writers/WriterBE.ts b/writers/WriterBE.ts index b039241..54e3734 100644 --- a/writers/WriterBE.ts +++ b/writers/WriterBE.ts @@ -173,6 +173,8 @@ export class WriterBE extends WriterBase implements IWriter { return this; } + // ! TODO: Implement this properly. This was a quick, hacky implementation for mc-beta-server + // ! as it is used in one single place in the whole game's protocol. public writeJavaUTF(text: string) { const dataWriter = new WriterBE();