2021-01-18 12:58:54 +05:30
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2013-12-05 00:04:12 +05:30
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UpdateChecker.h"
|
|
|
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
2015-01-31 23:51:47 +05:30
|
|
|
#include <QDebug>
|
2013-12-05 00:04:12 +05:30
|
|
|
|
|
|
|
#define API_VERSION 0
|
|
|
|
#define CHANLIST_FORMAT 0
|
|
|
|
|
2021-09-05 00:57:09 +05:30
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "sys.h"
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
UpdateChecker::UpdateChecker(shared_qobject_ptr<QNetworkAccessManager> nam, QString channelUrl, QString currentChannel, int currentBuild)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2021-11-22 03:51:12 +05:30
|
|
|
m_network = nam;
|
2021-09-05 00:57:09 +05:30
|
|
|
m_channelUrl = channelUrl;
|
2018-07-15 18:21:05 +05:30
|
|
|
m_currentChannel = currentChannel;
|
|
|
|
m_currentBuild = currentBuild;
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
QList<UpdateChecker::ChannelListEntry> UpdateChecker::getChannelList() const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_channels;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool UpdateChecker::hasChannels() const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return !m_channels.isEmpty();
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
2022-04-21 08:04:13 +05:30
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
SparkleUpdater* UpdateChecker::getSparkleUpdater()
|
|
|
|
{
|
|
|
|
return m_sparkleUpdater;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-31 21:29:03 +05:30
|
|
|
void UpdateChecker::checkForUpdate(QString updateChannel, bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2022-04-21 08:04:13 +05:30
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
m_sparkleUpdater->setAllowedChannel(updateChannel);
|
|
|
|
if (notifyNoUpdate)
|
|
|
|
{
|
|
|
|
qDebug() << "Checking for updates.";
|
|
|
|
m_sparkleUpdater->checkForUpdates();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Sparkle already handles automatic update checks.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << "Checking for updates.";
|
|
|
|
|
|
|
|
// If the channel list hasn't loaded yet, load it and defer checking for updates until
|
|
|
|
// later.
|
|
|
|
if (!m_chanListLoaded)
|
|
|
|
{
|
2021-09-05 00:57:09 +05:30
|
|
|
qDebug() << "Channel list isn't loaded yet. Loading channel list and deferring update check.";
|
2018-07-15 18:21:05 +05:30
|
|
|
m_checkUpdateWaiting = true;
|
|
|
|
m_deferredUpdateChannel = updateChannel;
|
|
|
|
updateChanList(notifyNoUpdate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_updateChecking)
|
|
|
|
{
|
|
|
|
qDebug() << "Ignoring update check request. Already checking for updates.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the desired channel within the channel list and get its repo URL. If if cannot be
|
|
|
|
// found, error.
|
2021-09-05 00:57:09 +05:30
|
|
|
QString stableUrl;
|
2018-07-15 18:21:05 +05:30
|
|
|
m_newRepoUrl = "";
|
|
|
|
for (ChannelListEntry entry : m_channels)
|
|
|
|
{
|
2021-09-05 00:57:09 +05:30
|
|
|
qDebug() << "channelEntry = " << entry.id;
|
|
|
|
if(entry.id == "stable") {
|
|
|
|
stableUrl = entry.url;
|
|
|
|
}
|
|
|
|
if (entry.id == updateChannel) {
|
2018-07-15 18:21:05 +05:30
|
|
|
m_newRepoUrl = entry.url;
|
2021-09-05 00:57:09 +05:30
|
|
|
qDebug() << "is intended update channel: " << entry.id;
|
|
|
|
}
|
|
|
|
if (entry.id == m_currentChannel) {
|
2018-07-15 18:21:05 +05:30
|
|
|
m_currentRepoUrl = entry.url;
|
2021-09-05 00:57:09 +05:30
|
|
|
qDebug() << "is current update channel: " << entry.id;
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "m_repoUrl = " << m_newRepoUrl;
|
|
|
|
|
2021-09-05 00:57:09 +05:30
|
|
|
if (m_newRepoUrl.isEmpty()) {
|
|
|
|
qWarning() << "m_repoUrl was empty. defaulting to 'stable': " << stableUrl;
|
|
|
|
m_newRepoUrl = stableUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing applies, error
|
2018-07-15 18:21:05 +05:30
|
|
|
if (m_newRepoUrl.isEmpty())
|
|
|
|
{
|
2021-09-05 00:57:09 +05:30
|
|
|
qCritical() << "failed to select any update repository for: " << updateChannel;
|
2018-07-15 18:21:05 +05:30
|
|
|
emit updateCheckFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:57:09 +05:30
|
|
|
m_updateChecking = true;
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QUrl indexUrl = QUrl(m_newRepoUrl).resolved(QUrl("index.json"));
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
indexJob = new NetJob("GoUpdate Repository Index", m_network);
|
2021-11-22 03:51:12 +05:30
|
|
|
indexJob->addNetAction(Net::Download::makeByteArray(indexUrl, &indexData));
|
|
|
|
connect(indexJob.get(), &NetJob::succeeded, [this, notifyNoUpdate](){ updateCheckFinished(notifyNoUpdate); });
|
|
|
|
connect(indexJob.get(), &NetJob::failed, this, &UpdateChecker::updateCheckFailed);
|
2021-12-31 09:57:59 +05:30
|
|
|
indexJob->start();
|
2022-04-21 08:04:13 +05:30
|
|
|
#endif
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
2013-12-25 02:30:07 +05:30
|
|
|
void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << "Finished downloading repo index. Checking for new versions.";
|
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
indexJob.reset();
|
|
|
|
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(indexData, &jsonError);
|
|
|
|
indexData.clear();
|
|
|
|
if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject())
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to parse GoUpdate repository index. JSON error"
|
|
|
|
<< jsonError.errorString() << "at offset" << jsonError.offset;
|
|
|
|
m_updateChecking = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject object = jsonDoc.object();
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
int apiVersion = object.value("ApiVersion").toVariant().toInt(&success);
|
|
|
|
if (apiVersion != API_VERSION || !success)
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to check for updates. API version mismatch. We're using"
|
|
|
|
<< API_VERSION << "server has" << apiVersion;
|
|
|
|
m_updateChecking = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Processing repository version list.";
|
|
|
|
QJsonObject newestVersion;
|
|
|
|
QJsonArray versions = object.value("Versions").toArray();
|
|
|
|
for (QJsonValue versionVal : versions)
|
|
|
|
{
|
|
|
|
QJsonObject version = versionVal.toObject();
|
|
|
|
if (newestVersion.value("Id").toVariant().toInt() <
|
|
|
|
version.value("Id").toVariant().toInt())
|
|
|
|
{
|
|
|
|
newestVersion = version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've got the version with the greatest ID number. Now compare it to our current build
|
|
|
|
// number and update if they're different.
|
|
|
|
int newBuildNumber = newestVersion.value("Id").toVariant().toInt();
|
|
|
|
if (newBuildNumber != m_currentBuild)
|
|
|
|
{
|
|
|
|
qDebug() << "Found newer version with ID" << newBuildNumber;
|
|
|
|
// Update!
|
|
|
|
GoUpdate::Status updateStatus;
|
|
|
|
updateStatus.updateAvailable = true;
|
|
|
|
updateStatus.currentVersionId = m_currentBuild;
|
|
|
|
updateStatus.currentRepoUrl = m_currentRepoUrl;
|
|
|
|
updateStatus.newVersionId = newBuildNumber;
|
|
|
|
updateStatus.newRepoUrl = m_newRepoUrl;
|
|
|
|
emit updateAvailable(updateStatus);
|
|
|
|
}
|
|
|
|
else if (notifyNoUpdate)
|
|
|
|
{
|
|
|
|
emit noUpdateFound();
|
|
|
|
}
|
|
|
|
m_updateChecking = false;
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateChecker::updateCheckFailed()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qCritical() << "Update check failed for reasons unknown.";
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
2014-07-14 04:27:54 +05:30
|
|
|
void UpdateChecker::updateChanList(bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << "Loading the channel list.";
|
|
|
|
|
|
|
|
if (m_chanListLoading)
|
|
|
|
{
|
|
|
|
qDebug() << "Ignoring channel list update request. Already grabbing channel list.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_chanListLoading = true;
|
2021-12-31 09:57:59 +05:30
|
|
|
chanListJob = new NetJob("Update System Channel List", m_network);
|
2021-11-22 03:51:12 +05:30
|
|
|
chanListJob->addNetAction(Net::Download::makeByteArray(QUrl(m_channelUrl), &chanlistData));
|
|
|
|
connect(chanListJob.get(), &NetJob::succeeded, [this, notifyNoUpdate]() { chanListDownloadFinished(notifyNoUpdate); });
|
|
|
|
connect(chanListJob.get(), &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed);
|
2021-12-31 09:57:59 +05:30
|
|
|
chanListJob->start();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
2014-07-14 04:27:54 +05:30
|
|
|
void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
chanListJob.reset();
|
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(chanlistData, &jsonError);
|
|
|
|
chanlistData.clear();
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
// TODO: Report errors to the user.
|
|
|
|
qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" << jsonError.offset;
|
|
|
|
m_chanListLoading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject object = jsonDoc.object();
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
int formatVersion = object.value("format_version").toVariant().toInt(&success);
|
|
|
|
if (formatVersion != CHANLIST_FORMAT || !success)
|
|
|
|
{
|
|
|
|
qCritical()
|
|
|
|
<< "Failed to check for updates. Channel list format version mismatch. We're using"
|
|
|
|
<< CHANLIST_FORMAT << "server has" << formatVersion;
|
|
|
|
m_chanListLoading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load channels into a temporary array.
|
|
|
|
QList<ChannelListEntry> loadedChannels;
|
|
|
|
QJsonArray channelArray = object.value("channels").toArray();
|
|
|
|
for (QJsonValue chanVal : channelArray)
|
|
|
|
{
|
|
|
|
QJsonObject channelObj = chanVal.toObject();
|
2021-11-22 03:51:12 +05:30
|
|
|
ChannelListEntry entry {
|
|
|
|
channelObj.value("id").toVariant().toString(),
|
|
|
|
channelObj.value("name").toVariant().toString(),
|
|
|
|
channelObj.value("description").toVariant().toString(),
|
|
|
|
channelObj.value("url").toVariant().toString()
|
|
|
|
};
|
2018-07-15 18:21:05 +05:30
|
|
|
if (entry.id.isEmpty() || entry.name.isEmpty() || entry.url.isEmpty())
|
|
|
|
{
|
|
|
|
qCritical() << "Channel list entry with empty ID, name, or URL. Skipping.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
loadedChannels.append(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the channel list we just loaded into the object's channel list.
|
|
|
|
m_channels.swap(loadedChannels);
|
|
|
|
|
|
|
|
m_chanListLoading = false;
|
|
|
|
m_chanListLoaded = true;
|
|
|
|
qDebug() << "Successfully loaded UpdateChecker channel list.";
|
|
|
|
|
|
|
|
// If we're waiting to check for updates, do that now.
|
2021-11-22 03:51:12 +05:30
|
|
|
if (m_checkUpdateWaiting) {
|
2018-07-15 18:21:05 +05:30
|
|
|
checkForUpdate(m_deferredUpdateChannel, notifyNoUpdate);
|
2021-11-22 03:51:12 +05:30
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
emit channelListLoaded();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
2015-04-26 17:17:14 +05:30
|
|
|
void UpdateChecker::chanListDownloadFailed(QString reason)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_chanListLoading = false;
|
|
|
|
qCritical() << QString("Failed to download channel list: %1").arg(reason);
|
|
|
|
emit channelListLoaded();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
2014-01-07 07:13:25 +05:30
|
|
|
|