From 6aa9bd0f77dcb5128167fae62e32aa5252fe85c6 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Mon, 2 Dec 2013 00:55:24 +0100 Subject: Renew the updater branch Now with some actual consensus on what the updater will do! --- gui/MainWindow.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'gui/MainWindow.cpp') diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 854091c6..a6796a29 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -59,6 +59,7 @@ #include "gui/dialogs/CopyInstanceDialog.h" #include "gui/dialogs/AccountListDialog.h" #include "gui/dialogs/AccountSelectDialog.h" +#include "gui/dialogs/UpdateDialog.h" #include "gui/dialogs/EditAccountDialog.h" #include "gui/ConsoleWindow.h" @@ -83,6 +84,7 @@ #include "logic/SkinUtils.h" #include "logic/LegacyInstance.h" +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -236,6 +238,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi MMC->lwjgllist()->loadList(); } + // set up the updater object. + auto updater = MMC->goupdate(); + connect(updater.get(), SIGNAL(updateAvailable()), SLOT(updateAvailable())); + // if automatic update checks are allowed, start one. + if(MMC->settings()->get("AutoUpdate").toBool()) + on_actionCheckUpdate_triggered(); + assets_downloader = new OneSixAssets(); connect(assets_downloader, SIGNAL(indexStarted()), SLOT(assetsIndexStarted())); connect(assets_downloader, SIGNAL(filesStarted()), SLOT(assetsFilesStarted())); @@ -417,6 +426,24 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *ev) return QMainWindow::eventFilter(obj, ev); } +void MainWindow::updateAvailable() +{ + UpdateDialog dlg; + UpdateAction action = (UpdateAction) dlg.exec(); + switch(action) + { + case UPDATE_LATER: + QLOG_INFO() << "Don't install update yet!"; + break; + case UPDATE_NOW: + QLOG_INFO() << "Install update NOW!"; + break; + case UPDATE_ONEXIT: + QLOG_INFO() << "Install update on exit!"; + break; + } +} + void MainWindow::onCatToggled(bool state) { setCatBackground(state); @@ -604,6 +631,8 @@ void MainWindow::on_actionConfig_Folder_triggered() void MainWindow::on_actionCheckUpdate_triggered() { + auto updater = MMC->goupdate(); + updater->checkForUpdate(); } void MainWindow::on_actionSettings_triggered() -- cgit From bf94aaea7527a8f5b9f3b8c1ab6ff4e88cbd748f Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 4 Dec 2013 12:34:12 -0600 Subject: Rework the update checking system --- CMakeLists.txt | 46 ++++++-- MultiMC.cpp | 7 +- MultiMC.h | 8 +- MultiMCVersion.h | 13 ++- config.h.in | 14 ++- gui/MainWindow.cpp | 11 +- gui/MainWindow.h | 2 +- logic/GoUpdate.cpp | 99 ----------------- logic/GoUpdate.h | 43 -------- logic/updater/UpdateChecker.cpp | 234 ++++++++++++++++++++++++++++++++++++++++ logic/updater/UpdateChecker.h | 93 ++++++++++++++++ 11 files changed, 394 insertions(+), 176 deletions(-) delete mode 100644 logic/GoUpdate.cpp delete mode 100644 logic/GoUpdate.h create mode 100644 logic/updater/UpdateChecker.cpp create mode 100644 logic/updater/UpdateChecker.h (limited to 'gui/MainWindow.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 95b1c18f..f5ea5026 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,20 +118,50 @@ SET(MultiMC_VERSION_MINOR 0) SET(MultiMC_VERSION_BUILD -1 CACHE STRING "Build number. -1 for no build number.") # Build type -SET(MultiMC_VERSION_BUILD_TYPE "custombuild" CACHE STRING "Build type. Usually corresponds to the buildbot build name. Empty string for no build type.") +SET(MultiMC_VERSION_BUILD_TYPE "custombuild" CACHE STRING "Build type. If this is set, it is appended to the end of the version string with a dash (-. It is not used for anything other than indicating in the version string what type of build this is (eg 'lin64').") -SET(MultiMC_VERSION_STRING "${MultiMC_VERSION_MAJOR}.${MultiMC_VERSION_MINOR}") +# Version channel +SET(MultiMC_VERSION_CHANNEL "" CACHE STRING "The current build's channel. Included in the version string.") + +# Channel list URL +SET(MultiMC_CHANLIST_URL "" CACHE STRING "URL for the channel list.") + +# Updater enabled? +SET(MultiMC_UPDATER false CACHE BOOL "Whether or not the update system is enabled. If this is enabled, you must also set MultiMC_CHANLIST_URL and MultiMC_VERSION_CHANNEL in order for it to work properly.") + +# Build a version string to display in the configure logs. +SET(MultiMC_VERSION_STRING "${MultiMC_VERSION_MAJOR}.${MultiMC_VERSION_MINOR}") IF (MultiMC_VERSION_BUILD GREATER -1) SET(MultiMC_VERSION_STRING "${MultiMC_VERSION_STRING}.${MultiMC_VERSION_BUILD}") ENDIF () - +IF (NOT MultiMC_VERSION_CHANNEL STREQUAL "") + SET(MultiMC_VERSION_STRING "${MultiMC_VERSION_STRING}-${MultiMC_VERSION_CHANNEL}") +ENDIF () IF (NOT MultiMC_VERSION_BUILD_TYPE STREQUAL "") SET(MultiMC_VERSION_STRING "${MultiMC_VERSION_STRING}-${MultiMC_VERSION_BUILD_TYPE}") ENDIF () MESSAGE(STATUS "MultiMC 5 version ${MultiMC_VERSION_STRING}") +# If the update system is enabled, make sure MultiMC_CHANLIST_URL and MultiMC_VERSION_CHANNEL are set. +IF (MultiMC_UPDATER) + IF (MultiMC_VERSION_CHANNEL STREQUAL "") + MESSAGE(FATAL_ERROR "Update system is enabled, but MultiMC_VERSION_CHANNEL is not set.\n" + "Please ensure the CMake variables MultiMC_VERSION_CHANNEL, MultiMC_CHANLIST_URL, and MultiMC_VERSION_BUILD are set.") + ENDIF () + IF (MultiMC_CHANLIST_URL STREQUAL "") + MESSAGE(FATAL_ERROR "Update system is enabled, but MultiMC_CHANLIST_URL is not set.\n" + "Please ensure the CMake variables MultiMC_VERSION_CHANNEL, MultiMC_CHANLIST_URL, and MultiMC_VERSION_BUILD are set.") + ENDIF () + IF (MultiMC_VERSION_BUILD LESS 0) + MESSAGE(FATAL_ERROR "Update system is enabled, but MultiMC_VERSION_BUILD is not set.\n" + "Please ensure the CMake variables MultiMC_VERSION_CHANNEL, MultiMC_CHANLIST_URL, and MultiMC_VERSION_BUILD are set.") + ENDIF () + + MESSAGE(STATUS "Updater is enabled. Channel list URL: ${MultiMC_CHANLIST_URL}") +ENDIF () + #### Custom target to just print the version. ADD_CUSTOM_TARGET(version echo "Version: ${MultiMC_VERSION_STRING}") @@ -151,10 +181,6 @@ ELSE() MESSAGE(STATUS "Failed to check Git commit. ${GIT_COMMIT_CHECK_RESULTVAR}") ENDIF() -#### GoUpdate URL -SET(MultiMC_REPO_BASE_URL "invalid" CACHE STRING "Base URL for the updater.") -SET(MultiMC_VERSION_BRANCH "invalid" CACHE STRING "URL of the stable update repo.") - ######## Configure header ######## configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/include/config.h") @@ -290,6 +316,10 @@ logic/auth/flows/ValidateTask.cpp logic/auth/flows/InvalidateTask.h logic/auth/flows/InvalidateTask.cpp +# Update system +logic/updater/UpdateChecker.h +logic/updater/UpdateChecker.cpp + # legacy instances logic/LegacyInstance.h logic/LegacyInstance.cpp @@ -358,8 +388,6 @@ logic/NagUtils.h logic/NagUtils.cpp logic/SkinUtils.h logic/SkinUtils.cpp -logic/GoUpdate.h -logic/GoUpdate.cpp ) diff --git a/MultiMC.cpp b/MultiMC.cpp index 3df73c18..128e71f3 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -20,7 +20,8 @@ #include "logic/net/HttpMetaCache.h" #include "logic/JavaUtils.h" -#include "logic/GoUpdate.h" + +#include "logic/updater/UpdateChecker.h" #include "pathutils.h" #include "cmdutils.h" @@ -33,7 +34,7 @@ using namespace Util::Commandline; MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv), - m_version{VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_BUILD_TYPE} + m_version{VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_CHANNEL, VERSION_BUILD_TYPE} { setOrganizationName("MultiMC"); setApplicationName("MultiMC5"); @@ -140,7 +141,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv), initGlobalSettings(); // initialize the updater - m_go_update.reset(new GoUpdate()); + m_updateChecker.reset(new UpdateChecker()); // and instances auto InstDirSetting = m_settings->getSetting("InstanceDir"); diff --git a/MultiMC.h b/MultiMC.h index 0ab7a8b5..659104ba 100644 --- a/MultiMC.h +++ b/MultiMC.h @@ -17,7 +17,7 @@ class IconList; class QNetworkAccessManager; class ForgeVersionList; class JavaVersionList; -class GoUpdate; +class UpdateChecker; #if defined(MMC) #undef MMC @@ -85,9 +85,9 @@ public: return m_metacache; } - std::shared_ptr goupdate() + std::shared_ptr updateChecker() { - return m_go_update; + return m_updateChecker; } std::shared_ptr lwjgllist(); @@ -112,7 +112,7 @@ private: std::shared_ptr m_mmc_translator; std::shared_ptr m_settings; std::shared_ptr m_instances; - std::shared_ptr m_go_update; + std::shared_ptr m_updateChecker; std::shared_ptr m_accounts; std::shared_ptr m_icons; std::shared_ptr m_qnam; diff --git a/MultiMCVersion.h b/MultiMCVersion.h index 863976b2..8978516b 100644 --- a/MultiMCVersion.h +++ b/MultiMCVersion.h @@ -32,8 +32,9 @@ struct MultiMCVersion QString::number(major), QString::number(minor)); - if (build > 0) vstr += QString(".%1").arg(QString::number(build)); - if (!buildType.isEmpty()) vstr += QString("-%1").arg(buildType); + if (build >= 0) vstr += "." + QString::number(build); + if (!channel.isEmpty()) vstr += "-" + channel; + if (!buildType.isEmpty()) vstr += "-" + buildType; return vstr; } @@ -60,10 +61,14 @@ struct MultiMCVersion */ int build; + /*! + * \brief This build's channel. + */ + QString channel; + /*! * \brief The build type. - * This indicates the type of build that this is. For example, lin64-stable. - * Usually corresponds to this build's buildbot builder name. + * This indicates the type of build that this is. For example, lin64 or custombuild. */ QString buildType; }; diff --git a/config.h.in b/config.h.in index 26c8b1e9..b58dc322 100644 --- a/config.h.in +++ b/config.h.in @@ -2,19 +2,17 @@ #define VERSION_MAJOR @MultiMC_VERSION_MAJOR@ #define VERSION_MINOR @MultiMC_VERSION_MINOR@ -// Build number and type -- numer is used by the updater, type is purely visual +// Build number, channel, and type -- number and channel are used by the updater, type is purely visual #define VERSION_BUILD @MultiMC_VERSION_BUILD@ +#define VERSION_CHANNEL "@MultiMC_VERSION_CHANNEL@" #define VERSION_BUILD_TYPE "@MultiMC_VERSION_BUILD_TYPE@" -// URL base for the updater -#define VERSION_REPO "@MultiMC_REPO_BASE_URL@" +// URL for the updater's channel +#define CHANLIST_URL "@MultiMC_CHANLIST_URL@" -// The branch used for this build. User can switch between 'stable' and 'develop' -// if this is one of them. Otherwise, it pulls only from this one. -#define VERSION_BRANCH "@MultiMC_VERSION_BRANCH@" - -// the commit hash of this build +// The commit hash of this build #define GIT_COMMIT "@MultiMC_GIT_COMMIT@" // This is printed on start to standard output #define VERSION_STR "@MultiMC_VERSION_STRING@" + diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index a6796a29..39c78360 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -84,7 +84,8 @@ #include "logic/SkinUtils.h" #include "logic/LegacyInstance.h" -#include + +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -239,8 +240,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi } // set up the updater object. - auto updater = MMC->goupdate(); - connect(updater.get(), SIGNAL(updateAvailable()), SLOT(updateAvailable())); + auto updater = MMC->updateChecker(); + QObject::connect(updater.get(), &UpdateChecker::updateAvailable, this, &MainWindow::updateAvailable); // if automatic update checks are allowed, start one. if(MMC->settings()->get("AutoUpdate").toBool()) on_actionCheckUpdate_triggered(); @@ -426,7 +427,7 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *ev) return QMainWindow::eventFilter(obj, ev); } -void MainWindow::updateAvailable() +void MainWindow::updateAvailable(QString repo, QString versionName, int versionId) { UpdateDialog dlg; UpdateAction action = (UpdateAction) dlg.exec(); @@ -631,7 +632,7 @@ void MainWindow::on_actionConfig_Folder_triggered() void MainWindow::on_actionCheckUpdate_triggered() { - auto updater = MMC->goupdate(); + auto updater = MMC->updateChecker(); updater->checkForUpdate(); } diff --git a/gui/MainWindow.h b/gui/MainWindow.h index e3713f89..1f498eca 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -161,7 +161,7 @@ slots: void startTask(Task *task); - void updateAvailable(); + void updateAvailable(QString repo, QString versionName, int versionId); void activeAccountChanged(); diff --git a/logic/GoUpdate.cpp b/logic/GoUpdate.cpp deleted file mode 100644 index 6ac53d19..00000000 --- a/logic/GoUpdate.cpp +++ /dev/null @@ -1,99 +0,0 @@ - -#include "GoUpdate.h" - -#include "config.h" -#include "logger/QsLog.h" - -GoUpdate::GoUpdate() -{ - currentBuildIndex = VERSION_BUILD; - builderName = VERSION_BUILD_TYPE; - repoUrlBase = VERSION_REPO; -} - -void GoUpdate::updateCheckFailed() -{ - // TODO: log errors better - QLOG_ERROR() << "Update check failed for reasons unknown."; -} - -void GoUpdate::updateCheckFinished() -{ - QJsonParseError jsonError; - QByteArray data; - { - ByteArrayDownloadPtr dl = - std::dynamic_pointer_cast(index_job->first()); - data = dl->m_data; - index_job.reset(); - } - - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); - if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject()) - { - return; - } - - QVariant doc = jsonDoc.toVariant(); - auto stuff = doc.toMap(); - - // check api version (or later, branch?) - int ApiVersion = stuff["ApiVersion"].toInt(); - if (ApiVersion != 0) - return; - - // parse and store the channel list - auto parsedChannels = stuff["Channels"].toList(); - for (auto channel : parsedChannels) - { - auto chanMap = channel.toMap(); - channels.append({chanMap["Id"].toString(), chanMap["Name"].toString(), - chanMap["CurrentVersion"].toInt()}); - } - - // parse and store the version list - auto parsedVersions = stuff["Versions"].toList(); - for (auto version : parsedVersions) - { - auto verMap = version.toMap(); - int versionId = verMap["Id"].toInt(); - versions.append({versionId, verMap["Name"].toString()}); - if (currentBuildIndex < versionId) - { - newBuildIndex = versionId; - } - } - - if (newBuildIndex != -1) - { - QLOG_INFO() << "Update is available."; - emit updateAvailable(); - } - else - { - QLOG_INFO() << "Update check finished."; - } -} - -void GoUpdate::checkForUpdate() -{ - if (repoUrlBase == "invalid") - { - return; - } - - auto job = new NetJob("Assets index"); - job->addNetAction( - ByteArrayDownload::make(QUrl(repoUrlBase + "/" + VERSION_BRANCH + "/index.json"))); - connect(job, SIGNAL(succeeded()), SLOT(updateCheckFinished())); - connect(job, SIGNAL(failed()), SLOT(updateCheckFailed())); - index_job.reset(job); - job->start(); -} - -/* - files.multimc.org/lin64/ - Hi Forkkie - files.multimc.org/win32/ - files.multimc.org/lin32/ -*/ diff --git a/logic/GoUpdate.h b/logic/GoUpdate.h deleted file mode 100644 index 756a71cf..00000000 --- a/logic/GoUpdate.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include "net/NetJob.h" - -class GoUpdate : public QObject -{ - Q_OBJECT - -public: - struct version_channel - { - QString id; - QString name; - int latestVersion; - }; - - struct version_summary - { - int id; - QString name; - }; - -signals: - void updateAvailable(); - -private slots: - void updateCheckFinished(); - void updateCheckFailed(); - -public: - GoUpdate(); - void checkForUpdate(); -private: - NetJobPtr index_job; - NetJobPtr fromto_job; - - QString repoUrlBase; - QString builderName; - int currentBuildIndex; - int newBuildIndex = -1; - - QList versions; - QList channels; -}; diff --git a/logic/updater/UpdateChecker.cpp b/logic/updater/UpdateChecker.cpp new file mode 100644 index 00000000..68c32ae8 --- /dev/null +++ b/logic/updater/UpdateChecker.cpp @@ -0,0 +1,234 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 "MultiMC.h" + +#include "config.h" +#include "logger/QsLog.h" + +#include +#include +#include + +#define API_VERSION 0 +#define CHANLIST_FORMAT 0 + +UpdateChecker::UpdateChecker() +{ + m_currentChannel = VERSION_CHANNEL; + m_channelListUrl = CHANLIST_URL; + m_updateChecking = false; + m_chanListLoading = false; + m_checkUpdateWaiting = false; + m_chanListLoaded = false; +} + +void UpdateChecker::checkForUpdate() +{ + QLOG_DEBUG() << "Checking for updates."; + + // If the channel list hasn't loaded yet, load it and defer checking for updates until later. + if (!m_chanListLoaded) + { + QLOG_DEBUG() << "Channel list isn't loaded yet. Loading channel list and deferring update check."; + m_checkUpdateWaiting = true; + updateChanList(); + return; + } + + if (m_updateChecking) + { + QLOG_DEBUG() << "Ignoring update check request. Already checking for updates."; + return; + } + + m_updateChecking = true; + + // Get the URL for the channel we're using. + // TODO: Allow user to select channels. For now, we'll just use the current channel. + QString updateChannel = m_currentChannel; + + // Find the desired channel within the channel list and get its repo URL. If if cannot be found, error. + m_repoUrl = ""; + for (ChannelListEntry entry : m_channels) + { + if (entry.id == updateChannel) + m_repoUrl = entry.url; + } + + // If we didn't find our channel, error. + if (m_repoUrl.isEmpty()) + { + emit updateCheckFailed(); + return; + } + + QUrl indexUrl = QUrl(m_repoUrl).resolved(QUrl("index.json")); + + auto job = new NetJob("GoUpdate Repository Index"); + job->addNetAction(ByteArrayDownload::make(indexUrl)); + connect(job, SIGNAL(succeeded()), SLOT(updateCheckFinished())); + connect(job, SIGNAL(failed()), SLOT(updateCheckFailed())); + indexJob.reset(job); + job->start(); +} + +void UpdateChecker::updateCheckFinished() +{ + QLOG_DEBUG() << "Finished downloading repo index. Checking for new versions."; + + QJsonParseError jsonError; + QByteArray data; + { + ByteArrayDownloadPtr dl = std::dynamic_pointer_cast(indexJob->first()); + data = dl->m_data; + indexJob.reset(); + } + + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject()) + { + QLOG_ERROR() << "Failed to parse GoUpdate repository index. JSON error" << jsonError.errorString() << "at offset" << jsonError.offset; + return; + } + + QJsonObject object = jsonDoc.object(); + + bool success = false; + int apiVersion = object.value("ApiVersion").toVariant().toInt(&success); + if (apiVersion != API_VERSION || !success) + { + QLOG_ERROR() << "Failed to check for updates. API version mismatch. We're using" << API_VERSION << "server has" << apiVersion; + return; + } + + QLOG_DEBUG() << "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()) + { + QLOG_DEBUG() << "Found newer version with ID" << 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 != MMC->version().build) + { + // Update! + emit updateAvailable(m_repoUrl, newestVersion.value("Name").toVariant().toString(), newBuildNumber); + } + + m_updateChecking = false; +} + +void UpdateChecker::updateCheckFailed() +{ + // TODO: log errors better + QLOG_ERROR() << "Update check failed for reasons unknown."; +} + +void UpdateChecker::updateChanList() +{ + QLOG_DEBUG() << "Loading the channel list."; + + if (m_channelListUrl.isEmpty()) + { + QLOG_ERROR() << "Failed to update channel list. No channel list URL set." + << "If you'd like to use MultiMC's update system, please pass the channel list URL to CMake at compile time."; + return; + } + + m_chanListLoading = true; + NetJob* job = new NetJob("Update System Channel List"); + job->addNetAction(ByteArrayDownload::make(QUrl(m_channelListUrl))); + QObject::connect(job, &NetJob::succeeded, this, &UpdateChecker::chanListDownloadFinished); + QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed); + chanListJob.reset(job); + job->start(); +} + +void UpdateChecker::chanListDownloadFinished() +{ + QByteArray data; + { + ByteArrayDownloadPtr dl = std::dynamic_pointer_cast(chanListJob->first()); + 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. + QLOG_ERROR() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" << jsonError.offset; + return; + } + + QJsonObject object = jsonDoc.object(); + + bool success = false; + int formatVersion = object.value("format_version").toVariant().toInt(&success); + if (formatVersion != CHANLIST_FORMAT || !success) + { + QLOG_ERROR() << "Failed to check for updates. Channel list format version mismatch. We're using" << CHANLIST_FORMAT << "server has" << formatVersion; + return; + } + + // Load channels into a temporary array. + QList loadedChannels; + QJsonArray channelArray = object.value("channels").toArray(); + for (QJsonValue chanVal : channelArray) + { + QJsonObject channelObj = chanVal.toObject(); + ChannelListEntry entry{ + channelObj.value("id").toVariant().toString(), + channelObj.value("name").toVariant().toString(), + channelObj.value("description").toVariant().toString(), + channelObj.value("url").toVariant().toString() + }; + if (entry.id.isEmpty() || entry.name.isEmpty() || entry.url.isEmpty()) + { + QLOG_ERROR() << "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; + QLOG_INFO() << "Successfully loaded UpdateChecker channel list."; + + // If we're waiting to check for updates, do that now. + if (m_checkUpdateWaiting) + checkForUpdate(); +} + +void UpdateChecker::chanListDownloadFailed() +{ + m_chanListLoading = false; + QLOG_ERROR() << "Failed to download channel list."; +} + diff --git a/logic/updater/UpdateChecker.h b/logic/updater/UpdateChecker.h new file mode 100644 index 00000000..829f7303 --- /dev/null +++ b/logic/updater/UpdateChecker.h @@ -0,0 +1,93 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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. + */ + +#pragma once + +#include "logic/net/NetJob.h" + +#include + +class UpdateChecker : public QObject +{ + Q_OBJECT + +public: +signals: + //! Signal emitted when an update is available. Passes the URL for the repo and the ID and name for the version. + void updateAvailable(QString repoUrl, QString versionName, int versionId); + +private slots: + void updateCheckFinished(); + void updateCheckFailed(); + + void chanListDownloadFinished(); + void chanListDownloadFailed(); + +public: + UpdateChecker(); + void checkForUpdate(); + + /*! + * Causes the update checker to download the channel list from the URL specified in config.h (generated by CMake). + * If this isn't called before checkForUpdate(), it will automatically be called. + */ + void updateChanList(); + + /*! + * An entry in the channel list. + */ + struct ChannelListEntry + { + QString id; + QString name; + QString description; + QString url; + }; + +private: + NetJobPtr indexJob; + NetJobPtr chanListJob; + + QString m_repoUrl; + + QString m_channelListUrl; + QString m_currentChannel; + + QList m_channels; + + /*! + * True while the system is checking for updates. + * If checkForUpdate is called while this is true, it will be ignored. + */ + bool m_updateChecking; + + /*! + * True if the channel list has loaded. + * If this is false, trying to check for updates will call updateChanList first. + */ + bool m_chanListLoaded; + + /*! + * Set to true while the channel list is currently loading. + */ + bool m_chanListLoading; + + /*! + * Set to true when checkForUpdate is called while the channel list isn't loaded. + * When the channel list finishes loading, if this is true, the update checker will check for updates. + */ + bool m_checkUpdateWaiting; +}; + -- cgit From 48ec8e67b8cff63cd2d0e08c17e7bb576b4e07a7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 5 Dec 2013 13:52:55 -0600 Subject: Implement DownloadUpdateTask Installing updates is not implemented yet. That's next. --- CMakeLists.txt | 2 + gui/MainWindow.cpp | 14 +- logic/updater/DownloadUpdateTask.cpp | 322 +++++++++++++++++++++++++++++++++++ logic/updater/DownloadUpdateTask.h | 182 ++++++++++++++++++++ logic/updater/UpdateChecker.cpp | 13 ++ logic/updater/UpdateChecker.h | 37 ++-- 6 files changed, 555 insertions(+), 15 deletions(-) create mode 100644 logic/updater/DownloadUpdateTask.cpp create mode 100644 logic/updater/DownloadUpdateTask.h (limited to 'gui/MainWindow.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index f5ea5026..b30ab23a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -319,6 +319,8 @@ logic/auth/flows/InvalidateTask.cpp # Update system logic/updater/UpdateChecker.h logic/updater/UpdateChecker.cpp +logic/updater/DownloadUpdateTask.h +logic/updater/DownloadUpdateTask.cpp # legacy instances logic/LegacyInstance.h diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 39c78360..618884ef 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -74,6 +74,8 @@ #include "logic/auth/flows/RefreshTask.h" #include "logic/auth/flows/ValidateTask.h" +#include "logic/updater/DownloadUpdateTask.h" + #include "logic/BaseInstance.h" #include "logic/InstanceFactory.h" #include "logic/MinecraftProcess.h" @@ -434,13 +436,19 @@ void MainWindow::updateAvailable(QString repo, QString versionName, int versionI switch(action) { case UPDATE_LATER: - QLOG_INFO() << "Don't install update yet!"; + QLOG_INFO() << "Update will be installed later."; break; case UPDATE_NOW: - QLOG_INFO() << "Install update NOW!"; + { + QLOG_INFO() << "Installing update."; + ProgressDialog updateDlg(this); + DownloadUpdateTask updateTask(repo, versionId, &updateDlg); + updateDlg.exec(&updateTask); + } break; case UPDATE_ONEXIT: - QLOG_INFO() << "Install update on exit!"; + // TODO: Implement installing updates on exit. + QLOG_INFO() << "Installing on exit is not implemented yet."; break; } } diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp new file mode 100644 index 00000000..fc4d321f --- /dev/null +++ b/logic/updater/DownloadUpdateTask.cpp @@ -0,0 +1,322 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 "DownloadUpdateTask.h" + +#include "MultiMC.h" +#include "logic/updater/UpdateChecker.h" +#include "logic/net/NetJob.h" +#include "pathutils.h" + +#include +#include +#include + + +DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent) : + Task(parent) +{ + m_cVersionId = MMC->version().build; + + m_nRepoUrl = repoUrl; + m_nVersionId = versionId; + + m_updateFilesDir.setAutoRemove(false); +} + +void DownloadUpdateTask::executeTask() +{ + // GO! + // This will call the next step when it's done. + findCurrentVersionInfo(); +} + +void DownloadUpdateTask::findCurrentVersionInfo() +{ + setStatus(tr("Finding information about the current version.")); + + auto checker = MMC->updateChecker(); + + // This runs after we've tried loading the channel list. + // If the channel list doesn't need to be loaded, this will be called immediately. + // If the channel list does need to be loaded, this will be called when it's done. + auto processFunc = [this, &checker] () -> void + { + // Now, check the channel list again. + if (checker->hasChannels()) + { + // We still couldn't load the channel list. Give up. Call loadVersionInfo and return. + QLOG_INFO() << "Reloading the channel list didn't work. Giving up."; + loadVersionInfo(); + return; + } + + QList channels = checker->getChannelList(); + QString channelId = MMC->version().channel; + + // Search through the channel list for a channel with the correct ID. + for (auto channel : channels) + { + if (channel.id == channelId) + { + QLOG_INFO() << "Found matching channel."; + m_cRepoUrl = channel.url; + break; + } + } + + // Now that we've done that, load version info. + loadVersionInfo(); + }; + + if (checker->hasChannels()) + { + // Load the channel list and wait for it to finish loading. + QLOG_INFO() << "No channel list entries found. Will try reloading it."; + + QObject::connect(checker.get(), &UpdateChecker::channelListLoaded, processFunc); + checker->updateChanList(); + } + else + { + processFunc(); + } +} + +void DownloadUpdateTask::loadVersionInfo() +{ + setStatus(tr("Loading version information.")); + + // Create the net job for loading version info. + NetJob* netJob = new NetJob("Version Info"); + + // Find the index URL. + QUrl newIndexUrl = QUrl(m_nRepoUrl).resolved(QString::number(m_nVersionId) + ".json"); + + // Add a net action to download the version info for the version we're updating to. + netJob->addNetAction(ByteArrayDownload::make(newIndexUrl)); + + // If we have a current version URL, get that one too. + if (!m_cRepoUrl.isEmpty()) + { + QUrl cIndexUrl = QUrl(m_cRepoUrl).resolved(QString::number(m_cVersionId) + ".json"); + netJob->addNetAction(ByteArrayDownload::make(cIndexUrl)); + } + + // Connect slots so we know when it's done. + QObject::connect(netJob, &NetJob::succeeded, this, &DownloadUpdateTask::vinfoDownloadFinished); + QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::vinfoDownloadFailed); + + // Store the NetJob in a class member. We don't want to lose it! + m_vinfoNetJob.reset(netJob); + + // Finally, we start the network job and the thread's event loop to wait for it to finish. + netJob->start(); +} + +void DownloadUpdateTask::vinfoDownloadFinished() +{ + // Both downloads succeeded. OK. Parse stuff. + parseDownloadedVersionInfo(); +} + +void DownloadUpdateTask::vinfoDownloadFailed() +{ + // Something failed. We really need the second download (current version info), so parse downloads anyways as long as the first one succeeded. + if (m_vinfoNetJob->first()->m_status != Job_Failed) + { + parseDownloadedVersionInfo(); + return; + } + + // TODO: Give a more detailed error message. + QLOG_ERROR() << "Failed to download version info files."; + emitFailed(tr("Failed to download version info files.")); +} + +void DownloadUpdateTask::parseDownloadedVersionInfo() +{ + setStatus(tr("Reading file lists.")); + + parseVersionInfo(NEW_VERSION, &m_nVersionFileList); + + // If there is a second entry in the network job's list, load it as the current version's info. + if (m_vinfoNetJob->size() >= 2 && m_vinfoNetJob->operator[](1)->m_status != Job_Failed) + { + parseVersionInfo(CURRENT_VERSION, &m_cVersionFileList); + } + + // We don't need this any more. + m_vinfoNetJob.reset(); + + // Now that we're done loading version info, we can move on to the next step. Process file lists and download files. + processFileLists(); +} + +void DownloadUpdateTask::parseVersionInfo(VersionInfoFileEnum vfile, VersionFileList* list) +{ + if (vfile == CURRENT_VERSION) setStatus(tr("Reading file list for current version.")); + else if (vfile == NEW_VERSION) setStatus(tr("Reading file list for new version.")); + + QLOG_DEBUG() << "Reading file list for" << (vfile == NEW_VERSION ? "new" : "current") << "version."; + + QByteArray data; + { + ByteArrayDownloadPtr dl = std::dynamic_pointer_cast( + vfile == NEW_VERSION ? m_vinfoNetJob->first() : m_vinfoNetJob->operator[](1)); + data = dl->m_data; + } + + QJsonParseError jsonError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError) + { + QLOG_ERROR() << "Failed to parse version info JSON:" << jsonError.errorString() << "at" << jsonError.offset; + return; + } + + QJsonObject json = jsonDoc.object(); + + QLOG_DEBUG() << "Loading version info from JSON."; + QJsonArray filesArray = json.value("Files").toArray(); + for (QJsonValue fileValue : filesArray) + { + QJsonObject fileObj = fileValue.toObject(); + + VersionFileEntry file{ + fileObj.value("Path").toString(), + fileObj.value("Executable").toBool(false), + FileSourceList(), + fileObj.value("MD5").toString(), + }; + + QJsonArray sourceArray = fileObj.value("Sources").toArray(); + for (QJsonValue val : sourceArray) + { + QJsonObject sourceObj = val.toObject(); + + QString type = sourceObj.value("SourceType").toString(); + if (type == "http") + { + file.sources.append(FileSource("http", sourceObj.value("Url").toString())); + } + else if (type == "httpc") + { + file.sources.append(FileSource("httpc", sourceObj.value("Url").toString(), sourceObj.value("CompressionType").toString())); + } + else + { + QLOG_WARN() << "Unknown source type" << type << "ignored."; + } + } + + QLOG_DEBUG() << "Loaded info for" << file.path; + + list->append(file); + } +} + +void DownloadUpdateTask::processFileLists() +{ + setStatus(tr("Processing file lists. Figuring out how to install the update.")); + + // First, if we've loaded the current version's file list, we need to iterate through it and + // delete anything in the current one version's list that isn't in the new version's list. + for (VersionFileEntry entry : m_cVersionFileList) + { + for (VersionFileEntry newEntry : m_nVersionFileList) + { + if (newEntry.path == entry.path) + continue; + } + // If the loop reaches the end, we didn't find a match. Delete the file. + m_operationList.append(UpdateOperation::DeleteOp(entry.path)); + } + + // Create a network job for downloading files. + NetJob* netJob = new NetJob("Update Files"); + + // Next, check each file in MultiMC's folder and see if we need to update them. + for (VersionFileEntry entry : m_nVersionFileList) + { + // TODO: Let's not MD5sum a ton of files on the GUI thread. We should probably find a way to do this in the background. + QString fileMD5; + QFile entryFile(entry.path); + if (entryFile.open(QFile::ReadOnly)) + { + QCryptographicHash hash(QCryptographicHash::Md5); + hash.addData(entryFile.readAll()); + fileMD5 = hash.result().toHex(); + } + + if (!entryFile.exists() || fileMD5.isEmpty() || fileMD5 != entry.md5) + { + QLOG_DEBUG() << "Found file" << entry.path << "that needs updating."; + + // Go through the sources list and find one to use. + // TODO: Make a NetAction that takes a source list and tries each of them until one works. For now, we'll just use the first http one. + for (FileSource source : entry.sources) + { + if (source.type == "http") + { + QLOG_DEBUG() << "Will download" << entry.path << "from" << source.url; + + // Download it to updatedir/- where filepath is the file's path with slashes replaced by underscores. + QString dlPath = PathCombine(m_updateFilesDir.path(), entry.path.replace("/", "_")); + + // We need to download the file to the updatefiles folder and add a task to copy it to its install path. + FileDownloadPtr download = FileDownload::make(source.url, dlPath); + download->m_check_md5 = true; + download->m_expected_md5 = entry.md5; + netJob->addNetAction(download); + + // Now add a copy operation to our operations list to install the file. + m_operationList.append(UpdateOperation::CopyOp(dlPath, entry.path)); + } + } + } + } + + // Add listeners to wait for the downloads to finish. + QObject::connect(netJob, &NetJob::succeeded, this, &DownloadUpdateTask::fileDownloadFinished); + QObject::connect(netJob, &NetJob::progress, this, &DownloadUpdateTask::fileDownloadProgressChanged); + QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::fileDownloadFailed); + + // Now start the download. + setStatus(tr("Downloading %1 update files.").arg(QString::number(netJob->size()))); + QLOG_DEBUG() << "Begin downloading update files to" << m_updateFilesDir.path(); + m_filesNetJob.reset(netJob); + netJob->start(); + + // TODO: Write update operations to a file for the update installer. +} + +void DownloadUpdateTask::fileDownloadFinished() +{ + emitSucceeded(); +} + +void DownloadUpdateTask::fileDownloadFailed() +{ + // TODO: Give more info about the failure. + QLOG_ERROR() << "Failed to download update files."; + emitFailed(tr("Failed to download update files.")); +} + +void DownloadUpdateTask::fileDownloadProgressChanged(qint64 current, qint64 total) +{ + setProgress((int)(((float)current / (float)total)*100)); +} + diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h new file mode 100644 index 00000000..6fb745ab --- /dev/null +++ b/logic/updater/DownloadUpdateTask.h @@ -0,0 +1,182 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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. + */ + +#pragma once + +#include "logic/tasks/Task.h" +#include "logic/net/NetJob.h" + +/*! + * The DownloadUpdateTask is a task that takes a given version ID and repository URL, + * downloads that version's files from the repository, and prepares to install them. + */ +class DownloadUpdateTask : public Task +{ + Q_OBJECT + +public: + explicit DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent=0); + +protected: + // TODO: We should probably put these data structures into a separate header... + + /*! + * Struct that describes an entry in a VersionFileEntry's `Sources` list. + */ + struct FileSource + { + FileSource(QString type, QString url, QString compression="") + { + this->type = type; + this->url = url; + this->compressionType = compression; + } + + QString type; + QString url; + QString compressionType; + }; + + typedef QList FileSourceList; + + /*! + * Structure that describes an entry in a GoUpdate version's `Files` list. + */ + struct VersionFileEntry + { + QString path; + bool isExecutable; + FileSourceList sources; + QString md5; + }; + + typedef QList VersionFileList; + + + /*! + * Structure that describes an operation to perform when installing updates. + */ + struct UpdateOperation + { + static UpdateOperation CopyOp(QString fsource, QString fdest) { return UpdateOperation{OP_COPY, fsource, fdest, 644}; } + static UpdateOperation MoveOp(QString fsource, QString fdest) { return UpdateOperation{OP_MOVE, fsource, fdest, 644}; } + static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 644}; } + static UpdateOperation ChmodOp(QString file, int fmode) { return UpdateOperation{OP_CHMOD, file, "", fmode}; } + + //! Specifies the type of operation that this is. + enum Type + { + OP_COPY, + OP_DELETE, + OP_MOVE, + OP_CHMOD, + } type; + + //! The source file. If this is a DELETE or CHMOD operation, this is the file that will be modified. + QString source; + + //! The destination file. If this is a DELETE or CHMOD operation, this field will be ignored. + QString dest; + + //! The mode to change the source file to. Ignored if this isn't a CHMOD operation. + int mode; + + // Yeah yeah, polymorphism blah blah inheritance, blah blah object oriented. I'm lazy, OK? + }; + + typedef QList UpdateOperationList; + + /*! + * Used for arguments to parseVersionInfo and friends to specify which version info file to parse. + */ + enum VersionInfoFileEnum { NEW_VERSION, CURRENT_VERSION }; + + + //! Entry point for tasks. + virtual void executeTask(); + + /*! + * Attempts to find the version ID and repository URL for the current version. + * The function will look up the repository URL in the UpdateChecker's channel list. + * If the repository URL can't be found, this function will return false. + */ + virtual void findCurrentVersionInfo(); + + /*! + * Downloads the version info files from the repository. + * The files for both the current build, and the build that we're updating to need to be downloaded. + * If the current version's info file can't be found, MultiMC will not delete files that + * were removed between versions. It will still replace files that have changed, however. + * Note that although the repository URL for the current version is not given to the update task, + * the task will attempt to look it up in the UpdateChecker's channel list. + * If an error occurs here, the function will call emitFailed and return false. + */ + virtual void loadVersionInfo(); + + /*! + * This function is called when version information is finished downloading. + * This handles parsing the JSON downloaded by the version info network job and then calls processFileLists. + * Note that this function will sometimes be called even if the version info download emits failed. If + * we couldn't download the current version's info file, we can still update. This will be called even if the + * current version's info file fails to download, as long as the new version's info file succeeded. + */ + virtual void parseDownloadedVersionInfo(); + + /*! + * Loads the file list from the given version info JSON object into the given list. + */ + virtual void parseVersionInfo(VersionInfoFileEnum vfile, VersionFileList* list); + + /*! + * Takes a list of file entries for the current version's files and the new version's files + * and populates the downloadList and operationList with information about how to download and install the update. + */ + virtual void processFileLists(); + + VersionFileList m_downloadList; + UpdateOperationList m_operationList; + + VersionFileList m_nVersionFileList; + VersionFileList m_cVersionFileList; + + //! Network job for downloading version info files. + NetJobPtr m_vinfoNetJob; + + //! Network job for downloading update files. + NetJobPtr m_filesNetJob; + + // Version ID and repo URL for the new version. + int m_nVersionId; + QString m_nRepoUrl; + + // Version ID and repo URL for the currently installed version. + int m_cVersionId; + QString m_cRepoUrl; + + /*! + * Temporary directory to store update files in. + * This will be set to not auto delete. Task will fail if this fails to be created. + */ + QTemporaryDir m_updateFilesDir; + +protected slots: + void vinfoDownloadFinished(); + void vinfoDownloadFailed(); + + void fileDownloadFinished(); + void fileDownloadFailed(); + void fileDownloadProgressChanged(qint64 current, qint64 total); +}; + diff --git a/logic/updater/UpdateChecker.cpp b/logic/updater/UpdateChecker.cpp index 68c32ae8..5ff1898e 100644 --- a/logic/updater/UpdateChecker.cpp +++ b/logic/updater/UpdateChecker.cpp @@ -37,6 +37,16 @@ UpdateChecker::UpdateChecker() m_chanListLoaded = false; } +QList UpdateChecker::getChannelList() const +{ + return m_channels; +} + +bool UpdateChecker::hasChannels() const +{ + return m_channels.isEmpty(); +} + void UpdateChecker::checkForUpdate() { QLOG_DEBUG() << "Checking for updates."; @@ -224,11 +234,14 @@ void UpdateChecker::chanListDownloadFinished() // If we're waiting to check for updates, do that now. if (m_checkUpdateWaiting) checkForUpdate(); + + emit channelListLoaded(); } void UpdateChecker::chanListDownloadFailed() { m_chanListLoading = false; QLOG_ERROR() << "Failed to download channel list."; + emit channelListLoaded(); } diff --git a/logic/updater/UpdateChecker.h b/logic/updater/UpdateChecker.h index 829f7303..59fb8e47 100644 --- a/logic/updater/UpdateChecker.h +++ b/logic/updater/UpdateChecker.h @@ -23,18 +23,6 @@ class UpdateChecker : public QObject { Q_OBJECT -public: -signals: - //! Signal emitted when an update is available. Passes the URL for the repo and the ID and name for the version. - void updateAvailable(QString repoUrl, QString versionName, int versionId); - -private slots: - void updateCheckFinished(); - void updateCheckFailed(); - - void chanListDownloadFinished(); - void chanListDownloadFailed(); - public: UpdateChecker(); void checkForUpdate(); @@ -56,6 +44,31 @@ public: QString url; }; + /*! + * Returns a the current channel list. + * If the channel list hasn't been loaded, this list will be empty. + */ + QList getChannelList() const; + + /*! + * Returns true if the channel list is empty. + */ + bool hasChannels() const; + +signals: + //! Signal emitted when an update is available. Passes the URL for the repo and the ID and name for the version. + void updateAvailable(QString repoUrl, QString versionName, int versionId); + + //! Signal emitted when the channel list finishes loading or fails to load. + void channelListLoaded(); + +private slots: + void updateCheckFinished(); + void updateCheckFailed(); + + void chanListDownloadFinished(); + void chanListDownloadFailed(); + private: NetJobPtr indexJob; NetJobPtr chanListJob; -- cgit From 6ac94ddcb6f64ffae3948bed778bccc33a92f0fd Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 6 Dec 2013 12:59:58 -0600 Subject: Finish implementing update installation. Also add the option to update on exit. --- MultiMC.cpp | 69 +++++++++++++++++++++++++++++++++++- MultiMC.h | 18 ++++++++++ gui/MainWindow.cpp | 27 +++++++++----- gui/MainWindow.h | 5 +++ gui/dialogs/UpdateDialog.ui | 7 ++++ logic/updater/DownloadUpdateTask.cpp | 5 +++ logic/updater/DownloadUpdateTask.h | 5 +++ mmc_updater/src/UpdateInstaller.cpp | 2 +- 8 files changed, 128 insertions(+), 10 deletions(-) (limited to 'gui/MainWindow.cpp') diff --git a/MultiMC.cpp b/MultiMC.cpp index 128e71f3..e3107ac4 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -2,10 +2,12 @@ #include "MultiMC.h" #include #include +#include #include #include #include #include +#include #include "gui/MainWindow.h" #include "gui/dialogs/VersionSelectDialog.h" @@ -409,6 +411,65 @@ std::shared_ptr MultiMC::javalist() return m_javalist; } +#ifdef WINDOWS +#define UPDATER_BIN "updater.exe" +#elif LINUX +#define UPDATER_BIN "updater" +#elif OSX +#define UPDATER_BIN "updater" +#else +#error Unsupported operating system. +#endif + +void MultiMC::installUpdates(const QString& updateFilesDir, bool restartOnFinish) +{ + QLOG_INFO() << "Installing updates."; +#if LINUX + // On Linux, the MultiMC executable file is actually in the bin folder inside the installation directory. + // This means that MultiMC's *actual* install path is the parent folder. + // We need to tell the updater to run with this directory as the install path, rather than the bin folder where the executable is. + // On other operating systems, we'll just use the path to the executable. + QString appDir = QFileInfo(MMC->applicationDirPath()).dir().path(); + + // On Linux, we also need to set the finish command to the launch script, rather than the binary. + QString finishCmd = PathCombine(appDir, "MultiMC"); +#else + QString appDir = MMC->applicationDirPath(); + QString finishCmd = MMC->applicationFilePath(); +#endif + + // Build the command we'll use to run the updater. + // Note, the above comment about the app dir path on Linux is irrelevant here because the updater binary is always in the + // same folder as the main binary. + QString updaterBinary = PathCombine(MMC->applicationDirPath(), UPDATER_BIN); + QStringList args; + // ./updater --install-dir $INSTALL_DIR --package-dir $UPDATEFILES_DIR --script $UPDATEFILES_DIR/file_list.xml --wait $PID --mode main + args << "--install-dir" << appDir; + args << "--package-dir" << updateFilesDir; + args << "--script" << PathCombine(updateFilesDir, "file_list.xml"); + args << "--wait" << QString::number(MMC->applicationPid()); + + if (restartOnFinish) + args << "--finish-cmd" << finishCmd; + + QLOG_INFO() << "Running updater with command" << updaterBinary << args.join(" "); + + QProcess::startDetached(updaterBinary, args); + + // Now that we've started the updater, quit MultiMC. + MMC->quit(); +} + +void MultiMC::setUpdateOnExit(const QString& updateFilesDir) +{ + m_updateOnExitPath = updateFilesDir; +} + +QString MultiMC::getExitUpdatePath() const +{ + return m_updateOnExitPath; +} + int main_gui(MultiMC &app) { // show main window @@ -417,7 +478,13 @@ int main_gui(MultiMC &app) mainWin.restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("MainWindowGeometry").toByteArray())); mainWin.show(); mainWin.checkSetDefaultJava(); - return app.exec(); + auto exitCode = app.exec(); + + // Update if necessary. + if (!app.getExitUpdatePath().isEmpty()) + app.installUpdates(app.getExitUpdatePath(), false); + + return exitCode; } int main(int argc, char *argv[]) diff --git a/MultiMC.h b/MultiMC.h index 659104ba..22cea029 100644 --- a/MultiMC.h +++ b/MultiMC.h @@ -98,6 +98,22 @@ public: std::shared_ptr javalist(); + /*! + * Installs update from the given update files directory. + */ + void installUpdates(const QString& updateFilesDir, bool restartOnFinish=false); + + /*! + * Sets MultiMC to install updates from the given directory when it exits. + */ + void setUpdateOnExit(const QString& updateFilesDir); + + /*! + * Gets the path to install updates from on exit. + * If this is an empty string, no updates should be installed on exit. + */ + QString getExitUpdatePath() const; + private: void initLogger(); @@ -124,6 +140,8 @@ private: QsLogging::DestinationPtr m_fileDestination; QsLogging::DestinationPtr m_debugDestination; + QString m_updateOnExitPath; + Status m_status = MultiMC::Failed; MultiMCVersion m_version; }; diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 618884ef..7ea5c291 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -439,20 +439,31 @@ void MainWindow::updateAvailable(QString repo, QString versionName, int versionI QLOG_INFO() << "Update will be installed later."; break; case UPDATE_NOW: - { - QLOG_INFO() << "Installing update."; - ProgressDialog updateDlg(this); - DownloadUpdateTask updateTask(repo, versionId, &updateDlg); - updateDlg.exec(&updateTask); - } + downloadUpdates(repo, versionId); break; case UPDATE_ONEXIT: - // TODO: Implement installing updates on exit. - QLOG_INFO() << "Installing on exit is not implemented yet."; + downloadUpdates(repo, versionId, true); break; } } +void MainWindow::downloadUpdates(QString repo, int versionId, bool installOnExit) +{ + QLOG_INFO() << "Downloading updates."; + // TODO: If the user chooses to update on exit, we should download updates in the background. + // Doing so is a bit complicated, because we'd have to make sure it finished downloading before actually exiting MultiMC. + ProgressDialog updateDlg(this); + DownloadUpdateTask updateTask(repo, versionId, &updateDlg); + // If the task succeeds, install the updates. + if (updateDlg.exec(&updateTask)) + { + if (installOnExit) + MMC->setUpdateOnExit(updateTask.updateFilesDir()); + else + MMC->installUpdates(updateTask.updateFilesDir()); + } +} + void MainWindow::onCatToggled(bool state) { setCatBackground(state); diff --git a/gui/MainWindow.h b/gui/MainWindow.h index 1f498eca..b99c54ee 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -168,6 +168,11 @@ slots: void changeActiveAccount(); void repopulateAccountsMenu(); + + /*! + * Runs the DownloadUpdateTask and installs updates. + */ + void downloadUpdates(QString repo, int versionId, bool installOnExit=false); protected: bool eventFilter(QObject *obj, QEvent *ev); diff --git a/gui/dialogs/UpdateDialog.ui b/gui/dialogs/UpdateDialog.ui index f2361bd3..1fe65e62 100644 --- a/gui/dialogs/UpdateDialog.ui +++ b/gui/dialogs/UpdateDialog.ui @@ -41,6 +41,13 @@ + + + + Update after MultiMC closes + + + diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp index 7c5eb84a..ef975c93 100644 --- a/logic/updater/DownloadUpdateTask.cpp +++ b/logic/updater/DownloadUpdateTask.cpp @@ -391,3 +391,8 @@ void DownloadUpdateTask::fileDownloadProgressChanged(qint64 current, qint64 tota setProgress((int)(((float)current / (float)total)*100)); } +QString DownloadUpdateTask::updateFilesDir() +{ + return m_updateFilesDir.path(); +} + diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h index dc30ca15..f5b23d12 100644 --- a/logic/updater/DownloadUpdateTask.h +++ b/logic/updater/DownloadUpdateTask.h @@ -28,6 +28,11 @@ class DownloadUpdateTask : public Task public: explicit DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent=0); + + /*! + * Gets the directory that contains the update files. + */ + QString updateFilesDir(); protected: // TODO: We should probably put these data structures into a separate header... diff --git a/mmc_updater/src/UpdateInstaller.cpp b/mmc_updater/src/UpdateInstaller.cpp index 3ddc1ec0..ced6ff39 100644 --- a/mmc_updater/src/UpdateInstaller.cpp +++ b/mmc_updater/src/UpdateInstaller.cpp @@ -388,7 +388,7 @@ void UpdateInstaller::restartMainApp() { try { - std::string command = m_installDir + '/' + m_finishCmd; + std::string command = m_finishCmd; std::list args; if (!command.empty()) -- cgit