Skip to content

Commit 9cb7551

Browse files
committed
read updater url from current.json file
1 parent 7f396fd commit 9cb7551

File tree

8 files changed

+88
-42
lines changed

8 files changed

+88
-42
lines changed

currentversionfetcher.cpp

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include "currentversionfetcher.h"
2+
#include "system.h"
23

34
#include <QDebug>
45
#include <QUrl>
56
#include <QNetworkRequest>
67
#include <QNetworkReply>
78
#include <QJsonDocument>
89
#include <QJsonObject>
10+
#include <QJsonArray>
911

1012
CurrentVersionFetcher::CurrentVersionFetcher(QObject* parent) : QObject(parent), manager_(new QNetworkAccessManager(this))
1113
{
@@ -18,37 +20,83 @@ void CurrentVersionFetcher::fetchCurrentVersion(QString url)
1820
manager_->get(request);
1921
}
2022

23+
void ComponentVersionFetcher(QJsonObject jsonObject, QString componentSlug, QString componentSystem, QString *componentVersion, QString *componentUrl)
24+
{
25+
QString componentMirror;
26+
QString componentPath;
27+
28+
QJsonObject componentObject = jsonObject[componentSlug].toObject();
29+
if (componentObject.isEmpty()) {
30+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSlug << "” key";
31+
} else {
32+
QJsonValue version = componentObject.value("version");
33+
if (version == QJsonValue::Undefined) {
34+
qDebug() << "ComponentVersionFetcher: undefined “version” value for" << componentSlug;
35+
} else {
36+
*componentVersion = version.toString();
37+
}
38+
39+
QJsonArray mirrorsArray = componentObject["mirrors"].toArray();
40+
if (!mirrorsArray.count()) {
41+
qDebug() << "ComponentVersionFetcher: undefined “mirrors” key for " << componentSlug;
42+
} else {
43+
componentMirror = mirrorsArray.first().toString();
44+
}
45+
46+
QJsonObject parcelsObject = componentObject["parcels"].toObject();
47+
if (parcelsObject.isEmpty()) {
48+
qDebug() << "ComponentVersionFetcher: undefined “parcels” key for" << componentSlug;
49+
} else {
50+
QJsonObject systemObject = parcelsObject[componentSystem].toObject();
51+
if (systemObject.isEmpty()) {
52+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSystem << "” key for " << componentSlug;
53+
} else {
54+
QJsonValue path = systemObject.value("path");
55+
if (path == QJsonValue::Undefined) {
56+
qDebug() << "ComponentVersionFetcher: undefined “path” value for" << componentSlug;
57+
} else {
58+
componentPath = path.toString();
59+
}
60+
}
61+
}
62+
63+
*componentUrl = componentMirror + "/" + componentPath;
64+
if (*componentUrl == "/") {
65+
*componentUrl = "";
66+
}
67+
68+
qDebug() << "ComponentVersionFetcher: fetched component =" << componentSlug;
69+
qDebug() << "ComponentVersionFetcher: fetched system =" << componentSystem;
70+
qDebug() << "ComponentVersionFetcher: fetched version =" << *componentVersion;
71+
qDebug() << "ComponentVersionFetcher: fetched mirror =" << componentMirror;
72+
qDebug() << "ComponentVersionFetcher: fetched path =" << componentPath;
73+
qDebug() << "ComponentVersionFetcher: fetched url =" << *componentUrl;
74+
}
75+
}
76+
2177
void CurrentVersionFetcher::reply(QNetworkReply* reply)
2278
{
23-
QString gameVersion;
2479
QString updaterVersion;
80+
QString updaterUrl;
81+
QString gameVersion;
2582

2683
if (reply->error() != QNetworkReply::NoError) {
2784
qDebug() << "CurrentVersionFetcher: network error";
28-
emit onCurrentVersions(updaterVersion, gameVersion);
85+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion);
2986
return;
3087
}
3188

3289
QJsonParseError error;
3390
QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error);
3491
if (error.error != QJsonParseError::NoError) {
3592
qDebug() << "CurrentVersionFetcher: JSON parsing error";
36-
emit onCurrentVersions(updaterVersion, gameVersion);
93+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion);
3794
return;
3895
}
96+
3997
QJsonObject jsonObject = json.object();
4098

41-
QJsonObject updaterObject = jsonObject["updater"].toObject();
42-
if (!updaterObject.isEmpty()) {
43-
QJsonValue version = updaterObject.value("version");
44-
if (version != QJsonValue::Undefined) {
45-
updaterVersion = version.toString();
46-
} else {
47-
qDebug() << "CurrentVersionFetcher: undefined “version” updater value";
48-
}
49-
} else {
50-
qDebug() << "CurrentVersionFetcher: undefined “updater” key";
51-
}
99+
ComponentVersionFetcher(jsonObject, "updater", Sys::updaterSystem(), &updaterVersion, &updaterUrl);
52100

53101
QJsonObject gameObject = jsonObject["game"].toObject();
54102
if (!gameObject.isEmpty()) {
@@ -62,8 +110,6 @@ void CurrentVersionFetcher::reply(QNetworkReply* reply)
62110
qDebug() << "CurrentVersionFetcher: undefined “game” key";
63111
}
64112

65-
qDebug() << "CurrentVersionFetcher: fetched versions: updater =" << updaterVersion << "game =" << gameVersion;
66-
67-
emit onCurrentVersions(updaterVersion, gameVersion);
113+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion);
68114
}
69115

currentversionfetcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CurrentVersionFetcher : public QObject
1414
void fetchCurrentVersion(QString url);
1515

1616
signals:
17-
void onCurrentVersions(QString updaterVersion, QString gameVersion);
17+
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString gameVersion);
1818

1919
private slots:
2020
void reply(QNetworkReply* reply);

osx.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ QString archiveName()
3636
return "macos-amd64.zip";
3737
}
3838

39+
QString updaterSystem()
40+
{
41+
return "macos-amd64";
42+
}
43+
3944
QString defaultInstallPath()
4045
{
4146
return QDir::homePath() + "/Games/Unvanquished";
@@ -144,11 +149,6 @@ bool updateUpdater(const QString& updaterArchive)
144149
return true;
145150
}
146151

147-
QString updaterArchiveName()
148-
{
149-
return "UnvUpdaterOSX.zip";
150-
}
151-
152152
std::string getCertStore()
153153
{
154154
return ""; // Not used on OSX.

qmldownloader.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace {
1111
static const QRegularExpression COMMAND_REGEX("%command%");
12-
static QString UPDATER_BASE_URL("https://github.com/Unvanquished/updater/releases/download");
1312
} // namespace
1413

1514
QmlDownloader::QmlDownloader() : downloadSpeed_(0),
@@ -199,14 +198,15 @@ void QmlDownloader::stopAria()
199198
// Initiates an asynchronous request for the latest available versions.
200199
void QmlDownloader::checkForUpdate()
201200
{
202-
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString)), this, SLOT(onCurrentVersions(QString, QString)));
201+
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString, QString)), this, SLOT(onCurrentVersions(QString, QString, QString)));
203202
fetcher_.fetchCurrentVersion("https://cdn.unvanquished.net/current.json");
204203
}
205204

206205
// Receives the results of the checkForUpdate request.
207-
void QmlDownloader::onCurrentVersions(QString updaterVersion, QString gameVersion)
206+
void QmlDownloader::onCurrentVersions(QString updaterVersion, QString updaterUrl, QString gameVersion)
208207
{
209208
latestUpdaterVersion_ = updaterVersion;
209+
latestUpdaterUrl_ = updaterUrl;
210210
latestGameVersion_ = gameVersion;
211211
}
212212

@@ -216,14 +216,13 @@ void QmlDownloader::onCurrentVersions(QString updaterVersion, QString gameVersio
216216
void QmlDownloader::autoLaunchOrUpdate()
217217
{
218218
qDebug() << "Previously-installed game version:" << settings_.currentVersion();
219-
QString gitUpdaterVersion = "v" + latestUpdaterVersion_;
219+
QString gitUpdaterVersion = "v" + latestUpdaterVersion_;
220220
if (!latestUpdaterVersion_.isEmpty() && gitUpdaterVersion != QString(GIT_VERSION)) {
221221
qDebug() << "Updater update to version" << latestUpdaterVersion_ << "required";
222-
QString url = UPDATER_BASE_URL + "/" + gitUpdaterVersion + "/" + Sys::updaterArchiveName();
223222
temp_dir_.reset(new QTemporaryDir());
224223
worker_ = new DownloadWorker(ariaLogFilename_);
225224
worker_->setDownloadDirectory(QDir(temp_dir_->path()).canonicalPath().toStdString());
226-
worker_->addUpdaterUri(url.toStdString());
225+
worker_->addUpdaterUri(latestUpdaterUrl_.toStdString());
227226
worker_->moveToThread(&thread_);
228227
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
229228
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));

qmldownloader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public slots:
6464
void setTotalSize(int size);
6565
void setCompletedSize(int size);
6666
void onDownloadEvent(int event);
67-
void onCurrentVersions(QString updaterVersion, QString gameVersion);
67+
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString gameVersion);
6868

6969
Q_INVOKABLE void startUpdate();
7070
Q_INVOKABLE void toggleDownload();
@@ -87,8 +87,9 @@ public slots:
8787
DownloadWorker* worker_;
8888
DownloadTimeCalculator downloadTime_;
8989
Settings settings_;
90-
QString latestGameVersion_;
9190
QString latestUpdaterVersion_;
91+
QString latestUpdaterUrl_;
92+
QString latestGameVersion_;
9293
DownloadState state_;
9394
std::unique_ptr<QTemporaryDir> temp_dir_;
9495

system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace Sys {
1010
QString archiveName();
11+
QString updaterSystem();
1112
QString defaultInstallPath();
1213
bool validateInstallPath(const QString& installPath); // Checks installing as root in homepath on Linux
1314
bool install();
1415
bool installUpdater(const QString& installPath); // Copies current application to <install path>/updater[.exe|.app]
1516
bool updateUpdater(const QString& updaterArchive);
16-
QString updaterArchiveName();
1717
std::string getCertStore();
1818
QSettings* makePersistentSettings(QObject* parent);
1919
QString getGameCommand(const QString& installPath); // Substitution for %command%

unix.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ QString archiveName()
1515
return "linux-amd64.zip";
1616
}
1717

18+
QString updaterSystem()
19+
{
20+
return "linux-amd64";
21+
}
22+
1823
void migrateHomePath()
1924
{
2025
QString legacyHomePath = QDir::homePath() + "/.unvanquished";
@@ -183,11 +188,6 @@ bool updateUpdater(const QString& updaterArchive)
183188
return true;
184189
}
185190

186-
QString updaterArchiveName()
187-
{
188-
return "UnvUpdaterLinux.zip";
189-
}
190-
191191
std::string getCertStore()
192192
{
193193
// From Go: https://golang.org/src/crypto/x509/root_linux.go

win.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ QString archiveName()
135135
}
136136
}
137137

138+
QString updaterSystem()
139+
{
140+
return "windows-i686";
141+
}
142+
138143
QString defaultInstallPath()
139144
{
140145
static const char* PROGRAM_FILES_VAR = "programfiles";
@@ -244,11 +249,6 @@ bool updateUpdater(const QString& updaterArchive)
244249
return true;
245250
}
246251

247-
QString updaterArchiveName()
248-
{
249-
return "UnvUpdaterWin.zip";
250-
}
251-
252252
std::string getCertStore()
253253
{
254254
return ""; // Not used on Windows.

0 commit comments

Comments
 (0)