-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdumptext2.py
599 lines (583 loc) · 17.2 KB
/
dumptext2.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/env python3
import sys
import os, os.path
from io import BytesIO
import struct
import re
lament = lambda *args, **kwargs: print(*args, file=sys.stderr, **kwargs)
unpack = lambda fmt, data: struct.unpack(fmt, bytes(data))
extended='°ÀÁÂÄÇÈÉÊËÌÍÎÏÑÒÓÔÖÙÚÛÜßàáâäçèéêëìíîïñòóôöùúûü¡ª¿'
def parse_jp_text(f):
s = ''
bs = bytearray()
def arg2():
return '{:04X}'.format(unpack('>H', f.read(2))[0]).encode('shift-jis')
lastx = 0
special = f.read(12) # TODO
while 1:
b1 = f.read(1)
if not b1:
break
b2 = f.read(1)
if not b2:
break
x1 = ord(b1)
x2 = ord(b2)
x = x1*0x100 + x2
odd = x1 % 2
xs = bytes('{:04X}'.format(x), 'shift-jis')
# shift-jis
shifty1 = (0x81 <= x1 <= 0x9F and x1 != 0x85 and x1 != 0x86) or 0xE0 <= x1 <= 0xEF
shifty2e = 0x9F <= x2 <= 0xFC
shifty2o = 0x40 <= x2 <= 0x9E
try:
(b1 + b2).decode('shift-jis')
shifty = True
except:
shifty = False
if x == 0x0020:
bs += b' ' # doesn't seem to be fullwidth?
elif x == 0x0500:
# end mark
break
elif x == 0x0009:
# if verbose:
# bs += b'[pause??]'
bs += b'\n\n'
elif x == 0x000A:
bs += b'\n'
elif x == 0x000B:
# if verbose:
# bs += b'[pause?]'
bs += b'\n\n'
elif x == 0x000C:
if verbose:
bs += b'[pause]'
bs += b'\n\n'
elif x == 0x001F:
if verbose:
bs += b'[spaces '+arg2()+b']'
else:
bs += b' '*int(arg2(), 16)
elif x == 0x0100:
bs += b'[Link]'
elif x == 0x0101:
if verbose:
bs += b'[instant on]'
elif x == 0x0102:
if verbose:
bs += b'[instant off]'
elif x == 0x0103:
if verbose:
bs += b'[no skip sound?]'
elif x == 0x0104:
if verbose:
bs += b'[keepalive]'
elif x == 0x0110:
if verbose:
bs += b'[next wait '+arg2()+b']'
else:
arg2()
bs += b'\n\n'
elif x == 0x0111: # 0x1C
if verbose:
bs += b'[end wait '+arg2()+b']'
else:
arg2()
elif x == 0x0112: # 0x1D
if verbose:
bs += b'[end wait alt 0112 '+arg2()+b']'
else:
arg2()
elif x == 0x0120:
bs += b'[sound '+arg2()+b']'
elif x == 0x0128:
if verbose:
bs += b'[wait '+arg2()+b']'
else:
arg2()
elif x == 0x0135: # unique to JP?
bs += b'[unk 0135]'
elif x == 0x0201:
bs += b'[failed song]'
elif x == 0x0202:
if verbose:
bs += b'[two-choice]'
elif x == 0x0203:
if verbose:
bs += b'[three-choice]'
elif x == 0x0204:
bs += b'[postman timer]'
elif x == 0x0208:
bs += b'[deku score?]'
elif x == 0x020B:
bs += b'[highscore? 020B]'
elif x == 0x020C:
bs += b'[rupee prompt]'
elif x == 0x020D:
bs += b'[rupees selected]'
elif x == 0x020E:
bs += b'[rupees]'
elif x == 0x020F:
bs += b'[hours/minutes remaining]'
elif x == 0x021C: # X人目
bs += b'[fairies]'
elif x == 0x021D: # X匹
bs += b'[gold skulltulas]'
elif x == 0x021E:
bs += b'[score? 021E]'
elif x == 0x021F:
bs += b'[score? 021F]'
elif x == 0x0220:
bs += b'[doggy prompt]'
elif x == 0x0221:
bs += b'[bombers code prompt]'
elif x == 0x0222:
bs += b'[item prompt]'
elif x == 0x0224:
bs += b'[soar destination]'
elif x == 0x0225:
bs += b'[lottery prompt]'
elif x == 0x0227:
bs += b'[fairies remaining]'
elif x == 0x0228:
bs += b'[fairies remaining]'
elif x == 0x0229:
bs += b'[fairies remaining]'
elif x == 0x022A:
bs += b'[fairies remaining]'
elif x == 0x022B:
bs += b'[witch archery]'
elif x == 0x022C:
bs += b'[winning numbers]'
elif x == 0x022D:
bs += b'[ticket numbers]'
elif x == 0x022E:
bs += b'[item worth]'
elif x == 0x022F:
bs += b'[bombers code]'
elif x == 0x0230:
if verbose:
bs += b'[end convo]'
elif x == 0x0231:
bs += b'[skull color]'
elif x == 0x0232:
bs += b'[skull color]'
elif x == 0x0233:
bs += b'[skull color]'
elif x == 0x0234:
bs += b'[skull color]'
elif x == 0x0235:
bs += b'[skull color]'
elif x == 0x0236:
bs += b'[skull color]'
elif x == 0x0237:
bs += b'[hours remaining]'
elif x == 0x0238:
bs += b'[time until morning]'
elif x == 0x0240:
if verbose:
bs += b'[no skip?]'
elif x == 0x0306:
bs += b'[octorok archery highscore]'
elif x == 0x0307:
bs += b'[epona highscore?]'
elif x == 0x0308:
bs += b'[highscore? 0308]'
elif x == 0x0309:
bs += b'[epona highscore]'
elif x == 0x030A:
bs += b'[deku highscore]'
elif x == 0x030B:
bs += b'[deku highscore]'
elif x == 0x030C:
bs += b'[deku highscore]'
elif x == 0x030D:
bs += b'[unused name 1]'
elif x == 0x030E:
bs += b'[unused name 2]'
elif x == 0x030F:
bs += b'[unused name 3]'
elif x == 0x0310:
bs += b'[highscore? 0310]'
elif x == 0x037E:
bs += b'[unk 037E]'
elif x1 == 0x20: # 0x00-0x08
if x2 == 0x00:
if verbose:
bs += b'[white]'
elif x2 == 0x01:
if verbose:
bs += b'[red]'
elif x2 == 0x02:
if verbose:
bs += b'[green]'
elif x2 == 0x03:
if verbose:
bs += b'[dark blue]'
elif x2 == 0x04:
if verbose:
bs += b'[yellow]'
elif x2 == 0x05:
if verbose:
bs += b'[light blue]'
elif x2 == 0x06:
if verbose:
bs += b'[pink]'
elif x2 == 0x07:
if verbose:
bs += b'[silver]'
elif x2 == 0x08:
if verbose:
bs += b'[orange]'
else:
raise Exception('unknown color')
elif x == 0x839F:
bs += b'[A]'
elif x == 0x83A0:
bs += b'[B]'
elif x == 0x83A1:
bs += b'[C]'
elif x == 0x83A2:
bs += b'[L]'
elif x == 0x83A3:
bs += b'[R]'
elif x == 0x83A4:
bs += b'[Z]'
elif x == 0x83A5:
bs += b'[C Up]'
elif x == 0x83A6:
bs += b'[C Down]'
elif x == 0x83A7:
bs += b'[C Left]'
elif x == 0x83A8:
bs += b'[C Right]'
elif x == 0x83A9:
bs += b'[Triangle]'
elif x == 0x83AA:
bs += b'[Control Stick]'
elif x == 0x83AB:
bs += b'[DPad]'
elif x == 0x0000:
lament(bs)
lament('{:04X}'.format(lastx))
raise Exception('unexpected 0000')
elif shifty1 and shifty2o:
if not shifty:
lament('CRAP {:02X}{:02X}'.format(x1, x2))
raise Exception('not actually shifty')
bs += b1
bs += b2
elif shifty1 and shifty2e:
if not shifty:
lament('CRAP {:02X}{:02X}'.format(x1, x2))
raise Exception('not actually shifty')
bs += b1
bs += b2
elif shifty:
# last resort...
lament()
lament(bs.decode('shift-jis'))
lament('unknown {:04X}'.format(x))
raise Exception('looks shifty')
bs += b1
bs += b2
else:
lament(bs)
lament('unknown {:02X}{:02X}'.format(x1, x2))
raise Exception('unknown character')
lastx = x
s = bs.decode('shift-jis')
return s
def parse_en_text(f):
s = ''
bs = bytearray()
def arg():
return '{:02X}'.format(ord(f.read(1))).encode('utf-8')
lastx = 0
special = f.read(11) # TODO
while 1:
b1 = f.read(1)
if not b1:
break
x = ord(b1)
if b'\x20' <= b1 <= b'\x7E':
# ascii
if b1 == b'"':
bs += b'""'
else:
bs += b1
continue
if b'\x7F' <= b1 <= b'\xAF':
bs += extended[x - 0x7F].encode('utf-8')
continue
elif x == 0x00:
if verbose:
bs += b'[white]'
elif x == 0x01:
if verbose:
bs += b'[red]'
elif x == 0x02:
if verbose:
bs += b'[green]'
elif x == 0x03:
if verbose:
bs += b'[dark blue]'
elif x == 0x04:
if verbose:
bs += b'[yellow]'
elif x == 0x05:
if verbose:
bs += b'[light blue]'
elif x == 0x06:
if verbose:
bs += b'[pink]'
elif x == 0x07:
if verbose:
bs += b'[silver]'
elif x == 0x08:
if verbose:
bs += b'[orange]'
elif x == 0x0A:
if verbose:
bs += b'[spaces? '+arg()+b']'
else:
arg()
elif x == 0x0B:
bs += b'[record?]'
elif x == 0x0C:
bs += b'[fairies]'
elif x == 0x0D:
bs += b'[gold skulltulas]'
elif x == 0x10:
if verbose:
bs += b'[pause]'
bs += b'\n\n'
elif x == 0x11:
bs += b'\n'
elif x == 0x12:
bs += b'\n\n'
elif x == 0x13:
bs += b'\n'
elif x == 0x15:
if verbose:
bs += b'[no skip?]'
elif x == 0x16:
bs += b'[Link]'
elif x == 0x17:
if verbose:
bs += b'[instant on]'
elif x == 0x18:
if verbose:
bs += b'[instant off]'
elif x == 0x19:
if verbose:
bs += b'[no skip sound?]'
elif x == 0x1A:
if verbose:
bs += b'[keepalive]'
elif x == 0x1B:
if verbose:
bs += b'[next wait '+arg()+arg()+b']'
else:
arg()
arg()
bs += b'\n\n'
elif x == 0x1C:
if verbose:
bs += b'[end wait '+arg()+arg()+b']'
else:
arg()
arg()
elif x == 0x1D:
if verbose:
bs += b'[end wait alt '+arg()+arg()+b']'
else:
arg()
arg()
elif x == 0x1E:
bs += b'[sound '+arg()+arg()+b']'
elif x == 0x1F:
if verbose:
bs += b'[wait '+arg()+arg()+b']'
else:
arg()
arg()
elif x == 0xC2:
if verbose:
bs += b'[two-choice]'
elif x == 0xC3:
if verbose:
bs += b'[three-choice]'
elif x == 0xB0:
bs += b'[A]'
elif x == 0xB1:
bs += b'[B]'
elif x == 0xB2:
bs += b'[C]'
elif x == 0xB3:
bs += b'[L]'
elif x == 0xB4:
bs += b'[R]'
elif x == 0xB5:
bs += b'[Z]'
elif x == 0xB6:
bs += b'[C Up]'
elif x == 0xB7:
bs += b'[C Down]'
elif x == 0xB8:
bs += b'[C Left]'
elif x == 0xB9:
bs += b'[C Right]'
elif x == 0xBA:
bs += b'[Triangle]'
elif x == 0xBB:
bs += b'[Control Stick]'
elif x == 0xBF:
# end marker
break
elif x == 0xC1:
bs += b'[failed song]'
elif x == 0xC4:
bs += b'[postman timer]'
elif x == 0xC8:
bs += b'[deku score]'
elif x == 0xCB:
bs += b'[score]'
elif x == 0xCC:
bs += b'[rupee prompt]'
elif x == 0xCD:
bs += b'[rupees selected]'
elif x == 0xCE:
bs += b'[rupees]'
elif x == 0xCF:
bs += b'[hours remaining CF]'
elif x == 0xD0:
bs += b'[doggy bet]'
elif x == 0xD1:
bs += b'[bombers code prompt]'
elif x == 0xD2:
bs += b'[item prompt]'
elif x == 0xD4:
bs += b'[soar destination]'
elif x == 0xD5:
bs += b'[lottery prompt]'
elif x == 0xD7:
bs += b'[fairies remaining]'
elif x == 0xD8:
bs += b'[fairies remaining]'
elif x == 0xD9:
bs += b'[fairies remaining]'
elif x == 0xDA:
bs += b'[fairies remaining]'
elif x == 0xDB:
bs += b'[witch archery]'
elif x == 0xDC:
bs += b'[winning numbers]'
elif x == 0xDD:
bs += b'[ticket numbers]'
elif x == 0xDE:
bs += b'[item worth]'
elif x == 0xDF:
bs += b'[bombers code]'
elif x == 0xE0:
if verbose:
bs += b'[end convo]'
elif x == 0xE1:
bs += b'[skull color]'
elif x == 0xE2:
bs += b'[skull color]'
elif x == 0xE3:
bs += b'[skull color]'
elif x == 0xE4:
bs += b'[skull color]'
elif x == 0xE5:
bs += b'[skull color]'
elif x == 0xE6:
bs += b'[skull color]'
elif x == 0xE7:
bs += b'[hours remaining E7]'
elif x == 0xE8:
bs += b'[time until morning]'
elif x == 0xFA:
bs += b'[deku highscore]'
elif x == 0xFB:
bs += b'[deku highscore]'
elif x == 0xFC:
bs += b'[deku highscore]'
elif x == 0xF6:
bs += b'[octorok archery highscore]'
elif x == 0xF9:
bs += b'[epona highscore]'
else:
lament(bs)
lament('unknown {:02X}'.format(x))
raise Exception('unknown character')
lastx = x
s = bs.decode('utf-8')
return s
def dump_text(parser, msgtable, msgs):
if not isinstance(msgtable, BytesIO):
msgtable = BytesIO(msgtable)
if not isinstance(msgs, BytesIO):
msgs = BytesIO(msgs)
msgtable_end = msgtable.seek(0, 2)
msgs_end = msgs.seek(0, 2)
msgtable.seek(0)
msgs.seek(0)
lastid = 0
for i in range(msgtable_end//8):
msgid, = unpack('>H', msgtable.read(2))
if msgid >= 0xFFFC:
break
if msgid != lastid + 1:
print('"",""')
xy = msgtable.read(1)
unused = msgtable.read(1)
bank = msgtable.read(1)
offset, = unpack('>L', b'\x00'+msgtable.read(3))
msgs.seek(offset)
text = parser(msgs)
text = re.sub('\n\n+', '\n\n', text)
#print('{:04} {:04X} {:06X}'.format(i, msgid, offset))
print('"${:04X}","{}"'.format(msgid, text))
lastid = msgid
def dumpit(codefile, textfile, language, table_offset, table_size):
if os.path.exists(codefile) and os.path.exists(textfile):
with open(codefile, 'rb') as f:
f.seek(table_offset)
msgtable = f.read(table_size)
with open(textfile, 'rb') as f:
msgs = f.read()
if language == 'jp':
dump_text(parse_jp_text, msgtable, msgs)
elif language == 'en':
dump_text(parse_en_text, msgtable, msgs)
else:
raise Exception('unsupported')
return True
else:
return False
import sys
args = sys.argv[1:]
# MM (U) 1.0 and (J) 1.0
if len(args) > 1 and (args[1].startswith('v') or args[1].startswith('V')):
verbose = True
else:
verbose = False
if args[0] == 'jp':
dirname = 'dump/mm-JP10-5fb2301aacbf85278af30dca3e4194ad48599e36'
codefile = dirname+'/0028 V00B5F000'
jp_start = 0x11A398
jp_end = 0x123128
textfile = dirname+'/0026 V00AF9000'
dumpit(codefile, textfile, 'jp', jp_start, jp_end - jp_start)
elif args[0] == 'en':
dirname = 'dump/mm-US10-d6133ace5afaa0882cf214cf88daba39e266c078'
codefile = dirname+'/0031 V00B3C000'
en_start = 0x1210D8
en_end = 0x12A048
textfile = dirname+'/0029 V00AD1000'
dumpit(codefile, textfile, 'en', en_start, en_end - en_start)
else:
raise Exception('unknown language to dump')