-
Notifications
You must be signed in to change notification settings - Fork 9
/
launcherwindow.cpp
408 lines (333 loc) · 11.6 KB
/
launcherwindow.cpp
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
/*
* Copyright (c) 2015-2016 Joshua Snyder
* Distributed under the GNU GPL v3. For full terms see the file LICENSE
*
* This file is part of Shticker Book Rewritten.
*
* Shticker Book Rewritten is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shticker Book Rewritten is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Shticker Book Rewritten. If not, see <http://www.gnu.org/licenses/>.
*/
#include "launcherwindow.h"
#include "ui_launcherwindow.h"
#include "globaldefines.h"
#include "updateworker.h"
#include "filelocationchooser.h"
#include <QDir>
#include <QMessageBox>
#include <QCloseEvent>
#include <QProcess>
#include <QThread>
#include <QSettings>
#include <QEventLoop>
LauncherWindow::LauncherWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::LauncherWindow)
{
gameInstances = 0;
ui->setupUi(this);
connect(this, SIGNAL(enableUpdate(bool)), ui->updateButton, SLOT(setEnabled(bool)));
connect(ui->updatesCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleAutoUpdates()));
connect(ui->updateButton, SIGNAL(clicked(bool)), this, SLOT(updateFiles()));
//read the previous settings
readSettings();
//check if the user has already chosen a file location for the game files
while(filePath == "/")
{
setFilePath();
}
//setup saved toons
ui->savedToonsBox->addItem("Saved logins");
ui->savedToonsBox->addItems(savedUsers);
connect(ui->savedToonsBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(fillCredentials(QString)));
//setup the webviews
ui->newsWebview->setUrl(QUrl("https://www.toontownrewritten.com/news/launcher"));
ui->fishWebview->setUrl(QUrl("http://siggen.toontown-click.de/fishadvisor/en/fishes.html"));
ui->invasionsWebview->setUrl(QUrl("http://toonhq.org/invasions/"));
ui->groupsWebview->setUrl(QUrl("http://toonhq.org/groups/"));
//change news view to a dark background since text is white
connect(ui->newsWebview->page(), SIGNAL(loadFinished(bool)), this, SLOT(newsViewLoaded()));
//disable login until files are updated
emit enableLogin(false);
loginIsReady = false;
if(autoUpdate)
{
updateFiles();
}
else
{
loginReady();
ui->progressBar->hide();
}
}
LauncherWindow::~LauncherWindow()
{
delete ui;
}
void LauncherWindow::relayMessage(QString message)
{
emit sendMessage(message);
}
void LauncherWindow::relayProgressBarReceived(int receivedBytes)
{
emit sendProgressBarReceived(receivedBytes);
}
void LauncherWindow::relayShowProgressBar()
{
emit showProgressBar();
}
void LauncherWindow::relayHideProgressBar()
{
emit hideProgressBar();
}
void LauncherWindow::updatesReady()
{
if(gameInstances == 0)
{
emit enableUpdate(true);
}
}
void LauncherWindow::loginReady()
{
emit enableLogin(true);
loginIsReady = true;
emit sendMessage("Logins are ready!");
}
void LauncherWindow::initiateLogin()
{
if(loginIsReady)
{
qDebug() << "Initiating login sequence\n";
//disable login again to prevent duplicate logins
emit enableLogin(false);
loginIsReady = false;
loginWorker = new LoginWorker(this);
connect(loginWorker, SIGNAL(sendMessage(QString)), this, SLOT(relayMessage(QString)));
connect(loginWorker, SIGNAL(gameStarted()), this, SLOT(gameHasStarted()));
connect(loginWorker, SIGNAL(gameFinished(int, QByteArray)), this, SLOT(gameHasFinished(int, QByteArray)));
connect(loginWorker, SIGNAL(authenticationFailed()), this, SLOT(authenticationFailed()));
//start login and then the game
loginWorker->initiateLogin(ui->usernameBox->text(), ui->passwordBox->text());
}
else
{
qDebug() << "Login isn't ready, ignoring login event\n";
}
}
void LauncherWindow::gameHasStarted()
{
//disable updates while an instance is running
emit enableUpdate(false);
//check whether to save the credentials or not
if(ui->saveCredentialsBox->isChecked())
{
//check to see if it already exists
if(savedUsers.contains(ui->usernameBox->text()))
{
int i;
i = savedUsers.indexOf(ui->usernameBox->text());
//replace the password for this username
savedPasses.replace(i, ui->passwordBox->text());
}
else
{
savedUsers.append(ui->usernameBox->text());
savedPasses.append(ui->passwordBox->text());
ui->savedToonsBox->addItem(ui->usernameBox->text());
}
//uncheck the box now
ui->saveCredentialsBox->setChecked(false);
writeSettings();
}
//clear the username and password boxes to prevent accidental relaunching of the game and to be ready to launch another
ui->usernameBox->clear();
ui->passwordBox->clear();
ui->savedToonsBox->setCurrentIndex(0);
//increment to show how many instances are running
gameInstances++;
qDebug() << "New game instance, there are now" << gameInstances;
//enable login again now that the game has finished starting
loginReady();
}
void LauncherWindow::gameHasFinished(int exitCode, QByteArray gameOutput)
{
//increment to show how many instances are running
gameInstances--;
qDebug() << "Game instance has closed, there are now" << gameInstances << "Exit code is:" << exitCode;
if(exitCode != 0)
{
qDebug() << "TTR has crashed. Output from the engine is:" << gameOutput;
emit sendMessage("Looks like Toontown Rewritten has crashed.");
QMessageBox::warning(this, "Toontown Rewritten has crashed.",
"Looks like Toontown Rewritten has crashed. The engine's error message is:\n" + gameOutput,
QMessageBox::Ok);
}
//re-enable updates (checks to see if no other instances are running as well)
updatesReady();
}
void LauncherWindow::authenticationFailed()
{
emit enableLogin(true);
loginIsReady = true;
}
//confirm closing of the launcher to alert of closing all running games
void LauncherWindow::closeEvent(QCloseEvent *event)
{
//check if any game instances are running
if(gameInstances!=0)
{
//create a dialog box to confirm exit and warn about running instances
QMessageBox::StandardButton dialog;
dialog = QMessageBox::warning(this,
"Please confirm closing.",
"Are you sure you would like to close? Closing the launcher when any game instance is running will cause it to close. There are currently "
+ QString::number(gameInstances) + " instances running.",
QMessageBox::Yes | QMessageBox::No);
if( dialog == QMessageBox::Yes)
{
//save the current settings before quitting
writeSettings();
exit(0);
}
else
{
event->ignore();
}
}
//no running instances so we can just close
else
{
//save the current settings before quitting
writeSettings();
exit(0);
}
}
void LauncherWindow::newsViewLoaded()
{
ui->newsWebview->page()->runJavaScript(QString("document.body.style.backgroundColor = \"#141618\";"));
}
void LauncherWindow::toggleAutoUpdates()
{
if(ui->updatesCheckBox->isChecked())
{
autoUpdate = true;
}
else
{
autoUpdate = false;
}
writeSettings();
}
void LauncherWindow::writeSettings()
{
QSettings settings("Shticker-Book-Rewritten", "Shticker-Book-Rewritten");
settings.beginGroup("LauncherWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.setValue("update", autoUpdate);
settings.endGroup();
settings.beginGroup("Logins");
settings.setValue("username", savedUsers);
settings.setValue("pass", savedPasses);
settings.endGroup();
}
void LauncherWindow::readSettings()
{
QSettings settings("Shticker-Book-Rewritten", "Shticker-Book-Rewritten");
settings.beginGroup("LauncherWindow");
resize(settings.value("size", QSize(400, 400)).toSize());
move(settings.value("pos", QPoint(200, 200)).toPoint());
autoUpdate = settings.value("update", true).toBool();
settings.endGroup();
settings.beginGroup("Logins");
savedUsers = settings.value("username").toStringList();
savedPasses = settings.value("pass").toStringList();
settings.endGroup();
if(autoUpdate)
{
ui->updatesCheckBox->setChecked(true);
}
readSettingsPath();
}
void LauncherWindow::readSettingsPath()
{
QSettings settings("Shticker-Book-Rewritten", "Shticker-Book-Rewritten");
settings.beginGroup("FilesPath");
filePath = settings.value("path").toString();
settings.endGroup();
filePath = filePath + QString("/");
cachePath = filePath + QString(".cache/");
}
void LauncherWindow::fillCredentials(QString username)
{
if(username == "Saved logins")
{
ui->usernameBox->clear();
ui->passwordBox->clear();
return;
}
else
{
int i;
i = savedUsers.indexOf(username);
ui->usernameBox->setText(username);
ui->passwordBox->setText(savedPasses.at(i));
}
}
void LauncherWindow::updateFiles()
{
ui->progressBar->show();
emit enableUpdate(false);
//check to make sure the cache directory exists and make it if it doesn't
if(!QDir(filePath).exists())
{
QDir().mkdir(filePath);
}
if(!QDir(cachePath).exists())
{
QDir().mkdir(cachePath);
}
//Begin updating the game files
updateThread = new QThread(this);
UpdateWorker *updateWorker = new UpdateWorker();
//make a new thread for the updating process since it can bog down the main thread and make the window unresponsive
updateWorker->moveToThread(updateThread);
connect(updateThread, SIGNAL(started()), updateWorker, SLOT(startUpdating()));
connect(updateWorker, SIGNAL(updateComplete()), updateThread, SLOT(quit()));
//allow the update worker to communicate with the main window
connect(updateWorker, SIGNAL(sendMessage(QString)), this, SLOT(relayMessage(QString)));
connect(updateWorker, SIGNAL(sendProgressBarReceived(int)), this, SLOT(relayProgressBarReceived(int)));
connect(updateWorker, SIGNAL(showProgressBar()), this, SLOT(relayShowProgressBar()));
connect(updateWorker, SIGNAL(hideProgressBar()), this, SLOT(relayHideProgressBar()));
connect(updateWorker, SIGNAL(updateComplete()), this, SLOT(loginReady()));
connect(updateWorker, SIGNAL(updateComplete()), this, SLOT(updatesReady()));
updateThread->start();
}
void LauncherWindow::changeFilePath()
{
//disable login until files are updated
emit enableLogin(false);
loginIsReady = false;
setFilePath();
updateFiles();
}
void LauncherWindow::setFilePath()
{
FileLocationChooser *chooser = new FileLocationChooser;
chooser->show();
chooser->activateWindow();
//wait until a path is chosen
QEventLoop waitForPath;
connect(chooser, SIGNAL(finished()), &waitForPath, SLOT(quit()));
connect(chooser, SIGNAL(rejected()), &waitForPath, SLOT(quit()));
waitForPath.exec();
chooser->deleteLater();
readSettingsPath();
}