File size: 9,616 Bytes
034d0a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
//
// Based on the original work by Felix Barx
// Copyright (c) 2015, Felix Barz
// All rights reserved.

#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QTimer>
#include <QXmlStreamReader>
#include "citra_qt/uisettings.h"
#include "citra_qt/updater/updater.h"
#include "citra_qt/updater/updater_p.h"
#include "common/file_util.h"
#include "common/logging/log.h"

#ifdef Q_OS_MACOS
#define DEFAULT_TOOL_PATH QStringLiteral("../../../../maintenancetool")
#else
#define DEFAULT_TOOL_PATH QStringLiteral("../maintenancetool")
#endif

Updater::Updater(QObject* parent) : Updater(DEFAULT_TOOL_PATH, parent) {}

Updater::Updater(const QString& maintenance_tool_path, QObject* parent)
    : QObject(parent), backend(std::make_unique<UpdaterPrivate>(this)) {
    backend->tool_path = UpdaterPrivate::ToSystemExe(maintenance_tool_path);
}

Updater::~Updater() = default;

bool Updater::ExitedNormally() const {
    return backend->normal_exit;
}

int Updater::ErrorCode() const {
    return backend->last_error_code;
}

QByteArray Updater::ErrorLog() const {
    return backend->last_error_log;
}

bool Updater::IsRunning() const {
    return backend->running;
}

QList<Updater::UpdateInfo> Updater::LatestUpdateInfo() const {
    return backend->update_info;
}

bool Updater::HasUpdater() const {
    return backend->HasUpdater();
}

bool Updater::CheckForUpdates() {
    return backend->StartUpdateCheck();
}

void Updater::AbortUpdateCheck(int max_delay, bool async) {
    backend->StopUpdateCheck(max_delay, async);
}

void Updater::LaunchUI() {
    backend->LaunchUI();
}

void Updater::SilentlyUpdate() {
    backend->SilentlyUpdate();
}

void Updater::LaunchUIOnExit() {
    backend->LaunchUIOnExit();
}

Updater::UpdateInfo::UpdateInfo() = default;

Updater::UpdateInfo::UpdateInfo(const Updater::UpdateInfo&) = default;

Updater::UpdateInfo::UpdateInfo(QString name, QString version, quint64 size)
    : name(std::move(name)), version(std::move(version)), size(size) {}

UpdaterPrivate::UpdaterPrivate(Updater* parent_ptr) : QObject(nullptr), parent(parent_ptr) {
    connect(qApp, &QCoreApplication::aboutToQuit, this, &UpdaterPrivate::AboutToExit,
            Qt::DirectConnection);
    qRegisterMetaType<QProcess::ExitStatus>("QProcess::ExitStatus");
}

UpdaterPrivate::~UpdaterPrivate() {
    if (main_process && main_process->state() != QProcess::NotRunning) {
        main_process->kill();
        main_process->waitForFinished(1000);
    }
}

QString UpdaterPrivate::ToSystemExe(QString base_path) {
#if defined(Q_OS_WIN32)
    if (!base_path.endsWith(QStringLiteral(".exe")))
        return base_path + QStringLiteral(".exe");
    else
        return base_path;
#elif defined(Q_OS_MACOS)
    if (base_path.endsWith(QStringLiteral(".app")))
        base_path.truncate(base_path.lastIndexOf(QStringLiteral(".")));
    return base_path + QStringLiteral(".app/Contents/MacOS/") + QFileInfo(base_path).fileName();
#elif defined(Q_OS_UNIX)
    return base_path;
#endif
}

QFileInfo UpdaterPrivate::GetMaintenanceTool() const {
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
    const auto appimage_path = QProcessEnvironment::systemEnvironment()
                                   .value(QStringLiteral("APPIMAGE"), {})
                                   .toStdString();
    if (!appimage_path.empty()) {
        const auto appimage_dir = FileUtil::GetParentPath(appimage_path);
        LOG_DEBUG(Frontend, "Detected app image directory: {}", appimage_dir);
        return QFileInfo(QString::fromStdString(std::string(appimage_dir)), tool_path);
    }
#endif
    return QFileInfo(QCoreApplication::applicationDirPath(), tool_path);
}

bool UpdaterPrivate::HasUpdater() const {
    return GetMaintenanceTool().exists();
}

bool UpdaterPrivate::StartUpdateCheck() {
    if (running || !HasUpdater()) {
        return false;
    }

    update_info.clear();
    normal_exit = true;
    last_error_code = EXIT_SUCCESS;
    last_error_log.clear();

    main_process = new QProcess(this);
    main_process->setProgram(GetMaintenanceTool().absoluteFilePath());
    main_process->setArguments({QStringLiteral("--checkupdates")});

    connect(main_process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this,
            &UpdaterPrivate::UpdaterReady, Qt::QueuedConnection);
    connect(main_process, qOverload<QProcess::ProcessError>(&QProcess::errorOccurred), this,
            &UpdaterPrivate::UpdaterError, Qt::QueuedConnection);

    main_process->start(QIODevice::ReadOnly);
    running = true;

    emit parent->UpdateInfoChanged(update_info);
    emit parent->RunningChanged(true);
    return true;
}

void UpdaterPrivate::StopUpdateCheck(int delay, bool async) {
    if (main_process == nullptr || main_process->state() == QProcess::NotRunning) {
        return;
    }

    if (delay > 0) {
        main_process->terminate();
        if (async) {
            QTimer* timer = new QTimer(this);
            timer->setSingleShot(true);

            connect(timer, &QTimer::timeout, this, [this, timer]() {
                StopUpdateCheck(0, false);
                timer->deleteLater();
            });

            timer->start(delay);
        } else {
            if (!main_process->waitForFinished(delay)) {
                main_process->kill();
                main_process->waitForFinished(100);
            }
        }
    } else {
        main_process->kill();
        main_process->waitForFinished(100);
    }
}

XMLParseResult UpdaterPrivate::ParseResult(const QByteArray& output,
                                           QList<Updater::UpdateInfo>& out) {
    const auto out_string = QString::fromUtf8(output);
    const auto xml_begin = out_string.indexOf(QStringLiteral("<updates>"));
    if (xml_begin < 0)
        return XMLParseResult::NoUpdate;
    const auto xml_end = out_string.indexOf(QStringLiteral("</updates>"), xml_begin);
    if (xml_end < 0)
        return XMLParseResult::NoUpdate;

    QList<Updater::UpdateInfo> updates;
    QXmlStreamReader reader(out_string.mid(xml_begin, (xml_end + 10) - xml_begin));

    reader.readNextStartElement();
    // should always work because it was search for
    if (reader.name() != QStringLiteral("updates")) {
        return XMLParseResult::InvalidXML;
    }

    while (reader.readNextStartElement()) {
        if (reader.name() != QStringLiteral("update"))
            return XMLParseResult::InvalidXML;

        auto ok = false;
        Updater::UpdateInfo info(
            reader.attributes().value(QStringLiteral("name")).toString(),
            reader.attributes().value(QStringLiteral("version")).toString(),
            reader.attributes().value(QStringLiteral("size")).toULongLong(&ok));

        if (info.name.isEmpty() || info.version.isNull() || !ok)
            return XMLParseResult::InvalidXML;
        if (reader.readNextStartElement())
            return XMLParseResult::InvalidXML;

        updates.append(info);
    }

    if (reader.hasError()) {
        LOG_ERROR(Frontend, "Cannot read xml for update: {}", reader.errorString().toStdString());
        return XMLParseResult::InvalidXML;
    }

    out = updates;
    return XMLParseResult::Success;
}

void UpdaterPrivate::UpdaterReady(int exit_code, QProcess::ExitStatus exit_status) {
    if (main_process == nullptr) {
        return;
    }

    if (exit_status != QProcess::NormalExit) {
        UpdaterError(QProcess::Crashed);
        return;
    }

    normal_exit = true;
    last_error_code = exit_code;
    last_error_log = main_process->readAllStandardError();
    const auto update_out = main_process->readAllStandardOutput();
    main_process->deleteLater();
    main_process = nullptr;

    running = false;
    emit parent->RunningChanged(false);

    QList<Updater::UpdateInfo> update_info;
    auto err = ParseResult(update_out, update_info);
    bool has_error = false;

    if (err == XMLParseResult::Success) {
        if (!update_info.isEmpty())
            emit parent->UpdateInfoChanged(update_info);
    } else if (err == XMLParseResult::InvalidXML) {
        has_error = true;
    }

    emit parent->CheckUpdatesDone(!update_info.isEmpty(), has_error);
}

void UpdaterPrivate::UpdaterError(QProcess::ProcessError error) {
    if (main_process) {
        normal_exit = false;
        last_error_code = error;
        last_error_log = main_process->errorString().toUtf8();
        main_process->deleteLater();
        main_process = nullptr;

        running = false;
        emit parent->RunningChanged(false);
        emit parent->CheckUpdatesDone(false, true);
    }
}

void UpdaterPrivate::LaunchWithArguments(const QStringList& args) {
    if (!HasUpdater()) {
        return;
    }

    QFileInfo tool_info = GetMaintenanceTool();

    if (!QProcess::startDetached(tool_info.absoluteFilePath(), args, tool_info.absolutePath())) {
        LOG_WARNING(Frontend, "Unable to start program {}",
                    tool_info.absoluteFilePath().toStdString());
    }
}

void UpdaterPrivate::LaunchUI() {
    LOG_INFO(Frontend, "Launching update UI...");
    LaunchWithArguments(run_arguments);
}

void UpdaterPrivate::SilentlyUpdate() {
    LOG_INFO(Frontend, "Launching silent update...");
    LaunchWithArguments(silent_arguments);
}

void UpdaterPrivate::AboutToExit() {
    if (launch_ui_on_exit) {
        LaunchUI();
    } else if (UISettings::values.update_on_close) {
        SilentlyUpdate();
    }
}

void UpdaterPrivate::LaunchUIOnExit() {
    launch_ui_on_exit = true;
}