2017-04-20 08:52:04 +05:30
|
|
|
#include "minecraft/onesix/OneSixInstance.h"
|
2016-10-03 04:25:54 +05:30
|
|
|
|
|
|
|
#include "InstanceImportTask.h"
|
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "BaseInstanceProvider.h"
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "Env.h"
|
|
|
|
#include "MMCZip.h"
|
|
|
|
#include "NullInstance.h"
|
|
|
|
#include "settings/INISettingsObject.h"
|
|
|
|
#include "icons/IIconList.h"
|
2016-10-26 21:42:33 +05:30
|
|
|
#include <QtConcurrentRun>
|
2017-04-22 01:53:31 +05:30
|
|
|
|
2017-04-22 22:21:04 +05:30
|
|
|
// FIXME: this does not belong here, it's Minecraft/Flame specific
|
|
|
|
#include "minecraft/flame/FileResolvingTask.h"
|
|
|
|
#include "minecraft/flame/PackManifest.h"
|
2017-04-20 08:52:04 +05:30
|
|
|
#include "Json.h"
|
2016-10-03 04:25:54 +05:30
|
|
|
|
|
|
|
InstanceImportTask::InstanceImportTask(SettingsObjectPtr settings, const QUrl sourceUrl, BaseInstanceProvider * target,
|
|
|
|
const QString &instName, const QString &instIcon, const QString &instGroup)
|
|
|
|
{
|
|
|
|
m_globalSettings = settings;
|
|
|
|
m_sourceUrl = sourceUrl;
|
|
|
|
m_target = target;
|
|
|
|
m_instName = instName;
|
|
|
|
m_instIcon = instIcon;
|
|
|
|
m_instGroup = instGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::executeTask()
|
|
|
|
{
|
|
|
|
InstancePtr newInstance;
|
|
|
|
|
|
|
|
if (m_sourceUrl.isLocalFile())
|
|
|
|
{
|
|
|
|
m_archivePath = m_sourceUrl.toLocalFile();
|
|
|
|
extractAndTweak();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString()));
|
2016-10-03 04:25:54 +05:30
|
|
|
m_downloadRequired = true;
|
|
|
|
|
|
|
|
const QString path = m_sourceUrl.host() + '/' + m_sourceUrl.path();
|
|
|
|
auto entry = ENV.metacache()->resolveEntry("general", path);
|
|
|
|
entry->setStale(true);
|
|
|
|
m_filesNetJob.reset(new NetJob(tr("Modpack download")));
|
|
|
|
m_filesNetJob->addNetAction(Net::Download::makeCached(m_sourceUrl, entry));
|
|
|
|
m_archivePath = entry->getFullPath();
|
|
|
|
auto job = m_filesNetJob.get();
|
|
|
|
connect(job, &NetJob::succeeded, this, &InstanceImportTask::downloadSucceeded);
|
|
|
|
connect(job, &NetJob::progress, this, &InstanceImportTask::downloadProgressChanged);
|
|
|
|
connect(job, &NetJob::failed, this, &InstanceImportTask::downloadFailed);
|
|
|
|
m_filesNetJob->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadSucceeded()
|
|
|
|
{
|
|
|
|
extractAndTweak();
|
2017-04-22 01:53:31 +05:30
|
|
|
m_filesNetJob.reset();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadFailed(QString reason)
|
|
|
|
{
|
|
|
|
emitFailed(reason);
|
2017-04-22 01:53:31 +05:30
|
|
|
m_filesNetJob.reset();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadProgressChanged(qint64 current, qint64 total)
|
|
|
|
{
|
|
|
|
setProgress(current / 2, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QFileInfo findRecursive(const QString &dir, const QString &name)
|
|
|
|
{
|
|
|
|
for (const auto info : QDir(dir).entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files, QDir::DirsLast))
|
|
|
|
{
|
|
|
|
if (info.isFile() && info.fileName() == name)
|
|
|
|
{
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
else if (info.isDir())
|
|
|
|
{
|
|
|
|
const QFileInfo res = findRecursive(info.absoluteFilePath(), name);
|
|
|
|
if (res.isFile() && res.exists())
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QFileInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::extractAndTweak()
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
setStatus(tr("Extracting modpack"));
|
2016-10-26 21:42:33 +05:30
|
|
|
m_stagingPath = m_target->getStagedInstancePath();
|
|
|
|
QDir extractDir(m_stagingPath);
|
2017-04-22 02:00:39 +05:30
|
|
|
qDebug() << "Attempting to create instance from" << m_archivePath;
|
2016-10-26 21:42:33 +05:30
|
|
|
|
|
|
|
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, m_archivePath, extractDir.absolutePath());
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &InstanceImportTask::extractFinished);
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &InstanceImportTask::extractAborted);
|
|
|
|
m_extractFutureWatcher.setFuture(m_extractFuture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::extractFinished()
|
|
|
|
{
|
|
|
|
if (m_extractFuture.result().isEmpty())
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2016-10-26 21:42:33 +05:30
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2016-10-03 04:25:54 +05:30
|
|
|
emitFailed(tr("Failed to extract modpack"));
|
|
|
|
return;
|
|
|
|
}
|
2016-10-26 21:42:33 +05:30
|
|
|
QDir extractDir(m_stagingPath);
|
2017-04-22 01:53:31 +05:30
|
|
|
|
2017-04-22 02:00:39 +05:30
|
|
|
qDebug() << "Fixing permissions for extracted pack files...";
|
2017-04-22 01:53:31 +05:30
|
|
|
QDirIterator it(extractDir, QDirIterator::Subdirectories);
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
auto filepath = it.next();
|
|
|
|
QFileInfo file(filepath);
|
|
|
|
auto permissions = QFile::permissions(filepath);
|
|
|
|
auto origPermissions = permissions;
|
|
|
|
if(file.isDir())
|
|
|
|
{
|
|
|
|
// Folder +rwx for current user
|
|
|
|
permissions |= QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// File +rw for current user
|
|
|
|
permissions |= QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser;
|
|
|
|
}
|
|
|
|
if(origPermissions != permissions)
|
|
|
|
{
|
|
|
|
if(!QFile::setPermissions(filepath, permissions))
|
|
|
|
{
|
|
|
|
qWarning() << "Could not fix" << filepath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Fixed" << filepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
const QFileInfo instanceCfgFile = findRecursive(extractDir.absolutePath(), "instance.cfg");
|
2017-04-22 22:21:04 +05:30
|
|
|
const QFileInfo flameJson = findRecursive(extractDir.absolutePath(), "manifest.json");
|
2017-04-20 08:52:04 +05:30
|
|
|
if (instanceCfgFile.isFile())
|
|
|
|
{
|
2017-04-22 02:00:39 +05:30
|
|
|
qDebug() << "Pack appears to be exported from MultiMC.";
|
2017-04-20 08:52:04 +05:30
|
|
|
processMultiMC(instanceCfgFile);
|
|
|
|
}
|
2017-04-22 22:21:04 +05:30
|
|
|
else if (flameJson.isFile())
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2017-04-22 22:21:04 +05:30
|
|
|
qDebug() << "Pack appears to be from 'Flame'.";
|
|
|
|
processFlame(flameJson);
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
else
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2017-04-22 01:53:31 +05:30
|
|
|
qCritical() << "Archive does not contain a recognized modpack type.";
|
2016-10-26 21:42:33 +05:30
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2017-04-20 08:52:04 +05:30
|
|
|
emitFailed(tr("Archive does not contain a recognized modpack type."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::extractAborted()
|
|
|
|
{
|
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
|
|
|
emitFailed(tr("Instance import has been aborted."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-22 22:21:04 +05:30
|
|
|
void InstanceImportTask::processFlame(const QFileInfo & manifest)
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2017-04-22 08:50:06 +05:30
|
|
|
const static QMap<QString,QString> forgemap = {
|
|
|
|
{"1.2.5", "3.4.9.171"},
|
|
|
|
{"1.4.2", "6.0.1.355"},
|
|
|
|
{"1.4.7", "6.6.2.534"},
|
|
|
|
{"1.5.2", "7.8.1.737"}
|
|
|
|
};
|
2017-04-22 22:21:04 +05:30
|
|
|
Flame::Manifest pack;
|
2017-04-20 08:52:04 +05:30
|
|
|
try
|
|
|
|
{
|
2017-04-22 22:21:04 +05:30
|
|
|
Flame::loadManifest(pack, manifest.absoluteFilePath());
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
catch (JSONValidationError & e)
|
|
|
|
{
|
2017-04-22 01:53:31 +05:30
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2017-04-22 22:21:04 +05:30
|
|
|
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
|
2016-10-03 04:25:54 +05:30
|
|
|
return;
|
|
|
|
}
|
2017-04-20 08:52:04 +05:30
|
|
|
m_packRoot = manifest.absolutePath();
|
2017-04-22 01:53:31 +05:30
|
|
|
if(!pack.overrides.isEmpty())
|
|
|
|
{
|
|
|
|
QString overridePath = FS::PathCombine(m_packRoot, pack.overrides);
|
|
|
|
QString mcPath = FS::PathCombine(m_packRoot, "minecraft");
|
|
|
|
if (!QFile::rename(overridePath, mcPath))
|
|
|
|
{
|
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2017-04-22 22:21:04 +05:30
|
|
|
emitFailed(tr("Could not rename the overrides folder:\n") + pack.overrides);
|
2017-04-22 01:53:31 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString forgeVersion;
|
|
|
|
for(auto &loader: pack.minecraft.modLoaders)
|
|
|
|
{
|
|
|
|
auto id = loader.id;
|
|
|
|
if(id.startsWith("forge-"))
|
|
|
|
{
|
|
|
|
id.remove("forge-");
|
|
|
|
forgeVersion = id;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-22 22:21:04 +05:30
|
|
|
qWarning() << "Unknown mod loader in manifest:" << id;
|
2017-04-22 01:53:31 +05:30
|
|
|
}
|
|
|
|
|
2017-04-22 08:50:06 +05:30
|
|
|
QString configPath = FS::PathCombine(m_packRoot, "instance.cfg");
|
2017-04-20 08:52:04 +05:30
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
|
|
|
|
instanceSettings->registerSetting("InstanceType", "Legacy");
|
|
|
|
instanceSettings->set("InstanceType", "OneSix");
|
|
|
|
OneSixInstance instance(m_globalSettings, instanceSettings, m_packRoot);
|
2017-04-22 08:50:06 +05:30
|
|
|
auto mcVersion = pack.minecraft.version;
|
2017-04-22 22:21:04 +05:30
|
|
|
// Hack to correct some 'special sauce'...
|
2017-04-22 08:50:06 +05:30
|
|
|
if(mcVersion.endsWith('.'))
|
|
|
|
{
|
|
|
|
mcVersion.remove(QRegExp("[.]+$"));
|
2017-04-22 22:21:04 +05:30
|
|
|
qWarning() << "Mysterious trailing dots removed from Minecraft version while importing pack.";
|
2017-04-22 08:50:06 +05:30
|
|
|
}
|
|
|
|
instance.setComponentVersion("net.minecraft", mcVersion);
|
2017-04-22 01:53:31 +05:30
|
|
|
if(!forgeVersion.isEmpty())
|
|
|
|
{
|
2017-04-22 08:50:06 +05:30
|
|
|
// FIXME: dirty, nasty, hack. Proper solution requires dependency resolution and knowledge of the metadata.
|
|
|
|
if(forgeVersion == "recommended")
|
|
|
|
{
|
|
|
|
if(forgemap.contains(mcVersion))
|
|
|
|
{
|
|
|
|
forgeVersion = forgemap[mcVersion];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Could not map recommended forge version for" << mcVersion;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 01:53:31 +05:30
|
|
|
instance.setComponentVersion("net.minecraftforge", forgeVersion);
|
|
|
|
}
|
2017-04-22 08:50:06 +05:30
|
|
|
if (m_instIcon != "default")
|
|
|
|
{
|
|
|
|
instance.setIconKey(m_instIcon);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(pack.name.contains("Direwolf20"))
|
|
|
|
{
|
|
|
|
instance.setIconKey("steve");
|
|
|
|
}
|
|
|
|
else if(pack.name.contains("FTB") || pack.name.contains("Feed The Beast"))
|
|
|
|
{
|
|
|
|
instance.setIconKey("ftb_logo");
|
|
|
|
}
|
2017-04-22 22:21:04 +05:30
|
|
|
else
|
|
|
|
{
|
|
|
|
// default to something other than the MultiMC default to distinguish these
|
|
|
|
instance.setIconKey("flame");
|
|
|
|
}
|
2017-04-22 08:50:06 +05:30
|
|
|
}
|
|
|
|
instance.init();
|
|
|
|
QString jarmodsPath = FS::PathCombine(m_packRoot, "minecraft", "jarmods");
|
|
|
|
QFileInfo jarmodsInfo(jarmodsPath);
|
|
|
|
if(jarmodsInfo.isDir())
|
|
|
|
{
|
|
|
|
// install all the jar mods
|
|
|
|
qDebug() << "Found jarmods:";
|
|
|
|
QDir jarmodsDir(jarmodsPath);
|
|
|
|
QStringList jarMods;
|
|
|
|
for (auto info: jarmodsDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files))
|
|
|
|
{
|
|
|
|
qDebug() << info.fileName();
|
|
|
|
jarMods.push_back(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
auto profile = instance.getMinecraftProfile();
|
|
|
|
profile->installJarMods(jarMods);
|
|
|
|
// nuke the original files
|
|
|
|
FS::deletePath(jarmodsPath);
|
|
|
|
}
|
2017-04-20 08:52:04 +05:30
|
|
|
instance.setName(m_instName);
|
2017-04-22 22:21:04 +05:30
|
|
|
m_modIdResolver.reset(new Flame::FileResolvingTask(pack));
|
|
|
|
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::succeeded, [&]()
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2017-04-22 22:21:04 +05:30
|
|
|
auto results = m_modIdResolver->getResults();
|
|
|
|
m_filesNetJob.reset(new NetJob(tr("Mod download")));
|
2017-04-22 01:53:31 +05:30
|
|
|
for(auto result: results.files)
|
|
|
|
{
|
|
|
|
auto path = FS::PathCombine(m_packRoot, "minecraft/mods", result.fileName);
|
|
|
|
auto dl = Net::Download::makeFile(result.url,path);
|
|
|
|
m_filesNetJob->addNetAction(dl);
|
|
|
|
}
|
2017-04-22 22:21:04 +05:30
|
|
|
m_modIdResolver.reset();
|
2017-04-22 01:53:31 +05:30
|
|
|
connect(m_filesNetJob.get(), &NetJob::succeeded, this, [&]()
|
|
|
|
{
|
|
|
|
m_filesNetJob.reset();
|
|
|
|
if (!m_target->commitStagedInstance(m_stagingPath, m_packRoot, m_instName, m_instGroup))
|
|
|
|
{
|
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
|
|
|
emitFailed(tr("Unable to commit instance"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::failed, [&](QString reason)
|
|
|
|
{
|
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
|
|
|
m_filesNetJob.reset();
|
|
|
|
emitFailed(reason);
|
|
|
|
});
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::progress, [&](qint64 current, qint64 total)
|
|
|
|
{
|
|
|
|
setProgress(current, total);
|
|
|
|
});
|
2017-05-04 02:41:52 +05:30
|
|
|
setStatus(tr("Downloading mods..."));
|
2017-04-22 01:53:31 +05:30
|
|
|
m_filesNetJob->start();
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
2017-04-22 01:53:31 +05:30
|
|
|
);
|
2017-04-22 22:21:04 +05:30
|
|
|
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::failed, [&](QString reason)
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2017-04-22 22:21:04 +05:30
|
|
|
m_modIdResolver.reset();
|
|
|
|
emitFailed(tr("Unable to resolve mod IDs:\n") + reason);
|
2017-04-22 01:53:31 +05:30
|
|
|
});
|
2017-04-22 22:21:04 +05:30
|
|
|
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::progress, [&](qint64 current, qint64 total)
|
2017-04-22 01:53:31 +05:30
|
|
|
{
|
|
|
|
setProgress(current, total);
|
|
|
|
});
|
2017-04-22 22:21:04 +05:30
|
|
|
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::status, [&](QString status)
|
2017-04-22 01:53:31 +05:30
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
setStatus(status);
|
2017-04-22 01:53:31 +05:30
|
|
|
});
|
2017-04-22 22:21:04 +05:30
|
|
|
m_modIdResolver->start();
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::processMultiMC(const QFileInfo & config)
|
|
|
|
{
|
2016-10-03 04:25:54 +05:30
|
|
|
// FIXME: copy from FolderInstanceProvider!!! FIX IT!!!
|
2017-04-20 08:52:04 +05:30
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(config.absoluteFilePath());
|
2016-10-03 04:25:54 +05:30
|
|
|
instanceSettings->registerSetting("InstanceType", "Legacy");
|
|
|
|
|
2017-04-20 08:52:04 +05:30
|
|
|
QString actualDir = config.absolutePath();
|
2016-10-03 04:25:54 +05:30
|
|
|
NullInstance instance(m_globalSettings, instanceSettings, actualDir);
|
|
|
|
|
|
|
|
// reset time played on import... because packs.
|
|
|
|
instance.resetTimePlayed();
|
|
|
|
|
|
|
|
// set a new nice name
|
|
|
|
instance.setName(m_instName);
|
|
|
|
|
|
|
|
// if the icon was specified by user, use that. otherwise pull icon from the pack
|
|
|
|
if (m_instIcon != "default")
|
|
|
|
{
|
|
|
|
instance.setIconKey(m_instIcon);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_instIcon = instance.iconKey();
|
|
|
|
auto importIconPath = FS::PathCombine(instance.instanceRoot(), m_instIcon + ".png");
|
|
|
|
if (QFile::exists(importIconPath))
|
|
|
|
{
|
|
|
|
// import icon
|
|
|
|
auto iconList = ENV.icons();
|
|
|
|
if (iconList->iconFileExists(m_instIcon))
|
|
|
|
{
|
|
|
|
iconList->deleteIcon(m_instIcon);
|
|
|
|
}
|
|
|
|
iconList->installIcons({importIconPath});
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 21:42:33 +05:30
|
|
|
if (!m_target->commitStagedInstance(m_stagingPath, actualDir, m_instName, m_instGroup))
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2016-10-26 21:42:33 +05:30
|
|
|
m_target->destroyStagingPath(m_stagingPath);
|
2016-10-03 04:25:54 +05:30
|
|
|
emitFailed(tr("Unable to commit instance"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
|
|
|
}
|