Skip to content

Commit 878fe7a

Browse files
committed
Fetch all URLs from a single URL
1 parent 1ef5537 commit 878fe7a

15 files changed

+217
-79
lines changed

News.qml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Item {
3232
bottomMargin: 10
3333
}
3434

35+
function fetchFallbackNews() {
36+
console.log("fetching fallback posts json");
37+
fetchNews(downloader.newsFallbackUrl);
38+
}
39+
3540
function fetchNews(jsonUrl) {
3641
var news = new XMLHttpRequest();
3742

@@ -55,11 +60,8 @@ Item {
5560
}
5661

5762
if (newsObj === null) {
58-
var fallbackJsonUrl = 'qrc:/resources/disconnected_posts.json';
59-
60-
if (jsonUrl !== fallbackJsonUrl) {
61-
console.log("fetching fallback posts json");
62-
fetchNews(fallbackJsonUrl);
63+
if (jsonUrl !== downloader.newsFallbackUrl) {
64+
fetchFallbackNews();
6365
return;
6466
}
6567
}
@@ -98,6 +100,14 @@ Item {
98100
news.send();
99101
}
100102

103+
Connections {
104+
target: splashController
105+
106+
onNewsUrlFetched: {
107+
fetchNews(newsUrl);
108+
}
109+
}
110+
101111
SwipeView {
102112
id: swipe
103113

@@ -111,7 +121,10 @@ Item {
111121
}
112122

113123
Component.onCompleted: {
114-
fetchNews('https://unvanquished.net/api/get_recent_posts/');
124+
var newsUrl = downloader.newsUrl;
125+
if (newsUrl != "") {
126+
fetchNews(newsUrl);
127+
}
115128
}
116129
}
117130

currentversionfetcher.cpp

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
*/
1717

1818
#include "currentversionfetcher.h"
19+
#include "system.h"
1920

2021
#include <QDebug>
2122
#include <QUrl>
2223
#include <QNetworkRequest>
2324
#include <QNetworkReply>
2425
#include <QJsonDocument>
2526
#include <QJsonObject>
27+
#include <QJsonArray>
2628

2729
CurrentVersionFetcher::CurrentVersionFetcher(QObject* parent) : QObject(parent), manager_(new QNetworkAccessManager(this))
2830
{
@@ -35,35 +37,90 @@ void CurrentVersionFetcher::fetchCurrentVersion(QString url)
3537
manager_->get(request);
3638
}
3739

40+
void ComponentVersionFetcher(QJsonObject jsonObject, QString componentSlug, QString componentSystem, QString *componentVersion, QString *componentUrl)
41+
{
42+
QString componentMirror;
43+
QString componentPath;
44+
45+
QJsonObject componentsObject = jsonObject["components"].toObject();
46+
47+
QJsonObject componentObject = componentsObject[componentSlug].toObject();
48+
if (componentObject.isEmpty()) {
49+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSlug << "” key";
50+
} else {
51+
QJsonValue version = componentObject.value("version");
52+
if (version == QJsonValue::Undefined) {
53+
qDebug() << "ComponentVersionFetcher: undefined “version” value for" << componentSlug;
54+
} else {
55+
*componentVersion = version.toString();
56+
}
57+
58+
QJsonArray mirrorsArray = componentObject["mirrors"].toArray();
59+
if (!mirrorsArray.count()) {
60+
qDebug() << "ComponentVersionFetcher: undefined “mirrors” key for " << componentSlug;
61+
} else {
62+
componentMirror = mirrorsArray.first().toString();
63+
}
64+
65+
QJsonObject parcelsObject = componentObject["parcels"].toObject();
66+
if (parcelsObject.isEmpty()) {
67+
qDebug() << "ComponentVersionFetcher: undefined “parcels” key for" << componentSlug;
68+
} else {
69+
QJsonObject systemObject = parcelsObject[componentSystem].toObject();
70+
if (systemObject.isEmpty()) {
71+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSystem << "” key for " << componentSlug;
72+
} else {
73+
QJsonValue path = systemObject.value("path");
74+
if (path == QJsonValue::Undefined) {
75+
qDebug() << "ComponentVersionFetcher: undefined “path” value for" << componentSlug;
76+
} else {
77+
componentPath = path.toString();
78+
}
79+
}
80+
}
81+
82+
*componentUrl = componentMirror + componentPath;
83+
84+
qDebug() << "ComponentVersionFetcher: fetched component =" << componentSlug;
85+
qDebug() << "ComponentVersionFetcher: fetched system =" << componentSystem;
86+
qDebug() << "ComponentVersionFetcher: fetched version =" << *componentVersion;
87+
qDebug() << "ComponentVersionFetcher: fetched mirror =" << componentMirror;
88+
qDebug() << "ComponentVersionFetcher: fetched path =" << componentPath;
89+
qDebug() << "ComponentVersionFetcher: fetched url =" << *componentUrl;
90+
}
91+
}
92+
3893
void CurrentVersionFetcher::reply(QNetworkReply* reply)
3994
{
40-
QString game;
41-
QString updater;
95+
QString updaterVersion;
96+
QString updaterUrl;
97+
QString gameVersion;
98+
QString gameUrl;
99+
QString newsVersion;
100+
QString newsUrl;
101+
42102
if (reply->error() != QNetworkReply::NoError) {
43103
qDebug() << "CurrentVersionFetcher: network error";
44-
emit onCurrentVersions(updater, game);
104+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
45105
return;
46106
}
107+
47108
QJsonParseError error;
48109
QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error);
49110
if (error.error != QJsonParseError::NoError) {
50111
qDebug() << "CurrentVersionFetcher: JSON parsing error";
51-
emit onCurrentVersions(updater, game);
112+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
52113
return;
53114
}
54-
QJsonValue value = json.object().value("updater");
55-
if (value != QJsonValue::Undefined) {
56-
updater = value.toString();
57-
} else {
58-
qDebug() << "CurrentVersionFetcher: undefined “updater” value";
59-
}
60-
value = json.object().value("unvanquished");
61-
if (value != QJsonValue::Undefined) {
62-
game = value.toString();
63-
} else {
64-
qDebug() << "CurrentVersionFetcher: undefined “unvanquished” value";
65-
}
66-
qDebug() << "CurrentVersionFetcher: fetched versions: updater =" << updater << "game =" << game;
67-
emit onCurrentVersions(updater, game);
115+
116+
QJsonObject jsonObject = json.object();
117+
118+
ComponentVersionFetcher(jsonObject, "updater", Sys::updaterSystem(), &updaterVersion, &updaterUrl);
119+
120+
ComponentVersionFetcher(jsonObject, "game", "all-all", &gameVersion, &gameUrl);
121+
122+
ComponentVersionFetcher(jsonObject, "news", "all-all", &newsVersion, &newsUrl);
123+
124+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
68125
}
69126

currentversionfetcher.h

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

3333
signals:
34-
void onCurrentVersions(QString updater, QString game);
34+
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString gameVersion, QString gameUrl, QString newsUrl);
3535

3636
private slots:
3737
void reply(QNetworkReply* reply);

deployment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Network resources used by the updater
22
- News REST endpoint which returns links to Wordpress articles on unvanquished.net. The featured image in each news article must be a type which is supported by the updater (see issue #51). Currently PNG and JPEG are known to work.
3-
- versions.json file on unvanquished.net, used to determine whether update is needed
3+
- current.json file on unvanquished.net, used to determine whether update is needed
44
- Github releases. These are targeted by download links on unvanquished.net and by the updater's self-update process.
55
- Torrent URL used to download the latest game version
66

@@ -36,4 +36,4 @@
3636
```
3737
2. Upload `UnvUpdaterWin.zip` and `UnvanquishedUpdater.exe` from `build-docker/release-win/`.
3838
39-
4. Bump the updater version on unvanquished.net to the new tag, so that it is reflected in versions.json and the download links.
39+
4. Bump the updater version on unvanquished.net to the new tag, so that it is reflected in current.json and the download links.

gamelauncher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ GameLauncher::GameLauncher(const QString& connectUrl, const Settings& settings)
3131
// Does our version of daemon have -connect-trusted?
3232
static bool haveConnectTrusted(const QString& gameVersion)
3333
{
34-
// Updater version up to v0.2.0 may set "unknown" as game version if versions.json request fails
34+
// Updater version up to v0.2.0 may set "unknown" as game version if current.json request fails
3535
if (gameVersion == "unknown")
3636
return false;
3737
// Hacky string comparison, assume we won't go down to 0.9 or up to 0.100 :)

linux.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ QString archiveName()
8282
return "linux-amd64.zip";
8383
}
8484

85+
QString updaterSystem()
86+
{
87+
return "linux-amd64";
88+
}
89+
8590
void migrateHomePath()
8691
{
8792
QString legacyHomePath = QDir::homePath() + "/.unvanquished";
@@ -265,11 +270,6 @@ bool updateUpdater(const QString& updaterArchive, const QString& connectUrl)
265270
return false;
266271
}
267272

268-
QString updaterArchiveName()
269-
{
270-
return "UnvUpdaterLinux.zip";
271-
}
272-
273273
std::string getCertStore()
274274
{
275275
// From Go: https://golang.org/src/crypto/x509/root_linux.go

main.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct CommandLineOptions {
6363
QString ariaLogFilename;
6464
int splashMilliseconds = 3000;
6565
RelaunchCommand relaunchCommand = RelaunchCommand::NONE;
66-
QString updateUpdaterVersion;
66+
QString updateUpdaterUrl;
6767
QString connectUrl;
6868
};
6969

@@ -122,13 +122,16 @@ CommandLineOptions getCommandLineOptions(const QApplication& app) {
122122
splashMsOption.setValueName("duration in milliseconds");
123123
QCommandLineOption internalCommandOption("internalcommand");
124124
internalCommandOption.setValueName("command");
125+
QCommandLineOption updaterUrl("updaterurl");
126+
updaterUrl.setValueName("url");
125127
QCommandLineParser optionParser;
126128
optionParser.addHelpOption();
127129
optionParser.addVersionOption();
128130
optionParser.addOption(logFileNameOption);
129131
optionParser.addOption(ariaLogFilenameOption);
130132
optionParser.addOption(splashMsOption);
131133
optionParser.addOption(internalCommandOption);
134+
optionParser.addOption(updaterUrl);
132135
optionParser.addPositionalArgument("URL", "address of Unvanquished server to connect to", "[URL]");
133136
optionParser.process(app);
134137
CommandLineOptions options;
@@ -145,9 +148,13 @@ CommandLineOptions getCommandLineOptions(const QApplication& app) {
145148
options.relaunchCommand = RelaunchCommand::PLAY_NOW;
146149
} else if (command == "updategame") {
147150
options.relaunchCommand = RelaunchCommand::UPDATE_GAME;
148-
} else if (command.startsWith("updateupdater:")) {
151+
} else if (command.startsWith("updateupdater")) {
149152
options.relaunchCommand = RelaunchCommand::UPDATE_UPDATER;
150-
options.updateUpdaterVersion = command.section(':', 1);
153+
if (optionParser.isSet(updaterUrl)) {
154+
options.updateUpdaterUrl = optionParser.value(updaterUrl);
155+
} else {
156+
options.updateUpdaterUrl = "";
157+
}
151158
} else {
152159
argParseError("Invalid --internalcommand option: " + command);
153160
}
@@ -209,9 +216,9 @@ int main(int argc, char *argv[])
209216
}
210217

211218
SplashController splashController(
212-
options.relaunchCommand, options.updateUpdaterVersion, options.connectUrl, settings);
219+
options.relaunchCommand, options.updateUpdaterUrl, options.connectUrl, settings);
213220
splashController.checkForUpdate();
214-
QmlDownloader downloader(options.ariaLogFilename, options.connectUrl, settings);
221+
QmlDownloader downloader(options.ariaLogFilename, options.connectUrl, splashController, settings);
215222
QQmlApplicationEngine engine;
216223
engine.addImportPath(QLatin1String("qrc:/"));
217224
engine.addImageProvider(QLatin1String("fluidicons"), new IconsImageProvider());

osx.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ QString archiveName()
5353
return "macos-amd64.zip";
5454
}
5555

56+
QString updaterSystem()
57+
{
58+
return "macos-amd64";
59+
}
60+
5661
QString defaultInstallPath()
5762
{
5863
return QDir::homePath() + "/Games/Unvanquished";
@@ -163,11 +168,6 @@ bool updateUpdater(const QString& updaterArchive, const QString&)
163168
return true;
164169
}
165170

166-
QString updaterArchiveName()
167-
{
168-
return "UnvUpdaterOSX.zip";
169-
}
170-
171171
std::string getCertStore()
172172
{
173173
return ""; // Not used on OSX.

qmldownloader.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include "qmldownloader.h"
2424
#include "system.h"
2525

26-
static const QString UPDATER_BASE_URL("https://github.com/Unvanquished/updater/releases/download");
2726

28-
QmlDownloader::QmlDownloader(QString ariaLogFilename, QString connectUrl, Settings& settings) :
27+
QmlDownloader::QmlDownloader(QString ariaLogFilename, QString connectUrl, SplashController& splashController, Settings& settings) :
2928
ariaLogFilename_(ariaLogFilename),
3029
connectUrl_(connectUrl),
30+
splashController_(splashController),
3131
settings_(settings),
3232
downloadSpeed_(0),
3333
uploadSpeed_(0),
@@ -42,6 +42,14 @@ QmlDownloader::~QmlDownloader()
4242
stopAria();
4343
}
4444

45+
QString QmlDownloader::newsFallbackUrl() const {
46+
return "qrc:/resources/disconnected_posts.json";
47+
}
48+
49+
QString QmlDownloader::newsUrl() const {
50+
return splashController_.newsUrl();
51+
}
52+
4553
int QmlDownloader::downloadSpeed() const {
4654
return downloadSpeed_;
4755
}
@@ -155,6 +163,9 @@ void QmlDownloader::startUpdate(const QString& selectedInstallPath)
155163
emit fatalMessage("Install dir not writable. Please select another");
156164
return;
157165
}
166+
167+
QString gameUrl = splashController_.gameUrl();
168+
qDebug() << "Using torrent file:" << gameUrl;
158169
// Persist the install path only now that download has been initiated and we know the path is good
159170
emit statusMessage("Installing to " + dir.canonicalPath());
160171
if (settings_.installPath() != selectedInstallPath) {
@@ -166,7 +177,7 @@ void QmlDownloader::startUpdate(const QString& selectedInstallPath)
166177
setState(DOWNLOADING);
167178
worker_ = new DownloadWorker(ariaLogFilename_);
168179
worker_->setDownloadDirectory(dir.canonicalPath().toStdString());
169-
worker_->addTorrent("https://cdn.unvanquished.net/current.torrent");
180+
worker_->addTorrent(gameUrl.toStdString());
170181
worker_->moveToThread(&thread_);
171182
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
172183
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));
@@ -201,14 +212,13 @@ void QmlDownloader::stopAria()
201212
}
202213
}
203214

204-
void QmlDownloader::startUpdaterUpdate(QString version)
215+
void QmlDownloader::startUpdaterUpdate(QString updaterUrl)
205216
{
206-
QString url = UPDATER_BASE_URL + "/" + version + "/" + Sys::updaterArchiveName();
207217
temp_dir_.reset(new QTemporaryDir());
208218
worker_ = new DownloadWorker(ariaLogFilename_);
209219
worker_->setDownloadDirectory(QDir(temp_dir_->path()).canonicalPath().toStdString());
210220
worker_->setConnectUrl(connectUrl_);
211-
worker_->addUpdaterUri(url.toStdString());
221+
worker_->addUpdaterUri(updaterUrl.toStdString());
212222
worker_->moveToThread(&thread_);
213223
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
214224
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));

0 commit comments

Comments
 (0)