diff options
31 files changed, 1056 insertions, 1104 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b4281d5a..f06a7902 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -458,10 +458,14 @@ SET(MULTIMC_SOURCES logic/auth/flows/ValidateTask.cpp # Update system + logic/updater/GoUpdate.h + logic/updater/GoUpdate.cpp logic/updater/UpdateChecker.h logic/updater/UpdateChecker.cpp - logic/updater/DownloadUpdateTask.h - logic/updater/DownloadUpdateTask.cpp + logic/updater/DownloadTask.h + logic/updater/DownloadTask.cpp + + # Notifications - short warning messages logic/notifications/NotificationChecker.h logic/notifications/NotificationChecker.cpp diff --git a/MultiMC.cpp b/MultiMC.cpp index 8375dc1b..2ce668f2 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -192,7 +192,7 @@ MultiMC::MultiMC(int &argc, char **argv, bool test_mode) : QApplication(argc, ar initTranslations(); // initialize the updater - m_updateChecker.reset(new UpdateChecker(BuildConfig.CHANLIST_URL, BuildConfig.VERSION_BUILD)); + m_updateChecker.reset(new UpdateChecker(BuildConfig.CHANLIST_URL, BuildConfig.VERSION_CHANNEL, BuildConfig.VERSION_BUILD)); m_translationChecker.reset(new TranslationDownloader()); @@ -155,7 +155,7 @@ private: private: friend class UpdateCheckerTest; - friend class DownloadUpdateTaskTest; + friend class DownloadTaskTest; QDateTime startTime; diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 1b76e6af..0b0af7f4 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -369,7 +369,7 @@ namespace Ui { #include "logic/auth/flows/AuthenticateTask.h" #include "logic/auth/flows/RefreshTask.h" -#include "logic/updater/DownloadUpdateTask.h" +#include "logic/updater/DownloadTask.h" #include "logic/news/NewsChecker.h" @@ -913,7 +913,7 @@ void MainWindow::updateNewsLabel() } } -void MainWindow::updateAvailable(QString repo, QString versionName, int versionId) +void MainWindow::updateAvailable(GoUpdate::Status status) { UpdateDialog dlg; UpdateAction action = (UpdateAction)dlg.exec(); @@ -923,10 +923,10 @@ void MainWindow::updateAvailable(QString repo, QString versionName, int versionI qDebug() << "Update will be installed later."; break; case UPDATE_NOW: - downloadUpdates(repo, versionId); + downloadUpdates(status); break; case UPDATE_ONEXIT: - downloadUpdates(repo, versionId, true); + downloadUpdates(status, true); break; } } @@ -977,7 +977,7 @@ void MainWindow::notificationsChanged() MMC->settings()->set("ShownNotifications", intListToString(shownNotifications)); } -void MainWindow::downloadUpdates(QString repo, int versionId, bool installOnExit) +void MainWindow::downloadUpdates(GoUpdate::Status status, bool installOnExit) { qDebug() << "Downloading updates."; // TODO: If the user chooses to update on exit, we should download updates in the @@ -985,7 +985,9 @@ void MainWindow::downloadUpdates(QString repo, int versionId, bool installOnExit // 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(MMC->root(), repo, versionId, &updateDlg); + status.rootPath = MMC->rootPath; + + GoUpdate::DownloadTask updateTask(status, &updateDlg); // If the task succeeds, install the updates. if (updateDlg.exec(&updateTask)) { @@ -1827,6 +1829,7 @@ void MainWindow::launchInstance(InstancePtr instance, AuthSessionPtr session, console = new ConsoleWindow(proc); connect(console, SIGNAL(isClosing()), this, SLOT(instanceEnded())); + proc->setHeader("MultiMC version: " + BuildConfig.printableVersionString() + "\n\n"); proc->arm(); if (profiler) diff --git a/gui/MainWindow.h b/gui/MainWindow.h index cccd9f9c..13edce81 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -22,6 +22,7 @@ #include "logic/BaseInstance.h" #include "logic/auth/MojangAccount.h" #include "logic/net/NetJob.h" +#include "logic/updater/GoUpdate.h" class NewsChecker; class NotificationChecker; @@ -157,7 +158,7 @@ slots: void startTask(Task *task); - void updateAvailable(QString repo, QString versionName, int versionId); + void updateAvailable(GoUpdate::Status status); void updateNotAvailable(); @@ -172,9 +173,9 @@ slots: void updateNewsLabel(); /*! - * Runs the DownloadUpdateTask and installs updates. + * Runs the DownloadTask and installs updates. */ - void downloadUpdates(QString repo, int versionId, bool installOnExit = false); + void downloadUpdates(GoUpdate::Status status, bool installOnExit = false); protected: bool eventFilter(QObject *obj, QEvent *ev); diff --git a/logic/BaseProcess.cpp b/logic/BaseProcess.cpp index eb1bbfdc..b0509d46 100644 --- a/logic/BaseProcess.cpp +++ b/logic/BaseProcess.cpp @@ -139,6 +139,12 @@ void BaseProcess::setWorkdir(QString path) m_prepostlaunchprocess.setWorkingDirectory(mcDir.absolutePath()); } +void BaseProcess::printHeader() +{ + emit log(m_header); +} + + void BaseProcess::logOutput(const QStringList &lines, MessageLevel::Enum defaultLevel, bool guessLevel, bool censor) { diff --git a/logic/BaseProcess.h b/logic/BaseProcess.h index ff8b9ed3..75bf0217 100644 --- a/logic/BaseProcess.h +++ b/logic/BaseProcess.h @@ -54,6 +54,12 @@ public: /* methods */ return m_instance; } + /// Set the text printed on top of the log + void setHeader(QString header) + { + m_header = header; + } + void setWorkdir(QString path); void killProcess(); @@ -79,6 +85,8 @@ protected: /* methods */ bool waitForPrePost(); QString substituteVariables(const QString &cmd) const; + void printHeader(); + virtual QMap<QString, QString> getVariables() const = 0; virtual QString censorPrivateInfo(QString in) = 0; virtual MessageLevel::Enum guessLevel(const QString &message, MessageLevel::Enum defaultLevel) = 0; @@ -130,4 +138,5 @@ protected: QString m_out_leftover; QProcess m_prepostlaunchprocess; bool killed = false; + QString m_header; }; diff --git a/logic/forge/ForgeMirrors.cpp b/logic/forge/ForgeMirrors.cpp index 06abb88f..e922fcc3 100644 --- a/logic/forge/ForgeMirrors.cpp +++ b/logic/forge/ForgeMirrors.cpp @@ -21,7 +21,7 @@ void ForgeMirrors::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->get(request); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); diff --git a/logic/forge/ForgeXzDownload.cpp b/logic/forge/ForgeXzDownload.cpp index ac2c4b1f..65b20959 100644 --- a/logic/forge/ForgeXzDownload.cpp +++ b/logic/forge/ForgeXzDownload.cpp @@ -70,7 +70,7 @@ void ForgeXzDownload::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->get(request); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); diff --git a/logic/minecraft/MinecraftProcess.cpp b/logic/minecraft/MinecraftProcess.cpp index e8682a50..75287856 100644 --- a/logic/minecraft/MinecraftProcess.cpp +++ b/logic/minecraft/MinecraftProcess.cpp @@ -14,8 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "BuildConfig.h" - #include "logic/minecraft/MinecraftProcess.h" #include "logic/BaseInstance.h" @@ -158,7 +156,7 @@ QStringList MinecraftProcess::javaArguments() const void MinecraftProcess::arm() { - emit log("MultiMC version: " + BuildConfig.printableVersionString() + "\n\n"); + printHeader(); emit log("Minecraft folder is:\n" + workingDirectory() + "\n\n"); if (!preLaunch()) diff --git a/logic/net/ByteArrayDownload.cpp b/logic/net/ByteArrayDownload.cpp index 9f71f911..7a41ee28 100644 --- a/logic/net/ByteArrayDownload.cpp +++ b/logic/net/ByteArrayDownload.cpp @@ -31,7 +31,7 @@ void ByteArrayDownload::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->get(request); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 6ddfc55f..0bbe75c3 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -77,7 +77,7 @@ void CacheDownload::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->get(request); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); diff --git a/logic/net/MD5EtagDownload.cpp b/logic/net/MD5EtagDownload.cpp index 6e1d0cfc..03752ea8 100644 --- a/logic/net/MD5EtagDownload.cpp +++ b/logic/net/MD5EtagDownload.cpp @@ -86,7 +86,7 @@ void MD5EtagDownload::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->get(request); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); diff --git a/logic/net/NetAction.h b/logic/net/NetAction.h index 81cb1bdb..b306bdfe 100644 --- a/logic/net/NetAction.h +++ b/logic/net/NetAction.h @@ -19,6 +19,7 @@ #include <QUrl> #include <memory> #include <QNetworkReply> +#include <logic/QObjectPtr.h> enum JobStatus { @@ -58,7 +59,7 @@ public: public: /// the network reply - std::shared_ptr<QNetworkReply> m_reply; + QObjectPtr<QNetworkReply> m_reply; /// the content of the content-type header QString m_content_type; diff --git a/logic/net/NetJob.cpp b/logic/net/NetJob.cpp index e10afbc8..5a50b876 100644 --- a/logic/net/NetJob.cpp +++ b/logic/net/NetJob.cpp @@ -29,7 +29,7 @@ void NetJob::partSucceeded(int index) m_doing.remove(index); m_done.insert(index); - disconnect(downloads[index].get(), 0, this, 0); + downloads[index].get()->disconnect(this); startMoreParts(); } @@ -46,7 +46,7 @@ void NetJob::partFailed(int index) slot.failures++; m_todo.enqueue(index); } - disconnect(downloads[index].get(), 0, this, 0); + downloads[index].get()->disconnect(this); startMoreParts(); } diff --git a/logic/net/NetJob.h b/logic/net/NetJob.h index 0d8b7fbd..708afa79 100644 --- a/logic/net/NetJob.h +++ b/logic/net/NetJob.h @@ -108,6 +108,7 @@ private: qint64 current_progress = 0; qint64 total_progress = 1; int failures = 0; + bool connected = false; }; QString m_job_name; QList<NetActionPtr> downloads; diff --git a/logic/screenshots/ImgurAlbumCreation.cpp b/logic/screenshots/ImgurAlbumCreation.cpp index 45db75cf..f7cc2971 100644 --- a/logic/screenshots/ImgurAlbumCreation.cpp +++ b/logic/screenshots/ImgurAlbumCreation.cpp @@ -36,7 +36,7 @@ void ImgurAlbumCreation::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->post(request, data); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, &QNetworkReply::uploadProgress, this, &ImgurAlbumCreation::downloadProgress); connect(rep, &QNetworkReply::finished, this, &ImgurAlbumCreation::downloadFinished); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), diff --git a/logic/screenshots/ImgurUpload.cpp b/logic/screenshots/ImgurUpload.cpp index 1d490436..4ae37dfa 100644 --- a/logic/screenshots/ImgurUpload.cpp +++ b/logic/screenshots/ImgurUpload.cpp @@ -51,7 +51,7 @@ void ImgurUpload::start() auto worker = ENV.qnam(); QNetworkReply *rep = worker->post(request, multipart); - m_reply = std::shared_ptr<QNetworkReply>(rep); + m_reply.reset(rep); connect(rep, &QNetworkReply::uploadProgress, this, &ImgurUpload::downloadProgress); connect(rep, &QNetworkReply::finished, this, &ImgurUpload::downloadFinished); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), diff --git a/logic/updater/DownloadTask.cpp b/logic/updater/DownloadTask.cpp new file mode 100644 index 00000000..badbc0d0 --- /dev/null +++ b/logic/updater/DownloadTask.cpp @@ -0,0 +1,179 @@ +/* Copyright 2013-2014 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 "DownloadTask.h" + +#include "logic/updater/UpdateChecker.h" +#include "GoUpdate.h" +#include "logic/net/NetJob.h" +#include "pathutils.h" + +#include <QFile> +#include <QTemporaryDir> +#include <QCryptographicHash> + +namespace GoUpdate +{ + +DownloadTask::DownloadTask(Status status, QObject *parent) + : Task(parent) +{ + m_status = status; + + m_updateFilesDir.setAutoRemove(false); +} + +void DownloadTask::setUseLocalUpdater(bool useLocal) +{ + m_keepLocalUpdater = useLocal; +} + +void DownloadTask::executeTask() +{ + loadVersionInfo(); +} + +void DownloadTask::loadVersionInfo() +{ + setStatus(tr("Loading version information...")); + + m_currentVersionFileListDownload.reset(); + m_newVersionFileListDownload.reset(); + + NetJob *netJob = new NetJob("Version Info"); + + // Find the index URL. + QUrl newIndexUrl = QUrl(m_status.newRepoUrl).resolved(QString::number(m_status.newVersionId) + ".json"); + qDebug() << m_status.newRepoUrl << " turns into " << newIndexUrl; + + netJob->addNetAction(m_newVersionFileListDownload = ByteArrayDownload::make(newIndexUrl)); + + // If we have a current version URL, get that one too. + if (!m_status.currentRepoUrl.isEmpty()) + { + QUrl cIndexUrl = QUrl(m_status.currentRepoUrl).resolved(QString::number(m_status.currentVersionId) + ".json"); + netJob->addNetAction(m_currentVersionFileListDownload = ByteArrayDownload::make(cIndexUrl)); + qDebug() << m_status.currentRepoUrl << " turns into " << cIndexUrl; + } + + // connect signals and start the job + connect(netJob, &NetJob::succeeded, this, &DownloadTask::processDownloadedVersionInfo); + connect(netJob, &NetJob::failed, this, &DownloadTask::vinfoDownloadFailed); + m_vinfoNetJob.reset(netJob); + netJob->start(); +} + +void DownloadTask::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_newVersionFileListDownload->m_status != Job_Failed) + { + processDownloadedVersionInfo(); + return; + } + + // TODO: Give a more detailed error message. + qCritical() << "Failed to download version info files."; + emitFailed(tr("Failed to download version info files.")); +} + +void DownloadTask::processDownloadedVersionInfo() +{ + VersionFileList m_currentVersionFileList; + VersionFileList m_newVersionFileList; + OperationList operationList; + + setStatus(tr("Reading file list for new version...")); + qDebug() << "Reading file list for new version..."; + QString error; + if (!parseVersionInfo(m_newVersionFileListDownload->m_data, m_newVersionFileList, error)) + { + qCritical() << error; + emitFailed(error); + return; + } + + // if we have the current version info, use it. + if (m_currentVersionFileListDownload && m_currentVersionFileListDownload->m_status != Job_Failed) + { + setStatus(tr("Reading file list for current version...")); + qDebug() << "Reading file list for current version..."; + // if this fails, it's not a complete loss. + QString error; + if(!parseVersionInfo( m_currentVersionFileListDownload->m_data, m_currentVersionFileList, error)) + { + qDebug() << error << "This is not a fatal error."; + } + } + + // We don't need this any more. + m_currentVersionFileListDownload.reset(); + m_newVersionFileListDownload.reset(); + m_vinfoNetJob.reset(); + + setStatus(tr("Processing file lists - figuring out how to install the update...")); + + // make a new netjob for the actual update files + NetJobPtr netJob (new NetJob("Update Files")); + + // fill netJob and operationList + if (!processFileLists(m_currentVersionFileList, m_newVersionFileList, m_status.rootPath, m_updateFilesDir.path(), netJob, operationList, m_keepLocalUpdater)) + { + emitFailed(tr("Failed to process update lists...")); + return; + } + + // write the instruction file for the file swapper + if(!writeInstallScript(operationList, PathCombine(m_updateFilesDir.path(), "file_list.xml"))) + { + emitFailed(tr("Failed to write update script file.")); + return; + } + + // Now start the download. + QObject::connect(netJob.get(), &NetJob::succeeded, this, &DownloadTask::fileDownloadFinished); + QObject::connect(netJob.get(), &NetJob::progress, this, &DownloadTask::fileDownloadProgressChanged); + QObject::connect(netJob.get(), &NetJob::failed, this, &DownloadTask::fileDownloadFailed); + + setStatus(tr("Downloading %1 update files.").arg(QString::number(netJob->size()))); + qDebug() << "Begin downloading update files to" << m_updateFilesDir.path(); + m_filesNetJob = netJob; + m_filesNetJob->start(); +} + +void DownloadTask::fileDownloadFinished() +{ + emitSucceeded(); +} + +void DownloadTask::fileDownloadFailed() +{ + // TODO: Give more info about the failure. + qCritical() << "Failed to download update files."; + emitFailed(tr("Failed to download update files.")); +} + +void DownloadTask::fileDownloadProgressChanged(qint64 current, qint64 total) +{ + setProgress((int)(((float)current / (float)total) * 100)); +} + +QString DownloadTask::updateFilesDir() +{ + return m_updateFilesDir.path(); +} + +}
\ No newline at end of file diff --git a/logic/updater/DownloadTask.h b/logic/updater/DownloadTask.h new file mode 100644 index 00000000..2b8c6299 --- /dev/null +++ b/logic/updater/DownloadTask.h @@ -0,0 +1,85 @@ +/* Copyright 2013-2014 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" +#include "GoUpdate.h" + +namespace GoUpdate +{ +/*! + * The DownloadTask 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 DownloadTask : public Task +{ + Q_OBJECT + +public: + explicit DownloadTask(Status status, QObject* parent = 0); + + /// Get the directory that will contain the update files. + QString updateFilesDir(); + + /// set updater download behavior + void setUseLocalUpdater(bool useLocal); + +protected: + //! Entry point for tasks. + virtual void executeTask() override; + + /*! + * 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. + */ + void loadVersionInfo(); + + NetJobPtr m_vinfoNetJob; + ByteArrayDownloadPtr m_currentVersionFileListDownload; + ByteArrayDownloadPtr m_newVersionFileListDownload; + + NetJobPtr m_filesNetJob; + + Status m_status; + + bool m_keepLocalUpdater; + + /*! + * 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: + /*! + * This function is called when version information is finished downloading + * and at least the new file list download succeeded + */ + void processDownloadedVersionInfo(); + void vinfoDownloadFailed(); + + void fileDownloadFinished(); + void fileDownloadFailed(); + void fileDownloadProgressChanged(qint64 current, qint64 total); +}; + +}
\ No newline at end of file diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp deleted file mode 100644 index 228d26d2..00000000 --- a/logic/updater/DownloadUpdateTask.cpp +++ /dev/null @@ -1,549 +0,0 @@ -/* Copyright 2013-2015 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/Env.h" -#include "BuildConfig.h" - -#include "logic/updater/UpdateChecker.h" -#include "logic/net/NetJob.h" -#include "pathutils.h" - -#include <QFile> -#include <QTemporaryDir> -#include <QCryptographicHash> - -#include <QDomDocument> - -DownloadUpdateTask::DownloadUpdateTask(QString rootPath, QString repoUrl, int versionId, QObject *parent) - : Task(parent) -{ - m_cVersionId = BuildConfig.VERSION_BUILD; - m_rootPath = rootPath; - - 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::processChannels() -{ - auto checker = MMC->updateChecker(); - - // Now, check the channel list again. - if (!checker->hasChannels()) - { - // We still couldn't load the channel list. Give up. Call loadVersionInfo and return. - qDebug() << "Reloading the channel list didn't work. Giving up."; - loadVersionInfo(); - return; - } - - QList<UpdateChecker::ChannelListEntry> channels = checker->getChannelList(); - QString channelId = BuildConfig.VERSION_CHANNEL; - - m_cRepoUrl.clear(); - // Search through the channel list for a channel with the correct ID. - for (auto channel : channels) - { - if (channel.id == channelId) - { - qDebug() << "Found matching channel."; - m_cRepoUrl = channel.url; - break; - } - } - - // Now that we've done that, load version info. - loadVersionInfo(); -} - -void DownloadUpdateTask::findCurrentVersionInfo() -{ - setStatus(tr("Finding information about the current version...")); - - auto checker = MMC->updateChecker(); - - if (!checker->hasChannels()) - { - // Load the channel list and wait for it to finish loading. - qDebug() << "No channel list entries found. Will try reloading it."; - - QObject::connect(checker.get(), &UpdateChecker::channelListLoaded, this, - &DownloadUpdateTask::processChannels); - checker->updateChanList(false); - } - else - { - processChannels(); - } -} - -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"); - qDebug() << m_nRepoUrl << " turns into " << newIndexUrl; - - // 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)); - qDebug() << m_cRepoUrl << " turns into " << 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. - qCritical() << "Failed to download version info files."; - emitFailed(tr("Failed to download version info files.")); -} - -void DownloadUpdateTask::parseDownloadedVersionInfo() -{ - setStatus(tr("Reading file list for new version...")); - qDebug() << "Reading file list for new version..."; - QString error; - if (!parseVersionInfo( - std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->first())->m_data, - &m_nVersionFileList, &error)) - { - emitFailed(error); - return; - } - - // 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) - { - setStatus(tr("Reading file list for current version...")); - qDebug() << "Reading file list for current version..."; - QString error; - parseVersionInfo( - std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->operator[](1))->m_data, - &m_cVersionFileList, &error); - } - - // 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(); -} - -bool DownloadUpdateTask::parseVersionInfo(const QByteArray &data, VersionFileList *list, - QString *error) -{ - QJsonParseError jsonError; - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); - if (jsonError.error != QJsonParseError::NoError) - { - *error = QString("Failed to parse version info JSON: %1 at %2") - .arg(jsonError.errorString()) - .arg(jsonError.offset); - qCritical() << error; - return false; - } - - QJsonObject json = jsonDoc.object(); - - qDebug() << data; - qDebug() << "Loading version info from JSON."; - QJsonArray filesArray = json.value("Files").toArray(); - for (QJsonValue fileValue : filesArray) - { - QJsonObject fileObj = fileValue.toObject(); - - QString file_path = fileObj.value("Path").toString(); -#ifdef Q_OS_MAC - // On OSX, the paths for the updater need to be fixed. - // basically, anything that isn't in the .app folder is ignored. - // everything else is changed so the code that processes the files actually finds - // them and puts the replacements in the right spots. - if (!fixPathForOSX(file_path)) - continue; -#endif - VersionFileEntry file{file_path, fileObj.value("Perms").toVariant().toInt(), - FileSourceList(), fileObj.value("MD5").toString(), }; - qDebug() << "File" << file.path << "with perms" << file.mode; - - 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 - { - qWarning() << "Unknown source type" << type << "ignored."; - } - } - - qDebug() << "Loaded info for" << file.path; - - list->append(file); - } - - return true; -} - -void DownloadUpdateTask::processFileLists() -{ - // Create a network job for downloading files. - NetJob *netJob = new NetJob("Update Files"); - - if (!processFileLists(netJob, m_cVersionFileList, m_nVersionFileList, m_operationList)) - { - emitFailed(tr("Failed to process update lists...")); - return; - } - - // 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()))); - qDebug() << "Begin downloading update files to" << m_updateFilesDir.path(); - m_filesNetJob.reset(netJob); - netJob->start(); - - writeInstallScript(m_operationList, PathCombine(m_updateFilesDir.path(), "file_list.xml")); -} - -bool -DownloadUpdateTask::processFileLists(NetJob *job, - const DownloadUpdateTask::VersionFileList ¤tVersion, - const DownloadUpdateTask::VersionFileList &newVersion, - DownloadUpdateTask::UpdateOperationList &ops) -{ - 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 : currentVersion) - { - QFileInfo toDelete(PathCombine(m_rootPath, entry.path)); - if (!toDelete.exists()) - { - qCritical() << "Expected file " << toDelete.absoluteFilePath() - << " doesn't exist!"; - } - bool keep = false; - - // - for (VersionFileEntry newEntry : newVersion) - { - if (newEntry.path == entry.path) - { - qDebug() << "Not deleting" << entry.path - << "because it is still present in the new version."; - keep = true; - break; - } - } - - // If the loop reaches the end and we didn't find a match, delete the file. - if (!keep) - { - if (toDelete.exists()) - ops.append(UpdateOperation::DeleteOp(entry.path)); - } - } - - // Next, check each file in MultiMC's folder and see if we need to update them. - for (VersionFileEntry entry : newVersion) - { - // 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; - QString realEntryPath = PathCombine(m_rootPath, entry.path); - QFile entryFile(realEntryPath); - QFileInfo entryInfo(realEntryPath); - - bool needs_upgrade = false; - if (!entryFile.exists()) - { - needs_upgrade = true; - } - else - { - bool pass = true; - if (!entryInfo.isReadable()) - { - qCritical() << "File " << realEntryPath << " is not readable."; - pass = false; - } - if (!entryInfo.isWritable()) - { - qCritical() << "File " << realEntryPath << " is not writable."; - pass = false; - } - if (!entryFile.open(QFile::ReadOnly)) - { - qCritical() << "File " << realEntryPath << " cannot be opened for reading."; - pass = false; - } - if (!pass) - { - ops.clear(); - return false; - } - } - - if(!needs_upgrade) - { - QCryptographicHash hash(QCryptographicHash::Md5); - auto foo = entryFile.readAll(); - - hash.addData(foo); - fileMD5 = hash.result().toHex(); - if ((fileMD5 != entry.md5)) - { - qDebug() << "MD5Sum does not match!"; - qDebug() << "Expected:'" << entry.md5 << "'"; - qDebug() << "Got: '" << fileMD5 << "'"; - needs_upgrade = true; - } - } - - // skip file. it doesn't need an upgrade. - if (!needs_upgrade) - { - qDebug() << "File" << realEntryPath << " does not need updating."; - continue; - } - - // yep. this file actually needs an upgrade. PROCEED. - qDebug() << "Found file" << realEntryPath << " that needs updating."; - - // if it's the updater we want to treat it separately - bool isUpdater = entry.path.endsWith("updater") || entry.path.endsWith("updater.exe"); - - // 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") - { - qDebug() << "Will download" << entry.path << "from" << source.url; - - // Download it to updatedir/<filepath>-<md5> where filepath is the file's - // path with slashes replaced by underscores. - QString dlPath = - PathCombine(m_updateFilesDir.path(), QString(entry.path).replace("/", "_")); - - if (isUpdater) - { - if(BuildConfig.UPDATER_FORCE_LOCAL) - { - qDebug() << "Skipping updater download and using local version."; - } - else - { - auto cache_entry = ENV.metacache()->resolveEntry("root", entry.path); - qDebug() << "Updater will be in " << cache_entry->getFullPath(); - // force check. - cache_entry->stale = true; - - auto download = CacheDownload::make(QUrl(source.url), cache_entry); - job->addNetAction(download); - } - } - else - { - // We need to download the file to the updatefiles folder and add a task - // to copy it to its install path. - auto download = MD5EtagDownload::make(source.url, dlPath); - download->m_expected_md5 = entry.md5; - job->addNetAction(download); - ops.append(UpdateOperation::CopyOp(dlPath, entry.path, entry.mode)); - } - } - } - } - return true; -} - -bool DownloadUpdateTask::writeInstallScript(UpdateOperationList &opsList, QString scriptFile) -{ - // Build the base structure of the XML document. - QDomDocument doc; - - QDomElement root = doc.createElement("update"); - root.setAttribute("version", "3"); - doc.appendChild(root); - - QDomElement installFiles = doc.createElement("install"); - root.appendChild(installFiles); - - QDomElement removeFiles = doc.createElement("uninstall"); - root.appendChild(removeFiles); - - // Write the operation list to the XML document. - for (UpdateOperation op : opsList) - { - QDomElement file = doc.createElement("file"); - - switch (op.type) - { - case UpdateOperation::OP_COPY: - { - // Install the file. - QDomElement name = doc.createElement("source"); - QDomElement path = doc.createElement("dest"); - QDomElement mode = doc.createElement("mode"); - name.appendChild(doc.createTextNode(op.file)); - path.appendChild(doc.createTextNode(op.dest)); - // We need to add a 0 at the beginning here, because Qt doesn't convert to octal - // correctly. - mode.appendChild(doc.createTextNode("0" + QString::number(op.mode, 8))); - file.appendChild(name); - file.appendChild(path); - file.appendChild(mode); - installFiles.appendChild(file); - qDebug() << "Will install file " << op.file << " to " << op.dest; - } - break; - - case UpdateOperation::OP_DELETE: - { - // Delete the file. - file.appendChild(doc.createTextNode(op.file)); - removeFiles.appendChild(file); - qDebug() << "Will remove file" << op.file; - } - break; - - default: - qWarning() << "Can't write update operation of type" << op.type - << "to file. Not implemented."; - continue; - } - } - - // Write the XML document to the file. - QFile outFile(scriptFile); - - if (outFile.open(QIODevice::WriteOnly)) - { - outFile.write(doc.toByteArray()); - } - else - { - emitFailed(tr("Failed to write update script file.")); - return false; - } - - return true; -} - -bool DownloadUpdateTask::fixPathForOSX(QString &path) -{ - if (path.startsWith("MultiMC.app/")) - { - // remove the prefix and add a new, more appropriate one. - path.remove(0, 12); - return true; - } - else - { - qCritical() << "Update path not within .app: " << path; - return false; - } -} - -void DownloadUpdateTask::fileDownloadFinished() -{ - emitSucceeded(); -} - -void DownloadUpdateTask::fileDownloadFailed() -{ - // TODO: Give more info about the failure. - qCritical() << "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)); -} - -QString DownloadUpdateTask::updateFilesDir() -{ - return m_updateFilesDir.path(); -} diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h deleted file mode 100644 index 9a994687..00000000 --- a/logic/updater/DownloadUpdateTask.h +++ /dev/null @@ -1,220 +0,0 @@ -/* Copyright 2013-2015 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 rootPath, QString repoUrl, int versionId, QObject* parent=0); - - /*! - * Gets the directory that contains the update files. - */ - QString updateFilesDir(); - -public: - - // 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<FileSource> FileSourceList; - - /*! - * Structure that describes an entry in a GoUpdate version's `Files` list. - */ - struct VersionFileEntry - { - QString path; - int mode; - FileSourceList sources; - QString md5; - }; - typedef QList<VersionFileEntry> VersionFileList; - - /*! - * Structure that describes an operation to perform when installing updates. - */ - struct UpdateOperation - { - static UpdateOperation CopyOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_COPY, fsource, fdest, fmode}; } - static UpdateOperation MoveOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_MOVE, fsource, fdest, fmode}; } - static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 0644}; } - 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 file to operate on. If this is a DELETE or CHMOD operation, this is the file that will be modified. - QString file; - - //! 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<UpdateOperation> UpdateOperationList; - -protected: - friend class DownloadUpdateTaskTest; - - - /*! - * 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(); - - /*! - * 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. - */ - void processChannels(); - - /*! - * 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 bool parseVersionInfo(const QByteArray &data, VersionFileList* list, QString *error); - - /*! - * 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 bool processFileLists(NetJob *job, const VersionFileList ¤tVersion, const VersionFileList &newVersion, UpdateOperationList &ops); - - /*! - * Calls \see processFileLists to populate the \see m_operationList and a NetJob, and then executes - * the NetJob to fetch all needed files - */ - virtual void processFileLists(); - - /*! - * Takes the operations list and writes an install script for the updater to the update files directory. - */ - virtual bool writeInstallScript(UpdateOperationList& opsList, QString scriptFile); - - 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; - - // path to the root of the application - QString m_rootPath; - - /*! - * 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; - - /*! - * Filters paths - * This fixes destination paths for OSX. - * The updater runs in MultiMC.app/Contents/MacOs by default - * The destination paths are such as this: MultiMC.app/blah/blah - * - * Therefore we chop off the 'MultiMC.app' prefix - * - * Returns false if the path couldn't be fixed (is invalid) - */ - static bool fixPathForOSX(QString &path); - -protected slots: - void vinfoDownloadFinished(); - void vinfoDownloadFailed(); - - void fileDownloadFinished(); - void fileDownloadFailed(); - void fileDownloadProgressChanged(qint64 current, qint64 total); -}; - diff --git a/logic/updater/GoUpdate.cpp b/logic/updater/GoUpdate.cpp new file mode 100644 index 00000000..84dc164c --- /dev/null +++ b/logic/updater/GoUpdate.cpp @@ -0,0 +1,315 @@ +#include "GoUpdate.h" +#include <pathutils.h> +#include <QDebug> +#include <QDomDocument> +#include <QFile> +#include <logic/Env.h> + +namespace GoUpdate +{ + +bool parseVersionInfo(const QByteArray &data, VersionFileList &list, QString &error) +{ + QJsonParseError jsonError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError) + { + error = QString("Failed to parse version info JSON: %1 at %2") + .arg(jsonError.errorString()) + .arg(jsonError.offset); + qCritical() << error; + return false; + } + + QJsonObject json = jsonDoc.object(); + + qDebug() << data; + qDebug() << "Loading version info from JSON."; + QJsonArray filesArray = json.value("Files").toArray(); + for (QJsonValue fileValue : filesArray) + { + QJsonObject fileObj = fileValue.toObject(); + + QString file_path = fileObj.value("Path").toString(); +#ifdef Q_OS_MAC + // On OSX, the paths for the updater need to be fixed. + // basically, anything that isn't in the .app folder is ignored. + // everything else is changed so the code that processes the files actually finds + // them and puts the replacements in the right spots. + if (!fixPathForOSX(file_path)) + continue; +#endif + VersionFileEntry file{file_path, fileObj.value("Perms").toVariant().toInt(), + FileSourceList(), fileObj.value("MD5").toString(), }; + qDebug() << "File" << file.path << "with perms" << file.mode; + + 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 + { + qWarning() << "Unknown source type" << type << "ignored."; + } + } + + qDebug() << "Loaded info for" << file.path; + + list.append(file); + } + + return true; +} + +bool processFileLists +( + const VersionFileList ¤tVersion, + const VersionFileList &newVersion, + const QString &rootPath, + const QString &tempPath, + NetJobPtr job, + OperationList &ops, + bool useLocalUpdater +) +{ + // 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 : currentVersion) + { + QFileInfo toDelete(PathCombine(rootPath, entry.path)); + if (!toDelete.exists()) + { + qCritical() << "Expected file " << toDelete.absoluteFilePath() + << " doesn't exist!"; + } + bool keep = false; + + // + for (VersionFileEntry newEntry : newVersion) + { + if (newEntry.path == entry.path) + { + qDebug() << "Not deleting" << entry.path + << "because it is still present in the new version."; + keep = true; + break; + } + } + + // If the loop reaches the end and we didn't find a match, delete the file. + if (!keep) + { + if (toDelete.exists()) + ops.append(Operation::DeleteOp(entry.path)); + } + } + + // Next, check each file in MultiMC's folder and see if we need to update them. + for (VersionFileEntry entry : newVersion) + { + // 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; + QString realEntryPath = PathCombine(rootPath, entry.path); + QFile entryFile(realEntryPath); + QFileInfo entryInfo(realEntryPath); + + bool needs_upgrade = false; + if (!entryFile.exists()) + { + needs_upgrade = true; + } + else + { + bool pass = true; + if (!entryInfo.isReadable()) + { + qCritical() << "File " << realEntryPath << " is not readable."; + pass = false; + } + if (!entryInfo.isWritable()) + { + qCritical() << "File " << realEntryPath << " is not writable."; + pass = false; + } + if (!entryFile.open(QFile::ReadOnly)) + { + qCritical() << "File " << realEntryPath << " cannot be opened for reading."; + pass = false; + } + if (!pass) + { + ops.clear(); + return false; + } + } + + if(!needs_upgrade) + { + QCryptographicHash hash(QCryptographicHash::Md5); + auto foo = entryFile.readAll(); + + hash.addData(foo); + fileMD5 = hash.result().toHex(); + if ((fileMD5 != entry.md5)) + { + qDebug() << "MD5Sum does not match!"; + qDebug() << "Expected:'" << entry.md5 << "'"; + qDebug() << "Got: '" << fileMD5 << "'"; + needs_upgrade = true; + } + } + + // skip file. it doesn't need an upgrade. + if (!needs_upgrade) + { + qDebug() << "File" << realEntryPath << " does not need updating."; + continue; + } + + // yep. this file actually needs an upgrade. PROCEED. + qDebug() << "Found file" << realEntryPath << " that needs updating."; + + // if it's the updater we want to treat it separately + bool isUpdater = entry.path.endsWith("updater") || entry.path.endsWith("updater.exe"); + + // 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") + continue; + + qDebug() << "Will download" << entry.path << "from" << source.url; + + // Download it to updatedir/<filepath>-<md5> where filepath is the file's + // path with slashes replaced by underscores. + QString dlPath = PathCombine(tempPath, QString(entry.path).replace("/", "_")); + + if (isUpdater) + { + if(useLocalUpdater) + { + qDebug() << "Skipping updater download and using local version."; + } + else + { + auto cache_entry = ENV.metacache()->resolveEntry("root", entry.path); + qDebug() << "Updater will be in " << cache_entry->getFullPath(); + // force check. + cache_entry->stale = true; + + auto download = CacheDownload::make(QUrl(source.url), cache_entry); + job->addNetAction(download); + } + } + else + { + // We need to download the file to the updatefiles folder and add a task + // to copy it to its install path. + auto download = MD5EtagDownload::make(source.url, dlPath); + download->m_expected_md5 = entry.md5; + job->addNetAction(download); + ops.append(Operation::CopyOp(dlPath, entry.path, entry.mode)); + } + } + } + return true; +} + +bool fixPathForOSX(QString &path) +{ + if (path.startsWith("MultiMC.app/")) + { + // remove the prefix and add a new, more appropriate one. + path.remove(0, 12); + return true; + } + else + { + qCritical() << "Update path not within .app: " << path; + return false; + } +} + +bool writeInstallScript(OperationList &opsList, QString scriptFile) +{ + // Build the base structure of the XML document. + QDomDocument doc; + + QDomElement root = doc.createElement("update"); + root.setAttribute("version", "3"); + doc.appendChild(root); + + QDomElement installFiles = doc.createElement("install"); + root.appendChild(installFiles); + + QDomElement removeFiles = doc.createElement("uninstall"); + root.appendChild(removeFiles); + + // Write the operation list to the XML document. + for (Operation op : opsList) + { + QDomElement file = doc.createElement("file"); + + switch (op.type) + { + case Operation::OP_COPY: + { + // Install the file. + QDomElement name = doc.createElement("source"); + QDomElement path = doc.createElement("dest"); + QDomElement mode = doc.createElement("mode"); + name.appendChild(doc.createTextNode(op.file)); + path.appendChild(doc.createTextNode(op.dest)); + // We need to add a 0 at the beginning here, because Qt doesn't convert to octal + // correctly. + mode.appendChild(doc.createTextNode("0" + QString::number(op.mode, 8))); + file.appendChild(name); + file.appendChild(path); + file.appendChild(mode); + installFiles.appendChild(file); + qDebug() << "Will install file " << op.file << " to " << op.dest; + } + break; + + case Operation::OP_DELETE: + { + // Delete the file. + file.appendChild(doc.createTextNode(op.file)); + removeFiles.appendChild(file); + qDebug() << "Will remove file" << op.file; + } + break; + + default: + qWarning() << "Can't write update operation of type" << op.type + << "to file. Not implemented."; + continue; + } + } + + // Write the XML document to the file. + QFile outFile(scriptFile); + + if (outFile.open(QIODevice::WriteOnly)) + { + outFile.write(doc.toByteArray()); + } + else + { + return false; + } + + return true; +} + + +}
\ No newline at end of file diff --git a/logic/updater/GoUpdate.h b/logic/updater/GoUpdate.h new file mode 100644 index 00000000..bea6cce7 --- /dev/null +++ b/logic/updater/GoUpdate.h @@ -0,0 +1,135 @@ +#pragma once +#include <QByteArray> +#include <logic/net/NetJob.h> + +namespace GoUpdate +{ + +/** + * A temporary object exchanged between updated checker and the actual update task + */ +struct Status +{ + bool updateAvailable = false; + + int newVersionId = -1; + QString newRepoUrl; + + int currentVersionId = -1; + QString currentRepoUrl; + + // path to the root of the application + QString rootPath; +}; + +/** + * 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; + } + + bool operator==(const FileSource &f2) const + { + return type == f2.type && url == f2.url && compressionType == f2.compressionType; + } + + QString type; + QString url; + QString compressionType; +}; +typedef QList<FileSource> FileSourceList; + +/** + * Structure that describes an entry in a GoUpdate version's `Files` list. + */ +struct VersionFileEntry +{ + QString path; + int mode; + FileSourceList sources; + QString md5; + bool operator==(const VersionFileEntry &v2) const + { + return path == v2.path && mode == v2.mode && sources == v2.sources && md5 == v2.md5; + } +}; +typedef QList<VersionFileEntry> VersionFileList; + +/** + * Structure that describes an operation to perform when installing updates. + */ +struct Operation +{ + static Operation CopyOp(QString fsource, QString fdest, int fmode=0644) { return Operation{OP_COPY, fsource, fdest, fmode}; } + static Operation MoveOp(QString fsource, QString fdest, int fmode=0644) { return Operation{OP_MOVE, fsource, fdest, fmode}; } + static Operation DeleteOp(QString file) { return Operation{OP_DELETE, file, "", 0644}; } + static Operation ChmodOp(QString file, int fmode) { return Operation{OP_CHMOD, file, "", fmode}; } + + // FIXME: for some types, some of the other fields are irrelevant! + bool operator==(const Operation &u2) const + { + return type == u2.type && file == u2.file && dest == u2.dest && mode == u2.mode; + } + + //! Specifies the type of operation that this is. + enum Type + { + OP_COPY, + OP_DELETE, + OP_MOVE, + OP_CHMOD, + } type; + + //! The file to operate on. If this is a DELETE or CHMOD operation, this is the file that will be modified. + QString file; + + //! 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; +}; +typedef QList<Operation> OperationList; + +/** + * Takes the @OperationList list and writes an install script for the updater to the update files directory. + */ +bool writeInstallScript(OperationList& opsList, QString scriptFile); + +/** + * Loads the file list from the given version info JSON object into the given list. + */ +bool parseVersionInfo(const QByteArray &data, VersionFileList& list, QString &error); + +/*! + * 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. + */ +bool processFileLists +( + const VersionFileList ¤tVersion, + const VersionFileList &newVersion, + const QString &rootPath, + const QString &tempPath, + NetJobPtr job, + OperationList &ops, + bool useLocalUpdater +); + +/*! + * This fixes destination paths for OSX - removes 'MultiMC.app' prefix + * The updater runs in MultiMC.app/Contents/MacOs by default + * The destination paths are such as this: MultiMC.app/blah/blah + * + * @return false if the path couldn't be fixed (is invalid) + */ +bool fixPathForOSX(QString &path); + +} +Q_DECLARE_METATYPE(GoUpdate::Status);
\ No newline at end of file diff --git a/logic/updater/UpdateChecker.cpp b/logic/updater/UpdateChecker.cpp index 8ac84bf1..7d214ed4 100644 --- a/logic/updater/UpdateChecker.cpp +++ b/logic/updater/UpdateChecker.cpp @@ -23,10 +23,12 @@ #define API_VERSION 0 #define CHANLIST_FORMAT 0 -UpdateChecker::UpdateChecker(QString channelListUrl, int currentBuild) +UpdateChecker::UpdateChecker(QString channelListUrl, QString currentChannel, int currentBuild) { m_channelListUrl = channelListUrl; + m_currentChannel = currentChannel; m_currentBuild = currentBuild; + m_updateChecking = false; m_chanListLoading = false; m_checkUpdateWaiting = false; @@ -69,24 +71,26 @@ void UpdateChecker::checkForUpdate(QString updateChannel, bool notifyNoUpdate) // Find the desired channel within the channel list and get its repo URL. If if cannot be // found, error. - m_repoUrl = ""; + m_newRepoUrl = ""; for (ChannelListEntry entry : m_channels) { if (entry.id == updateChannel) - m_repoUrl = entry.url; + m_newRepoUrl = entry.url; + if (entry.id == m_currentChannel) + m_currentRepoUrl = entry.url; } - qDebug() << "m_repoUrl = " << m_repoUrl; + qDebug() << "m_repoUrl = " << m_newRepoUrl; // If we didn't find our channel, error. - if (m_repoUrl.isEmpty()) + if (m_newRepoUrl.isEmpty()) { qCritical() << "m_repoUrl is empty!"; emit updateCheckFailed(); return; } - QUrl indexUrl = QUrl(m_repoUrl).resolved(QUrl("index.json")); + QUrl indexUrl = QUrl(m_newRepoUrl).resolved(QUrl("index.json")); auto job = new NetJob("GoUpdate Repository Index"); job->addNetAction(ByteArrayDownload::make(indexUrl)); @@ -149,8 +153,13 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate) { qDebug() << "Found newer version with ID" << newBuildNumber; // Update! - emit updateAvailable(m_repoUrl, newestVersion.value("Name").toVariant().toString(), - newBuildNumber); + 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) { diff --git a/logic/updater/UpdateChecker.h b/logic/updater/UpdateChecker.h index 2b08680a..0eaceea0 100644 --- a/logic/updater/UpdateChecker.h +++ b/logic/updater/UpdateChecker.h @@ -16,6 +16,7 @@ #pragma once #include "logic/net/NetJob.h" +#include "GoUpdate.h" #include <QUrl> @@ -24,7 +25,7 @@ class UpdateChecker : public QObject Q_OBJECT public: - UpdateChecker(QString channelListUrl, int currentBuild); + UpdateChecker(QString channelListUrl, QString currentChannel, int currentBuild); void checkForUpdate(QString updateChannel, bool notifyNoUpdate); /*! @@ -57,7 +58,7 @@ 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); + void updateAvailable(GoUpdate::Status status); //! Signal emitted when the channel list finishes loading or fails to load. void channelListLoaded(); @@ -77,8 +78,6 @@ private: NetJobPtr indexJob; NetJobPtr chanListJob; - QString m_repoUrl; - QString m_channelListUrl; QList<ChannelListEntry> m_channels; @@ -110,6 +109,11 @@ private: * if m_checkUpdateWaiting, this is the last used update channel */ QString m_deferredUpdateChannel; + int m_currentBuild = -1; + QString m_currentChannel; + QString m_currentRepoUrl; + + QString m_newRepoUrl; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2f6b1a63..e891c379 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,7 +27,7 @@ add_unit_test(userutils tst_userutils.cpp) add_unit_test(modutils tst_modutils.cpp) add_unit_test(inifile tst_inifile.cpp) add_unit_test(UpdateChecker tst_UpdateChecker.cpp) -add_unit_test(DownloadUpdateTask tst_DownloadUpdateTask.cpp) +add_unit_test(DownloadTask tst_DownloadTask.cpp) # Tests END # diff --git a/tests/data/tst_DownloadUpdateTask-test_writeInstallScript.xml b/tests/data/tst_DownloadTask-test_writeInstallScript.xml index 09c162ca..09c162ca 100644 --- a/tests/data/tst_DownloadUpdateTask-test_writeInstallScript.xml +++ b/tests/data/tst_DownloadTask-test_writeInstallScript.xml diff --git a/tests/tst_DownloadTask.cpp b/tests/tst_DownloadTask.cpp new file mode 100644 index 00000000..9c87b027 --- /dev/null +++ b/tests/tst_DownloadTask.cpp @@ -0,0 +1,256 @@ +#include <QTest> +#include <QSignalSpy> + +#include "TestUtil.h" + +#include "logic/updater/GoUpdate.h" +#include "logic/updater/DownloadTask.h" +#include "logic/updater/UpdateChecker.h" +#include "depends/util/include/pathutils.h" + +using namespace GoUpdate; + +FileSourceList encodeBaseFile(const char *suffix) +{ + auto base = qApp->applicationDirPath(); + QUrl localFile = QUrl::fromLocalFile(base + suffix); + QString localUrlString = localFile.toString(QUrl::FullyEncoded); + auto item = FileSource("http", localUrlString); + return FileSourceList({item}); +} + +Q_DECLARE_METATYPE(VersionFileList) +Q_DECLARE_METATYPE(Operation) + +QDebug operator<<(QDebug dbg, const FileSource &f) +{ + dbg.nospace() << "FileSource(type=" << f.type << " url=" << f.url + << " comp=" << f.compressionType << ")"; + return dbg.maybeSpace(); +} + +QDebug operator<<(QDebug dbg, const VersionFileEntry &v) +{ + dbg.nospace() << "VersionFileEntry(path=" << v.path << " mode=" << v.mode + << " md5=" << v.md5 << " sources=" << v.sources << ")"; + return dbg.maybeSpace(); +} + +QDebug operator<<(QDebug dbg, const Operation::Type &t) +{ + switch (t) + { + case Operation::OP_COPY: + dbg << "OP_COPY"; + break; + case Operation::OP_DELETE: + dbg << "OP_DELETE"; + break; + case Operation::OP_MOVE: + dbg << "OP_MOVE"; + break; + case Operation::OP_CHMOD: + dbg << "OP_CHMOD"; + break; + } + return dbg.maybeSpace(); +} + +QDebug operator<<(QDebug dbg, const Operation &u) +{ + dbg.nospace() << "Operation(type=" << u.type << " file=" << u.file + << " dest=" << u.dest << " mode=" << u.mode << ")"; + return dbg.maybeSpace(); +} + +class DownloadTaskTest : public QObject +{ + Q_OBJECT +private +slots: + void initTestCase() + { + } + void cleanupTestCase() + { + } + + void test_writeInstallScript() + { + OperationList ops; + + ops << Operation::CopyOp("sourceOne", "destOne", 0777) + << Operation::CopyOp("MultiMC.exe", "M/u/l/t/i/M/C/e/x/e") + << Operation::DeleteOp("toDelete.abc"); + auto testFile = "tests/data/tst_DownloadTask-test_writeInstallScript.xml"; + const QString script = QDir::temp().absoluteFilePath("MultiMCUpdateScript.xml"); + QVERIFY(writeInstallScript(ops, script)); + QCOMPARE(TestsInternal::readFileUtf8(script).replace(QRegExp("[\r\n]+"), "\n"), + MULTIMC_GET_TEST_FILE_UTF8(testFile).replace(QRegExp("[\r\n]+"), "\n")); + } + + void test_parseVersionInfo_data() + { + QTest::addColumn<QByteArray>("data"); + QTest::addColumn<VersionFileList>("list"); + QTest::addColumn<QString>("error"); + QTest::addColumn<bool>("ret"); + + QTest::newRow("one") + << MULTIMC_GET_TEST_FILE("tests/data/1.json") + << (VersionFileList() + << VersionFileEntry{"fileOne", + 493, + encodeBaseFile("/tests/data/fileOneA"), + "9eb84090956c484e32cb6c08455a667b"} + << VersionFileEntry{"fileTwo", + 644, + encodeBaseFile("/tests/data/fileTwo"), + "38f94f54fa3eb72b0ea836538c10b043"} + << VersionFileEntry{"fileThree", + 750, + encodeBaseFile("/tests/data/fileThree"), + "f12df554b21e320be6471d7154130e70"}) + << QString() << true; + QTest::newRow("two") + << MULTIMC_GET_TEST_FILE("tests/data/2.json") + << (VersionFileList() + << VersionFileEntry{"fileOne", + 493, + encodeBaseFile("/tests/data/fileOneB"), + "42915a71277c9016668cce7b82c6b577"} + << VersionFileEntry{"fileTwo", + 644, + encodeBaseFile("/tests/data/fileTwo"), + "38f94f54fa3eb72b0ea836538c10b043"}) + << QString() << true; + } + void test_parseVersionInfo() + { + QFETCH(QByteArray, data); + QFETCH(VersionFileList, list); + QFETCH(QString, error); + QFETCH(bool, ret); + + VersionFileList outList; + QString outError; + bool outRet = parseVersionInfo(data, outList, outError); + QCOMPARE(outRet, ret); + QCOMPARE(outList, list); + QCOMPARE(outError, error); + } + + void test_processFileLists_data() + { + QTest::addColumn<QString>("tempFolder"); + QTest::addColumn<VersionFileList>("currentVersion"); + QTest::addColumn<VersionFileList>("newVersion"); + QTest::addColumn<OperationList>("expectedOperations"); + + QTemporaryDir tempFolderObj; + QString tempFolder = tempFolderObj.path(); + // update fileOne, keep fileTwo, remove fileThree + QTest::newRow("test 1") + << tempFolder << (VersionFileList() + << VersionFileEntry{ + "tests/data/fileOne", 493, + FileSourceList() + << FileSource( + "http", "http://host/path/fileOne-1"), + "9eb84090956c484e32cb6c08455a667b"} + << VersionFileEntry{ + "tests/data/fileTwo", 644, + FileSourceList() + << FileSource( + "http", "http://host/path/fileTwo-1"), + "38f94f54fa3eb72b0ea836538c10b043"} + << VersionFileEntry{ + "tests/data/fileThree", 420, + FileSourceList() + << FileSource( + "http", "http://host/path/fileThree-1"), + "f12df554b21e320be6471d7154130e70"}) + << (VersionFileList() + << VersionFileEntry{ + "tests/data/fileOne", 493, + FileSourceList() + << FileSource("http", + "http://host/path/fileOne-2"), + "42915a71277c9016668cce7b82c6b577"} + << VersionFileEntry{ + "tests/data/fileTwo", 644, + FileSourceList() + << FileSource("http", + "http://host/path/fileTwo-2"), + "38f94f54fa3eb72b0ea836538c10b043"}) + << (OperationList() + << Operation::DeleteOp("tests/data/fileThree") + << Operation::CopyOp( + PathCombine(tempFolder, + QString("tests/data/fileOne").replace("/", "_")), + "tests/data/fileOne", 493)); + } + void test_processFileLists() + { + QFETCH(QString, tempFolder); + QFETCH(VersionFileList, currentVersion); + QFETCH(VersionFileList, newVersion); + QFETCH(OperationList, expectedOperations); + + OperationList operations; + + processFileLists(currentVersion, newVersion, QCoreApplication::applicationDirPath(), tempFolder, new NetJob("Dummy"), operations, false); + qDebug() << (operations == expectedOperations); + qDebug() << operations; + qDebug() << expectedOperations; + QCOMPARE(operations, expectedOperations); + } +/* + void test_masterTest() + { + qDebug() << "#####################"; + MMC->m_version.build = 1; + MMC->m_version.channel = "develop"; + auto channels = + QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/channels.json")); + auto root = QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/")); + qDebug() << "channels: " << channels; + qDebug() << "root: " << root; + MMC->updateChecker()->setChannelListUrl(channels.toString()); + MMC->updateChecker()->setCurrentChannel("develop"); + + DownloadTask task(root.toString(), 2); + + QSignalSpy succeededSpy(&task, SIGNAL(succeeded())); + + task.start(); + + QVERIFY(succeededSpy.wait()); + } +*/ + void test_OSXPathFixup() + { + QString path, pathOrig; + bool result; + // Proper OSX path + pathOrig = path = "MultiMC.app/Foo/Bar/Baz"; + qDebug() << "Proper OSX path: " << path; + result = fixPathForOSX(path); + QCOMPARE(path, QString("Foo/Bar/Baz")); + QCOMPARE(result, true); + + // Bad OSX path + pathOrig = path = "translations/klingon.lol"; + qDebug() << "Bad OSX path: " << path; + result = fixPathForOSX(path); + QCOMPARE(path, pathOrig); + QCOMPARE(result, false); + } +}; + +extern "C" +{ + QTEST_GUILESS_MAIN(DownloadTaskTest) +} + +#include "tst_DownloadTask.moc" diff --git a/tests/tst_DownloadUpdateTask.cpp b/tests/tst_DownloadUpdateTask.cpp deleted file mode 100644 index 524095ed..00000000 --- a/tests/tst_DownloadUpdateTask.cpp +++ /dev/null @@ -1,273 +0,0 @@ -#include <QTest> -#include <QSignalSpy> - -#include "TestUtil.h" - -#include "logic/updater/DownloadUpdateTask.h" -#include "logic/updater/UpdateChecker.h" -#include "depends/util/include/pathutils.h" - -DownloadUpdateTask::FileSourceList encodeBaseFile(const char *suffix) -{ - auto base = qApp->applicationDirPath(); - QUrl localFile = QUrl::fromLocalFile(base + suffix); - QString localUrlString = localFile.toString(QUrl::FullyEncoded); - auto item = DownloadUpdateTask::FileSource("http", localUrlString); - return DownloadUpdateTask::FileSourceList({item}); -} - -Q_DECLARE_METATYPE(DownloadUpdateTask::VersionFileList) -Q_DECLARE_METATYPE(DownloadUpdateTask::UpdateOperation) - -bool operator==(const DownloadUpdateTask::FileSource &f1, - const DownloadUpdateTask::FileSource &f2) -{ - return f1.type == f2.type && f1.url == f2.url && f1.compressionType == f2.compressionType; -} -bool operator==(const DownloadUpdateTask::VersionFileEntry &v1, - const DownloadUpdateTask::VersionFileEntry &v2) -{ - return v1.path == v2.path && v1.mode == v2.mode && v1.sources == v2.sources && - v1.md5 == v2.md5; -} -bool operator==(const DownloadUpdateTask::UpdateOperation &u1, - const DownloadUpdateTask::UpdateOperation &u2) -{ - return u1.type == u2.type && u1.file == u2.file && u1.dest == u2.dest && u1.mode == u2.mode; -} - -QDebug operator<<(QDebug dbg, const DownloadUpdateTask::FileSource &f) -{ - dbg.nospace() << "FileSource(type=" << f.type << " url=" << f.url - << " comp=" << f.compressionType << ")"; - return dbg.maybeSpace(); -} - -QDebug operator<<(QDebug dbg, const DownloadUpdateTask::VersionFileEntry &v) -{ - dbg.nospace() << "VersionFileEntry(path=" << v.path << " mode=" << v.mode - << " md5=" << v.md5 << " sources=" << v.sources << ")"; - return dbg.maybeSpace(); -} - -QDebug operator<<(QDebug dbg, const DownloadUpdateTask::UpdateOperation::Type &t) -{ - switch (t) - { - case DownloadUpdateTask::UpdateOperation::OP_COPY: - dbg << "OP_COPY"; - break; - case DownloadUpdateTask::UpdateOperation::OP_DELETE: - dbg << "OP_DELETE"; - break; - case DownloadUpdateTask::UpdateOperation::OP_MOVE: - dbg << "OP_MOVE"; - break; - case DownloadUpdateTask::UpdateOperation::OP_CHMOD: - dbg << "OP_CHMOD"; - break; - } - return dbg.maybeSpace(); -} - -QDebug operator<<(QDebug dbg, const DownloadUpdateTask::UpdateOperation &u) -{ - dbg.nospace() << "UpdateOperation(type=" << u.type << " file=" << u.file - << " dest=" << u.dest << " mode=" << u.mode << ")"; - return dbg.maybeSpace(); -} - -class DownloadUpdateTaskTest : public QObject -{ - Q_OBJECT -private -slots: - void initTestCase() - { - } - void cleanupTestCase() - { - } - - void test_writeInstallScript() - { - DownloadUpdateTask task(QCoreApplication::applicationDirPath(), - QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/")).toString(), 0); - - DownloadUpdateTask::UpdateOperationList ops; - - ops << DownloadUpdateTask::UpdateOperation::CopyOp("sourceOne", "destOne", 0777) - << DownloadUpdateTask::UpdateOperation::CopyOp("MultiMC.exe", "M/u/l/t/i/M/C/e/x/e") - << DownloadUpdateTask::UpdateOperation::DeleteOp("toDelete.abc"); - auto testFile = "tests/data/tst_DownloadUpdateTask-test_writeInstallScript.xml"; - const QString script = QDir::temp().absoluteFilePath("MultiMCUpdateScript.xml"); - QVERIFY(task.writeInstallScript(ops, script)); - QCOMPARE(TestsInternal::readFileUtf8(script).replace(QRegExp("[\r\n]+"), "\n"), - MULTIMC_GET_TEST_FILE_UTF8(testFile).replace(QRegExp("[\r\n]+"), "\n")); - } - -// DISABLED: fails. -/* - void test_parseVersionInfo_data() - { - QTest::addColumn<QByteArray>("data"); - QTest::addColumn<DownloadUpdateTask::VersionFileList>("list"); - QTest::addColumn<QString>("error"); - QTest::addColumn<bool>("ret"); - - QTest::newRow("one") - << MULTIMC_GET_TEST_FILE("tests/data/1.json") - << (DownloadUpdateTask::VersionFileList() - << DownloadUpdateTask::VersionFileEntry{"fileOne", - 493, - encodeBaseFile("/tests/data/fileOneA"), - "9eb84090956c484e32cb6c08455a667b"} - << DownloadUpdateTask::VersionFileEntry{"fileTwo", - 644, - encodeBaseFile("/tests/data/fileTwo"), - "38f94f54fa3eb72b0ea836538c10b043"} - << DownloadUpdateTask::VersionFileEntry{"fileThree", - 750, - encodeBaseFile("/tests/data/fileThree"), - "f12df554b21e320be6471d7154130e70"}) - << QString() << true; - QTest::newRow("two") - << MULTIMC_GET_TEST_FILE("tests/data/2.json") - << (DownloadUpdateTask::VersionFileList() - << DownloadUpdateTask::VersionFileEntry{"fileOne", - 493, - encodeBaseFile("/tests/data/fileOneB"), - "42915a71277c9016668cce7b82c6b577"} - << DownloadUpdateTask::VersionFileEntry{"fileTwo", - 644, - encodeBaseFile("/tests/data/fileTwo"), - "38f94f54fa3eb72b0ea836538c10b043"}) - << QString() << true; - } - void test_parseVersionInfo() - { - QFETCH(QByteArray, data); - QFETCH(DownloadUpdateTask::VersionFileList, list); - QFETCH(QString, error); - QFETCH(bool, ret); - - DownloadUpdateTask::VersionFileList outList; - QString outError; - bool outRet = DownloadUpdateTask("", 0).parseVersionInfo(data, &outList, &outError); - QCOMPARE(outRet, ret); - QCOMPARE(outList, list); - QCOMPARE(outError, error); - } -*/ - void test_processFileLists_data() - { - QTest::addColumn<DownloadUpdateTask *>("downloader"); - QTest::addColumn<DownloadUpdateTask::VersionFileList>("currentVersion"); - QTest::addColumn<DownloadUpdateTask::VersionFileList>("newVersion"); - QTest::addColumn<DownloadUpdateTask::UpdateOperationList>("expectedOperations"); - - DownloadUpdateTask *downloader = new DownloadUpdateTask(QCoreApplication::applicationDirPath(), QString(), -1); - - // update fileOne, keep fileTwo, remove fileThree - QTest::newRow("test 1") - << downloader << (DownloadUpdateTask::VersionFileList() - << DownloadUpdateTask::VersionFileEntry{ - "tests/data/fileOne", 493, - DownloadUpdateTask::FileSourceList() - << DownloadUpdateTask::FileSource( - "http", "http://host/path/fileOne-1"), - "9eb84090956c484e32cb6c08455a667b"} - << DownloadUpdateTask::VersionFileEntry{ - "tests/data/fileTwo", 644, - DownloadUpdateTask::FileSourceList() - << DownloadUpdateTask::FileSource( - "http", "http://host/path/fileTwo-1"), - "38f94f54fa3eb72b0ea836538c10b043"} - << DownloadUpdateTask::VersionFileEntry{ - "tests/data/fileThree", 420, - DownloadUpdateTask::FileSourceList() - << DownloadUpdateTask::FileSource( - "http", "http://host/path/fileThree-1"), - "f12df554b21e320be6471d7154130e70"}) - << (DownloadUpdateTask::VersionFileList() - << DownloadUpdateTask::VersionFileEntry{ - "tests/data/fileOne", 493, - DownloadUpdateTask::FileSourceList() - << DownloadUpdateTask::FileSource("http", - "http://host/path/fileOne-2"), - "42915a71277c9016668cce7b82c6b577"} - << DownloadUpdateTask::VersionFileEntry{ - "tests/data/fileTwo", 644, - DownloadUpdateTask::FileSourceList() - << DownloadUpdateTask::FileSource("http", - "http://host/path/fileTwo-2"), - "38f94f54fa3eb72b0ea836538c10b043"}) - << (DownloadUpdateTask::UpdateOperationList() - << DownloadUpdateTask::UpdateOperation::DeleteOp("tests/data/fileThree") - << DownloadUpdateTask::UpdateOperation::CopyOp( - PathCombine(downloader->updateFilesDir(), - QString("tests/data/fileOne").replace("/", "_")), - "tests/data/fileOne", 493)); - } - void test_processFileLists() - { - QFETCH(DownloadUpdateTask *, downloader); - QFETCH(DownloadUpdateTask::VersionFileList, currentVersion); - QFETCH(DownloadUpdateTask::VersionFileList, newVersion); - QFETCH(DownloadUpdateTask::UpdateOperationList, expectedOperations); - - DownloadUpdateTask::UpdateOperationList operations; - - downloader->processFileLists(new NetJob("Dummy"), currentVersion, newVersion, - operations); - qDebug() << (operations == expectedOperations); - qDebug() << operations; - qDebug() << expectedOperations; - QCOMPARE(operations, expectedOperations); - } -/* - void test_masterTest() - { - qDebug() << "#####################"; - MMC->m_version.build = 1; - MMC->m_version.channel = "develop"; - auto channels = - QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/channels.json")); - auto root = QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/")); - qDebug() << "channels: " << channels; - qDebug() << "root: " << root; - MMC->updateChecker()->setChannelListUrl(channels.toString()); - MMC->updateChecker()->setCurrentChannel("develop"); - - DownloadUpdateTask task(root.toString(), 2); - - QSignalSpy succeededSpy(&task, SIGNAL(succeeded())); - - task.start(); - - QVERIFY(succeededSpy.wait()); - } -*/ - void test_OSXPathFixup() - { - QString path, pathOrig; - bool result; - // Proper OSX path - pathOrig = path = "MultiMC.app/Foo/Bar/Baz"; - qDebug() << "Proper OSX path: " << path; - result = DownloadUpdateTask::fixPathForOSX(path); - QCOMPARE(path, QString("Foo/Bar/Baz")); - QCOMPARE(result, true); - - // Bad OSX path - pathOrig = path = "translations/klingon.lol"; - qDebug() << "Bad OSX path: " << path; - result = DownloadUpdateTask::fixPathForOSX(path); - QCOMPARE(path, pathOrig); - QCOMPARE(result, false); - } -}; - -QTEST_GUILESS_MAIN(DownloadUpdateTaskTest) - -#include "tst_DownloadUpdateTask.moc" diff --git a/tests/tst_UpdateChecker.cpp b/tests/tst_UpdateChecker.cpp index 75c5dcdd..d212f79b 100644 --- a/tests/tst_UpdateChecker.cpp +++ b/tests/tst_UpdateChecker.cpp @@ -89,7 +89,7 @@ slots: QFETCH(bool, valid); QFETCH(QList<UpdateChecker::ChannelListEntry>, result); - UpdateChecker checker(channelUrl, 0); + UpdateChecker checker(channelUrl, channel, 0); QSignalSpy channelListLoadedSpy(&checker, SIGNAL(channelListLoaded())); QVERIFY(channelListLoadedSpy.isValid()); @@ -111,28 +111,15 @@ slots: QCOMPARE(checker.getChannelList(), result); } - void tst_UpdateChecking_data() - { - QTest::addColumn<QString>("channel"); - QTest::addColumn<QString>("channelUrl"); - QTest::addColumn<int>("currentBuild"); - QTest::addColumn<QList<QVariant> >("result"); - - QTest::newRow("valid channel") - << "develop" << findTestDataUrl("tests/data/channels.json") - << 2 - << (QList<QVariant>() << QString() << "1.0.3" << 3); - } void tst_UpdateChecking() { - QFETCH(QString, channel); - QFETCH(QString, channelUrl); - QFETCH(int, currentBuild); - QFETCH(QList<QVariant>, result); + QString channel = "develop"; + QString channelUrl = findTestDataUrl("tests/data/channels.json"); + int currentBuild = 2; - UpdateChecker checker(channelUrl, currentBuild); + UpdateChecker checker(channelUrl, channel, currentBuild); - QSignalSpy updateAvailableSpy(&checker, SIGNAL(updateAvailable(QString,QString,int))); + QSignalSpy updateAvailableSpy(&checker, SIGNAL(updateAvailable(GoUpdate::Status))); QVERIFY(updateAvailableSpy.isValid()); QSignalSpy channelListLoadedSpy(&checker, SIGNAL(channelListLoaded())); QVERIFY(channelListLoadedSpy.isValid()); @@ -142,13 +129,14 @@ slots: qDebug() << "CWD:" << QDir::current().absolutePath(); checker.m_channels[0].url = findTestDataUrl("tests/data/"); - checker.checkForUpdate(channel, false); QVERIFY(updateAvailableSpy.wait()); - QList<QVariant> res = result; - res[0] = checker.m_channels[0].url; - QCOMPARE(updateAvailableSpy.first(), res); + + auto status = updateAvailableSpy.first().first().value<GoUpdate::Status>(); + QCOMPARE(checker.m_channels[0].url, status.newRepoUrl); + QCOMPARE(3, status.newVersionId); + QCOMPARE(currentBuild, status.currentVersionId); } }; |