-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathurtupdater.cpp
More file actions
1288 lines (1052 loc) · 42 KB
/
urtupdater.cpp
File metadata and controls
1288 lines (1052 loc) · 42 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Urban Terror Updater
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @version 4.0.3
* @author Charles 'Barbatos' Duprey
* @email barbatos@urbanterror.info
* @copyright 2013-2023 Frozensand Games Limited
*/
#include "urtupdater.h"
#include "ui_urtupdater.h"
UrTUpdater::UrTUpdater(QWidget *parent) : QMainWindow(parent), ui(new Ui::UrTUpdater)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_QuitOnClose);
updaterVersion = URT_UPDATER_VERSION;
changelog = CHANGELOG_EMPTY_TEXT;
password = "";
downloadServer = -1;
gameEngine = -1;
currentVersion = -1;
askBeforeUpdating = -1;
nbFilesToDl = 0;
nbFilesDled = 0;
readyToProcess = false;
configFileExists = false;
threadStarted = false;
updateInProgress = false;
firstLaunch = false;
QStringList arguments = QCoreApplication::arguments();
if (arguments.count() >= 1) {
for (int i = 0; i < arguments.count(); i++) {
if (arguments.at(i) == "--password" && (i + 1 < arguments.count()) ) {
i++;
password = arguments.at( i );
}
}
}
QMenu *menuFile = menuBar()->addMenu("&File");
QMenu *menuHelp = menuBar()->addMenu("&Help");
QAction *actionSettings = menuFile->addAction("&Settings");
connect(actionSettings, SIGNAL(triggered()), this, SLOT(openSettings()));
QAction *actionChangelog = menuFile->addAction("&Changelog");
connect(actionChangelog, SIGNAL(triggered()), this, SLOT(openChangelogPage()));
QAction *actionAbout = menuHelp->addAction("&About");
connect(actionAbout, SIGNAL(triggered()), this, SLOT(openAboutPage()));
QAction *actionHelp = menuHelp->addAction("&Get help");
connect(actionHelp, SIGNAL(triggered()), this, SLOT(openHelpPage()));
actionHelp->setShortcut(QKeySequence("Ctrl+H"));
QAction *actionQuitter = menuFile->addAction("&Quit");
connect(actionQuitter, SIGNAL(triggered()), this, SLOT(close()));
actionQuitter->setShortcut(QKeySequence("Ctrl+Q"));
dlText = new QLabel(this);
dlText->move(150, 222);
dlText->setStyleSheet("color:white;");
dlText->setMinimumWidth(450);
dlText->setText("Getting information from the API...");
dlText->show();
currentChecksum = new QLabel(this);
currentChecksum->move(150, 270);
currentChecksum->setStyleSheet("color:white;");
currentChecksum->setMinimumWidth(450);
currentChecksum->setText("");
currentChecksum->hide();
globalDlText = new QLabel(this);
globalDlText->move(150, 273);
globalDlText->setStyleSheet("color:white;");
globalDlText->setMinimumWidth(450);
globalDlText->setText("Overall Progress:");
globalDlText->hide();
dlSpeed = new QLabel(this);
dlSpeed->move(150, 318);
dlSpeed->setStyleSheet("color:white;");
dlSpeed->setMinimumWidth(200);
dlSpeed->hide();
dlSize = new QLabel(this);
dlSize->move(470, 318);
dlSize->setStyleSheet("color:white;");
dlSize->setMinimumWidth(150);
dlSize->hide();
dlBar = new QProgressBar(this);
dlBar->move(150, 250);
dlBar->setFixedWidth(485);
dlBar->setFixedHeight(20);
dlBar->setRange(0, 100);
dlBar->show();
globalDlBar = new QProgressBar(this);
globalDlBar->move(150, 301);
globalDlBar->setFixedHeight(20);
globalDlBar->hide();
if (!menuBar()->isNativeMenuBar()) {
dlBar->setFixedWidth(485);
globalDlBar->setFixedWidth(485);
}
else {
dlBar->setFixedWidth(450);
globalDlBar->setFixedWidth(450);
}
playButton = new QPushButton(this);
playButton->move(400, 385);
playButton->setMinimumWidth(200);
playButton->setMinimumHeight(50);
playButton->setText("Updating...");
playButton->setStyleSheet("padding-bottom: 2px; color: white;font-weight: bold; font-size: 120%; text-transform: uppercase;background-color:#727272;height:50px;");
playButton->setIconSize(QSize(40, 40));
playButton->show();
changelogButton = new QPushButton(this);
changelogButton->move(150, 385);
changelogButton->setMinimumWidth(200);
changelogButton->setMinimumHeight(50);
changelogButton->setStyleSheet("padding-bottom: 2px; color: white;font-weight: bold; font-size: 120%; text-transform: uppercase;background-color:#727272;height:50px;");
changelogButton->setText("Changelog");
changelogButton->hide();
loaderAnim = new QMovie(":/images/urt_updating.gif");
connect(loaderAnim, SIGNAL(frameChanged(int)), this, SLOT(setLoadingIcon(int)));
playAnim = new QMovie(":/images/urt_play.gif");
connect(playAnim, SIGNAL(frameChanged(int)), this, SLOT(setPlayIcon(int)));
loaderAnim->start();
connect(playButton, SIGNAL(clicked()), this, SLOT(launchGame()));
connect(changelogButton, SIGNAL(clicked()), this, SLOT(openChangelogPage()));
connect(this, SIGNAL(checkingChanged(int, QString)), this, SLOT(updateCheckStatus(int, QString)));
connect(this, SIGNAL(requestNewDlLabel(QString)), this, SLOT(updateDlLabel(QString)));
if (!init()) {
QTimer::singleShot(0, this, SLOT(close()));
}
}
bool UrTUpdater::init()
{
updaterPath = getCurrentPath();
// Check if this is the first launch of the updater
if (!QFile::exists(updaterPath + URT_GAME_SUBDIR)) {
QMessageBox msg;
int result;
firstLaunch = true;
// OSX: do not allow trying to launch the Updater from its .dmg disk image
if (updaterPath.startsWith("/Volumes/")) {
folderError(updaterPath + URT_GAME_SUBDIR);
return false;
}
// OSX: Staring the updater in /Applications/ will be a mess.
// We'll ask the user if he allows us to move in a subdir called UrbanTerror.
if (updaterPath == "/Applications/") {
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setIcon(QMessageBox::Information);
msg.setText("You can't run me in the /Applications/ folder. \n\nDo you allow me to create a subfolder called UrbanTerror in /Applications/ and move myself into it ?");
result = msg.exec();
// User don't want us to move. But we don't want to mess inside the /Applications/ folder, so we quit.
if (result == QMessageBox::Cancel) {
return false;
}
if (!QDir().mkdir("/Applications/UrbanTerror/")) {
QMessageBox::critical(this, "Can't create subfolder in /Applications/", "I can't create the folder \"/Applications/UrbanTerror/\"\n\nIt's probably because the folder already exists or I do not have sufficient permissions to create it." );
return false;
}
if (!QDir().rename(getBundlePath(), "/Applications/UrbanTerror/UrTUpdater.app")) {
QMessageBox::critical(this, "Can't move the updater to /Applications/UrbanTerror/", "I failed to move myself into the UrbanTerror subfolder." );
return false;
}
// Now we exec the updater from the new location and we can leave, gracefuly
QProcess::startDetached("open", QStringList("/Applications/UrbanTerror/UrTUpdater.app"));
return false;
}
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setIcon(QMessageBox::Information);
msg.setText("The game " URT_GAME_NAME " will be installed in this path:\n\n" + updaterPath + "\n\n"
+ "To change the installation path, please click on \"Cancel\" and copy this Updater to where you want it to be installed.");
result = msg.exec();
// If we want to quit
if (result == QMessageBox::Cancel) {
return false;
}
}
parseLocalConfig();
getManifest("versionInfo");
return true;
}
QString UrTUpdater::getPlatform()
{
#ifdef Q_OS_MAC
return "Mac";
#endif
#ifdef Q_OS_LINUX
if (QSysInfo::WordSize == 64) {
return "Linux64";
}
else {
return "Linux32";
}
#endif
#ifdef Q_OS_WIN32
return "Windows";
#endif
return "Linux32";
}
QString UrTUpdater::getCurrentPath()
{
QDir dir = QDir(QCoreApplication::applicationDirPath());
// If we're on Mac, 'dir' will contain the path to the executable
// inside of the Updater's bundle which isn't what we want.
// We need to cd ../../..
if (getPlatform() == "Mac") {
dir.cdUp();
dir.cdUp();
dir.cdUp();
}
return dir.absolutePath() + "/";
}
QString UrTUpdater::getBundlePath()
{
QDir dir = QDir(QCoreApplication::applicationDirPath());
// If we're on Mac, 'dir' will contain the path to the executable
// inside of the Updater's bundle which isn't what we want.
// We need to cd ../..
if (getPlatform() == "Mac") {
dir.cdUp();
dir.cdUp();
}
return dir.absolutePath() + "/";
}
void UrTUpdater::parseLocalConfig()
{
QDomDocument *dom = new QDomDocument();
QFile *f = new QFile(updaterPath + URT_UPDATER_CFG);
if (!f->open(QFile::ReadOnly)) {
delete f;
delete dom;
configFileExists = false;
return;
}
dom->setContent(f);
QDomNode node = dom->firstChild();
while (!node.isNull()) {
if (node.toElement().nodeName() == "UpdaterConfig") {
QDomNode conf = node.firstChild();
while (!conf.isNull()) {
if (conf.toElement().nodeName() == "DownloadServer") {
downloadServer = conf.toElement().text().toInt();
}
if (conf.toElement().nodeName() == "GameEngine") {
gameEngine = conf.toElement().text().toInt();
}
if (conf.toElement().nodeName() == "CurrentVersion") {
currentVersion = conf.toElement().text().toInt();
}
if (conf.toElement().nodeName() == "AskBeforeUpdating") {
askBeforeUpdating = conf.toElement().text().toInt();
}
conf = conf.nextSibling();
}
}
node = node.nextSibling();
}
configFileExists = true;
f->close();
delete f;
delete dom;
}
void UrTUpdater::saveLocalConfig()
{
QFile *f = new QFile(updaterPath + URT_UPDATER_CFG);
QXmlStreamWriter *xml = new QXmlStreamWriter();
if (!f->open(QFile::WriteOnly)) {
QMessageBox::critical(0, "Could not write the config file", "Could not write the Updater config file inside of the game folder. Your updater preferences won't be saved.");
delete f;
return;
}
xml->setDevice(f);
xml->writeStartDocument();
xml->writeStartElement("UpdaterConfig");
xml->writeStartElement("DownloadServer");
xml->writeCharacters(QString::number(downloadServer));
xml->writeEndElement();
xml->writeStartElement("GameEngine");
xml->writeCharacters(QString::number(gameEngine));
xml->writeEndElement();
xml->writeStartElement("CurrentVersion");
xml->writeCharacters(QString::number(currentVersion));
xml->writeEndElement();
xml->writeStartElement("AskBeforeUpdating");
xml->writeCharacters(QString::number(askBeforeUpdating));
xml->writeEndElement();
xml->writeEndElement();
xml->writeEndDocument();
f->close();
// qDebug() << "Local config saved." << endl;
delete f;
delete xml;
}
void UrTUpdater::getManifest(QString query)
{
QUrl APIUrl(URT_API_LINK);
QUrlQuery url;
QNetworkRequest apiRequest(APIUrl);
QNetworkAccessManager *apiManager = new QNetworkAccessManager(this);
apiRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=utf-8");
url.addQueryItem("platform", getPlatform());
url.addQueryItem("query", query);
url.addQueryItem("password", password);
url.addQueryItem("version", QString::number(currentVersion));
url.addQueryItem("engine", QString::number(gameEngine));
url.addQueryItem("server", QString::number(downloadServer));
url.addQueryItem("updaterVersion", updaterVersion);
QNetworkProxyFactory::setUseSystemConfiguration(true);
apiAnswer = apiManager->post(apiRequest, url.query(QUrl::FullyEncoded).toUtf8());
connect(apiAnswer, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(setDLValueP(qint64, qint64)));
connect(apiAnswer, SIGNAL(finished()), this, SLOT(parseAPIAnswer()));
connect(apiAnswer, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(networkError(QNetworkReply::NetworkError)));
}
void UrTUpdater::parseAPIAnswer()
{
QByteArray apiByteAnswer = apiAnswer->readAll();
QString apiData = QString(apiByteAnswer);
currentChecksum->show();
QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
connect(watcher, SIGNAL(finished()), this, SLOT(work()));
QFuture<void> parser = QtConcurrent::run(&UrTUpdater::parseManifest, this, apiData);
watcher->setFuture(parser);
}
void UrTUpdater::updateDlLabel(QString label)
{
dlText->setText(label);
}
void UrTUpdater::parseManifest(QString data)
{
QDomDocument* dom = new QDomDocument();
dom->setContent(data);
packsList.clear();
filesToDownload.clear();
downloadServers.clear();
enginesList.clear();
versionsList.clear();
newsList.clear();
QDomNode node = dom->firstChild();
emit requestNewDlLabel("Parsing the answer of the API...");
while (!node.isNull()) {
if (node.toElement().nodeName() == "Updater") {
QDomNode updater = node.firstChild();
while (!updater.isNull()) {
if (updater.toElement().nodeName() == "APIVersion") {
apiVersion = updater.toElement().text();
}
if (updater.toElement().nodeName() == "Changelog") {
changelog = updater.toElement().text();
}
if (updater.toElement().nodeName() == "Licence") {
licenceText = updater.toElement().text();
}
if (updater.toElement().nodeName() == "NewsList") {
QDomNode newsListNode = updater.firstChild();
while (!newsListNode.isNull()) {
if (newsListNode.nodeName() == "NewsText") {
newsList.append(newsListNode.toElement().text());
}
newsListNode = newsListNode.nextSibling();
}
}
else if (updater.toElement().nodeName() == "ServerList") {
QDomNode serverListNode = updater.firstChild();
while (!serverListNode.isNull()) {
if (serverListNode.nodeName() == "Server") {
QDomNode serverNode = serverListNode.firstChild();
int serverId = -1;
QString serverURL;
QString serverName;
QString serverLocation;
serverInfo_s si;
while (!serverNode.isNull()) {
if (serverNode.nodeName() == "ServerName") {
serverName = serverNode.toElement().text();
}
if (serverNode.nodeName() == "ServerURL") {
serverURL = serverNode.toElement().text();
}
if (serverNode.nodeName() == "ServerLocation") {
serverLocation = serverNode.toElement().text();
}
if (serverNode.nodeName() == "ServerId") {
serverId = serverNode.toElement().text().toInt();
}
serverNode = serverNode.nextSibling();
}
si.serverId = serverId;
si.serverName = serverName;
si.serverURL = serverURL;
si.serverLocation = serverLocation;
downloadServers.append(si);
}
serverListNode = serverListNode.nextSibling();
}
}
else if (updater.toElement().nodeName() == "EngineList") {
QDomNode engineListNode = updater.firstChild();
while (!engineListNode.isNull()) {
if (engineListNode.nodeName() == "Engine") {
QDomNode engineNode = engineListNode.firstChild();
int engineId = -1;
QString engineDir;
QString engineName;
QString engineLaunchString;
engineInfo_s ei;
while (!engineNode.isNull()) {
if (engineNode.nodeName() == "EngineName") {
engineName = engineNode.toElement().text();
}
if (engineNode.nodeName() == "EngineDir") {
engineDir = engineNode.toElement().text();
}
if (engineNode.nodeName() == "EngineId") {
engineId = engineNode.toElement().text().toInt();
}
if (engineNode.nodeName() == "EngineLaunchString") {
engineLaunchString = engineNode.toElement().text();
}
engineNode = engineNode.nextSibling();
}
ei.engineId = engineId;
ei.engineName = engineName;
ei.engineDir = engineDir;
ei.engineLaunchString = engineLaunchString;
enginesList.append(ei);
}
engineListNode = engineListNode.nextSibling();
}
}
else if (updater.toElement().nodeName() == "VersionList") {
QDomNode versionListNode = updater.firstChild();
while (!versionListNode.isNull()) {
if (versionListNode.nodeName() == "Version") {
QDomNode versionNode = versionListNode.firstChild();
int versionId = -1;
QString versionName;
versionInfo_s vi;
while (!versionNode.isNull()) {
if (versionNode.nodeName() == "VersionName") {
versionName = versionNode.toElement().text();
}
if (versionNode.nodeName() == "VersionNumber") {
versionId = versionNode.toElement().text().toInt();
}
versionNode = versionNode.nextSibling();
}
vi.versionId = versionId;
vi.versionName = versionName;
versionsList.append(vi);
}
versionListNode = versionListNode.nextSibling();
}
}
else if (updater.toElement().nodeName() == "Files") {
emit checkingChanged(0, "");
QDomNode files = updater.firstChild();
emit requestNewDlLabel("Checking the game files checksums. It may take a few minutes...");
int i = 0, l;
l = updater.childNodes().length();
while (!files.isNull()) {
if (files.nodeName() == "File") {
QDomNode fileInfo = files.firstChild();
QString fileDir;
QString fileName;
QString fileMd5;
QString fileSize;
QString fileUrl;
bool mustDownload = false;
while (!fileInfo.isNull()) {
if (fileInfo.nodeName() == "FileDir") {
fileDir = fileInfo.toElement().text();
}
if (fileInfo.nodeName() == "FileName") {
fileName = fileInfo.toElement().text();
}
if (fileInfo.nodeName() == "FileMD5") {
fileMd5 = fileInfo.toElement().text();
}
if (fileInfo.nodeName() == "FileSize") {
fileSize = fileInfo.toElement().text();
}
if (fileInfo.nodeName() == "FileUrl") {
fileUrl = fileInfo.toElement().text();
}
fileInfo = fileInfo.nextSibling();
}
QString filePath(updaterPath + fileDir + fileName);
QFile* f = new QFile(filePath);
i++;
QString statusText = QString("Checking %1 (%2 of %3)").arg(fileName).arg(i).arg(l);
emit checkingChanged(i * 100.0 / l, statusText);
// If the file does not exist, it must be downloaded.
if (!f->exists()) {
if (!fileMd5.isEmpty()) {
mustDownload = true;
}
}
// If the md5 string is empty, it means that the API wants
// us to delete this file if needed
else if (!fileName.isEmpty() && fileMd5.isEmpty()) {
QFile::remove(filePath);
}
// Check the file's md5sum to see if it needs to be updated.
else if (!fileName.isEmpty() && !fileMd5.isEmpty()) {
QString hashResult;
if (f->open(QIODevice::ReadOnly))
{
QCryptographicHash hash(QCryptographicHash::Md5);
while (!f->atEnd()) {
hash.addData(f->read(4096));
emit checkingChanged((i * 100 + (100 * f->pos() / f->size())) / l, statusText);
}
f->close();
hashResult = hash.result().toHex();
}
if (hashResult != fileMd5) {
mustDownload = true;
}
}
if (!fileMd5.isEmpty() && !fileName.isEmpty()) {
packsList.append(fileName);
}
if (mustDownload) {
fileInfo_s fi;
fi.fileName = fileName;
fi.filePath = fileDir;
fi.fileMd5 = fileMd5;
fi.fileSize = fileSize;
fi.fileUrl = fileUrl;
filesToDownload.append(fi);
}
delete f;
}
files = files.nextSibling();
}
emit checkingChanged(100, "");
}
updater = updater.nextSibling();
}
}
node = node.nextSibling();
}
delete dom;
}
void UrTUpdater::work()
{
currentChecksum->hide();
// Workaround - you can't call ->show() from parseManifest()
// because this function is running on its own thread.
if (changelog != CHANGELOG_EMPTY_TEXT) {
changelogButton->show();
}
checkAPIVersion();
checkDownloadServer();
checkGameEngine();
checkVersion();
drawNews();
if (!threadStarted) {
startDlThread();
}
if (readyToProcess) {
checkFiles();
downloadFiles();
}
else {
if (firstLaunch) {
openLicencePage();
// Create the game folder
if (!QDir().mkdir(updaterPath + URT_GAME_SUBDIR)) {
folderError(QString(updaterPath + URT_GAME_SUBDIR));
}
firstLaunch = false;
openSettings();
return;
}
readyToProcess = true;
getManifest("versionFiles");
}
}
void UrTUpdater::updateCheckStatus(int progress, QString status)
{
currentChecksum->setText(status);
dlBar->setValue(progress);
}
void UrTUpdater::setDLValueP(qint64 r, qint64 t)
{
dlBar->setValue(r * 100.0 / t);
}
void UrTUpdater::startDlThread()
{
if (threadStarted) {
return;
}
dlThread = new QThread();
dl = new Download(getServerUrlById(downloadServer), updaterPath, getPlatform());
dl->moveToThread(dlThread);
connect(dlThread, SIGNAL(started()), dl, SLOT(init()));
connect(dlThread, SIGNAL(finished()), dlThread, SLOT(deleteLater()));
connect(dl, SIGNAL(dlError(QNetworkReply::NetworkError)), this, SLOT(networkError(QNetworkReply::NetworkError)));
connect(dl, SIGNAL(folderError(QString)), this, SLOT(folderError(QString)));
connect(dl, SIGNAL(fileDownloaded()), this, SLOT(fileDownloaded()));
connect(dl, SIGNAL(bytesDownloaded(qint64, QString, int, int)), this, SLOT(bytesDownloaded(qint64, QString, int, int)));
connect(this, SIGNAL(dlFile(QString,QString, int, QString)), dl, SLOT(downloadFile(QString, QString, int, QString)));
threadStarted = true;
dlThread->start();
}
void UrTUpdater::downloadFiles()
{
if (filesToDownload.size() <= 0) {
dlBar->setRange(0, 100);
dlBar->setValue(100);
globalDlBar->hide();
globalDlText->hide();
dlSpeed->hide();
dlSize->hide();
updateInProgress = false;
loaderAnim->stop();
playAnim->start();
playButton->setText("Play!");
updateDlLabel("Your game is up to date!");
return;
}
else {
if (askBeforeUpdating == 1) {
QMessageBox msg;
int result;
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setIcon(QMessageBox::Information);
msg.setText("A new update is available. Would you like to download it now?");
result = msg.exec();
if (result == QMessageBox::Cancel) {
updateDlLabel("Your game is outdated!");
loaderAnim->stop();
playAnim->start();
playButton->setText("Play!");
return;
}
}
totalSizeToDl = getTotalSizeToDl();
updateInProgress = true;
nbFilesToDl = filesToDownload.size();
nbFilesDled = 0;
downloadedBytes = 0;
currentFile = filesToDownload.takeFirst();
dlBar->setValue(0);
dlBar->setRange(0, currentFile.fileSize.toInt());
globalDlBar->setValue(0);
globalDlBar->setRange(0, totalSizeToDl);
globalDlBar->show();
globalDlText->show();
dlSpeed->show();
dlSize->show();
emit dlFile(currentFile.filePath, currentFile.fileName, currentFile.fileSize.toInt(), currentFile.fileUrl);
}
}
void UrTUpdater::bytesDownloaded(qint64 speed, QString unit, int nbBytes, int dled)
{
QString nb;
QString nb2;
int totalSize = totalSizeToDl;
downloadedBytes += dled;
globalDlBar->setValue(downloadedBytes);
dlBar->setValue(nbBytes);
updateDlLabel("Current file: " + currentFile.filePath + currentFile.fileName + " (" + (QString::number(nbFilesDled+1)) + "/" + QString::number(nbFilesToDl) + ")");
dlSpeed->setText("Speed: " + QString::number(speed, 'f', 2) + " " + QString(unit));
int bytes = downloadedBytes;
nb = getSize(&bytes);
nb2 = getSize(&totalSize);
dlSize->setText(QString::number(bytes) + nb + "/ " + QString::number(totalSize) + nb2); // / " + QString::number(currentFile.fileSize)/1000 + " kB"
}
void UrTUpdater::fileDownloaded()
{
if (filesToDownload.size() > 0) {
nbFilesDled++;
currentFile = filesToDownload.takeFirst();
dlBar->setRange(0, currentFile.fileSize.toInt());
dlBar->setValue(0);
emit dlFile(currentFile.filePath, currentFile.fileName, currentFile.fileSize.toInt(), currentFile.fileUrl);
}
else {
dlBar->setRange(0, 100);
dlBar->setValue(100);
globalDlBar->hide();
globalDlText->hide();
dlSize->hide();
dlSpeed->hide();
filesToDownload.clear();
nbFilesToDl = 0;
nbFilesDled = 0;
downloadedBytes = 0;
getManifest("versionFiles");
}
}
void UrTUpdater::checkFiles()
{
QStringList nameFilter("zUrT*.pk3");
QDir filesPath(updaterPath + URT_GAME_SUBDIR);
QStringList filesList = filesPath.entryList(nameFilter);
if (packsList.size() <= 0) {
return;
}
foreach (QString file, filesList) {
if (!packsList.contains(file)) {
QFile* fileToRm = new QFile(updaterPath + URT_GAME_SUBDIR + "/" + file);
fileToRm->remove();
delete fileToRm;
}
}
}
void UrTUpdater::checkAPIVersion()
{
if (apiVersion != updaterVersion) {
QMessageBox::critical(0, "Updater outdated", "This version ("+updaterVersion+") of the Updater is outdated. Please download the new Updater here: http://get.urbanterror.info");
this->close();
}
}
void UrTUpdater::checkDownloadServer()
{
QList<serverInfo_s>::iterator li;
bool found = false;
if (downloadServers.size() < 1) {
apiError();
}
// Check if the download server that is stored in the config file still exists
for (li = downloadServers.begin(); li != downloadServers.end(); ++li) {
if (li->serverId == downloadServer) {
found = true;
}
}
// If the engine isn't available anymore, pick the first one in the list
if (!found) {
downloadServer = downloadServers.at(0).serverId;
}
}
void UrTUpdater::checkGameEngine()
{
QList<engineInfo_s>::iterator li;
bool found = false;
if (enginesList.size() < 1) {
apiError();
}
// Check if the engine that is stored in the config file still exists
for (li = enginesList.begin(); li != enginesList.end(); ++li) {
if (li->engineId == gameEngine) {
found = true;
}
}
// If the server isn't a mirror anymore, pick the first one in the list
if (!found) {
gameEngine = enginesList.at(0).engineId;
}
}
void UrTUpdater::checkVersion()
{
QList<versionInfo_s>::iterator li;
bool found = false;
if (versionsList.size() < 1) {
apiError();
}
// Check if the version that is stored in the config file still exists
for (li = versionsList.begin(); li != versionsList.end(); ++li) {
if (li->versionId == currentVersion) {
found = true;
}
}
// If the version isn't available anymore, pick the first one in the list
if (!found) {
currentVersion = versionsList.at(0).versionId;
}
}
void UrTUpdater::drawNews()
{
QList<QString>::iterator li;
int i = 0;
if (newsList.size() < 1) {
apiError();
}
for (li = newsList.begin(); li != newsList.end(); ++li, i++) {
QLabel* news = new QLabel(this);
if (!menuBar()->isNativeMenuBar()) {
news->move(150, 85 + (i*26));
}
else {
news->move(150, 65 + (i*26));
}
news->setMinimumWidth(450);
news->setText(*li);
news->setOpenExternalLinks(true);
news->setVisible(true);
}
}
void UrTUpdater::networkError(QNetworkReply::NetworkError code)
{
QString error = "";
bool critical = false;
switch(code) {
case QNetworkReply::ConnectionRefusedError:
error = "Error: the remote server refused the connection. Please try again later.";
critical = true;
break;
case QNetworkReply::RemoteHostClosedError:
error = "Error: the remote server closed the connection prematurely. Please try again later.";
break;
case QNetworkReply::HostNotFoundError:
error = "Error: the remote server could not be found. Please check your internet connection!";
critical = true;
break;
case QNetworkReply::TimeoutError: