forked from krapnikkk/fgui-restore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArray.js
More file actions
192 lines (161 loc) · 4.29 KB
/
Copy pathByteArray.js
File metadata and controls
192 lines (161 loc) · 4.29 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class ByteArray {
stringTable;
version = 0;
littleEndian;
_buffer;
_view;
_pos;
_length;
constructor(buffer, offset, length) {
offset = offset || 0;
if (length == null || length == -1)
length = buffer.byteLength - offset;
this._buffer = buffer;
this._view = new DataView(this._buffer, offset, length);
this._pos = 0;
this._length = length;
}
get data() {
return this._buffer;
}
get pos() {
return this._pos;
}
set pos(value) {
if (value > this._length) throw "Out of bounds";
this._pos = value;
}
get length() {
return this._length;
}
skip(count) {
this._pos += count;
}
validate(forward) {
if (this._pos + forward > this._length) throw "Out of bounds";
}
readByte() {
this.validate(1);
let ret = this._view.getUint8(this._pos);
this._pos++;
return ret;
}
readBool() {
return this.readByte() == 1;
}
readShort() {
this.validate(2);
let ret = this._view.getInt16(this._pos, this.littleEndian);
this._pos += 2;
return ret;
}
readUshort() {
this.validate(2);
let ret = this._view.getUint16(this._pos, this.littleEndian);
this._pos += 2;
return ret;
}
readInt() {
this.validate(4);
let ret = this._view.getInt32(this._pos, this.littleEndian);
this._pos += 4;
return ret;
}
readUint() {
this.validate(4);
let ret = this._view.getUint32(this._pos, this.littleEndian);
this._pos += 4;
return ret;
}
readFloat() {
this.validate(4);
let ret = this._view.getFloat32(this._pos, this.littleEndian);
this._pos += 4;
return ret;
}
readString(len) {
if (len == undefined) len = this.readUshort();
this.validate(len);
let decoder = new TextDecoder();
let ret = decoder.decode(new DataView(this._buffer, this._view.byteOffset + this._pos, len));
this._pos += len;
return ret;
}
readS() {
var index = this.readUshort();
if (index == 65534) //null
return null;
else if (index == 65533)
return ""
else
return this.stringTable[index];
}
readSArray(cnt) {
var ret = new Array(cnt);
for (var i = 0; i < cnt; i++)
ret[i] = this.readS();
return ret;
}
writeS(value) {
var index = this.readUshort();
if (index != 65534 && index != 65533)
this.stringTable[index] = value;
}
readColor() {
var r = this.readByte();
var g = this.readByte();
var b = this.readByte();
this.readByte(); //a
return `rgb(${r},${g},${b})`;
}
readFullColor() {
var r = this.readByte();
var g = this.readByte();
var b = this.readByte();
var a = this.readByte();
return `rgba(${r},${g},${b},${a/255}`;
}
readChar() {
var i = this.readUshort();
return String.fromCharCode(i);
}
readBuffer() {
var count = this.readUint();
this.validate(count);
var ba = new ByteArray(this._buffer, this._view.byteOffset + this._pos, count);
ba.stringTable = this.stringTable;
ba.version = this.version;
this._pos += count;
return ba;
}
seek(indexTablePos, blockIndex) {
var tmp = this._pos;
this._pos = indexTablePos;
var segCount = this.readByte();
if (blockIndex < segCount) {
var useShort = this.readByte() == 1;
var newPos;
if (useShort) {
this._pos += 2 * blockIndex;
newPos = this.readUshort();
}
else {
this._pos += 4 * blockIndex;
newPos = this.readUint();
}
if (newPos > 0) {
this._pos = indexTablePos + newPos;
return true;
}
else {
this._pos = tmp;
return false;
}
}
else {
this._pos = tmp;
return false;
}
}
}
module.exports = ByteArray;