2022-05-02 23:03:21 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 16:50:05 +05:30
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-05-02 23:03:21 +05:30
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-05-02 23:03:21 +05:30
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-05-02 23:03:21 +05:30
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-05-02 23:03:21 +05:30
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2020-06-07 21:16:12 +05:30
|
|
|
*/
|
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
#include "InstanceImportTask.h"
|
2022-07-09 04:14:44 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
#include "Application.h"
|
2016-10-03 04:25:54 +05:30
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "MMCZip.h"
|
|
|
|
#include "NullInstance.h"
|
2022-07-09 04:14:44 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
#include "icons/IconList.h"
|
2019-06-01 01:22:58 +05:30
|
|
|
#include "icons/IconUtils.h"
|
2017-04-22 01:53:31 +05:30
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
#include "modplatform/technic/TechnicPackProcessor.h"
|
2022-07-08 05:40:41 +05:30
|
|
|
#include "modplatform/modrinth/ModrinthInstanceCreationTask.h"
|
2022-07-09 03:14:43 +05:30
|
|
|
#include "modplatform/flame/FlameInstanceCreationTask.h"
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2022-07-09 04:14:44 +05:30
|
|
|
#include "settings/INISettingsObject.h"
|
2022-05-16 06:46:52 +05:30
|
|
|
|
2022-07-09 04:14:44 +05:30
|
|
|
#include <QtConcurrentRun>
|
2022-01-31 19:55:36 +05:30
|
|
|
#include <algorithm>
|
2021-11-22 03:51:12 +05:30
|
|
|
|
2022-07-09 04:14:44 +05:30
|
|
|
#include <quazip/quazipdir.h>
|
|
|
|
|
2022-05-16 06:46:52 +05:30
|
|
|
InstanceImportTask::InstanceImportTask(const QUrl sourceUrl, QWidget* parent)
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sourceUrl = sourceUrl;
|
2022-05-16 06:46:52 +05:30
|
|
|
m_parent = parent;
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
2022-04-08 03:26:34 +05:30
|
|
|
bool InstanceImportTask::abort()
|
|
|
|
{
|
2022-05-28 05:11:57 +05:30
|
|
|
if (m_filesNetJob)
|
|
|
|
m_filesNetJob->abort();
|
2022-04-08 03:26:34 +05:30
|
|
|
m_extractFuture.cancel();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
void InstanceImportTask::executeTask()
|
|
|
|
{
|
2022-07-09 04:14:44 +05:30
|
|
|
if (m_sourceUrl.isLocalFile()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
m_archivePath = m_sourceUrl.toLocalFile();
|
|
|
|
processZipPack();
|
2022-07-09 04:14:44 +05:30
|
|
|
} else {
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString()));
|
|
|
|
m_downloadRequired = true;
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2022-07-09 04:14:44 +05:30
|
|
|
const QString path(m_sourceUrl.host() + '/' + m_sourceUrl.path());
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
auto entry = APPLICATION->metacache()->resolveEntry("general", path);
|
2018-07-15 18:21:05 +05:30
|
|
|
entry->setStale(true);
|
2022-07-09 04:14:44 +05:30
|
|
|
m_archivePath = entry->getFullPath();
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob = new NetJob(tr("Modpack download"), APPLICATION->network());
|
2018-07-15 18:21:05 +05:30
|
|
|
m_filesNetJob->addNetAction(Net::Download::makeCached(m_sourceUrl, entry));
|
2022-07-09 04:14:44 +05:30
|
|
|
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::succeeded, this, &InstanceImportTask::downloadSucceeded);
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::progress, this, &InstanceImportTask::downloadProgressChanged);
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::failed, this, &InstanceImportTask::downloadFailed);
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob->start();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadSucceeded()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
processZipPack();
|
|
|
|
m_filesNetJob.reset();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadFailed(QString reason)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(reason);
|
|
|
|
m_filesNetJob.reset();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::downloadProgressChanged(qint64 current, qint64 total)
|
|
|
|
{
|
2022-07-09 04:14:44 +05:30
|
|
|
setProgress(current, total);
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
|
|
|
|
2017-09-04 11:47:25 +05:30
|
|
|
void InstanceImportTask::processZipPack()
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Extracting modpack"));
|
|
|
|
QDir extractDir(m_stagingPath);
|
|
|
|
qDebug() << "Attempting to create instance from" << m_archivePath;
|
2016-10-26 21:42:33 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// 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;
|
|
|
|
}
|
2017-09-04 11:47:25 +05:30
|
|
|
|
2022-05-25 01:13:38 +05:30
|
|
|
QuaZipDir packZipDir(m_packZip.get());
|
|
|
|
|
|
|
|
// https://docs.modrinth.com/docs/modpacks/format_definition/#storage
|
|
|
|
bool modrinthFound = packZipDir.exists("/modrinth.index.json");
|
|
|
|
bool technicFound = packZipDir.exists("/bin/modpack.jar") || packZipDir.exists("/bin/version.json");
|
2018-07-15 18:21:05 +05:30
|
|
|
QString root;
|
2022-05-25 01:13:38 +05:30
|
|
|
|
|
|
|
// NOTE: Prioritize modpack platforms that aren't searched for recursively.
|
|
|
|
// Especially Flame has a very common filename for its manifest, which may appear inside overrides for example
|
|
|
|
if(modrinthFound)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-05-25 01:13:38 +05:30
|
|
|
// process as Modrinth pack
|
|
|
|
qDebug() << "Modrinth:" << modrinthFound;
|
|
|
|
m_modpackType = ModpackType::Modrinth;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2020-06-07 21:16:12 +05:30
|
|
|
else if (technicFound)
|
|
|
|
{
|
|
|
|
// process as Technic pack
|
|
|
|
qDebug() << "Technic:" << technicFound;
|
|
|
|
extractDir.mkpath(".minecraft");
|
|
|
|
extractDir.cd(".minecraft");
|
|
|
|
m_modpackType = ModpackType::Technic;
|
|
|
|
}
|
2022-05-25 01:13:38 +05:30
|
|
|
else
|
2022-01-31 19:55:36 +05:30
|
|
|
{
|
2022-05-25 01:13:38 +05:30
|
|
|
QString mmcRoot = MMCZip::findFolderOfFileInZip(m_packZip.get(), "instance.cfg");
|
|
|
|
QString flameRoot = MMCZip::findFolderOfFileInZip(m_packZip.get(), "manifest.json");
|
|
|
|
|
2022-05-29 17:46:13 +05:30
|
|
|
if (!mmcRoot.isNull())
|
2022-05-25 01:13:38 +05:30
|
|
|
{
|
|
|
|
// process as MultiMC instance/pack
|
|
|
|
qDebug() << "MultiMC:" << mmcRoot;
|
|
|
|
root = mmcRoot;
|
|
|
|
m_modpackType = ModpackType::MultiMC;
|
|
|
|
}
|
2022-05-29 17:46:13 +05:30
|
|
|
else if(!flameRoot.isNull())
|
2022-05-25 01:13:38 +05:30
|
|
|
{
|
|
|
|
// process as Flame pack
|
|
|
|
qDebug() << "Flame:" << flameRoot;
|
|
|
|
root = flameRoot;
|
|
|
|
m_modpackType = ModpackType::Flame;
|
|
|
|
}
|
2022-01-31 19:55:36 +05:30
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
if(m_modpackType == ModpackType::Unknown)
|
|
|
|
{
|
|
|
|
emitFailed(tr("Archive does not contain a recognized modpack type."));
|
|
|
|
return;
|
|
|
|
}
|
2017-09-04 11:47:25 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// 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);
|
2016-10-26 21:42:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::extractFinished()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_packZip.reset();
|
2021-02-09 09:34:23 +05:30
|
|
|
if (!m_extractFuture.result())
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to extract modpack"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QDir extractDir(m_stagingPath);
|
2017-04-22 01:53:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << "Fixing permissions for extracted pack files...";
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
logWarning(tr("Could not fix permissions for %1").arg(filepath));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Fixed" << filepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 01:53:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
switch(m_modpackType)
|
|
|
|
{
|
|
|
|
case ModpackType::MultiMC:
|
|
|
|
processMultiMC();
|
|
|
|
return;
|
2020-06-07 21:16:12 +05:30
|
|
|
case ModpackType::Technic:
|
|
|
|
processTechnic();
|
|
|
|
return;
|
2022-01-31 19:55:36 +05:30
|
|
|
case ModpackType::Flame:
|
|
|
|
processFlame();
|
|
|
|
return;
|
|
|
|
case ModpackType::Modrinth:
|
|
|
|
processModrinth();
|
|
|
|
return;
|
2018-07-15 18:21:05 +05:30
|
|
|
case ModpackType::Unknown:
|
|
|
|
emitFailed(tr("Archive does not contain a recognized modpack type."));
|
|
|
|
return;
|
|
|
|
}
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceImportTask::extractAborted()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(tr("Instance import has been aborted."));
|
2022-07-08 05:40:41 +05:30
|
|
|
emit aborted();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
|
2017-09-04 11:47:25 +05:30
|
|
|
void InstanceImportTask::processFlame()
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2022-07-09 03:14:43 +05:30
|
|
|
auto* inst_creation_task = new FlameCreationTask(m_stagingPath, m_globalSettings, m_parent);
|
|
|
|
|
|
|
|
inst_creation_task->setName(m_instName);
|
|
|
|
inst_creation_task->setIcon(m_instIcon);
|
|
|
|
inst_creation_task->setGroup(m_instGroup);
|
|
|
|
|
|
|
|
connect(inst_creation_task, &Task::succeeded, this, [this, inst_creation_task] {
|
|
|
|
setOverride(inst_creation_task->shouldOverride());
|
|
|
|
emitSucceeded();
|
|
|
|
});
|
|
|
|
connect(inst_creation_task, &Task::failed, this, &InstanceImportTask::emitFailed);
|
|
|
|
connect(inst_creation_task, &Task::progress, this, &InstanceImportTask::setProgress);
|
|
|
|
connect(inst_creation_task, &Task::status, this, &InstanceImportTask::setStatus);
|
|
|
|
connect(inst_creation_task, &Task::finished, inst_creation_task, &InstanceCreationTask::deleteLater);
|
2022-05-29 01:23:12 +05:30
|
|
|
|
2022-07-09 03:14:43 +05:30
|
|
|
connect(this, &Task::aborted, inst_creation_task, &InstanceCreationTask::abort);
|
2022-05-29 01:23:12 +05:30
|
|
|
|
2022-07-09 03:14:43 +05:30
|
|
|
inst_creation_task->start();
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
void InstanceImportTask::processTechnic()
|
|
|
|
{
|
|
|
|
shared_qobject_ptr<Technic::TechnicPackProcessor> packProcessor = new Technic::TechnicPackProcessor();
|
|
|
|
connect(packProcessor.get(), &Technic::TechnicPackProcessor::succeeded, this, &InstanceImportTask::emitSucceeded);
|
|
|
|
connect(packProcessor.get(), &Technic::TechnicPackProcessor::failed, this, &InstanceImportTask::emitFailed);
|
|
|
|
packProcessor->run(m_globalSettings, m_instName, m_instIcon, m_stagingPath);
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:47:25 +05:30
|
|
|
void InstanceImportTask::processMultiMC()
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
NullInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// reset time played on import... because packs.
|
|
|
|
instance.resetTimePlayed();
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// set a new nice name
|
|
|
|
instance.setName(m_instName);
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// if the icon was specified by user, use that. otherwise pull icon from the pack
|
2022-05-16 06:46:52 +05:30
|
|
|
if (m_instIcon != "default") {
|
2018-07-15 18:21:05 +05:30
|
|
|
instance.setIconKey(m_instIcon);
|
2022-05-16 06:46:52 +05:30
|
|
|
} else {
|
2018-07-15 18:21:05 +05:30
|
|
|
m_instIcon = instance.iconKey();
|
2019-06-01 01:22:58 +05:30
|
|
|
|
|
|
|
auto importIconPath = IconUtils::findBestIconIn(instance.instanceRoot(), m_instIcon);
|
2022-05-16 06:46:52 +05:30
|
|
|
if (!importIconPath.isNull() && QFile::exists(importIconPath)) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// import icon
|
2021-11-22 03:51:12 +05:30
|
|
|
auto iconList = APPLICATION->icons();
|
2022-05-16 06:46:52 +05:30
|
|
|
if (iconList->iconFileExists(m_instIcon)) {
|
2018-07-15 18:21:05 +05:30
|
|
|
iconList->deleteIcon(m_instIcon);
|
|
|
|
}
|
2022-05-16 06:46:52 +05:30
|
|
|
iconList->installIcons({ importIconPath });
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
2022-01-31 19:55:36 +05:30
|
|
|
|
2022-05-16 06:46:52 +05:30
|
|
|
void InstanceImportTask::processModrinth()
|
|
|
|
{
|
2022-07-08 05:40:41 +05:30
|
|
|
auto* inst_creation_task = new ModrinthCreationTask(m_stagingPath, m_globalSettings, m_parent, m_sourceUrl.toString());
|
2022-01-31 19:55:36 +05:30
|
|
|
|
2022-07-08 05:40:41 +05:30
|
|
|
inst_creation_task->setName(m_instName);
|
|
|
|
inst_creation_task->setIcon(m_instIcon);
|
|
|
|
inst_creation_task->setGroup(m_instGroup);
|
2022-06-11 22:13:09 +05:30
|
|
|
|
2022-07-08 21:30:44 +05:30
|
|
|
connect(inst_creation_task, &Task::succeeded, this, [this, inst_creation_task] {
|
|
|
|
setOverride(inst_creation_task->shouldOverride());
|
|
|
|
emitSucceeded();
|
|
|
|
});
|
2022-07-08 05:40:41 +05:30
|
|
|
connect(inst_creation_task, &Task::failed, this, &InstanceImportTask::emitFailed);
|
|
|
|
connect(inst_creation_task, &Task::progress, this, &InstanceImportTask::setProgress);
|
|
|
|
connect(inst_creation_task, &Task::status, this, &InstanceImportTask::setStatus);
|
2022-07-08 21:30:44 +05:30
|
|
|
connect(inst_creation_task, &Task::finished, inst_creation_task, &InstanceCreationTask::deleteLater);
|
2022-07-08 05:40:41 +05:30
|
|
|
|
2022-07-08 21:30:44 +05:30
|
|
|
connect(this, &Task::aborted, inst_creation_task, &InstanceCreationTask::abort);
|
2022-07-08 05:40:41 +05:30
|
|
|
|
|
|
|
inst_creation_task->start();
|
2022-01-31 19:55:36 +05:30
|
|
|
}
|