-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
418 lines (394 loc) · 16.9 KB
/
script.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
from requests import get
import os
import json
import datetime
import zipfile
import csv
booleans = ["yes", "no"]
manifest = get("https://launchermeta.mojang.com/mc/game/version_manifest.json").json()
def isValidMinecraftVersion(minecraft_version):
if minecraft_version is None:
return False
for version in manifest["versions"]:
if version["id"] == minecraft_version:
return True
return False
def getMinecraftVersionInfos(minecraft_version_id):
if minecraft_version_id is None:
return None
for version in manifest["versions"]:
if version["id"] == minecraft_version_id:
return version
return None
class Command:
def __init__(self, desc, callback):
self.desc = desc
self.callback = callback
def registerCommand(name, desc, callback):
if name not in cmdList:
cmdList[name] = Command(desc, callback)
def init():
language_name = None
while language_name is None or language_name == "":
print("What is your language name?")
language_name = input("-> ")
language_region = None
while language_region is None or language_region == "":
print("What is your language region?")
language_region = input("-> ")
language_code = None
while language_code is None or language_code == "":
print("What is your language code? (Example: US English is en_us)")
language_code = input("-> ")
print("Is your language bidirectional?")
language_bidirectional = str(input("-> ")).lower()
while language_bidirectional is None or language_bidirectional not in booleans:
print("Is your language bidirectional?")
language_bidirectional = str(input("-> ")).lower()
if language_bidirectional == "yes":
language_bidirectional = True
else:
language_bidirectional = False
minecraft_version_id = None
while minecraft_version_id is None or isValidMinecraftVersion(minecraft_version_id) is not True:
print("Which minecraft version do you want to translate first?")
minecraft_version_id = input("-> ")
minecraft_version = getMinecraftVersionInfos(minecraft_version_id)
if datetime.datetime.strptime(str(minecraft_version["releaseTime"]).replace("+00:00", ""),
"%Y-%m-%dT%H:%M:%S") >= datetime.datetime(2019, 11, 21):
pack_format = 5
elif datetime.datetime.strptime(str(minecraft_version["releaseTime"]).replace("+00:00", ""),
"%Y-%m-%dT%H:%M:%S") >= datetime.datetime(2017, 11, 27):
pack_format = 4
elif datetime.datetime.strptime(str(minecraft_version["releaseTime"]).replace("+00:00", ""),
"%Y-%m-%dT%H:%M:%S") >= datetime.datetime(2016, 8, 10):
pack_format = 3
elif datetime.datetime.strptime(str(minecraft_version["releaseTime"]).replace("+00:00", ""),
"%Y-%m-%dT%H:%M:%S") >= datetime.datetime(2015, 7, 29):
pack_format = 2
else:
pack_format = 1
print("Initializing workspace")
print("Generating mcmeta file")
if not os.path.isdir("resourcepack"):
os.mkdir("resourcepack")
mcmeta = {
"pack": {
"description": language_name + " translation of Minecraft",
"pack_format": pack_format
},
"language": {
language_code: {
"name": language_name,
"region": language_region,
"bidirectional": language_bidirectional
}
}
}
mcmeta_file = open("resourcepack/pack.mcmeta", "w")
mcmeta_file.write(json.dumps(mcmeta, indent=4))
mcmeta_file.close()
print("Done")
print("Downloading {} client jar".format(minecraft_version_id))
if not os.path.isdir(".temp"):
os.mkdir(".temp")
if not os.path.isfile(".temp/client-{}.jar".format(minecraft_version_id)):
minecraft_version_manifest = get(minecraft_version["url"]).json()
client_jar_file = open(".temp/client-{}.jar".format(minecraft_version_id), "wb")
client_jar_data = get(minecraft_version_manifest["downloads"]["client"]["url"]).content
client_jar_file.write(client_jar_data)
client_jar_file.close()
print("Done")
print("Extracting en_us.json file")
jar_content = zipfile.ZipFile(".temp/client-{}.jar".format(minecraft_version_id), 'r')
jar_content.extract('assets/minecraft/lang/en_us.json', '.temp/{}'.format(minecraft_version_id))
jar_content.close()
print("Done")
print("Reading en_us.json file")
en_us_file = open(".temp/{}/assets/minecraft/lang/en_us.json".format(minecraft_version_id), 'r')
en_us = json.loads(en_us_file.read())
print("Done")
print("Making csv file")
with open("translations.csv", 'w', newline='') as csvfile:
csv_content = csv.writer(csvfile, dialect='excel')
csv_content.writerow(['Keys', 'English', language_name])
for entry in en_us:
if entry == "language.name":
csv_content.writerow([entry, en_us[entry], language_name])
elif entry == "language.region":
csv_content.writerow([entry, en_us[entry], language_region])
elif entry == "language.code":
csv_content.writerow([entry, en_us[entry], language_code])
else:
csv_content.writerow([entry, en_us[entry], ''])
csvfile.close()
en_us_file.close()
print("Done")
print("Cleaning temp")
os.remove(".temp/client-{}.jar".format(minecraft_version_id))
os.remove(".temp/{}/assets/minecraft/lang/en_us.json".format(minecraft_version_id))
os.rmdir(".temp/{}/assets/minecraft/lang".format(minecraft_version_id))
os.rmdir(".temp/{}/assets/minecraft".format(minecraft_version_id))
os.rmdir(".temp/{}/assets".format(minecraft_version_id))
os.rmdir(".temp/{}".format(minecraft_version_id))
os.rmdir(".temp")
print("Done")
def upgrade():
file_path = None
while file_path is None:
print("Path of your translation.csv file.")
file_path = input("-> ")
old_version_id = None
while old_version_id is None or isValidMinecraftVersion(old_version_id) is False:
print("Minecraft version of your language file")
old_version_id = input("-> ")
old_version = getMinecraftVersionInfos(old_version_id)
new_version_id = None
new_version = None
while new_version_id is None or isValidMinecraftVersion(new_version_id) is False or new_version is None:
print("Minecraft version you want to upgrade it to")
new_version_id = input("-> ")
if isValidMinecraftVersion(new_version_id):
new_version = getMinecraftVersionInfos(new_version_id)
if datetime.datetime.strptime(str(new_version["releaseTime"]).replace("+00:00", ""),
"%Y-%m-%dT%H:%M:%S") <= datetime.datetime \
.strptime(str(old_version["releaseTime"]).replace("+00:00", ""), "%Y-%m-%dT%H:%M:%S"):
print("The version you input is older than the previous version of your lang file!")
new_version = None
print("Opening CSV file")
old_rows = []
with open(file_path, 'r', newline='') as csv_file:
csv_file_content = csv.reader(csv_file, dialect='excel')
for row in csv_file_content:
old_rows.append(row)
csv_file.close()
print("Done")
print("Downloading {} client jar".format(new_version_id))
if not os.path.isdir(".temp"):
os.mkdir(".temp")
if not os.path.isfile(".temp/client-{}.jar".format(new_version_id)):
minecraft_version_manifest = get(new_version["url"]).json()
client_jar_file = open(".temp/client-{}.jar".format(new_version_id), "wb")
client_jar_data = get(minecraft_version_manifest["downloads"]["client"]["url"]).content
client_jar_file.write(client_jar_data)
client_jar_file.close()
print("Done")
print("Extracting en_us.json file")
jar_content = zipfile.ZipFile(".temp/client-{}.jar".format(new_version_id), 'r')
jar_content.extract('assets/minecraft/lang/en_us.json', '.temp/{}'.format(new_version_id))
jar_content.close()
print("Done")
print("Reading en_us.json file")
en_us_file = open(".temp/{}/assets/minecraft/lang/en_us.json".format(new_version_id), 'r')
en_us = json.loads(en_us_file.read())
en_us_file.close()
print("Done")
print("Comparing old and new entries")
todo_key_list = ["New keys:", ""]
removed_rows = 0
to_remove = []
for row in old_rows:
keep = True
if row[0] not in en_us:
keep = False
if row[0] == 'Keys':
keep = True
if keep is False:
removed_rows = removed_rows + 1
to_remove.append(row)
for to_remove_entry in to_remove:
old_rows.remove(to_remove_entry)
print("Removed {} rows".format(removed_rows))
new_rows = 0
to_add = []
for key in en_us:
add = True
for row in old_rows:
if key == row[0]:
add = False
if add:
new_rows = new_rows + 1
to_add.append([key, en_us[key], ''])
todo_key_list.append(key)
for to_add_entry in to_add:
old_rows.append(to_add_entry)
print("Added {} rows".format(new_rows))
index = 0
replaced = 0
todo_key_list.append("")
todo_key_list.append("Changed keys:")
todo_key_list.append("")
for row in old_rows:
if row[0] == 'Keys':
continue
else:
index = index + 1
replace = True
if row[1] == en_us[row[0]]:
replace = False
if replace:
replaced = replaced + 1
new_row = [row[0], en_us[row[0]], '']
old_rows[index] = new_row
todo_key_list.append(row[0])
print("Replaced {} rows".format(replaced))
print("Sorting Rows")
sorted_rows = [old_rows[0]]
for key in en_us:
for row in old_rows:
if row[0] == key:
sorted_rows.append(row)
print("Done")
print("Done")
print("Creating upgraded CSV file")
with open(file_path.replace(".csv", "") + "-{}.csv".format(new_version_id), 'w', newline='') as csvfile:
csv_content = csv.writer(csvfile, dialect='excel')
for entry in sorted_rows:
csv_content.writerow(entry)
csvfile.close()
print("Done")
print("Creating to_translate.txt file")
with open("to_translate.txt", 'w') as todo_file:
todo_file_content = todo_key_list[0]
index = 0
for todo in todo_key_list:
if index == 0:
index = index + 1
continue
todo_file_content = todo_file_content + "\n{}".format(todo)
index = index + 1
todo_file.write(todo_file_content)
todo_file.close()
print("Done")
print("Cleaning temp")
os.remove(".temp/client-{}.jar".format(new_version_id))
os.remove(".temp/{}/assets/minecraft/lang/en_us.json".format(new_version_id))
os.rmdir(".temp/{}/assets/minecraft/lang".format(new_version_id))
os.rmdir(".temp/{}/assets/minecraft".format(new_version_id))
os.rmdir(".temp/{}/assets".format(new_version_id))
os.rmdir(".temp/{}".format(new_version_id))
os.rmdir(".temp")
print("Done")
def build():
file_path = None
while file_path is None:
print("Path of your translation.csv file.")
file_path = input("-> ")
print("Reading CSV file")
csv_rows = []
with open(file_path, 'r', newline='') as csv_file:
csv_file_content = csv.reader(csv_file, dialect='excel')
for row in csv_file_content:
csv_rows.append(row)
csv_file.close()
print("Done")
print("Reading pack.mcmeta")
with open("resourcepack/pack.mcmeta", 'r') as mcmeta_file:
mcmeta = json.loads(mcmeta_file.read())
mcmeta_file.close()
print("Done")
print("Generating lang file")
if mcmeta["pack"]["pack_format"] > 3:
lang_file_content = {}
for row in csv_rows:
if row is not csv_rows[0]:
if row[2] != "":
lang_file_content[row[0]] = row[2]
if not os.path.isdir("resourcepack/assets"):
os.mkdir("resourcepack/assets")
if not os.path.isdir("resourcepack/assets/minecraft"):
os.mkdir("resourcepack/assets/minecraft")
if not os.path.isdir("resourcepack/assets/minecraft/lang"):
os.mkdir("resourcepack/assets/minecraft/lang")
with open("resourcepack/assets/minecraft/lang/{}.json".format(lang_file_content["language.code"]),
'w') as lang_file:
lang_file.write(json.dumps(lang_file_content, indent=4))
lang_file.close()
print("Done")
print("Building Resource pack ZIP")
resourcepack_zip = zipfile.ZipFile("{}_Translation.zip".format(csv_rows[0][2]), 'w')
resourcepack_zip.write("resourcepack/pack.mcmeta", "pack.mcmeta")
if mcmeta["pack"]["pack_format"] > 3:
resourcepack_zip.write("resourcepack/assets/minecraft/lang/{}.json".format(csv_rows[3][2]),
"assets/minecraft/lang/{}.json".format(csv_rows[3][2]))
resourcepack_zip.close()
print("Done")
def importPack():
file_path = None
while file_path is None:
print("Path to the language pack files")
file_path = input("-> ")
minecraft_version_id = None
while minecraft_version_id is None or isValidMinecraftVersion(minecraft_version_id) is not True:
print("Which minecraft version was your language pack made for?")
minecraft_version_id = input("-> ")
minecraft_version = getMinecraftVersionInfos(minecraft_version_id)
print("Reading mcmeta file")
mcmeta = {}
with open(os.path.join(file_path, "pack.mcmeta")) as mcmeta_file:
mcmeta = json.loads(mcmeta_file.read())
mcmeta_file.close()
language_meta = {}
for language in mcmeta["language"]:
language_meta = mcmeta["language"][language]
language_meta["code"] = language
print("Done")
print("Downloading {} client jar".format(minecraft_version_id))
if not os.path.isdir(".temp"):
os.mkdir(".temp")
if not os.path.isfile(".temp/client-{}.jar".format(minecraft_version_id)):
minecraft_version_manifest = get(minecraft_version["url"]).json()
client_jar_file = open(".temp/client-{}.jar".format(minecraft_version_id), "wb")
client_jar_data = get(minecraft_version_manifest["downloads"]["client"]["url"]).content
client_jar_file.write(client_jar_data)
client_jar_file.close()
print("Done")
print("Extracting en_us.json file")
jar_content = zipfile.ZipFile(".temp/client-{}.jar".format(minecraft_version_id), 'r')
jar_content.extract('assets/minecraft/lang/en_us.json', '.temp/{}'.format(minecraft_version_id))
jar_content.close()
print("Done")
print("Reading en_us.json file")
en_us_file = open(".temp/{}/assets/minecraft/lang/en_us.json".format(minecraft_version_id), 'r')
en_us = json.loads(en_us_file.read())
en_us_file.close()
print("Done")
print("Reading {}.json file".format(language_meta["code"]))
lang_file = open(os.path.join(file_path, "assets/minecraft/lang/{}.json".format(language_meta["code"])))
lang_json = json.loads(lang_file.read())
lang_file.close()
print("Done")
print("Generating CSV file")
with open("translations.csv", 'w', newline='') as csvfile:
csv_content = csv.writer(csvfile, dialect='excel')
csv_content.writerow(['Keys', 'English', language_meta["name"]])
for key in en_us:
if lang_json.get(key) is None:
csv_content.writerow([key, en_us[key], ''])
else:
csv_content.writerow([key, en_us[key], lang_json[key]])
csvfile.close()
print("Done")
print("Cleaning temp")
os.remove(".temp/client-{}.jar".format(minecraft_version_id))
os.remove(".temp/{}/assets/minecraft/lang/en_us.json".format(minecraft_version_id))
os.rmdir(".temp/{}/assets/minecraft/lang".format(minecraft_version_id))
os.rmdir(".temp/{}/assets/minecraft".format(minecraft_version_id))
os.rmdir(".temp/{}/assets".format(minecraft_version_id))
os.rmdir(".temp/{}".format(minecraft_version_id))
os.rmdir(".temp")
print("Done")
cmdList = {}
cmdName = None
registerCommand("init", "Initializes a workspace to make a minecraft language file", init)
registerCommand("upgrade", "Upgrade workspace to another version of minecraft", upgrade)
registerCommand("build", "Build your language resourcepack", build)
registerCommand("importPack", "Import a language pack and setup a workspace for it", importPack)
while cmdName not in cmdList:
if cmdName is not None:
print("\nInvalid command!")
print("What command do you want to use?")
for cmd in cmdList:
print("- " + cmd + ": " + cmdList[cmd].desc)
cmdName = input("-> ")
cmdList[cmdName].callback()