-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
277 lines (187 loc) · 7.39 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
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
from typing import Union
from pprint import pprint
import distutils.spawn
import re
from typing import List
from config import *
import psutil
import subprocess
import os
from pygetwindow import BaseWindow
import pygetwindow as gw
import pyautogui as pag
from psutil import Process
import platform
def is_windows():
return platform.system() == 'Windows'
def is_vm():
if is_windows():
wmic_output = subprocess.check_output(
['wmic', 'bios', 'get', 'serialnumber'], shell=True)
wmic_output = wmic_output.decode()
wmic_output = wmic_output.replace('SerialNumber', '')
wmic_output = wmic_output.strip()
return wmic_output == '0'
else:
raise Exception(
"Checking if I'm in a VM is not implemented for Linux.")
def has_gpu():
# TODO This is flawed. Actually check OpenGL version.
if is_windows():
if is_vm():
return False
else: # not a vm but windows
return True
else: # linux, vm? maybe
return True
def write_time_file(i: float):
if os.path.exists(LOADING_TIME_FILE):
os.remove(LOADING_TIME_FILE)
with open(LOADING_TIME_FILE, 'w') as f:
f.write(str(i))
def cycle_windows_backwards():
"""Alt shift tab."""
pag.hotkey("alt", "shift", "tab")
def altf4():
"""Alt-F4."""
pag.hotkey('alt', 'f4')
def ensure_path_exists(p, err_msg=None) -> Exception:
msg = "Path {} does not exist! Halting! ".format(p)
if err_msg:
msg += err_msg
if not os.path.exists(p):
raise Exception(msg)
def is_multimc(p: Process) -> bool:
name, cmdline = "", ""
try:
name = p.name()
cmdline = p.cmdline()
except (PermissionError, psutil.AccessDenied, ProcessLookupError, psutil.NoSuchProcess) as e: # Windows can do this
if isinstance(e, PermissionError) or isinstance(e, psutil.AccessDenied):
print("Not allowed to view process {}".format(p.pid))
if isinstance(e, ProcessLookupError) or isinstance(e, psutil.NoSuchProcess):
print("Process {} does not exist. Race condition?".format(p.pid))
pass
# print(name, cmdline)
# if 'MultiMC' in name:
if 'multimc' in name.lower() or 'multimc' in ' '.join(cmdline).lower():
print(p)
return True
return False
def remove_file(fp):
if os.path.exists(fp):
os.remove(fp)
def get_multimc_binary_path():
"""Ex. C:/tools/MultiMC/MultiMC.exe"""
for path in MMC_BINARY_PATHS:
if os.path.exists(path):
return path
return None
def get_multimc_instances_path():
"""Ex. C:/tools/MultiMC/instances/"""
multimc_instances_folder = os.path.join(
os.path.dirname(get_multimc_binary_path()), 'instances')
ensure_path_exists(multimc_instances_folder, "MMC Instances folder")
return multimc_instances_folder
def get_multimc_instance_path():
"""Ex. C:/tools/MultiMC/instances/.TMP.PYAUTOGUI-TEST-MODPACK-EXPORT.tmp/"""
p = os.path.join(get_multimc_instances_path(), MODPACK_NAME)
ensure_path_exists(p)
return p
def get_multimc_instance_logfile_path():
"""Ex. C:/tools/MultiMC/instances/.TMP.PYAUTOGUI-TEST-MODPACK-EXPORT.tmp/minecraft/logs/latest.log"""
p = os.path.join(get_multimc_instance_path(), 'minecraft/logs/latest.log')
ensure_path_exists(p)
return p
def ensure_packwiz_installed():
if not distutils.spawn.find_executable("packwiz"):
raise Exception(
"`packwiz` not found on PATH. Download from https://github.com/comp500/packwiz")
def ensure_multimc_installed():
if not get_multimc_binary_path():
print("Could not find MultiMC binary. Searched these paths:")
pprint(MMC_BINARY_PATHS)
raise Exception("Could not find MMC instance.")
def ensure_multimc_closed():
# multimc must be closed
multimc_open = False
for p in psutil.process_iter():
multimc_open = is_multimc(p)
if multimc_open:
raise Exception("MultiMC must be CLOSED for this to work.")
def open_multimc() -> subprocess.Popen:
path = get_multimc_binary_path()
print("Exec {} in background".format(path))
# os.system(path) # NOTE: this blocks.
return subprocess.Popen([path])
def get_multimc_window_title() -> str:
windowTitles = gw.getAllTitles()
for title in windowTitles:
if 'multimc' in title.lower():
print(title)
return title
pprint(windowTitles)
raise Exception("Could not find window title containing 'MultiMC'!")
def get_multimc_window() -> BaseWindow:
w: BaseWindow = gw.getWindowsWithTitle(get_multimc_window_title())[0]
pprint(w)
return w
def get_active_window_title() -> str:
return gw.getActiveWindow().title
def get_packDOTtoml_path(modpack_path)->str:
return os.path.join(modpack_path, 'pack.toml')
def get_indexDOTtoml_path(modpack_path)->str:
return os.path.join(modpack_path, 'index.toml')
def generate_modpack_zip(modpack_path) -> str:
packDOTtoml_path=get_packDOTtoml_path(modpack_path)
indexDOTtoml_path=get_indexDOTtoml_path(modpack_path)
zip_path=os.path.join(modpack_path,ZIP_NAME)
default_zip_path=os.path.join(modpack_path,DEFAULT_ZIP_NAME)
print("Generating modpack zip at "+zip_path)
if not os.path.exists(packDOTtoml_path):
raise Exception(
"Could not find pack.toml in '{}'!".format(packDOTtoml_path))
remove_file(default_zip_path)
remove_file(zip_path)
# For some odd reason, packwiz refresh fails if this file doesn't exist.
if not os.path.exists(indexDOTtoml_path):
with open(indexDOTtoml_path, 'w') as _:
pass
print(subprocess.check_call(['packwiz', 'refresh'], cwd=modpack_path)) # Make index.toml
print(subprocess.check_call(['packwiz', 'cf', 'export'],cwd=modpack_path))
print("Done!")
os.rename(default_zip_path, zip_path)
return zip_path
def line_in_data_matches_rexp(data: List[str], rexps: Union[List[str], str]) -> bool:
"""Does a line in a list of data match one or more regular expressions?"""
if isinstance(rexps, str):
rexps = [rexps]
for line in data:
for rexp in rexps:
results = re.findall(rexp, line)
if len(results) > 0:
return True
return False
def get_file_data(path: str) -> List[str]:
with open(path, 'r') as f:
data = f.readlines()
return data
def dump_list_str_to_stdout(l: List[str], endl=''):
for line in l:
print(line, end=endl)
def line_in_file_matches_rexp(path: str, rexps: Union[List[str], str]) -> bool:
"""Does a line in a file match one or more regular expressions?"""
if isinstance(rexps, str):
rexps = [rexps]
with open(path, 'r') as f:
data = f.readlines()
return line_in_data_matches_rexp(data, rexps)
def logfile_says_ran_out_of_VRAM_while_stitching(data: List[str]):
"""Does the logfile indicate Minecraft has crashed due to running out of VRAM while stitching textures?"""
return line_in_data_matches_rexp(data, MINECRAFT_TEXTURE_STITCHER_OUT_OF_VRAM)
def logdata_says_done_loading_mods(data: List[str]):
"""Does the logfile indicate forge is done loading mods?"""
return line_in_data_matches_rexp(data, FORGE_LOADED_REXP)
def logdata_says_minecraft_crash_report(data: List[str]):
"""Does the logfile indicate Minecraft has crashed?"""
return line_in_data_matches_rexp(data, MINECRAFT_CRASHED_REXP)