This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scene-parser.coffee
426 lines (394 loc) · 8.66 KB
/
scene-parser.coffee
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
Dissolve = require "dissolve"
fs = require "fs"
con = require "./constants"
fatal = (msg) ->
process.stderr.write "Fail: #{msg}\n"
# process.exit 0
lightTypes =
1: "lUNKNOWN1"
128: "lUNKNOWN128"
129: "lUNKNOWN129"
triggerTypes =
0: "trUNKNOWN1"
1: "trUNKNOWN2"
2: "trUNKNOWN3"
effectTypes =
1: "efUNKNOWN1"
4: "efUNKNOWN4"
5: "efUNKNOWN5"
6: "efUNKNOWN6"
7: "efUNKNOWN7"
10: "efUNKNOWN10"
13: "efUNKNOWN13"
effectTypes2 =
1: "ef2UNKNOWN1"
6: "ef2UNKNOWN6"
10: "ef2UNKNOWN10"
11: "ef2SNOWFLAKES"
13: "ef2UNKNOWN5"
clump = (fval) ->
val = if fval >= 1 then 255 else fval * 255
val = Math.round val
clumpInt val
clumpInt = (val) ->
val = val.toString 16
val = "0#{val}" if val.length < 2
val
floatColorToStr = (c) ->
"0x#{clump c.a}#{clump c.r}#{clump c.g}#{clump c.b}"
intColorToStr = (c) ->
"0x#{clumpInt c.a}#{clumpInt c.r}#{clumpInt c.g}#{clumpInt c.b}"
class SceneParser extends Dissolve
constructor: ->
Dissolve.call @
@loop (end) ->
@scene()
if @stopParsing
@push @vars
end()
zstring: (name) ->
lenProp = "#{name}_len"
@uint32le lenProp
.string name, lenProp
.tap -> delete @vars[lenProp]
fcolor: (name) ->
@tap name, ->
@floatle "r"
.floatle "g"
.floatle "b"
.floatle "a"
.tap ->
@vars[name] = floatColorToStr @vars[name]
icolor: (name) ->
@tap name, ->
@uint8 "r"
.uint8 "g"
.uint8 "b"
.uint8 "a"
.tap ->
@vars[name] = intColorToStr @vars[name]
vector: (name) ->
@tap name, ->
@floatle "x"
.floatle "y"
.floatle "z"
light: ->
@uint32le "idx"
.uint32le "type"
.fcolor "color"
.uint32le "param"
.tap ->
@vars.type = lightTypes[@vars.type] or "lUNKNOWN"
switch @vars.type
when "lUNKNOWN1"
@vector "v1"
.vector "v2"
when "lUNKNOWN128"
@floatle "arg"
.vector "v"
when "lUNKNOWN129"
@uint32le "someAnotherInt"
.vector "v1"
.vector "v2"
foModelV4: ->
@uint32le "idx"
.zstring "fileName"
.vector "v1"
.vector "v2"
.floatle "f1"
.floatle "f2"
.floatle "f3"
.floatle "f4"
.floatle "f5"
.icolor "someColor"
.uint8 "ff1"
.uint8 "ff2"
.uint32le "i6"
.uint8 "ff3"
.int32le "i7"
model: ->
@uint32le "idx"
.zstring "fileName"
.vector "position"
.vector "rotation"
.floatle "scaleX"
.floatle "scaleY"
.floatle "scaleZ"
.icolor "someColor"
.uint8 "i1"
.int32le "i1.5"
.uint8 "i2"
dynamicModel: ->
currentCount = 0
@uint32le "idx"
.uint32le "c1"
.uint32le "c2"
.vector "position"
.vector "rotation"
.floatle "f1"
.floatle "f2"
.vector "v1"
.uint32le "ii1"
.uint32le "ii2"
.loop "someThing", (end) ->
return end true if currentCount++ is 3
@floatle "a1"
.floatle "a2"
.floatle "a3"
.floatle "a4"
.floatle "a5"
.floatle "a6"
.floatle "a7"
.uint8 "someFlag"
.icolor "someColor"
.uint32le "cc"
.zstring "s1"
.zstring "s2"
trigger: ->
@uint32le "idx"
.uint32le "type"
.uint32le "someFlag"
.vector "v"
.uint32le "ii"
.uint32le "ii1"
.uint32le "ii2"
.uint32le "ii3"
.uint32le "ii4"
.zstring "s"
.tap ->
switch @vars.type
when 0
@vector "vv"
.vector "vv2"
when 1
@vector "vv"
.floatle "int"
when 2
@vector "vv"
effect: ->
@uint32le "idx"
.uint32le "type"
.tap ->
@vars.type = effectTypes[@vars.type] or "efUNKNOWN"
switch @vars.type
when "efUNKNOWN1", "efUNKNOWN5", "efUNKNOWN6", "efUNKNOWN10"
@uint32le "param"
.vector "v1"
.vector "v2"
when "efUNKNOWN4"
@uint32le "param"
.vector "v"
when "efUNKNOWN7"
@zstring "effectFile"
.vector "v"
when "efUNKNOWN13"
@zstring "effectFile"
.vector "v1"
.vector "v2"
.vector "v3"
.uint32le "ii"
effectV2: ->
@uint32le "idx"
.uint32le "type"
.uint32le "i1"
.uint32le "i2"
.uint32le "i3"
.uint32le "i4"
.uint32le "i5"
.tap ->
@vars.type = effectTypes2[@vars.type] or "ef2UNKNOWN"
switch @vars.type
when "ef2UNKNOWN1", "ef2UNKNOWN6", "ef2UNKNOWN10"
@uint32le "ii"
.vector "v1"
.vector "v2"
when "ef2SNOWFLAKES"
@uint32le "ii"
when "ef2UNKNOWN5"
@zstring "s"
.vector "v1"
.vector "v2"
.vector "v3"
.uint32le "ii"
vertexModifier: ->
@uint32le "idx"
.uint32le "type"
.vector "v"
.icolor "color"
.tap ->
if @vars.type is 1
@vector "vv"
@uint32le "ii"
.uint8 "c"
sample3D: ->
@uint32le "idx"
.zstring "fileName"
.vector "v1"
.vector "v2"
.vector "v3"
.uint32le "i1"
.uint32le "i2"
.uint32le "i3"
.uint32le "i4"
.uint32le "i5"
sample2D: ->
@uint32le "idx"
.zstring "fileName"
.uint32le "i1"
.uint32le "i2"
.uint8 "c"
waypoint: ->
@uint32le "iii"
.uint8 "a"
sceneItem: ->
@zstring "s"
.uint32le "i1"
.uint32le "i2"
textureProperty: ->
@zstring "fileName"
.int32le "ii"
behaviour: ->
@uint32le "type"
.uint32le "modelId"
.tap ->
@vars.type = con.behaviourType[@vars.type] or "zBEH_UNKNOWN_#{@vars.type}"
counted: (name, countVar, cb) ->
currentCount = 0
@uint32le countVar
# .tap -> process.stderr.write "Parsing #{@vars[countVar]} items #{name}\n"
.loop name, (end) ->
# TODO: remove stopParsing check here, it's only for debug
return end true if currentCount++ is @vars[countVar] or @stopParsing
cb.call @
exactlyCounted: (name, count, cb) ->
currentCount = 0
@loop name, (end) ->
return end true if currentCount++ is count
cb.call @
scene: ->
@zstring "section"
.tap ->
sectionName = @vars.section
delete @vars.section
@tap sectionName[1..-2], ->
@sceneSection sectionName
sceneSection: (sectionName) ->
switch sectionName
when "[Scenefile]"
return
when "[Version]"
@zstring "author"
.uint32le "v1"
.uint32le "v2"
.uint32le "v3"
.uint32le "buildVersion"
.zstring "date"
.zstring "time"
.uint32le "year"
.uint32le "vv2"
when "[Misc]"
@zstring "sceneFile"
.zstring "scenePath"
.zstring "texturePath"
.fcolor "someColor"
.vector "v1"
.vector "v2"
.uint8 "b1"
.uint8 "b2"
.uint8 "b3"
.uint8 "b4"
.uint8 "someFlag"
.tap ->
return unless @vars.someFlag
@uint8 "bb1"
.uint8 "bb2"
.uint8 "bb3"
.uint8 "bb4"
.floatle "someFloat"
.floatle "f1"
.floatle "f2"
when "[WaypointSystem]"
@uint32le "version"
.uint32le "mustBeZero"
.tap ->
return unless @vars.mustBeZero is 0
version = @vars.version
@buffer "data", 0x18 if version >= 5
@counted "v5", "count1", ->
@uint32le "ii1"
@uint32le "ii1ext" if version >= 4
@vector "v1"
.counted "innerdata1", "ci1", ->
@uint32le "x"
.counted "innerdata2", "ci2", ->
@uint32le "x"
if version >= 2
@counted "v2", "count2", ->
@uint32le "iiv2"
.counted "inner2data1", "ci3", ->
@uint32le "y"
if version >= 3
@tap ->
return unless @vars.count1
@exactlyCounted "v3", @vars.count1, ->
@counted "inner3data1", "ci4", ->
@uint32le "y"
@uint32le "mustBe0xFFFF"
return
# @uint32le "count"
# .uint32le "cc"
# .tap ->
# return if @vars.cc
# .tap -> @buffer "data", 24 if @vars.count >= 5
# .uint32le "iii"
when "[Dataset]"
@uint32le "size"
.buffer "data", "size"
.zstring "s1"
.zstring "s2"
when "[SceneOrigin]"
@vector "origin"
when "[Backdrop]"
@zstring "backdropFile"
when "[Lights]"
@counted "lights", "count", @light
when "[FOModels_v4]"
@counted "models", "count", @foModelV4
when "[Models_v3]"
@counted "models", "count", @model
when "[DynamicModels]"
@counted "models", "count", @dynamicModel
when "[Triggers]"
@counted "triggers", "count", @trigger
when "[2DSamples_v2]"
@counted "samples", "count", @sample2D
when "[3DSamples]", "[3DSamples_v2]"
@counted "samples", "count", @sample3D
when "[Effects]"
@counted "effects", "count", @effect
when "[Effects_v2]"
@counted "effects", "count", @effectV2
when "[VertexModifiers]"
@counted "modifiers", "count", @vertexModifier
when "[TextureProperties]"
@counted "properties", "count", @textureProperty
when "[Behaviours]"
@counted "items", "count", @behaviour
when "[Scene]"
@counted "items", "count", @sceneItem
when "[AmbientSound]"
@uint32le "count"
when "[Music]"
@uint32le "count"
when "[EOS]"
@tap -> @stopParsing = true
return
else
fatal "Section #{sectionName} is not supported yet"
exports.SceneParser = SceneParser
# Handle utility call directly from console
if require.main is module
parser = new SceneParser()
parser.on "data", (model) ->
console.log JSON.stringify model, null, 4
process.stdin.pipe parser