-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTnglReader.js
More file actions
147 lines (123 loc) · 3.04 KB
/
TnglReader.js
File metadata and controls
147 lines (123 loc) · 3.04 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
export class TnglReader {
constructor(dataView) {
this._dataView = dataView;
this._index = 0;
}
peekValue(byteCount, unsigned) {
const masks = [0x00, 0x80, 0x8000, 0x800000, 0x80000000];
const offsets = [0x00, 0x100, 0x10000, 0x1000000, 0x100000000];
if (this._index + byteCount > this._dataView.byteLength) {
console.error("End of the data");
throw "PeekOutOfRange";
}
let value = 0;
// if (byteCount == 1) {
// if (unsigned) {
// value = this._dataView.getUint8(this._index);
// } else {
// value = this._dataView.getInt8(this._index);
// }
// }
// else {
for (let i = byteCount; i > 0; i--) {
value <<= 8;
value |= this._dataView.getUint8(this._index + i - 1);
}
// }
// return unsigned ? value >>> 0 : value;
if (unsigned) {
return value >>> 0;
} else {
if (byteCount < 4) {
value >>>= 0;
if ((value & masks[byteCount]) != 0) {
return value - offsets[byteCount];
}
} else {
return value;
}
}
}
readValue(byteCount, unsigned) {
try {
const val = this.peekValue(byteCount, unsigned);
this.foward(byteCount);
return val;
} catch {
throw "Read out of range";
}
}
readBytes(byteCount) {
if (this._index + byteCount <= this._dataView.byteLength) {
let bytes = [];
for (let i = 0; i < byteCount; i++) {
bytes.push(this._dataView.getUint8(this._index + i));
}
this.foward(byteCount);
return bytes;
} else {
console.error("End of the data");
throw "Bytes read out of range";
}
}
readString(bufferLength) {
if (this._index + bufferLength <= this._dataView.byteLength) {
let string = "";
let endOfTheString = false;
for (let i = 0; i < bufferLength; i++) {
let charCode = this._dataView.getUint8(this._index + i);
if (charCode === 0) {
endOfTheString = true;
}
if (!endOfTheString) {
string += String.fromCharCode(charCode);
}
}
return string;
} else {
console.warn("End of the data");
throw "Bytes read out of range";
}
}
peekFlag() {
return this.peekValue(1, true);
}
readFlag() {
return this.readValue(1, true);
}
readInt8() {
return this.readValue(1, false);
}
readUint8() {
return this.readValue(1, true);
}
readInt16() {
return this.readValue(2, false);
}
readUint16() {
return this.readValue(2, true);
}
readInt32() {
return this.readValue(4, false);
}
readUint32() {
return this.readValue(4, true);
}
get available() {
return this._dataView.byteLength - this._index;
}
foward(byteCount) {
if (this._index + byteCount <= this._dataView.byteLength) {
this._index += byteCount;
} else {
this._index = this._dataView.byteLength;
}
}
back(byteCount) {
if (this._index >= byteCount) {
this._index -= byteCount;
} else {
this._index = 0;
}
}
}