forked from LRFLEW/OpenRCT2Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.cpp
189 lines (162 loc) · 6.22 KB
/
updater.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
#include "updater.h"
#include "platform.h"
#ifdef Q_OS_LINUX
#include "archives/gzipreadfilter.h"
#include "archives/tarextractor.h"
#else
#include "archives/zipextractor.h"
#endif
#include <QBuffer>
#include <QCryptographicHash>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkRequest>
#include <QProcess>
#include <QStringBuilder>
#include <QTemporaryFile>
#include <QUrlQuery>
Updater::Updater(QObject *parent) : QObject(parent), url(QStringLiteral("https://openrct2.org/altapi/"))
{
connect(&net, &QNetworkAccessManager::sslErrors, [](QNetworkReply * reply, const QList<QSslError> & errors){Q_UNUSED(reply); qDebug() << errors;});
}
void Updater::download() {
if (update != nullptr) update->abort(), update = nullptr;
if (api != nullptr) api->abort(), api = nullptr;
if (bundle != nullptr) bundle->abort(), bundle = nullptr;
QUrlQuery query;
query.addQueryItem(QStringLiteral("command"), QStringLiteral("get-latest-download"));
query.addQueryItem(QStringLiteral("flavourId"), QStringLiteral(OPENRCT2_FLAVOR));
QVariant stableVar = settings.value(QStringLiteral("stable"));
bool stable = stableVar.isValid() && stableVar.toBool();
query.addQueryItem(QStringLiteral("gitBranch"), stable ? QStringLiteral("master") : QStringLiteral("develop"));
url.setQuery(query.query());
QNetworkRequest urequest(QStringLiteral("https://api.github.com/repos/LRFLEW/OpenRCT2Launcher/releases/latest"));
update = net.get(urequest);
connect(update, &QNetworkReply::finished, this, &Updater::receivedUpdate);
QNetworkRequest request(url);
api = net.get(request);
connect(api, &QNetworkReply::finished, this, &Updater::receivedAPI);
}
void Updater::receivedUpdate() {
if (update->error() != QNetworkReply::NoError) {
// Don't emit, so the error is mostly silent
qDebug() << update->errorString();
return;
}
QByteArray data = update->readAll();
update->close();
update->deleteLater();
update = nullptr;
QJsonDocument response = QJsonDocument::fromJson(data);
QJsonObject robj = response.object();
QString tag = robj[QStringLiteral("tag_name")].toString();
if (tag.startsWith('v')) {
QStringRef ver = tag.midRef(1);
if (ver.compare(QLatin1String(APP_VERSION)) > 0) {
// Show Update Notification
QMessageBox msg;
msg.setText(QStringLiteral("<font size=4>") % tr("New Update is Available") % QStringLiteral("</font>"));
msg.setInformativeText(
QStringLiteral("<font size=4 style=\"white-space:nowrap;\" align=center><p>") %
tr("There is a new update available for the launcher.") %
QStringLiteral("<br />") %
tr("It is recomended that you update.") %
QStringLiteral("</p><p>") %
tr("Installed Version: ") %
QStringLiteral(APP_VERSION) %
QStringLiteral("<br />") %
tr("Latest Version: ") %
ver %
QStringLiteral("</p><p><a href=\"") %
robj[QStringLiteral("html_url")].toString() %
QStringLiteral("\">") %
tr("Download Page") %
QStringLiteral("</a></p></font>"));
msg.setMinimumWidth(1000);
msg.exec();
}
}
}
void Updater::receivedAPI() {
if (api->error() != QNetworkReply::NoError) {
emit error(api->errorString());
return;
}
QByteArray data = api->readAll();
api->close();
api->deleteLater();
api = nullptr;
QJsonDocument response = QJsonDocument::fromJson(data);
QJsonObject robj = response.object();
if (robj[QStringLiteral("error")].toInt() != 0) {
emit error(robj[QStringLiteral("errorMessage")].toString());
return;
}
QString have = settings.value(QStringLiteral("downloadId")).toString();
version = robj[QStringLiteral("downloadId")].toString();
if (have == version && OPENRCT2_HOMEDIR.cd(QStringLiteral(OPENRCT2_BIN))) {
emit installed();
} else {
size = robj[QStringLiteral("fileSize")].toInt();
hash = QByteArray::fromHex(robj[QStringLiteral("fileHash")].toString().toLatin1());
githash = QByteArray::fromHex(robj[QStringLiteral("gitHash")].toString().toLatin1());
QNetworkRequest request(robj[QStringLiteral("url")].toString());
bundle = net.get(request);
connect(bundle, &QNetworkReply::finished, this, &Updater::receivedBundle);
connect(bundle, &QNetworkReply::downloadProgress, this, &Updater::downloadProgress);
}
}
void Updater::receivedBundle() {
if (bundle->error() != QNetworkReply::NoError) {
emit error(bundle->errorString());
return;
}
QByteArray data = bundle->readAll();
bundle->close();
bundle->deleteLater();
bundle = nullptr;
if (data.size() != size) {
emit error(tr("Invalid Download"));
return;
}
QByteArray fhash = QCryptographicHash::hash(data, QCryptographicHash::Algorithm::Sha256);
if (fhash != hash) {
emit error(tr("Invalid Download"));
return;
}
QDir bin = OPENRCT2_HOMEDIR;
if (bin.cd(QStringLiteral(OPENRCT2_BIN))) {
bin.removeRecursively();
if (!bin.mkpath(QStringLiteral("."))) {
emit error(tr("bin dir not created"));
return;
}
} else {
bin.mkpath(QStringLiteral(OPENRCT2_BIN));
if (!bin.cd(QStringLiteral(OPENRCT2_BIN))) {
emit error(tr("bin dir not created"));
return;
}
}
if (extract(data, bin)) {
emit installed();
settings.setValue(QStringLiteral("downloadId"), version);
settings.setValue(QStringLiteral("gitHash"), githash);
settings.sync();
} else {
emit error(tr("Error extracting archive"));
}
}
bool Updater::extract(QByteArray &data, QDir &bin) {
QBuffer buffer(&data);
#ifdef Q_OS_LINUX
GZipReadFilter gzip(&buffer);
return extractTar(&gzip, bin);
#else
return extractZip(&buffer, bin);
#endif
}