This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
160 lines (120 loc) · 4.12 KB
/
utils.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
import json
import re
import shutil
import time
from pathlib import Path
from zipfile import ZipFile, ZipInfo
from rarfile import RarFile, RarInfo
from secret import (
APP_PATH,
DECIPHER_PATH,
DOWNLOAD_PATH,
EXPORTS,
IMAGE_PATH,
QQ_RECV,
SCRIPT_PATH,
)
ELEMENT_TYPE = [None, "Flame", "Water", "Wind", "Light", "Shadow"]
WEAPON_TYPE = [None, "Sword", "Blade", "Dagger", "Axe", "Lance", "Bow", "Wand", "Staff"]
ST_CTIME = lambda p: p.stat().st_ctime
def extract_file(lib, f, path):
""" Extract File from compressed folder """
try:
if isinstance(f, ZipInfo):
lib.extract(f, path)
path = path / f.filename
elif isinstance(f, RarInfo):
lib.extract(f)
path.parent.mkdir(parents=True, exist_ok=True)
Path(SCRIPT_PATH / f.filename).rename(path)
except:
code = "Failed"
else:
code = "Succeed"
print("{}: {}".format(code, path))
def get_images():
""" Extract Image from .rar """
files = {
"chara": "adventurer",
"weapon": "weapon",
"dragon": "dragon",
"amulet": "wyrmprint",
"skill": "skills",
"ability": "abilities",
}
path = max(QQ_RECV.glob("*.rar"), key=ST_CTIME)
p = re.compile(r"icon/({})/l/.*/(\w+)_rgba8.png".format("|".join(files.keys())))
try:
shutil.rmtree("./images")
Path("./images").mkdir()
with RarFile(path) as rf:
for f in rf.infolist():
if m := p.search(f.filename):
save_path = IMAGE_PATH / files[m[1]] / "{}.png".format(m[2])
extract_file(rf, f, save_path)
except FileNotFoundError:
print("File Not Found.")
else:
shutil.rmtree("./resources")
def get_files():
files = {
"AbilityData": "abilities",
"AmuletData": "wyrmprint",
"CharaData": "adventurer",
"DragonData": "dragon",
"FortPlantDetail": "facility",
"SkillData": "skills",
"TextLabel": "en",
"TextLabelJP": "ja",
"WeaponData": "weapon",
}
# update EN files
en_path = max(DOWNLOAD_PATH.glob("*_Dump.zip"), key=ST_CTIME)
p = re.compile(r"MonoBehaviour/({}).txt".format("|".join(files.keys())))
try:
# Make sure MonoBehaviour folder in zip files
with ZipFile(en_path) as zf:
for f in zf.infolist():
if r := p.search(f.filename):
f.filename = "{}.txt".format(files[r[1]])
extract_file(zf, f, DECIPHER_PATH)
except FileNotFoundError:
raise Exception("EN Zip: Wrong path or not exists.")
# update ZH TextLabel
zh_path = max(QQ_RECV.glob("*.zip"), key=ST_CTIME)
try:
with ZipFile(zh_path) as zf:
for f in zf.infolist():
if "TextLabel.txt" in f.filename:
f.filename = "zh.txt"
extract_file(zf, f, DECIPHER_PATH)
break
except FileNotFoundError:
raise Exception("ZH Zip: Wrong path or not exists.")
def get_text_path(file_name):
return DECIPHER_PATH / "{}.txt".format(file_name)
def get_json_path(folder, file_name):
return SCRIPT_PATH / folder / "{}.json".format(file_name)
def read_json(file_name, folder=EXPORTS):
path = get_json_path(folder, file_name)
if path.is_file():
with path.open(encoding="utf-8") as f:
return json.load(f)
return {}
def save_json(file_name, data, folder=EXPORTS):
path = get_json_path(folder, file_name)
with path.open("w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.write("\n")
print("Save file: {}".format(path))
def save_content(file_name, data):
path = APP_PATH / "src" / "data" / "content" / "{}.js".format(file_name)
with path.open("w", encoding="utf-8") as f:
f.write("const {} =\n".format(file_name))
json.dump(data, f, ensure_ascii=False, indent=2)
f.write(";\n\nexport default {};\n".format(file_name))
print("save file: {}".format(path))
if __name__ == "__main__":
get_files()
get_images()
pass