Skip to content

Commit

Permalink
Implement C# 7 varint writing/reading
Browse files Browse the repository at this point in the history
  • Loading branch information
tgpholly committed Dec 16, 2024
1 parent bf8b3f7 commit 4f33542
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
22 changes: 22 additions & 0 deletions base/ReaderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions base/WriterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions readers/IReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IReader {
readUShortString(): string,
readShortString(): string,
readBytesAsString(bytesToRead:number): string,
readVarint(): number,
readShort(): number,
readUShort(): number,
readInt(): number,
Expand All @@ -25,4 +26,6 @@ export interface IReader {
readShortsAsString(shortsToRead:number): string,
readUString16(): string,
readString16(): string,
readArbInt(length: number): number,
readArbUInt(length: number): number,
}
12 changes: 12 additions & 0 deletions readers/ReaderBE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions readers/ReaderLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions writers/IWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions writers/WriterBE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 4f33542

Please sign in to comment.