forked from MrCheeze/skywardsword-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzevdat.py
204 lines (193 loc) · 7.8 KB
/
zevdat.py
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
from util import toStr, unpack, objToJson
from collections import OrderedDict
from pathlib import Path
import struct
import sys
import subprocess
def parse_zevdat(zevdat: bytes):
out = OrderedDict()
out['header'] = unpack("magic evntcount actorcount unk1count unk2count unk3count alwaysev unk4count unk5count unk6count", ">Hhhhhh2shhh",zevdat[:0x14])
assert out['header']['magic'] == 0x775A
evnts = []
for i in range(out['header']['evntcount']):
offset = 0x14+i*0x28
evntdata = zevdat[offset:0x14+(i+1)*0x28]
# dummies always just zeros
evnt = unpack('name dummy1 unk1 dummy2 actorindex actorcount', '>32ssb2shh',evntdata)
evnt['name'] = toStr(evnt['name'])
evnt['offset'] = offset
evnts.append(evnt)
out['evnts'] = evnts
# print(f"evntslen: {len(out['evnts'])}")
actors = []
evntsend = 0x14+out['header']['evntcount']*0x28
for i in range(out['header']['actorcount']):
offset = evntsend+i*0x28
evntdata = zevdat[offset:evntsend+(i+1)*0x28]
# dummies always just zeros
actor = unpack('name unk1 unk2 actindex actcount', '>32shhhh',evntdata)
actor['name'] = toStr(actor['name'])
actor['offset'] = offset
actors.append(actor)
out['actors'] = actors
# print(f"actorlen: {len(out['actors'])}")
actorsend = evntsend+out['header']['actorcount']*0x28
unk1s = []
for i in range(out['header']['unk1count']):
offset = actorsend+i*0x1C
evntdata = zevdat[offset:actorsend+(i+1)*0x1C]
# dummy0 is always 0, dummy1 is always1
unk1 = unpack('name waitfor actorindex unk3 dummy0 thisindex dummy1', '>16shhhhhh', evntdata)
unk1['name'] = toStr(unk1['name'])
# unk1['actor'] = out['actors'][unk1['actorindex']]
unk1['offset'] = offset
unk1s.append(unk1)
out['unk1s'] = unk1s
# print(f"unk1len: {len(out['unk1s'])}")
unk1send = actorsend+out['header']['unk1count']*0x1C
unk2s = []
for i in range(out['header']['unk2count']):
offset = unk1send+i*0xC
evntdata = zevdat[offset:unk1send+(i+1)*0xC]
# thisindex is never read
unk2 = unpack('name eventSubManagerIndex thisindex unk3Index unk3Count', '>4shhhh', evntdata)
unk2['name'] = toStr(unk2['name'])
unk2['offset'] = offset
# unk2['asdfunk1'] = unk1s[unk2['thisindex']]
unk2s.append(unk2)
out['unk2s'] = unk2s
assert len(out['unk2s']) == len(out['unk1s'])
# print(f"unk2len: {len(out['unk2s'])}")
unk2send = unk1send+out['header']['unk2count']*0xC
unk3s = []
for i in range(out['header']['unk3count']):
offset = unk2send+i*0xC
evntdata = zevdat[offset:unk2send+(i+1)*0xC]
# unkindex is either to unk1s or unk2s
unk3 = unpack('name unk1 datatype dataindex datalen', '>4shhhh', evntdata)
unk3['name'] = toStr(unk3['name'])
unk3['offset'] = offset
unk3s.append(unk3)
out['unk3s'] = unk3s
# print(f"unk2len: {len(out['unk2s'])}")
unk3send = unk2send+out['header']['unk3count']*0xC
unk4s = []
out['intsoffset'] = unk3send
for i in range(out['header']['unk4count']):
evntdata = zevdat[unk3send+i*4:unk3send+(i+1)*4]
unk4 = struct.unpack('>I', evntdata)[0]
unk4s.append(unk4)
out['ints'] = unk4s
unk4send = unk3send+out['header']['unk4count']*4
out['floatsoffset'] = unk4send
unk5s = []
for i in range(out['header']['unk5count']):
evntdata = zevdat[unk4send+i*4:unk4send+(i+1)*4]
unk5 = struct.unpack('>f', evntdata)[0]
unk5s.append(unk5)
out['floats'] = unk5s
unk5send = unk4send+out['header']['unk5count']*4
out['stringssoffset'] = unk5send
out['stringbytes'] = zevdat[unk5send:]
out['strings'] = str(zevdat[unk5send:])
for cmd in out['unk3s']:
if cmd['datatype'] == 0:
typename = 'ints'
elif cmd['datatype'] == 1:
typename = 'floats'
elif cmd['datatype'] == 2:
typename = 'stringbytes'
else:
raise Exception(f'unk datatype {cmd["datatype"]}')
dataindex = cmd['dataindex']
datalen = cmd['datalen']
cmd['data'] = out[typename][dataindex:dataindex+datalen]
if typename == 'stringbytes':
cmd['data'] = toStr(cmd['data'])
for event in out['evnts']:
start = event['actorindex']
length = event['actorcount']
event['actors'] = out['actors'][start:start+length]
for actor in out['actors']:
start = actor['actindex']
length = actor['actcount']
actor['acts1'] = out['unk1s'][start:start+length]
actor['acts2'] = out['unk2s'][start:start+length]
for unk2 in out['unk2s']:
start = unk2['unk3Index']
length = unk2['unk3Count']
unk2['data'] = out['unk3s'][start:start+length]
# all the data *should* be in events somehow, so delete the individual things
del out['actors']
del out['unk1s']
del out['unk2s']
del out['unk3s']
del out['floats']
del out['strings']
del out['stringbytes']
del out['ints']
return out
def process_all():
outdir = Path('output') / 'zev'
outdir.mkdir(exist_ok=True)
for i, zevfile in enumerate(Path('allzev').glob('*_zev.dat')):
# if i > 5:
# break
parsed = parse_zevdat(zevfile.read_bytes())
name = zevfile.parts[-1][:-8]
# for event in parsed["evnts"]:
# eventname = event["name"]
# proc = subprocess.Popen(["dot", "-Tpng", "-o", f"graphviztest/{eventname}.png"], stdin=subprocess.PIPE)
# proc.communicate(input=event_to_graphviz(event).encode("ASCII"))
with (outdir / f'{name}.json').open('w') as f:
f.write(objToJson(parsed))
def get_event(filename, evnt_name):
parsed = parse_zevdat((Path('allzev') / f"{filename}_zev.dat").read_bytes())
return next((x for x in parsed['evnts'] if x['name'] == evnt_name), None)
def event_to_graphviz(event) -> str:
out = "digraph {\n"
out += f'label="{event["name"]}";\n'
# draw each actor
for actoridx, actor in enumerate(event['actors']):
out += f'subgraph cluster_{actoridx} {{\n'
out += f'label="{actor["name"]}";\n'
# draw each action of the actor
for i in range(len(actor['acts1'])):
unk1 = actor['acts1'][i]
actionidx = unk1['thisindex']
out += f'action{actionidx} [label="{unk1["name"]}"];\n'
# line from previous
if i == 0:
pass
else:
out += f"action{actionidx-1} -> action{actionidx};\n"
out += "}\n"
for actor in event['actors']:
# draw each action of the actor
for i in range(len(actor['acts1'])):
unk1 = actor['acts1'][i]
actionidx = unk1['thisindex']
# if this waits
waitfor = unk1['waitfor']
if waitfor != -1:
out += f"action{waitfor} -> action{actionidx} [label=wait];\n"
return out + '\n}'
if __name__ == '__main__':
if len(sys.argv) == 1:
process_all()
elif len(sys.argv) == 3:
evntfile = sys.argv[1]
evntname = sys.argv[2]
evnt = get_event(evntfile, evntname)
if evnt is None:
print(f"cannot fine event '{evntname}'", file=sys.stderr)
exit(1)
print(event_to_graphviz(evnt))
else:
usage = "python3 zevdat.py filename eventname\nExample: python3 zevdat.py F300 SalbageFayCall2"
print(usage, file=sys.stderr)
# evnt = get_event("F300", "SalbageFayCall2")
# print(event_to_graphviz(evnt))
# get_event(parse_zevdat((Path('allzev') / 'F000_zev.dat').read_bytes()), "LINK_YOKAN")
# asdf = parse_zevdat((Path('allzev') / 'Common_zev.dat').read_bytes())
# asdf = parse_zevdat((Path('allzev') / 'Asura_zev.dat').read_bytes())