forked from UTAwesome/UTLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.h
217 lines (177 loc) · 7.13 KB
/
bootstrap.h
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
#ifndef BOOTSTRAP_H
#define BOOTSTRAP_H
#include <QObject>
#include "torrentdownloader.h"
#include "download.h"
#include <QFile>
#include <QProcess>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QSettings>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
class Bootstrap : public QObject {
Q_OBJECT
Download download;
Download download2;
Download downloadServers;
QFile torrentFile;
QString motd;
//TorrentDownloader& torrentDownloader;
QProcess unpackProcess;
QString downloadedPath;
QString releasePath;
QString torrentFullFile;
QSettings& settings;
QSet<QString> stockMaps;
QTimer bootstrapRedownloadTimer;
enum class ErrorReason {
UnpackingProcessError
};
private slots:
void unpackFinished(int code) {
if(code == 0) {
settings.setValue("LastInstalledTorrent", torrentFullFile);
settings.sync();
emit ready();
} else {
emit fatalError(ErrorReason::UnpackingProcessError);
}
}
public:
QString MOTD() {
return motd;
}
bool isStockMap(QString map) const {
return stockMaps.contains(map);
}
Bootstrap(QSettings& _settings, QObject* parent = nullptr) : QObject(parent), settings(_settings) {
download.setTarget("https://utlauncher.rushbase.net/bootstrap.json");
connect(&download, &Download::done, [=](QByteArray a) {
auto json = QJsonDocument::fromJson(a).object();
QString url = json.value("torrentUrl").toObject().value("windows64").toString();
//QString url = "https://utlauncher.rushbase.net/openSUSE-13.1-KDE-Live-x86_64.iso.torrent";
QRegExp rx("(\\d+)\\.(\\d+)\\.(\\d+)");
auto latestVersion = json.value("latestVersion").toString();
if(rx.indexIn(latestVersion) == 0) {
auto latestMajor = rx.cap(1).toInt();
auto latestMinor = rx.cap(2).toInt();
auto latestPatch = rx.cap(3).toInt();
int numLatest = 1000 * latestMajor + 100*latestMinor + latestPatch;
int numCurrent = 1000 * VERSION_MAJOR + 100*VERSION_MINOR + VERSION_PATCH;
if(numLatest > numCurrent) {
QMessageBox::information(NULL, "Newer version is available", QString("Current version: %1.%2.%3<br>Latest version: %4 %5 %6<br><br>").arg(VERSION_MAJOR).arg(VERSION_MINOR).arg(VERSION_PATCH).arg(latestMajor).arg(latestMinor).arg(latestPatch) + json.value("downloadMessage").toString());
}
}
motd = json.value("MOTD").toString();
downloadServers.setTarget(json.value("serversUrl").toString());
downloadServers.download();
bootstrapRedownloadTimer.singleShot(15*60000, &download, SLOT(download()));
stockMaps.clear();
for(auto stockMap : json.value("stockMaps").toArray()) {
auto mapString = stockMap.toString();
stockMaps.insert(mapString);
}
#ifdef NO_DOWNLOAD
emit ready();
return;
#endif
/*
QString filename = QFileInfo(url).fileName();
download2.setTarget(url);
auto settingsDir = QFileInfo(this->settings.fileName()).dir();
#ifdef __WIN32__
QFile::copy(":/unrar.exe" , settingsDir.absolutePath() + "/unrar.exe");
#endif
settingsDir.mkpath(QString("UTLauncher/Torrents"));
settingsDir.mkpath(QString("UTLauncher/Downloads"));
settingsDir.mkpath(QString("UTLauncher/Release"));
torrentFullFile = settingsDir.absolutePath() + "/UTLauncher/Torrents/" + filename;
auto torrentDownloadDir = settingsDir.absolutePath() + "/UTLauncher/Downloads/" + filename;
QString downloadedFile = filename;
downloadedFile.replace(".torrent", "");
downloadedPath = settingsDir.absolutePath() + "/UTLauncher/Downloads/" + filename + "/" + downloadedFile;
releasePath = settingsDir.absolutePath() + "/UTLauncher/Release";
if(settings.value("LastInstalledTorrent").toString() == torrentFullFile) {
emit ready();
return;
}
qDebug() << downloadedPath;
emit message("Downloading torrent...");
qDebug() << torrentFullFile;
torrentFile.setFileName(torrentFullFile);
qDebug() << torrentFile.open(QIODevice::WriteOnly);
connect(&download2, &Download::chunk, [&](QByteArray chunk) {
torrentFile.write(chunk);
});
connect(&download2, &Download::done, [=]() {
torrentFile.close();
emit message("Downloading UT release...");
torrentDownloader.download(torrentFullFile, torrentDownloadDir);
});
download2.download();
*/
});
// connect(&torrentDownloader, &TorrentDownloader::progress, [&](double progress) {
// emit message(QString("Downloading latest UT build... %1").arg(QString::number(progress, 'f', 2)));
// });
// connect(&torrentDownloader, &TorrentDownloader::finished, [=]() {
// emit message(QString("Preparing the build..."));
// #ifdef __WIN32__
// auto unrar = QFileInfo(this->settings.fileName()).dir().absolutePath() + "/unrar.exe";
// #else
// auto unrar = "unrar";
// #endif
//
// unpackProcess.start(unrar, QStringList() << "x" << "-y" << downloadedPath << releasePath );
// connect(&unpackProcess, &QProcess::started, [=]() {
//
// });
//
// connect(&unpackProcess, SIGNAL(finished(int)), this, SLOT(unpackFinished(int)));
// });
connect(&downloadServers, &Download::done, [=](QByteArray a) {
emit serversInfo(QJsonDocument::fromJson(a));
});
};
void start() {
download.download();
}
QString programExePath() {
QString path = settings.value(
#ifdef LAUNCH_WITH_UE4
"UTExePathUE4"
#else
"UTExePath"
#endif
).toString();
if(QFile::exists(path))
return path;
return "";
}
QString editorExePath() {
QString path = settings.value("UE4ExePath").toString();
if(QFile::exists(path))
return path;
return "";
}
QString projectPath() {
QString path = settings.value("UTProjectPath").toString();
if(QFile::exists(path))
return path;
return "";
}
public slots:
void refreshServers() {
downloadServers.download();
}
signals:
void message(QString message);
void serversInfo(QJsonDocument document);
void ready();
void fatalError(ErrorReason reason);
};
#endif // BOOTSTRAP_H