2018-03-01 00:12:30 +05:30
|
|
|
#include "FtbPackInstallTask.h"
|
|
|
|
#include "Env.h"
|
|
|
|
#include "MMCZip.h"
|
|
|
|
#include "QtConcurrent"
|
2018-03-04 01:52:00 +05:30
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "settings/INISettingsObject.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/ComponentList.h"
|
2018-03-12 19:39:07 +05:30
|
|
|
#include "minecraft/GradleSpecifier.h"
|
2018-03-01 00:12:30 +05:30
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
FtbPackInstallTask::FtbPackInstallTask(FtbModpack pack, QString version)
|
2018-03-01 00:12:30 +05:30
|
|
|
{
|
2018-03-19 07:06:12 +05:30
|
|
|
m_pack = pack;
|
|
|
|
m_version = version;
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::executeTask()
|
|
|
|
{
|
2018-03-01 00:12:30 +05:30
|
|
|
downloadPack();
|
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::downloadPack()
|
|
|
|
{
|
|
|
|
setStatus(tr("Downloading zip for %1").arg(m_pack.name));
|
|
|
|
|
2018-03-30 00:25:47 +05:30
|
|
|
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");
|
2018-03-19 07:06:12 +05:30
|
|
|
|
|
|
|
entry->setStale(true);
|
2018-03-30 00:25:47 +05:30
|
|
|
QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1").arg(packoffset);
|
2018-03-19 07:06:12 +05:30
|
|
|
job->addNetAction(Net::Download::makeCached(url, entry));
|
|
|
|
archivePath = entry->getFullPath();
|
2018-03-01 00:12:30 +05:30
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
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();
|
2018-03-01 00:12:30 +05:30
|
|
|
|
2018-03-04 01:52:00 +05:30
|
|
|
progress(1, 4);
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::onDownloadSucceeded()
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
abortable = false;
|
2018-03-19 07:06:12 +05:30
|
|
|
unzip();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::onDownloadFailed(QString reason)
|
|
|
|
{
|
2018-03-01 00:12:30 +05:30
|
|
|
emitFailed(reason);
|
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total)
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
abortable = true;
|
|
|
|
progress(current, total * 4);
|
2018-03-19 07:06:12 +05:30
|
|
|
setStatus(tr("Downloading zip for %1 (%2\%)").arg(m_pack.name).arg(current / 10));
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::unzip()
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
progress(2, 4);
|
|
|
|
setStatus(tr("Extracting modpack"));
|
2018-03-01 00:12:30 +05:30
|
|
|
QDir extractDir(m_stagingPath);
|
|
|
|
|
|
|
|
m_packZip.reset(new QuaZip(archivePath));
|
2018-03-19 07:06:12 +05:30
|
|
|
if(!m_packZip->open(QuaZip::mdUnzip))
|
|
|
|
{
|
2018-03-01 00:12:30 +05:30
|
|
|
emitFailed(tr("Failed to open modpack file %1!").arg(archivePath));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-04 01:52:00 +05:30
|
|
|
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, archivePath, extractDir.absolutePath() + "/unzip");
|
2018-03-01 00:12:30 +05:30
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &FtbPackInstallTask::onUnzipFinished);
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &FtbPackInstallTask::onUnzipCanceled);
|
|
|
|
m_extractFutureWatcher.setFuture(m_extractFuture);
|
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::onUnzipFinished()
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
install();
|
2018-03-01 00:12:30 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::onUnzipCanceled()
|
|
|
|
{
|
2018-03-01 00:12:30 +05:30
|
|
|
emitAborted();
|
|
|
|
}
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void FtbPackInstallTask::install()
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
progress(3, 4);
|
|
|
|
setStatus(tr("Installing modpack"));
|
|
|
|
QDir unzipMcDir(m_stagingPath + "/unzip/minecraft");
|
2018-03-19 07:06:12 +05:30
|
|
|
if(unzipMcDir.exists())
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
//ok, found minecraft dir, move contents to instance dir
|
2018-03-19 07:06:12 +05:30
|
|
|
if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft"))
|
|
|
|
{
|
2018-03-12 19:39:07 +05:30
|
|
|
emitFailed(tr("Failed to move unzipped minecraft!"));
|
|
|
|
return;
|
|
|
|
}
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString instanceConfigPath = FS::PathCombine(m_stagingPath, "instance.cfg");
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(instanceConfigPath);
|
|
|
|
instanceSettings->registerSetting("InstanceType", "Legacy");
|
|
|
|
instanceSettings->set("InstanceType", "OneSix");
|
|
|
|
|
|
|
|
MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
|
|
|
|
auto components = instance.getComponentList();
|
|
|
|
components->buildingFromScratch();
|
2018-03-19 07:06:12 +05:30
|
|
|
components->setComponentVersion("net.minecraft", m_pack.mcVersion, true);
|
2018-03-04 01:52:00 +05:30
|
|
|
|
|
|
|
bool fallback = true;
|
|
|
|
|
|
|
|
//handle different versions
|
|
|
|
QFile packJson(m_stagingPath + "/.minecraft/pack.json");
|
|
|
|
QDir jarmodDir = QDir(m_stagingPath + "/unzip/instMods");
|
2018-03-19 07:06:12 +05:30
|
|
|
if(packJson.exists())
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
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();
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
foreach (const QJsonValue &value, libs)
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
QString nameValue = value.toObject().value("name").toString();
|
2018-03-19 07:06:12 +05:30
|
|
|
if(!nameValue.startsWith("net.minecraftforge"))
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:39:07 +05:30
|
|
|
GradleSpecifier forgeVersion(nameValue);
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(m_pack.mcVersion, "").replace("-", ""));
|
2018-03-04 01:52:00 +05:30
|
|
|
packJson.remove();
|
|
|
|
fallback = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
if(jarmodDir.exists())
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
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");
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
if(fallback)
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
//TODO: Some fallback mechanism... or just keep failing!
|
|
|
|
emitFailed(tr("No installation method found!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
components->saveNow();
|
|
|
|
|
|
|
|
progress(4, 4);
|
|
|
|
|
|
|
|
instance.init();
|
|
|
|
instance.setName(m_instName);
|
2018-03-19 07:06:12 +05:30
|
|
|
if(m_instIcon == "default")
|
|
|
|
{
|
2018-03-04 01:52:00 +05:30
|
|
|
m_instIcon = "ftb_logo";
|
|
|
|
}
|
|
|
|
instance.setIconKey(m_instIcon);
|
|
|
|
instance.setGroupInitial(m_instGroup);
|
|
|
|
instanceSettings->resumeSave();
|
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:39:07 +05:30
|
|
|
bool FtbPackInstallTask::abort()
|
2018-03-04 01:52:00 +05:30
|
|
|
{
|
2018-03-19 07:06:12 +05:30
|
|
|
if(abortable)
|
|
|
|
{
|
|
|
|
return netJobContainer->abort();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|