2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 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
|
|
|
|
|
2015-01-31 21:29:03 +05:30
|
|
|
UpdateChecker::UpdateChecker(QString channelListUrl, int currentBuild)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2015-01-31 21:29:03 +05:30
|
|
|
m_channelListUrl = channelListUrl;
|
|
|
|
m_currentBuild = currentBuild;
|
2013-12-05 00:04:12 +05:30
|
|
|
m_updateChecking = false;
|
|
|
|
m_chanListLoading = false;
|
|
|
|
m_checkUpdateWaiting = false;
|
|
|
|
m_chanListLoaded = false;
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
QList<UpdateChecker::ChannelListEntry> UpdateChecker::getChannelList() const
|
|
|
|
{
|
|
|
|
return m_channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UpdateChecker::hasChannels() const
|
|
|
|
{
|
2013-12-14 20:32:51 +05:30
|
|
|
return !m_channels.isEmpty();
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
2015-01-31 21:29:03 +05:30
|
|
|
void UpdateChecker::checkForUpdate(QString updateChannel, bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Checking for updates.";
|
2013-12-05 00:04:12 +05:30
|
|
|
|
2013-12-25 02:30:07 +05:30
|
|
|
// If the channel list hasn't loaded yet, load it and defer checking for updates until
|
|
|
|
// later.
|
2013-12-05 00:04:12 +05:30
|
|
|
if (!m_chanListLoaded)
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Channel list isn't loaded yet. Loading channel list and deferring "
|
2013-12-25 02:30:07 +05:30
|
|
|
"update check.";
|
2013-12-05 00:04:12 +05:30
|
|
|
m_checkUpdateWaiting = true;
|
2015-01-31 21:29:03 +05:30
|
|
|
m_deferredUpdateChannel = updateChannel;
|
2014-07-14 04:27:54 +05:30
|
|
|
updateChanList(notifyNoUpdate);
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_updateChecking)
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Ignoring update check request. Already checking for updates.";
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_updateChecking = true;
|
|
|
|
|
2013-12-25 02:30:07 +05:30
|
|
|
// Find the desired channel within the channel list and get its repo URL. If if cannot be
|
|
|
|
// found, error.
|
2013-12-05 00:04:12 +05:30
|
|
|
m_repoUrl = "";
|
|
|
|
for (ChannelListEntry entry : m_channels)
|
|
|
|
{
|
|
|
|
if (entry.id == updateChannel)
|
|
|
|
m_repoUrl = entry.url;
|
|
|
|
}
|
|
|
|
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "m_repoUrl = " << m_repoUrl;
|
|
|
|
|
2013-12-05 00:04:12 +05:30
|
|
|
// If we didn't find our channel, error.
|
|
|
|
if (m_repoUrl.isEmpty())
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "m_repoUrl is empty!";
|
2013-12-05 00:04:12 +05:30
|
|
|
emit updateCheckFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl indexUrl = QUrl(m_repoUrl).resolved(QUrl("index.json"));
|
|
|
|
|
|
|
|
auto job = new NetJob("GoUpdate Repository Index");
|
|
|
|
job->addNetAction(ByteArrayDownload::make(indexUrl));
|
2013-12-25 02:30:07 +05:30
|
|
|
connect(job, &NetJob::succeeded, [this, notifyNoUpdate]()
|
|
|
|
{ updateCheckFinished(notifyNoUpdate); });
|
2013-12-05 00:04:12 +05:30
|
|
|
connect(job, SIGNAL(failed()), SLOT(updateCheckFailed()));
|
|
|
|
indexJob.reset(job);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2013-12-25 02:30:07 +05:30
|
|
|
void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Finished downloading repo index. Checking for new versions.";
|
2013-12-05 00:04:12 +05:30
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QByteArray data;
|
|
|
|
{
|
2013-12-25 02:30:07 +05:30
|
|
|
ByteArrayDownloadPtr dl =
|
|
|
|
std::dynamic_pointer_cast<ByteArrayDownload>(indexJob->first());
|
2013-12-05 00:04:12 +05:30
|
|
|
data = dl->m_data;
|
|
|
|
indexJob.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject())
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Failed to parse GoUpdate repository index. JSON error"
|
2013-12-25 02:30:07 +05:30
|
|
|
<< jsonError.errorString() << "at offset" << jsonError.offset;
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject object = jsonDoc.object();
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
int apiVersion = object.value("ApiVersion").toVariant().toInt(&success);
|
|
|
|
if (apiVersion != API_VERSION || !success)
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Failed to check for updates. API version mismatch. We're using"
|
2013-12-25 02:30:07 +05:30
|
|
|
<< API_VERSION << "server has" << apiVersion;
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Processing repository version list.";
|
2013-12-05 00:04:12 +05:30
|
|
|
QJsonObject newestVersion;
|
|
|
|
QJsonArray versions = object.value("Versions").toArray();
|
|
|
|
for (QJsonValue versionVal : versions)
|
|
|
|
{
|
|
|
|
QJsonObject version = versionVal.toObject();
|
2013-12-25 02:30:07 +05:30
|
|
|
if (newestVersion.value("Id").toVariant().toInt() <
|
|
|
|
version.value("Id").toVariant().toInt())
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
|
|
|
newestVersion = version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 02:30:07 +05:30
|
|
|
// We've got the version with the greatest ID number. Now compare it to our current build
|
|
|
|
// number and update if they're different.
|
2013-12-05 00:04:12 +05:30
|
|
|
int newBuildNumber = newestVersion.value("Id").toVariant().toInt();
|
2015-01-31 21:29:03 +05:30
|
|
|
if (newBuildNumber != m_currentBuild)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Found newer version with ID" << newBuildNumber;
|
2013-12-05 00:04:12 +05:30
|
|
|
// Update!
|
2013-12-25 02:30:07 +05:30
|
|
|
emit updateAvailable(m_repoUrl, newestVersion.value("Name").toVariant().toString(),
|
|
|
|
newBuildNumber);
|
|
|
|
}
|
|
|
|
else if (notifyNoUpdate)
|
|
|
|
{
|
|
|
|
emit noUpdateFound();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
m_updateChecking = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateChecker::updateCheckFailed()
|
|
|
|
{
|
2015-01-31 23:51:47 +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
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Loading the channel list.";
|
2013-12-05 00:04:12 +05:30
|
|
|
|
|
|
|
if (m_channelListUrl.isEmpty())
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Failed to update channel list. No channel list URL set."
|
|
|
|
<< "If you'd like to use MultiMC's update system, please pass the channel "
|
2013-12-25 02:30:07 +05:30
|
|
|
"list URL to CMake at compile time.";
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_chanListLoading = true;
|
2013-12-25 02:30:07 +05:30
|
|
|
NetJob *job = new NetJob("Update System Channel List");
|
2013-12-05 00:04:12 +05:30
|
|
|
job->addNetAction(ByteArrayDownload::make(QUrl(m_channelListUrl)));
|
2014-07-14 04:27:54 +05:30
|
|
|
connect(job, &NetJob::succeeded, [this, notifyNoUpdate]()
|
|
|
|
{ chanListDownloadFinished(notifyNoUpdate); });
|
2013-12-05 00:04:12 +05:30
|
|
|
QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed);
|
|
|
|
chanListJob.reset(job);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2014-07-14 04:27:54 +05:30
|
|
|
void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate)
|
2013-12-05 00:04:12 +05:30
|
|
|
{
|
|
|
|
QByteArray data;
|
|
|
|
{
|
2013-12-25 02:30:07 +05:30
|
|
|
ByteArrayDownloadPtr dl =
|
|
|
|
std::dynamic_pointer_cast<ByteArrayDownload>(chanListJob->first());
|
2013-12-05 00:04:12 +05:30
|
|
|
data = dl->m_data;
|
|
|
|
chanListJob.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
// TODO: Report errors to the user.
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at"
|
2013-12-25 02:30:07 +05:30
|
|
|
<< jsonError.offset;
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
2013-12-25 02:30:07 +05:30
|
|
|
|
2013-12-05 00:04:12 +05:30
|
|
|
QJsonObject object = jsonDoc.object();
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
int formatVersion = object.value("format_version").toVariant().toInt(&success);
|
|
|
|
if (formatVersion != CHANLIST_FORMAT || !success)
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical()
|
2013-12-25 02:30:07 +05:30
|
|
|
<< "Failed to check for updates. Channel list format version mismatch. We're using"
|
|
|
|
<< CHANLIST_FORMAT << "server has" << formatVersion;
|
2013-12-05 00:04:12 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load channels into a temporary array.
|
|
|
|
QList<ChannelListEntry> loadedChannels;
|
|
|
|
QJsonArray channelArray = object.value("channels").toArray();
|
|
|
|
for (QJsonValue chanVal : channelArray)
|
|
|
|
{
|
|
|
|
QJsonObject channelObj = chanVal.toObject();
|
2013-12-25 02:30:07 +05:30
|
|
|
ChannelListEntry entry{channelObj.value("id").toVariant().toString(),
|
|
|
|
channelObj.value("name").toVariant().toString(),
|
|
|
|
channelObj.value("description").toVariant().toString(),
|
|
|
|
channelObj.value("url").toVariant().toString()};
|
2013-12-05 00:04:12 +05:30
|
|
|
if (entry.id.isEmpty() || entry.name.isEmpty() || entry.url.isEmpty())
|
|
|
|
{
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Channel list entry with empty ID, name, or URL. Skipping.";
|
2013-12-05 00:04:12 +05:30
|
|
|
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;
|
2015-01-31 23:51:47 +05:30
|
|
|
qDebug() << "Successfully loaded UpdateChecker channel list.";
|
2013-12-05 00:04:12 +05:30
|
|
|
|
|
|
|
// If we're waiting to check for updates, do that now.
|
|
|
|
if (m_checkUpdateWaiting)
|
2015-01-31 21:29:03 +05:30
|
|
|
checkForUpdate(m_deferredUpdateChannel, notifyNoUpdate);
|
2013-12-06 01:22:55 +05:30
|
|
|
|
|
|
|
emit channelListLoaded();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateChecker::chanListDownloadFailed()
|
|
|
|
{
|
|
|
|
m_chanListLoading = false;
|
2015-01-31 23:51:47 +05:30
|
|
|
qCritical() << "Failed to download channel list.";
|
2013-12-06 01:22:55 +05:30
|
|
|
emit channelListLoaded();
|
2013-12-05 00:04:12 +05:30
|
|
|
}
|
2014-01-07 07:13:25 +05:30
|
|
|
|