-
Notifications
You must be signed in to change notification settings - Fork 10
/
utlauncher.cpp
348 lines (285 loc) · 12.4 KB
/
utlauncher.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
#include "utlauncher.h"
#include "serverbrowser.h"
#include "awesome.h"
#include <QStatusBar>
#include "configdialog.h"
#include <QProgressDialog>
QtAwesome* awesome;
QColor UTLauncher::iconColor() const {
if(getenv("ICON_COLOR")) {
return QColor(getenv("ICON_COLOR"));
} else {
QWidget w;
return w.palette().color(w.foregroundRole());
}
}
UTLauncher::UTLauncher(int& argc, char** argv) :
QApplication(argc, argv),
settings(QSettings::IniFormat, QSettings::UserScope, "CodeCharm", "UTLauncher"),
bootstrap(settings), systemTray(QIcon(":/icon.png")) {
QPixmap pixmap(":/splash.jpg");
splash = new UTSplash(pixmap);
awesome = new QtAwesome(qApp);
awesome->initFontAwesome();
awesome->setDefaultOption( "color" , iconColor() );
awesome->setDefaultOption( "color-active" , iconColor() );
awesome->setDefaultOption( "color-selected" , iconColor() );
splash->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
splash->size(),
qApp->desktop()->availableGeometry()
));
settings.setValue("FirstRun", false);
settings.sync();
bootstrap.start();
connect(&bootstrap, &Bootstrap::message, [=](QString msg) {
splash->showMessage(msg);
});
connect(&bootstrap, &Bootstrap::ready, this, &UTLauncher::prepareConfig);
browser = new ServerBrowser();
connect(&bootstrap, &Bootstrap::serversInfo, this, &UTLauncher::gotServersInfo, Qt::QueuedConnection );
splash->showMessage("Getting version information...");
if(!settings.value("StartMinimized", false).toBool()) {
splash->show();
}
}
void UTLauncher::gotServersInfo(QJsonDocument document)
{
browser->loadFromJson(document.object());
serversRefreshTimer.singleShot(5*60000, &bootstrap, SLOT(refreshServers()));
}
void UTLauncher::prepareConfig()
{
startServerBrowser();
}
void UTLauncher::closeSplash()
{
if(splash) {
splash->deleteLater();
splash = nullptr;
}
}
#include <3rdparty/quazip/quazip/quazip.h>
#include <3rdparty/quazip/quazip/quazipfile.h>
#include <QMessageBox>
void UTLauncher::startServerBrowser()
{
splashTimer.singleShot(2000, this, SLOT(closeSplash()));
if(!settings.value("StartMinimized", false).toBool()) {
browser->show();
} else {
qApp->setQuitOnLastWindowClosed(false); // workaround for app not starting
}
browser->setMOTD(bootstrap.MOTD());
auto hasEditorSupport = [=] {
QString editorPath = bootstrap.editorExePath();
QString projectPath = bootstrap.projectPath();
return QFile::exists(editorPath) && QFile::exists(projectPath);
};
auto openSettings = [=](bool mandatoryEditor = false) {
ConfigDialog dialog(settings, mandatoryEditor);
dialog.exec();
browser->setEditorSupport(hasEditorSupport());
browser->setHideOnClose(settings.value("MinimizeToTrayOnClose").toBool());
};
connect(browser, &ServerBrowser::openServer, [=](QString url, bool spectate, bool inEditor) {
if(inEditor) {
QString editorPath = bootstrap.editorExePath();
QString projectPath = bootstrap.projectPath();
if(!editorPath.length() || !projectPath.length()) {
openSettings(true);
return;
}
QProcess::startDetached(editorPath, QStringList() << projectPath << "-GAME" << (url + (spectate?"?SpectatorOnly=1":"")) );
} else {
QString exePath = bootstrap.programExePath();
if(!exePath.length()) {
openSettings();
return;
}
const auto serverEntry = browser->serverEntryFromAddress(url);
auto launch = [=] {
qDebug() << "Launching!!\n";
QProcess::startDetached(exePath, QStringList()
#ifdef LAUNCH_WITH_UE4
<< "UnrealTournament"
#endif
<< (url + (spectate?"?SpectatorOnly=1":""))
<< "-SaveToUserDir");
};
if(serverEntry) {
if(bootstrap.isStockMap(serverEntry->map)) {
launch();
return;
}
QString exePath = bootstrap.programExePath();
QFileInfo exeInfo(exePath);
auto contentDir = QDir(exeInfo.dir());
contentDir.cdUp();
contentDir.cdUp();
contentDir.cd("Content");
auto zipFilePath = contentDir.absoluteFilePath(serverEntry->map + ".zip");
if(QFile::exists(zipFilePath)) {
launch();
return;
}
Download mapDownload;
mapDownload.setTarget("https://ut.rushbase.net/customcontent/Data/" + serverEntry->map + ".zip");
QProgressDialog dialog("Downloading map: " + serverEntry->map, "Cancel", 0, 100);
QFile zipFile(zipFilePath);
zipFile.open(QIODevice::WriteOnly);
int httpCode = 200;
connect(&mapDownload, &Download::error, [&](int code) {
httpCode = code;
if(code != 200) {
zipFile.remove();
QMessageBox::critical(nullptr, "Unable to download map", QString("Got code %1 while trying to download map:<br>%2").arg(code).arg(serverEntry->map));
}
});
connect(&mapDownload, &Download::chunk, [&](QByteArray chunk) {
qDebug() << "Reading!\n";
zipFile.write(chunk);
});
connect(&mapDownload, &Download::progress, [&](double progress) {
dialog.setValue(100*progress);
if(progress == 1.0) {
dialog.accept();
}
});
mapDownload.download();
qDebug() << "Downloading map " << serverEntry->map;
if(!dialog.exec() || httpCode != 200) {
zipFile.remove(); // remove unfinished download
return;
}
zipFile.close();
QProgressDialog installDialog("Installing map: " + serverEntry->map, "", 0, 100);
installDialog.setWindowModality(Qt::ApplicationModal);
installDialog.show();
installDialog.setValue(0);
qDebug() << "Installed!!\n";
QuaZip zip(zipFilePath);
auto textCodec = QTextCodec::codecForName("TSCII");
zip.setFileNameCodec(textCodec);
zip.setCommentCodec(textCodec);
zip.open(QuaZip::mdUnzip);
QuaZipFile file(&zip);
size_t totalSize = 0;
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile()) {
QuaZipFileInfo info;
zip.getCurrentFileInfo(&info);
totalSize += info.uncompressedSize;
}
qDebug() << "Total size " << totalSize;
size_t accumulatedSize = 0;
/* TODO!!!!: this whole thing needs to be redone asynchronously */
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile()) {
file.open(QIODevice::ReadOnly);
//same functionality as QIODevice::readData() -- data is a char*, maxSize is qint64
//file.readData(data,maxSize);
QString filename = file.getActualFileName();
if(file.isOpen()) {
qApp->processEvents();
qDebug() << installDialog.isVisible() << installDialog.value();
if(!installDialog.isVisible() && installDialog.value() != 100 && installDialog.value() != -1)
return;
bool isDir = (filename.right(1) == "/");
if(isDir) {
contentDir.mkpath(filename);
} else {
QFile f(contentDir.absoluteFilePath(filename));
f.open(QIODevice::WriteOnly);
auto data = file.readAll();
accumulatedSize += data.size();
qDebug() << accumulatedSize;
installDialog.setValue(100 * (double)accumulatedSize / totalSize);
qApp->processEvents();
qDebug() << installDialog.isVisible() << installDialog.value();
if(!installDialog.isVisible() && installDialog.value() != 100 && installDialog.value() != -1)
return;
f.write(data);
}
qDebug() << file.getActualFileName() << isDir;
//do something with the data
if(file.isOpen())
file.close();
qApp->processEvents();
qDebug() << installDialog.isVisible() << installDialog.value();
if(!installDialog.isVisible() && installDialog.value() != 100 && installDialog.value() != -1)
return;
}
else {
qDebug() << "Cannot open" << filename << "inside zip";;
}
}
qDebug() << "Closing zip\n";
zip.close();
launch();
}
}
});
disconnect(&bootstrap, &Bootstrap::ready, this, &UTLauncher::prepareConfig);
connect(&bootstrap, &Bootstrap::ready, this, [=] {
browser->setMOTD(bootstrap.MOTD());
});
connect(browser, &ServerBrowser::openSettings, openSettings);
browser->setEditorSupport(hasEditorSupport());
browser->setHideOnClose(settings.value("MinimizeToTrayOnClose", false).toBool());
systemTray.setToolTip("UTLauncher");
auto systemTrayMenu = new QMenu(browser);
auto showBrowser = new QAction(awesome->icon(fa::listalt), "Server List", this);
connect(showBrowser, &QAction::triggered, [=]() {
browser->showNormal();
browser->raise();
browser->activateWindow();
});
auto runUTAction = new QAction(awesome->icon( fa::gamepad ),"Run UT", this);
connect(runUTAction, &QAction::triggered, [=]() {
QString exePath = bootstrap.programExePath();
if(!exePath.length()) {
browser->show();
openSettings();
return;
}
QProcess::startDetached(exePath
#ifdef LAUNCH_WITH_UE4
, QStringList() << "UnrealTournament" << "-SaveToUserDir"
#endif
);
});
auto runEditorAction = new QAction(awesome->icon( fa::code ),"Run Editor", this);
connect(runEditorAction, &QAction::triggered, [=]() {
QString editorPath = bootstrap.editorExePath();
QString projectPath = bootstrap.projectPath();
QProcess::startDetached(editorPath, QStringList() << projectPath);
});
auto quitAction = new QAction(awesome->icon( fa::signout ),"Quit", this);
connect(quitAction, &QAction::triggered, [=]() {
QApplication::quit();
});
systemTrayMenu->addAction(showBrowser);
systemTrayMenu->addSeparator();
systemTrayMenu->addAction(runUTAction);
systemTrayMenu->addAction(runEditorAction);
systemTrayMenu->addSeparator();
systemTrayMenu->addAction(quitAction);
systemTray.setContextMenu(systemTrayMenu);
systemTray.show();
connect(&systemTray, &QSystemTrayIcon::activated, [=](QSystemTrayIcon::ActivationReason reason) {
qApp->setQuitOnLastWindowClosed(true);
runEditorAction->setVisible(hasEditorSupport());
switch(reason) {
case QSystemTrayIcon::Trigger:
{
if(browser->isHidden()) {
browser->show();
} else {
browser->hide();
}
break;
}
}
});
}