2019-11-04 04:18:12 +05:30
|
|
|
#include "PackInstallTask.h"
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include <QtConcurrent>
|
2019-11-04 04:18:12 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "MMCZip.h"
|
2018-03-04 01:52:00 +05:30
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "settings/INISettingsObject.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
2020-06-27 15:32:31 +05:30
|
|
|
#include "minecraft/PackProfile.h"
|
2018-03-12 19:39:07 +05:30
|
|
|
#include "minecraft/GradleSpecifier.h"
|
2019-01-04 06:18:36 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "Application.h"
|
2019-11-04 04:18:12 +05:30
|
|
|
|
|
|
|
namespace LegacyFTB {
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
PackInstallTask::PackInstallTask(shared_qobject_ptr<QNetworkAccessManager> network, Modpack pack, QString version)
|
2018-03-01 00:12:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_pack = pack;
|
|
|
|
m_version = version;
|
2021-11-22 03:51:12 +05:30
|
|
|
m_network = network;
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::executeTask()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
downloadPack();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::downloadPack()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Downloading zip for %1").arg(m_pack.name));
|
2018-03-19 07:06:12 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
auto packoffset = QString("%1/%2/%3").arg(m_pack.dir, m_version.replace(".", "_"), m_pack.file);
|
2021-11-22 03:51:12 +05:30
|
|
|
auto entry = APPLICATION->metacache()->resolveEntry("FTBPacks", packoffset);
|
2021-12-31 09:57:59 +05:30
|
|
|
netJobContainer = new NetJob("Download FTB Pack", m_network);
|
2018-03-19 07:06:12 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
entry->setStale(true);
|
2018-08-02 04:22:31 +05:30
|
|
|
QString url;
|
2019-11-04 04:18:12 +05:30
|
|
|
if(m_pack.type == PackType::Private)
|
2018-08-02 04:22:31 +05:30
|
|
|
{
|
2020-07-18 19:48:02 +05:30
|
|
|
url = QString(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "privatepacks/%1").arg(packoffset);
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-18 19:48:02 +05:30
|
|
|
url = QString(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "modpacks/%1").arg(packoffset);
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
2021-11-22 03:51:12 +05:30
|
|
|
netJobContainer->addNetAction(Net::Download::makeCached(url, entry));
|
2018-07-15 18:21:05 +05:30
|
|
|
archivePath = entry->getFullPath();
|
2018-03-01 00:12:30 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
connect(netJobContainer.get(), &NetJob::succeeded, this, &PackInstallTask::onDownloadSucceeded);
|
|
|
|
connect(netJobContainer.get(), &NetJob::failed, this, &PackInstallTask::onDownloadFailed);
|
|
|
|
connect(netJobContainer.get(), &NetJob::progress, this, &PackInstallTask::onDownloadProgress);
|
2021-12-31 09:57:59 +05:30
|
|
|
netJobContainer->start();
|
2018-03-01 00:12:30 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
progress(1, 4);
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::onDownloadSucceeded()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
abortable = false;
|
|
|
|
unzip();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::onDownloadFailed(QString reason)
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-12-11 01:20:15 +05:30
|
|
|
abortable = false;
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(reason);
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::onDownloadProgress(qint64 current, qint64 total)
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
abortable = true;
|
|
|
|
progress(current, total * 4);
|
|
|
|
setStatus(tr("Downloading zip for %1 (%2%)").arg(m_pack.name).arg(current / 10));
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::unzip()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-03 01:04:55 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), QOverload<QString, QString>::of(MMCZip::extractDir), archivePath, extractDir.absolutePath() + "/unzip");
|
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, archivePath, extractDir.absolutePath() + "/unzip");
|
2022-05-03 01:04:55 +05:30
|
|
|
#endif
|
2019-11-04 04:18:12 +05:30
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &PackInstallTask::onUnzipFinished);
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &PackInstallTask::onUnzipCanceled);
|
2018-07-15 18:21:05 +05:30
|
|
|
m_extractFutureWatcher.setFuture(m_extractFuture);
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::onUnzipFinished()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
install();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::onUnzipCanceled()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitAborted();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
void PackInstallTask::install()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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"))
|
|
|
|
{
|
2022-03-18 17:49:09 +05:30
|
|
|
emitFailed(tr("Failed to move unzipped Minecraft!"));
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString instanceConfigPath = FS::PathCombine(m_stagingPath, "instance.cfg");
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(instanceConfigPath);
|
2021-02-10 03:16:51 +05:30
|
|
|
instanceSettings->suspendSave();
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
|
2020-06-27 15:32:31 +05:30
|
|
|
auto components = instance.getPackProfile();
|
2018-07-15 18:21:05 +05:30
|
|
|
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();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
2019-11-04 04:18:12 +05:30
|
|
|
bool PackInstallTask::abort()
|
2018-03-04 01:52:00 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(abortable)
|
|
|
|
{
|
|
|
|
return netJobContainer->abort();
|
|
|
|
}
|
|
|
|
return false;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
2019-11-04 04:18:12 +05:30
|
|
|
|
2020-07-18 19:48:02 +05:30
|
|
|
}
|