This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher_handler.py
101 lines (84 loc) · 3.71 KB
/
launcher_handler.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
from threading import Thread
import tkinter.font as font
import tkinter.ttk as ttk
from requests import get
from json import load
from tkinter import *
import logging
def log_message(level, message):
logging.basicConfig(format='[%(asctime)s %(process)d %(levelname)s] %(message)s')
logging.getLogger().setLevel(logging.INFO)
logging.log(level, message)
def get_versions():
versions = []
data = load(open("./launcher.json"))
for version in data["versions"]:
versions.append(version)
reversed_versions = [entry for entry in reversed(versions)]
return reversed_versions
def error_window(parent, error, description):
def error_window_exit(window):
log_message(logging.WARNING, "Error dismissed")
window.destroy()
window.update()
log_message(logging.ERROR, error)
window = Toplevel(parent)
window.geometry("200x100")
window.title("Error!")
window.iconbitmap("./favicon.ico")
window.resizable(False, False)
Label(window, text=f"Error: {error.title()}!\n", fg="red", font=font.Font(size=10, weight="bold")).pack()
Label(window, text=f"{description}\n", fg="black").pack()
ttk.Button(window, text="Dismiss", width=14, command=lambda: error_window_exit(window)).pack()
def download_window(parent, version):
def download_window_exit(window):
log_message(logging.INFO, "Download completed")
window.destroy()
window.update()
log_message(logging.INFO, "Download started")
window = Toplevel(parent)
window.geometry("225x100")
window.title("0% Done")
window.iconbitmap("./favicon.ico")
window.resizable(False, False)
title = Label(window, text=f"Downloading Terraria {version}...", font=font.Font(size=10, weight="bold"))
title.pack()
progressbar = ttk.Progressbar(window, orient=HORIZONTAL, length=180)
progressbar.pack(padx=5, pady=15)
button = ttk.Button(window, text="Cancel", width=14, command=lambda: download_window_exit(window))
button.pack()
Thread(target=download_version, args=(window, version, title, progressbar, button)).start()
def download_version(window, version, title, progressbar, button):
def close_program():
window.destroy()
def disable_event():
log_message(logging.WARNING, "Discarded WM_DELETE_WINDOW protocol")
pass
try:
window.protocol("WM_DELETE_WINDOW", disable_event)
button.config(state="disabled")
with open(f"./versions/{version}.zip", 'wb') as f:
response = get(f"https://archive.org/download/Terraria-DE-Archive/{version[0:3]}/{version}.zip", stream=True)
total = response.headers.get('content-length')
if total is None:
f.write(response.content)
else:
downloaded = 0
total = int(total)
for data in response.iter_content(chunk_size=max(int(total/1000), 1024*1024)):
downloaded += len(data)
f.write(data)
done = int(100*downloaded/total)
progressbar['value'] = done
log_message(logging.INFO, f"Download progress: {done}%")
window.title(f"{done}% Done")
if done == 100:
window.title("Complete")
button.config(text="Done")
except:
log_message(logging.ERROR, f"Download failed")
window.title("Failed")
title.config(text="Download Failed!", fg="red")
button.config(text="Cancel")
window.protocol("WM_DELETE_WINDOW", close_program)
button.config(state="normal")