-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsplashcontroller.cpp
134 lines (118 loc) · 4.98 KB
/
splashcontroller.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
/* Unvanquished Updater
* Copyright (C) Unvanquished Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "splashcontroller.h"
#include <QCoreApplication>
#include <QDebug>
#include "system.h"
SplashController::SplashController(
RelaunchCommand command, const QString& updateUpdaterVersion,
const QString& connectUrl, const Settings& settings) :
relaunchCommand_(command), updateUpdaterVersion_(updateUpdaterVersion),
connectUrl_(connectUrl), settings_(settings) {}
// Possibly initiates an asynchronous request for the latest available versions.
void SplashController::checkForUpdate()
{
// Don't need versions.json if we already know what to do next
if (relaunchCommand_ != RelaunchCommand::NONE) {
return;
}
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 SplashController::onCurrentVersions(QString updater, QString game)
{
latestUpdaterVersion_ = updater;
latestGameVersion_ = game;
}
// Return value is whether the program should exit
bool SplashController::relaunchForSettings()
{
qDebug() << "Possibly relaunching to open settings window";
return Sys::RelaunchElevated("--splashms 1 --internalcommand updategame")
!= Sys::ElevationResult::UNNEEDED;
}
void SplashController::launchGameIfInstalled()
{
if (settings_.installedVersion().isEmpty()) {
qDebug() << "No game installed, exiting";
QCoreApplication::quit();
} else {
qDebug() << "Fall back to launching installed game";
emit updateNeeded(false);
}
}
// 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 SplashController::autoLaunchOrUpdate()
{
qDebug() << "Previously-installed game version:" << settings_.installedVersion();
switch (relaunchCommand_) {
case RelaunchCommand::UPDATE_GAME:
qDebug() << "Game update menu requested as relaunch action";
// It is assumed the process is already elevated
emit updateNeeded(true);
return;
case RelaunchCommand::UPDATE_UPDATER:
qDebug() << "Updater update to" << updateUpdaterVersion_ << "requested as relaunch action";
// It is assumed the process is already elevated
emit updaterUpdate(updateUpdaterVersion_);
return;
case RelaunchCommand::PLAY_NOW:
// Should never happen
case RelaunchCommand::NONE:
break;
}
// If no relaunch action, detect update needed based on versions.json
if (!latestUpdaterVersion_.isEmpty() && latestUpdaterVersion_ != QString(GIT_VERSION)) {
qDebug() << "Updater update to version" << latestUpdaterVersion_ << "required";
// Remember the URL if we are doing updater update
QString updaterArgs = "--splashms 1 --internalcommand updateupdater:" + latestUpdaterVersion_;
if (!connectUrl_.isEmpty()) {
updaterArgs += " -- " + connectUrl_;
}
switch (Sys::RelaunchElevated(updaterArgs)) {
case Sys::ElevationResult::UNNEEDED:
emit updaterUpdate(latestUpdaterVersion_);
return;
case Sys::ElevationResult::RELAUNCHED:
QCoreApplication::quit();
return;
case Sys::ElevationResult::FAILED:
launchGameIfInstalled();
return;
}
} else if (settings_.installedVersion().isEmpty() ||
(!latestGameVersion_.isEmpty() && settings_.installedVersion() != latestGameVersion_)) {
qDebug() << "Game update required.";
switch (Sys::RelaunchElevated("--splashms 1 --internalcommand updategame")) {
case Sys::ElevationResult::UNNEEDED:
emit updateNeeded(true);
return;
case Sys::ElevationResult::RELAUNCHED:
QCoreApplication::quit();
return;
case Sys::ElevationResult::FAILED:
launchGameIfInstalled();
return;
}
} else {
emit updateNeeded(false);
}
}