Skip to content

Commit 014fc36

Browse files
committed
Fetch all URLs from a single URL
1 parent cb1d60c commit 014fc36

15 files changed

+201
-83
lines changed

News.qml

Lines changed: 15 additions & 9 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

@@ -109,10 +119,6 @@ Item {
109119
left: leftButton.right
110120
right: rightButton.left
111121
}
112-
113-
Component.onCompleted: {
114-
fetchNews('https://unvanquished.net/api/get_recent_posts/');
115-
}
116122
}
117123

118124
PageIndicator {

currentversionfetcher.cpp

Lines changed: 76 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,91 @@ 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 componentObject = jsonObject[componentSlug].toObject();
46+
if (componentObject.isEmpty()) {
47+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSlug << "” key";
48+
} else {
49+
QJsonValue version = componentObject.value("version");
50+
if (version == QJsonValue::Undefined) {
51+
qDebug() << "ComponentVersionFetcher: undefined “version” value for" << componentSlug;
52+
} else {
53+
*componentVersion = version.toString();
54+
}
55+
56+
QJsonArray mirrorsArray = componentObject["mirrors"].toArray();
57+
if (!mirrorsArray.count()) {
58+
qDebug() << "ComponentVersionFetcher: undefined “mirrors” key for " << componentSlug;
59+
} else {
60+
componentMirror = mirrorsArray.first().toString();
61+
}
62+
63+
QJsonObject parcelsObject = componentObject["parcels"].toObject();
64+
if (parcelsObject.isEmpty()) {
65+
qDebug() << "ComponentVersionFetcher: undefined “parcels” key for" << componentSlug;
66+
} else {
67+
QJsonObject systemObject = parcelsObject[componentSystem].toObject();
68+
if (systemObject.isEmpty()) {
69+
qDebug() << "ComponentVersionFetcher: undefined “" << componentSystem << "” key for " << componentSlug;
70+
} else {
71+
QJsonValue path = systemObject.value("path");
72+
if (path == QJsonValue::Undefined) {
73+
qDebug() << "ComponentVersionFetcher: undefined “path” value for" << componentSlug;
74+
} else {
75+
componentPath = path.toString();
76+
}
77+
}
78+
}
79+
80+
*componentUrl = componentMirror + "/" + componentPath;
81+
if (*componentUrl == "/") {
82+
*componentUrl = "";
83+
}
84+
85+
qDebug() << "ComponentVersionFetcher: fetched component =" << componentSlug;
86+
qDebug() << "ComponentVersionFetcher: fetched system =" << componentSystem;
87+
qDebug() << "ComponentVersionFetcher: fetched version =" << *componentVersion;
88+
qDebug() << "ComponentVersionFetcher: fetched mirror =" << componentMirror;
89+
qDebug() << "ComponentVersionFetcher: fetched path =" << componentPath;
90+
qDebug() << "ComponentVersionFetcher: fetched url =" << *componentUrl;
91+
}
92+
}
93+
3894
void CurrentVersionFetcher::reply(QNetworkReply* reply)
3995
{
40-
QString game;
41-
QString updater;
96+
QString updaterVersion;
97+
QString updaterUrl;
98+
QString gameVersion;
99+
QString gameUrl;
100+
QString newsVersion;
101+
QString newsUrl;
102+
42103
if (reply->error() != QNetworkReply::NoError) {
43104
qDebug() << "CurrentVersionFetcher: network error";
44-
emit onCurrentVersions(updater, game);
105+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
45106
return;
46107
}
108+
47109
QJsonParseError error;
48110
QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error);
49111
if (error.error != QJsonParseError::NoError) {
50112
qDebug() << "CurrentVersionFetcher: JSON parsing error";
51-
emit onCurrentVersions(updater, game);
113+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
52114
return;
53115
}
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);
116+
117+
QJsonObject jsonObject = json.object();
118+
119+
ComponentVersionFetcher(jsonObject, "updater", Sys::updaterSystem(), &updaterVersion, &updaterUrl);
120+
121+
ComponentVersionFetcher(jsonObject, "game", "all-all", &gameVersion, &gameUrl);
122+
123+
ComponentVersionFetcher(jsonObject, "news", "all-all", &newsVersion, &newsUrl);
124+
125+
emit onCurrentVersions(updaterVersion, updaterUrl, gameVersion, gameUrl, newsUrl);
68126
}
69127

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

@@ -35,4 +35,4 @@
3535
```
3636
2. Upload `UnvUpdaterWin.zip` and `UnvanquishedUpdater.exe` from `build-docker/release-win/`.
3737
38-
4. Bump the updater version on unvanquished.net to the new tag, so that it is reflected in versions.json and the download links.
38+
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
@@ -79,6 +79,11 @@ QString archiveName()
7979
return "linux-amd64.zip";
8080
}
8181

82+
QString updaterSystem()
83+
{
84+
return "linux-amd64";
85+
}
86+
8287
void migrateHomePath()
8388
{
8489
QString legacyHomePath = QDir::homePath() + "/.unvanquished";
@@ -247,11 +252,6 @@ bool updateUpdater(const QString& updaterArchive, const QString& connectUrl)
247252
return false;
248253
}
249254

250-
QString updaterArchiveName()
251-
{
252-
return "UnvUpdaterLinux.zip";
253-
}
254-
255255
std::string getCertStore()
256256
{
257257
// 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
@@ -62,7 +62,7 @@ struct CommandLineOptions {
6262
QString ariaLogFilename;
6363
int splashMilliseconds = 3000;
6464
RelaunchCommand relaunchCommand = RelaunchCommand::NONE;
65-
QString updateUpdaterVersion;
65+
QString updateUpdaterUrl;
6666
QString connectUrl;
6767
};
6868

@@ -121,13 +121,16 @@ CommandLineOptions getCommandLineOptions(const QApplication& app) {
121121
splashMsOption.setValueName("duration in milliseconds");
122122
QCommandLineOption internalCommandOption("internalcommand");
123123
internalCommandOption.setValueName("command");
124+
QCommandLineOption updaterUrl("updaterurl");
125+
updaterUrl.setValueName("url");
124126
QCommandLineParser optionParser;
125127
optionParser.addHelpOption();
126128
optionParser.addVersionOption();
127129
optionParser.addOption(logFileNameOption);
128130
optionParser.addOption(ariaLogFilenameOption);
129131
optionParser.addOption(splashMsOption);
130132
optionParser.addOption(internalCommandOption);
133+
optionParser.addOption(updaterUrl);
131134
optionParser.addPositionalArgument("URL", "address of Unvanquished server to connect to", "[URL]");
132135
optionParser.process(app);
133136
CommandLineOptions options;
@@ -144,9 +147,13 @@ CommandLineOptions getCommandLineOptions(const QApplication& app) {
144147
options.relaunchCommand = RelaunchCommand::PLAY_NOW;
145148
} else if (command == "updategame") {
146149
options.relaunchCommand = RelaunchCommand::UPDATE_GAME;
147-
} else if (command.startsWith("updateupdater:")) {
150+
} else if (command.startsWith("updateupdater")) {
148151
options.relaunchCommand = RelaunchCommand::UPDATE_UPDATER;
149-
options.updateUpdaterVersion = command.section(':', 1);
152+
if (optionParser.isSet(updaterUrl)) {
153+
options.updateUpdaterUrl = optionParser.value(updaterUrl);
154+
} else {
155+
options.updateUpdaterUrl = "";
156+
}
150157
} else {
151158
argParseError("Invalid --internalcommand option: " + command);
152159
}
@@ -208,9 +215,9 @@ int main(int argc, char *argv[])
208215
}
209216

210217
SplashController splashController(
211-
options.relaunchCommand, options.updateUpdaterVersion, options.connectUrl, settings);
218+
options.relaunchCommand, options.updateUpdaterUrl, options.connectUrl, settings);
212219
splashController.checkForUpdate();
213-
QmlDownloader downloader(options.ariaLogFilename, options.connectUrl, settings);
220+
QmlDownloader downloader(options.ariaLogFilename, options.connectUrl, splashController, settings);
214221
QQmlApplicationEngine engine;
215222
engine.addImportPath(QLatin1String("qrc:/"));
216223
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: 12 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,10 @@ QmlDownloader::~QmlDownloader()
4242
stopAria();
4343
}
4444

45+
QString QmlDownloader::newsFallbackUrl() const {
46+
return "qrc:/resources/disconnected_posts.json";
47+
}
48+
4549
int QmlDownloader::downloadSpeed() const {
4650
return downloadSpeed_;
4751
}
@@ -155,6 +159,9 @@ void QmlDownloader::startUpdate(const QString& selectedInstallPath)
155159
emit fatalMessage("Install dir not writable. Please select another");
156160
return;
157161
}
162+
163+
QString gameUrl = splashController_.gameUrl();
164+
qDebug() << "Using torrent file:" << gameUrl;
158165
// Persist the install path only now that download has been initiated and we know the path is good
159166
emit statusMessage("Installing to " + dir.canonicalPath());
160167
if (settings_.installPath() != selectedInstallPath) {
@@ -166,7 +173,7 @@ void QmlDownloader::startUpdate(const QString& selectedInstallPath)
166173
setState(DOWNLOADING);
167174
worker_ = new DownloadWorker(ariaLogFilename_);
168175
worker_->setDownloadDirectory(dir.canonicalPath().toStdString());
169-
worker_->addTorrent("https://cdn.unvanquished.net/current.torrent");
176+
worker_->addTorrent(gameUrl.toStdString());
170177
worker_->moveToThread(&thread_);
171178
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
172179
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));
@@ -201,14 +208,13 @@ void QmlDownloader::stopAria()
201208
}
202209
}
203210

204-
void QmlDownloader::startUpdaterUpdate(QString version)
211+
void QmlDownloader::startUpdaterUpdate(QString updaterUrl)
205212
{
206-
QString url = UPDATER_BASE_URL + "/" + version + "/" + Sys::updaterArchiveName();
207213
temp_dir_.reset(new QTemporaryDir());
208214
worker_ = new DownloadWorker(ariaLogFilename_);
209215
worker_->setDownloadDirectory(QDir(temp_dir_->path()).canonicalPath().toStdString());
210216
worker_->setConnectUrl(connectUrl_);
211-
worker_->addUpdaterUri(url.toStdString());
217+
worker_->addUpdaterUri(updaterUrl.toStdString());
212218
worker_->moveToThread(&thread_);
213219
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
214220
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));

0 commit comments

Comments
 (0)