forked from institution/mpskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharmap.py
96 lines (75 loc) · 2.27 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
import sys
import os.path
import json
from record import Record
from conf import conf
from fail import fail,warning,printf
# NOTE: charmap will not be applied to "[]" characters and everything inside them
# NOTE: charmap: keep lower and upper case characters separated by 32
# NOTE: charmap: keep digits at their default positions
# NOTE: charmap: keep 010, 013 codes (line-feed and carriage-return)
# NOTE: charmap: in Rex keep "@"
default_charmap = u'''{
"000": "|",
"010": "\\u000A",
"013": "\\u000D",
"064": "@",
"048": "0",
"049": "1",
"050": "2",
"051": "3",
"052": "4",
"053": "5",
"054": "6",
"055": "7",
"056": "8",
"057": "9"
}
'''
charmap_filename = 'charmap-mpskit.json'
def save_default_charmap(cwd):
charmap_path = os.path.join(cwd, charmap_filename)
if not os.path.exists(charmap_path):
with open(charmap_path, 'w', encoding='utf-8') as f:
f.write(default_charmap)
printf(charmap_path)
else:
printf("charmap already exists at: {}", charmap_path)
def find_charmap_path(cwd):
""" find charmap file starting from cwd directory and searching upwards"""
cur = cwd
for _ in range(64):
charmap_path = os.path.join(cur, charmap_filename)
if os.path.exists(charmap_path):
return charmap_path
par = os.path.abspath(os.path.join(cur, os.pardir))
if par == cur:
return None
cur = par
warning("charmap file not found due to search depth limit; curr_dir: {}", cur)
return None
def load_charmap(cwd):
charmap_path = find_charmap_path(cwd)
if charmap_path is None:
# warning('cannot locate charmap file: {}', charmap_filename)
printf('using default charmap')
# printf('you can create default charmap with: mpskit charmap create')
# printf('it should be located in game main directory')
charmap_json = default_charmap
else:
printf('charmap-path: {}', charmap_path)
with open(charmap_path, encoding='utf-8') as f:
charmap_json = f.read()
try:
cm = json.loads(charmap_json)
except json.decoder.JSONDecodeError as e:
#import ipdb; ipdb.set_trace()
fail("while reading charmap file: {}", str(e))
conf.charmap_decode = {}
conf.charmap_encode = {}
for (k, v) in cm.items():
kk = chr(int(k))
vv = v
conf.charmap_decode[kk] = vv
conf.charmap_encode[vv] = kk
printf('charmap-size: {}', len(conf.charmap_decode))