Better version handling
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
10aac4fe17
commit
f7931c2ee2
@ -37,7 +37,6 @@ class ResourceDownloadTask : public SequentialTask {
|
|||||||
const QString& getFilename() const { return m_pack_version.fileName; }
|
const QString& getFilename() const { return m_pack_version.fileName; }
|
||||||
const QString& getCustomPath() const { return m_pack_version.custom_target_folder; }
|
const QString& getCustomPath() const { return m_pack_version.custom_target_folder; }
|
||||||
const QVariant& getVersionID() const { return m_pack_version.fileId; }
|
const QVariant& getVersionID() const { return m_pack_version.fileId; }
|
||||||
ModPlatform::IndexedPack& getPack() { return m_pack; }
|
|
||||||
const ModPlatform::ResourceProvider& getProvider() const { return m_pack.provider; }
|
const ModPlatform::ResourceProvider& getProvider() const { return m_pack.provider; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <qvariant.h>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
@ -95,6 +96,7 @@ struct IndexedPack {
|
|||||||
|
|
||||||
bool versionsLoaded = false;
|
bool versionsLoaded = false;
|
||||||
QVector<IndexedVersion> versions;
|
QVector<IndexedVersion> versions;
|
||||||
|
QVariant loadedFileId; // to check for already downloaded mods
|
||||||
|
|
||||||
// Don't load by default, since some modplatform don't have that info
|
// Don't load by default, since some modplatform don't have that info
|
||||||
bool extraDataLoaded = true;
|
bool extraDataLoaded = true;
|
||||||
@ -110,11 +112,12 @@ struct IndexedPack {
|
|||||||
}
|
}
|
||||||
[[nodiscard]] bool isAnyVersionSelected() const
|
[[nodiscard]] bool isAnyVersionSelected() const
|
||||||
{
|
{
|
||||||
|
if (loadedFileId.isValid())
|
||||||
|
return true;
|
||||||
if (!versionsLoaded)
|
if (!versionsLoaded)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return std::any_of(versions.constBegin(), versions.constEnd(),
|
return std::any_of(versions.constBegin(), versions.constEnd(), [](auto const& v) { return v.is_currently_selected; });
|
||||||
[](auto const& v) { return v.is_currently_selected; });
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -159,26 +159,10 @@ void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlat
|
|||||||
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(!m_selected.isEmpty());
|
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(!m_selected.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
static ModPlatform::IndexedVersion& getVersionWithID(ModPlatform::IndexedPack& pack, QVariant id)
|
|
||||||
{
|
|
||||||
Q_ASSERT(pack.versionsLoaded);
|
|
||||||
auto it = std::find_if(pack.versions.begin(), pack.versions.end(), [id](auto const& v) { return v.fileId == id; });
|
|
||||||
Q_ASSERT(it != pack.versions.end());
|
|
||||||
return *it;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResourceDownloadDialog::removeResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver)
|
void ResourceDownloadDialog::removeResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver)
|
||||||
{
|
{
|
||||||
if (auto selected_task_it = m_selected.find(pack.name); selected_task_it != m_selected.end()) {
|
dynamic_cast<ResourcePage*>(m_container->getPage(Modrinth::id()))->removeResourceFromPage(pack.name);
|
||||||
auto selected_task = *selected_task_it;
|
dynamic_cast<ResourcePage*>(m_container->getPage(Flame::id()))->removeResourceFromPage(pack.name);
|
||||||
auto old_version_id = selected_task->getVersionID();
|
|
||||||
|
|
||||||
if (selected_task->getProvider() != pack.provider) // If the pack name matches but they are different providers search for the
|
|
||||||
// old one(in the actual pack) and deselect it.
|
|
||||||
getVersionWithID(selected_task->getPack(), old_version_id).is_currently_selected = false;
|
|
||||||
else if (ver.fileId != old_version_id) // If the new and old version IDs don't match, search for the old one and deselect it.
|
|
||||||
getVersionWithID(pack, old_version_id).is_currently_selected = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deselect the new version too, since all versions of that pack got removed.
|
// Deselect the new version too, since all versions of that pack got removed.
|
||||||
ver.is_currently_selected = false;
|
ver.is_currently_selected = false;
|
||||||
|
@ -6,12 +6,24 @@
|
|||||||
|
|
||||||
#include "minecraft/MinecraftInstance.h"
|
#include "minecraft/MinecraftInstance.h"
|
||||||
#include "minecraft/PackProfile.h"
|
#include "minecraft/PackProfile.h"
|
||||||
|
#include "minecraft/mod/ModFolderModel.h"
|
||||||
|
#include "modplatform/ModIndex.h"
|
||||||
|
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
|
||||||
namespace ResourceDownload {
|
namespace ResourceDownload {
|
||||||
|
|
||||||
ModModel::ModModel(BaseInstance const& base_inst, ResourceAPI* api) : ResourceModel(api), m_base_instance(base_inst) {}
|
ModModel::ModModel(BaseInstance const& base_inst, ResourceAPI* api) : ResourceModel(api), m_base_instance(base_inst)
|
||||||
|
{
|
||||||
|
auto folder = static_cast<MinecraftInstance const&>(m_base_instance).loaderModList();
|
||||||
|
for (auto mod : folder->allMods()) {
|
||||||
|
auto meta = mod->metadata();
|
||||||
|
ModPlatform::IndexedPack pack{ meta->project_id, meta->provider, meta->name, meta->slug };
|
||||||
|
pack.loadedFileId = meta->file_id;
|
||||||
|
qWarning() << pack.loadedFileId;
|
||||||
|
addPack(pack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/******** Make data requests ********/
|
/******** Make data requests ********/
|
||||||
|
|
||||||
|
@ -55,8 +55,7 @@
|
|||||||
|
|
||||||
namespace ResourceDownload {
|
namespace ResourceDownload {
|
||||||
|
|
||||||
ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance& instance)
|
ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance& instance) : ResourcePage(dialog, instance)
|
||||||
: ResourcePage(dialog, instance)
|
|
||||||
{
|
{
|
||||||
connect(m_ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch);
|
connect(m_ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch);
|
||||||
connect(m_ui->resourceFilterButton, &QPushButton::clicked, this, &ModPage::filterMods);
|
connect(m_ui->resourceFilterButton, &QPushButton::clicked, this, &ModPage::filterMods);
|
||||||
@ -75,12 +74,10 @@ void ModPage::setFilterWidget(unique_qobject_ptr<ModFilterWidget>& widget)
|
|||||||
m_filter_widget->setInstance(&static_cast<MinecraftInstance&>(m_base_instance));
|
m_filter_widget->setInstance(&static_cast<MinecraftInstance&>(m_base_instance));
|
||||||
m_filter = m_filter_widget->getFilter();
|
m_filter = m_filter_widget->getFilter();
|
||||||
|
|
||||||
connect(m_filter_widget.get(), &ModFilterWidget::filterChanged, this, [&]{
|
connect(m_filter_widget.get(), &ModFilterWidget::filterChanged, this,
|
||||||
m_ui->searchButton->setStyleSheet("text-decoration: underline");
|
[&] { m_ui->searchButton->setStyleSheet("text-decoration: underline"); });
|
||||||
});
|
connect(m_filter_widget.get(), &ModFilterWidget::filterUnchanged, this,
|
||||||
connect(m_filter_widget.get(), &ModFilterWidget::filterUnchanged, this, [&]{
|
[&] { m_ui->searchButton->setStyleSheet("text-decoration: none"); });
|
||||||
m_ui->searchButton->setStyleSheet("text-decoration: none");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/******** Callbacks to events in the UI (set up in the derived classes) ********/
|
/******** Callbacks to events in the UI (set up in the derived classes) ********/
|
||||||
@ -151,6 +148,7 @@ void ModPage::updateVersionList()
|
|||||||
void ModPage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
|
void ModPage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
|
||||||
{
|
{
|
||||||
bool is_indexed = !APPLICATION->settings()->get("ModMetadataDisabled").toBool();
|
bool is_indexed = !APPLICATION->settings()->get("ModMetadataDisabled").toBool();
|
||||||
|
m_model->addPack(pack);
|
||||||
m_parent_dialog->addResource(pack, version, is_indexed);
|
m_parent_dialog->addResource(pack, version, is_indexed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,12 +3,14 @@
|
|||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
#include "ResourceModel.h"
|
#include "ResourceModel.h"
|
||||||
|
#include <qlist.h>
|
||||||
|
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QPixmapCache>
|
#include <QPixmapCache>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
#include "BuildConfig.h"
|
#include "BuildConfig.h"
|
||||||
@ -335,6 +337,14 @@ void ResourceModel::searchRequestSucceeded(QJsonDocument& doc)
|
|||||||
ModPlatform::IndexedPack pack;
|
ModPlatform::IndexedPack pack;
|
||||||
try {
|
try {
|
||||||
loadIndexedPack(pack, packObj);
|
loadIndexedPack(pack, packObj);
|
||||||
|
if (auto sel =
|
||||||
|
std::find_if(m_selected.begin(), m_selected.end(),
|
||||||
|
[&pack](ModPlatform::IndexedPack& i) { return i.provider == pack.provider && i.addonId == pack.addonId; });
|
||||||
|
sel != m_selected.end()) {
|
||||||
|
pack.versionsLoaded = sel->versionsLoaded;
|
||||||
|
pack.versions = sel->versions;
|
||||||
|
pack.loadedFileId = sel->loadedFileId;
|
||||||
|
}
|
||||||
newList.append(pack);
|
newList.append(pack);
|
||||||
} catch (const JSONValidationError& e) {
|
} catch (const JSONValidationError& e) {
|
||||||
qWarning() << "Error while loading resource from " << debugName() << ": " << e.cause();
|
qWarning() << "Error while loading resource from " << debugName() << ": " << e.cause();
|
||||||
@ -398,6 +408,11 @@ void ResourceModel::versionRequestSucceeded(QJsonDocument& doc, ModPlatform::Ind
|
|||||||
try {
|
try {
|
||||||
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
||||||
loadIndexedPackVersions(current_pack, arr);
|
loadIndexedPackVersions(current_pack, arr);
|
||||||
|
if (current_pack.loadedFileId.isValid())
|
||||||
|
if (auto ver = std::find_if(current_pack.versions.begin(), current_pack.versions.end(),
|
||||||
|
[¤t_pack](ModPlatform::IndexedVersion v) { return v.fileId == current_pack.loadedFileId; });
|
||||||
|
ver != current_pack.versions.end())
|
||||||
|
ver->is_currently_selected = true;
|
||||||
} catch (const JSONValidationError& e) {
|
} catch (const JSONValidationError& e) {
|
||||||
qDebug() << doc;
|
qDebug() << doc;
|
||||||
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();
|
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();
|
||||||
@ -441,4 +456,19 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe
|
|||||||
emit projectInfoUpdated();
|
emit projectInfoUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ResourceModel::removePack(QString& rem)
|
||||||
|
{
|
||||||
|
m_selected.removeIf([&rem](ModPlatform::IndexedPack i) { return rem == i.name; });
|
||||||
|
auto pack = std::find_if(m_packs.begin(), m_packs.end(), [&rem](ModPlatform::IndexedPack i) { return rem == i.name; });
|
||||||
|
if (pack == m_packs.end()) { // ignore it if is not in the current search
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!pack->versionsLoaded) {
|
||||||
|
pack->loadedFileId = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (auto& ver : pack->versions)
|
||||||
|
ver.is_currently_selected = false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ResourceDownload
|
} // namespace ResourceDownload
|
||||||
|
@ -80,6 +80,9 @@ 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. */
|
/** 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&);
|
std::optional<QIcon> getIcon(QModelIndex&, const QUrl&);
|
||||||
|
|
||||||
|
void addPack(ModPlatform::IndexedPack& add) { m_selected.append(add); }
|
||||||
|
void removePack(QString& rem);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** Resets the model's data. */
|
/** Resets the model's data. */
|
||||||
void clearData();
|
void clearData();
|
||||||
@ -124,6 +127,7 @@ class ResourceModel : public QAbstractListModel {
|
|||||||
QSet<QUrl> m_failed_icon_actions;
|
QSet<QUrl> m_failed_icon_actions;
|
||||||
|
|
||||||
QList<ModPlatform::IndexedPack> m_packs;
|
QList<ModPlatform::IndexedPack> m_packs;
|
||||||
|
QList<ModPlatform::IndexedPack> m_selected;
|
||||||
|
|
||||||
// HACK: We need this to prevent callbacks from calling the model after it has already been deleted.
|
// HACK: We need this to prevent callbacks from calling the model after it has already been deleted.
|
||||||
// This leaks a tiny bit of memory per time the user has opened a resource dialog. How to make this better?
|
// This leaks a tiny bit of memory per time the user has opened a resource dialog. How to make this better?
|
||||||
|
@ -308,6 +308,7 @@ void ResourcePage::onVersionSelectionChanged(QString data)
|
|||||||
|
|
||||||
void ResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
|
void ResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
|
||||||
{
|
{
|
||||||
|
m_model->addPack(pack);
|
||||||
m_parent_dialog->addResource(pack, version);
|
m_parent_dialog->addResource(pack, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,6 +317,11 @@ void ResourcePage::removeResourceFromDialog(ModPlatform::IndexedPack& pack, ModP
|
|||||||
m_parent_dialog->removeResource(pack, version);
|
m_parent_dialog->removeResource(pack, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ResourcePage::removeResourceFromPage(QString& name)
|
||||||
|
{
|
||||||
|
m_model->removePack(name);
|
||||||
|
}
|
||||||
|
|
||||||
void ResourcePage::onResourceSelected()
|
void ResourcePage::onResourceSelected()
|
||||||
{
|
{
|
||||||
if (m_selected_version_index < 0)
|
if (m_selected_version_index < 0)
|
||||||
|
@ -74,6 +74,7 @@ class ResourcePage : public QWidget, public BasePage {
|
|||||||
|
|
||||||
virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
||||||
virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
|
||||||
|
virtual void removeResourceFromPage(QString& name);
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
virtual void triggerSearch() {}
|
virtual void triggerSearch() {}
|
||||||
|
@ -13,8 +13,7 @@
|
|||||||
|
|
||||||
namespace ResourceDownload {
|
namespace ResourceDownload {
|
||||||
|
|
||||||
ShaderPackResourcePage::ShaderPackResourcePage(ShaderPackDownloadDialog* dialog, BaseInstance& instance)
|
ShaderPackResourcePage::ShaderPackResourcePage(ShaderPackDownloadDialog* dialog, BaseInstance& instance) : ResourcePage(dialog, instance)
|
||||||
: ResourcePage(dialog, instance)
|
|
||||||
{
|
{
|
||||||
connect(m_ui->searchButton, &QPushButton::clicked, this, &ShaderPackResourcePage::triggerSearch);
|
connect(m_ui->searchButton, &QPushButton::clicked, this, &ShaderPackResourcePage::triggerSearch);
|
||||||
connect(m_ui->packView, &QListView::doubleClicked, this, &ShaderPackResourcePage::onResourceSelected);
|
connect(m_ui->packView, &QListView::doubleClicked, this, &ShaderPackResourcePage::onResourceSelected);
|
||||||
@ -38,7 +37,8 @@ QMap<QString, QString> ShaderPackResourcePage::urlHandlers() const
|
|||||||
{
|
{
|
||||||
QMap<QString, QString> map;
|
QMap<QString, QString> map;
|
||||||
map.insert(QRegularExpression::anchoredPattern("(?:www\\.)?modrinth\\.com\\/shaders\\/([^\\/]+)\\/?"), "modrinth");
|
map.insert(QRegularExpression::anchoredPattern("(?:www\\.)?modrinth\\.com\\/shaders\\/([^\\/]+)\\/?"), "modrinth");
|
||||||
map.insert(QRegularExpression::anchoredPattern("(?:www\\.)?curseforge\\.com\\/minecraft\\/customization\\/([^\\/]+)\\/?"), "curseforge");
|
map.insert(QRegularExpression::anchoredPattern("(?:www\\.)?curseforge\\.com\\/minecraft\\/customization\\/([^\\/]+)\\/?"),
|
||||||
|
"curseforge");
|
||||||
map.insert(QRegularExpression::anchoredPattern("minecraft\\.curseforge\\.com\\/projects\\/([^\\/]+)\\/?"), "curseforge");
|
map.insert(QRegularExpression::anchoredPattern("minecraft\\.curseforge\\.com\\/projects\\/([^\\/]+)\\/?"), "curseforge");
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ void ShaderPackResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack,
|
|||||||
{
|
{
|
||||||
if (version.loaders.contains(QStringLiteral("canvas")))
|
if (version.loaders.contains(QStringLiteral("canvas")))
|
||||||
version.custom_target_folder = QStringLiteral("resourcepacks");
|
version.custom_target_folder = QStringLiteral("resourcepacks");
|
||||||
|
m_model->addPack(pack);
|
||||||
m_parent_dialog->addResource(pack, version);
|
m_parent_dialog->addResource(pack, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user