-
Notifications
You must be signed in to change notification settings - Fork 14
/
reader.js
392 lines (310 loc) · 9.19 KB
/
reader.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
'use strict';
let assert = require('assert');
let util = require('./util');
class BufferConsumer {
constructor(buffer) {
this.buffer = buffer;
this.position = 0;
}
get length() {
return this.buffer.length;
}
consume(amount) {
let slice = this.buffer.slice(0, amount);
this.buffer = this.buffer.slice(amount);
this.position += amount;
return slice;
}
consume7BitEncodedNumber() {
let number = 0;
let byte = 0;
let bitsRead = 0;
do {
byte = this.consume(1).readUInt8();
number += (byte & ~128) << bitsRead;
bitsRead += 7;
} while(byte & 128);
return number;
}
}
exports.BufferConsumer = BufferConsumer;
class Reader {
consume(buffer, readerResolver) {
return {
type: this.type,
data: this._consume(buffer, readerResolver)
};
}
isValueType() {
return true;
}
get type() {
throw new Exception('Non-overloaded type method');
}
}
class DictionaryReader extends Reader {
constructor(keyReader, valueReader) {
super();
this.keyReader = keyReader;
this.valueReader = valueReader;
}
_consume(buffer, readerResolver) {
let dict = {};
let count = buffer.consume(4).readUInt32LE();
for(let i = 0; i < count; i++) {
let key = this.keyReader.isValueType() ? this.keyReader.consume(buffer, readerResolver) : readerResolver.consume(buffer);
let value = this.valueReader.isValueType() ? this.valueReader.consume(buffer, readerResolver) : readerResolver.consume(buffer);
if(key.type) key = key.data;
dict[key] = value;
}
return dict;
}
get type() {
return `Dictionary<${this.keyReader.type},${this.valueReader.type}>`;
}
isValueType() {
return false;
}
}
class ArrayReader extends Reader {
constructor(elementReader) {
super();
this.elementReader = elementReader;
}
_consume(buffer, readerResolver) {
let array = [];
let count = buffer.consume(4).readUInt32LE();
for(let i = 0; i < count; i++) {
let element = this.elementReader.isValueType() ? this.elementReader.consume(buffer, readerResolver) : readerResolver.consume(buffer);
array.push(element);
}
return array;
}
get type() {
return `Array<${this.elementReader.type}>`;
}
isValueType() {
return false;
}
}
class ListReader extends ArrayReader {
get type() {
return `List<${this.elementReader.type}>`;
}
}
class Texture2DReader extends Reader {
_consume(buffer, readerResolver) {
let format = buffer.consume(4).readInt32LE();
let width = buffer.consume(4).readUInt32LE();
let height = buffer.consume(4).readUInt32LE();
let count = buffer.consume(4).readUInt32LE();
assert.equal(count, 1);
let size = buffer.consume(4).readUInt32LE();
let data = buffer.consume(size);
let dxt = require('dxt');
if(format == 4) {
data = dxt.decompress(data, width, height, dxt.kDxt1);
} else if(format == 5) {
data = dxt.decompress(data, width, height, dxt.kDxt3);
} else if(format == 6) {
data = dxt.decompress(data, width, height, dxt.kDxt5);
} else if(format != 0) {
throw new util.ReadError('Non-implemented Texture2D type: ' + format);
}
for(let i = 0; i < data.length; i += 4) {
let inverseAlpha = 255 / data[i + 3];
data[i] = Math.min(Math.ceil(data[i] * inverseAlpha), 255);
data[i + 1] = Math.min(Math.ceil(data[i + 1] * inverseAlpha), 255);
data[i + 2] = Math.min(Math.ceil(data[i + 2] * inverseAlpha), 255);
}
// Uncomment this for testing, as compression changes the buffer each time.
// format = 0;
return {
format,
width,
height,
data
};
}
get type() {
return 'Texture2D';
}
isValueType() {
return false;
}
}
class SpriteFontReader extends Reader {
_consume(buffer, readerResolver) {
let texture = readerResolver.consume(buffer);
let glyphs = readerResolver.consume(buffer);
let cropping = readerResolver.consume(buffer);
let characterMap = readerResolver.consume(buffer);
let verticalSpacing = buffer.consume(4).readInt32LE();
let horizontalSpacing = buffer.consume(4).readFloatLE();
let kerning = readerResolver.consume(buffer);
let nullableCharReader = new NullableReader(new CharReader());
let defaultCharacter = nullableCharReader.consume(buffer);
return {
texture,
glyphs,
cropping,
characterMap,
verticalSpacing,
horizontalSpacing,
kerning,
defaultCharacter
};
}
get type() {
return 'SpriteFont';
}
isValueType() {
return false;
}
}
class TBinReader extends Reader {
_consume(buffer, readerResolver) {
let size = buffer.consume(4).readInt32LE();
let data = buffer.consume(size);
return {
data: data
};
}
isValueType() {
return false;
}
get type() {
return 'TBin';
}
}
class Vector3Reader extends Reader {
_consume(buffer, readerResolver) {
return {
x: buffer.consume(4).readFloatLE(),
y: buffer.consume(4).readFloatLE(),
z: buffer.consume(4).readFloatLE(),
};
}
get type() {
return 'Vector3';
}
}
function Utf8CharSize(byte) {
// From http://stackoverflow.com/a/2954379
return (( 0xE5000000 >> (( byte >> 3 ) & 0x1e )) & 3 ) + 1;
}
class CharReader extends Reader {
_consume(buffer, readerResolver) {
let charSize = Utf8CharSize(buffer.buffer[0]);
return buffer.consume(charSize).toString('utf8');
}
get type() {
return 'Char';
}
}
class StringReader extends Reader {
_consume(buffer, readerResolver) {
let size = buffer.consume7BitEncodedNumber();
let string = buffer.consume(size).toString('utf8');
return string;
}
get type() {
return 'String';
}
isValueType() {
return false;
}
}
exports.StringReader = StringReader;
class NullableReader extends Reader {
constructor(elementReader) {
super();
this.elementReader = elementReader;
}
_consume(buffer, readerResolver) {
let booleanReader = new BooleanReader();
let hasValue = booleanReader._consume(buffer, readerResolver);
let value = null;
if(hasValue) {
value = this.elementReader.isValueType() ? this.elementReader.consume(buffer, readerResolver) : readerResolver.consume(buffer);
}
return {
data: value
};
}
get type() {
return `Nullable<${this.elementReader.type}>`
}
}
class RectangleReader extends Reader {
_consume(buffer, readerResolver) {
return {
x: buffer.consume(4).readInt32LE(),
y: buffer.consume(4).readInt32LE(),
width: buffer.consume(4).readInt32LE(),
height: buffer.consume(4).readInt32LE()
};
}
get type() {
return 'Rectangle';
}
}
class Int32Reader extends Reader {
_consume(buffer, readerResolver) {
return buffer.consume(4).readInt32LE();
}
get type() {
return 'Int32';
}
}
class BooleanReader extends Reader {
_consume(buffer, readerResolver) {
return Boolean(buffer.consume(1).readUInt8());
}
get type() {
return 'Boolean';
}
}
class ReaderResolver {
constructor(readers) {
this.readers = readers;
}
consume(buffer) {
let index = buffer.consume7BitEncodedNumber() - 1;
return this.readers[index].consume(buffer, this);
}
}
exports.ReaderResolver = ReaderResolver;
function getReader(type) {
let typeInfo = util.getTypeInfo(type);
typeInfo.subtypes = typeInfo.subtypes.map(getReader);
switch(typeInfo.type) {
case 'Dictionary':
return new DictionaryReader(typeInfo.subtypes[0], typeInfo.subtypes[1]);
case 'Array':
return new ArrayReader(typeInfo.subtypes[0]);
case 'List':
return new ListReader(typeInfo.subtypes[0]);
case 'Texture2D':
return new Texture2DReader();
case 'Vector3':
return new Vector3Reader();
case 'String':
return new StringReader();
case 'Int32':
return new Int32Reader();
case 'Char':
return new CharReader();
case 'Boolean':
return new BooleanReader();
case 'SpriteFont':
return new SpriteFontReader();
case 'Rectangle':
return new RectangleReader();
case 'TBin':
return new TBinReader();
default:
throw new util.ReadError('Non-implemented file reader for "' + type + '"');
}
}
exports.getReader = getReader;