forked from dyang886/Game-Cheats-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
167 lines (133 loc) · 5.35 KB
/
config.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
import gettext
import json
import locale
import os
import shutil
import sys
import tempfile
import pinyin
import polib
ts = None
def resource_path(relative_path):
if hasattr(sys, "_MEIPASS"):
full_path = os.path.join(sys._MEIPASS, relative_path)
else:
full_path = os.path.join(os.path.abspath("."), relative_path)
if not os.path.exists(full_path):
resource_name = os.path.basename(relative_path)
formatted_message = tr("Couldn't find {missing_resource}. Please try reinstalling the application.").format(missing_resource=resource_name)
raise FileNotFoundError(formatted_message)
return full_path
def apply_settings(settings):
with open(SETTINGS_FILE, "w") as f:
json.dump(settings, f, indent=4)
def load_settings():
locale.setlocale(locale.LC_ALL, '')
system_locale = locale.getlocale()[0]
locale_mapping = {
"English_United States": "en_US",
"Chinese (Simplified)_China": "zh_CN",
"Chinese (Simplified)_Hong Kong SAR": "zh_CN",
"Chinese (Simplified)_Macao SAR": "zh_CN",
"Chinese (Simplified)_Singapore": "zh_CN",
"Chinese (Traditional)_Hong Kong SAR": "zh_TW",
"Chinese (Traditional)_Macao SAR": "zh_TW",
"Chinese (Traditional)_Taiwan": "zh_TW"
}
app_locale = locale_mapping.get(system_locale, 'en_US')
default_settings = {
"downloadPath": os.path.join(os.environ["APPDATA"], "GCM Trainers"),
"language": app_locale,
"theme": "black",
"enSearchResults": False,
"autoUpdateDatabase": True,
"autoUpdate": True,
"WeModPath": os.path.join(os.environ["LOCALAPPDATA"], "WeMod"),
"autoStart": False,
"showWarning": True,
"downloadServer": "intl",
"removeBgMusic": True,
}
try:
with open(SETTINGS_FILE, "r") as f:
settings = json.load(f)
except Exception as e:
print("Error loading settings json" + str(e))
settings = default_settings
for key, value in default_settings.items():
settings.setdefault(key, value)
with open(SETTINGS_FILE, "w") as f:
json.dump(settings, f, indent=4)
return settings
def get_translator():
if not hasattr(sys, 'frozen'):
for root, dirs, files in os.walk(resource_path("locale/")):
for file in files:
if file.endswith(".po"):
po = polib.pofile(os.path.join(root, file))
po.save_as_mofile(os.path.join(root, os.path.splitext(file)[0] + ".mo"))
lang = settings["language"]
gettext.bindtextdomain("Game Cheats Manager",resource_path("locale/"))
gettext.textdomain("Game Cheats Manager")
lang = gettext.translation("Game Cheats Manager", resource_path("locale/"), languages=[lang])
lang.install()
return lang.gettext
def is_chinese(text):
for char in text:
if '\u4e00' <= char <= '\u9fff':
return True
return False
def sort_trainers_key(name):
if is_chinese(name):
return pinyin.get(name, format="strip", delimiter=" ")
return name
def ensure_trainer_details_exist():
dst = os.path.join(DATABASE_PATH, "xgqdetail.json")
if not os.path.exists(dst):
shutil.copyfile(resource_path("dependency/xgqdetail.json"), dst)
def ensure_trainer_download_path_is_valid():
try:
os.makedirs(settings["downloadPath"], exist_ok=True)
except Exception:
settings["downloadPath"] = os.path.join(os.environ["APPDATA"], "GCM Trainers")
apply_settings(settings)
os.makedirs(settings["downloadPath"], exist_ok=True)
setting_path = os.path.join(os.environ["APPDATA"], "GCM Settings")
os.makedirs(setting_path, exist_ok=True)
SETTINGS_FILE = os.path.join(setting_path, "settings.json")
DATABASE_PATH = os.path.join(setting_path, "db")
os.makedirs(DATABASE_PATH, exist_ok=True)
DOWNLOAD_TEMP_DIR = os.path.join(tempfile.gettempdir(), "GameCheatsManagerTemp", "download")
VERSION_TEMP_DIR = os.path.join(tempfile.gettempdir(), "GameCheatsManagerTemp", "version")
WEMOD_TEMP_DIR = os.path.join(tempfile.gettempdir(), "GameCheatsManagerTemp", "wemod")
settings = load_settings()
tr = get_translator()
ensure_trainer_details_exist()
ensure_trainer_download_path_is_valid()
if settings["theme"] == "black":
dropDownArrow_path = resource_path("assets/dropdown-white.png").replace("\\", "/")
elif settings["theme"] == "white":
dropDownArrow_path = resource_path("assets/dropdown-black.png").replace("\\", "/")
upArrow_path = resource_path("assets/up.png").replace("\\", "/")
downArrow_path = resource_path("assets/down.png").replace("\\", "/")
leftArrow_path = resource_path("assets/left.png").replace("\\", "/")
rightArrow_path = resource_path("assets/right.png").replace("\\", "/")
resourceHacker_path = resource_path("dependency/ResourceHacker.exe")
unzip_path = resource_path("dependency/7z/7z.exe")
binmay_path = resource_path("dependency/binmay.exe")
emptyMidi_path = resource_path("dependency/TrainerBGM.mid")
elevator_path = resource_path("dependency/elevator.exe")
search_path = resource_path("assets/search.png")
language_options = {
"English (US)": "en_US",
"简体中文": "zh_CN",
"繁體中文": "zh_TW"
}
theme_options = {
tr("Black"): "black",
tr("white"): "white"
}
server_options = {
tr("International"): "intl",
tr("China") + tr(" (Some trainers cannot be downloaded)"): "china"
}