-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodswitcher.py
284 lines (260 loc) · 10.5 KB
/
modswitcher.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
import logging
from sys import stdout
from pathlib import Path
import os
import subprocess
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, FileModifiedEvent
import ctypes
from datetime import datetime
import time
import json
import re
import minecraft_freezer
import pystray
from PIL import Image
os.system("title Mod switcher")
os.chdir(Path(__file__).parent)
logs = Path('logs')
if not logs.is_dir():
try:
os.mkdir(logs)
except IOError:
print('Could not create logs directory')
ctypes.windll.user32.MessageBoxW(0, 'Mod switcher was unable to complete startup.\nCould not create logs directory', Path(__file__).name, 16)
exit()
i = 0
logfile = logs.joinpath(datetime.now().strftime('%Y-%m-%d-') + str(i) + '.log')
while logfile.is_file():
i += 1
logfile = logs.joinpath(datetime.now().strftime('%Y-%m-%d-') + str(i) + '.log')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s]: %(message)s',
datefmt='%H:%M:%S')
file_handler = logging.FileHandler(logfile)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler(stdout)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
def exit_startup(message: str|None):
if message:
logger.fatal("Unable to complete startup:", message)
ctypes.windll.user32.MessageBoxW(0, 'Mod switcher was unable to complete startup.\n' + message, Path(__file__).name, 16)
else:
logger.fatal("Unable to complete startup")
ctypes.windll.user32.MessageBoxW(0, 'Mod switcher was unable to complete startup.', Path(__file__).name, 16)
exit()
# Get .minecraft directory
root = None
appdata = os.getenv('APPDATA')
if appdata != None:
root = Path(appdata).joinpath('.minecraft')
if not root.is_dir():
root = None
if root == None:
exit_startup('Could not find minecraft game directory')
logger.info(f'Found .minecraft at "{root}"')
# Create freezer
freezer = minecraft_freezer.Freezer(str(root))
# Get mods directory
mods_dir = root.joinpath('mods')
if not mods_dir.is_dir():
exit_startup("Folder 'mods' does not exist, no point in running")
logger.info(f'Found mods folder at "{mods_dir}"')
# Get profiles directory
profiles_dir = mods_dir.joinpath('profiles')
if not profiles_dir.is_dir():
logger.info('Profiles folder does not exist, attempting to create')
try:
os.mkdir(profiles_dir)
except:
exit_startup(f'Could not create profiles folder {profiles_dir}')
logger.info(f'Found profiles folder at "{profiles_dir}"')
# Get config
config_path = profiles_dir.joinpath('modswitcher.json')
selected_profiles_path = profiles_dir.joinpath(".selected_profile")
# Get launcher profiles file
launcher_profiles_path = Path(root).joinpath('launcher_profiles.json')
if not launcher_profiles_path.is_file():
exit_startup(f'"{launcher_profiles_path}" does not exist')
launcher_profiles_old_path = Path(root).joinpath('launcher_profiles.json_old')
with open(launcher_profiles_path, 'r') as f:
with open(launcher_profiles_old_path, 'w+') as old:
old.write(f.read())
# Change mod profiles
def move_jars(src: Path, dest: Path, contents: list[str]):
if len(contents) == 0:
logger.info(f'"{src}" is empty')
return
for mod in contents:
src_path = src.joinpath(mod).absolute()
name = src_path.name
if src_path.is_file():
if name.endswith(".jar"):
dest_path = dest.joinpath(name)
logging.debug(f'Attempting to move "{src_path}" to "{dest_path}"')
try:
shutil.move(src_path, dest_path)
logging.info(f'Moved "{name}" to "{dest}"')
except:
logging.error(f'Could not move "{name}" to "{dest}"')
else:
logging.info(f'"{name}" is not a .jar file, ignoring')
else:
logging.debug(f'"{name}" is a directory, ignoring')
def load_profile(profile: str, contents: list[str]):
# Move all .jar files from profile directory to mods directory and update selected_profile
logger.info(f'Loading "{profile}"')
profile_dir = profiles_dir.joinpath(profile)
move_jars(profile_dir, mods_dir, contents)
# Update selected profile
try:
with open(selected_profiles_path, 'w') as f:
f.write(profile)
logger.info('Updated selected profile')
except BaseException as e:
logger.fatal('Could not update selected profile')
raise e
def unload_profile(profile: str, contents: list[str]):
# Move all .jar files from mods directory to profile directory
logger.info(f'Unloading "{profile}"')
profile_dir = profiles_dir.joinpath(profile)
if not profile_dir.is_dir():
logger.info(f'"{profile_dir}" does not exist, attempting to create')
try:
os.mkdir(profile_dir)
except BaseException as e:
logger.error(f'Could not create "{profile_dir}"')
raise e
move_jars(mods_dir, profile_dir, contents)
def get_selected_profile():
if selected_profiles_path.exists():
with open(selected_profiles_path, "r") as f:
return f.read()
else:
with open(selected_profiles_path, "w") as f:
f.write("default")
return "default"
def launch(profile_name: str):
logger.info(f'Minecraft profile launched: "{profile_name}"')
new_profile = "default"
try:
with open(config_path, 'r') as f:
config = json.load(f)
profiles = config['profiles']
for pattern in profiles:
logger.debug(f'Matching "{profile_name}" against "{pattern}"')
match = re.match(pattern, profile_name)
if match:
profile = profiles[pattern]
logger.info(f'Found match against "{pattern}" for profile "{profile}"')
new_profile = profile
break
except:
new_profile = profile_name
# Get selected profile
selected_profile = get_selected_profile()
logger.info(f'Previous profile: "{selected_profile}"')
if new_profile.strip() == "" or not profiles_dir.joinpath(new_profile).is_dir():
new_profile = 'default'
logger.info(f'Mods profile being launched is "{new_profile}"')
if new_profile == selected_profile:
logger.info('Profile is already loaded, doing nothing')
return
try:
mods_to_unload = os.listdir(mods_dir)
mods_to_load = os.listdir(profiles_dir.joinpath(new_profile))
unload_profile(selected_profile, mods_to_unload)
load_profile(new_profile, mods_to_load)
except IOError:
pass
return
def parse_time(last_used: str) -> int:
logging.debug(f'Parsing lastUsed time "{last_used}"')
lengths = [4, 2, 2, 2, 2, 2, 3]
params = [int(last_used[sum(lengths[:i])+i:sum(lengths[:i+1])+i]) for i in range(len(lengths))]
logging.debug(f'Extracted params "{params}"')
params[6] *= 1000
dt = datetime(*params)
if dt == datetime(1970, 1, 1, 0, 0, 0, 0):
logging.debug(f'Profile has never been launched')
return 0
timestamp = int(time.mktime(dt.timetuple()))
logging.debug(f'"{last_used}" as a unix timestamp is {timestamp}')
return timestamp
# Watchdog
class EventHandler(FileSystemEventHandler):
def on_modified(self, event: FileModifiedEvent):
logger.debug(f'Modified: {event.src_path}')
if not event.is_directory and Path(event.src_path) == launcher_profiles_path:
with open(launcher_profiles_path, 'r') as f:
text = f.read()
with open(launcher_profiles_old_path, 'r') as f:
if f.read() == text:
logger.debug('Launcher profiles file content has not changed')
return
logger.info('Launch profiles file has been modified')
logger.debug(f'Updating "{launcher_profiles_old_path}"')
with open(launcher_profiles_old_path, 'w') as f:
f.write(text)
profiles = json.loads(text)['profiles']
logger.info('Finding latest profile')
# Find latest profile
latest_profile: str|None = None
latest_time = 0.0
for profile in profiles:
if 'gameDir' in profiles[profile]:
logging.debug(f'"{profile}" is not in the default directory, ignoring')
continue
logging.debug(f'Checking lastUsed time for {profile}')
last_used = parse_time(profiles[profile]["lastUsed"])
if last_used == latest_time:
logging.error(f'Profiles "{latest_profile}" and "{profile}" have the same lastUsed time')
return
if last_used > latest_time:
latest_profile = profile
latest_time = parse_time(profiles[profile]["lastUsed"])
if latest_profile == None:
logging.error("Could not find the latest profile")
return
logging.info(f'Found latest profile "{latest_profile}" AKA "{profiles[latest_profile]["name"]}"')
try:
logging.info("Freezing Minecraft")
freezer.suspend()
launch(profiles[latest_profile]["name"])
finally:
logging.info("Unfreezing Minecraft")
freezer.resume()
# Configure observer
event_handler = EventHandler()
observer = Observer()
observer.schedule(event_handler, root, recursive=False)
logger.info(f'Starting watch of launcher profiles at "{launcher_profiles_path}"')
observer.start()
# Configure iconr
icon = Image.open("icon.ico")
def open_mods():
subprocess.call(f'explorer "{mods_dir.absolute()}"', shell=True)
def open_log():
subprocess.call(f'explorer "{logs.absolute()}"', shell=True)
def exit_app():
logging.fatal("Exited with tray icon")
app.stop()
app = pystray.Icon(name="modswitcher", title="Mod switcher", icon=icon, menu=pystray.Menu(
pystray.MenuItem("Open mods folder", open_mods),
pystray.MenuItem("Show logs", open_log),
pystray.MenuItem("Exit", exit_app)
))
try:
app.run()
except KeyboardInterrupt:
logging.fatal("Exited with KeyboardInterrupt")
except BaseException as e:
logging.fatal(e)
finally:
ctypes.windll.user32.MessageBoxW(0, "Mod switcher has stopped.", Path(__file__).name, 16)
observer.stop()
observer.join()