NOISSUE refactor pack import (extraction and paths)

It now:
* Doesn't extract until it knows the content format is good.
* Extracts in a predictable location, not requiring to use a second path for the actual pack root.
This commit is contained in:
Petr Mrázek 2017-09-04 08:17:25 +02:00
parent b8adbb9b73
commit d80382180e
10 changed files with 129 additions and 58 deletions

View File

@ -37,7 +37,7 @@ public:
{
return QString();
}
virtual bool commitStagedInstance(const QString & keyPath, const QString & path, const QString& instanceName, const QString & groupName)
virtual bool commitStagedInstance(const QString & path, const QString& instanceName, const QString & groupName)
{
return false;
}

View File

@ -310,6 +310,52 @@ void FolderInstanceProvider::on_InstFolderChanged(const Setting &setting, QVaria
emit instancesChanged();
}
}
/*
class FolderInstanceStaging : public Task
{
public:
FolderInstanceStaging(FolderInstanceProvider * parent, Task * child, const QString& instanceName, const QString& groupName)
{
m_parent = parent;
m_child.reset(child);
connect(child, &Task::succeeded, this, &FolderInstanceStaging::childSucceded);
connect(child, &Task::failed, this, &FolderInstanceStaging::childFailed);
connect(child, &Task::status, this, &FolderInstanceStaging::setStatus);
connect(child, &Task::progress, this, &FolderInstanceStaging::setProgress);
m_instanceName = instanceName;
m_groupName = groupName;
}
protected:
virtual void executeTask() override
{
m_stagingPath = m_parent->getStagedInstancePath();
m_child->start();
}
private slots:
void childSucceded()
{
if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName))
emitSucceeded();
// TODO: implement exponential backoff retry scheme with limit
emitFailed("Failed to commit instance");
}
void childFailed(const QString & reason)
{
m_parent->destroyStagingPath(m_stagingPath);
emitFailed(reason);
}
private:
QString m_stagingPath;
FolderInstanceProvider * m_parent;
unique_qobject_ptr<Task> m_child;
QString m_instanceName;
QString m_groupName;
};
*/
QString FolderInstanceProvider::getStagedInstancePath()
{
@ -324,8 +370,7 @@ QString FolderInstanceProvider::getStagedInstancePath()
return path;
}
bool FolderInstanceProvider::commitStagedInstance(const QString& keyPath, const QString& path, const QString& instanceName,
const QString& groupName)
bool FolderInstanceProvider::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName)
{
QDir dir;
QString instID = FS::DirNameFromString(instanceName, m_instDir);
@ -335,7 +380,6 @@ bool FolderInstanceProvider::commitStagedInstance(const QString& keyPath, const
if(!dir.rename(path, destination))
{
qWarning() << "Failed to move" << path << "to" << destination;
destroyStagingPath(keyPath);
return false;
}
groupMap[instID] = groupName;

View File

@ -37,7 +37,7 @@ public:
* Commit the staging area given by @keyPath to the provider - used when creation succeeds.
* Used by instance manipulation tasks.
*/
bool commitStagedInstance(const QString & keyPath, const QString & path, const QString& instanceName, const QString & groupName) override;
bool commitStagedInstance(const QString & keyPath, const QString& instanceName, const QString & groupName) override;
/**
* Destroy a previously created staging area given by @keyPath - used when creation fails.
* Used by instance manipulation tasks.

View File

@ -56,7 +56,7 @@ void InstanceCopyTask::copyFinished()
InstancePtr inst(new NullInstance(m_globalSettings, instanceSettings, m_stagingPath));
inst->setName(m_instName);
inst->setIconKey(m_instIcon);
m_target->commitStagedInstance(m_stagingPath, m_stagingPath, m_instName, m_instGroup);
m_target->commitStagedInstance(m_stagingPath, m_instName, m_instGroup);
emitSucceeded();
}

View File

@ -20,14 +20,6 @@ InstanceCreationTask::InstanceCreationTask(SettingsObjectPtr settings, BaseInsta
void InstanceCreationTask::executeTask()
{
setStatus(tr("Creating instance from version %1").arg(m_version->name()));
/*
auto minecraftVersion = std::dynamic_pointer_cast<MinecraftVersion>(m_version);
if(!minecraftVersion)
{
emitFailed(tr("The supplied version is not a Minecraft version."));
return ;
}
*/
QString stagingPath = m_target->getStagedInstancePath();
QDir rootDir(stagingPath);
@ -41,6 +33,6 @@ void InstanceCreationTask::executeTask()
inst->setName(m_instName);
inst->setIconKey(m_instIcon);
inst->init();
m_target->commitStagedInstance(stagingPath, stagingPath, m_instName, m_instGroup);
m_target->commitStagedInstance(stagingPath, m_instName, m_instGroup);
emitSucceeded();
}

View File

@ -34,7 +34,7 @@ void InstanceImportTask::executeTask()
if (m_sourceUrl.isLocalFile())
{
m_archivePath = m_sourceUrl.toLocalFile();
extractAndTweak();
processZipPack();
}
else
{
@ -57,7 +57,7 @@ void InstanceImportTask::executeTask()
void InstanceImportTask::downloadSucceeded()
{
extractAndTweak();
processZipPack();
m_filesNetJob.reset();
}
@ -92,14 +92,47 @@ static QFileInfo findRecursive(const QString &dir, const QString &name)
return QFileInfo();
}
void InstanceImportTask::extractAndTweak()
void InstanceImportTask::processZipPack()
{
setStatus(tr("Extracting modpack"));
m_stagingPath = m_target->getStagedInstancePath();
QDir extractDir(m_stagingPath);
qDebug() << "Attempting to create instance from" << m_archivePath;
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, m_archivePath, extractDir.absolutePath());
// open the zip and find relevant files in it
m_packZip.reset(new QuaZip(m_archivePath));
if (!m_packZip->open(QuaZip::mdUnzip))
{
emitFailed(tr("Unable to open supplied modpack zip file."));
return;
}
QStringList blacklist = {"instance.cfg", "manifest.json"};
QString mmcFound = MMCZip::findFolderOfFileInZip(m_packZip.get(), "instance.cfg");
QString flameFound = MMCZip::findFolderOfFileInZip(m_packZip.get(), "manifest.json");
QString root;
if(!mmcFound.isNull())
{
// process as MultiMC instance/pack
qDebug() << "MultiMC:" << mmcFound;
root = mmcFound;
m_modpackType = ModpackType::MultiMC;
}
if(!flameFound.isNull())
{
// process as Flame pack
qDebug() << "Flame:" << flameFound;
root = flameFound;
m_modpackType = ModpackType::Flame;
}
if(m_modpackType == ModpackType::Unknown)
{
emitFailed(tr("Archive does not contain a recognized modpack type."));
return;
}
// make sure we extract just the pack
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractSubDir, m_packZip.get(), root, 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);
@ -107,6 +140,7 @@ void InstanceImportTask::extractAndTweak()
void InstanceImportTask::extractFinished()
{
m_packZip.reset();
if (m_extractFuture.result().isEmpty())
{
m_target->destroyStagingPath(m_stagingPath);
@ -146,23 +180,18 @@ void InstanceImportTask::extractFinished()
}
}
const QFileInfo instanceCfgFile = findRecursive(extractDir.absolutePath(), "instance.cfg");
const QFileInfo flameJson = findRecursive(extractDir.absolutePath(), "manifest.json");
if (instanceCfgFile.isFile())
switch(m_modpackType)
{
qDebug() << "Pack appears to be exported from MultiMC.";
processMultiMC(instanceCfgFile);
}
else if (flameJson.isFile())
{
qDebug() << "Pack appears to be from 'Flame'.";
processFlame(flameJson);
}
else
{
qCritical() << "Archive does not contain a recognized modpack type.";
m_target->destroyStagingPath(m_stagingPath);
emitFailed(tr("Archive does not contain a recognized modpack type."));
case ModpackType::Flame:
processFlame();
return;
case ModpackType::MultiMC:
processMultiMC();
return;
case ModpackType::Unknown:
m_target->destroyStagingPath(m_stagingPath);
emitFailed(tr("Archive does not contain a recognized modpack type."));
return;
}
}
@ -173,7 +202,7 @@ void InstanceImportTask::extractAborted()
return;
}
void InstanceImportTask::processFlame(const QFileInfo & manifest)
void InstanceImportTask::processFlame()
{
const static QMap<QString,QString> forgemap = {
{"1.2.5", "3.4.9.171"},
@ -184,7 +213,8 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
Flame::Manifest pack;
try
{
Flame::loadManifest(pack, manifest.absoluteFilePath());
QString configPath = FS::PathCombine(m_stagingPath, "manifest.json");
Flame::loadManifest(pack, configPath);
}
catch (JSONValidationError & e)
{
@ -192,11 +222,10 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
return;
}
m_packRoot = manifest.absolutePath();
if(!pack.overrides.isEmpty())
{
QString overridePath = FS::PathCombine(m_packRoot, pack.overrides);
QString mcPath = FS::PathCombine(m_packRoot, "minecraft");
QString overridePath = FS::PathCombine(m_stagingPath, pack.overrides);
QString mcPath = FS::PathCombine(m_stagingPath, "minecraft");
if (!QFile::rename(overridePath, mcPath))
{
m_target->destroyStagingPath(m_stagingPath);
@ -218,11 +247,11 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
qWarning() << "Unknown mod loader in manifest:" << id;
}
QString configPath = FS::PathCombine(m_packRoot, "instance.cfg");
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
instanceSettings->registerSetting("InstanceType", "Legacy");
instanceSettings->set("InstanceType", "OneSix");
OneSixInstance instance(m_globalSettings, instanceSettings, m_packRoot);
OneSixInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
auto mcVersion = pack.minecraft.version;
// Hack to correct some 'special sauce'...
if(mcVersion.endsWith('.'))
@ -268,7 +297,7 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
}
}
instance.init();
QString jarmodsPath = FS::PathCombine(m_packRoot, "minecraft", "jarmods");
QString jarmodsPath = FS::PathCombine(m_stagingPath, "minecraft", "jarmods");
QFileInfo jarmodsInfo(jarmodsPath);
if(jarmodsInfo.isDir())
{
@ -294,7 +323,7 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
m_filesNetJob.reset(new NetJob(tr("Mod download")));
for(auto result: results.files)
{
auto path = FS::PathCombine(m_packRoot, "minecraft/mods", result.fileName);
auto path = FS::PathCombine(m_stagingPath, "minecraft/mods", result.fileName);
auto dl = Net::Download::makeFile(result.url,path);
m_filesNetJob->addNetAction(dl);
}
@ -302,7 +331,7 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
connect(m_filesNetJob.get(), &NetJob::succeeded, this, [&]()
{
m_filesNetJob.reset();
if (!m_target->commitStagedInstance(m_stagingPath, m_packRoot, m_instName, m_instGroup))
if (!m_target->commitStagedInstance(m_stagingPath, m_instName, m_instGroup))
{
m_target->destroyStagingPath(m_stagingPath);
emitFailed(tr("Unable to commit instance"));
@ -342,14 +371,14 @@ void InstanceImportTask::processFlame(const QFileInfo & manifest)
m_modIdResolver->start();
}
void InstanceImportTask::processMultiMC(const QFileInfo & config)
void InstanceImportTask::processMultiMC()
{
// FIXME: copy from FolderInstanceProvider!!! FIX IT!!!
auto instanceSettings = std::make_shared<INISettingsObject>(config.absoluteFilePath());
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
instanceSettings->registerSetting("InstanceType", "Legacy");
QString actualDir = config.absolutePath();
NullInstance instance(m_globalSettings, instanceSettings, actualDir);
NullInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
// reset time played on import... because packs.
instance.resetTimePlayed();
@ -377,7 +406,7 @@ void InstanceImportTask::processMultiMC(const QFileInfo & config)
iconList->installIcons({importIconPath});
}
}
if (!m_target->commitStagedInstance(m_stagingPath, actualDir, m_instName, m_instGroup))
if (!m_target->commitStagedInstance(m_stagingPath, m_instName, m_instGroup))
{
m_target->destroyStagingPath(m_stagingPath);
emitFailed(tr("Unable to commit instance"));

View File

@ -9,6 +9,7 @@
#include "settings/SettingsObject.h"
#include "QObjectPtr.h"
class QuaZip;
class BaseInstanceProvider;
namespace Flame
{
@ -27,9 +28,9 @@ protected:
virtual void executeTask() override;
private:
void extractAndTweak();
void processMultiMC(const QFileInfo &config);
void processFlame(const QFileInfo &manifest);
void processZipPack();
void processMultiMC();
void processFlame();
private slots:
void downloadSucceeded();
@ -46,11 +47,16 @@ private: /* data */
BaseInstanceProvider * m_target;
QString m_archivePath;
bool m_downloadRequired = false;
QString m_packRoot;
QString m_instName;
QString m_instIcon;
QString m_instGroup;
QString m_stagingPath;
std::unique_ptr<QuaZip> m_packZip;
QFuture<QStringList> m_extractFuture;
QFutureWatcher<QStringList> m_extractFutureWatcher;
enum class ModpackType{
Unknown,
MultiMC,
Flame
} m_modpackType = ModpackType::Unknown;
};

View File

@ -168,7 +168,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
}
// ours
QString MMCZip::findFileInZip(QuaZip * zip, const QString & what, const QString &root)
QString MMCZip::findFolderOfFileInZip(QuaZip * zip, const QString & what, const QString &root)
{
QuaZipDir rootDir(zip, root);
for(auto fileName: rootDir.entryList(QDir::Files))
@ -178,7 +178,7 @@ QString MMCZip::findFileInZip(QuaZip * zip, const QString & what, const QString
}
for(auto fileName: rootDir.entryList(QDir::Dirs))
{
QString result = findFileInZip(zip, what, root + fileName);
QString result = findFolderOfFileInZip(zip, what, root + fileName);
if(!result.isEmpty())
{
return result;

View File

@ -44,7 +44,7 @@ namespace MMCZip
*
* \return the path prefix where the file is
*/
QString MULTIMC_LOGIC_EXPORT findFileInZip(QuaZip * zip, const QString & what, const QString &root = QString());
QString MULTIMC_LOGIC_EXPORT findFolderOfFileInZip(QuaZip * zip, const QString & what, const QString &root = QString(""));
/**
* Find a multiple files of the same name in archive by file name

View File

@ -146,7 +146,7 @@ void World::readFromZip(const QFileInfo &file)
{
return;
}
auto location = MMCZip::findFileInZip(&zip, "level.dat");
auto location = MMCZip::findFolderOfFileInZip(&zip, "level.dat");
is_valid = !location.isEmpty();
if (!is_valid)
{