this repo has no description
1// https://github.com/NotNite/brc-save-editor/blob/main/src/lib/binary.ts
2export interface BinaryInterface {
3 data: Uint8Array;
4 view: DataView;
5 length: number;
6 position: number;
7}
8
9export class BinaryReader implements BinaryInterface {
10 data: Uint8Array;
11 view: DataView;
12 length: number;
13 position: number;
14
15 constructor(data: Uint8Array) {
16 this.data = data;
17 this.view = new DataView(data.buffer);
18
19 this.length = data.length;
20 this.position = 0;
21 }
22
23 readByte() {
24 return this._read(this.view.getInt8, 1);
25 }
26
27 readBoolean() {
28 return this.readByte() !== 0;
29 }
30
31 readInt32() {
32 return this._read(this.view.getInt32, 4);
33 }
34
35 readUInt32() {
36 return this._read(this.view.getUint32, 4);
37 }
38
39 readSingle() {
40 return this._read(this.view.getFloat32, 4);
41 }
42
43 readInt64() {
44 return this._read(this.view.getBigInt64, 8);
45 }
46
47 readString(length: number) {
48 const result = this.read(length);
49 return new TextDecoder().decode(result);
50 }
51
52 read(length: number) {
53 const data = this.data.subarray(this.position, this.position + length);
54 this.position += length;
55 return data;
56 }
57
58 private _read<T>(
59 func: (position: number, littleEndian?: boolean) => T,
60 length: number
61 ): T {
62 const result = func.call(this.view, this.position, true);
63 this.position += length;
64 return result;
65 }
66}