-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher.py
235 lines (213 loc) · 8.8 KB
/
launcher.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
"""Functions to update TTR, then allow the user to login to an account and launch the game."""
import tkinter as tk
from tkinter import filedialog
import json
import os
import time
import requests
import threading
import updater
class TTRLauncher(tk.Frame):
"""Main class for the TTR MultiLauncher"""
def __init__(self, master=None):
while not os.path.exists('installdir.txt'):
installdir = filedialog.askdirectory(title='Select TTR Install Directory')
if installdir is None:
print('Please provide the installation directory.')
continue
with open('installdir.txt', 'a+') as file:
file.write(installdir)
with open('installdir.txt', 'r') as file:
updater.TTRUpdater(file.read())
super().__init__(master)
self.master = master
self.master.title('TTR MultiLauncher')
self.master.minsize(200, 200)
self.create_widgets()
self.pack(expand=True)
self.load_accts()
def create_widgets(self):
self.toonlist = tk.Listbox(self)
self.login_b = tk.Button(self, text='Login', command=self.login)
self.add_acct_b = tk.Button(self, text='Add Account', command=self.add_acct)
self.toonlist.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.login_b.pack(side=tk.LEFT)
self.add_acct_b.pack(side=tk.RIGHT)
self.grid(padx=2, pady=(0, 2))
def load_accts(self):
self.accts = []
with open('accounts.txt', 'a+') as accfile:
accfile.seek(0)
for line in accfile:
line = line.split(',')
if not len(line) == 3:
continue
self.accts.append((line[0], line[1], line[2]))
for acc in self.accts:
self.toonlist.insert(tk.END, acc[0])
def login(self):
selected_indices = self.toonlist.curselection()
if not selected_indices:
print("No account selected.")
return
for index in selected_indices:
acct = self.accts[index]
thread = threading.Thread(target=self.launch_account, args=(acct,))
thread.start()
def launch_account(self, account):
username = account[1].strip()
password = account[2].strip()
data = {'username': username, 'password': password}
self.do_request(data)
def do_request(self, data):
resp = json.loads(requests.post('https://www.toontownrewritten.com/api/login?format=json', data=data, headers={'Content-Type': 'application/x-www-form-urlencoded'}).content)
success = resp.get('success', 'false')
if success == 'true':
os.environ['TTR_PLAYCOOKIE'] = resp.get('cookie', 'CookieNotFound')
os.environ['TTR_GAMESERVER'] = resp.get('gameserver', 'ServerNotFound')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open('installdir.txt', 'r') as file:
os.chdir(file.read())
#Automated relogging if user requests?
if os.path.exists('TTREngine.exe'):
os.system('TTREngine.exe')
elif os.path.exists('TTREngine'):
os.system('./TTREngine')
else:
print('Platform not supported or TTREngine executable missing!')
elif success == 'delayed':
eta = int(resp.get('eta', 30))
pos = resp.get('position', 'unknown')
token = resp.get('queueToken', None)
if token is None:
print("No queue token was returned. This shouldn't be possible! There may be a problem with the TTR login API.")
return
print('Queue position: ' + pos + ' ETA: ' + repr(eta))
time.sleep(min(eta, 30))
self.do_request({'queueToken': token})
elif success == 'partial':
auth_token = resp.get('responseToken', None)
if auth_token is None:
print("A response token was not provided. This shouldn't be possible! There may be a problem with the TTR login API.")
return
tg_code = AuthRequestDialog(self).tg_code
if tg_code is None:
print("You need to provide a ToonGuard code to log in.")
return
self.do_request({'appToken': tg_code, 'authToken': auth_token})
elif success == 'false':
print(resp.get('banner', "Login failed without giving a reason. Check TTR status and try again."))
def add_acct(self):
dialog = AcctRequestDialog(self)
print(dialog.label)
if dialog.label is None:
return
self.accts.append((dialog.label, dialog.username, dialog.password))
self.toonlist.insert(tk.END, dialog.label)
self.toonlist.pack(side=tk.TOP, fill=tk.BOTH)
with open('accounts.txt', 'a+') as accfile:
accfile.write(dialog.label + ',' + dialog.username + ',' + dialog.password + '\n')
class AcctRequestDialog(tk.Toplevel):
"""Simple class to request account information"""
def __init__(self, parent, title=None):
tk.Toplevel.__init__(self, parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = tk.Frame(self)
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
self.grab_set()
self.label = None
self.username = None
self.password = None
if not self.initial_focus:
self.initial_focus = self
self.protocol('WM_DELETE_WINDOW', self.cancel)
self.geometry('+%d+%d' % (parent.winfo_rootx()+50, parent.winfo_rooty()+50))
self.initial_focus.focus_set()
self.wait_window(self)
def body(self, master):
tk.Label(master, text='Label:').grid(row=0)
tk.Label(master, text='Username:').grid(row=1)
tk.Label(master, text='Password:').grid(row=2)
self.label_entry = tk.Entry(master)
self.user_entry = tk.Entry(master)
self.pass_entry = tk.Entry(master)
self.label_entry.grid(row=0, column=1)
self.user_entry.grid(row=1, column=1)
self.pass_entry.grid(row=2, column=1)
box = tk.Frame(self)
tk.Button(box, text='OK', width=10, command=self.confirm, default=tk.ACTIVE).pack(side=tk.LEFT)
tk.Button(box, text='Cancel', width=10, command=self.cancel).pack(side=tk.LEFT)
box.pack(side=tk.BOTTOM, padx=5, pady=5)
self.bind('<Return>', self.confirm)
self.bind('<Escape>', self.cancel)
return self.label_entry
def confirm(self, event=None):
if not self.validate():
self.initial_focus.focus_set()
return
self.withdraw()
self.update_idletasks()
self.apply()
self.cancel()
def cancel(self, event=None):
self.parent.focus_set()
self.destroy()
def validate(self):
return 1
def apply(self):
self.label = self.label_entry.get()
self.username = self.user_entry.get()
self.password = self.pass_entry.get()
class AuthRequestDialog(tk.Toplevel):
"""Simple class to request ToonGuard auth code"""
def __init__(self, parent, title=None):
tk.Toplevel.__init__(self, parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = tk.Frame(self)
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
self.grab_set()
self.tg_code = None
if not self.initial_focus:
self.initial_focus = self
self.protocol('WM_DELETE_WINDOW', self.cancel)
self.geometry('+%d+%d' % (parent.winfo_rootx()+50, parent.winfo_rooty()+50))
self.initial_focus.focus_set()
self.wait_window(self)
def body(self, master):
tk.Label(master, text='ToonGuard Code:').grid(row=0)
self.tg_entry = tk.Entry(master)
self.tg_entry.grid(row=0, column=1)
box = tk.Frame(self)
tk.Button(box, text='OK', width=10, command=self.confirm, default=tk.ACTIVE).pack(side=tk.LEFT)
tk.Button(box, text='Cancel', width=10, command=self.cancel).pack(side=tk.LEFT)
box.pack(side=tk.BOTTOM, padx=5, pady=5)
self.bind('<Return>', self.confirm)
self.bind('<Escape>', self.cancel)
return self.tg_entry
def confirm(self, event=None):
if not self.validate():
self.initial_focus.focus_set()
return
self.withdraw()
self.update_idletasks()
self.apply()
self.cancel()
def cancel(self, event=None):
self.parent.focus_set()
self.destroy()
def validate(self):
return 1
def apply(self):
self.tg_code = self.tg_entry.get()
if __name__ == '__main__':
ROOT = tk.Tk()
APP = TTRLauncher(master=ROOT)
APP.mainloop()