-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinFile.ts
45 lines (34 loc) · 920 Bytes
/
BinFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export class BinFile {
private blob: string | null = null;
private ptr: number = -1;
fromString(content: string, callback: (...args: any[]) => any) {
this.blob = content;
this.ptr = 0;
this.decode(callback);
}
getUByte() {
return (this.blob as string).charCodeAt(this.ptr++);
}
getUShort() {
const b1 = this.getUByte(),
b2 = this.getUByte();
return (b2 << 8) + b1;
}
getShort() {
return ((this.getUShort() + 0x8000) % 0x10000) - 0x8000;
}
getString(length: number) {
const start = this.ptr;
this.ptr += length;
return (this.blob as string).substr(start, length);
}
decode(callback: (...args: any[]) => any): void {
if (this.blob === null) {
console.warn('file has not been loaded!');
return;
}
console.warn('The decode function should be overwritten');
if (callback) callback();
}
}
export default BinFile;