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
/
Copy patheffect-parser.coffee
340 lines (312 loc) · 6.74 KB
/
effect-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
Dissolve = require "dissolve"
fs = require "fs"
con = require "./constants"
fatal = (msg) ->
process.stderr.write "Fail: #{msg}\n"
# process.exit 0
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 EffectParser 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"
@float "arg"
.vector "v"
when "lUNKNOWN129"
@uint32le "someAnotherInt"
.vector "v1"
.vector "v2"
foModelV4: ->
@uint32le "idx"
# .tap -> delete @vars.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"
# .tap -> delete @vars.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"
# .tap -> delete @vars.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"
.int32le "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 "[Effect Combiner]"
return
when "[Effect_Combiner_Description]"
@buffer "description", 0x20
when "[Effect_Combiner_Parameter]"
@uint32le "param1"
.uint8 "param2"
when "[Effect_Combiner_Position]"
@vector "v1"
.vector "v2"
when "[Effect_Combiner_Form]"
@vector "m1"
.vector "m2"
.vector "m3"
when "[MovingPlanes]"
return # TODO: implement
when "[RandomPlanes]"
return # TODO: implement
when "[Models]"
return # TODO: implement
when "[BeamStar]"
return # TODO: implement
when "[ElectricBolt]"
return # TODO: implement
when "[ParticleCollector]"
return # TODO: implement
when "[ParticleEmitter]"
@uint32le "count"
.tap ->
if @vars.count is 0x120
@buffer "emitterData", "count"
else
fatal "bad emitter data size"
@buffer "unparsedData", "count"
when "[ParticleBeam]"
return # Seems deprecated and ignored
when "[Particle Beam]"
return # TODO: implement
when "[PlaneBeam]"
return # TODO: implement
when "[Sparks]"
return # TODO: implement
when "[Sounds]"
return # TODO: implement
when "[EOF]"
@stopParsing = true
else
fatal "Section #{sectionName} is not supported yet"
exports.EffectParser = EffectParser
# Handle utility call directly from console
if require.main is module
parser = new EffectParser()
parser.on "data", (model) ->
console.log JSON.stringify model, null, 4
process.stdin.pipe parser