2022-04-02 17:56:03 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2021-12-27 09:22:54 +05:30
|
|
|
* Copyright (c) 2021-2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-04-02 17:56:03 +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-04-02 17:56:03 +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-04-02 17:56:03 +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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SolderPackInstallTask.h"
|
|
|
|
|
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <Json.h>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#include <MMCZip.h>
|
2022-04-02 17:56:03 +05:30
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
#include "TechnicPackProcessor.h"
|
2022-04-02 17:56:03 +05:30
|
|
|
#include "SolderPackManifest.h"
|
2022-04-02 04:41:33 +05:30
|
|
|
#include "net/ChecksumValidator.h"
|
2020-06-07 21:16:12 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
Technic::SolderPackInstallTask::SolderPackInstallTask(
|
|
|
|
shared_qobject_ptr<QNetworkAccessManager> network,
|
2021-12-27 09:22:54 +05:30
|
|
|
const QUrl &solderUrl,
|
|
|
|
const QString &pack,
|
|
|
|
const QString &version,
|
2021-11-22 03:51:12 +05:30
|
|
|
const QString &minecraftVersion
|
|
|
|
) {
|
2021-12-27 09:22:54 +05:30
|
|
|
m_solderUrl = solderUrl;
|
|
|
|
m_pack = pack;
|
|
|
|
m_version = version;
|
2021-11-22 03:51:12 +05:30
|
|
|
m_network = network;
|
2021-12-27 09:22:54 +05:30
|
|
|
m_minecraftVersion = minecraftVersion;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
2021-07-06 19:42:39 +05:30
|
|
|
bool Technic::SolderPackInstallTask::abort() {
|
|
|
|
if(m_abortable)
|
|
|
|
{
|
|
|
|
return m_filesNetJob->abort();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
void Technic::SolderPackInstallTask::executeTask()
|
|
|
|
{
|
2022-04-02 17:56:03 +05:30
|
|
|
setStatus(tr("Resolving modpack files"));
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob = new NetJob(tr("Resolving modpack files"), m_network);
|
2021-12-27 09:22:54 +05:30
|
|
|
auto sourceUrl = QString("%1/modpack/%2/%3").arg(m_solderUrl.toString(), m_pack, m_version);
|
|
|
|
m_filesNetJob->addNetAction(Net::Download::makeByteArray(sourceUrl, &m_response));
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
auto job = m_filesNetJob.get();
|
|
|
|
connect(job, &NetJob::succeeded, this, &Technic::SolderPackInstallTask::fileListSucceeded);
|
|
|
|
connect(job, &NetJob::failed, this, &Technic::SolderPackInstallTask::downloadFailed);
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob->start();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::fileListSucceeded()
|
|
|
|
{
|
2022-04-02 17:56:03 +05:30
|
|
|
setStatus(tr("Downloading modpack"));
|
|
|
|
|
|
|
|
QJsonParseError parse_error {};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(m_response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Solder at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << m_response;
|
|
|
|
return;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
2022-04-02 17:56:03 +05:30
|
|
|
auto obj = doc.object();
|
|
|
|
|
|
|
|
TechnicSolder::PackBuild build;
|
|
|
|
try {
|
|
|
|
TechnicSolder::loadPackBuild(build, obj);
|
|
|
|
}
|
|
|
|
catch (const JSONValidationError& e) {
|
|
|
|
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
|
2020-06-07 21:16:12 +05:30
|
|
|
m_filesNetJob.reset();
|
|
|
|
return;
|
|
|
|
}
|
2022-04-02 17:56:03 +05:30
|
|
|
|
|
|
|
if (!build.minecraft.isEmpty())
|
|
|
|
m_minecraftVersion = build.minecraft;
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob = new NetJob(tr("Downloading modpack"), m_network);
|
2022-04-02 17:56:03 +05:30
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
int i = 0;
|
2021-12-27 09:22:54 +05:30
|
|
|
for (const auto &mod : build.mods) {
|
2020-10-14 01:31:01 +05:30
|
|
|
auto path = FS::PathCombine(m_outputDir.path(), QString("%1").arg(i));
|
2022-04-02 04:41:33 +05:30
|
|
|
|
|
|
|
auto dl = Net::Download::makeFile(mod.url, path);
|
|
|
|
if (!mod.md5.isEmpty()) {
|
|
|
|
auto rawMd5 = QByteArray::fromHex(mod.md5.toLatin1());
|
|
|
|
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Md5, rawMd5));
|
|
|
|
}
|
|
|
|
m_filesNetJob->addNetAction(dl);
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2022-04-02 17:56:03 +05:30
|
|
|
m_modCount = build.mods.size();
|
2020-06-07 21:16:12 +05:30
|
|
|
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::succeeded, this, &Technic::SolderPackInstallTask::downloadSucceeded);
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::progress, this, &Technic::SolderPackInstallTask::downloadProgressChanged);
|
|
|
|
connect(m_filesNetJob.get(), &NetJob::failed, this, &Technic::SolderPackInstallTask::downloadFailed);
|
2021-12-31 09:57:59 +05:30
|
|
|
m_filesNetJob->start();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::downloadSucceeded()
|
|
|
|
{
|
2021-07-06 19:42:39 +05:30
|
|
|
m_abortable = false;
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
setStatus(tr("Extracting modpack"));
|
|
|
|
m_filesNetJob.reset();
|
|
|
|
m_extractFuture = QtConcurrent::run([this]()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
QString extractDir = FS::PathCombine(m_stagingPath, ".minecraft");
|
|
|
|
FS::ensureFolderPathExists(extractDir);
|
|
|
|
|
|
|
|
while (m_modCount > i)
|
|
|
|
{
|
2020-10-14 01:31:01 +05:30
|
|
|
auto path = FS::PathCombine(m_outputDir.path(), QString("%1").arg(i));
|
2021-02-09 09:34:23 +05:30
|
|
|
if (!MMCZip::extractDir(path, extractDir))
|
2020-06-07 21:16:12 +05:30
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &Technic::SolderPackInstallTask::extractFinished);
|
|
|
|
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &Technic::SolderPackInstallTask::extractAborted);
|
|
|
|
m_extractFutureWatcher.setFuture(m_extractFuture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::downloadFailed(QString reason)
|
|
|
|
{
|
2021-07-06 19:42:39 +05:30
|
|
|
m_abortable = false;
|
2020-06-07 21:16:12 +05:30
|
|
|
emitFailed(reason);
|
|
|
|
m_filesNetJob.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::downloadProgressChanged(qint64 current, qint64 total)
|
|
|
|
{
|
2021-07-06 19:42:39 +05:30
|
|
|
m_abortable = true;
|
2020-06-07 21:16:12 +05:30
|
|
|
setProgress(current / 2, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::extractFinished()
|
|
|
|
{
|
|
|
|
if (!m_extractFuture.result())
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to extract modpack"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QDir extractDir(m_stagingPath);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_qobject_ptr<Technic::TechnicPackProcessor> packProcessor = new Technic::TechnicPackProcessor();
|
|
|
|
connect(packProcessor.get(), &Technic::TechnicPackProcessor::succeeded, this, &Technic::SolderPackInstallTask::emitSucceeded);
|
|
|
|
connect(packProcessor.get(), &Technic::TechnicPackProcessor::failed, this, &Technic::SolderPackInstallTask::emitFailed);
|
2022-07-15 00:43:23 +05:30
|
|
|
packProcessor->run(m_globalSettings, name(), m_instIcon, m_stagingPath, m_minecraftVersion, true);
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Technic::SolderPackInstallTask::extractAborted()
|
|
|
|
{
|
|
|
|
emitFailed(tr("Instance import has been aborted."));
|
|
|
|
}
|
|
|
|
|