From 47ed2f48d4a118876263f37b9fe2ab8911c2a8fe Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sun, 3 Nov 2019 23:48:12 +0100 Subject: NOISSUE put legacy FTB support in a namespace, fix its base URL --- api/logic/CMakeLists.txt | 17 +- api/logic/modplatform/ftb/FtbPackFetchTask.cpp | 168 ---------------- api/logic/modplatform/ftb/FtbPackFetchTask.h | 40 ---- api/logic/modplatform/ftb/FtbPackInstallTask.cpp | 207 -------------------- api/logic/modplatform/ftb/FtbPackInstallTask.h | 48 ----- .../modplatform/ftb/FtbPrivatePackManager.cpp | 37 ---- api/logic/modplatform/ftb/FtbPrivatePackManager.h | 40 ---- api/logic/modplatform/ftb/PackHelpers.h | 41 ---- api/logic/modplatform/legacy_ftb/PackFetchTask.cpp | 172 +++++++++++++++++ api/logic/modplatform/legacy_ftb/PackFetchTask.h | 44 +++++ api/logic/modplatform/legacy_ftb/PackHelpers.h | 45 +++++ .../modplatform/legacy_ftb/PackInstallTask.cpp | 213 +++++++++++++++++++++ api/logic/modplatform/legacy_ftb/PackInstallTask.h | 52 +++++ .../modplatform/legacy_ftb/PrivatePackManager.cpp | 41 ++++ .../modplatform/legacy_ftb/PrivatePackManager.h | 44 +++++ api/logic/net/URLConstants.h | 3 +- 16 files changed, 621 insertions(+), 591 deletions(-) delete mode 100644 api/logic/modplatform/ftb/FtbPackFetchTask.cpp delete mode 100644 api/logic/modplatform/ftb/FtbPackFetchTask.h delete mode 100644 api/logic/modplatform/ftb/FtbPackInstallTask.cpp delete mode 100644 api/logic/modplatform/ftb/FtbPackInstallTask.h delete mode 100644 api/logic/modplatform/ftb/FtbPrivatePackManager.cpp delete mode 100644 api/logic/modplatform/ftb/FtbPrivatePackManager.h delete mode 100644 api/logic/modplatform/ftb/PackHelpers.h create mode 100644 api/logic/modplatform/legacy_ftb/PackFetchTask.cpp create mode 100644 api/logic/modplatform/legacy_ftb/PackFetchTask.h create mode 100644 api/logic/modplatform/legacy_ftb/PackHelpers.h create mode 100644 api/logic/modplatform/legacy_ftb/PackInstallTask.cpp create mode 100644 api/logic/modplatform/legacy_ftb/PackInstallTask.h create mode 100644 api/logic/modplatform/legacy_ftb/PrivatePackManager.cpp create mode 100644 api/logic/modplatform/legacy_ftb/PrivatePackManager.h (limited to 'api') diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index db1a7dec..c6b3e422 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -439,15 +439,14 @@ set(META_SOURCES ) set(FTB_SOURCES - modplatform/ftb/FtbPackFetchTask.h - modplatform/ftb/FtbPackFetchTask.cpp - modplatform/ftb/FtbPackInstallTask.h - modplatform/ftb/FtbPackInstallTask.cpp - - modplatform/ftb/FtbPrivatePackManager.h - modplatform/ftb/FtbPrivatePackManager.cpp - - modplatform/ftb/PackHelpers.h + modplatform/legacy_ftb/PackFetchTask.h + modplatform/legacy_ftb/PackFetchTask.cpp + modplatform/legacy_ftb/PackInstallTask.h + modplatform/legacy_ftb/PackInstallTask.cpp + modplatform/legacy_ftb/PrivatePackManager.h + modplatform/legacy_ftb/PrivatePackManager.cpp + + modplatform/legacy_ftb/PackHelpers.h ) set(FLAME_SOURCES diff --git a/api/logic/modplatform/ftb/FtbPackFetchTask.cpp b/api/logic/modplatform/ftb/FtbPackFetchTask.cpp deleted file mode 100644 index fe3f3fac..00000000 --- a/api/logic/modplatform/ftb/FtbPackFetchTask.cpp +++ /dev/null @@ -1,168 +0,0 @@ -#include "FtbPackFetchTask.h" -#include -#include "FtbPrivatePackManager.h" - -#include "net/URLConstants.h" - -void FtbPackFetchTask::fetch() -{ - publicPacks.clear(); - thirdPartyPacks.clear(); - - NetJob *netJob = new NetJob("FtbModpackFetch"); - - QUrl publicPacksUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/modpacks.xml"); - qDebug() << "Downloading public version info from" << publicPacksUrl.toString(); - netJob->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData)); - - QUrl thirdPartyUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/thirdparty.xml"); - qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString(); - netJob->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData)); - - QObject::connect(netJob, &NetJob::succeeded, this, &FtbPackFetchTask::fileDownloadFinished); - QObject::connect(netJob, &NetJob::failed, this, &FtbPackFetchTask::fileDownloadFailed); - - jobPtr.reset(netJob); - netJob->start(); -} - -void FtbPackFetchTask::fetchPrivate(const QStringList & toFetch) -{ - QString privatePackBaseUrl = URLConstants::FTB_CDN_BASE_URL + "static/%1.xml"; - - for (auto &packCode: toFetch) - { - QByteArray *data = new QByteArray(); - NetJob *job = new NetJob("Fetching private pack"); - job->addNetAction(Net::Download::makeByteArray(privatePackBaseUrl.arg(packCode), data)); - - QObject::connect(job, &NetJob::succeeded, this, [this, job, data, packCode] - { - FtbModpackList packs; - parseAndAddPacks(*data, FtbPackType::Private, packs); - foreach(FtbModpack currentPack, packs) - { - currentPack.packCode = packCode; - emit privateFileDownloadFinished(currentPack); - } - - job->deleteLater(); - - data->clear(); - delete data; - }); - - QObject::connect(job, &NetJob::failed, this, [this, job, packCode, data](QString reason) - { - emit privateFileDownloadFailed(reason, packCode); - job->deleteLater(); - - data->clear(); - delete data; - }); - - job->start(); - } -} - -void FtbPackFetchTask::fileDownloadFinished() -{ - jobPtr.reset(); - - QStringList failedLists; - - if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks)) - { - failedLists.append(tr("Public Packs")); - } - - if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks)) - { - failedLists.append(tr("Third Party Packs")); - } - - if(failedLists.size() > 0) - { - emit failed(tr("Failed to download some pack lists: %1").arg(failedLists.join("\n- "))); - } - else - { - emit finished(publicPacks, thirdPartyPacks); - } -} - -bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType, FtbModpackList &list) -{ - QDomDocument doc; - - QString errorMsg = "Unknown error."; - int errorLine = -1; - int errorCol = -1; - - if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)) - { - auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:3d!").arg(errorMsg, errorLine, errorCol); - qWarning() << fullErrMsg; - data.clear(); - return false; - } - - QDomNodeList nodes = doc.elementsByTagName("modpack"); - for(int i = 0; i < nodes.length(); i++) - { - QDomElement element = nodes.at(i).toElement(); - - FtbModpack modpack; - modpack.name = element.attribute("name"); - modpack.currentVersion = element.attribute("version"); - modpack.mcVersion = element.attribute("mcVersion"); - modpack.description = element.attribute("description"); - modpack.mods = element.attribute("mods"); - modpack.logo = element.attribute("logo"); - modpack.oldVersions = element.attribute("oldVersions").split(";"); - modpack.broken = false; - modpack.bugged = false; - - //remove empty if the xml is bugged - for(QString curr : modpack.oldVersions) - { - if(curr.isNull() || curr.isEmpty()) - { - modpack.oldVersions.removeAll(curr); - modpack.bugged = true; - qWarning() << "Removed some empty versions from" << modpack.name; - } - } - - if(modpack.oldVersions.size() < 1) - { - if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty()) - { - modpack.oldVersions.append(modpack.currentVersion); - qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")"; - } - else - { - modpack.broken = true; - qWarning() << "Broken pack:" << modpack.name << " => No valid version!"; - } - } - - modpack.author = element.attribute("author"); - - modpack.dir = element.attribute("dir"); - modpack.file = element.attribute("url"); - - modpack.type = packType; - - list.append(modpack); - } - - return true; -} - -void FtbPackFetchTask::fileDownloadFailed(QString reason) -{ - qWarning() << "Fetching FtbPacks failed:" << reason; - emit failed(reason); -} diff --git a/api/logic/modplatform/ftb/FtbPackFetchTask.h b/api/logic/modplatform/ftb/FtbPackFetchTask.h deleted file mode 100644 index f955fe83..00000000 --- a/api/logic/modplatform/ftb/FtbPackFetchTask.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include "net/NetJob.h" -#include -#include -#include -#include "PackHelpers.h" - -class MULTIMC_LOGIC_EXPORT FtbPackFetchTask : public QObject { - - Q_OBJECT - -public: - FtbPackFetchTask() = default; - virtual ~FtbPackFetchTask() = default; - - void fetch(); - void fetchPrivate(const QStringList &toFetch); - -private: - NetJobPtr jobPtr; - - QByteArray publicModpacksXmlFileData; - QByteArray thirdPartyModpacksXmlFileData; - - bool parseAndAddPacks(QByteArray &data, FtbPackType packType, FtbModpackList &list); - FtbModpackList publicPacks; - FtbModpackList thirdPartyPacks; - -protected slots: - void fileDownloadFinished(); - void fileDownloadFailed(QString reason); - -signals: - void finished(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks); - void failed(QString reason); - - void privateFileDownloadFinished(FtbModpack modpack); - void privateFileDownloadFailed(QString reason, QString packCode); -}; diff --git a/api/logic/modplatform/ftb/FtbPackInstallTask.cpp b/api/logic/modplatform/ftb/FtbPackInstallTask.cpp deleted file mode 100644 index 4962bcac..00000000 --- a/api/logic/modplatform/ftb/FtbPackInstallTask.cpp +++ /dev/null @@ -1,207 +0,0 @@ -#include "FtbPackInstallTask.h" -#include "Env.h" -#include "MMCZip.h" -#include "QtConcurrent" -#include "BaseInstance.h" -#include "FileSystem.h" -#include "settings/INISettingsObject.h" -#include "minecraft/MinecraftInstance.h" -#include "minecraft/ComponentList.h" -#include "minecraft/GradleSpecifier.h" - -#include "net/URLConstants.h" - -FtbPackInstallTask::FtbPackInstallTask(FtbModpack pack, QString version) -{ - m_pack = pack; - m_version = version; -} - -void FtbPackInstallTask::executeTask() -{ - downloadPack(); -} - -void FtbPackInstallTask::downloadPack() -{ - setStatus(tr("Downloading zip for %1").arg(m_pack.name)); - - auto packoffset = QString("%1/%2/%3").arg(m_pack.dir, m_version.replace(".", "_"), m_pack.file); - auto entry = ENV.metacache()->resolveEntry("FTBPacks", packoffset); - NetJob *job = new NetJob("Download FTB Pack"); - - entry->setStale(true); - QString url; - if(m_pack.type == FtbPackType::Private) - { - url = QString(URLConstants::FTB_CDN_BASE_URL + "privatepacks/%1").arg(packoffset); - } - else - { - url = QString(URLConstants::FTB_CDN_BASE_URL + "modpacks/%1").arg(packoffset); - } - job->addNetAction(Net::Download::makeCached(url, entry)); - archivePath = entry->getFullPath(); - - 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(); - - progress(1, 4); -} - -void FtbPackInstallTask::onDownloadSucceeded() -{ - abortable = false; - unzip(); -} - -void FtbPackInstallTask::onDownloadFailed(QString reason) -{ - abortable = false; - emitFailed(reason); -} - -void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total) -{ - abortable = true; - progress(current, total * 4); - setStatus(tr("Downloading zip for %1 (%2%)").arg(m_pack.name).arg(current / 10)); -} - -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)) - { - emitFailed(tr("Failed to open modpack file %1!").arg(archivePath)); - return; - } - - m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, archivePath, extractDir.absolutePath() + "/unzip"); - connect(&m_extractFutureWatcher, &QFutureWatcher::finished, this, &FtbPackInstallTask::onUnzipFinished); - connect(&m_extractFutureWatcher, &QFutureWatcher::canceled, this, &FtbPackInstallTask::onUnzipCanceled); - m_extractFutureWatcher.setFuture(m_extractFuture); -} - -void FtbPackInstallTask::onUnzipFinished() -{ - install(); -} - -void FtbPackInstallTask::onUnzipCanceled() -{ - emitAborted(); -} - -void FtbPackInstallTask::install() -{ - progress(3, 4); - setStatus(tr("Installing modpack")); - QDir unzipMcDir(m_stagingPath + "/unzip/minecraft"); - if(unzipMcDir.exists()) - { - //ok, found minecraft dir, move contents to instance dir - if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft")) - { - emitFailed(tr("Failed to move unzipped minecraft!")); - return; - } - } - - QString instanceConfigPath = FS::PathCombine(m_stagingPath, "instance.cfg"); - auto instanceSettings = std::make_shared(instanceConfigPath); - instanceSettings->registerSetting("InstanceType", "Legacy"); - instanceSettings->set("InstanceType", "OneSix"); - - MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath); - auto components = instance.getComponentList(); - components->buildingFromScratch(); - 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()) - { - packJson.open(QIODevice::ReadOnly | QIODevice::Text); - QJsonDocument doc = QJsonDocument::fromJson(packJson.readAll()); - packJson.close(); - - //we only care about the libs - QJsonArray libs = doc.object().value("libraries").toArray(); - - foreach (const QJsonValue &value, libs) - { - QString nameValue = value.toObject().value("name").toString(); - if(!nameValue.startsWith("net.minecraftforge")) - { - continue; - } - - GradleSpecifier forgeVersion(nameValue); - - components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(m_pack.mcVersion, "").replace("-", "")); - packJson.remove(); - fallback = false; - break; - } - - } - - if(jarmodDir.exists()) - { - qDebug() << "Found jarmods, installing..."; - - QStringList jarmods; - for (auto info: jarmodDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files)) - { - qDebug() << "Jarmod:" << info.fileName(); - jarmods.push_back(info.absoluteFilePath()); - } - - components->installJarMods(jarmods); - fallback = false; - } - - //just nuke unzip directory, it s not needed anymore - FS::deletePath(m_stagingPath + "/unzip"); - - if(fallback) - { - //TODO: Some fallback mechanism... or just keep failing! - emitFailed(tr("No installation method found!")); - return; - } - - components->saveNow(); - - progress(4, 4); - - instance.setName(m_instName); - if(m_instIcon == "default") - { - m_instIcon = "ftb_logo"; - } - instance.setIconKey(m_instIcon); - instanceSettings->resumeSave(); - - emitSucceeded(); -} - -bool FtbPackInstallTask::abort() -{ - if(abortable) - { - return netJobContainer->abort(); - } - return false; -} diff --git a/api/logic/modplatform/ftb/FtbPackInstallTask.h b/api/logic/modplatform/ftb/FtbPackInstallTask.h deleted file mode 100644 index 3319025e..00000000 --- a/api/logic/modplatform/ftb/FtbPackInstallTask.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once -#include "InstanceTask.h" -#include "net/NetJob.h" -#include "quazip.h" -#include "quazipdir.h" -#include "meta/Index.h" -#include "meta/Version.h" -#include "meta/VersionList.h" -#include "modplatform/ftb/PackHelpers.h" - -class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask -{ - Q_OBJECT - -public: - explicit FtbPackInstallTask(FtbModpack pack, QString version); - virtual ~FtbPackInstallTask(){} - - bool abort() override; - -protected: - //! Entry point for tasks. - virtual void executeTask() override; - -private: - void downloadPack(); - void unzip(); - void install(); - -private slots: - 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/FtbPrivatePackManager.cpp b/api/logic/modplatform/ftb/FtbPrivatePackManager.cpp deleted file mode 100644 index c3477cec..00000000 --- a/api/logic/modplatform/ftb/FtbPrivatePackManager.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "FtbPrivatePackManager.h" - -#include - -#include "FileSystem.h" - -void FtbPrivatePackManager::load() -{ - try - { - currentPacks = QString::fromUtf8(FS::read(m_filename)).split('\n', QString::SkipEmptyParts).toSet(); - dirty = false; - } - catch(...) - { - currentPacks = {}; - qWarning() << "Failed to read third party FTB pack codes from" << m_filename; - } -} - -void FtbPrivatePackManager::save() const -{ - if(!dirty) - { - return; - } - try - { - QStringList list = currentPacks.toList(); - FS::write(m_filename, list.join('\n').toUtf8()); - dirty = false; - } - catch(...) - { - qWarning() << "Failed to write third party FTB pack codes to" << m_filename; - } -} diff --git a/api/logic/modplatform/ftb/FtbPrivatePackManager.h b/api/logic/modplatform/ftb/FtbPrivatePackManager.h deleted file mode 100644 index 388224d6..00000000 --- a/api/logic/modplatform/ftb/FtbPrivatePackManager.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include "multimc_logic_export.h" - -class MULTIMC_LOGIC_EXPORT FtbPrivatePackManager -{ -public: - ~FtbPrivatePackManager() - { - save(); - } - void load(); - void save() const; - bool empty() const - { - return currentPacks.empty(); - } - const QSet &getCurrentPackCodes() const - { - return currentPacks; - } - void add(const QString &code) - { - currentPacks.insert(code); - dirty = true; - } - void remove(const QString &code) - { - currentPacks.remove(code); - dirty = true; - } - -private: - QSet currentPacks; - QString m_filename = "private_packs.txt"; - mutable bool dirty = false; -}; diff --git a/api/logic/modplatform/ftb/PackHelpers.h b/api/logic/modplatform/ftb/PackHelpers.h deleted file mode 100644 index 4306caee..00000000 --- a/api/logic/modplatform/ftb/PackHelpers.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -//Header for structs etc... -enum class FtbPackType -{ - Public, - ThirdParty, - Private -}; - -struct FtbModpack -{ - QString name; - QString description; - QString author; - QStringList oldVersions; - QString currentVersion; - QString mcVersion; - QString mods; - QString logo; - - //Technical data - QString dir; - QString file; //<- Url in the xml, but doesn't make much sense - - bool bugged = false; - bool broken = false; - - FtbPackType type; - QString packCode; -}; - -//We need it for the proxy model -Q_DECLARE_METATYPE(FtbModpack) - -typedef QList FtbModpackList; diff --git a/api/logic/modplatform/legacy_ftb/PackFetchTask.cpp b/api/logic/modplatform/legacy_ftb/PackFetchTask.cpp new file mode 100644 index 00000000..43c1e6f8 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PackFetchTask.cpp @@ -0,0 +1,172 @@ +#include "PackFetchTask.h" +#include "PrivatePackManager.h" + +#include +#include "net/URLConstants.h" + +namespace LegacyFTB { + +void PackFetchTask::fetch() +{ + publicPacks.clear(); + thirdPartyPacks.clear(); + + NetJob *netJob = new NetJob("LegacyFTB::ModpackFetch"); + + QUrl publicPacksUrl = QUrl(URLConstants::LEGACY_FTB_CDN_BASE_URL + "static/modpacks.xml"); + qDebug() << "Downloading public version info from" << publicPacksUrl.toString(); + netJob->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData)); + + QUrl thirdPartyUrl = QUrl(URLConstants::LEGACY_FTB_CDN_BASE_URL + "static/thirdparty.xml"); + qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString(); + netJob->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData)); + + QObject::connect(netJob, &NetJob::succeeded, this, &PackFetchTask::fileDownloadFinished); + QObject::connect(netJob, &NetJob::failed, this, &PackFetchTask::fileDownloadFailed); + + jobPtr.reset(netJob); + netJob->start(); +} + +void PackFetchTask::fetchPrivate(const QStringList & toFetch) +{ + QString privatePackBaseUrl = URLConstants::LEGACY_FTB_CDN_BASE_URL + "static/%1.xml"; + + for (auto &packCode: toFetch) + { + QByteArray *data = new QByteArray(); + NetJob *job = new NetJob("Fetching private pack"); + job->addNetAction(Net::Download::makeByteArray(privatePackBaseUrl.arg(packCode), data)); + + QObject::connect(job, &NetJob::succeeded, this, [this, job, data, packCode] + { + ModpackList packs; + parseAndAddPacks(*data, PackType::Private, packs); + foreach(Modpack currentPack, packs) + { + currentPack.packCode = packCode; + emit privateFileDownloadFinished(currentPack); + } + + job->deleteLater(); + + data->clear(); + delete data; + }); + + QObject::connect(job, &NetJob::failed, this, [this, job, packCode, data](QString reason) + { + emit privateFileDownloadFailed(reason, packCode); + job->deleteLater(); + + data->clear(); + delete data; + }); + + job->start(); + } +} + +void PackFetchTask::fileDownloadFinished() +{ + jobPtr.reset(); + + QStringList failedLists; + + if(!parseAndAddPacks(publicModpacksXmlFileData, PackType::Public, publicPacks)) + { + failedLists.append(tr("Public Packs")); + } + + if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, PackType::ThirdParty, thirdPartyPacks)) + { + failedLists.append(tr("Third Party Packs")); + } + + if(failedLists.size() > 0) + { + emit failed(tr("Failed to download some pack lists: %1").arg(failedLists.join("\n- "))); + } + else + { + emit finished(publicPacks, thirdPartyPacks); + } +} + +bool PackFetchTask::parseAndAddPacks(QByteArray &data, PackType packType, ModpackList &list) +{ + QDomDocument doc; + + QString errorMsg = "Unknown error."; + int errorLine = -1; + int errorCol = -1; + + if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)) + { + auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:3d!").arg(errorMsg, errorLine, errorCol); + qWarning() << fullErrMsg; + data.clear(); + return false; + } + + QDomNodeList nodes = doc.elementsByTagName("modpack"); + for(int i = 0; i < nodes.length(); i++) + { + QDomElement element = nodes.at(i).toElement(); + + Modpack modpack; + modpack.name = element.attribute("name"); + modpack.currentVersion = element.attribute("version"); + modpack.mcVersion = element.attribute("mcVersion"); + modpack.description = element.attribute("description"); + modpack.mods = element.attribute("mods"); + modpack.logo = element.attribute("logo"); + modpack.oldVersions = element.attribute("oldVersions").split(";"); + modpack.broken = false; + modpack.bugged = false; + + //remove empty if the xml is bugged + for(QString curr : modpack.oldVersions) + { + if(curr.isNull() || curr.isEmpty()) + { + modpack.oldVersions.removeAll(curr); + modpack.bugged = true; + qWarning() << "Removed some empty versions from" << modpack.name; + } + } + + if(modpack.oldVersions.size() < 1) + { + if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty()) + { + modpack.oldVersions.append(modpack.currentVersion); + qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")"; + } + else + { + modpack.broken = true; + qWarning() << "Broken pack:" << modpack.name << " => No valid version!"; + } + } + + modpack.author = element.attribute("author"); + + modpack.dir = element.attribute("dir"); + modpack.file = element.attribute("url"); + + modpack.type = packType; + + list.append(modpack); + } + + return true; +} + +void PackFetchTask::fileDownloadFailed(QString reason) +{ + qWarning() << "Fetching FTBPacks failed:" << reason; + emit failed(reason); +} + +} diff --git a/api/logic/modplatform/legacy_ftb/PackFetchTask.h b/api/logic/modplatform/legacy_ftb/PackFetchTask.h new file mode 100644 index 00000000..4a8469b1 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PackFetchTask.h @@ -0,0 +1,44 @@ +#pragma once + +#include "net/NetJob.h" +#include +#include +#include +#include "PackHelpers.h" + +namespace LegacyFTB { + +class MULTIMC_LOGIC_EXPORT PackFetchTask : public QObject { + + Q_OBJECT + +public: + PackFetchTask() = default; + virtual ~PackFetchTask() = default; + + void fetch(); + void fetchPrivate(const QStringList &toFetch); + +private: + NetJobPtr jobPtr; + + QByteArray publicModpacksXmlFileData; + QByteArray thirdPartyModpacksXmlFileData; + + bool parseAndAddPacks(QByteArray &data, PackType packType, ModpackList &list); + ModpackList publicPacks; + ModpackList thirdPartyPacks; + +protected slots: + void fileDownloadFinished(); + void fileDownloadFailed(QString reason); + +signals: + void finished(ModpackList publicPacks, ModpackList thirdPartyPacks); + void failed(QString reason); + + void privateFileDownloadFinished(Modpack modpack); + void privateFileDownloadFailed(QString reason, QString packCode); +}; + +} diff --git a/api/logic/modplatform/legacy_ftb/PackHelpers.h b/api/logic/modplatform/legacy_ftb/PackHelpers.h new file mode 100644 index 00000000..566210d0 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PackHelpers.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include + +namespace LegacyFTB { + +//Header for structs etc... +enum class PackType +{ + Public, + ThirdParty, + Private +}; + +struct Modpack +{ + QString name; + QString description; + QString author; + QStringList oldVersions; + QString currentVersion; + QString mcVersion; + QString mods; + QString logo; + + //Technical data + QString dir; + QString file; //<- Url in the xml, but doesn't make much sense + + bool bugged = false; + bool broken = false; + + PackType type; + QString packCode; +}; + +typedef QList ModpackList; + +} + +//We need it for the proxy model +Q_DECLARE_METATYPE(LegacyFTB::Modpack) diff --git a/api/logic/modplatform/legacy_ftb/PackInstallTask.cpp b/api/logic/modplatform/legacy_ftb/PackInstallTask.cpp new file mode 100644 index 00000000..ea7e2c0c --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PackInstallTask.cpp @@ -0,0 +1,213 @@ +#include "PackInstallTask.h" + +#include "Env.h" +#include "MMCZip.h" + +#include "BaseInstance.h" +#include "FileSystem.h" +#include "settings/INISettingsObject.h" +#include "minecraft/MinecraftInstance.h" +#include "minecraft/ComponentList.h" +#include "minecraft/GradleSpecifier.h" +#include "net/URLConstants.h" + +#include + +namespace LegacyFTB { + +PackInstallTask::PackInstallTask(Modpack pack, QString version) +{ + m_pack = pack; + m_version = version; +} + +void PackInstallTask::executeTask() +{ + downloadPack(); +} + +void PackInstallTask::downloadPack() +{ + setStatus(tr("Downloading zip for %1").arg(m_pack.name)); + + auto packoffset = QString("%1/%2/%3").arg(m_pack.dir, m_version.replace(".", "_"), m_pack.file); + auto entry = ENV.metacache()->resolveEntry("FTBPacks", packoffset); + NetJob *job = new NetJob("Download FTB Pack"); + + entry->setStale(true); + QString url; + if(m_pack.type == PackType::Private) + { + url = QString(URLConstants::LEGACY_FTB_CDN_BASE_URL + "privatepacks/%1").arg(packoffset); + } + else + { + url = QString(URLConstants::LEGACY_FTB_CDN_BASE_URL + "modpacks/%1").arg(packoffset); + } + job->addNetAction(Net::Download::makeCached(url, entry)); + archivePath = entry->getFullPath(); + + netJobContainer.reset(job); + connect(job, &NetJob::succeeded, this, &PackInstallTask::onDownloadSucceeded); + connect(job, &NetJob::failed, this, &PackInstallTask::onDownloadFailed); + connect(job, &NetJob::progress, this, &PackInstallTask::onDownloadProgress); + job->start(); + + progress(1, 4); +} + +void PackInstallTask::onDownloadSucceeded() +{ + abortable = false; + unzip(); +} + +void PackInstallTask::onDownloadFailed(QString reason) +{ + abortable = false; + emitFailed(reason); +} + +void PackInstallTask::onDownloadProgress(qint64 current, qint64 total) +{ + abortable = true; + progress(current, total * 4); + setStatus(tr("Downloading zip for %1 (%2%)").arg(m_pack.name).arg(current / 10)); +} + +void PackInstallTask::unzip() +{ + progress(2, 4); + setStatus(tr("Extracting modpack")); + QDir extractDir(m_stagingPath); + + m_packZip.reset(new QuaZip(archivePath)); + if(!m_packZip->open(QuaZip::mdUnzip)) + { + emitFailed(tr("Failed to open modpack file %1!").arg(archivePath)); + return; + } + + m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, archivePath, extractDir.absolutePath() + "/unzip"); + connect(&m_extractFutureWatcher, &QFutureWatcher::finished, this, &PackInstallTask::onUnzipFinished); + connect(&m_extractFutureWatcher, &QFutureWatcher::canceled, this, &PackInstallTask::onUnzipCanceled); + m_extractFutureWatcher.setFuture(m_extractFuture); +} + +void PackInstallTask::onUnzipFinished() +{ + install(); +} + +void PackInstallTask::onUnzipCanceled() +{ + emitAborted(); +} + +void PackInstallTask::install() +{ + progress(3, 4); + setStatus(tr("Installing modpack")); + QDir unzipMcDir(m_stagingPath + "/unzip/minecraft"); + if(unzipMcDir.exists()) + { + //ok, found minecraft dir, move contents to instance dir + if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft")) + { + emitFailed(tr("Failed to move unzipped minecraft!")); + return; + } + } + + QString instanceConfigPath = FS::PathCombine(m_stagingPath, "instance.cfg"); + auto instanceSettings = std::make_shared(instanceConfigPath); + instanceSettings->registerSetting("InstanceType", "Legacy"); + instanceSettings->set("InstanceType", "OneSix"); + + MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath); + auto components = instance.getComponentList(); + components->buildingFromScratch(); + 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()) + { + packJson.open(QIODevice::ReadOnly | QIODevice::Text); + QJsonDocument doc = QJsonDocument::fromJson(packJson.readAll()); + packJson.close(); + + //we only care about the libs + QJsonArray libs = doc.object().value("libraries").toArray(); + + foreach (const QJsonValue &value, libs) + { + QString nameValue = value.toObject().value("name").toString(); + if(!nameValue.startsWith("net.minecraftforge")) + { + continue; + } + + GradleSpecifier forgeVersion(nameValue); + + components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(m_pack.mcVersion, "").replace("-", "")); + packJson.remove(); + fallback = false; + break; + } + + } + + if(jarmodDir.exists()) + { + qDebug() << "Found jarmods, installing..."; + + QStringList jarmods; + for (auto info: jarmodDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files)) + { + qDebug() << "Jarmod:" << info.fileName(); + jarmods.push_back(info.absoluteFilePath()); + } + + components->installJarMods(jarmods); + fallback = false; + } + + //just nuke unzip directory, it s not needed anymore + FS::deletePath(m_stagingPath + "/unzip"); + + if(fallback) + { + //TODO: Some fallback mechanism... or just keep failing! + emitFailed(tr("No installation method found!")); + return; + } + + components->saveNow(); + + progress(4, 4); + + instance.setName(m_instName); + if(m_instIcon == "default") + { + m_instIcon = "ftb_logo"; + } + instance.setIconKey(m_instIcon); + instanceSettings->resumeSave(); + + emitSucceeded(); +} + +bool PackInstallTask::abort() +{ + if(abortable) + { + return netJobContainer->abort(); + } + return false; +} + +} \ No newline at end of file diff --git a/api/logic/modplatform/legacy_ftb/PackInstallTask.h b/api/logic/modplatform/legacy_ftb/PackInstallTask.h new file mode 100644 index 00000000..1eec1880 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PackInstallTask.h @@ -0,0 +1,52 @@ +#pragma once +#include "InstanceTask.h" +#include "net/NetJob.h" +#include "quazip.h" +#include "quazipdir.h" +#include "meta/Index.h" +#include "meta/Version.h" +#include "meta/VersionList.h" +#include "PackHelpers.h" + +namespace LegacyFTB { + +class MULTIMC_LOGIC_EXPORT PackInstallTask : public InstanceTask +{ + Q_OBJECT + +public: + explicit PackInstallTask(Modpack pack, QString version); + virtual ~PackInstallTask(){} + + bool abort() override; + +protected: + //! Entry point for tasks. + virtual void executeTask() override; + +private: + void downloadPack(); + void unzip(); + void install(); + +private slots: + 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; + + Modpack m_pack; + QString m_version; +}; + +} diff --git a/api/logic/modplatform/legacy_ftb/PrivatePackManager.cpp b/api/logic/modplatform/legacy_ftb/PrivatePackManager.cpp new file mode 100644 index 00000000..501e6003 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PrivatePackManager.cpp @@ -0,0 +1,41 @@ +#include "PrivatePackManager.h" + +#include + +#include "FileSystem.h" + +namespace LegacyFTB { + +void PrivatePackManager::load() +{ + try + { + currentPacks = QString::fromUtf8(FS::read(m_filename)).split('\n', QString::SkipEmptyParts).toSet(); + dirty = false; + } + catch(...) + { + currentPacks = {}; + qWarning() << "Failed to read third party FTB pack codes from" << m_filename; + } +} + +void PrivatePackManager::save() const +{ + if(!dirty) + { + return; + } + try + { + QStringList list = currentPacks.toList(); + FS::write(m_filename, list.join('\n').toUtf8()); + dirty = false; + } + catch(...) + { + qWarning() << "Failed to write third party FTB pack codes to" << m_filename; + } +} + +} diff --git a/api/logic/modplatform/legacy_ftb/PrivatePackManager.h b/api/logic/modplatform/legacy_ftb/PrivatePackManager.h new file mode 100644 index 00000000..0232bac7 --- /dev/null +++ b/api/logic/modplatform/legacy_ftb/PrivatePackManager.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include "multimc_logic_export.h" + +namespace LegacyFTB { + +class MULTIMC_LOGIC_EXPORT PrivatePackManager +{ +public: + ~PrivatePackManager() + { + save(); + } + void load(); + void save() const; + bool empty() const + { + return currentPacks.empty(); + } + const QSet &getCurrentPackCodes() const + { + return currentPacks; + } + void add(const QString &code) + { + currentPacks.insert(code); + dirty = true; + } + void remove(const QString &code) + { + currentPacks.remove(code); + dirty = true; + } + +private: + QSet currentPacks; + QString m_filename = "private_packs.txt"; + mutable bool dirty = false; +}; + +} diff --git a/api/logic/net/URLConstants.h b/api/logic/net/URLConstants.h index 5ff0f794..ebc495bb 100644 --- a/api/logic/net/URLConstants.h +++ b/api/logic/net/URLConstants.h @@ -29,7 +29,8 @@ const QString IMGUR_BASE_URL("https://api.imgur.com/3/"); const QString FMLLIBS_OUR_BASE_URL("https://files.multimc.org/fmllibs/"); const QString FMLLIBS_FORGE_BASE_URL("https://files.minecraftforge.net/fmllibs/"); const QString TRANSLATIONS_BASE_URL("https://files.multimc.org/translations/"); -const QString FTB_CDN_BASE_URL("https://ftb.forgecdn.net/FTB2/"); + +const QString LEGACY_FTB_CDN_BASE_URL("https://dist.creeper.host/FTB2/"); QString getJarPath(QString version); QString getLegacyJarUrl(QString version); -- cgit