-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathdump_effect_sbin.py
More file actions
444 lines (402 loc) · 15.8 KB
/
dump_effect_sbin.py
File metadata and controls
444 lines (402 loc) · 15.8 KB
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
import collections.abc
import dataclasses
import os
import re
import struct
import subprocess
import tempfile
import typing
ROM_VADDR = 0x08000000
ROM_SIZE = 0x2000000
class FromStruct:
def __init_subclass__(cls, /, spec):
cls._struct = struct.Struct(spec)
@classmethod
def from_io(cls, file: typing.BinaryIO):
return cls(*cls._struct.unpack(file.read(cls._struct.size)))
@classmethod
def iter_io(cls, file: typing.BinaryIO, max_size: int):
for tup in cls._struct.iter_unpack(file.read(max_size)):
yield cls(*tup)
@dataclasses.dataclass
class SiroHeader(FromStruct, spec="<4sLLL"):
magic: bytes
data_p: int
unk8: int
unkC: int
@dataclasses.dataclass
class EfbFileData(FromStruct, spec="<lLlLL"):
frameCount: int
frames: int
tileCount: int
tiles: int
pal: int
@dataclasses.dataclass
class ax_pose(FromStruct, spec="<hBbHHH"):
sprite: int
unk2_unk0: int
unk2_unk1: int
flags1: int
flags2: int
flags3: int
@dataclasses.dataclass
class ax_anim(FromStruct, spec="<BBhhhhh"):
frames: int
unkFlags: int
poseId: int
offset_x: int
offset_y: int
shadow_x: int
shadow_y: int
@dataclasses.dataclass
class ax_sprite(FromStruct, spec="<Ll"):
gfx: int
byteCount: int
PositionSets = collections.abc.Sequence[int]
@dataclasses.dataclass
class EfoFileData(FromStruct, spec="<LLLLLLLL"):
poses: int
animations: int
animCount: int
spriteData: int
positions: int
charData: int
plttData: int
charCount: int
T = typing.TypeVar("T", bound=FromStruct)
def get_siro_data(
baserom: typing.BinaryIO, offset: int, typ: type[T]
) -> tuple[SiroHeader, T]:
baserom.seek(offset - ROM_VADDR)
siro_header = SiroHeader.from_io(baserom)
assert siro_header.magic in {b"SIRO", b"SIR0"}, siro_header.magic
baserom.seek(siro_header.data_p - ROM_VADDR)
effect_header = typ.from_io(baserom)
return siro_header, effect_header
def dump_efbg(
baserom: typing.BinaryIO,
offset: int,
outfile: typing.TextIO,
dir: str,
prefix: str,
):
siro_header, effect_header = get_siro_data(baserom, offset, EfbFileData)
addrs = {
offset: siro_header,
siro_header.data_p: effect_header,
effect_header.frames: [],
effect_header.pal: None,
effect_header.tiles: None,
}
addrs.pop(0, None)
if effect_header.frameCount != 0 and effect_header.frames != 0:
baserom.seek(effect_header.frames - ROM_VADDR)
frame_ptrs = [
int.from_bytes(baserom.read(4), "little")
for _ in range(effect_header.frameCount)
]
addrs[effect_header.frames] = frame_ptrs
for i, ptr in sorted(enumerate(frame_ptrs), key=lambda t: t[1], reverse=True):
if ptr == 0:
continue
array_end = next(x for x in sorted(addrs) if x > ptr)
baserom.seek(ptr - ROM_VADDR)
values = [int.from_bytes(baserom.read(2), "little") for _ in range(10)]
binfilename = f"{dir}/{prefix}_{i:03d}.bin"
with open(binfilename, "wb") as ofp:
ofp.write(baserom.read(array_end - ptr - 20))
values.append(binfilename)
addrs[ptr] = values
if effect_header.tileCount != 0 and effect_header.tiles != 0:
baserom.seek(effect_header.tiles - ROM_VADDR)
binfilename = f"{dir}/{prefix}.4bpp"
with open(binfilename, "wb") as ofp:
ofp.write(baserom.read(32 * (effect_header.tileCount + 1)))
addrs[effect_header.tiles] = binfilename
if effect_header.pal != 0:
baserom.seek(effect_header.pal - ROM_VADDR)
binfilename = f"{dir}/{prefix}.pmdpal"
with open(binfilename, "wb") as ofp:
ofp.write(baserom.read(0x400))
addrs[effect_header.pal] = binfilename
print('#include "global.h"', file=outfile)
print('#include "decompress_sir.h"', file=outfile)
print('#include "structs/axdata.h"', file=outfile)
print(
f"const struct EfbFileData gUnknown_{siro_header.data_p:X};",
file=outfile,
)
for offset, value in sorted(addrs.items()):
label = f"gUnknown_{offset:X}"
if isinstance(value, SiroHeader):
print(
f'const SiroArchive {label} = {{ "{value.magic.decode()}", &gUnknown_{value.data_p:X} }};',
file=outfile,
)
elif isinstance(value, EfbFileData):
print(f"const struct EfbFileData {label} = {{", file=outfile)
# print(f" {value.unk0},", file=outfile)
print(
f" ARRAY_COUNT(gUnknown_{value.frames:X}), // {value.frameCount}",
file=outfile,
)
print(f" gUnknown_{value.frames:X},", file=outfile)
print(
f" sizeof(gUnknown_{value.tiles:X}) / 32 - 1, // {value.tileCount}",
file=outfile,
)
print(f" gUnknown_{value.tiles:X},", file=outfile)
print(f" gUnknown_{value.pal:X},", file=outfile)
print("};", file=outfile)
elif offset == effect_header.frames:
print(f"const u16 *const {label}[] = {{", file=outfile)
for ptr in value:
print(f" gUnknown_{ptr:X},", file=outfile)
print("};", file=outfile)
elif isinstance(value, list):
print(f"const u16 {label}[] = {{", file=outfile)
print(f' {", ".join(str(x) for x in value[:10])},', file=outfile)
print("};", file=outfile)
print(
f'const u16 {label}_tilemap[] = INCBIN_U16("{value[10]}");',
file=outfile,
)
elif isinstance(value, str):
if offset == effect_header.tiles:
incbin = "INCBIN_U32"
typ = "const u32"
elif offset == effect_header.pal:
incbin = "INCBIN_U8"
typ = "const RGB"
else:
raise ValueError("unrecognized data type")
print(f'{typ} {label}[] = {incbin}("{value}");', file=outfile)
else:
raise ValueError("unrecognized data type")
def dump_efob(
baserom: typing.BinaryIO, offset: int, outfile: typing.TextIO, dir: str, prefix: str
):
siro_header, efo_file_data = get_siro_data(baserom, offset, EfoFileData)
addrs = {
offset: siro_header,
siro_header.data_p: efo_file_data,
efo_file_data.poses: [],
efo_file_data.animations: [],
efo_file_data.spriteData: [],
efo_file_data.positions: [],
efo_file_data.charData: [],
efo_file_data.plttData: [],
}
addrs.pop(0, None)
# Pose pointers are of arbitrary length
# Pose arrays are of arbitrary length
# Neither length is stored with the data
# So instead I assume the pointers are in
# the same order as the arrays, and that
# they never overlap.
# Animations always follow the poses.
# Animations always consist of 8 cels
# Strategy: work back to front
if efo_file_data.animations != 0 and efo_file_data.animCount != 0:
all_anims = set()
for i in range(efo_file_data.animCount):
baserom.seek(efo_file_data.animations + 4 * i - ROM_VADDR)
anim_ptr = int.from_bytes(baserom.read(4), "little")
if anim_ptr == 0:
continue
addrs[efo_file_data.animations].append(anim_ptr)
baserom.seek(anim_ptr - ROM_VADDR)
anim_block = [int.from_bytes(baserom.read(4), "little") for _ in range(8)]
addrs[anim_ptr] = anim_block
all_anims |= set(anim_block)
for ptr in sorted(all_anims, reverse=True):
if ptr == 0:
break
if ptr in addrs:
continue
baserom.seek(ptr - ROM_VADDR)
array_end = next(x for x in sorted(addrs) if x > ptr)
addrs[ptr] = list(ax_anim.iter_io(baserom, array_end - ptr))
if efo_file_data.poses != 0:
baserom.seek(efo_file_data.poses - ROM_VADDR)
array_end = next(x for x in sorted(addrs) if x > efo_file_data.poses)
pose_ptrs = [
int.from_bytes(baserom.read(4), "little")
for _ in range(efo_file_data.poses, array_end, 4)
]
addrs[efo_file_data.poses] = pose_ptrs
for ptr in sorted(pose_ptrs, reverse=True):
if ptr == 0:
break
if ptr in addrs:
continue
baserom.seek(ptr - ROM_VADDR)
array_end = next(x for x in sorted(addrs) if x > ptr)
extra_ptr = None
if (array_end - ptr) % ax_pose._struct.size:
extra_ptr = array_end - 4
array_end -= (array_end - ptr) % ax_pose._struct.size
addrs[ptr] = list(ax_pose.iter_io(baserom, array_end - ptr))
if extra_ptr:
baserom.seek(extra_ptr - ROM_VADDR)
addrs[ptr].append(int.from_bytes(baserom.read(4), "little"))
if efo_file_data.spriteData != 0:
raise ValueError(f"{prefix} spriteData unexpectedly not null")
if efo_file_data.positions != 0:
raise ValueError(f"{prefix} positions unexpectedly not null")
if efo_file_data.charData != 0:
baserom.seek(efo_file_data.charData - ROM_VADDR)
binfilename = f"{dir}/{prefix}.4bpp"
with open(binfilename, "wb") as ofp:
ofp.write(baserom.read(32 * efo_file_data.charCount))
addrs[efo_file_data.charData] = binfilename
if efo_file_data.plttData != 0:
baserom.seek(efo_file_data.plttData - ROM_VADDR)
binfilename = f"{dir}/{prefix}.pmdpal"
with open(binfilename, "wb") as ofp:
ofp.write(baserom.read(64))
addrs[efo_file_data.plttData] = binfilename
print('#include "global.h"', file=outfile)
print('#include "decompress_sir.h"', file=outfile)
print('#include "structs/axdata.h"', file=outfile)
print(
f"const struct EfoFileData gUnknown_{siro_header.data_p:X};",
file=outfile,
)
for offset, value in sorted(addrs.items()):
label = f"gUnknown_{offset:X}"
if isinstance(value, SiroHeader):
print(
f'const SiroArchive {label} = {{ "{value.magic.decode()}", &gUnknown_{value.data_p:X} }};',
file=outfile,
)
elif isinstance(value, EfoFileData):
print(f"const EfoFileData {label} = {{", file=outfile)
print(f" gUnknown_{value.poses:X},", file=outfile)
print(f" gUnknown_{value.animations:X},", file=outfile)
print(
f" ARRAY_COUNT(gUnknown_{value.animations:X}), // {value.animCount}",
file=outfile,
)
print(f" NULL,", file=outfile)
print(f" NULL,", file=outfile)
print(f" gUnknown_{value.charData:X},", file=outfile)
print(f" gUnknown_{value.plttData:X},", file=outfile)
print(
f" sizeof(gUnknown_{value.charData:X}) / 32, // {value.charCount}",
file=outfile,
)
print("};", file=outfile)
elif isinstance(value, list) and value:
if isinstance(value[0], ax_pose):
print(f"const ax_pose {label}[] = {{", file=outfile)
for pose in value:
if isinstance(pose, int):
break
print(
f" {{ {pose.sprite}, {{ {pose.unk2_unk0}, {pose.unk2_unk1} }}, {pose.flags1}, {pose.flags2}, {pose.flags3} }},",
file=outfile,
)
print("};", file=outfile)
if isinstance(value[-1], int):
padding_addr = offset + ax_pose._struct.size * (len(value) - 1)
if padding_addr & 3:
padding_addr = (padding_addr + 3) & ~3
print(
f"UNUSED const u32 gUnknown_{padding_addr:X} = {value[-1]};",
file=outfile,
)
elif isinstance(value[0], ax_anim):
print(f"const ax_anim {label}[] = {{", file=outfile)
for anim in value:
print(
f" {{ {anim.frames}, {anim.unkFlags}, {anim.poseId}, {{ {anim.offset_x}, {anim.offset_y} }}, {{ {anim.shadow_x}, {anim.shadow_y} }} }},",
file=outfile,
)
print("};", file=outfile)
elif isinstance(value[0], int) and value[0] in addrs:
redirect = addrs[value[0]]
if isinstance(redirect[0], ax_pose):
typ = "const ax_pose *const"
elif isinstance(redirect[0], ax_anim):
typ = "const ax_anim *const"
elif isinstance(redirect[0], int):
assert (
redirect[0] in addrs
and isinstance(addrs[redirect[0]], list)
and isinstance(addrs[redirect[0]][0], ax_anim)
)
typ = "const ax_anim *const *const"
else:
raise ValueError("unrecognized data type")
print(f"{typ} {label}[] = {{", file=outfile)
for x in value:
print(f" gUnknown_{x:X},", file=outfile)
print("};", file=outfile)
else:
raise ValueError("unrecognized data type")
elif isinstance(value, str):
if offset == efo_file_data.charData:
typ = "const u32"
incbin = "INCBIN_U32"
elif offset == efo_file_data.plttData:
typ = "const RGB"
incbin = "INCBIN_U8"
else:
raise ValueError("unrecognized data type")
print(f'{typ} {label}[] = {incbin}("{value}");', file=outfile)
else:
raise ValueError("unrecognized data type")
def dump_effect_sbin(
baserom: typing.BinaryIO,
offset: int,
dir: str,
prefix: str,
):
os.makedirs(dir, exist_ok=True)
os.makedirs(f"src/{dir}", exist_ok=True)
try:
if prefix.startswith("efbg"):
with open(f"src/{dir}/{prefix}.c", "w") as outfile:
dump_efbg(baserom, offset, outfile, dir, prefix)
elif prefix.startswith("efob"):
with open(f"src/{dir}/{prefix}.c", "w") as outfile:
dump_efob(baserom, offset, outfile, dir, prefix)
print(f" src/data/effects/{prefix}.o(.rodata);")
except Exception as e:
raise ValueError(f"failed to process {prefix}") from e
subprocess.check_call(
[
"tools/gbagfx/gbagfx",
f"{dir}/{prefix}.pmdpal",
f"{dir}/{prefix}.pal",
]
)
with tempfile.NamedTemporaryFile(suffix=".gbapal") as gbapal:
subprocess.check_call(
["tools/gbagfx/gbagfx", f"{dir}/{prefix}.pal", gbapal.name]
)
png_args = [
"tools/gbagfx/gbagfx",
f"{dir}/{prefix}.4bpp",
f"{dir}/{prefix}.png",
"-palette",
gbapal.name,
]
if prefix.startswith("efob"):
png_args.append("-object")
else:
assert gbapal.truncate(32) == 32
subprocess.check_call(png_args)
def main():
pat = re.compile(r'\[\d+\] = \{ "(\w+)", &gUnknown_([0-9A-F]{7}), \},')
addrs = {}
with open("baserom.gba", "rb") as baserom, open("src/effect_files_table.c") as fp:
for m in pat.finditer(fp.read()):
prefix, offset = m.groups()
offset = int(offset, 16)
assert ROM_VADDR <= offset < ROM_VADDR + ROM_SIZE
dump_effect_sbin(baserom, offset, "data/effects", prefix)
addrs[offset] = f"data/effects/{prefix}.h"
if __name__ == "__main__":
main()