-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconstutils.py
executable file
·402 lines (362 loc) · 14.6 KB
/
constutils.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
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
import os
import sys
import pwd
import shutil
import subprocess
from urllib import request
# Import consts
from consts import (
STEAM_COMPAT_FOLDER,
BASE_STEAM_COMPAT,
SCAN_FOLDER,
WINEPREFIX,
INIT_FILE,
)
from coreutils import (
exit_with_message,
get_user_input,
popup_options,
show_message,
)
from corenodep import (
load_conf_setting,
save_conf_setting,
parse_version,
read_file,
)
from typing import (
List,
Union,
Optional,
)
from coreutils import (
log,
)
from mainutils import (
popup_execute,
)
if getattr(sys, "frozen", False):
SCRIPT_IMP_FILE = os.path.realpath(sys.executable)
else:
SCRIPT_IMP_FILE = os.path.realpath(__file__)
SCRIPT_PATH = os.path.dirname(SCRIPT_IMP_FILE)
# Ensure that wine is installed
def ensure_wine(verstr: Optional[str] = None) -> str:
WinePfx = os.path.join(BASE_STEAM_COMPAT, "drive_c")
ProtonPfx = os.path.join(WINEPREFIX, "drive_c")
if os.path.isdir(WinePfx):
try:
os.symlink(BASE_STEAM_COMPAT, WINEPREFIX)
except Exception as e:
pass
users = os.path.join(ProtonPfx, "users")
myuser = None
try:
myuser = os.getlogin()
except Exception as e:
pass
if not myuser:
try:
myuser = pwd.getpwuid(os.getuid()).pw_name
except Exception as e:
pass
if not myuser:
myuser = "steamuser"
mainuser = os.path.join(users, myuser)
steamuser = os.path.join(users, "steamuser")
if mainuser != steamuser:
if not os.path.isdir(mainuser) and not os.path.isdir(steamuser):
os.makedirs(mainuser, exist_ok=True)
elif os.path.isdir(steamuser) and not os.path.isdir(mainuser):
try:
os.symlink(steamuser, mainuser)
except Exception as e:
pass
if os.path.isdir(mainuser) and not os.path.isdir(steamuser):
try:
os.symlink(mainuser, steamuser)
except Exception as e:
pass
if os.path.isdir(ProtonPfx):
ProtonVersion = os.path.join(BASE_STEAM_COMPAT, "version")
if verstr:
with open(ProtonVersion, "w") as pver:
pver.write(verstr)
elif not os.path.isfile(ProtonVersion):
got = None
resp = None
while not got:
resp, tout = get_user_input(
"Input wine version", "Version file", "WineGE8.26", 40
)
got = parse_version(resp)
if not got:
show_message(
"The version string was incorrect.\nMake sure you have at least 1 number in the version string",
"Incorrect version",
20,
False,
)
if resp != None:
with open(ProtonVersion, "w") as pver:
pver.write(resp)
return ProtonVersion
else:
exit_with_message(
"Missing Prefix",
"Error: wine prefix is missing.\nMake sure you run the game without wemod-launcher once",
ask_for_log=True,
)
# Scan the steam compat folder for WeMod installed prefixes
def scanfolderforversions(
current_version_parts: List[Union[int, None]] = [None, None]
) -> List[Union[Optional[List[int]], Optional[str]]]:
# At default, we don't know of any available version
closest_version_folder = None
closest_version_number = None
priority = 6
# var to note a working ProtonGE 7 prefix
prefix_path_seven = None
# For all folders in steam compat
for folder in os.listdir(SCAN_FOLDER):
# Get the version file, folder path and check if WeMod is installed
folder_path = os.path.join(SCAN_FOLDER, folder)
version_file = os.path.join(folder_path, "version")
if os.path.isdir(folder_path) and os.path.exists(
os.path.join(folder_path, "pfx", ".wemod_installer")
):
folder_version_str = read_file(version_file)
folder_version_parts = parse_version(folder_version_str)
if (
folder_version_parts
and isinstance(folder_version_parts, list)
and folder_version_parts[0]
and folder_version_parts[1]
):
if (
folder_version_parts[0] == current_version_parts[0]
and folder_version_parts[1] == current_version_parts[1]
):
priority = 1
# Same major and minor version
closest_version_folder = folder_path
closest_version_number = folder_version_parts
# break # enable if ver 7 was found by the users
elif (
folder_version_parts[0] == current_version_parts[0]
and folder_version_parts[1] < current_version_parts[1]
):
# Same major, lower minor version
if priority > 2 or (
priority == 2
and (
not closest_version_folder
or folder_version_parts[1]
> closest_version_number[1]
)
):
priority = 2
closest_version_folder = folder_path
closest_version_number = folder_version_parts
elif (
folder_version_parts[0] == current_version_parts[0]
and folder_version_parts[1] > current_version_parts[1]
):
# Same major, higher minor version
if priority > 3 or (
priority == 3
and (
not closest_version_folder
or folder_version_parts[1]
< closest_version_number[1]
)
):
priority = 3
closest_version_folder = folder_path
closest_version_number = folder_version_parts
elif folder_version_parts[0] < current_version_parts[0]:
# Lower major version
if priority > 4 or (
priority == 4
and (
not closest_version_folder
or folder_version_parts[0]
> closest_version_number[0]
or (
folder_version_parts[0]
== closest_version_number[0]
and folder_version_parts[1]
> closest_version_number[1]
)
)
):
priority = 4
closest_version_folder = folder_path
closest_version_number = folder_version_parts
elif folder_version_parts[0] > current_version_parts[0]:
# Higher major version
if priority > 5 or (
priority == 5
and (
not closest_version_folder
or folder_version_parts[0]
< closest_version_number[0]
or (
folder_version_parts[0]
== closest_version_number[0]
and folder_version_parts[1]
< closest_version_number[1]
)
)
):
priority = 5
closest_version_folder = folder_path
closest_version_number = folder_version_parts
# try to find any GE-Proton7 prefix that the user might have for uploading
if (
folder_version_parts[0] == 7
and folder_version_str.find("GE-Proton") >= 0
):
minseven = load_conf_setting("ProtonMinorSeven")
if minseven:
minseven = int(minseven)
if minseven < folder_version_parts[1]:
save_conf_setting(
"ProtonMinorSeven",
str(folder_version_parts[1]),
)
prefix_path_seven = folder_path
else:
save_conf_setting(
"ProtonMinorSeven", str(folder_version_parts[1])
)
prefix_path_seven = folder_path
if prefix_path_seven:
from mainutils import copy_folder_with_progress
prefixesfolder = os.path.join(SCAN_FOLDER, "prefix")
os.makedirs(prefixesfolder, exists_ok=True)
protonconfminor = load_conf_setting("ProtonMinorSeven")
prefixesfile = os.path.join(
prefixesfolder, f"Proton7.{protonconfminor}.zip"
)
# ask the user to upload the prefix if they have one
prresp = show_message(
f"In your scan folder, the online missing prefix version with GE-Proton 7 (.{protonconfminor}) was found.\nPlease, be so kind and click yes to zip the prefix\nand that upload it to something like https://www.sendgb.com/; \nlastly, paste the link in a WeMod-Launcher issue on GitHub",
"GE-Proton7 found",
60,
True,
)
if prresp == "Yes":
sevenpfx = os.path.join(prefix_path_seven, "pfx")
seveninit = os.path.join(sevenpfx, ".wemod_installer")
initcont = read_file(seveninit)
with open(seveninit, "w") as init:
init.write("")
waslink = False
if os.path.islink(WINEPREFIX):
os.remove(WINEPREFIX)
waslink = True
copy_folder_with_progress(
prefix_path_seven, prefixesfile, True, [None], [None]
)
if waslink:
try:
os.symlink(prefix_path_seven, sevenpfx)
except Exception as e:
pass
if not initcont:
initcont = ""
with open(seveninit, "w") as init:
init.write(initcont)
os.system(f"xdg-open '{prefixesfolder}'")
elif prresp == "No":
save_conf_setting("ProtonMinorSeven", "99")
return closest_version_number, closest_version_folder
# Function to execute winetricks commands
def winetricks(command: str, proton_bin: str) -> int:
winetricks_sh = os.path.join(SCRIPT_PATH, "winetricks")
# Download winetricks if not present
if not os.path.isfile(winetricks_sh):
log("winetricks not found. Downloading...")
request.urlretrieve(
"https://github.com/Winetricks/winetricks/raw/master/src/winetricks",
winetricks_sh,
)
log(f"setting exec permissions on '{winetricks_sh}'")
process = subprocess.Popen(
f"sh -c 'chmod +x {winetricks_sh}'", shell=True
)
exit_code = process.wait()
if exit_code != 0:
message = f"failed to set exec permission on '{winetricks_sh}'"
log(message)
exit_with_message("ERROR", message, ask_for_log=True)
# Prepare the command with the correct environment
command = f"export PATH='{proton_bin}' && export WINEPREFIX='{WINEPREFIX}' && {winetricks_sh} {command}"
# Execute the command and return the response
resp = popup_execute("winetricks", command)
return resp
# Function to execute wine commands
def wine(command: str, proton_bin: str) -> int:
# Prepare the command with the correct environment
command = f"export PATH='{proton_bin}' && export WINEPREFIX='{WINEPREFIX}' && wine {command}"
# Execute the command and return the response
resp = popup_execute("wine", command)
return resp
def troubleshooter() -> None:
trouble = os.getenv("TROUBLESHOOT")
if not trouble and os.path.isfile(INIT_FILE):
trouble = read_file(INIT_FILE)
if not trouble:
trouble = load_conf_setting("Troubleshoot")
runtro = False
if trouble == None or (trouble and trouble.lower() == "true"):
runtro = True
log("Starting troubleshooter")
else:
log("Troubleshooter was disabled, continuing")
while runtro:
log("Troubleshooter start loop")
ret = popup_options(
"Troubleshooter",
"Did WeMod work as expected?\nIf not, troubleshoot common problems with WeMod.\nDeleting the game prefix usually helps.\nDeleting Wemod.exe helps if wemod updates their program.\nTo use the Troubleshooter after it was disabled,\nyou can add TROUBLESHOOT=true in front of the launch command",
[
[
"Disable troubleshooter globally",
"Disable troubleshooter for this game",
],
[
"Enable troubleshooter globally",
"Enable troubleshooter for this game",
],
["Delete game prefix", "Delete WeMod.exe"],
["Close wemod-launcher"],
],
120,
)
log(f"Selected in the troubleshooter was '{ret}'")
if ret == "Disable troubleshooter globally":
save_conf_setting("Troubleshoot", "false")
elif ret == "Disable troubleshooter for this game":
with open(INIT_FILE, "w") as init:
init.write("false")
elif ret == "Enable troubleshooter globally":
save_conf_setting("Troubleshoot", "true")
elif ret == "Enable troubleshooter for this game":
with open(INIT_FILE, "w") as init:
init.write("true")
elif ret == "Delete WeMod.exe":
try:
os.remove(os.path.join(SCRIPT_PATH, "wemod_bin", "WeMod.exe"))
except Exception as e:
pass
elif ret == "Delete game prefix":
try:
shutil.rmtree(BASE_STEAM_COMPAT)
except Exception as e:
pass
elif not ret or ret == "Close wemod-launcher":
runtro = False
log("Closing troubleshooter as requested")