Semi fixed the Modrinth dependency implementation

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-04-17 00:49:35 +03:00
parent bcea19b957
commit 7bd26ce468
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 25 additions and 15 deletions

View File

@ -69,7 +69,8 @@ void GetModDependenciesTask::prepareDependecies()
return;
}
for (auto dep : c_dependencies) {
auto task = m_getDependenciesVersionAPI(dep, [this](ModPlatform::IndexedVersion new_version) { addDependecies(new_version, 20); });
auto task =
m_getDependenciesVersionAPI(dep, [this](const ModPlatform::IndexedVersion& new_version) { addDependecies(new_version, 20); });
m_getNetworkDep->addTask(task);
}
m_getNetworkDep->start();
@ -89,7 +90,7 @@ void GetModDependenciesTask::addDependecies(const ModPlatform::IndexedVersion& n
}
for (auto dep : c_dependencies) {
auto task = m_getDependenciesVersionAPI(
dep, [this, level](ModPlatform::IndexedVersion new_versions) { addDependecies(new_versions, level - 1); });
dep, [this, level](const ModPlatform::IndexedVersion& new_versions) { addDependecies(new_versions, level - 1); });
m_getNetworkDep->addTask(task);
}
};
@ -101,13 +102,13 @@ QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion
for (auto ver_dep : version.dependencies) {
if (ver_dep.type == ModPlatform::DependencyType::REQUIRED) {
if (auto dep = std::find_if(c_dependencies.begin(), c_dependencies.end(),
[&ver_dep](auto i) { return i.addonId == ver_dep.addonId; });
[&ver_dep](const auto& i) { return i.addonId == ver_dep.addonId; });
dep == c_dependencies.end()) { // check the current dependency list
if (auto dep =
std::find_if(selected.begin(), selected.end(), [&ver_dep](auto i) { return i.addonId == ver_dep.addonId; });
if (auto dep = std::find_if(selected.begin(), selected.end(),
[&ver_dep](const auto& i) { return i.addonId == ver_dep.addonId; });
dep == selected.end()) { // check the selected versions
if (auto dep =
std::find_if(m_mods.begin(), m_mods.end(), [&ver_dep](auto i) { return i.mod_id() == ver_dep.addonId; });
if (auto dep = std::find_if(m_mods.begin(), m_mods.end(),
[&ver_dep](const auto& i) { return i.project_id == ver_dep.addonId; });
dep == m_mods.end()) { // check the existing mods
c_dependencies.append(ver_dep);
}
@ -124,10 +125,11 @@ QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion
auto c_dependencies = QList<ModPlatform::Dependency>();
for (auto ver_dep : version.dependencies) {
if (ver_dep.type == ModPlatform::DependencyType::REQUIRED) {
if (auto dep =
std::find_if(c_dependencies.begin(), c_dependencies.end(), [&ver_dep](auto i) { return i.addonId == ver_dep.addonId; });
if (auto dep = std::find_if(c_dependencies.begin(), c_dependencies.end(),
[&ver_dep](const auto& i) { return i.addonId == ver_dep.addonId; });
dep == c_dependencies.end()) { // check the current dependency list
if (auto dep = std::find_if(m_mods.begin(), m_mods.end(), [&ver_dep](auto i) { return i.mod_id() == ver_dep.addonId; });
if (auto dep =
std::find_if(m_mods.begin(), m_mods.end(), [&ver_dep](const auto& i) { return i.project_id == ver_dep.addonId; });
dep == m_mods.end()) { // check the existing mods
c_dependencies.append(ver_dep);
}

View File

@ -126,7 +126,7 @@ class ResourceAPI {
};
struct DependencySearchCallbacks {
std::function<void(QJsonDocument&, ModPlatform::Dependency)> on_succeed;
std::function<void(QJsonDocument&, const ModPlatform::Dependency&)> on_succeed;
};
public:

View File

@ -22,6 +22,7 @@
#include "Json.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "modplatform/ModIndex.h"
static ModrinthAPI api;
static ModPlatform::ProviderCapabilities ProviderCaps;
@ -144,7 +145,7 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject& obj, QString preferred_hash_t
auto dep = Json::ensureObject(d);
ModPlatform::Dependency dependency;
dependency.addonId = Json::requireString(dep, "project_id");
dependency.version = Json::requireString(dep, "version_id");
dependency.version = Json::ensureString(dep, "version_id");
auto depType = Json::requireString(dep, "dependency_type");
if (depType == "required")
@ -231,5 +232,5 @@ auto Modrinth::loadDependencyVersions(ModPlatform::Dependency m, QJsonArray& arr
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
return unsortedVersions.front();
return unsortedVersions.length() != 0 ? unsortedVersions.front() : ModPlatform::IndexedVersion();
}

View File

@ -459,11 +459,18 @@ QList<ModPlatform::IndexedVersion> ResourceModel::getDependecies(QDir& dir, QLis
// Use default if no callbacks are set
if (!callbacks.on_succeed)
callbacks.on_succeed = [this, dependency, succeeded](auto& doc, auto pack) {
callbacks.on_succeed = [this, dependency, succeeded](auto& doc, auto& pack) {
ModPlatform::IndexedVersion ver;
try {
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
auto arr = dependency.version.length() != 0 && doc.isObject()
? Json::toJsonArray(QList<QJsonObject>() << doc.object())
: doc.isObject() ? Json::ensureArray(doc.object(), "data")
: doc.array();
ver = loadDependencyVersions(dependency, arr);
if (!ver.addonId.isValid()) {
qWarning() << "Error while reading " << debugName() << " resource version empty ";
qDebug() << doc;
}
} catch (const JSONValidationError& e) {
qDebug() << doc;
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();