Skip to content

Commit

Permalink
Asynchronously request latest versions
Browse files Browse the repository at this point in the history
Instead of waiting for 3 seconds at the start and then requesting the
latest versions and then waiting for them to arrive before doing
anything, request them right away at the start. Then do the thing after
3 seconds have elapsed. If the versions didn't arrive during the
3-second waiting period, it is treated the same as a network failure.

I removed the network connectivity check, since there is now no risk of
hanging if the version request takes forever.

This fixes the bug where you can click the settings icon during the
period after the timer elapsed, but before it started the updater update
or automatically opened the game update page. Also, I disabled the
settings button after the timer elapses.

I think this also fixes a bug where an empty string would be saved in
the settings as the current version if you clicked settings before the
time elapsed and then updated the game, although I did not try to
actually produce this bug.
  • Loading branch information
slipher committed Jan 7, 2021
1 parent 6b6f4aa commit 8112410
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int main(int argc, char *argv[])
}
Settings settings;
QmlDownloader downloader;
downloader.checkForUpdate();
QQmlApplicationEngine engine;
engine.addImportPath(QLatin1String("qrc:/"));
engine.addImageProvider(QLatin1String("fluidicons"), new IconsImageProvider());
Expand Down
37 changes: 20 additions & 17 deletions qmldownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ void QmlDownloader::onDownloadEvent(int event)
if (state() != COMPLETED) {
qDebug() << "Calling Sys::install";
Sys::install();
settings_.setCurrentVersion(currentVersion_);
// FIXME: latestGameVersion_ could be empty if CurrentVersionFetcher didn't succeed
settings_.setCurrentVersion(latestGameVersion_);
settings_.setInstallFinished(true);
setState(COMPLETED);
setDownloadSpeed(0);
Expand Down Expand Up @@ -193,27 +194,29 @@ void QmlDownloader::stopAria()
}
}

// Initiates an asynchronous request for the latest available versions.
void QmlDownloader::checkForUpdate()
{
if (networkManager_.isOnline()) {
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString)), this, SLOT(onCurrentVersions(QString, QString)));
fetcher_.fetchCurrentVersion("https://dl.unvanquished.net/versions.json");
return;
}
else if (!settings_.installFinished()) {
emit updateNeeded(true);
return;
}
emit updateNeeded(false);
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString)), this, SLOT(onCurrentVersions(QString, QString)));
fetcher_.fetchCurrentVersion("https://dl.unvanquished.net/versions.json");
}

// Receives the results of the checkForUpdate request.
void QmlDownloader::onCurrentVersions(QString updater, QString game)
{
qDebug() << "Latest versions: updater =" << updater << "game =" << game;
latestUpdaterVersion_ = updater;
latestGameVersion_ = game;
}

// This runs after the splash screen has been displayed for the programmed amount of time (and the
// user did not click the settings button). If the CurrentVersionFetcher didn't emit anything yet,
// proceed as if the request for versions.json failed.
void QmlDownloader::autoLaunchOrUpdate()
{
qDebug() << "Previously-installed game version:" << settings_.currentVersion();
if (!updater.isEmpty() && updater != QString(GIT_VERSION)) {
qDebug() << "Updater update to version" << updater << "required";
QString url = UPDATER_BASE_URL + "/" + updater + "/" + Sys::updaterArchiveName();
if (!latestUpdaterVersion_.isEmpty() && latestUpdaterVersion_ != QString(GIT_VERSION)) {
qDebug() << "Updater update to version" << latestUpdaterVersion_ << "required";
QString url = UPDATER_BASE_URL + "/" + latestUpdaterVersion_ + "/" + Sys::updaterArchiveName();
temp_dir_.reset(new QTemporaryDir());
worker_ = new DownloadWorker();
worker_->setDownloadDirectory(QDir(temp_dir_->path()).canonicalPath().toStdString());
Expand All @@ -227,9 +230,9 @@ void QmlDownloader::onCurrentVersions(QString updater, QString game)
connect(worker_, SIGNAL(completedSizeChanged(int)), this, SLOT(setCompletedSize(int)));
connect(&thread_, SIGNAL(started()), worker_, SLOT(download()));
thread_.start();
} else if (settings_.currentVersion().isEmpty() || (!game.isEmpty() && settings_.currentVersion() != game)) {
} else if (settings_.currentVersion().isEmpty() ||
(!latestGameVersion_.isEmpty() && settings_.currentVersion() != latestGameVersion_)) {
qDebug() << "Game update required.";
currentVersion_ = game;
emit updateNeeded(true);
} else {
emit updateNeeded(false);
Expand Down
8 changes: 4 additions & 4 deletions qmldownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <QObject>
#include <QRegularExpression>
#include <QSettings>
#include <QNetworkConfigurationManager>
#include <QThread>
#include <QUrl>
#include <QTemporaryDir>
Expand Down Expand Up @@ -44,6 +43,7 @@ class QmlDownloader : public QObject
int totalSize() const;
int completedSize() const;
DownloadState state() const;
void checkForUpdate();

signals:
void downloadSpeedChanged(int downloadSpeed);
Expand All @@ -66,7 +66,7 @@ public slots:
Q_INVOKABLE void startUpdate();
Q_INVOKABLE void toggleDownload();
Q_INVOKABLE void startGame();
Q_INVOKABLE void checkForUpdate();
Q_INVOKABLE void autoLaunchOrUpdate();

private:
void stopAria();
Expand All @@ -84,8 +84,8 @@ public slots:
DownloadWorker* worker_;
DownloadTimeCalculator downloadTime_;
Settings settings_;
QNetworkConfigurationManager networkManager_;
QString currentVersion_;
QString latestGameVersion_;
QString latestUpdaterVersion_;
DownloadState state_;
std::unique_ptr<QTemporaryDir> temp_dir_;

Expand Down
5 changes: 4 additions & 1 deletion splash.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ ApplicationWindow {
interval: 3000
repeat: false
running: true
onTriggered: downloader.checkForUpdate()
onTriggered: {
settingsAction.enabled = false;
downloader.autoLaunchOrUpdate();
}
}

function showUpdater() {
Expand Down

0 comments on commit 8112410

Please sign in to comment.