forked from snesrev/zelda3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_compression.py
More file actions
551 lines (485 loc) · 20.9 KB
/
Copy pathtext_compression.py
File metadata and controls
551 lines (485 loc) · 20.9 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
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
import util, sys
kTextAlphabet_US = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"-", ".", ",",
# 64 - 79
"[...]", ">", "(", ")",
"[Ankh]", "[Waves]", "[Snake]", "[LinkL]", "[LinkR]",
"\"", "[Up]", "[Down]", "[Left]",
# 80 - 95
"[Right]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "<", "[A]", "[B]", "[X]", "[Y]",
]
kTextAlphabet_DE = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
# 64 - 79
"-", ".", ",", "[...]", ">", "(", ")",
"[Ankh]", "[Waves]", "[Snake]", "[LinkL]", "[LinkR]",
"\"", "[UpL]", "[UpR]", "[LeftL]",
# 80 - 95
"[LeftR]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "ö", "[A]", "[B]", "[X]", "[Y]", "ü",
# 96-111
"ß", ":", "[DownL]", "[DownR]", "[RightL]", "[RightR]",
"è", "é", "ê", "à", "ù", "ç", "Ä", "Ö", "Ü", "ä"
# 112-
]
kTextAlphabet_FR = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
# 64 - 79
"-", ".", ",", "[...]", ">", "(", ")",
"[Ankh]", "[Waves]", "[Snake]", "[LinkL]", "[LinkR]",
"\"", "[UpL]", "[UpR]", "[LeftL]",
# 80 - 95
"[LeftR]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "ö", "[A]", "[B]", "[X]", "[Y]", "ü",
# 96-111
"ô", ":", "[DownL]", "[DownR]", "[RightL]", "[RightR]",
"è", "é", "ê", "à", "ù", "ç", "â", "û", "î", "ä"
# 112-
]
kText_CommandLengths_US = [1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, ]
kText_CommandNames_US = [
"NextPic", "Choose", "Item", "Name", "Window", "Number",
"Position","ScrollSpd", "Selchg", "Unused_Crash", "Choose3",
"Choose2", "Scroll", "1", "2", "3", "Color",
"Wait", "Sound", "Speed", "Unused_Mark", "Unused_Mark2", "Unused_Clear",
"Waitkey"
]
kTextDictionary_US = [
' ', ' ', ' ', "'s ", 'and ',
'are ', 'all ', 'ain', 'and', 'at ',
'ast', 'an', 'at', 'ble', 'ba',
'be', 'bo', 'can ', 'che', 'com',
'ck', 'des', 'di', 'do', 'en ',
'er ', 'ear', 'ent', 'ed ', 'en',
'er', 'ev', 'for', 'fro', 'give ',
'get', 'go', 'have', 'has', 'her',
'hi', 'ha', 'ight ', 'ing ', 'in',
'is', 'it', 'just', 'know', 'ly ',
'la', 'lo', 'man', 'ma', 'me',
'mu', "n't ", 'non', 'not', 'open',
'ound', 'out ', 'of', 'on', 'or',
'per', 'ple', 'pow', 'pro', 're ',
're', 'some', 'se', 'sh', 'so',
'st', 'ter ', 'thin', 'ter', 'tha',
'the', 'thi', 'to', 'tr', 'up',
'ver', 'with', 'wa', 'we', 'wh',
'wi', 'you', 'Her', 'Tha', 'The',
'Thi', 'You',
]
kText_CommandLengths_EU = [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2]
kText_CommandNames_EU = [
"Selchg", "Choose3", "Choose2", "Scroll", "1", "2", "3",
"Color", "Wait", "Sound", "Speed", "Mark", "Mark2",
"Clear", "Waitkey", "EndMessage", "NextPic", "Choose",
"Item", "Name", "Window", "Number", "Position", "ScrollSpd",
]
kTextDictionary_DE = [
' ', ' ', ' ', '-Knopf', ' ich ',
' Sch', ' Ver', ' zu ', ' es ', 'aber',
'alle', 'auch', 'ang', 'aus', 'auf',
'an', 'bist', 'bin', 'bei', 'der ',
'die ', 'das ', 'den ', 'dem ', 'daß',
'der', 'die', 'das', 'den', 'da',
'etwas', 'ein ', 'ein', 'en ', 'er ',
'es ', 'en', 'er', 'es', 'ei',
'für', 'fe', 'habe', 'hier', 'hast',
'her', 'ich ', 'icht', 'ich', 'ist',
'ie ', 'im', 'ie', 'kannst ', 'kannst',
'kommen', 'kann ', 'll', 'mich', 'mein',
'mit', 'mal', 'mir', 'nicht ', 'nicht',
'nen', 'nn', 'och ', 'och', 'or',
'schon', 'sich', 'sein', 'sch', 'sie',
'st', 'tte', 'te ', 'te', 'und ',
'und', 'ung', 'um', 'von', 'ver',
'vor', 'wird', 'zu ', 'Amulett', 'Aber',
'Deine', 'Dich ', 'Dir ', 'Dir', 'Der',
'Die', 'Das', 'Du ', 'Du', 'Da',
'Ein', 'Hyrule', 'Hier', 'Ich ', 'Master-Schwert',
'Mach', 'Rubine', 'Sch', 'Sie', 'Ver',
'Weisen', 'Zelda',
]
kTextDictionary_FR = [
' ', ' de ', ' la ', ' le ', ' ! ',
' d', ' p', ' t', ' !', ", c'est moi, Sahasrahla",
', ', 'ais ', 'as ', 'an', 'ai',
'a ', 'che', 'ce', 'ch', 'dans ',
'des ', 'de ', 'de', 'est ', 'ent',
'en ', 'er ', 'es ', 'en', 'es',
'et', 'eu', 'e,', 'e ', 'ique',
'ien', 'is ', 'ie', 'in', 'ir',
'is', 'i ', 'les ', 'la ', 'le ',
'le', 'll', 'maintenant', 'magique', 'ment',
'mon', 'mai', 'me', 'ne ', 'onne',
'oir', 'our', 'ouv', 'oi', 'on',
'ou', 'or', 'pouvoir', 'pour', 'peux',
'pas', 'que ', 'qu', 'rubis', 're ',
'ra', 're', 'r ', 'sorcier', 's l',
's d', 'se', 'so', 's ', 'tro',
'te ', 'tu ', 'te', 't ', 'un',
'ur', 'u ', 'ver', 'Ah ! Ah ! Ah !', "C'est",
'Ganon', 'Maintenant', 'Merci', 'Monde', 'Perle de Lune',
'Tu as trouvé ', 'Ténèbres', 'Tu peux', 'Tu ',
]
# Uses the original format
def org_encoder(cmd, param):
if cmd not in kText_CommandNames_US:
raise Exception(f'Invalid cmd {cmd}')
cmd_index = kText_CommandNames_US.index(cmd)
if kText_CommandLengths_US[cmd_index] != (1 if param == None else 2):
raise Exception(f'Invalid cmd params {cmd} {param}')
if param == None:
return [cmd_index + 0x67]
return [cmd_index + 0x67, int(param)]
kCmdInfo = {
"Scroll" : (0x80, ),
"Waitkey" : (0x81, ),
"1" : (0x82, ),
"2" : (0x83, ),
"3" : (0x84, ),
"Name" : (0x85, ),
"Wait" : (0x87, {i:i+0x00 for i in range(16)}),
"Color" : (0x87, {i:i+0x10 for i in range(16)}),
"Number" : (0x87, {i:i+0x20 for i in range(16)}),
"Speed" : (0x87, {i:i+0x30 for i in range(16)}),
"Sound" : (0x87, {45 : 0x40, 64 : None}), # pt uses 64 for some reason
"Choose" : (0x87, 0x80),
"Choose2" : (0x87, 0x81),
"Choose3" : (0x87, 0x82),
"Selchg" : (0x87, 0x83),
"Item" : (0x87, 0x84),
"NextPic" : (0x87, 0x85),
"Window" : (0x87, {0 : None, 2 : 0x86}),
"Position" : (0x87, {0: 0x87, 1: 0x88}),
"ScrollSpd" : (0, {0 : None}),
}
# Uses another format where there's more bytes left for characters
def new_encoder(cmd, param):
if cmd not in kCmdInfo:
raise Exception(f'Invalid cmd {cmd}')
info = kCmdInfo[cmd]
if len(info) <= 1 or isinstance(info[1], int):
if param != None:
raise Exception(f'Invalid cmd params {cmd} {param}')
return info
else:
if param == None or param not in info[1]:
raise Exception(f'Invalid cmd params {cmd} {param}')
r = info[1][param]
return (info[0], r) if r != None else ()
kEncoders = {'org' : org_encoder, 'new' : new_encoder}
class LangUS:
alphabet = kTextAlphabet_US
dictionary = kTextDictionary_US
command_lengths = kText_CommandLengths_US
command_names = kText_CommandNames_US
rom_addrs = [0x9c8000, 0x8edf40]
COMMAND_START = 0x67
SWITCH_BANK = 0x80
FINISH = 0xff
DICT_BASE_ENC, DICT_BASE_DEC = 0x88, 0x88
ESCAPE_CHARACTER = None
encoder = 'org'
class LangEN(LangUS):
alphabet = kTextAlphabet_DE
dictionary = kTextDictionary_US
rom_addrs = [0x9c8000, 0x8edf60]
class LangES(LangUS):
alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "é", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"ó", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"[Waves]", ".", ",",
# 64 - 79
"[...]", ">", "(", ")",
"ñ", "ú", "á", "[LinkL]", "[LinkR]", "\"", "[Up]", "[Down]", "[Left]",
# 80 - 95
"[Right]", "í", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[Ankh]", "[4HeartR]", " ", "[Snake]", "[A]", "[B]", "[X]", "[Y]", "[I]",
"¡", "¿", "Ñ"
]
# "[Ankh]", "[Waves]", "[Snake]"
dictionary = [
' ', ' ', ' ', ' en', ' la ', ' el ', ' de ', 'ien', 'tra', ' de',
'te ', 'ar', 'a ', 'ada', 'es', 'as', 'o ', ' con', 'ero', 'ado',
'e ', 'que', 'en', 'al', 'os ', 'ora', 'nte', ' al', 'lo ', 'or',
'os', 'er', 'aci', 'res', ' que ', ' es', 'el', 'los ', 'tar', ' se',
', ', 'ro', ' de l', ' est', 're', 'on', 'an', 'pued', ' del', 'ás ',
'la', 'ti', 'la ', 'Es', 'to', 'ta', 'para', 'uer', 'ier', ' un ',
' por', 'oder', 'da', 'in', 'cu', ' ha', 'per', 'ano', ' ve', 'cer',
'lo', ' no ', 'ic', 'ra', 'ab', 'ir', ' una', 'undo', 'es ', 'as ',
'con', 'a, ', 'te', ' m', 'gu', ' tu', 'ando', ' p', 'de', 'le',
'ol', 'o, ', 'ten', 'lle', ' a ', 'aba', 'com',
]
rom_addrs = [0x9c8000, 0x8edf40]
class LangNL(LangUS):
alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"-", ".", ",", "[...]", ">", "(", ")", "[Ankh]",
"[Waves]", "[Snake]", "[LinkL]", "[LinkR]", "\"", "[Up]", "[Down]", "[Left]",
# 80 - 95
"[Right]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "<", "[A]", "[B]", "[X]", "[Y]",
]
dictionary = [
' ', ' ', ' ', "'s ", 'and ', 'are ', 'all ', 'ain', 'and', 'at ',
'ast', 'an', 'at', 'ble', 'ba', 'be', 'bo', 'can ', 'che', 'com',
'ck', 'des', 'di', 'do', 'en ', 'er ', 'ear', 'ent', 'ed ', 'en',
'er', 'ev', 'for', 'fro', 'give ', 'get', 'go', 'have', 'has', 'her',
'hi', 'ha', 'ight ', 'ing ', 'in', 'is', 'it', 'just', 'know', 'ly ',
'la', 'lo', 'man', 'ma', 'me', 'mu', "n't ", 'non', 'not', 'open',
'ound', 'out ', 'of', 'on', 'or', 'per', 'ple', 'pow', 'pro', 're ',
're', 'some', 'se', 'sh', 'so', 'st', 'ter ', 'thin', 'ter', 'tha',
'the', 'thi', 'to', 'tr', 'up', 'ver', 'with', 'wa', 'we', 'wh',
'wi', 'you', 'Her', 'Tha', 'The', 'Thi', 'You',
]
rom_addrs = [0x9c8000, 0x8edf40]
class LangSV(LangUS):
alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "Ö", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"å", ".", ",", "ä", ">", "(", ")","ö",
"Å", "Ä", "[LinkL]", "[LinkR]", "\"", "[Up]", "[Down]", "[Left]",
# 80 - 95
"[Right]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "<", "[Ankh]", "[Waves]", "[Snake]", "-", "[I]",
"[i]", "…", " ",
]
dictionary = [
' ', ' ', ' ', 'Du ', 'till', 'vill', 'bara', 'det', 'den', 'och',
'en ', 'r ', 'n ', 'ett', 'en', ' d', 'a ', 'Hjäl', 'har', 'ter',
't ', 'var', ' s', 'de', 'kan', 'med', 'som', 'för', 'att', 'ar',
' h', 'er', 'jag', 'dig', 'öppna', 'mig', 'är', 'inte', 'hit', 'på ',
'an', 'e ', 'rupie', '0kej', ' m', 'et', ', ', 'gång', 'måst', 'ten',
' f', 'u ', 'men', 'te', 'tt', 'ka', 'vara', 'ken', '0m ', 'från',
'myck', 'någo', 'in', ' k', ' i', 'vil', 'bar', 'ond', 'För', 'Jag',
'ra', 'tack', 'll', 'g ', 'ta', 'om', 'anna', 'alla', 'en,', 'ber',
'hem', 'han', 'st', 'ig', ' t', 'tro', 'kraf', 'ör', ' v', 'ag',
'… ', 'får', 'sin', 'mme', 'mma', 'en ', 'tat',
]
rom_addrs = [0x9c8000, 0x8edf40]
class LangPL(LangUS):
alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"-", ".", ",", "ć", "[Right]", "(", ")", "[Ankh]",
"[Waves]", "[Snake]", "[LinkL]", "[LinkR]","\"", "[Up]", "[Down]", "ę",
"ł", "ń", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"ą", "[4HeartR]", " ", "[Left]", "ó", "ś", "ż", "ź", "Ł",
"Ś", "Ż", "Ź",
] #
dictionary = ['Trój', '...', 'ść', 'Nie', ' nie', ' się', 'może', ' że', 'and', 'at ',
' ty', 'an', 'at', 'kus', 'ba', 'be', 'bo', 'chce', 'che', 'ki ',
'za', 'des', 'di', 'do', 'en ', 'er ', 'sz ', 'ent', 'ed ', 'en',
'er', ' w', 'moc', 'zię', 'przez', 'ale', 'go', 'dzie', 'has', 'rze',
'hi', 'ha', 'który', 'aby ', 'in', 'is', 'it', 'twoj', 'Może', 'łeś',
'la', 'lo', 'czn', 'ma', 'me', 'mu', 'szcz', 'ska', 'śli', 'przy',
'znaj', 'iecz', 'of', 'on', 'or', ' ', 'ple', 'pow', 'pro', 're ',
're', 'mnie', 'se', ' z', 'so', 'st', 'któr', ' jak', 'ksz', 'sze',
'coś', ' je', 'to', 'tr', 'up', 'kie', 'praw', 'wa', 'we', 'mi',
'wi', 'szy', 'chc', 'pra', 'cie', ' i ', 'esz',
]
rom_addrs = [0x9c8000, 0x8edf40]
class LangPT(LangUS):
alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", # 0 - 15
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", # 16 - 31
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", # 32 - 47
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "?", # 48 - 63
"-", ".", ",", "[...]", ">", "(", ")", "[Ankh]",
"[Waves]", "[Snake]", "[LinkL]", "[LinkR]", "\"", "[Up]", "[Down]", "[Left]",
# 80 - 95
"[Right]", "'", "[1HeartL]", "[1HeartR]", "[2HeartL]", "[3HeartL]", "[3HeartR]",
"[4HeartL]", "[4HeartR]", " ", "<", "[A]", "[B]", "[X]", "[Y]", "[I]",
"¡", "[!]", "Á", "À", "Â", "Ã", "É", "Ê", "Í", "Ó", "Ô", "Õ", "Ú", "á", "à", "â",
"ã", "é", "ê", "í", "ó", "ô", "õ", "ú", "ç",
]
dictionary = [
' ', ' ', ' ', ' ', 'o ', 'a ', 'e ', '..', 'de', 'ar',
's ', 'ra', ' d', 'es', 'ocê ', 'do', ' a', ' p', 'er', ' e',
'que', 'r ', 'os', 'te', ', ', 'as', 'or', 'm ', 'en', ' o',
'nt', 're', ' s', 'co', 'da', 'se', 'st', ' c', ' m', 'em',
'ma', 'ta', ' n', 'ad', 'on', 'al', 'ro', 'an', 'u ', 'nd',
' um', 'pa', 'ca', 'el', ' f', 'to', 'in', ' t', 'ou', 'ei',
'ss', 'ir', 'no', 'ri', 'tr', 'me', 'la', 'ia', 'le', 've',
'is', 'sa', 'eu', 'pe', 'a.', 'na', 'so', 'mo', 'ga', 'o.',
'á ', 'lo', 'ha', 'pr', 'ua', ' l', '! ', 'ui', 'am', 'ti',
'io', 'gu', 'i ', 'di', 'nh', ' i', 'id',
]
ESCAPE_CHARACTER = 0x62
rom_addrs = [0x9c8000, 0x8edf40]
encoder = 'new'
def __init__(self):
assert len(self.alphabet) == 121
class LangEU:
command_lengths = kText_CommandLengths_EU
command_names = kText_CommandNames_EU
COMMAND_START = 0x70
SWITCH_BANK = 0x88
FINISH = 0x8f
DICT_BASE_ENC, DICT_BASE_DEC = 0x88, 0x90
ESCAPE_CHARACTER = None
encoder = 'new'
class LangDE(LangEU):
alphabet = kTextAlphabet_DE
dictionary = kTextDictionary_DE
rom_addrs = [0x9c8000, 0x8CEB00]
class LangFR(LangEU):
alphabet = kTextAlphabet_FR
dictionary = kTextDictionary_FR
rom_addrs = [0x9c8000, 0x8CE800]
class LangFR_C(LangEU):
alphabet = kTextAlphabet_FR
dictionary = kTextDictionary_FR
rom_addrs = [0x9c8000, 0x8CF150]
kLanguages = {
'us' : LangUS(),
'de' : LangDE(),
'fr' : LangFR(),
'fr-c' : LangFR_C(),
'en' : LangEN(),
'es' : LangES(),
'pl' : LangPL(),
'pt' : LangPT(),
'redux' : LangUS(),
'nl' : LangNL(),
'sv' : LangSV(),
}
def dialogue_filename(s):
if s == 'us': return 'dialogue.txt'
return f"dialogue_{s.replace('-', '_')}.txt"
def uses_new_format(lang):
return kLanguages[lang].encoder == 'new'
dict_expansion = []
def decode_strings_generic(get_byte, lang):
info = kLanguages[lang]
p, rom_idx = info.rom_addrs[0], 1
result = []
while True:
s, srcdata = '', []
while True:
c = get_byte(p)
srcdata.append(c)
l = info.command_lengths[c - info.COMMAND_START] if c >= info.COMMAND_START and c < info.SWITCH_BANK else 1
p += l
if c == 0x7f: # EndMessage
break
if c < info.COMMAND_START:
if c == info.ESCAPE_CHARACTER:
c = get_byte(p); p += 1
srcdata.append(c)
s += info.alphabet[c]
elif c < info.SWITCH_BANK:
if l == 2:
srcdata.append(get_byte(p - 1))
s += '[%s %.2d]' % (info.command_names[c - info.COMMAND_START], get_byte(p - 1))
else:
s += '[%s]' % info.command_names[c - info.COMMAND_START]
elif c == info.FINISH:
return result # done
elif c == info.SWITCH_BANK:
p = info.rom_addrs[rom_idx]; rom_idx += 1
s, srcdata = '', []
elif c < info.SWITCH_BANK + 8:
if lang != 'pt':
assert 0
else:
s += info.dictionary[c - info.DICT_BASE_DEC]
dict_expansion.append(len(info.dictionary[c - info.DICT_BASE_DEC]))
result.append((s, srcdata))
if len(result) >= 397 and lang == 'pt':
return result
# print(len(result), s)
# if len(result) == 89:
# print(s)
# sys.exit(0)
def print_strings(rom, file = None):
texts = decode_strings_generic(rom.get_byte, rom.language)
if len(texts) == 396:
extra_str = "[Speed 00]0- [Number 00]. 1- [Number 01][2]2- [Number 02]. 3- [Number 03]"
texts = texts[:4] + [(extra_str, None)] + texts[4:]
for i, s in enumerate(texts):
print('%s: %s' % (i + 1, s[0]), file = file)
def encode_greedy_from_dict(s, i, rev, a2i, info):
a = s[i:]
if r := rev.get(a[0]):
for k, v in r.items():
if a.startswith(k):
return [v + info.DICT_BASE_ENC], len(k)
if a[0] == '[':
cmd, param = a[1:a.index(']')], None
cmdlen = len(cmd)
if r := a2i.get(a[:cmdlen+2]):
return [r], cmdlen+2
if ' ' in cmd:
cmd, param = cmd.split(' ', 1)
param = int(param)
return kEncoders[info.encoder](cmd, param), cmdlen + 2
else:
return [a2i[a[0]]], 1
print('substr %s not found' % a)
assert 0
def compress_strings(xs, lang = 'us'):
info = kLanguages[lang]
rev = {}
for a,b in enumerate(info.dictionary):
rev.setdefault(b[0], {})[b] = a
#rev = {b:a for a,b in enumerate(info.dictionary)}
a2i = {e:i for i,e in enumerate(info.alphabet)}
def compress_string(s):
i = 0
r = bytearray()
while i < len(s):
what, num = encode_greedy_from_dict(s, i, rev, a2i, info)
r.extend(what)
i += num
return r
return [compress_string(x) for x in xs]
def verify(get_byte):
for i, (decoded, original) in enumerate(decode_strings_generic(get_byte, 'us')):
c = compress_strings([decoded])[0]
if c != original:
print('String %s not match: %s, %s' % (decoded, c, original))
break
else:
pass
def encode_dictionary(lang = 'us'):
info = kLanguages[lang]
rev = {b:a for a,b in enumerate(info.alphabet)}
return [bytearray(rev[c] for c in line) for line in info.dictionary]
if __name__ == "__main__":
ROM = util.load_rom(sys.argv[1] if len(sys.argv) >= 2 else None, True)
decoded = decode_strings_generic(ROM.get_byte, 'de')
print('Total bytes: %d' % sum(len(a[1]) for a in decoded))
print('Dict tokens: %d' % len(dict_expansion))
print('Dict save: %d' % (sum(dict_expansion) - len(dict_expansion)))
print('US size ', len(kTextDictionary_US))
print('DE size ', len(kTextDictionary_DE))
texts = [a[0] for a in decoded]
# Pal seems to have one string too little
if len(texts) == 396:
extra_str = "[Speed 00]0- [Number 00]. 1- [Number 01][2]2- [Number 02]. 3- [Number 03]"
texts = texts[:4] + [extra_str] + texts[4:]
#for i, s in enumerate(texts):
# print('%s: %s' % (i + 1, s), file = None)
#encode_dictionary()
compr = compress_strings(texts, 'de')
print(f'Compressed size (excl eof): {sum(len(a) for a in compr)}')