2023-04-11 22:45:44 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-05-04 19:52:48 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
|
2023-04-11 22:45:44 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GetModDependenciesTask.h"
|
|
|
|
|
2023-04-14 21:18:37 +01:00
|
|
|
#include <QDebug>
|
2023-04-19 22:57:09 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include "Json.h"
|
2023-04-11 22:45:44 +01:00
|
|
|
#include "QObjectPtr.h"
|
|
|
|
#include "minecraft/mod/MetadataHandler.h"
|
|
|
|
#include "modplatform/ModIndex.h"
|
2023-06-19 19:20:35 +01:00
|
|
|
#include "modplatform/ResourceAPI.h"
|
2023-04-19 22:57:09 +01:00
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
|
|
|
#include "modplatform/modrinth/ModrinthAPI.h"
|
2023-04-11 22:45:44 +01:00
|
|
|
#include "tasks/ConcurrentTask.h"
|
|
|
|
#include "tasks/SequentialTask.h"
|
2023-04-19 22:57:09 +01:00
|
|
|
#include "ui/pages/modplatform/ModModel.h"
|
|
|
|
#include "ui/pages/modplatform/flame/FlameResourceModels.h"
|
|
|
|
#include "ui/pages/modplatform/modrinth/ModrinthResourceModels.h"
|
2023-04-11 22:45:44 +01:00
|
|
|
|
2023-06-18 22:36:37 +01:00
|
|
|
static Version mcVersion(BaseInstance* inst)
|
2023-04-11 22:45:44 +01:00
|
|
|
{
|
2023-04-19 22:57:09 +01:00
|
|
|
return static_cast<MinecraftInstance*>(inst)->getPackProfile()->getComponent("net.minecraft")->getVersion();
|
2023-04-11 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
2023-08-23 08:26:57 +01:00
|
|
|
static ModPlatform::ModLoaderTypes mcLoaders(BaseInstance* inst)
|
2023-04-11 22:45:44 +01:00
|
|
|
{
|
2023-04-19 22:57:09 +01:00
|
|
|
return static_cast<MinecraftInstance*>(inst)->getPackProfile()->getModLoaders().value();
|
2023-04-11 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
GetModDependenciesTask::GetModDependenciesTask(QObject* parent,
|
|
|
|
BaseInstance* instance,
|
|
|
|
ModFolderModel* folder,
|
2023-05-04 19:52:48 +01:00
|
|
|
QList<std::shared_ptr<PackDependency>> selected)
|
2023-06-24 13:59:55 +01:00
|
|
|
: SequentialTask(parent, tr("Get dependencies"))
|
2023-04-20 20:35:10 +01:00
|
|
|
, m_selected(selected)
|
|
|
|
, m_flame_provider{ ModPlatform::ResourceProvider::FLAME, std::make_shared<ResourceDownload::FlameModModel>(*instance),
|
|
|
|
std::make_shared<FlameAPI>() }
|
|
|
|
, m_modrinth_provider{ ModPlatform::ResourceProvider::MODRINTH, std::make_shared<ResourceDownload::ModrinthModModel>(*instance),
|
|
|
|
std::make_shared<ModrinthAPI>() }
|
2023-06-18 22:36:37 +01:00
|
|
|
, m_version(mcVersion(instance))
|
2023-04-20 20:35:10 +01:00
|
|
|
, m_loaderType(mcLoaders(instance))
|
2023-04-11 22:45:44 +01:00
|
|
|
{
|
2023-04-19 22:57:09 +01:00
|
|
|
for (auto mod : folder->allMods())
|
2023-05-14 20:13:53 +01:00
|
|
|
if (auto meta = mod->metadata(); meta)
|
|
|
|
m_mods.append(meta);
|
2023-04-19 22:57:09 +01:00
|
|
|
prepare();
|
2023-07-01 07:51:15 +01:00
|
|
|
}
|
2023-04-11 22:45:44 +01:00
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
void GetModDependenciesTask::prepare()
|
2023-04-11 22:45:44 +01:00
|
|
|
{
|
2023-04-19 22:57:09 +01:00
|
|
|
for (auto sel : m_selected) {
|
2023-05-28 16:22:55 +01:00
|
|
|
for (auto dep : getDependenciesForVersion(sel->version, sel->pack->provider)) {
|
|
|
|
addTask(prepareDependencyTask(dep, sel->pack->provider, 20));
|
2023-04-19 22:57:09 +01:00
|
|
|
}
|
2023-04-11 22:45:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 19:32:19 +01:00
|
|
|
ModPlatform::Dependency GetModDependenciesTask::getOverride(const ModPlatform::Dependency& dep,
|
|
|
|
const ModPlatform::ResourceProvider providerName)
|
2023-06-19 19:20:35 +01:00
|
|
|
{
|
2023-08-23 08:26:57 +01:00
|
|
|
if (auto isQuilt = m_loaderType & ModPlatform::Quilt; isQuilt || m_loaderType & ModPlatform::Fabric) {
|
2023-06-19 19:20:35 +01:00
|
|
|
auto overide = ModPlatform::getOverrideDeps();
|
|
|
|
auto over = std::find_if(overide.cbegin(), overide.cend(), [dep, providerName, isQuilt](auto o) {
|
|
|
|
return o.provider == providerName && dep.addonId == (isQuilt ? o.fabric : o.quilt);
|
|
|
|
});
|
|
|
|
if (over != overide.cend()) {
|
|
|
|
return { isQuilt ? over->quilt : over->fabric, dep.type };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dep;
|
|
|
|
}
|
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion(const ModPlatform::IndexedVersion& version,
|
|
|
|
const ModPlatform::ResourceProvider providerName)
|
2023-04-11 22:45:44 +01:00
|
|
|
{
|
2023-06-18 22:36:37 +01:00
|
|
|
QList<ModPlatform::Dependency> c_dependencies;
|
2023-04-19 22:57:09 +01:00
|
|
|
for (auto ver_dep : version.dependencies) {
|
2023-06-18 22:36:37 +01:00
|
|
|
if (ver_dep.type != ModPlatform::DependencyType::REQUIRED)
|
|
|
|
continue;
|
|
|
|
|
2023-06-19 15:42:16 +01:00
|
|
|
auto isOnlyVersion = providerName == ModPlatform::ResourceProvider::MODRINTH && ver_dep.addonId.toString().isEmpty();
|
2023-06-18 22:36:37 +01:00
|
|
|
if (auto dep = std::find_if(c_dependencies.begin(), c_dependencies.end(),
|
2023-06-19 15:42:16 +01:00
|
|
|
[&ver_dep, isOnlyVersion](const ModPlatform::Dependency& i) {
|
|
|
|
return isOnlyVersion ? i.version == ver_dep.version : i.addonId == ver_dep.addonId;
|
|
|
|
});
|
2023-06-18 22:36:37 +01:00
|
|
|
dep != c_dependencies.end())
|
|
|
|
continue; // check the current dependency list
|
|
|
|
|
|
|
|
if (auto dep = std::find_if(m_selected.begin(), m_selected.end(),
|
2023-06-19 15:42:16 +01:00
|
|
|
[&ver_dep, providerName, isOnlyVersion](std::shared_ptr<PackDependency> i) {
|
|
|
|
return i->pack->provider == providerName && (isOnlyVersion ? i->version.version == ver_dep.version
|
|
|
|
: i->pack->addonId == ver_dep.addonId);
|
2023-06-18 22:36:37 +01:00
|
|
|
});
|
|
|
|
dep != m_selected.end())
|
|
|
|
continue; // check the selected versions
|
|
|
|
|
|
|
|
if (auto dep = std::find_if(m_mods.begin(), m_mods.end(),
|
2023-06-19 15:42:16 +01:00
|
|
|
[&ver_dep, providerName, isOnlyVersion](std::shared_ptr<Metadata::ModStruct> i) {
|
|
|
|
return i->provider == providerName &&
|
|
|
|
(isOnlyVersion ? i->file_id == ver_dep.version : i->project_id == ver_dep.addonId);
|
2023-06-18 22:36:37 +01:00
|
|
|
});
|
|
|
|
dep != m_mods.end())
|
|
|
|
continue; // check the existing mods
|
|
|
|
|
|
|
|
if (auto dep = std::find_if(m_pack_dependencies.begin(), m_pack_dependencies.end(),
|
2023-06-19 15:42:16 +01:00
|
|
|
[&ver_dep, providerName, isOnlyVersion](std::shared_ptr<PackDependency> i) {
|
|
|
|
return i->pack->provider == providerName && (isOnlyVersion ? i->version.version == ver_dep.addonId
|
|
|
|
: i->pack->addonId == ver_dep.addonId);
|
2023-06-18 22:36:37 +01:00
|
|
|
});
|
|
|
|
dep != m_pack_dependencies.end()) // check loaded dependencies
|
|
|
|
continue;
|
|
|
|
|
2023-06-19 19:20:35 +01:00
|
|
|
c_dependencies.append(getOverride(ver_dep, providerName));
|
2023-04-11 22:45:44 +01:00
|
|
|
}
|
|
|
|
return c_dependencies;
|
2023-07-01 07:51:15 +01:00
|
|
|
}
|
2023-04-14 21:02:33 +01:00
|
|
|
|
2023-06-19 15:42:16 +01:00
|
|
|
Task::Ptr GetModDependenciesTask::getProjectInfoTask(std::shared_ptr<PackDependency> pDep)
|
2023-04-14 21:02:33 +01:00
|
|
|
{
|
2023-06-19 15:42:16 +01:00
|
|
|
auto provider = pDep->pack->provider == m_flame_provider.name ? m_flame_provider : m_modrinth_provider;
|
2023-06-23 18:01:17 +01:00
|
|
|
auto responseInfo = std::make_shared<QByteArray>();
|
2023-06-19 15:42:16 +01:00
|
|
|
auto info = provider.api->getProject(pDep->pack->addonId.toString(), responseInfo);
|
2023-04-19 22:57:09 +01:00
|
|
|
QObject::connect(info.get(), &NetJob::succeeded, [responseInfo, provider, pDep] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*responseInfo, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response for mod info at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
2023-06-19 12:10:29 +01:00
|
|
|
qDebug() << *responseInfo;
|
2023-04-19 22:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2023-04-20 20:35:10 +01:00
|
|
|
auto obj = provider.name == ModPlatform::ResourceProvider::FLAME ? Json::requireObject(Json::requireObject(doc), "data")
|
|
|
|
: Json::requireObject(doc);
|
2023-05-28 16:22:55 +01:00
|
|
|
provider.mod->loadIndexedPack(*pDep->pack, obj);
|
2023-04-19 22:57:09 +01:00
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading mod info: " << e.cause();
|
|
|
|
}
|
|
|
|
});
|
2023-06-19 15:42:16 +01:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task::Ptr GetModDependenciesTask::prepareDependencyTask(const ModPlatform::Dependency& dep,
|
|
|
|
const ModPlatform::ResourceProvider providerName,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
auto pDep = std::make_shared<PackDependency>();
|
|
|
|
pDep->dependency = dep;
|
|
|
|
pDep->pack = std::make_shared<ModPlatform::IndexedPack>();
|
|
|
|
pDep->pack->addonId = dep.addonId;
|
|
|
|
pDep->pack->provider = providerName;
|
2023-06-19 19:20:35 +01:00
|
|
|
|
2023-06-19 15:42:16 +01:00
|
|
|
m_pack_dependencies.append(pDep);
|
|
|
|
auto provider = providerName == m_flame_provider.name ? m_flame_provider : m_modrinth_provider;
|
|
|
|
|
|
|
|
auto tasks = makeShared<SequentialTask>(
|
|
|
|
this, QString("DependencyInfo: %1").arg(dep.addonId.toString().isEmpty() ? dep.version : dep.addonId.toString()));
|
|
|
|
|
|
|
|
if (!dep.addonId.toString().isEmpty()) {
|
|
|
|
tasks->addTask(getProjectInfoTask(pDep));
|
|
|
|
}
|
2023-04-19 22:57:09 +01:00
|
|
|
|
|
|
|
ResourceAPI::DependencySearchArgs args = { dep, m_version, m_loaderType };
|
|
|
|
ResourceAPI::DependencySearchCallbacks callbacks;
|
|
|
|
|
2023-07-01 07:51:15 +01:00
|
|
|
callbacks.on_succeed = [dep, provider, pDep, level, this](auto& doc, [[maybe_unused]] auto& pack) {
|
2023-04-19 22:57:09 +01:00
|
|
|
try {
|
|
|
|
QJsonArray arr;
|
|
|
|
if (dep.version.length() != 0 && doc.isObject()) {
|
|
|
|
arr.append(doc.object());
|
|
|
|
} else {
|
|
|
|
arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
|
|
|
}
|
2023-04-20 20:35:10 +01:00
|
|
|
pDep->version = provider.mod->loadDependencyVersions(dep, arr);
|
2023-04-19 22:57:09 +01:00
|
|
|
if (!pDep->version.addonId.isValid()) {
|
2023-08-23 08:26:57 +01:00
|
|
|
if (m_loaderType & ModPlatform::Quilt) { // falback for quilt
|
2023-06-20 11:28:57 +01:00
|
|
|
auto overide = ModPlatform::getOverrideDeps();
|
|
|
|
auto over = std::find_if(overide.cbegin(), overide.cend(),
|
|
|
|
[dep, provider](auto o) { return o.provider == provider.name && dep.addonId == o.quilt; });
|
|
|
|
if (over != overide.cend()) {
|
|
|
|
removePack(dep.addonId);
|
|
|
|
addTask(prepareDependencyTask({ over->fabric, dep.type }, provider.name, level));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 22:57:09 +01:00
|
|
|
qWarning() << "Error while reading mod version empty ";
|
|
|
|
qDebug() << doc;
|
|
|
|
return;
|
2023-04-14 21:02:33 +01:00
|
|
|
}
|
2023-04-19 22:57:09 +01:00
|
|
|
pDep->version.is_currently_selected = true;
|
2023-05-28 16:22:55 +01:00
|
|
|
pDep->pack->versions = { pDep->version };
|
|
|
|
pDep->pack->versionsLoaded = true;
|
2023-05-04 19:52:48 +01:00
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading mod version: " << e.cause();
|
|
|
|
return;
|
2023-04-14 21:02:33 +01:00
|
|
|
}
|
2023-04-19 22:57:09 +01:00
|
|
|
if (level == 0) {
|
2023-07-01 07:51:15 +01:00
|
|
|
qWarning() << "Dependency cycle exceeded";
|
2023-04-19 22:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-06-19 15:42:16 +01:00
|
|
|
if (dep.addonId.toString().isEmpty() && !pDep->version.addonId.toString().isEmpty()) {
|
|
|
|
pDep->pack->addonId = pDep->version.addonId;
|
2023-07-01 07:51:15 +01:00
|
|
|
auto dep_ = getOverride({ pDep->version.addonId, pDep->dependency.type }, provider.name);
|
|
|
|
if (dep_.addonId != pDep->version.addonId) {
|
2023-06-20 11:28:57 +01:00
|
|
|
removePack(pDep->version.addonId);
|
2023-07-01 07:51:15 +01:00
|
|
|
addTask(prepareDependencyTask(dep_, provider.name, level));
|
2023-06-19 19:20:35 +01:00
|
|
|
} else
|
|
|
|
addTask(getProjectInfoTask(pDep));
|
2023-06-19 15:42:16 +01:00
|
|
|
}
|
2023-07-01 07:51:15 +01:00
|
|
|
for (auto dep_ : getDependenciesForVersion(pDep->version, provider.name)) {
|
|
|
|
addTask(prepareDependencyTask(dep_, provider.name, level - 1));
|
2023-04-19 22:57:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-20 20:35:10 +01:00
|
|
|
auto version = provider.api->getDependencyVersion(std::move(args), std::move(callbacks));
|
2023-04-19 22:57:09 +01:00
|
|
|
tasks->addTask(version);
|
|
|
|
return tasks;
|
2023-07-01 07:51:15 +01:00
|
|
|
}
|
2023-06-20 11:28:57 +01:00
|
|
|
|
|
|
|
void GetModDependenciesTask::removePack(const QVariant addonId)
|
|
|
|
{
|
|
|
|
auto pred = [addonId](const std::shared_ptr<PackDependency>& v) { return v->pack->addonId == addonId; };
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
|
|
|
|
m_pack_dependencies.removeIf(pred);
|
|
|
|
#else
|
|
|
|
for (auto it = m_pack_dependencies.begin(); it != m_pack_dependencies.end();)
|
|
|
|
if (pred(*it))
|
|
|
|
it = m_pack_dependencies.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
#endif
|
|
|
|
}
|