-
Notifications
You must be signed in to change notification settings - Fork 9
/
mainwindow.cpp
1412 lines (1053 loc) · 45.4 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settings.h"
#include <QNetworkReply>
#include <QtXml>
#include "statusxmlcontenthandler.h"
#include <QProcess>
#include <QMessageBox>
#include <iostream>
#include <QDesktopWidget>
#include "loginservers.h"
#include "configparser.h"
#include "gameprocess.h"
#ifdef Q_OS_WIN32
#include "windebugmonitor.h"
#endif
#include "selfupdater.h"
#include <QCloseEvent>
#include "installfromswg.h"
#include "utils.h"
#include "filescanner.h"
#include "macroeditor.h"
#include <QWebElement>
#include <QWebFrame>
#if QT_VERSION >= 0x050000
#include <QtConcurrent/QtConcurrentRun>
#endif
#include "gamemods.h"
//#define ENABLE_MACRO_EDITOR
QString MainWindow::patchUrl = "http://www.launchpad2.net/SWGEmu/"; // Insert download URL here
//QString MainWindow::newsUrl = "http://www.swgemu.com/forums/index.php#bd";
QString MainWindow::newsUrl = "http://www.swgemu.com/forums/forum.php";
QString MainWindow::gameExecutable = "SWGEmu.exe";
#ifdef Q_OS_WIN32
QString MainWindow::selfUpdateUrl = "http://launchpad2.net/setup.cfg"; // Insert update URL here
#else
QString MainWindow::selfUpdateUrl = "http://launchpad2.net/setuplinux86_64.cfg"; // Insert linux update URL here
#endif
const QString MainWindow::version = "0.23";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), networkAccessManager(this), clientFilesNetworkAccessManager(this),
novaNetworkAccessManager(this), requiredFilesNetworkManager(this), patchesNetworkManager(this),
fullScanWorkingThreads(0) {
ui->setupUi(this);
QCoreApplication::setOrganizationName("SWGEmu");
QCoreApplication::setOrganizationDomain("swgemu.com");
QCoreApplication::setApplicationName("Launchpad");
requiredFilesCount = 0;
nextFileToDownload = 0;
updateTimeCounter = 5;
gameProcessesCount = 0;
runningFullScan = false;
fileScanner = new FileScanner(this);
settings = new Settings(this);
loginServers = new LoginServers(this);
systemTrayIcon = new QSystemTrayIcon(this);
systemTrayIcon->setIcon(QIcon(":/img/swgemu.svg"));
systemTrayMenu = new QMenu();
closeAction = new QAction("Close", NULL);
systemTrayMenu->addAction(closeAction);
systemTrayIcon->setContextMenu(systemTrayMenu);
QToolButton* newsButton = new QToolButton(ui->mainToolBar);
newsButton->setIcon(QIcon(":/img/globe.svg"));
newsButton->setText("News");
newsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
newsButton->setCheckable(true);
ui->mainToolBar->addWidget(newsButton);
connect(newsButton, SIGNAL(clicked()), this, SLOT(triggerNews()));
toolButtons.append(newsButton);
QToolButton* updateStatusButton = new QToolButton(ui->mainToolBar);
updateStatusButton->setIcon(QIcon(":/img/update_status.svg"));
updateStatusButton->setText("Update status");
updateStatusButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(updateStatusButton);
connect(updateStatusButton, SIGNAL(clicked()), this, SLOT(updateServerStatus()));
toolButtons.append(updateStatusButton);
QToolButton* gameSettingsButton = new QToolButton(ui->mainToolBar);
gameSettingsButton->setIcon(QIcon(":/img/game_settings.svg"));
gameSettingsButton->setText("Game settings");
gameSettingsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(gameSettingsButton);
connect(gameSettingsButton, SIGNAL(clicked()), this, SLOT(startSWGSetup()));
toolButtons.append(gameSettingsButton);
QToolButton* gameModsButton = new QToolButton(ui->mainToolBar);
gameModsButton->setIcon(QIcon(":/img/magic.svg"));
gameModsButton->setText("Game mods");
gameModsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(gameModsButton);
connect(gameModsButton, SIGNAL(clicked()), this, SLOT(showGameModsOptions()));
toolButtons.append(gameModsButton);
#ifdef ENABLE_MACRO_EDITOR
QToolButton* macroEditorButton = new QToolButton(ui->mainToolBar);
macroEditorButton->setIcon(QIcon(":/img/book.svg"));
macroEditorButton->setText("Macro Editor");
macroEditorButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(macroEditorButton);
connect(macroEditorButton, SIGNAL(clicked()), this, SLOT(showMacroEditor()));
toolButtons.append(macroEditorButton);
#endif
QToolButton* profCalculatorButton = new QToolButton(ui->mainToolBar);
profCalculatorButton->setIcon(QIcon(":/img/design.svg"));
profCalculatorButton->setText("Profession Calculator");
profCalculatorButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(profCalculatorButton);
connect(profCalculatorButton, SIGNAL(clicked()), this, SLOT(startKodanCalculator()));
toolButtons.append(profCalculatorButton);
QToolButton* deleteProfilesButton = new QToolButton(ui->mainToolBar);
deleteProfilesButton->setIcon(QIcon(":/img/bin.svg"));
deleteProfilesButton->setText("Delete game profiles");
deleteProfilesButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(deleteProfilesButton);
connect(deleteProfilesButton, SIGNAL(clicked()), this, SLOT(deleteProfiles()));
toolButtons.append(deleteProfilesButton);
QToolButton* updateButton = new QToolButton(ui->mainToolBar);
updateButton->setIcon(QIcon(":/img/cloud_down.svg"));
updateButton->setText("Check for updates");
updateButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->mainToolBar->addWidget(updateButton);
connect(updateButton, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
toolButtons.append(updateButton);
cancelWorkingThreads = false;
connect(ui->mainToolBar, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(toolBarOrientationChanged(Qt::Orientation)));
connect(systemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systemTrayActivated(QSystemTrayIcon::ActivationReason)));
connect(closeAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->actionFolders, SIGNAL(triggered()), this, SLOT(showSettings()));
connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(statusXmlIsReady(QNetworkReply*)) );
connect(&novaNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(statusXmlIsReady(QNetworkReply*)) );
connect(&clientFilesNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFileFinished(QNetworkReply*)));
connect(&requiredFilesNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requiredFileDownloadFileFinished(QNetworkReply*)));
connect(&patchesNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(patchesDownloadFileFinished(QNetworkReply*)));
connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(webPageLoadFinished(bool)));
connect(ui->pushButton_Start, SIGNAL(clicked()), this, SLOT(startSWG()));
connect(fileScanner, SIGNAL(requiredFileExists(QString)), this, SLOT(updateBasicLoadProgress(QString)));
connect(fileScanner, SIGNAL(fullScannedFile(QString, bool)), this, SLOT(updateFullScanProgress(QString, bool)));
connect(this, SIGNAL(startDownload()), this, SLOT(startFileDownload()));
connect(ui->actionLogin_Servers, SIGNAL(triggered()), loginServers, SLOT(show()));
connect(ui->actionShow_news, SIGNAL(triggered()), this, SLOT(triggerNews()));
connect(ui->checkBox_instances, SIGNAL(toggled(bool)), this, SLOT(triggerMultipleInstances(bool)));
connect(ui->actionUpdate_Status, SIGNAL(triggered()), this, SLOT(updateServerStatus()));
connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
connect(ui->actionCheck_for_updates, SIGNAL(triggered()), this, SLOT(checkForUpdates()));
connect(ui->actionGame_Settings, SIGNAL(triggered()), this, SLOT(startSWGSetup()));
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAboutDialog()));
connect(ui->actionDelete_Profiles, SIGNAL(triggered()), this, SLOT(deleteProfiles()));
connect(fileScanner, SIGNAL(addFileToDownload(QString)), this, SLOT(addFileToDownloadSlot(QString)));
connect(ui->actionInstall_from_SWG, SIGNAL(triggered()), this, SLOT(installSWGEmu()));
ui->groupBox_browser->hide();
QTabBar* tabBar = ui->tabWidget->tabBar();
tabBar->setTabButton(0, QTabBar::RightSide, 0);
tabBar->setTabButton(0, QTabBar::LeftSide, 0);
QSettings settingsOptions;
QString swgFolder = settingsOptions.value("swg_folder").toString();
bool multipleInstances = settingsOptions.value("multiple_instances").toBool();
ui->checkBox_instances->setChecked(multipleInstances);
ui->textBrowser->viewport()->setAutoFillBackground(false);
ui->textBrowser->setAutoFillBackground(false);
updateServerStatus();
connect(&loadWatcher, SIGNAL(finished()), this, SLOT(loadFinished()));
//connect(&fullScanWatcher, SIGNAL(finished()), this, SLOT(fullScanFinished()));
connect(ui->pushButton_FullScan, SIGNAL(clicked()), this, SLOT(startFullScan()));
loginServers->reloadServers();
updateLoginServerList();
silentSelfUpdater = new SelfUpdater(true, this);
if (!swgFolder.isEmpty())
startLoadBasicCheck();
else {
#ifdef Q_OS_WIN32
QDir dir("C:/SWGEmu");
if (dir.exists() && FileScanner::checkSwgFolder("C:/SWGEmu")) {
settingsOptions.setValue("swg_folder", "C:/SWGEmu");
startLoadBasicCheck();
} else
#endif
QMessageBox::warning(this, "Error", "Please set the swgemu folder in Settings->Options or install using Settings->Install From SWG option");
}
restoreGeometry(settingsOptions.value("mainWindowGeometry").toByteArray());
restoreState(settingsOptions.value("mainWindowState").toByteArray());
QString savedLogin = settingsOptions.value("selected_login_server", "").toString();
if (!savedLogin.isEmpty()) {
int idx = ui->comboBox_login->findText(savedLogin);
if (idx >= 0) {
ui->comboBox_login->setCurrentIndex(idx);
}
}
requiredFilesNetworkManager.get(QNetworkRequest(QUrl(patchUrl + "required2.txt")));
//patchesNetworkManager.get(QNetworkRequest(QUrl(patchUrl + "patches.txt")));
silentSelfUpdater->silentCheck();
//ui->webView->setUrl(newsUrl);
//gameMods = new GameMods(this);
}
MainWindow::~MainWindow() {
delete ui;
ui = NULL;
//delete fileScanner;
fileScanner = NULL;
//delete settings;
settings = NULL;
//delete loginServers;
loginServers = NULL;
while (gameProcesses.size() > 0) {
GameProcess* process = gameProcesses[0];
gameProcesses.remove(0);
delete process;
}
#ifdef Q_OS_WIN32
if (GameProcess::debugMonitor) {
delete GameProcess::debugMonitor;
GameProcess::debugMonitor = NULL;
}
#endif
//delete silentSelfUpdater;
silentSelfUpdater = NULL;
//gameMods = NULL;
}
void MainWindow::toolBarOrientationChanged(Qt::Orientation ) {
/*
Qt::ToolButtonStyle style = Qt::ToolButtonTextBesideIcon;
switch (orientation) {
case Qt::Horizontal:
style = Qt::ToolButtonTextBesideIcon;
break;
case Qt::Vertical:
style = Qt::ToolButtonTextUnderIcon;
break;
}
for (int i = 0; i < toolButtons.size(); ++i) {
QToolButton* button = toolButtons.at(i);
button->setToolButtonStyle(style);
button->set
// button->set
}
*/
}
void MainWindow::systemTrayActivated(QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
this->showNormal();
this->raise();
}
}
void MainWindow::deleteProfiles() {
if (QMessageBox::question(this, "Warning", "Are you sure you want to delete the game profiles folder?") != QMessageBox::Yes )
return;
QSettings settings;
QString folder = settings.value("swg_folder").toString();
#ifdef Q_OS_WIN32
QDir dir(folder + "\\profiles");
#else
QDir dir(folder + "/profiles");
#endif
if (!dir.exists()) {
QMessageBox::warning(this, "Warning", "No profiles folder found");
return;
}
if (dir.removeRecursively()) {
QMessageBox::information(this, "Success", "Profiles directory deleted!");
} else {
QMessageBox::warning(this, "Warning", "There was an error deleting the profiles directory!");
}
}
void MainWindow::readBasiliskServerStatus() {
networkAccessManager.get(QNetworkRequest(QUrl("http://www.swgemu.com/status/basilisk.xml")));
}
void MainWindow::readNovaServerStatus() {
novaNetworkAccessManager.get(QNetworkRequest(QUrl("http://www.swgemu.com/status/nova.xml")));
}
void MainWindow::checkForUpdates() {
SelfUpdater updater(false, this);
updater.execUpdate();
}
void MainWindow::updateLoginServerList() {
ui->comboBox_login->clear();
QSettings settings;
settings.beginWriteArray("login_servers");
for (int i = 0; i < loginServers->count(); ++i) {
LoginServer* server = loginServers->getServer(i);
settings.setArrayIndex(i);
ui->comboBox_login->addItem(server->text());
settings.setValue("name", server->text());
settings.setValue("host", server->getHost());
settings.setValue("port", server->getPort());
}
settings.endArray();
}
void MainWindow::updateServerStatus() {
//ui->statusBar->showMessage("Updating server status..");
ui->textBrowser->clear();
readBasiliskServerStatus();
readNovaServerStatus();
}
void MainWindow::triggerMultipleInstances(bool newValue) {
QSettings settings;
settings.setValue("multiple_instances", newValue);
}
void MainWindow::triggerNews() {
if (ui->groupBox_browser->isHidden()) {
ui->statusBar->showMessage("Loading page...");
ui->webView->setUrl(newsUrl);
if (!isMaximized()) {
QDesktopWidget* mydesk = QApplication::desktop();
int screen = mydesk->screenNumber(this);
QRect screenSize = mydesk->screenGeometry(screen);
QRect windowGeometry = this->geometry();
int difference = 811 - 356;
if (windowGeometry.height() + difference + pos().y() > screenSize.bottom()) {
this->move(pos().x(), pos().y() - (windowGeometry.height() + difference + pos().y() - screenSize.bottom()));
}
this->resize(907, 811);
}
ui->actionShow_news->setText("Hide news");
//ui->actionShow_news->setChecked(true);
ui->groupBox_browser->show();
} else {
ui->groupBox_browser->hide();
if (!isMaximized())
this->resize(907, 256);
//ui->actionShow_news->setChecked(false);
ui->actionShow_news->setText("Show news");
}
}
QFile* MainWindow::getRequiredFilesFile() {
QSettings settings;
//QString folder = settings.value("swg_folder").toString();
QFile* file = NULL;
//if (QDir(folder).exists()) {
file = new QFile("required2.txt");
if (file->exists()) {
if (file->open(QIODevice::ReadOnly | QIODevice::Text)) {
return file;
} else
delete file;
} else {
delete file;
}
//}
file = new QFile(":/files/required2.txt");
file->open(QIODevice::ReadOnly | QIODevice::Text);
return file;
}
void MainWindow::addFileToDownloadSlot(QString file) {
filesToDownload.append(file);
}
void MainWindow::showAboutDialog() {
QMessageBox::about(this, "SWGemu", "SWGEmu Launchpad version " + version + "\n\nThis program 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.");
}
void MainWindow::requiredFileDownloadFileFinished(QNetworkReply* reply) {
if (reply && reply->error() != QNetworkReply::NoError) {
// QMessageBox::warning(this, "ERROR getting new patch information", reply->errorString());
return;
}
qDebug() << "got required2.txt";
QString data = reply->readAll();
// qDebug() << data;
QSettings settings;
// QString folder = settings.value("swg_folder").toString();
// if (QDir(folder).exists()) {
QFile file("required2.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream << data;
file.close();
return;
}
//}
startLoadBasicCheck();
/*
ui->pushButton_FullScan->setEnabled(false);
ui->pushButton_Start->setEnabled(false);
ui->label_current_work->setText("Checking for new client updates...");
QStringList currentFiles = getRequiredFiles();
*/
}
void MainWindow::patchesDownloadFileFinished(QNetworkReply* reply) {
if (reply && reply->error() != QNetworkReply::NoError) {
// QMessageBox::warning(this, "ERROR getting new patch information", reply->errorString());
updateTimeCounter = -1;
enableStart();
return;
}
try {
QString data = reply->readAll();
qDebug() << "got patches.txt" << data;
QTextStream file(&data);
QSettings settings;
QString swgFolder = settings.value("swg_folder").toString();
while (!file.atEnd()) {
QString fileString = file.readLine();
QByteArray line(fileString.toStdString().c_str());
//process_line(line);
//QRegExp rx("(\\ |\\,|\\.|\\;|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t'
QList<QByteArray> query = line.split(';');
//QListIterator<QByteArray>
QString name = query.at(0);
QString size = query.at(1);
QString md5 = query.at(2).trimmed();
QString action = query.at(3);
QString file = swgFolder + "/" + name;
QFile fileObject(file);
if ((action == "A") && !fileObject.exists()) {
qDebug() << file << "doesnt exist";
//return -1;
//filesToDownload.append(MainWindow::patchUrl + name);
appendToFilesToDownloadStringList(MainWindow::patchUrl + name);
continue;
} else if ((action == "D") && fileObject.exists()) {
fileObject.remove();
}
}
}
catch (...) {
enableStart();
}
if (filesToDownload.size() > 0) {
startFileDownload();
updateTimeCounter = -2;
} else {
updateTimeCounter = -1;
enableStart();
}
}
void MainWindow::updateBasicLoadProgress(QString successFile) {
//ui->progressBar_loading->set
ui->progressBar_loading->setValue(++currentReadFiles);
}
void MainWindow::updateFullScanProgress(QString successFile, bool success) {
/*if (success)
ui->label_current_work->setText(successFile + " valid.");
else
ui->label_current_work->setText(successFile + " invalid!");*/
if (!success) {
//ui->label_current_work->setPalette(QPalette::);
ui->label_current_work->setStyleSheet("color:red");
ui->label_current_work->setText(successFile + " invalid!");
}
ui->progressBar_FullScan->setValue(++currentReadFiles);
if (currentReadFiles == requiredFilesCount) {
qDebug() << "full scan finished";
runningFullScan = false;
if (filesToDownload.size() == 0)
ui->label_current_work->setText("Full scan successfull");
if (!cancelWorkingThreads)
emit startDownload();
}
}
void MainWindow::startFullScan(bool forceConfigRestore) {
if (!forceConfigRestore) {
if (QMessageBox::question(this, "Warning", "This will restore your files to their original state removing any mods you might have. Do you want to continue?") != QMessageBox::Yes)
return;
}
requiredFilesCount = getRequiredFiles().size();
currentReadFiles = 0;
QSettings settings;
QString folder = settings.value("swg_folder").toString();
QDir checkDir(folder);
if (!checkDir.exists() || folder.isEmpty() || !FileScanner::checkSwgFolder(folder)) {
QMessageBox::warning(this, "ERROR", "Invalid game folder!");
return;
}
bool restoreConfigFiles = forceConfigRestore ? true : QMessageBox::question(this, "Config files", "Do you want to restore the config files too?") == QMessageBox::Yes;
if (requiredFilesCount == 0 && !restoreConfigFiles)
return;
ui->pushButton_Start->setEnabled(false);
ui->pushButton_FullScan->setEnabled(false);
ui->progressBar_FullScan->setMaximum(requiredFilesCount);
ui->progressBar_FullScan->setValue(0);
ui->label_current_work->setStyleSheet("color:black");
ui->label_current_work->setText("Doing full scan..");
ui->actionFolders->setEnabled(false);
filesToDownload.clear();
if (restoreConfigFiles || !QFile(folder + "/swgemu.cfg").exists()) {
filesToDownload.append(patchUrl + "swgemu.cfg");
filesToDownload.append(patchUrl + "swgemu_live.cfg");
filesToDownload.append(patchUrl + "swgemu_login.cfg");
filesToDownload.append(patchUrl + "swgemu_preload.cfg");
//filesToDownload.append(patchUrl + "Emu_opt.cfg");
filesToDownload.append(patchUrl + "swgemu_machineoptions.iff");
filesToDownload.append(patchUrl + "options.cfg");
//filesToDownload.append(patchUrl + "user.cfg");
}
bool multiThreaded = settings.value("multi_threaded_full_scan", false).toBool();
if (multiThreaded) {
fullScanWorkingThreads = getRequiredFiles().size();
runningFullScan = true;
QtConcurrent::run(fileScanner, &FileScanner::fullScanMultiThreaded, restoreConfigFiles);
} else {
runningFullScan = true;
QFuture<int> future = QtConcurrent::run(fileScanner, &FileScanner::fullScanSingleThreaded, restoreConfigFiles);
fullScanWatcher.setFuture(future);
}
}
void MainWindow::startLoadBasicCheck() {
if (runningFullScan)
return;
ui->pushButton_Start->setEnabled(false);
// ui->pushButton_Start->setText("Start (" + QString::number(updateTimeCounter) + ")");
ui->label_current_work->setStyleSheet("color:black");
ui->label_current_work->setText("Checking files and updates..");
requiredFilesCount = getRequiredFiles().size();
currentReadFiles = 0;
ui->progressBar_loading->setMaximum(requiredFilesCount);
ui->progressBar_loading->setValue(0);
QSettings settingsOptions;
QString swgFolder = settingsOptions.value("swg_folder").toString();
QFuture<int> future = QtConcurrent::run(fileScanner, &FileScanner::loadAndBasicCheckFiles, swgFolder);
loadWatcher.setFuture(future);
//QTimer::singleShot(1000, this, SLOT(runUpdateCheckTimer()));
}
void MainWindow::runUpdateCheckTimer() {
if (--updateTimeCounter >= 0) {
QTimer::singleShot(1000, this, SLOT(runUpdateCheckTimer()));
ui->pushButton_Start->setText("Start (" + QString::number(updateTimeCounter) + ")");
} else {
ui->pushButton_Start->setText("Start");
//ui->pushButton_Start->setEnabled(true);
// qDebug() << "hui is: " << updateTimeCounter;
if (updateTimeCounter == -1) {
enableStart();
}
}
}
void MainWindow::startFileDownload() {
requiredFilesCount = filesToDownload.count();
currentReadFiles = 0;
if (filesToDownload.length() == 0) {
ui->actionFolders->setEnabled(true);
ui->pushButton_Start->setEnabled(true);
ui->pushButton_FullScan->setEnabled(true);
return;
}
ui->progressBar_loading->setMaximum(requiredFilesCount);
ui->progressBar_loading->setValue(0);
//filesToDownload = files;
nextFileToDownload = 0;
QString downloadingFile = filesToDownload.at(nextFileToDownload);
downloadingFile = downloadingFile.mid(downloadingFile.lastIndexOf("/"));
ui->label_current_work->setStyleSheet("color:black");
ui->label_current_work->setText("Downloading: " + downloadingFile);
qDebug() << "downloading " << filesToDownload.at(nextFileToDownload);
QNetworkReply* reply = clientFilesNetworkAccessManager.get(QNetworkRequest(filesToDownload.at(nextFileToDownload)));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
lastReceivedBytesTime.restart();
lastReceivedBytes = 0;
}
void MainWindow::downloadFileFinished(QNetworkReply* reply) {
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(this, "ERROR", reply->errorString());
filesToDownload.clear();
nextFileToDownload = 0;
return;
}
int bufferSize = 8192 * 2;
char* buffer = (char*) malloc(bufferSize);
QSettings settings;
QString swgFolder = settings.value("swg_folder").toString();
QString downloadedFile = filesToDownload.at(nextFileToDownload);
downloadedFile = downloadedFile.remove(0, patchUrl.length());
QString dir;
if (downloadedFile.contains("/")) {
dir = downloadedFile.mid(0, downloadedFile.lastIndexOf("/"));
}
downloadedFile = downloadedFile.mid(downloadedFile.lastIndexOf("/") + 1);
QString fullPath;
if (dir.isEmpty())
fullPath = swgFolder + "/" + downloadedFile;
else
fullPath = swgFolder + "/" + dir + "/" + downloadedFile;
if (!QDir(swgFolder + "/" + dir).exists())
QDir(swgFolder + "/" + dir).mkpath(".");
QFile fileObject(fullPath);
//fileObject.set
if (!fileObject.open(QIODevice::WriteOnly)) {
QMessageBox::critical(this, "ERROR", "Could not open to write downloaded file to disk! " + swgFolder + "/" + downloadedFile);
filesToDownload.clear();
nextFileToDownload = 0;
free(buffer);
return;
}
int read = 0;
while ((read = reply->read(buffer, bufferSize)) > 0) {
if (fileObject.write(buffer, read) == -1) {
QMessageBox::critical(this, "ERROR", "Could not write downloaded file to disk!");
filesToDownload.clear();
nextFileToDownload = 0;
free(buffer);
return;
}
}
fileObject.close();
free(buffer);
qDebug() << "downloading file:" << downloadedFile << " finished!";
if (++nextFileToDownload < filesToDownload.size()) {
QString downloadingFile = filesToDownload.at(nextFileToDownload);
downloadingFile = downloadingFile.mid(downloadingFile.lastIndexOf("/") + 1);
ui->label_current_work->setStyleSheet("color:black");
ui->label_current_work->setText("Downloading: " + downloadingFile);
QNetworkReply* reply = clientFilesNetworkAccessManager.get(QNetworkRequest(filesToDownload.at(nextFileToDownload)));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
lastReceivedBytesTime.restart();
lastReceivedBytes = 0;
ui->progressBar_loading->setValue(nextFileToDownload);
} else {
ui->progressBar_loading->setValue(nextFileToDownload);
downloadFinished();
}
}
void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
if (lastReceivedBytes == 0) {
ui->progressBar_FullScan->setMaximum(bytesTotal);
}
ui->progressBar_FullScan->setValue(bytesReceived);
QTime nowTime;
nowTime.restart();
int delta = lastReceivedBytesTime.msecsTo(nowTime);
if (delta < 500 && bytesReceived < bytesTotal)
return;
QString downloadingFile = filesToDownload.at(nextFileToDownload);
downloadingFile = downloadingFile.mid(downloadingFile.lastIndexOf("/") + 1);
int speed = 0;
if (delta != 0)
speed = ((bytesReceived - lastReceivedBytes) / delta);
QString label = "Downloading: " + downloadingFile + " " + QString::number(bytesReceived / 1000) + " kB / " + QString::number(bytesTotal / 1000) + " kB " + QString::number(speed) + " kB/s";
ui->label_current_work->setStyleSheet("color:black");
ui->label_current_work->setText(label);
lastReceivedBytesTime.restart();
lastReceivedBytes = bytesReceived;
}
void MainWindow::startKodanCalculator() {
#ifdef Q_OS_WIN32
if (!QProcess::startDetached("KSWGProfCalcEditor.exe", QStringList(), QDir::currentPath())) {
QMessageBox::warning(this, "ERROR", "Could not launch profession calculator!");
}
#else
QSettings settings;
QString wineBinary = settings.value("wine_binary").toString();
if (wineBinary.isEmpty())
wineBinary = "wine";
QString args = settings.value("wine_args").toString();
QStringList argsList;
if (!args.isEmpty())
argsList = Utils::getArgumentList(args);
argsList.append("KSWGProfCalcEditor.exe");
qDebug() << argsList;
if (!QProcess::startDetached(wineBinary, argsList, QDir::currentPath())) {
QMessageBox::warning(this, "ERROR", "Could not launch game settings!");
}
#endif
}
void MainWindow::startSWGSetup() {
//QMessageBox::
//QProcess* process = new QProcess();
QSettings settings;
QString folder = settings.value("swg_folder").toString();
#ifdef Q_OS_WIN32
//qDebug() << folder;
/*process->setWorkingDirectory(folder);
process->start(folder + "\\" + "SWGEmu_Setup.exe");*/
if (!QProcess::startDetached(folder + "\\" + "SWGEmu_Setup.exe", QStringList(), folder)) {
QMessageBox::warning(this, "ERROR", "Could not launch game settings!");
}
#else
QString wineBinary = settings.value("wine_binary").toString();
if (wineBinary.isEmpty())
wineBinary = "wine";
QString args = settings.value("wine_args").toString();
QStringList argsList;
if (!args.isEmpty())
argsList = Utils::getArgumentList(args);
argsList.append(folder + "/" + "SWGEmu_Setup.exe");
qDebug() << argsList;
if (!QProcess::startDetached(wineBinary, argsList, folder)) {
QMessageBox::warning(this, "ERROR", "Could not launch game settings!");
}
#endif
}
void MainWindow::downloadFinished() {
nextFileToDownload = 0;
if (filesToDownload.contains(patchUrl + "swgemu.cfg")) {
QMessageBox::information(this, "Game Settings", "SWGEmu game settings application will now launch, please set your resolution in the Graphics tab!");
startSWGSetup();
}
filesToDownload.clear();
ui->label_current_work->setStyleSheet("color:green");
ui->label_current_work->setText("Download finished");
ui->pushButton_Start->setEnabled(true);
ui->actionFolders->setEnabled(true);
ui->pushButton_FullScan->setEnabled(true);
qDebug() << "download finished";
//startLoadBasicCheck();
}
void MainWindow::showSettings() {
settings->exec();
}
/*
void MainWindow::fullScanFinished() {
if (cancelWorkingThreads)
return;
int res = fullScanWatcher.result();
qDebug() << "full scan finished with result " << res;
if (res == 0)
ui->label_current_work->setText("Full scan successfull");
}
*/
void MainWindow::enableStart() {
ui->pushButton_Start->setEnabled(true);
ui->pushButton_Start->setText("Start");
ui->actionFolders->setEnabled(true);
ui->label_current_work->setStyleSheet("color:green");
ui->label_current_work->setText("Basic checks passed.");
}
void MainWindow::loadFinished() {
if (cancelWorkingThreads)
return;
int res = loadWatcher.result();
if (res == 0) {
if (updateTimeCounter < 0) {
enableStart();
} else {
updateTimeCounter = 5;
ui->pushButton_Start->setText("Start (" + QString::number(updateTimeCounter) + ")");
QTimer::singleShot(1000, this, SLOT(runUpdateCheckTimer()));
patchesNetworkManager.get(QNetworkRequest(QUrl(patchUrl + "patches.txt")));
}
} else {
ui->pushButton_Start->setEnabled(false);
ui->actionFolders->setEnabled(true);
ui->label_current_work->setStyleSheet("color:red");
ui->label_current_work->setText("Basic checks failed. Please run full scan to try and fix the issues.");
ui->progressBar_loading->setValue(ui->progressBar_loading->maximum());
}
}
QVector<QPair<QString, qint64> > MainWindow::getRequiredFiles() {
QSettings settings;
//QString folder = settings.value("swg_folder").toString();
QVector<QPair<QString, qint64> > data;
//QStringList files;
//if (QDir(folder).exists()) {
{
QFile file("required2.txt");
if (file.exists()) {
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
while (!file.atEnd()) {
QByteArray line = file.readLine();
QList<QByteArray> query = line.split(';');
//QListIterator<QByteArray>