2023-10-24 12:04:07 +01:00
|
|
|
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
|
|
|
|
// Check LICENSE in repository root for more information.
|
|
|
|
|
2023-04-28 16:47:02 +01:00
|
|
|
export class ReaderBase {
|
|
|
|
public buffer:Buffer;
|
|
|
|
public offset:number;
|
|
|
|
|
|
|
|
public constructor(buffer:Buffer) {
|
|
|
|
this.buffer = buffer;
|
|
|
|
this.offset = 0;
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:01:33 +00:00
|
|
|
public get readOffset() {
|
|
|
|
return this.offset;
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:14:03 +00:00
|
|
|
public get length() {
|
|
|
|
return this.buffer.length;
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:47:02 +01:00
|
|
|
public readBuffer(bytes:number) {
|
|
|
|
const value = this.buffer.subarray(this.offset, this.offset + bytes);
|
|
|
|
this.offset += bytes;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This has to be a copy as the subarray is only cropped & offset
|
|
|
|
// Realistically this is what we want anyway.
|
|
|
|
public readUint8Array(bytes:number) {
|
|
|
|
const croppedBuffer = this.readBuffer(bytes);
|
|
|
|
const newArray = new Uint8Array(croppedBuffer.length);
|
|
|
|
for (let i = 0; i < croppedBuffer.length; i++) {
|
|
|
|
newArray[i] = croppedBuffer[i];
|
|
|
|
}
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
public readByte() {
|
|
|
|
const value = this.buffer.readInt8(this.offset);
|
|
|
|
this.offset++;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public readUByte() {
|
|
|
|
const value = this.buffer.readUInt8(this.offset);
|
|
|
|
this.offset++;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public readBool() {
|
|
|
|
return Boolean(this.readUByte());
|
|
|
|
}
|
2024-01-01 22:23:16 +00:00
|
|
|
|
2024-11-26 15:08:21 +00:00
|
|
|
public readUShortString() {
|
2024-01-01 22:23:16 +00:00
|
|
|
const length = this.readUByte();
|
|
|
|
let text = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
text += String.fromCharCode(this.readUByte());
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2024-11-26 15:08:21 +00:00
|
|
|
public readShortString() {
|
|
|
|
const length = this.readByte();
|
|
|
|
let text = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
text += String.fromCharCode(this.readByte());
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:23:16 +00:00
|
|
|
public readBytesAsString(bytesToRead:number) {
|
|
|
|
let text = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < bytesToRead; i++) {
|
|
|
|
text += String.fromCharCode(this.readUByte());
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2023-04-28 16:47:02 +01:00
|
|
|
}
|