-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiniDataView.ts
56 lines (41 loc) · 1.83 KB
/
BiniDataView.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
46
47
48
49
50
51
52
53
54
55
56
import jDataView from "jdataview";
import { BiniStringBlock } from "./BiniStringBlock";
import { Section } from "./ini/Section";
export class BiniDataView extends jDataView {
constructor(buffer: Uint8Array, public filePath: string) {
super(buffer);
}
getReverseInt32(): number {
const reverseBytes = this.getBytes(4).reverse();
return new jDataView(reverseBytes).getInt32();
}
getReverseInt16(): number {
const reverseBytes = this.getBytes(2).reverse();
return new jDataView(reverseBytes).getInt16();
}
/**
* Reads the BINI file this data view is currently viewing.
* @param byteLength the byteLength of the array passed in the ctor.
*/
readBiniFile(byteLength: number): Section[] | null {
const fileTag = this.getString(4);
const version = this.getReverseInt32();
// Check if it's really a bini file
if (fileTag === "BINI" && version === 1) {
const stringBlockOffset = this.getReverseInt32();
if (stringBlockOffset > byteLength) throw "The string block offset was out of range: " + stringBlockOffset;
const sectionBlockOffset = this.tell();
this.seek(stringBlockOffset);
const stringBlockOffsetLength = byteLength - stringBlockOffset;
const strings = this.getString(stringBlockOffsetLength);
const stringBlock = new BiniStringBlock(strings);
this.seek(sectionBlockOffset);
const sections: Section[] = [];
while (this.tell() < stringBlockOffset) {
sections.push(Section.fromBini(this.filePath, this, stringBlock))
}
return sections;
}
return null;
}
}