-
Notifications
You must be signed in to change notification settings - Fork 4
/
dump_metatiles.py
48 lines (45 loc) · 1.45 KB
/
dump_metatiles.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
import os
if __name__ == '__main__':
walk = list(os.walk('data/tilesets'))
rom = bytearray(open('baserom.gba', 'rb').read())
lines = []
for line in open('data/tilesets/metatiles.s'):
if 'gMetatiles' in line:
name = line.split(':')[0].split('gMetatiles_')[1]
attributes = False
elif 'gMetatileAttributes' in line:
name = line.split(':')[0].split('gMetatileAttributes_')[1]
attributes = True
if '.incbin' in line:
start, length = map(lambda x: int(x, 0), line.split(',')[1:3])
data = rom[start:start+length]
filename = None
if name.endswith('Primary'):
primary = 'primary'
elif name.endswith('Secondary'):
primary = 'secondary'
else:
primary = None
name = name.replace('Secondary', '')
name = name.replace('Primary', '')
old_name = name
name = ''
for i, letter in enumerate(old_name):
if letter.isupper() and i and not old_name[i-1].isupper():
name += '_'
if letter.isdigit() and i and not old_name[i-1].isdigit():
name += '_'
name += letter.lower()
for root, dirs, files in walk:
if name.lower() in dirs:
if (not primary) or primary in root:
if attributes:
filename = os.path.join(root, name.lower() + '/metatile_attributes.bin')
else:
filename = os.path.join(root, name.lower() + '/metatiles.bin')
break
if filename:
line = '\t.incbin "{}"\n'.format(filename)
open(filename, 'wb').write(data)
lines += [line]
print ''.join(lines)