forked from tf2classic/TF2CDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
121 lines (108 loc) · 3.93 KB
/
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
"""
UX-related functions, like showing messages,
asking questions, generally handling any sort
of communication or interactivity with the user.
"""
from os import environ, makedirs, path, rmdir
from sys import exit
from time import sleep
from gettext import gettext as _
from rich import print
import vars
import downloads
import versions
import troubleshoot
def message(msg: str, delay: int = 0) -> None:
"""
Show a message to user.
Delay stops program for specified amount of seconds.
"""
print("[bold yellow]" + msg)
if not vars.SCRIPT_MODE:
sleep(delay)
def main_menu():
print(_("""Welcome to PF2Downloader. Enter a number to continue.\n
1 - Install or reinstall the game
2 - Check for and apply any available updates
3 - Verify and repair game files"""))
user_choice = input()
if user_choice == '1':
message(_("Starting the download for Pre-Fortress 2... You may see some errors that are safe to ignore."), 3)
downloads.install()
troubleshoot.apply_blacklist()
message_end(_("The installation has successfully completed. Remember to restart Steam!"), 0)
elif user_choice == '2':
if versions.check_for_updates():
downloads.update()
message_end(_("The update has successfully completed."), 0)
else:
message(_("Starting the download for Pre-Fortress 2... You may see some errors that are safe to ignore."), 3)
downloads.install()
troubleshoot.apply_blacklist()
message_end(_("The installation has successfully completed. Remember to restart Steam!"), 0)
elif user_choice == '3':
version_json = versions.get_version_list()["versions"]
downloads.butler_verify(vars.SOURCE_URL + version_json[versions.get_installed_version()]["signature"], vars.INSTALL_PATH + '/pf2', vars.SOURCE_URL + version_json[versions.get_installed_version()]["heal"])
message_end(_("The verification process has completed, and any corruption has been repaired."), 0)
else:
message(_("Invalid choice. Please retry."))
main_menu()
def message_yes_no(msg: str, default: bool = None, script_mode_default_override: bool = None) -> bool:
"""
Show a message to user and get yes/no answer.
"""
if vars.SCRIPT_MODE:
# Display msg even though we are in script mode, this because it might
# contain useful information.
print(msg)
print(_("The application is in script mode, using default choice."))
if script_mode_default_override is not None:
return script_mode_default_override
return default
valid = {"yes": True, "no": False, "y": True, "n": False}
localyes = _("yes")
localno = _("no")
valid[localyes] = True
valid[localyes[0]] = True
valid[localno] = False
valid[localno[0]] = False
prompt = _(" {y/n}")
if default:
prompt = _(" {Y/n}")
elif default is not None:
prompt = _(" {y/N}")
msg += prompt
while True:
print(msg)
choice = input().lower()
if default is not None and choice == "":
return default
if choice in valid:
return valid[choice]
print(_("[bold blue]Please respond with 'yes' or 'no' (or 'y' or 'n').[/bold blue]"))
def message_dir(msg: str) -> str:
"""
Show a message and ask for a directory.
"""
while True:
dir = input(msg + ": ")
if dir.count("~") > 0:
dir = path.expanduser(dir)
if dir.count("$") > 0:
dir = path.expandvars(dir)
if path.isdir(dir):
return dir
try:
makedirs(dir)
rmdir(dir)
return dir
except Exception:
pass
def message_end(msg: str, code: int) -> None:
"""
Show a message and exit.
"""
print("[bold green]" + msg)
if not vars.SCRIPT_MODE:
input(_("Press Enter to exit."))
exit(code)