forked from major-gnuisance/thcrap-linux-ez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththcrap_auto_gui.py
659 lines (536 loc) · 22.4 KB
/
thcrap_auto_gui.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/python3
"""
thcrap_auto_gui: auto-loader for thcrap on Linux, now with GUI!
Usage on Steam:
1. Put this file in ~/thcrap_auto_gui.py
2. In Steam, use this for command line arguments: ~/thcrap_auto_gui.py %command%
On first launch, thcrap will be downloaded.
Add patch configurations via the thcrap configurator utility (Thcrap
Config button).
Don't try to add games in the "Find games" screen; when done with the
thcrap configurator, just click Cancel.
Any configurations you've added will automatically appear in the
launcher. Click the one you want to use, then "Start Game".
"""
import io
import os
import shutil
import sys
import subprocess
import json
import urllib.request
from os import path
from pathlib import Path
from zipfile import ZipFile
# Development: keep TEST definition.
try:
TEST
except NameError:
TEST = False
# Eventually set this based on options & args
gui = True
APP_NAME="Unnamed Thcrap Launcher"
THCRAP_URL = 'https://github.com/thpatch/thcrap/releases/latest/download/thcrap.zip'
# UNIX path to the game's executable. Must be the last argument in the
# command line in normal use.
GAME_EXE = sys.argv[-1]
# Base directory for thcrap
thcrap_dir = path.join(".", "thcrap")
# The base directory can be replaced with an absolute path to use a
# global thcrap dir
# Example of global dir: ~/.thcrap
#thcrap_dir = path.join(os.environ["HOME"], ".thcrap")
# Path to a cached thcrap.zip, to avoid redownloads. Used during development.
thcrap_zip_cache = path.join(os.environ["HOME"], ".cache", "thcrap", "thcrap.zip")
thcrap = path.join(thcrap_dir, "thcrap.exe")
thcrap_loader = path.join(thcrap_dir, "thcrap_loader.exe")
thcrap_config = path.join(thcrap_dir, "config")
thcrap_configjs = path.join(thcrap_config, "config.js")
thcrap_update_dll = path.join(thcrap_dir, "bin", "thcrap_update.dll")
thcrap_update_dll_disabled = thcrap_update_dll.replace(".dll", "_disabled.dll")
thcrap_steam_dll = path.join(thcrap_dir, "bin", "steam_api.dll")
thcrap_steam_dll_disabled = thcrap_steam_dll.replace(".dll", "_disabled.dll")
launcher_settings_path = path.join(thcrap_dir, "thcrap_launcher.json")
CONFIG_NAME_MAP = {
"no patch": "日本語",
"jp": "日本語",
"en": "English",
"es": "Español",
"de": "Deutsch",
"pt": "Português",
"fr": "Français",
"zh": "中文",
"kr": " 한국어",
"en_troll": "English Troll",
}
def decorate_lang(code):
label = CONFIG_NAME_MAP.get(code, None)
if label:
return f'{label}\n({code})'
return code
# Guard
def check_exe(filename):
"""Does filename look like a .exe that exists under the current directory?"""
cwd = os.environ["PWD"] # Not os.cwd(), that one canonicalizes the cwd path
if not filename.casefold().endswith(".exe"):
raise Exception(f"{filename} doesn't end in exe")
if not Path(filename).is_relative_to(cwd):
raise Exception(f"{filename} is not inside the current directory, {cwd}")
if not path.exists(filename):
raise Exception(f"{filename} does not exist")
return True
def init_thcrap(thcrap=thcrap,
thcrap_dir=thcrap_dir,
thcrap_zip_cache=thcrap_zip_cache,
zip_url=THCRAP_URL):
"""Ensures thcrap_dir exists and thcrap is extracted in it"""
# Make thcrap directory
if not path.isdir(thcrap_dir):
Path(thcrap_dir).mkdir(parents=True)
# Install thcrap if it doesn't already exist
if not path.exists(thcrap):
# Use cached development zip, if available
if path.exists(thcrap_zip_cache):
shutil.unpack_archive(thcrap_zip, thcrap_dir)
# Download and extract the thcrap zip
else:
with urllib.request.urlopen(zip_url) as f:
data = io.BytesIO(f.read())
with ZipFile(data) as zipf:
zipf.extractall(thcrap_dir)
def load_json(path):
"""Load config.js, if it exists."""
# TODO: support JSON5, maybe
try:
with open(path, encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_json(config, path):
"Save config.js"
with open(path, "w", encoding="utf-8", newline="\r\n") as file:
json.dump(config, file, indent=2)
def override_config_defaults():
"Change some default settings for thcrap"
config = load_json(thcrap_configjs)
overrides = {
# Stop thcrap from running in the background
"background_updates": False,
# Update only the game being launched
"update_others": False,
}
save_json(config | overrides, thcrap_configjs)
def is_patch_config_file(path):
try:
with open(path) as file:
contents = json.load(file)
return contents.get('patches', None)
except Exception:
return False
def list_configs():
"Return list of available patch configs"
try:
return [
f.name.removesuffix('.js')
for f in os.scandir(thcrap_config)
if f.name.endswith('.js')
and f.is_file()
and f.stat().st_size < 2**20
and is_patch_config_file(path.join(thcrap_config, f.name))
]
except Exception:
return []
def run_thcrap_config():
"Run thcrap configuration tool. Blocking."
# Preseed config
if not path.exists(thcrap_config):
os.mkdir(thcrap_config)
override_config_defaults()
args = sys.argv[1:-1] + [thcrap, '--skip-search-games']
subprocess.run(args, check=False)
def load_settings():
return load_json(launcher_settings_path)
def save_settings(settings):
save_json(settings, launcher_settings_path)
def get_lastrun(settings=load_settings()):
"""Get name of last config used"""
return str(settings.get('last_run', 'no_config'))
def set_lastrun(config_name):
"""Save name of last config used"""
save_settings(load_settings() | {'last_run': config_name})
def exec_game(config="en"):
if config != 'no patch':
# Relative path to game exe, from thcrap dir.
# Usually something like "../thXX.exe"
game_exe_rel = os.path.relpath(GAME_EXE, thcrap_dir)
# Build the modified command line.
new_command_line = sys.argv[1:-1] + [thcrap_loader, f'{config}.js', game_exe_rel]
else:
# Launch game unpatched, in Japanese locale
os.environ["LANG"]="ja_JP.UTF-8"
new_command_line = sys.argv[1:]
##### Aside #####
# The line above turns a command line like:
# [STEAM STUFF] /path/to/th18.exe
#
# Into something like:
# [STEAM STUFF] /path/to/thcrap/thcrap_loader.exe en.js ../th18.exe
#################
# Exec the next program in the command line
os.execvp(new_command_line[0], new_command_line)
# This ends control for the Python script
if gui:
from tkinter import *
from tkinter import ttk, font
import tkinter.colorchooser
import tkinter.messagebox
import colorsys
import types
def hex2rgb(hx):
"hex string to rgb (float)"
hx = hx.strip('#')
n = len(hx) // 3 # nibbles per color
m = 2 ** (n * 4) - 1 # max value per color
def hex2float(h):
return int(h, 16) / m
return tuple(hex2float(hx[i:i+n])
for i in range(0, len(hx), n))
def rgb2hex(r, g, b, nibbles=3):
"rgb (float) to hex, with given number of nibbles"
m = 2 ** (nibbles * 4) - 1 # max value per color
def float2hex(fl):
i = min(m, max(0, int(fl * m)))
return format(i, f'0{nibbles}x')
hex = "".join([float2hex(fl) for fl in (r, g, b)])
return f"#{hex}"
def lighten(rgb, factor=0.20):
(h, s, v) = colorsys.rgb_to_hsv(*hex2rgb(rgb))
v = min(1, v + v * factor)
return rgb2hex(*colorsys.hsv_to_rgb(h, s, v))
def darken(rgb, factor=0.20):
(h, s, v) = colorsys.rgb_to_hsv(*hex2rgb(rgb))
v = max(0, v - v * factor)
return rgb2hex(*colorsys.hsv_to_rgb(h, s, v))
class Launcher:
def __init__(self, configs):
self.configs = configs
root = self.root = Tk()
root.title(APP_NAME)
root.minsize(800, 600)
root.geometry('1280x800')
self.default_color = {
'bg_main': '#1f3b3e',
'bg_button': '#475a61',
'green': '#5abd42',
'red': '#bd4242',
'fg_1': '#bbb',
'fg_2': '#eee',
}
settings = load_settings()
self.color = settings.get('color', {})
self.set_style()
# Top level frame, contains everything else
mainframe = ttk.Frame(root, padding="10", style='Main.TFrame')
self.mainframe = mainframe
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
# Make top level frame fill the window
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# Quit on ESC
root.bind('<Escape>', lambda *_: self.quit())
# Press buttons with enter key
root.bind_class('TButton', '<Return>', ' ttk::button::activate %W')
# Configure top level frame grid.
# 3 equal columns
for i in range(3):
mainframe.columnconfigure(i, weight=1, uniform='a')
# Middle row fills extra space
mainframe.rowconfigure(1, weight=1)
self.add_bottom_buttons()
# App name at the top
ttk.Label(mainframe, text=APP_NAME, style="Main.TLabel")\
.grid(row=0, columnspan=3)
# Tabbed notebook widget, occupies middle row of top level frame
notebook = ttk.Notebook(mainframe)
notebook.grid(row=1, columnspan=3, sticky='nsew')
# Frame that holds grid of available configurations
configs_frame = ttk.Frame(notebook)
self.configs_frame = configs_frame
# Frame that holds configuration-specific settings
config_settings_frame = ttk.Frame(notebook, padding="20")
self.config_settings_frame = config_settings_frame
ttk.Label(config_settings_frame, text="TODO").pack()
# Frame that holds global settings
settings_frame = ttk.Frame(notebook, padding="20")
self.settings_frame = settings_frame
self.add_settings()
# Add Frames to notebook
notebook.add(configs_frame, text="Configurations")
notebook.add(config_settings_frame, text="Patch Options")
notebook.add(settings_frame, text="Launcher Settings")
self.configvar = IntVar(value=0)
self.refresh_configs()
# root.attributes("-fullscreen", True)
if not TEST:
root.mainloop()
def set_color(self, color_name, color):
self.color[color_name]=color
def get_colors(self):
return (self.default_color | self.color)
def get_color(self, color_name):
return self.get_colors().get(color_name)
def save_colors(self):
settings = load_settings()
settings['color']=self.color
save_settings(settings)
def reset_colors(self):
self.color = {}
self.set_style()
self.add_settings()
def change_color(self, color_name):
"""Show color picker and change color."""
current_color = self.get_color(color_name)
(_, color) = tkinter.colorchooser.askcolor(color=current_color)
if color:
self.set_color(color_name, color)
self.set_style()
self.add_settings()
def set_style(self):
"""Define and alter Tk styles"""
# Increase default font size
default_font = font.nametofont('TkDefaultFont')
default_font.configure(size=18)
underline_font = default_font.copy()
underline_font.configure(underline='yes')
bold_font = default_font.copy()
bold_font.configure(weight='bold')
# Gotta keep a reference or they get garbage collected and
# removed from Tk.
self.__fonts = (bold_font, underline_font)
sty = ttk.Style(self.root)
color = types.SimpleNamespace(**(self.get_colors()))
# Prevents unsightly flashes of white when changing
# notebook tabs
self.root.configure(background=color.bg_main)
sty.configure('TFrame',
background=color.bg_main)
sty.configure('Main.TFrame',
background=lighten(color.bg_main))
sty.configure('Main.TLabel',
background=lighten(color.bg_main))
sty.configure('TLabel',
background=color.bg_main,
foreground=color.fg_1,
padding=10)
sty.configure('TNotebook',
background=lighten(color.bg_main),
tabposition='nwe',
borderwidth=0)
sty.configure('TNotebook.Tab',
foreground=color.fg_2,
background=darken(color.bg_main),
padding='20 20',
borderwidth=0)
sty.map('TNotebook.Tab',
background=[('selected', color.bg_main)])
sty.configure('Toolbutton',
anchor='center', justify='center',
foreground=color.fg_2, background=color.bg_button,
relief='flat')
sty.map('Toolbutton',
background=[('selected', lighten(color.bg_button, 0.4)),
('active', lighten(color.bg_button, 0.2))],
# foreground=[('selected', '#8fd')],
borderwidth=[('', 0)])
sty.configure('TButton',
foreground='#f5f5f5', background=color.bg_button,
font=bold_font, borderwidth=2,
relief='flat')
sty.map('TButton',
background=[('active', lighten(color.bg_button))])
sty.configure('Setting.TButton',
anchor='w')
sty.configure('Start.TButton', background=color.green)
sty.map('Start.TButton',
background=[('active', lighten(color.green))])
sty.configure('Quit.TButton', background=color.red)
sty.map('Quit.TButton',
background=[('active', lighten(color.red))])
sty.configure('TCheckbutton',
foreground='#f5f5f5', background=color.bg_button,
font=bold_font, borderwidth=2,
relief='flat',
indicatorcolor=darken(color.bg_button),
indicatordiameter=25,
indicatormargin=10,
indicatorrelief='flat')
sty.map('TCheckbutton',
background=[('active', lighten(color.bg_button))],
indicatorcolor=[('selected', color.green)])
def set_updater(self, enable):
if enable and path.exists(thcrap_update_dll_disabled):
os.rename(thcrap_update_dll_disabled, thcrap_update_dll)
self.updater_var.set(1)
elif path.exists(thcrap_update_dll):
os.rename(thcrap_update_dll, thcrap_update_dll_disabled)
self.updater_var.set(0)
def set_steamintegration(self, enable):
if enable and path.exists(thcrap_steam_dll_disabled):
os.rename(thcrap_steam_dll_disabled, thcrap_steam_dll)
self.steamintegration_var.set(1)
elif path.exists(thcrap_steam_dll):
os.rename(thcrap_steam_dll, thcrap_steam_dll_disabled)
self.steamintegration_var.set(0)
def add_settings(self):
frame = self.settings_frame
for widget in frame.winfo_children():
widget.destroy()
gridargs = {
'ipady': 30,
'pady': 4,
'padx': 10,
'sticky': 'ew',
}
self.updater_var = IntVar(value=1 if path.exists(thcrap_update_dll) else 0)
updater_checkbox = ttk.Checkbutton(
frame,
text='Thcrap Updater',
command=lambda *_: self.set_updater(self.updater_var.get() != 0),
variable=self.updater_var)
updater_checkbox.grid(**gridargs)
self.steamintegration_var = IntVar(value=1 if path.exists(thcrap_steam_dll) else 0)
steamintegration_checkbox = ttk.Checkbutton(
frame,
text='Thcrap Steam Integration',
command=lambda *_: self.set_steamintegration(self.steamintegration_var.get() != 0),
variable=self.steamintegration_var)
steamintegration_checkbox.grid(**gridargs)
self._bugs = IntVar(value=0)
bugs = ttk.Checkbutton(
frame,
text='Disable all bugs',
# state='disabled',
variable=self._bugs,
command=lambda *_: bugs.destroy()
)
bugs.grid(**gridargs)
save_colors = ttk.Button(
frame,
text='Save Colors',
command=lambda *_: self.save_colors())
save_colors.grid(**gridargs)
default_colors = ttk.Button(
frame,
text='Reset Colors',
command=lambda *_: self.reset_colors())
default_colors.grid(**gridargs)
def change_color_factory(color_name):
def change_color():
self.change_color(color_name)
return change_color
color_buttons = []
for color_name, color in self.get_colors().items():
color_buttons.append(
ttk.Button(
frame, style='Setting.TButton',
text=f'Change color {color_name} ({color})',
command=change_color_factory(color_name)))
for i,button in enumerate(color_buttons):
button.grid(**gridargs, column=1, row=i)
for i in range(frame.grid_size()[1]):
frame.rowconfigure(i, weight=1, uniform='a')
for i in range(frame.grid_size()[0]):
frame.columnconfigure(i, weight=1, uniform='a')
def add_bottom_buttons(self):
bottom_buttons = [
ttk.Button(self.mainframe,
text="Thcrap Config",
command=lambda *_: self.run_thcrap()),
ttk.Button(self.mainframe, style="Quit.TButton",
text="Quit",
command=lambda *_: self.quit()),
ttk.Button(self.mainframe, style="Start.TButton",
text="Play",
command=lambda *_: self.start_game()),
]
bottom_button_args = {
'row': 2,
'padx': 10,
'pady': 10,
'ipady': 20,
'sticky': 'nsew',
}
for i,b in enumerate(bottom_buttons):
b.grid(column=i, **bottom_button_args)
self.bottom_buttons = bottom_buttons
bottom_buttons[2].focus()
def run_thcrap(self, *args):
run_thcrap_config()
self.refresh_configs(['no patch', *list_configs()])
def quit(self, *args):
self.root.destroy()
def start_game(self, *args):
config = self.configvar.get()
config_name = self.configs[config]
self.root.destroy()
set_lastrun(config_name)
exec_game(config_name)
def refresh_configs(self, configs=None):
if configs:
self.configs = configs
try:
lastrun = get_lastrun()
self.configvar.set(self.configs.index(lastrun))
except ValueError:
pass
for widget in self.configs_frame.winfo_children():
widget.destroy()
radio_buttons = []
for i,k in enumerate(self.configs):
radio_buttons.append(
ttk.Radiobutton(self.configs_frame,
text=decorate_lang(k),
variable=self.configvar,
value=i))
MIN_ROWS = 2
MAX_ROWS = 6 # Desired. Can exceed.
MIN_COLUMNS = 2
MAX_COLUMNS = 4
# More rows up to MAX_ROWS, then more columns up to
# MAX_COLUMNS, then even more rows.
columns_want = 1 + (len(radio_buttons) - 1) // MAX_ROWS
columns = max(min(columns_want, MAX_COLUMNS), MIN_COLUMNS)
# Create columns with equal width
for i in range(columns):
self.configs_frame\
.columnconfigure(i, weight=1, pad=4, uniform='a')
# Distribute buttons into grid
for (i,r) in enumerate(radio_buttons):
r['style'] = 'Toolbutton'
r.grid(row = i // columns,
column = i % columns,
padx=4, pady=4, sticky='nsew')
# Distribute space among existing rows
rows = max(self.configs_frame.grid_size()[1], MIN_ROWS)
for i in range(rows):
self.configs_frame.rowconfigure(i, weight=1, uniform='a')
configs = ["no patch", "en", "es", "de", "pt", "zh", "kr", "en_troll",
"foo", "bar", "en", "jp"]
if gui:
if TEST:
launcher = Launcher(configs[:2])
launcher.refresh_configs([f'Config {i}\n({i})' for i in range(20)])
else:
try:
check_exe(GAME_EXE)
init_thcrap()
Launcher(['no patch', *list_configs()])
except Exception as e:
tkinter.messagebox.showerror(
title=f'Error in {APP_NAME}',
message=f'{APP_NAME} encountered an error and will now exit.\nError: {e}'
)
else:
check_exe(GAME_EXE)
init_thcrap()
exec_game(list_configs()[0])