-
Notifications
You must be signed in to change notification settings - Fork 3
/
mtgo_translator.py
274 lines (227 loc) · 9.43 KB
/
mtgo_translator.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
# Execute this script at the root of a mtgjson repository clone from https://github.com/mtgjson/mtgjson
import json
import os
import re
import subprocess
# You can change the target language here
TARGET = 'fr'
TARGET_NAME = 'French'
# find the MTGO path.
def find_mtgo():
mtgo_dir = os.path.join(os.getenv('LOCALAPPDATA'), 'Apps','2.0','Data')
for _ in range(0, 2):
mtgo_dir = os.path.join(mtgo_dir, os.listdir(mtgo_dir)[0])
versions = [os.path.join(mtgo_dir, folder) for folder in os.listdir(mtgo_dir) if ('mtgo..tion' in folder)]
if len(versions) > 1:
latest_version = max(versions, key=os.path.getmtime)
mtgo_dir = latest_version
elif versions:
mtgo_dir = versions[0]
else:
print("Could not find MTGO data directory.")
print("Please run MTGO at least once before using this tool.")
exit()
return os.path.join(mtgo_dir, 'Data','CardDataSource')
mtgo_dir = find_mtgo()
if not os.path.exists('mtgjson'):
try:
CREATE_NEW_CONSOLE = 0x00000010
subprocess.run(['git', 'clone', 'https://github.com/mtgjson/mtgjson.git', '--depth', '1'], creationflags=CREATE_NEW_CONSOLE)
except subprocess.CalledProcessError:
#TODO: Try doing zip download.
print("Unable to clone mtgjson. Please install git, and make sure it's on your PATH.")
# print("Alternatively, download https://github.com/mtgjson/mtgjson/archive/master.zip, and unzip it as mtgjson")
exit()
cardnames_fn_xml = 'CARDNAME_STRING.xml'
oracles_fn_xml = 'REAL_ORACLETEXT_STRING.xml'
flavors_fn_xml = 'FLAVORTEXT_STRING.xml'
cardnames_xml = open(mtgo_dir + os.sep + cardnames_fn_xml, 'rb').read()
oracles_xml = open(mtgo_dir + os.sep + oracles_fn_xml, 'rb').read()
flavors_xml = open(mtgo_dir + os.sep + flavors_fn_xml, 'rb').read()
# making backups
open(mtgo_dir + os.sep + cardnames_fn_xml + '.bak', 'wb').write(cardnames_xml)
open(mtgo_dir + os.sep + oracles_fn_xml + '.bak', 'wb').write(oracles_xml)
open(mtgo_dir + os.sep + flavors_fn_xml + '.bak', 'wb').write(flavors_xml)
local = []
base = []
cards = {}
for fn in os.listdir(os.path.join('mtgjson', 'json')):
if not fn.endswith('.json'): continue
if fn.endswith(TARGET + '.json'):
local.append(fn)
elif fn.count('.') == 1:
base.append(fn)
for fn in base:
ext = json.load(open(os.path.join('mtgjson', 'json', fn), 'r', encoding='utf8'))
for card in ext['cards']:
name = card['name']
mid = None
if 'names' in card and len(card['names']) > 1:
continue # issue with flip cards
if 'foreignNames' not in card:
continue
for foreign in card['foreignNames']:
if foreign['language'] == TARGET_NAME and 'multiverseid' in foreign:
local_name = foreign['name']
mid = foreign['multiverseid']
break
if mid is None:
continue
if 'text' not in card:
continue
oracle = card['text']
if mid not in cards:
cards[mid] = {}
cards[mid]['name'] = name
cards[mid]['translated_name'] = local_name
cards[mid]['oracle_text'] = oracle
if 'flavor' in card:
cards[mid]['flavor'] = card['flavor']
for fn in local:
ext = json.load(open(os.path.join('mtgjson', 'json', fn), 'r', encoding='utf8'))
for card in ext['cards']:
mid = card['multiverseid']
if mid not in cards:
continue
c = cards[mid]
c['translated_text'] = card['originalText']
if 'flavor' in card:
c['translated_flavor'] = card['flavor']
cards_by_name = {}
for mid in cards:
card = cards[mid]
if 'name' not in card:
print(card)
name = cards[mid]['name']
cards_by_name[name] = cards[mid]
re_card_name = re.compile(r"\s*<CARDNAME_STRING_ITEM id='(?P<id>.*)'>(?P<text>.*)</CARDNAME_STRING_ITEM>")
re_id = re.compile(r"\s*<(?P<type>.*) id='(?P<id>.*)'/>")
ids_database = {}
for fn in os.listdir(mtgo_dir):
if not fn.startswith('client_'):
continue
lines = open(mtgo_dir + os.sep + fn, 'r', encoding='utf8').read().split('\n')
i = 0
while i < len(lines):
if '<DigitalObject' in lines[i]:
cardname_id = None
oracle_id = None
flavor_id = None
j = i+1
while 'DigitalObject' not in lines[j]:
m = re_id.match(lines[j])
if m is not None:
typ = m.group('type')
id = m.group('id')
if typ == 'CARDNAME_STRING':
cardname_id = id
elif typ == 'REAL_ORACLETEXT_STRING':
oracle_id = id
elif typ == 'FLAVORTEXT_STRING':
flavor_id = id
j += 1
if cardname_id is not None:
if cardname_id in ids_database:
ids = ids_database[cardname_id]
if oracle_id is not None:
ids['oracle'] = oracle_id
if flavor_id is not None:
ids['flavor'] = flavor_id
else:
ids_database[cardname_id] = { 'oracle' : oracle_id, 'flavor' : flavor_id }
i = j
i += 1
cardnames_xml = cardnames_xml.decode('latin-1')
card_name_by_mtgo_id = {}
output = '<?xml version="1.0" encoding="UTF-8"?>\n'
for ln in cardnames_xml.split('\n')[1:]:
m = re_card_name.match(ln)
if m is None:
output += ln + '\n'
continue
id = m.group('id')
name = m.group('text').replace("\\'", "'")
card_name_by_mtgo_id[id] = name
if name in cards_by_name and 'translated_name' in cards_by_name[name]:
card = cards_by_name[name]
name = card['translated_name']
name = name.replace("'","\\'")
output += "<CARDNAME_STRING_ITEM id='%s'>%s</CARDNAME_STRING_ITEM>\n" % (id, name)
open(mtgo_dir + os.sep + cardnames_fn_xml, 'wb').write(output.encode('utf-8', 'replace'))
oracles_xml = oracles_xml.decode('latin-1')
card_id_by_oracle_id = {}
for card_id in ids_database:
ids = ids_database[card_id]
if ids['oracle'] is not None:
card_id_by_oracle_id[ids['oracle']] = card_id
re_oracle_text = re.compile(r"\s*<REAL_ORACLETEXT_STRING_ITEM id='(?P<id>.*)'>(?P<text>.*)</REAL_ORACLETEXT_STRING_ITEM>")
n_oracle_tr = 0
output = '<?xml version="1.0" encoding="UTF-8"?>\n'
for ln in oracles_xml.split('\n')[1:]:
m = re_oracle_text.match(ln)
if m is None:
output += ln + '\n'
continue
id = m.group('id')
text = m.group('text')
if id in card_id_by_oracle_id:
card_id = card_id_by_oracle_id[id]
name = card_name_by_mtgo_id[card_id]
if name in cards_by_name:
card = cards_by_name[name]
if 'translated_text' in card:
text = card['translated_text']
text = text.replace("'","\\'")
text = text.replace("\n", "\\n")
text = text.replace("{W/U}", "{;a}")
text = text.replace("{W/B}", "{;b}")
text = text.replace("{U/B}", "{;c}")
text = text.replace("{U/R}", "{;d}")
text = text.replace("{B/R}", "{;e}")
text = text.replace("{B/G}", "{;f}")
text = text.replace("{R/G}", "{;g}")
text = text.replace("{R/W}", "{;h}")
text = text.replace("{G/W}", "{;i}")
text = text.replace("{G/U}", "{;j}")
text = text.replace("—", "@-")
text = text.replace("(", "@i(")
text = text.replace(")", ")@i")
n_oracle_tr += 1
output += "<REAL_ORACLETEXT_STRING_ITEM id='%s'>%s</REAL_ORACLETEXT_STRING_ITEM>\n" % (id, text)
print('%d oracle texts translated' % n_oracle_tr)
open(mtgo_dir + os.sep + oracles_fn_xml, 'wb').write(output.encode('utf-8', 'replace'))
flavors_xml = flavors_xml.decode('latin-1')
card_id_by_flavor_id = {}
for card_id in ids_database:
ids = ids_database[card_id]
if ids['flavor'] is not None:
card_id_by_flavor_id[ids['flavor']] = card_id
re_flavor_text = re.compile(r"\s*<FLAVORTEXT_STRING_ITEM id='(?P<id>.*)'>(?P<text>.*)</FLAVORTEXT_STRING_ITEM>")
n_flavor_tr = 0
output = '<?xml version="1.0" encoding="UTF-8"?>\n'
for ln in flavors_xml.split('\n')[1:]:
m = re_flavor_text.match(ln)
if m is None:
output += ln + '\n'
continue
id = m.group('id')
text = m.group('text')
if id in card_id_by_flavor_id: # and n_flavor_tr < 1225:
card_id = card_id_by_flavor_id[id]
name = card_name_by_mtgo_id[card_id]
if name in cards_by_name:
card = cards_by_name[name]
if 'translated_flavor' in card:
text = card['translated_flavor']
text = text.replace("'","\\'")
text = text.replace("\n", "\\n")
text = text.replace("—", "@-")
text = text.replace("« ", "\\\"")
text = text.replace("<< ", "\\\"")
text = text.replace(" »", "\\\"")
text = text.replace(" >>", "\\\"")
text = '@i' + text + '@i'
n_flavor_tr += 1
output += "<FLAVORTEXT_STRING_ITEM id='%s'>%s</FLAVORTEXT_STRING_ITEM>\n" % (id, text)
print('%d flavor texts translated' % n_flavor_tr)
open(mtgo_dir + os.sep + flavors_fn_xml, 'wb').write(output.encode('utf-8', 'replace'))