diff options
| author | Petr Mrázek <peterix@gmail.com> | 2015-02-08 17:56:14 +0100 |
|---|---|---|
| committer | Petr Mrázek <peterix@gmail.com> | 2015-04-12 20:57:17 +0200 |
| commit | 4730f54df7edf4775dfddf45f77c60edd86c32d9 (patch) | |
| tree | 22fe05326976cbdadf150c1cfe0710f375e34edf /logic | |
| parent | 7a71ecd8af0454e405b25080a4b266fc99306269 (diff) | |
| download | PrismLauncher-4730f54df7edf4775dfddf45f77c60edd86c32d9.tar.gz PrismLauncher-4730f54df7edf4775dfddf45f77c60edd86c32d9.tar.bz2 PrismLauncher-4730f54df7edf4775dfddf45f77c60edd86c32d9.zip | |
SCRATCH separate the generic updater logic from the application
Diffstat (limited to 'logic')
| -rw-r--r-- | logic/BaseProcess.cpp | 6 | ||||
| -rw-r--r-- | logic/BaseProcess.h | 9 | ||||
| -rw-r--r-- | logic/forge/ForgeMirrors.cpp | 2 | ||||
| -rw-r--r-- | logic/forge/ForgeXzDownload.cpp | 2 | ||||
| -rw-r--r-- | logic/minecraft/MinecraftProcess.cpp | 4 | ||||
| -rw-r--r-- | logic/net/ByteArrayDownload.cpp | 2 | ||||
| -rw-r--r-- | logic/net/CacheDownload.cpp | 2 | ||||
| -rw-r--r-- | logic/net/MD5EtagDownload.cpp | 2 | ||||
| -rw-r--r-- | logic/net/NetAction.h | 3 | ||||
| -rw-r--r-- | logic/net/NetJob.cpp | 4 | ||||
| -rw-r--r-- | logic/net/NetJob.h | 1 | ||||
| -rw-r--r-- | logic/screenshots/ImgurAlbumCreation.cpp | 2 | ||||
| -rw-r--r-- | logic/screenshots/ImgurUpload.cpp | 2 | ||||
| -rw-r--r-- | logic/updater/DownloadTask.cpp | 179 | ||||
| -rw-r--r-- | logic/updater/DownloadTask.h | 85 | ||||
| -rw-r--r-- | logic/updater/DownloadUpdateTask.cpp | 549 | ||||
| -rw-r--r-- | logic/updater/DownloadUpdateTask.h | 220 | ||||
| -rw-r--r-- | logic/updater/GoUpdate.cpp | 315 | ||||
| -rw-r--r-- | logic/updater/GoUpdate.h | 135 | ||||
| -rw-r--r-- | logic/updater/UpdateChecker.cpp | 25 | ||||
| -rw-r--r-- | logic/updater/UpdateChecker.h | 12 |
21 files changed, 767 insertions, 794 deletions
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) |
