summaryrefslogtreecommitdiff
path: root/tools/mapfile_tool.py
blob: 92e53ad2b21c13d55932635c4bd392864787689a (plain)
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
# half assed

# d_profile: same place
# d_bases: same place
# d_enemies and part of d_bases: offset by 0x20
# d_en_boss: offset by 0x20

def make_hex_offset(offs):
	return '0x%08X' % offs

def fix_offs_pal_v2(offs):
	# do this later
	return 0xdeadbeef

def fix_offs_ntsc_v1(offs):
	# .text section
	if offs >= 0x800B4604 and offs <= 0x800C8E4C:
		return offs - 0x50

	if offs >= 0x800C8E50 and offs <= 0x800E4D70:
		return offs - 0xF0

	if offs >= 0x800E4EC0 and offs <= 0x8010F200:
		return offs - 0x110

	if offs >= 0x8010F430 and offs <= 0x802BB6BC:
		return offs - 0x140

	if offs >= 0x802BB6C0 and offs <= 0x802BB74C:
		return offs - 0x150

	if offs >= 0x802BB860 and offs <= 0x802BBBFC:
		return offs - 0x260

	if offs >= 0x802BBC90 and offs <= 0x802EDCC0:
		return offs - 0x2F0

	# .ctors, .dtors, .rodata, part of .data section
	if offs >= 0x802EDCE0 and offs <= 0x80317734:
		return offs - 0x300

	# .data section
	if offs >= 0x80317750 and offs <= 0x80322FE0:
		return offs - 0x318

	if offs >= 0x80323118 and offs <= 0x8032E77C:
		return offs - 0x348

	if offs >= 0x8032E780 and offs <= 0x8035197C:
		return offs - 0x340

	# .sdata section, part of .sbss
	if offs >= 0x80351980 and offs <= 0x80427E87:
		return offs - 0x300

	# .sbss, .sdata2, .sbss2 sections
	if offs >= 0x80427E88 and offs <= 0x80429563:
		return offs - 0x310

	if offs >= 0x80429564 and offs <= 0x80429D7F:
		return offs - 0x2F8

	if offs >= 0x80429D80 and offs <= 0x807684BF:
		return offs - 0x2E0

	# part of d_basesNP, d_enemiesNP, d_en_bossNP
	if offs >= 0x8098A43C:
		return offs + 0x20

	return offs


def fix_offs_ntsc_v2(offs):
	offs = fix_offs_ntsc_v1(offs)

	if offs >= 0x807685A0 and offs <= 0x807AAA70:
		return offs + 0x40

	if offs >= 0x807AAA74 and offs <= 0x8099081C:
		return offs + 0x10

	if offs >= 0x80990820:
		return offs + 0x20

	return offs


def fix_offs_jpn_v1(offs):
	# .text section
	if offs >= 0x800B4604 and offs <= 0x800B475C:
		return offs - 0x50

	if offs >= 0x800B4760 and offs <= 0x800C8DAC:
		return offs - 0xD0

	if offs >= 0x800C8E50 and offs <= 0x800E4D6C:
		return offs - 0x170

	if offs >= 0x800E4D94 and offs <= 0x800E4EB4:
		return offs - 0x194

	if offs >= 0x800E4EB8 and offs <= 0x8010F1D0:
		return offs - 0x190

	if offs >= 0x8010F430 and offs <= 0x802BB6BC:
		return offs - 0x330

	if offs >= 0x802BB6D0 and offs <= 0x802BB74C:
		return offs - 0x340

	if offs >= 0x802BB860 and offs <= 0x802BBBFC:
		return offs - 0x450

	if offs >= 0x802BBC90 and offs <= 0x802EDCC0:
		return offs - 0x4E0

	# .ctors, .dtors, .rodata, part of .data section
	if offs >= 0x802EDCE0 and offs <= 0x80317734:
		return offs - 0x4E0

	# .data section
	if offs >= 0x80317750 and offs <= 0x80322FDC:
		return offs - 0x4F8

	if offs >= 0x80323118 and offs <= 0x8035197C:
		return offs - 0x5E0

	# .sdata, part of .sbss section
	if offs >= 0x80351980 and offs <= 0x80427E5F:
		return offs - 0x580

	if offs >= 0x80427E88 and offs <= 0x8042954B:
		return offs - 0x5A8

	if offs >= 0x80429570 and offs <= 0x80429D7F:
		return offs - 0x5C8

	# part of .sdata2, .sbss2 section
	if offs >= 0x80429D80 and offs <= 0x807684BF: # end offset is right before d_profileNP header
		return offs - 0x5C0

	# d_profileNP and d_basesNP
	# "no change" gap ends at 8779ABC
	if offs >= 0x80779C70 and offs <= 0x8078891F:
		return offs - 0x130

	if offs >= 0x80788AD0 and offs <= 0x80789EEF:
		return offs - 0x260

	if offs >= 0x80789F00 and offs <= 0x808D3B87:
		return offs - 0x270

	if offs >= 0x808D3BD4 and offs <= 0x808D3C1F:
		return offs - 0x2B4

	if offs >= 0x808D3C20 and offs <= 0x80940C47:
		return offs - 0x2C0

	if offs >= 0x80940F58 and offs <= 0x80943167:
		return offs - 0x4E8

	if offs >= 0x809431F8 and offs <= 0x8094329F:
		return offs - 0x4F8

	if offs >= 0x809432C0 and offs <= 0x80944E77:
		return offs - 0x500

	if offs >= 0x80945144 and offs <= 0x80945153:
		return offs - 0x714

	if offs >= 0x80945158 and offs <= 0x8098A36B:
		return offs - 0x718

	if offs >= 0x8098A478 and offs <= 0x8098F81F:
		return offs - 0x6F8

	# d_enemiesNP
	# this offset starts at the .rel header
	if offs >= 0x809A2CA0 and offs <= 0xDEADBEEF:
		return offs - 0x700

	if offs >= 0x8010F430 and offs <= 0xDEADBEEF:
		return offs - 0x330

	if offs >= 0x8010F430 and offs <= 0xDEADBEEF:
		return offs - 0x330

	return offs


import yaml

original = 'pal'
fix_for = {
	'pal2': fix_offs_ntsc_v1,
	'ntsc': fix_offs_ntsc_v1,
	'ntsc2': fix_offs_ntsc_v2,
	'jpn': fix_offs_jpn_v1
}

def do_mapfile(src, dest, fix_offset):
	map = [x.rstrip() for x in open(src, 'r')]
	new = []

	for line in map:
		if ' = 0x80' in line:
			pos = line.find('0x80')
			offs = int(line[pos:pos+10], 0)
			offs = fix_offset(offs)
			line = line[:pos] + make_hex_offset(offs) + line[pos+10:]

		new.append(line + '\n')

	open(dest, 'w').writelines(new)


def work_on_hook(hook, id, func):
	#print 'doing %s (%s) for %s' % (hook['name'], hook['type'], id)

	t = hook['type']

	if t == 'patch':
		hook['addr_%s' % id] = func(hook['addr_%s' % original])

	elif t == 'branch_insn' or t == 'add_func_pointer':
		hook['src_addr_%s' % id] = func(hook['src_addr_%s' % original])

		if 'target_func_%s' % original in hook:
			hook['target_func_%s' % id] = func(hook['target_func_%s' % original])

	elif t == 'nop_insn':
		area = hook['area_%s' % original]
		if isinstance(area, list):
			start = func(area[0])
			new_area = [start, start + (area[1] - area[0])]
		else:
			new_area = func(area)

		hook['area_%s' % id] = new_area

	#if hook['name'] == 'WM_onCreate': print hook


def do_module(src, dest):
	m = yaml.safe_load(open(src, 'r').read())

	if 'hooks' not in m:
		return

	for id, func in fix_for.iteritems():
		for hook in m['hooks']:
			work_on_hook(hook, id, func)

	open(dest, 'w').write(yaml.dump(m))


def do_project(f, already_done):
	proj = yaml.safe_load(open(f, 'r').read())

	for m in proj['modules']:
		if m not in already_done:
			already_done.add(m)
			do_module(m.replace('processed/', ''), m)


def main():
	do_mapfile('kamek_pal.x', 'kamek_pal2.x', fix_offs_pal_v2)
	do_mapfile('kamek_pal.x', 'kamek_ntsc.x', fix_offs_ntsc_v1)
	do_mapfile('kamek_pal.x', 'kamek_ntsc2.x', fix_offs_ntsc_v2)
	do_mapfile('kamek_pal.x', 'kamek_jpn.x', fix_offs_jpn_v1)

	already_done = set()
	do_project('NewerProject.yaml', already_done)
	do_project('NewerProjectKP.yaml', already_done)
	do_project('SummerSun.yaml', already_done)

	do_module('anotherhax.yaml', 'processed/anotherhax.yaml')

if __name__ == '__main__':
	main()