From 8e44ab2338f4ca63d58de4b3329c384df9d6c053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 19 Mar 2018 02:36:12 +0100 Subject: NOISSUE redo new instance dialog --- api/logic/modplatform/ftb/FtbPackDownloader.cpp | 84 ++++----------------- api/logic/modplatform/ftb/FtbPackDownloader.h | 49 +++--------- api/logic/modplatform/ftb/FtbPackFetchTask.cpp | 2 + api/logic/modplatform/ftb/FtbPackFetchTask.h | 2 +- api/logic/modplatform/ftb/FtbPackInstallTask.cpp | 96 +++++++++++++++--------- api/logic/modplatform/ftb/FtbPackInstallTask.h | 42 +++++------ api/logic/modplatform/ftb/PackHelpers.h | 4 +- 7 files changed, 111 insertions(+), 168 deletions(-) (limited to 'api/logic/modplatform/ftb') diff --git a/api/logic/modplatform/ftb/FtbPackDownloader.cpp b/api/logic/modplatform/ftb/FtbPackDownloader.cpp index caadd4ae..3e274c9e 100644 --- a/api/logic/modplatform/ftb/FtbPackDownloader.cpp +++ b/api/logic/modplatform/ftb/FtbPackDownloader.cpp @@ -3,36 +3,25 @@ #include "FtbPackFetchTask.h" #include "Env.h" -FtbPackDownloader::FtbPackDownloader() { +FtbPackDownloader::FtbPackDownloader() +{ done = false; fetching = false; } -FtbPackDownloader::~FtbPackDownloader(){ -} - -bool FtbPackDownloader::isValidPackSelected(){ - FtbModpack dummy; - dummy.name = "__INVALID__"; - - FtbModpack other = fetchedPacks.value(selected.name, dummy); - if(other.name == "__INVALID__") { - return false; - } - - return other.oldVersions.contains(selectedVersion) && !other.broken; -} - -QString FtbPackDownloader::getSuggestedInstanceName() { - return selected.name; +FtbPackDownloader::~FtbPackDownloader() +{ } -FtbModpackList FtbPackDownloader::getModpacks() { +FtbModpackList FtbPackDownloader::getModpacks() +{ return static_cast(fetchedPacks.values()); } -void FtbPackDownloader::fetchModpacks(bool force = false){ - if(fetching || (!force && done)) { +void FtbPackDownloader::fetchModpacks(bool force = false) +{ + if(fetching || (!force && done)) + { qDebug() << "Skipping modpack refetch because done or already fetching [done =>" << done << "| fetching =>" << fetching << "]"; return; } @@ -46,8 +35,10 @@ void FtbPackDownloader::fetchModpacks(bool force = false){ } -void FtbPackDownloader::fetchSuccess(FtbModpackList modpacks) { - for(int i = 0; i < modpacks.size(); i++) { +void FtbPackDownloader::fetchSuccess(FtbModpackList modpacks) +{ + for(int i = 0; i < modpacks.size(); i++) + { fetchedPacks.insert(modpacks.at(i).name, modpacks.at(i)); } @@ -57,53 +48,10 @@ void FtbPackDownloader::fetchSuccess(FtbModpackList modpacks) { fetchTask->deleteLater(); } -void FtbPackDownloader::fetchFailed(QString reason) { +void FtbPackDownloader::fetchFailed(QString reason) +{ qWarning() << "Failed to fetch FtbData" << reason; fetching = false; emit packFetchFailed(); fetchTask->deleteLater(); } - -void FtbPackDownloader::selectPack(FtbModpack modpack, QString version) { - selected = modpack; - selectedVersion = version; -} - -FtbModpack FtbPackDownloader::getSelectedPack() { - return selected; -} - -void FtbPackDownloader::downloadSelected(MetaEntryPtr cache) { - NetJob *job = new NetJob("Downlad FTB Pack"); - - cache->setStale(true); - QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1/%2/%3").arg(selected.dir, selectedVersion.replace(".", "_"), selected.file); - job->addNetAction(Net::Download::makeCached(url, cache)); - downloadPath = cache->getFullPath(); - - netJobContainer.reset(job); - - connect(job, &NetJob::succeeded, this, &FtbPackDownloader::_downloadSucceeded); - connect(job, &NetJob::failed, this, &FtbPackDownloader::_downloadFailed); - connect(job, &NetJob::progress, this, &FtbPackDownloader::_downloadProgress); - job->start(); -} - -void FtbPackDownloader::_downloadSucceeded() { - netJobContainer.reset(); - emit downloadSucceded(downloadPath); -} - -void FtbPackDownloader::_downloadProgress(qint64 current, qint64 total) { - emit downloadProgress(current, total); -} - -void FtbPackDownloader::_downloadFailed(QString reason) { - netJobContainer.reset(); - emit downloadFailed(reason); -} - -NetJobPtr FtbPackDownloader::getNetJob() -{ - return netJobContainer; -} diff --git a/api/logic/modplatform/ftb/FtbPackDownloader.h b/api/logic/modplatform/ftb/FtbPackDownloader.h index c5cc9bd2..cf7eb567 100644 --- a/api/logic/modplatform/ftb/FtbPackDownloader.h +++ b/api/logic/modplatform/ftb/FtbPackDownloader.h @@ -11,54 +11,29 @@ #pragma once -class FtbPackDownloader; -class MULTIMC_LOGIC_EXPORT FtbPackDownloader : public QObject { - +class MULTIMC_LOGIC_EXPORT FtbPackDownloader : public QObject +{ Q_OBJECT -private: - QMap fetchedPacks; - bool fetching = false; - bool done = false; - - FtbModpack selected; - QString selectedVersion; - QString downloadPath; - - FtbPackFetchTask *fetchTask = 0; - NetJobPtr netJobContainer; - - void _downloadSucceeded(); - void _downloadFailed(QString reason); - void _downloadProgress(qint64 current, qint64 total); - -private slots: - void fetchSuccess(FtbModpackList modlist); - void fetchFailed(QString reason); - public: FtbPackDownloader(); - ~FtbPackDownloader(); - - bool isValidPackSelected(); - void selectPack(FtbModpack modpack, QString version); - - FtbModpack getSelectedPack(); + virtual ~FtbPackDownloader(); void fetchModpacks(bool force); - void downloadSelected(MetaEntryPtr cache); - - QString getSuggestedInstanceName(); - FtbModpackList getModpacks(); - NetJobPtr getNetJob(); signals: void ready(); void packFetchFailed(); - void downloadSucceded(QString archivePath); - void downloadFailed(QString reason); - void downloadProgress(qint64 current, qint64 total); +private slots: + void fetchSuccess(FtbModpackList modlist); + void fetchFailed(QString reason); +private: + QMap fetchedPacks; + bool fetching = false; + bool done = false; + + FtbPackFetchTask *fetchTask = 0; }; diff --git a/api/logic/modplatform/ftb/FtbPackFetchTask.cpp b/api/logic/modplatform/ftb/FtbPackFetchTask.cpp index 9e151186..30253bb9 100644 --- a/api/logic/modplatform/ftb/FtbPackFetchTask.cpp +++ b/api/logic/modplatform/ftb/FtbPackFetchTask.cpp @@ -58,6 +58,8 @@ void FtbPackFetchTask::fileDownloadFinished(){ modpack.mods = element.attribute("mods"); modpack.image = element.attribute("image"); modpack.oldVersions = element.attribute("oldVersions").split(";"); + modpack.broken = false; + modpack.bugged = false; //remove empty if the xml is bugged for(QString curr : modpack.oldVersions) { diff --git a/api/logic/modplatform/ftb/FtbPackFetchTask.h b/api/logic/modplatform/ftb/FtbPackFetchTask.h index 3cfac4ed..a2e4b5ab 100644 --- a/api/logic/modplatform/ftb/FtbPackFetchTask.h +++ b/api/logic/modplatform/ftb/FtbPackFetchTask.h @@ -12,7 +12,7 @@ class MULTIMC_LOGIC_EXPORT FtbPackFetchTask : public QObject { public: FtbPackFetchTask(); - ~FtbPackFetchTask(); + virtual ~FtbPackFetchTask(); void fetch(); diff --git a/api/logic/modplatform/ftb/FtbPackInstallTask.cpp b/api/logic/modplatform/ftb/FtbPackInstallTask.cpp index 530f72ca..e3bb2340 100644 --- a/api/logic/modplatform/ftb/FtbPackInstallTask.cpp +++ b/api/logic/modplatform/ftb/FtbPackInstallTask.cpp @@ -9,52 +9,65 @@ #include "minecraft/ComponentList.h" #include "minecraft/GradleSpecifier.h" -FtbPackInstallTask::FtbPackInstallTask(FtbPackDownloader *downloader, SettingsObjectPtr settings, - const QString &stagingPath, const QString &instName, const QString &instIcon, const QString &instGroup) : - m_globalSettings(settings), m_stagingPath(stagingPath), m_instName(instName), m_instIcon(instIcon), m_instGroup(instGroup) +FtbPackInstallTask::FtbPackInstallTask(FtbModpack pack, QString version) { - m_downloader = downloader; + m_pack = pack; + m_version = version; } -void FtbPackInstallTask::executeTask() { +void FtbPackInstallTask::executeTask() +{ downloadPack(); } -void FtbPackInstallTask::downloadPack(){ - FtbModpack toInstall = m_downloader->getSelectedPack(); - setStatus(tr("Downloading zip for %1").arg(toInstall.name)); +void FtbPackInstallTask::downloadPack() +{ + setStatus(tr("Downloading zip for %1").arg(m_pack.name)); + + auto entry = ENV.metacache()->resolveEntry("general", "FTBPacks/" + m_pack.name); + NetJob *job = new NetJob("Downlad FTB Pack"); + + entry->setStale(true); + QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1/%2/%3").arg(m_pack.dir, m_version.replace(".", "_"), m_pack.file); + job->addNetAction(Net::Download::makeCached(url, entry)); + archivePath = entry->getFullPath(); - auto entry = ENV.metacache()->resolveEntry("general", "FTBPacks/" + toInstall.name); - m_downloader->downloadSelected(entry); + netJobContainer.reset(job); + connect(job, &NetJob::succeeded, this, &FtbPackInstallTask::onDownloadSucceeded); + connect(job, &NetJob::failed, this, &FtbPackInstallTask::onDownloadFailed); + connect(job, &NetJob::progress, this, &FtbPackInstallTask::onDownloadProgress); + job->start(); - connect(m_downloader, &FtbPackDownloader::downloadSucceded, this, &FtbPackInstallTask::onDownloadSucceeded); - connect(m_downloader, &FtbPackDownloader::downloadProgress, this, &FtbPackInstallTask::onDownloadProgress); - connect(m_downloader, &FtbPackDownloader::downloadFailed, this,&FtbPackInstallTask::onDownloadFailed); progress(1, 4); } -void FtbPackInstallTask::onDownloadSucceeded(QString archivePath){ +void FtbPackInstallTask::onDownloadSucceeded() +{ abortable = false; - unzip(archivePath); + unzip(); } -void FtbPackInstallTask::onDownloadFailed(QString reason) { +void FtbPackInstallTask::onDownloadFailed(QString reason) +{ emitFailed(reason); } -void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total){ +void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total) +{ abortable = true; progress(current, total * 4); - setStatus(tr("Downloading zip for %1 (%2\%)").arg(m_downloader->getSelectedPack().name).arg(current / 10)); + setStatus(tr("Downloading zip for %1 (%2\%)").arg(m_pack.name).arg(current / 10)); } -void FtbPackInstallTask::unzip(QString archivePath) { +void FtbPackInstallTask::unzip() +{ progress(2, 4); setStatus(tr("Extracting modpack")); QDir extractDir(m_stagingPath); m_packZip.reset(new QuaZip(archivePath)); - if(!m_packZip->open(QuaZip::mdUnzip)) { + if(!m_packZip->open(QuaZip::mdUnzip)) + { emitFailed(tr("Failed to open modpack file %1!").arg(archivePath)); return; } @@ -65,22 +78,26 @@ void FtbPackInstallTask::unzip(QString archivePath) { m_extractFutureWatcher.setFuture(m_extractFuture); } -void FtbPackInstallTask::onUnzipFinished() { +void FtbPackInstallTask::onUnzipFinished() +{ install(); } -void FtbPackInstallTask::onUnzipCanceled() { +void FtbPackInstallTask::onUnzipCanceled() +{ emitAborted(); } -void FtbPackInstallTask::install() { +void FtbPackInstallTask::install() +{ progress(3, 4); - FtbModpack toInstall = m_downloader->getSelectedPack(); setStatus(tr("Installing modpack")); QDir unzipMcDir(m_stagingPath + "/unzip/minecraft"); - if(unzipMcDir.exists()) { + if(unzipMcDir.exists()) + { //ok, found minecraft dir, move contents to instance dir - if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft")) { + if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft")) + { emitFailed(tr("Failed to move unzipped minecraft!")); return; } @@ -94,14 +111,15 @@ void FtbPackInstallTask::install() { MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath); auto components = instance.getComponentList(); components->buildingFromScratch(); - components->setComponentVersion("net.minecraft", toInstall.mcVersion, true); + components->setComponentVersion("net.minecraft", m_pack.mcVersion, true); bool fallback = true; //handle different versions QFile packJson(m_stagingPath + "/.minecraft/pack.json"); QDir jarmodDir = QDir(m_stagingPath + "/unzip/instMods"); - if(packJson.exists()) { + if(packJson.exists()) + { packJson.open(QIODevice::ReadOnly | QIODevice::Text); QJsonDocument doc = QJsonDocument::fromJson(packJson.readAll()); packJson.close(); @@ -109,15 +127,17 @@ void FtbPackInstallTask::install() { //we only care about the libs QJsonArray libs = doc.object().value("libraries").toArray(); - foreach (const QJsonValue &value, libs) { + foreach (const QJsonValue &value, libs) + { QString nameValue = value.toObject().value("name").toString(); - if(!nameValue.startsWith("net.minecraftforge")) { + if(!nameValue.startsWith("net.minecraftforge")) + { continue; } GradleSpecifier forgeVersion(nameValue); - components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(toInstall.mcVersion, "").replace("-", "")); + components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(m_pack.mcVersion, "").replace("-", "")); packJson.remove(); fallback = false; break; @@ -125,7 +145,8 @@ void FtbPackInstallTask::install() { } - if(jarmodDir.exists()) { + if(jarmodDir.exists()) + { qDebug() << "Found jarmods, installing..."; QStringList jarmods; @@ -142,7 +163,8 @@ void FtbPackInstallTask::install() { //just nuke unzip directory, it s not needed anymore FS::deletePath(m_stagingPath + "/unzip"); - if(fallback) { + if(fallback) + { //TODO: Some fallback mechanism... or just keep failing! emitFailed(tr("No installation method found!")); return; @@ -154,7 +176,8 @@ void FtbPackInstallTask::install() { instance.init(); instance.setName(m_instName); - if(m_instIcon == "default") { + if(m_instIcon == "default") + { m_instIcon = "ftb_logo"; } instance.setIconKey(m_instIcon); @@ -166,8 +189,9 @@ void FtbPackInstallTask::install() { bool FtbPackInstallTask::abort() { - if(abortable) { - return m_downloader->getNetJob()->abort(); + if(abortable) + { + return netJobContainer->abort(); } return false; } diff --git a/api/logic/modplatform/ftb/FtbPackInstallTask.h b/api/logic/modplatform/ftb/FtbPackInstallTask.h index 7d6e5276..64f4809d 100644 --- a/api/logic/modplatform/ftb/FtbPackInstallTask.h +++ b/api/logic/modplatform/ftb/FtbPackInstallTask.h @@ -1,5 +1,5 @@ #pragma once -#include "tasks/Task.h" +#include "InstanceTask.h" #include "modplatform/ftb/FtbPackDownloader.h" #include "BaseInstanceProvider.h" #include "net/NetJob.h" @@ -9,47 +9,41 @@ #include "meta/Version.h" #include "meta/VersionList.h" -class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public Task { +class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask { Q_OBJECT public: - explicit FtbPackInstallTask(FtbPackDownloader *downloader, SettingsObjectPtr settings, const QString &stagingPath, const QString &instName, - const QString &instIcon, const QString &instGroup); + explicit FtbPackInstallTask(FtbModpack pack, QString version); + virtual ~FtbPackInstallTask(){} + bool abort() override; protected: //! Entry point for tasks. virtual void executeTask() override; -private: /* data */ - SettingsObjectPtr m_globalSettings; - QString m_stagingPath; - QString m_instName; - QString m_instIcon; - QString m_instGroup; - NetJobPtr m_netJobPtr; - - FtbPackDownloader *m_downloader; - - std::unique_ptr m_packZip; - QFuture m_extractFuture; - QFutureWatcher m_extractFutureWatcher; - +private: void downloadPack(); - void unzip(QString archivePath); + void unzip(); void install(); - bool moveRecursively(QString source, QString dest); - - bool abortable = false; - private slots: - void onDownloadSucceeded(QString archivePath); + void onDownloadSucceeded(); void onDownloadFailed(QString reason); void onDownloadProgress(qint64 current, qint64 total); void onUnzipFinished(); void onUnzipCanceled(); +private: /* data */ + bool abortable = false; + std::unique_ptr m_packZip; + QFuture m_extractFuture; + QFutureWatcher m_extractFutureWatcher; + NetJobPtr netJobContainer; + QString archivePath; + + FtbModpack m_pack; + QString m_version; }; diff --git a/api/logic/modplatform/ftb/PackHelpers.h b/api/logic/modplatform/ftb/PackHelpers.h index f761ed6b..b184ed33 100644 --- a/api/logic/modplatform/ftb/PackHelpers.h +++ b/api/logic/modplatform/ftb/PackHelpers.h @@ -17,8 +17,8 @@ struct FtbModpack { QString dir; QString file; //<- Url in the xml, but doesn't make much sense - bool bugged = false; - bool broken = false; + bool bugged = true; + bool broken = true; }; //We need it for the proxy model Q_DECLARE_METATYPE(FtbModpack) -- cgit v1.2.3