-
Notifications
You must be signed in to change notification settings - Fork 10
/
rwstream.js
623 lines (562 loc) · 14.2 KB
/
rwstream.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
function
AttachPlugins()
{
frameTKList[rwID_NODENAME] = { streamRead: NodeNameStreamRead };
geometryTKList[rwID_BINMESHPLUGIN] = { streamRead: rpMeshRead };
materialTKList[rwID_MATERIALEFFECTSPLUGIN] = { streamRead: rpMatfxMaterialStreamRead };
materialTKList[rwID_ENVMAT] = { streamRead: envMatStreamRead };
materialTKList[rwID_SPECMAT] = { streamRead: specMatStreamRead };
atomicTKList[rwID_MATERIALEFFECTSPLUGIN] = { streamRead: rpMatfxAtomicStreamRead };
}
function
RwStreamCreate(buffer)
{
return {
buffer: buffer,
view: new DataView(buffer),
offset: 0,
eof: false
};
}
function
RwStreamSkip(stream, len)
{
stream.offset += len;
}
function
RwStreamReadUInt8(stream)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let v = stream.view.getUint8(stream.offset, true);
stream.offset += 1;
return v;
}
function
RwStreamReadUInt16(stream)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let v = stream.view.getUint16(stream.offset, true);
stream.offset += 2;
return v;
}
function
RwStreamReadUInt32(stream)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let v = stream.view.getUint32(stream.offset, true);
stream.offset += 4;
return v;
}
function
RwStreamReadInt32(stream)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let v = stream.view.getInt32(stream.offset, true);
stream.offset += 4;
return v;
}
function
RwStreamReadReal(stream)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let v = stream.view.getFloat32(stream.offset, true);
stream.offset += 4;
return v;
}
function
RwStreamReadString(stream, length)
{
if(stream.offset >= stream.buffer.byteLength){
stream.eof = true;
return null;
}
let a = new Uint8Array(stream.buffer, stream.offset, length);
let s = "";
for(let i = 0; i < length && a[i] != 0; i++)
s += String.fromCharCode(a[i]);
stream.offset += length;
return s;
}
function
rwStreamReadChunkHeader(stream)
{
let t = RwStreamReadUInt32(stream);
let l = RwStreamReadUInt32(stream);
let id = RwStreamReadUInt32(stream);
if(stream.eof)
return null;
let version = 0;
let build = 0;
if((id & 0xFFFF0000) == 0){
version = id<<8;
build = 0;
}else{
version = ((id>>14) & 0x3FF00) + 0x30000 |
((id>>16) & 3);
build = id & 0xFFFF;
}
return {
type: t,
length: l,
version: version,
build: build,
};
}
function
RwStreamFindChunk(stream, type)
{
let header;
while(header = rwStreamReadChunkHeader(stream)){
if(header.type == type)
return header;
RwStreamSkip(stream, header.length);
}
return null;
}
function
rwPluginRegistryReadDataChunks(tklist, stream, object)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_EXTENSION)) == null)
return null;
let end = stream.offset + header.length;
while(stream.offset < end){
header = rwStreamReadChunkHeader(stream);
if(header.type in tklist && tklist[header.type].streamRead){
if(!tklist[header.type].streamRead(stream, object, header.length))
return null;
}else
RwStreamSkip(stream, header.length);
}
return 1;
}
function
rwStringStreamFindAndRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRING)) == null)
return null;
return RwStreamReadString(stream, header.length);
}
function
rwFrameListStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let numFrames = RwStreamReadInt32(stream);
let frames = [];
for(let i = 0; i < numFrames; i++){
let xx = RwStreamReadReal(stream);
let xy = RwStreamReadReal(stream);
let xz = RwStreamReadReal(stream);
let yx = RwStreamReadReal(stream);
let yy = RwStreamReadReal(stream);
let yz = RwStreamReadReal(stream);
let zx = RwStreamReadReal(stream);
let zy = RwStreamReadReal(stream);
let zz = RwStreamReadReal(stream);
let wx = RwStreamReadReal(stream);
let wy = RwStreamReadReal(stream);
let wz = RwStreamReadReal(stream);
frame = RwFrameCreate();
mat4.set(frame.matrix,
xx, xy, xz, 0,
yx, yy, yz, 0,
zx, zy, zz, 0,
wx, wy, wz, 1);
frames.push(frame);
let parent = RwStreamReadInt32(stream);
RwStreamReadInt32(stream); // unused
if(parent >= 0)
RwFrameAddChild(frames[parent], frame);
}
for(let i = 0; i < numFrames; i++)
if(!rwPluginRegistryReadDataChunks(frameTKList, stream, frames[i]))
return null;
return frames;
}
function
RwTextureStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let flags = RwStreamReadUInt32(stream); // we ignore this
let name = rwStringStreamFindAndRead(stream);
if(name == null) return null;
let mask = rwStringStreamFindAndRead(stream);
if(mask == null) return null;
let tex = RwTextureRead(name, mask);
if(!rwPluginRegistryReadDataChunks(textureTKList, stream, tex))
return null;
return tex;
}
function
RpMaterialStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let mat = RpMaterialCreate();
RwStreamReadInt32(stream); // flags, unused
mat.color[0] = RwStreamReadUInt8(stream);
mat.color[1] = RwStreamReadUInt8(stream);
mat.color[2] = RwStreamReadUInt8(stream);
mat.color[3] = RwStreamReadUInt8(stream);
RwStreamReadInt32(stream); // unused
let textured = RwStreamReadInt32(stream);
mat.surfaceProperties[0] = RwStreamReadReal(stream);
mat.surfaceProperties[1] = RwStreamReadReal(stream);
mat.surfaceProperties[2] = RwStreamReadReal(stream);
if(textured){
if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null)
return null;
mat.texture = RwTextureStreamRead(stream);
if(mat.texture == null)
return null;
}
if(!rwPluginRegistryReadDataChunks(materialTKList, stream, mat))
return null;
return mat;
}
function
rpMaterialListStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let numMaterials = RwStreamReadInt32(stream);
let indices = [];
while(numMaterials--)
indices.push(RwStreamReadInt32(stream));
let materials = []
for(let i = 0; i < indices.length; i++){
if(indices[i] >= 0)
materials.push(materials[indices[i]]);
else{
if((header = RwStreamFindChunk(stream, rwID_MATERIAL)) == null)
return null;
let m = RpMaterialStreamRead(stream);
if(m == null)
return null;
materials.push(m);
}
}
return materials;
}
function
RpGeometryStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let flags = RwStreamReadUInt32(stream);
let numTriangles = RwStreamReadInt32(stream);
let numVertices = RwStreamReadInt32(stream);
let numMorphTargets = RwStreamReadInt32(stream);
if(header.version < 0x34000)
RwStreamSkip(stream, 12);
if(flags & 0x01000000) return null; // native geometry not supported
let geo = RpGeometryCreate(flags, numMorphTargets);
geo.numVertices = numVertices;
if(geo.prelit)
for(let i = 0; i < numVertices; i++){
let r = RwStreamReadUInt8(stream);
let g = RwStreamReadUInt8(stream);
let b = RwStreamReadUInt8(stream);
let a = RwStreamReadUInt8(stream);
geo.prelit.push([r, g, b, a]);
}
for(let i = 0; i < geo.texCoords.length; i++){
let texCoords = geo.texCoords[i];
for(let j = 0; j < numVertices; j++){
let u = RwStreamReadReal(stream);
let v = RwStreamReadReal(stream);
texCoords.push([u, v]);
}
}
for(let i = 0; i < numTriangles; i++){
let w1 = RwStreamReadUInt32(stream);
let w2 = RwStreamReadUInt32(stream);
let v1 = w1>>16 & 0xFFFF;
let v2 = w1 & 0xFFFF;
let v3 = w2>>16 & 0xFFFF;
let matid = w2 & 0xFFFF;
geo.triangles.push([v1, v2, v3, matid]);
}
for(let i = 0; i < numMorphTargets; i++){
let mt = geo.morphTargets[i];
RwStreamSkip(stream, 4*4 + 4 + 4); // ignore bounding sphere and flags
for(let j = 0; j < numVertices; j++){
let x = RwStreamReadReal(stream);
let y = RwStreamReadReal(stream);
let z = RwStreamReadReal(stream);
mt.vertices.push([x, y, z]);
}
if(mt.normals)
for(let j = 0; j < numVertices; j++){
let x = RwStreamReadReal(stream);
let y = RwStreamReadReal(stream);
let z = RwStreamReadReal(stream);
mt.normals.push([x, y, z]);
}
}
if((header = RwStreamFindChunk(stream, rwID_MATLIST)) == null)
return null;
geo.materials = rpMaterialListStreamRead(stream);
if(geo.materials == null)
return null;
if(!rwPluginRegistryReadDataChunks(geometryTKList, stream, geo))
return null;
return geo;
}
function
rpMeshRead(stream, geo, length)
{
geo.meshtype = RwStreamReadInt32(stream);
let numMeshes = RwStreamReadInt32(stream);
geo.totalMeshIndices = RwStreamReadInt32(stream);
while(numMeshes--){
let numIndices = RwStreamReadInt32(stream);
let matid = RwStreamReadInt32(stream);
let m = {
indices: [],
material: geo.materials[matid]
};
while(numIndices--)
m.indices.push(RwStreamReadInt32(stream));
geo.meshes.push(m);
}
return geo;
}
function
rpGeometryListStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let numGeoms = RwStreamReadInt32(stream);
let geoms = []
while(numGeoms--){
if((header = RwStreamFindChunk(stream, rwID_GEOMETRY)) == null)
return null;
let g = RpGeometryStreamRead(stream);
if(g == null)
return null;
geoms.push(g);
}
return geoms;
}
function
rpClumpAtomicStreamRead(stream, frames, geos)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let atomic = RpAtomicCreate();
let frame = RwStreamReadInt32(stream);
let geometry = RwStreamReadInt32(stream);
let flags = RwStreamReadInt32(stream); // ignored
RwStreamReadInt32(stream); // unused
RpAtomicSetFrame(atomic, frames[frame]);
atomic.geometry = geos[geometry];
if(!rwPluginRegistryReadDataChunks(atomicTKList, stream, atomic))
return null;
return atomic;
}
function
RpClumpStreamRead(stream)
{
let header;
if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null)
return null;
let numAtomics = RwStreamReadInt32(stream);
let numLights = 0;
let numCameras = 0;
if(header.version > 0x33000){
numLights = RwStreamReadInt32(stream);
numCameras = RwStreamReadInt32(stream);
}
if((header = RwStreamFindChunk(stream, rwID_FRAMELIST)) == null)
return null;
frames = rwFrameListStreamRead(stream);
if(frames == null)
return null;
clump = RpClumpCreate();
RpClumpSetFrame(clump, frames[0]);
if((header = RwStreamFindChunk(stream, rwID_GEOMETRYLIST)) == null)
return null;
geos = rpGeometryListStreamRead(stream);
if(geos == null)
return null;
while(numAtomics--){
if((header = RwStreamFindChunk(stream, rwID_ATOMIC)) == null)
return null;
let a = rpClumpAtomicStreamRead(stream, frames, geos);
if(a == null)
return null;
clump.atomics.push(a);
}
if(!rwPluginRegistryReadDataChunks(clumpTKList, stream, clump))
return null;
rwFrameSynchLTM(clump.frame);
// TODO? lights, cameras
return clump;
}
/*
* Plugins
*/
/* MatFX */
var rpMATFXEFFECTBUMPMAP = 1;
var rpMATFXEFFECTENVMAP = 2;
var rpMATFXEFFECTBUMPENVMAP = 3;
var rpMATFXEFFECTDUAL = 4;
var rpMATFXEFFECTUVTRANSFORM = 5;
var rpMATFXEFFECTDUALUVTRANSFORM = 6;
function
RpMatFXMaterialSetEffects(mat, effects)
{
mat.matfx = {
type: effects,
bump: false,
env: false,
dual: false,
uvxform: false
};
// TODO: init the relevant fields here
switch(effects){
case rpMATFXEFFECTBUMPMAP:
mat.matfx.bump = true;
break;
case rpMATFXEFFECTENVMAP:
mat.matfx.env = true;
break;
case rpMATFXEFFECTBUMPENVMAP:
mat.matfx.bump = true;
mat.matfx.env = true;
break;
case rpMATFXEFFECTDUAL:
mat.matfx.dual = true;
break;
case rpMATFXEFFECTUVTRANSFORM:
mat.matfx.uvxform = true;
break;
case rpMATFXEFFECTDUALUVTRANSFORM:
mat.matfx.dual = true;
mat.matfx.uvxform = true;
break;
}
}
function
rpMatfxMaterialStreamRead(stream, mat, length)
{
let header;
let effects = RwStreamReadInt32(stream);
RpMatFXMaterialSetEffects(mat, effects);
let mfx = mat.matfx;
for(let i = 0; i < 2; i++){
let type = RwStreamReadInt32(stream);
switch(type){
case rpMATFXEFFECTBUMPMAP:
mfx.bumpCoefficient = RwStreamReadReal(stream);
if(RwStreamReadInt32(stream)){
if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null)
return null;
mfx.bumpedTex = RwTextureStreamRead(stream);
if(mfx.bumpedTex == null)
return null;
}
if(RwStreamReadInt32(stream)){
if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null)
return null;
mfx.bumpTex = RwTextureStreamRead(stream);
if(mfx.bumpTex == null)
return null;
}
break;
case rpMATFXEFFECTENVMAP:
mfx.envCoefficient = RwStreamReadReal(stream);
mfx.envFBalpha = RwStreamReadInt32(stream);
if(RwStreamReadInt32(stream)){
if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null)
return null;
mfx.envTex = RwTextureStreamRead(stream);
if(mfx.envTex == null)
return null;
}
break;
case rpMATFXEFFECTDUAL:
mfs.srcBlend = RwStreamReadInt32(stream);
mfs.dstBlend = RwStreamReadInt32(stream);
if(RwStreamReadInt32(stream)){
if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null)
return null;
mfx.dualTex = RwTextureStreamRead(stream);
if(mfx.dualTex == null)
return null;
}
break;
}
}
return mat;
}
function
rpMatfxAtomicStreamRead(stream, atomic, length)
{
atomic.matfx = RwStreamReadInt32(stream);
if(atomic.matfx)
atomic.pipeline = matFXPipe;
return atomic;
}
/* GTA Node Name */
function
NodeNameStreamRead(stream, frame, length)
{
frame.name = RwStreamReadString(stream, length);
return frame;
}
/* GTA Env Map */
function
envMatStreamRead(stream, mat, length)
{
let sclX = RwStreamReadReal(stream);
let sclY = RwStreamReadReal(stream);
let transSclX = RwStreamReadReal(stream);
let transSclY = RwStreamReadReal(stream);
let shininess = RwStreamReadReal(stream);
RwStreamReadInt32(stream); // ignore
mat.envMap = {
scale: [ sclX, sclY ],
transScale: [ transSclX, transSclY ],
shininess: shininess
};
return mat;
}
/* GTA Spec Map */
function
specMatStreamRead(stream, mat, length)
{
let specularity = RwStreamReadReal(stream);
let texname = RwStreamReadString(stream, 24);
mat.specMap = {
specularity: specularity,
texture: RwTextureRead(texname, "")
};
return mat;
}