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 pathska-parser.coffee
92 lines (78 loc) · 2.02 KB
/
ska-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
Dissolve = require "dissolve"
fs = require "fs"
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 AnimationParser extends Dissolve
constructor: ->
Dissolve.call @
# Main parser loop
@animation().tap ->
@push @vars
# Utility methods
unparse: (names...) ->
@tap -> delete @vars[name] for name in names
return @
break: ->
@tap -> @stopParsing = true
return @
counted: (name, countVar, cb) ->
currentCount = 0
@uint32le countVar
.loop name, (end) ->
return end true if currentCount++ is @vars[countVar]
cb.call @
exactlyCounted: (name, count, cb) ->
currentCount = 0
@loop name, (end) ->
return end true if currentCount++ is count
cb.call @
flatten: (item, prop) ->
@tap ->
for i in [0...@vars[item].length]
@vars[item][i] = @vars[item][i][prop]
# Renderware specific item parsers
animation: () ->
@uint32le "pointCount"
.int32le "flags"
.floatle "duration"
.tap ->
@exactlyCounted "points", @vars.pointCount, @point
# .unparse "blocks"
point: ->
@tap "rot", ->
@floatle "x"
.floatle "y"
.floatle "z"
.floatle "w"
.tap "pos", ->
@floatle "x"
.floatle "y"
.floatle "z"
.floatle "time"
.uint32le "parent"
.tap ->
@vars.parent /= 36
# TODO: consider enabling back normalization code
# recipr = Math.sqrt @vars.f1 * @vars.f1 + @vars.f2 * @vars.f2 +
# @vars.f3 * @vars.f3 + @vars.f4 * @vars.f4
# @vars.f1 /= recipr
# @vars.f2 /= recipr
# @vars.f3 /= recipr
# @vars.f4 /= recipr
exports.AnimationParser = AnimationParser
# Handle utility call directly from console
if require.main is module
parser = new AnimationParser()
parser.on "data", (animation) ->
console.log JSON.stringify animation, null, 4
process.stdin.pipe parser