-
Notifications
You must be signed in to change notification settings - Fork 4
/
charmap.py
618 lines (603 loc) · 10.7 KB
/
charmap.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# coding: utf-8
"""Charmap for Pokémon Emerald.
"""
def decode(input, decode_charmap):
old = bytearray(input)
new = u''
i = 0
len_old = len(old)
while i < len_old:
start = i
item = decode_charmap
while hasattr(item, 'get'):
char = old[i]
item_ = item.get(char)
if item_ is None:
item_ = item.get(None)
item = item_
i += 1
if item in ['{COLOR}', '{HIGHLIGHT}', '{SHADOW}']:
color = colors.get(old[i])
if color:
item += '{{{}}}'.format(color)
i += 1
elif item in ['{COLOR_HIGHLIGHT_SHADOW}']:
for _ in xrange(3):
color = colors.get(old[i])
if color:
item += '{{{}}}'.format(color)
i += 1
chars = item
if chars is None:
chars = ''.join('{' + hex(byte) + '}' for byte in old[start:i])
new += chars
return new
def encode(input, encode_charmap):
old = input
new = ''
i = 0
len_old = len(old)
while i < len_old:
char = old[i]
if char == '{':
j = i + old.find('}', i) + 1
char = old[i:j]
i = j
else:
i += 1
chars = encode_charmap.get(char)
if chars is None:
# assume hex i.e. '{FF}'
chars = chr(int(char[1:-1], 16))
new += chars
return new
colors = [
'TRANSPARENT',
'DARK_GREY',
'RED',
'GREEN',
'BLUE',
'YELLOW',
'CYAN',
'MAGENTA',
'LIGHT_GREY',
'BLACK',
'BLACK2',
'SILVER',
'WHITE',
'SKY_BLUE',
'LIGHT_BLUE',
'WHITE2',
]
colors = dict(enumerate(colors))
emerald_decode = {
0x00: u' ',
0x01: u'À',
0x02: u'Á',
0x03: u'Â',
0x04: u'Ç',
0x05: u'È',
0x06: u'É',
0x07: u'Ê',
0x08: u'Ë',
0x09: u'Ì',
0x0A: u'{0x0A}', # None
0x0B: u'Î',
0x0C: u'Ï',
0x0D: u'Ò',
0x0E: u'Ó',
0x0F: u'Ô',
0x10: u'Œ',
0x11: u'Ù',
0x12: u'Ú',
0x13: u'Û',
0x14: u'Ñ',
0x15: u'ß',
0x16: u'à',
0x17: u'á',
0x18: u'{0x18}', # None
0x19: u'ç',
0x1A: u'è',
0x1B: u'é',
0x1C: u'ê',
0x1D: u'ë',
0x1E: u'ì',
0x1F: u'{0x1F}', # None
0x20: u'î',
0x21: u'ï',
0x22: u'ò',
0x23: u'ó',
0x24: u'ô',
0x25: u'œ',
0x26: u'ù',
0x27: u'ú',
0x28: u'û',
0x29: u'ñ',
0x2A: u'º',
0x2B: u'ª',
0x2C: u'¹',
0x2D: u'&',
0x2E: u'+',
0x2F: u'{0x2F}', # None
0x30: u'{0x30}', # None
0x31: u'{0x31}', # None
0x32: u'{0x32}', # None
0x33: u'{0x33}', # None
0x34: u'{0x34}', # None # '[Lv]'
0x35: u'=',
0x36: u';',
0x37: u'{0x37}', # None
0x38: u'{0x38}', # None
0x39: u'{0x39}', # None
0x3A: u'{0x3A}', # None
0x3B: u'{0x3B}', # None
0x3C: u'{0x3C}', # None
0x3D: u'{0x3D}', # None
0x3E: u'{0x3E}', # None
0x3F: u'{0x3F}', # None
0x40: u'{0x40}', # None
0x41: u'{0x41}', # None
0x42: u'{0x42}', # None
0x43: u'{0x43}', # None
0x44: u'{0x44}', # None
0x45: u'{0x45}', # None
0x46: u'{0x46}', # None
0x47: u'{0x47}', # None
0x48: u'{0x48}', # None
0x49: u'{0x49}', # None
0x4A: u'{0x4A}', # None
0x4B: u'{0x4B}', # None
0x4C: u'{0x4C}', # None
0x4D: u'{0x4D}', # None
0x4E: u'{0x4E}', # None
0x4F: u'{0x4F}', # None
0x50: u'{0x50}', # None
0x51: u'¿',
0x52: u'¡',
0x53: {
None: u'{PK}',
0x54: u'{PKMN}',
},
0x54: u'{MN}',
0x55: {
None: u'{PO}',
0x56:{0x57:{0x58:{0x59: u'{POKEBLOCK}'}}},
},
0x56: u'{KE}',
0x57: u'{BL}',
0x58: u'{OC}',
0x59: u'{K}',
0x5A: u'Í',
0x5B: u'%',
0x5C: u'(',
0x5D: u')',
0x5E: u'{0x5E}', # None
0x5F: u'{0x5F}', # None
0x60: u'{0x60}', # None
0x61: u'{0x61}', # None
0x62: u'{0x62}', # None
0x63: u'{0x63}', # None
0x64: u'{0x64}', # None
0x65: u'{0x65}', # None
0x66: u'{0x66}', # None
0x67: u'{0x67}', # None
0x68: u'â',
0x69: u'{0x69}', # None
0x6A: u'{0x6A}', # None
0x6B: u'{0x6B}', # None
0x6C: u'{0x6C}', # None
0x6D: u'{0x6D}', # None
0x6E: u'{0x6E}', # None
0x6F: u'í',
0x70: u'{0x70}', # None
0x71: u'{0x71}', # None
0x72: u'{0x72}', # None
0x73: u'{0x73}', # None
0x74: u'{0x74}', # None
0x75: u'{0x75}', # None
0x76: u'{0x76}', # None
0x77: u'{0x77}', # None
0x78: u'{0x78}', # None
0x79: u'{0x79}', # None
0x7A: u'{0x7A}', # None
0x7B: u'{0x7B}', # None
0x7C: u'{0x7C}', # None
0x7D: u'{0x7D}', # None
0x7E: u'{0x7E}', # None
0x7F: u'{0x7F}',
0x80: u'{0x80}', # None
0x81: u'{0x81}', # None
0x82: u'{0x82}', # None
0x83: u'{0x83}', # None
0x84: u'{0x84}', # None
0x85: u'{0x85}', # None
0x86: u'{0x86}', # None
0x87: u'{0x87}', # None
0x88: u'{0x88}', # None
0x89: u'{0x89}', # None
0x8A: u'{0x8A}', # None
0x8B: u'{0x8B}', # None
0x8C: u'{0x8C}', # None
0x8D: u'{0x8D}', # None
0x8E: u'{0x8E}', # None
0x8F: u'{0x8F}', # None
0x90: u'{0x90}', # None
0x91: u'{0x91}', # None
0x92: u'{0x92}', # None
0x93: u'{0x93}', # None
0x94: u'{0x94}', # None
0x95: u'{0x95}', # None
0x96: u'{0x96}', # None
0x97: u'{0x97}', # None
0x98: u'{0x98}', # None
0x99: u'{0x99}', # None
0x9A: u'{0x9A}', # None
0x9B: u'{0x9B}', # None
0x9C: u'{0x9C}', # None
0x9D: u'{0x9D}', # None
0x9E: u'{0x9E}', # None
0x9F: u'{0x9F}', # None
0xA0: u'{0xA0}', # None
0xA1: u'0',
0xA2: u'1',
0xA3: u'2',
0xA4: u'3',
0xA5: u'4',
0xA6: u'5',
0xA7: u'6',
0xA8: u'7',
0xA9: u'8',
0xAA: u'9',
0xAB: u'!',
0xAC: u'?',
0xAD: u'.',
0xAE: u'-',
0xAF: u'|',
0xB0: u'…',
0xB1: u'“',
0xB2: u'”',
0xB3: u'‘',
0xB4: u'’', #u'\'',
0xB5: u'♂',
0xB6: u'♀',
0xB7: u'¥',
0xB8: u',',
0xB9: u'×',
0xBA: u'/',
0xBB: u'A',
0xBC: u'B',
0xBD: u'C',
0xBE: u'D',
0xBF: u'E',
0xC0: u'F',
0xC1: u'G',
0xC2: u'H',
0xC3: u'I',
0xC4: u'J',
0xC5: u'K',
0xC6: u'L',
0xC7: u'M',
0xC8: u'N',
0xC9: u'O',
0xCA: u'P',
0xCB: u'Q',
0xCC: u'R',
0xCD: u'S',
0xCE: u'T',
0xCF: u'U',
0xD0: u'V',
0xD1: u'W',
0xD2: u'X',
0xD3: u'Y',
0xD4: u'Z',
0xD5: u'a',
0xD6: u'b',
0xD7: u'c',
0xD8: u'd',
0xD9: u'e',
0xDA: u'f',
0xDB: u'g',
0xDC: u'h',
0xDD: u'i',
0xDE: u'j',
0xDF: u'k',
0xE0: u'l',
0xE1: u'm',
0xE2: u'n',
0xE3: u'o',
0xE4: u'p',
0xE5: u'q',
0xE6: u'r',
0xE7: u's',
0xE8: u't',
0xE9: u'u',
0xEA: u'v',
0xEB: u'w',
0xEC: u'x',
0xED: u'y',
0xEE: u'z',
0xEF: u'{0xEF}', # None
0xF0: u':',
0xF1: u'Ä',
0xF2: u'Ö',
0xF3: u'Ü',
0xF4: u'ä',
0xF5: u'ö',
0xF6: u'ü',
0xF7: u'{0xF7}', # None
0xF8: u'{0xF8}', # None
0xF9: u'{0xF9}', # None
0xFA: u'\\l', # None
0xFB: u'\\p',
0xFC: {
0: '{NAME_END}',
1: '{COLOR}',
2: '{HIGHLIGHT}',
3: '{SHADOW}',
4: '{COLOR_HIGHLIGHT_SHADOW}',
6: '{SIZE}',
8: '{PAUSE}',
9: '{PAUSE_UNTIL_PRESS}',
12: '{ESCAPE}',
13: '{SHIFT_TEXT}',
16: '{PLAY_MUSIC}',
21: '{ENLARGE}',
22: '{SET_TO_DEFAULT_SIZE}',
23: '{PAUSE_MUSIC}',
24: '{RESUME_MUSIC}',
},
0xFD: {
1: u'{PLAYER}',
2: u'{STR_VAR_1}',
3: u'{STR_VAR_2}',
4: u'{STR_VAR_3}',
5: u'{KUN}',
6: u'{RIVAL}',
7: u'{VERSION}',
8: u'{EVIL_TEAM}',
9: u'{GOOD_TEAM}',
10: u'{EVIL_LEADER}',
11: u'{GOOD_LEADER}',
12: u'{EVIL_LEGENDARY}',
13: u'{GOOD_LEGENDARY}',
},
0xFE: u'\\n',
0xFF: u'$',
}
emerald_jp_decode = {
# Hiragana
0x01: u'あ',
0x02: u'い',
0x03: u'う',
0x04: u'え',
0x05: u'お',
0x06: u'か',
0x07: u'き',
0x08: u'く',
0x09: u'け',
0x0A: u'こ',
0x0B: u'さ',
0x0C: u'し',
0x0D: u'す',
0x0E: u'せ',
0x0F: u'そ',
0x10: u'た',
0x11: u'ち',
0x12: u'つ',
0x13: u'て',
0x14: u'と',
0x15: u'な',
0x16: u'に',
0x17: u'ぬ',
0x18: u'ね',
0x19: u'の',
0x1A: u'は',
0x1B: u'ひ',
0x1C: u'ふ',
0x1D: u'へ',
0x1E: u'ほ',
0x1F: u'ま',
0x20: u'み',
0x21: u'む',
0x22: u'め',
0x23: u'も',
0x24: u'や',
0x25: u'ゆ',
0x26: u'よ',
0x27: u'ら',
0x28: u'り',
0x29: u'る',
0x2A: u'れ',
0x2B: u'ろ',
0x2C: u'わ',
0x2D: u'を',
0x2E: u'ん',
0x2F: u'ぁ',
0x30: u'ぃ',
0x31: u'ぅ',
0x32: u'ぇ',
0x33: u'ぉ',
0x34: u'ゃ',
0x35: u'ゅ',
0x36: u'ょ',
0x37: u'が',
0x38: u'ぎ',
0x39: u'ぐ',
0x3A: u'げ',
0x3B: u'ご',
0x3C: u'ざ',
0x3D: u'じ',
0x3E: u'ず',
0x3F: u'ぜ',
0x40: u'ぞ',
0x41: u'だ',
0x42: u'ぢ',
0x43: u'づ',
0x44: u'で',
0x45: u'ど',
0x46: u'ば',
0x47: u'び',
0x48: u'ぶ',
0x49: u'べ',
0x4A: u'ぼ',
0x4B: u'ぱ',
0x4C: u'ぴ',
0x4D: u'ぷ',
0x4E: u'ぺ',
0x4F: u'ぽ',
0x50: u'っ',
# Katakana
0x51: u'ア',
0x52: u'イ',
0x53: u'ウ',
0x54: u'エ',
0x55: u'オ',
0x56: u'カ',
0x57: u'キ',
0x58: u'ク',
0x59: u'ケ',
0x5A: u'コ',
0x5B: u'サ',
0x5C: u'シ',
0x5D: u'ス',
0x5E: u'セ',
0x5F: u'ソ',
0x60: u'タ',
0x61: u'チ',
0x62: u'ツ',
0x63: u'テ',
0x64: u'ト',
0x65: u'ナ',
0x66: u'ニ',
0x67: u'ヌ',
0x68: u'ネ',
0x69: u'ノ',
0x6A: u'ハ',
0x6B: u'ヒ',
0x6C: u'フ',
0x6D: u'ヘ',
0x6E: u'ホ',
0x6F: u'マ',
0x70: u'ミ',
0x71: u'ム',
0x72: u'メ',
0x73: u'モ',
0x74: u'ヤ',
0x75: u'ユ',
0x76: u'ヨ',
0x77: u'ラ',
0x78: u'リ',
0x79: u'ル',
0x7A: u'レ',
0x7B: u'ロ',
0x7C: u'ワ',
0x7D: u'ヲ',
0x7E: u'ン',
0x7F: u'ァ',
0x80: u'ィ',
0x81: u'ゥ',
0x82: u'ェ',
0x83: u'ォ',
0x84: u'ャ',
0x85: u'ュ',
0x86: u'ョ',
0x87: u'ガ',
0x88: u'ギ',
0x89: u'グ',
0x8A: u'ゲ',
0x8B: u'ゴ',
0x8C: u'ザ',
0x8D: u'ジ',
0x8E: u'ズ',
0x8F: u'ゼ',
0x90: u'ゾ',
0x91: u'ダ',
0x92: u'ヂ',
0x93: u'ヅ',
0x94: u'デ',
0x95: u'ド',
0x96: u'バ',
0x97: u'ビ',
0x98: u'ブ',
0x99: u'ベ',
0x9A: u'ボ',
0x9B: u'パ',
0x9C: u'ピ',
0x9D: u'プ',
0x9E: u'ペ',
0x9F: u'ポ',
0xA0: u'ッ',
# Japanese punctuation
0x00: u' ',
0xAB: u'!',
0xAC: u'?',
0xAD: u'。',
0xAE: u'ー',
0xAF: u'·',
0xB0: u'‥',
0xFA: u'\\l',
0xFB: u'\\p',
0xFC: {
0: '{NAME_END}',
1: '{COLOR}',
2: '{HIGHLIGHT}',
3: '{SHADOW}',
4: '{COLOR_HIGHLIGHT_SHADOW}',
6: '{SIZE}',
8: '{PAUSE}',
9: '{PAUSE_UNTIL_PRESS}',
12: '{ESCAPE}',
13: '{SHIFT_TEXT}',
16: '{PLAY_MUSIC}',
21: '{ENLARGE}',
22: '{SET_TO_DEFAULT_SIZE}',
23: '{PAUSE_MUSIC}',
24: '{RESUME_MUSIC}',
},
0xFD: {
1: u'{PLAYER}',
2: u'{STR_VAR_1}',
3: u'{STR_VAR_2}',
4: u'{STR_VAR_3}',
5: u'{KUN}',
6: u'{RIVAL}',
7: u'{VERSION}',
8: u'{EVIL_TEAM}',
9: u'{GOOD_TEAM}',
10: u'{EVIL_LEADER}',
11: u'{GOOD_LEADER}',
12: u'{EVIL_LEGENDARY}',
13: u'{GOOD_LEGENDARY}',
},
0xFE: u'\\n',
0xFF: u'$',
}
def reverse_charmap(charmap):
encode = {}
def recurse(keys, value):
if type(value) is dict:
for k, v in value.items():
if k is None:
recurse(keys, v)
else:
recurse(keys + [k], v)
else:
encode[value] = ''.join(map(chr, keys))
recurse([], charmap)
return encode
def old_reverse_charmap(charmap):
encode = {}
for k, v in charmap.items():
if type(v) is dict:
for k2, v2 in v.items():
if k2 is not None:
encode[v2] = chr(k) + chr(k2)
else:
encode[v2] = chr(k)
else:
encode[v] = chr(k)
return encode
emerald_encode = reverse_charmap(emerald_decode)
ruby_decode = emerald_decode
ruby_encode = emerald_encode
ruby_jp_decode = emerald_jp_decode