Refator task to work with multiple providers
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
fac33498db
commit
c1490cd627
@ -361,8 +361,6 @@ set(MINECRAFT_SOURCES
|
||||
minecraft/mod/tasks/LocalResourceParse.cpp
|
||||
minecraft/mod/tasks/GetModDependenciesTask.h
|
||||
minecraft/mod/tasks/GetModDependenciesTask.cpp
|
||||
minecraft/mod/tasks/LocalModGetAllTask.h
|
||||
minecraft/mod/tasks/LocalModGetAllTask.cpp
|
||||
|
||||
# Assets
|
||||
minecraft/AssetsUtils.h
|
||||
|
@ -38,6 +38,7 @@ class ResourceDownloadTask : public SequentialTask {
|
||||
const QString& getCustomPath() const { return m_pack_version.custom_target_folder; }
|
||||
const QVariant& getVersionID() const { return m_pack_version.fileId; }
|
||||
const ModPlatform::IndexedVersion& getVersion() const { return m_pack_version; }
|
||||
const ModPlatform::IndexedPack& getPack() const { return m_pack; }
|
||||
|
||||
private:
|
||||
ModPlatform::IndexedPack m_pack;
|
||||
|
@ -20,90 +20,74 @@
|
||||
#include "GetModDependenciesTask.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include "Json.h"
|
||||
#include "QObjectPtr.h"
|
||||
#include "minecraft/mod/MetadataHandler.h"
|
||||
#include "minecraft/mod/tasks/LocalModGetAllTask.h"
|
||||
#include "modplatform/ModIndex.h"
|
||||
#include "modplatform/flame/FlameAPI.h"
|
||||
#include "modplatform/modrinth/ModrinthAPI.h"
|
||||
#include "tasks/ConcurrentTask.h"
|
||||
#include "tasks/SequentialTask.h"
|
||||
#include "ui/pages/modplatform/ModModel.h"
|
||||
#include "ui/pages/modplatform/flame/FlameResourceModels.h"
|
||||
#include "ui/pages/modplatform/modrinth/ModrinthResourceModels.h"
|
||||
|
||||
GetModDependenciesTask::GetModDependenciesTask(QDir index_dir, QList<ModPlatform::IndexedVersion> selected, NewDependecyVersionAPITask api)
|
||||
: m_selected(selected), m_getDependenciesVersionAPI(api)
|
||||
static Version mcVersions(BaseInstance* inst)
|
||||
{
|
||||
m_getAllMods = makeShared<LocalModGetAllTask>(index_dir);
|
||||
m_getNetworkDep = makeShared<SequentialTask>(this, "GetDepInfo");
|
||||
connect(m_getNetworkDep.get(), &Task::finished, &loop, &QEventLoop::quit);
|
||||
QObject::connect(m_getAllMods.get(), &LocalModGetAllTask::getAllMods, [this](QList<Metadata::ModStruct> mods) {
|
||||
m_mods = mods;
|
||||
prepareDependecies();
|
||||
});
|
||||
return static_cast<MinecraftInstance*>(inst)->getPackProfile()->getComponent("net.minecraft")->getVersion();
|
||||
}
|
||||
|
||||
void GetModDependenciesTask::executeTask()
|
||||
static ResourceAPI::ModLoaderTypes mcLoaders(BaseInstance* inst)
|
||||
{
|
||||
setStatus(tr("Geting all mods"));
|
||||
m_getAllMods->start();
|
||||
loop.exec();
|
||||
emitSucceeded();
|
||||
return static_cast<MinecraftInstance*>(inst)->getPackProfile()->getModLoaders().value();
|
||||
}
|
||||
|
||||
auto GetModDependenciesTask::abort() -> bool
|
||||
GetModDependenciesTask::GetModDependenciesTask(QObject* parent,
|
||||
BaseInstance* instance,
|
||||
ModFolderModel* folder,
|
||||
QList<std::shared_ptr<PackDependecny>> selected)
|
||||
: SequentialTask(parent, "Get dependencies"), m_selected(selected), m_version(mcVersions(instance)), m_loaderType(mcLoaders(instance))
|
||||
{
|
||||
emitAborted();
|
||||
return true;
|
||||
}
|
||||
|
||||
void GetModDependenciesTask::prepareDependecies()
|
||||
{
|
||||
auto c_dependencies = getDependenciesForVersions(m_selected);
|
||||
if (c_dependencies.length() == 0) {
|
||||
m_getNetworkDep->start();
|
||||
return;
|
||||
}
|
||||
for (auto dep : c_dependencies) {
|
||||
auto task =
|
||||
m_getDependenciesVersionAPI(dep, [this](const ModPlatform::IndexedVersion& new_version) { addDependecies(new_version, 20); });
|
||||
m_getNetworkDep->addTask(task);
|
||||
}
|
||||
m_getNetworkDep->start();
|
||||
}
|
||||
|
||||
void GetModDependenciesTask::addDependecies(const ModPlatform::IndexedVersion& new_version, int level)
|
||||
{
|
||||
// some mutex?
|
||||
m_dependencies.append(new_version);
|
||||
auto c_dependencies = getDependenciesForVersion(new_version);
|
||||
if (c_dependencies.length() == 0) {
|
||||
return;
|
||||
}
|
||||
if (level == 0) {
|
||||
qWarning() << "Dependency cycle exeeded";
|
||||
return;
|
||||
}
|
||||
for (auto dep : c_dependencies) {
|
||||
auto task = m_getDependenciesVersionAPI(
|
||||
dep, [this, level](const ModPlatform::IndexedVersion& new_versions) { addDependecies(new_versions, level - 1); });
|
||||
m_getNetworkDep->addTask(task);
|
||||
}
|
||||
m_providers.append(Provider{ ModPlatform::ResourceProvider::FLAME, std::make_shared<ResourceDownload::FlameModModel>(*instance),
|
||||
std::make_shared<FlameAPI>() });
|
||||
m_providers.append(Provider{ ModPlatform::ResourceProvider::MODRINTH, std::make_shared<ResourceDownload::ModrinthModModel>(*instance),
|
||||
std::make_shared<ModrinthAPI>() });
|
||||
for (auto mod : folder->allMods())
|
||||
m_mods.append(mod->metadata());
|
||||
prepare();
|
||||
};
|
||||
|
||||
QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersions(const QList<ModPlatform::IndexedVersion>& selected)
|
||||
void GetModDependenciesTask::prepare()
|
||||
{
|
||||
for (auto sel : m_selected) {
|
||||
for (auto dep : getDependenciesForVersion(sel->version, sel->pack.provider)) {
|
||||
addTask(prepareDependencyTask(dep, sel->pack.provider, 20));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion(const ModPlatform::IndexedVersion& version,
|
||||
const ModPlatform::ResourceProvider providerName)
|
||||
{
|
||||
auto c_dependencies = QList<ModPlatform::Dependency>();
|
||||
for (auto version : selected) {
|
||||
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](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](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](const auto& i) { return i.project_id == ver_dep.addonId; });
|
||||
dep == m_mods.end()) { // check the existing mods
|
||||
c_dependencies.append(ver_dep);
|
||||
}
|
||||
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](const ModPlatform::Dependency& i) { return i.addonId == ver_dep.addonId; });
|
||||
dep == c_dependencies.end()) { // check the current dependency list
|
||||
if (auto dep = std::find_if(m_selected.begin(), m_selected.end(),
|
||||
[&ver_dep, providerName](std::shared_ptr<PackDependecny> i) {
|
||||
return i->pack.addonId == ver_dep.addonId && i->pack.provider == providerName;
|
||||
});
|
||||
dep == m_selected.end()) { // check the selected versions
|
||||
if (auto dep = std::find_if(m_mods.begin(), m_mods.end(),
|
||||
[&ver_dep, providerName](std::shared_ptr<Metadata::ModStruct> i) {
|
||||
return i->project_id == ver_dep.addonId && i->provider == providerName;
|
||||
});
|
||||
dep == m_mods.end()) { // check the existing mods
|
||||
c_dependencies.append(ver_dep);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -112,21 +96,80 @@ QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion
|
||||
return c_dependencies;
|
||||
};
|
||||
|
||||
QList<ModPlatform::Dependency> GetModDependenciesTask::getDependenciesForVersion(const ModPlatform::IndexedVersion& version)
|
||||
Task::Ptr GetModDependenciesTask::prepareDependencyTask(const ModPlatform::Dependency& dep,
|
||||
const ModPlatform::ResourceProvider providerName,
|
||||
int level)
|
||||
{
|
||||
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](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](const auto& i) { return i.project_id == ver_dep.addonId; });
|
||||
dep == m_mods.end()) { // check the existing mods
|
||||
c_dependencies.append(ver_dep);
|
||||
}
|
||||
}
|
||||
auto pDep = std::make_shared<PackDependecny>();
|
||||
pDep->dependency = dep;
|
||||
pDep->pack = { dep.addonId, providerName };
|
||||
m_pack_dependencies.append(pDep);
|
||||
auto provider =
|
||||
std::find_if(m_providers.begin(), m_providers.end(), [providerName](const Provider& p) { return p.name == providerName; });
|
||||
// if (provider == m_providers.end()) {
|
||||
// qWarning() << "Unsuported provider for dependency check";
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
auto tasks = makeShared<SequentialTask>(this, QString("DependencyInfo: %1").arg(dep.addonId.toString()));
|
||||
|
||||
auto responseInfo = new QByteArray();
|
||||
auto info = provider->api->getProject(dep.addonId.toString(), responseInfo);
|
||||
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();
|
||||
qWarning() << *responseInfo;
|
||||
return;
|
||||
}
|
||||
}
|
||||
return c_dependencies;
|
||||
};
|
||||
try {
|
||||
auto obj = provider->name == ModPlatform::ResourceProvider::FLAME ? Json::requireObject(Json::requireObject(doc), "data")
|
||||
: Json::requireObject(doc);
|
||||
provider->mod->loadIndexedPack(pDep->pack, obj);
|
||||
} catch (const JSONValidationError& e) {
|
||||
qDebug() << doc;
|
||||
qWarning() << "Error while reading mod info: " << e.cause();
|
||||
}
|
||||
});
|
||||
tasks->addTask(info);
|
||||
|
||||
ResourceAPI::DependencySearchArgs args = { dep, m_version, m_loaderType };
|
||||
ResourceAPI::DependencySearchCallbacks callbacks;
|
||||
|
||||
callbacks.on_succeed = [dep, provider, pDep, level, this](auto& doc, auto& pack) {
|
||||
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();
|
||||
}
|
||||
pDep->version = provider->mod->loadDependencyVersions(dep, arr);
|
||||
if (!pDep->version.addonId.isValid()) {
|
||||
qWarning() << "Error while reading mod version empty ";
|
||||
qDebug() << doc;
|
||||
return;
|
||||
}
|
||||
pDep->version.is_currently_selected = true;
|
||||
pDep->pack.versions = { pDep->version };
|
||||
pDep->pack.versionsLoaded = true;
|
||||
} catch (const JSONValidationError& e) {
|
||||
qDebug() << doc;
|
||||
qWarning() << "Error while reading mod version: " << e.cause();
|
||||
return;
|
||||
}
|
||||
if (level == 0) {
|
||||
qWarning() << "Dependency cycle exeeded";
|
||||
return;
|
||||
}
|
||||
for (auto dep : getDependenciesForVersion(pDep->version, provider->name)) {
|
||||
addTask(prepareDependencyTask(dep, provider->name, level - 1));
|
||||
}
|
||||
};
|
||||
|
||||
auto version = provider->api->getDependencyVersion(std::move(args), std::move(callbacks));
|
||||
tasks->addTask(version);
|
||||
return tasks;
|
||||
};
|
||||
|
@ -18,48 +18,64 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <qlist.h>
|
||||
#include <qvariant.h>
|
||||
#include <QDir>
|
||||
#include <QEventLoop>
|
||||
#include <QList>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "minecraft/mod/MetadataHandler.h"
|
||||
#include "minecraft/mod/tasks/LocalModGetAllTask.h"
|
||||
#include "minecraft/mod/ModFolderModel.h"
|
||||
#include "modplatform/ModIndex.h"
|
||||
#include "modplatform/ResourceAPI.h"
|
||||
#include "tasks/SequentialTask.h"
|
||||
#include "tasks/Task.h"
|
||||
#include "ui/pages/modplatform/ModModel.h"
|
||||
|
||||
class GetModDependenciesTask : public Task {
|
||||
class GetModDependenciesTask : public SequentialTask {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using Ptr = shared_qobject_ptr<GetModDependenciesTask>;
|
||||
using LocalModGetAllTaskPtr = shared_qobject_ptr<LocalModGetAllTask>;
|
||||
|
||||
using NewDependecyVersionAPITask =
|
||||
std::function<Task::Ptr(const ModPlatform::Dependency&, std::function<void(const ModPlatform::IndexedVersion&)>)>;
|
||||
struct PackDependecny {
|
||||
ModPlatform::Dependency dependency;
|
||||
ModPlatform::IndexedPack pack;
|
||||
ModPlatform::IndexedVersion version;
|
||||
PackDependecny(){};
|
||||
PackDependecny(const ModPlatform::IndexedPack& p, const ModPlatform::IndexedVersion& v)
|
||||
{
|
||||
pack = p;
|
||||
version = v;
|
||||
}
|
||||
};
|
||||
|
||||
explicit GetModDependenciesTask(QDir index_dir, QList<ModPlatform::IndexedVersion> selected, NewDependecyVersionAPITask api);
|
||||
struct Provider {
|
||||
ModPlatform::ResourceProvider name;
|
||||
std::shared_ptr<ResourceDownload::ModModel> mod;
|
||||
std::shared_ptr<ResourceAPI> api;
|
||||
};
|
||||
|
||||
auto canAbort() const -> bool override { return true; }
|
||||
auto abort() -> bool override;
|
||||
explicit GetModDependenciesTask(QObject* parent,
|
||||
BaseInstance* instance,
|
||||
ModFolderModel* folder,
|
||||
QList<std::shared_ptr<PackDependecny>> selected);
|
||||
|
||||
auto getDependecies() const -> QList<ModPlatform::IndexedVersion> { return m_dependencies; }
|
||||
auto getDependecies() const -> QList<std::shared_ptr<PackDependecny>> { return m_pack_dependencies; }
|
||||
|
||||
protected slots:
|
||||
//! Entry point for tasks.
|
||||
void executeTask() override;
|
||||
|
||||
void prepareDependecies();
|
||||
void addDependecies(const ModPlatform::IndexedVersion&, int);
|
||||
QList<ModPlatform::Dependency> getDependenciesForVersions(const QList<ModPlatform::IndexedVersion>&);
|
||||
QList<ModPlatform::Dependency> getDependenciesForVersion(const ModPlatform::IndexedVersion&);
|
||||
Task::Ptr prepareDependencyTask(const ModPlatform::Dependency&, const ModPlatform::ResourceProvider, int);
|
||||
QList<ModPlatform::Dependency> getDependenciesForVersion(const ModPlatform::IndexedVersion&,
|
||||
const ModPlatform::ResourceProvider providerName);
|
||||
void prepare();
|
||||
|
||||
private:
|
||||
QList<ModPlatform::IndexedVersion> m_selected;
|
||||
QList<ModPlatform::IndexedVersion> m_dependencies;
|
||||
QList<Metadata::ModStruct> m_mods;
|
||||
QList<std::shared_ptr<PackDependecny>> m_pack_dependencies;
|
||||
QList<std::shared_ptr<Metadata::ModStruct>> m_mods;
|
||||
QList<std::shared_ptr<PackDependecny>> m_selected;
|
||||
QList<Provider> m_providers;
|
||||
|
||||
LocalModGetAllTaskPtr m_getAllMods = nullptr;
|
||||
NewDependecyVersionAPITask m_getDependenciesVersionAPI;
|
||||
SequentialTask::Ptr m_getNetworkDep;
|
||||
QEventLoop loop;
|
||||
Version m_version;
|
||||
ResourceAPI::ModLoaderTypes m_loaderType;
|
||||
};
|
||||
|
@ -1,52 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
/*
|
||||
* PolyMC - Minecraft Launcher
|
||||
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
||||
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
||||
*
|
||||
* 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 "LocalModGetAllTask.h"
|
||||
|
||||
#include "FileSystem.h"
|
||||
#include "minecraft/mod/MetadataHandler.h"
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
LocalModGetAllTask::LocalModGetAllTask(QDir index_dir) : m_index_dir(index_dir)
|
||||
{
|
||||
// Ensure a '.index' folder exists in the mods folder, and create it if it does not
|
||||
if (!FS::ensureFolderPathExists(index_dir.path())) {
|
||||
emitFailed(QString("Unable to create index for all mods!"));
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
SetFileAttributesW(index_dir.path().toStdWString().c_str(), FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
|
||||
#endif
|
||||
}
|
||||
|
||||
void LocalModGetAllTask::executeTask()
|
||||
{
|
||||
setStatus(tr("Geting all mods"));
|
||||
emit getAllMods(Metadata::getAll(m_index_dir));
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
auto LocalModGetAllTask::abort() -> bool
|
||||
{
|
||||
emitAborted();
|
||||
return true;
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
/*
|
||||
* PolyMC - Minecraft Launcher
|
||||
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QDir>
|
||||
|
||||
#include "minecraft/mod/MetadataHandler.h"
|
||||
#include "tasks/Task.h"
|
||||
|
||||
class LocalModGetAllTask : public Task {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using Ptr = shared_qobject_ptr<LocalModGetAllTask>;
|
||||
|
||||
explicit LocalModGetAllTask(QDir index_dir);
|
||||
|
||||
auto canAbort() const -> bool override { return true; }
|
||||
auto abort() -> bool override;
|
||||
|
||||
protected slots:
|
||||
//! Entry point for tasks.
|
||||
void executeTask() override;
|
||||
|
||||
signals:
|
||||
void getAllMods(QList<Metadata::ModStruct>);
|
||||
|
||||
private:
|
||||
QDir m_index_dir;
|
||||
};
|
@ -23,8 +23,10 @@
|
||||
|
||||
#include <QPushButton>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include "Application.h"
|
||||
#include "QObjectPtr.h"
|
||||
#include "ResourceDownloadTask.h"
|
||||
|
||||
#include "minecraft/mod/ModFolderModel.h"
|
||||
@ -32,7 +34,10 @@
|
||||
#include "minecraft/mod/ShaderPackFolderModel.h"
|
||||
#include "minecraft/mod/TexturePackFolderModel.h"
|
||||
|
||||
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
|
||||
#include "modplatform/ModIndex.h"
|
||||
#include "ui/dialogs/CustomMessageBox.h"
|
||||
#include "ui/dialogs/ProgressDialog.h"
|
||||
#include "ui/dialogs/ReviewMessageBox.h"
|
||||
|
||||
#include "ui/pages/modplatform/ResourcePage.h"
|
||||
@ -122,30 +127,38 @@ void ResourceDownloadDialog::connectButtons()
|
||||
|
||||
void ResourceDownloadDialog::confirm()
|
||||
{
|
||||
auto keys = m_selected.keys();
|
||||
keys.sort(Qt::CaseInsensitive);
|
||||
|
||||
auto confirm_dialog = ReviewMessageBox::create(this, tr("Confirm %1 to download").arg(resourcesString()));
|
||||
confirm_dialog->retranslateUi(resourcesString());
|
||||
|
||||
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
|
||||
QList<ModPlatform::IndexedVersion> selectedVers;
|
||||
for (auto& task : keys) {
|
||||
auto selected = m_selected.constFind(task).value();
|
||||
selectedVers.append(selected->getVersion());
|
||||
}
|
||||
if (auto task = getModDependenciesTask(); task) {
|
||||
connect(task.get(), &Task::failed, this,
|
||||
[&](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); });
|
||||
|
||||
auto dir = model->indexDir();
|
||||
auto dependencies = m_selectedPage->getDependecies(dir, selectedVers);
|
||||
connect(task.get(), &Task::succeeded, this, [&]() {
|
||||
QStringList warnings = task->warnings();
|
||||
if (warnings.count()) {
|
||||
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->exec();
|
||||
}
|
||||
});
|
||||
|
||||
for (auto dep : dependencies) {
|
||||
dep.is_currently_selected = true;
|
||||
auto pack = ModPlatform::IndexedPack{ dep.addonId, ModPlatform::ResourceProvider::FLAME, dep.fileName, dep.fileName };
|
||||
m_selected.insert(dep.fileName, makeShared<ResourceDownloadTask>(pack, dep, getBaseModel(), true));
|
||||
}
|
||||
// Check for updates
|
||||
ProgressDialog progress_dialog(this);
|
||||
progress_dialog.setSkipButton(true, tr("Abort"));
|
||||
progress_dialog.setWindowTitle(tr("Checking for dependencies..."));
|
||||
auto ret = progress_dialog.execWithTask(task.get());
|
||||
|
||||
keys = m_selected.keys();
|
||||
// If the dialog was skipped / some download error happened
|
||||
if (ret == QDialog::DialogCode::Rejected) {
|
||||
QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection);
|
||||
return;
|
||||
} else
|
||||
for (auto dep : task->getDependecies()) {
|
||||
addResource(dep->pack, dep->version, true);
|
||||
}
|
||||
}
|
||||
|
||||
auto keys = m_selected.keys();
|
||||
keys.sort(Qt::CaseInsensitive);
|
||||
for (auto& task : keys) {
|
||||
auto selected = m_selected.constFind(task).value();
|
||||
confirm_dialog->appendResource({ task, selected->getFilename(), selected->getCustomPath() });
|
||||
@ -173,6 +186,7 @@ ResourcePage* ResourceDownloadDialog::getSelectedPage()
|
||||
|
||||
void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, bool is_indexed)
|
||||
{
|
||||
qWarning() << "DebugName: " << pack.name;
|
||||
removeResource(pack, ver);
|
||||
|
||||
ver.is_currently_selected = true;
|
||||
@ -256,6 +270,21 @@ QList<BasePage*> ModDownloadDialog::getPages()
|
||||
return pages;
|
||||
}
|
||||
|
||||
GetModDependenciesTask::Ptr ModDownloadDialog::getModDependenciesTask()
|
||||
{
|
||||
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
|
||||
auto keys = m_selected.keys();
|
||||
QList<std::shared_ptr<GetModDependenciesTask::PackDependecny>> selectedVers;
|
||||
for (auto& task : keys) {
|
||||
auto selected = m_selected.constFind(task).value();
|
||||
selectedVers.append(std::make_shared<GetModDependenciesTask::PackDependecny>(selected->getPack(), selected->getVersion()));
|
||||
}
|
||||
|
||||
return makeShared<GetModDependenciesTask>(this, m_instance, model, selectedVers);
|
||||
}
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
ResourcePackDownloadDialog::ResourcePackDownloadDialog(QWidget* parent,
|
||||
const std::shared_ptr<ResourcePackFolderModel>& resource_packs,
|
||||
BaseInstance* instance)
|
||||
|
@ -25,7 +25,9 @@
|
||||
#include <QLayout>
|
||||
|
||||
#include "QObjectPtr.h"
|
||||
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
|
||||
#include "modplatform/ModIndex.h"
|
||||
#include "tasks/Task.h"
|
||||
#include "ui/pages/BasePageProvider.h"
|
||||
|
||||
class BaseInstance;
|
||||
@ -80,6 +82,8 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
|
||||
protected:
|
||||
[[nodiscard]] virtual QString geometrySaveKey() const { return ""; }
|
||||
|
||||
[[nodiscard]] virtual GetModDependenciesTask::Ptr getModDependenciesTask() { return nullptr; }
|
||||
|
||||
protected:
|
||||
const std::shared_ptr<ResourceFolderModel> m_base_model;
|
||||
|
||||
@ -92,8 +96,6 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
|
||||
QHash<QString, DownloadTaskPtr> m_selected;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class ModDownloadDialog final : public ResourceDownloadDialog {
|
||||
Q_OBJECT
|
||||
|
||||
@ -106,6 +108,7 @@ class ModDownloadDialog final : public ResourceDownloadDialog {
|
||||
[[nodiscard]] QString geometrySaveKey() const override { return "ModDownloadGeometry"; }
|
||||
|
||||
QList<BasePage*> getPages() override;
|
||||
GetModDependenciesTask::Ptr getModDependenciesTask() override;
|
||||
|
||||
private:
|
||||
BaseInstance* m_instance;
|
||||
@ -135,8 +138,8 @@ class TexturePackDownloadDialog final : public ResourceDownloadDialog {
|
||||
|
||||
public:
|
||||
explicit TexturePackDownloadDialog(QWidget* parent,
|
||||
const std::shared_ptr<TexturePackFolderModel>& resource_packs,
|
||||
BaseInstance* instance);
|
||||
const std::shared_ptr<TexturePackFolderModel>& resource_packs,
|
||||
BaseInstance* instance);
|
||||
~TexturePackDownloadDialog() override = default;
|
||||
|
||||
//: String that gets appended to the texture pack download dialog title ("Download " + resourcesString())
|
||||
@ -153,9 +156,7 @@ class ShaderPackDownloadDialog final : public ResourceDownloadDialog {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ShaderPackDownloadDialog(QWidget* parent,
|
||||
const std::shared_ptr<ShaderPackFolderModel>& shader_packs,
|
||||
BaseInstance* instance);
|
||||
explicit ShaderPackDownloadDialog(QWidget* parent, const std::shared_ptr<ShaderPackFolderModel>& shader_packs, BaseInstance* instance);
|
||||
~ShaderPackDownloadDialog() override = default;
|
||||
|
||||
//: String that gets appended to the shader pack download dialog title ("Download " + resourcesString())
|
||||
|
@ -449,46 +449,4 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe
|
||||
emit projectInfoUpdated();
|
||||
}
|
||||
|
||||
QList<ModPlatform::IndexedVersion> ResourceModel::getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> selected)
|
||||
{
|
||||
auto task = std::make_unique<GetModDependenciesTask>(
|
||||
dir, selected,
|
||||
[this](const ModPlatform::Dependency& dependency, std::function<void(const ModPlatform::IndexedVersion&)> succeeded) -> Task::Ptr {
|
||||
auto args{ createDependecyArguments(dependency) };
|
||||
auto callbacks{ createDependecyCallbacks() };
|
||||
|
||||
// Use default if no callbacks are set
|
||||
if (!callbacks.on_succeed)
|
||||
callbacks.on_succeed = [this, dependency, succeeded](auto& doc, auto& pack) {
|
||||
ModPlatform::IndexedVersion ver;
|
||||
try {
|
||||
QJsonArray arr;
|
||||
if (dependency.version.length() != 0 && doc.isObject()) {
|
||||
arr.append(doc.object());
|
||||
} else {
|
||||
arr = 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;
|
||||
return;
|
||||
}
|
||||
} catch (const JSONValidationError& e) {
|
||||
qDebug() << doc;
|
||||
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();
|
||||
return;
|
||||
}
|
||||
|
||||
succeeded(ver);
|
||||
};
|
||||
|
||||
return m_api->getDependencyVersion(std::move(args), std::move(callbacks));
|
||||
});
|
||||
|
||||
task->start();
|
||||
|
||||
return task->getDependecies();
|
||||
};
|
||||
|
||||
} // namespace ResourceDownload
|
||||
|
@ -85,8 +85,6 @@ class ResourceModel : public QAbstractListModel {
|
||||
/** Gets the icon at the URL for the given index. If it's not fetched yet, fetch it and update when fisinhed. */
|
||||
std::optional<QIcon> getIcon(QModelIndex&, const QUrl&);
|
||||
|
||||
QList<ModPlatform::IndexedVersion> getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> m_selected);
|
||||
|
||||
protected:
|
||||
/** Resets the model's data. */
|
||||
void clearData();
|
||||
|
@ -405,9 +405,4 @@ void ResourcePage::openUrl(const QUrl& url)
|
||||
QDesktopServices::openUrl(url);
|
||||
}
|
||||
|
||||
QList<ModPlatform::IndexedVersion> ResourcePage::getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> selected)
|
||||
{
|
||||
return m_model->getDependecies(dir, selected);
|
||||
};
|
||||
|
||||
} // namespace ResourceDownload
|
||||
|
@ -75,8 +75,6 @@ class ResourcePage : public QWidget, public BasePage {
|
||||
virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
||||
virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
||||
|
||||
QList<ModPlatform::IndexedVersion> getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> m_selected);
|
||||
|
||||
protected slots:
|
||||
virtual void triggerSearch() {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user