From f7931c2ee202025740caf424fa00ffb76743a1d2 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Sat, 22 Apr 2023 00:47:51 +0300 Subject: [PATCH] Better version handling Signed-off-by: Trial97 --- launcher/ResourceDownloadTask.h | 1 - launcher/modplatform/ModIndex.h | 37 ++++++++++--------- .../ui/dialogs/ResourceDownloadDialog.cpp | 20 +--------- launcher/ui/pages/modplatform/ModModel.cpp | 16 +++++++- launcher/ui/pages/modplatform/ModPage.cpp | 18 ++++----- .../ui/pages/modplatform/ResourceModel.cpp | 30 +++++++++++++++ launcher/ui/pages/modplatform/ResourceModel.h | 4 ++ .../ui/pages/modplatform/ResourcePage.cpp | 6 +++ launcher/ui/pages/modplatform/ResourcePage.h | 3 +- .../ui/pages/modplatform/ShaderPackPage.cpp | 8 ++-- 10 files changed, 90 insertions(+), 53 deletions(-) diff --git a/launcher/ResourceDownloadTask.h b/launcher/ResourceDownloadTask.h index 27c3c2ec2..0d90bd360 100644 --- a/launcher/ResourceDownloadTask.h +++ b/launcher/ResourceDownloadTask.h @@ -37,7 +37,6 @@ class ResourceDownloadTask : public SequentialTask { const QString& getFilename() const { return m_pack_version.fileName; } const QString& getCustomPath() const { return m_pack_version.custom_target_folder; } const QVariant& getVersionID() const { return m_pack_version.fileId; } - ModPlatform::IndexedPack& getPack() { return m_pack; } const ModPlatform::ResourceProvider& getProvider() const { return m_pack.provider; } private: diff --git a/launcher/modplatform/ModIndex.h b/launcher/modplatform/ModIndex.h index 40f1efc4e..de0af78c5 100644 --- a/launcher/modplatform/ModIndex.h +++ b/launcher/modplatform/ModIndex.h @@ -1,23 +1,24 @@ // SPDX-License-Identifier: GPL-3.0-only /* -* PolyMC - Minecraft Launcher -* Copyright (c) 2022 flowln -* -* 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 . -*/ + * PolyMC - Minecraft Launcher + * Copyright (c) 2022 flowln + * + * 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 . + */ #pragma once +#include #include #include #include @@ -95,6 +96,7 @@ struct IndexedPack { bool versionsLoaded = false; QVector versions; + QVariant loadedFileId; // to check for already downloaded mods // Don't load by default, since some modplatform don't have that info bool extraDataLoaded = true; @@ -110,11 +112,12 @@ struct IndexedPack { } [[nodiscard]] bool isAnyVersionSelected() const { + if (loadedFileId.isValid()) + return true; if (!versionsLoaded) return false; - return std::any_of(versions.constBegin(), versions.constEnd(), - [](auto const& v) { return v.is_currently_selected; }); + return std::any_of(versions.constBegin(), versions.constEnd(), [](auto const& v) { return v.is_currently_selected; }); } }; diff --git a/launcher/ui/dialogs/ResourceDownloadDialog.cpp b/launcher/ui/dialogs/ResourceDownloadDialog.cpp index 7fb4a6578..562dda33e 100644 --- a/launcher/ui/dialogs/ResourceDownloadDialog.cpp +++ b/launcher/ui/dialogs/ResourceDownloadDialog.cpp @@ -159,26 +159,10 @@ void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlat 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) { - if (auto selected_task_it = m_selected.find(pack.name); selected_task_it != m_selected.end()) { - auto selected_task = *selected_task_it; - 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; - } + dynamic_cast(m_container->getPage(Modrinth::id()))->removeResourceFromPage(pack.name); + dynamic_cast(m_container->getPage(Flame::id()))->removeResourceFromPage(pack.name); // Deselect the new version too, since all versions of that pack got removed. ver.is_currently_selected = false; diff --git a/launcher/ui/pages/modplatform/ModModel.cpp b/launcher/ui/pages/modplatform/ModModel.cpp index 3ffe6cb06..b69d2cf9e 100644 --- a/launcher/ui/pages/modplatform/ModModel.cpp +++ b/launcher/ui/pages/modplatform/ModModel.cpp @@ -6,12 +6,24 @@ #include "minecraft/MinecraftInstance.h" #include "minecraft/PackProfile.h" +#include "minecraft/mod/ModFolderModel.h" +#include "modplatform/ModIndex.h" #include 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(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 ********/ @@ -24,7 +36,7 @@ ResourceAPI::SearchArgs ModModel::createSearchArguments() std::optional> versions{}; - { // Version filter + { // Version filter if (!m_filter->versions.empty()) versions = m_filter->versions; } diff --git a/launcher/ui/pages/modplatform/ModPage.cpp b/launcher/ui/pages/modplatform/ModPage.cpp index 04be43ada..efff1ff40 100644 --- a/launcher/ui/pages/modplatform/ModPage.cpp +++ b/launcher/ui/pages/modplatform/ModPage.cpp @@ -55,8 +55,7 @@ namespace ResourceDownload { -ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance& instance) - : ResourcePage(dialog, instance) +ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance& instance) : ResourcePage(dialog, instance) { connect(m_ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch); connect(m_ui->resourceFilterButton, &QPushButton::clicked, this, &ModPage::filterMods); @@ -75,12 +74,10 @@ void ModPage::setFilterWidget(unique_qobject_ptr& widget) m_filter_widget->setInstance(&static_cast(m_base_instance)); m_filter = m_filter_widget->getFilter(); - connect(m_filter_widget.get(), &ModFilterWidget::filterChanged, this, [&]{ - m_ui->searchButton->setStyleSheet("text-decoration: underline"); - }); - connect(m_filter_widget.get(), &ModFilterWidget::filterUnchanged, this, [&]{ - m_ui->searchButton->setStyleSheet("text-decoration: none"); - }); + connect(m_filter_widget.get(), &ModFilterWidget::filterChanged, this, + [&] { m_ui->searchButton->setStyleSheet("text-decoration: underline"); }); + connect(m_filter_widget.get(), &ModFilterWidget::filterUnchanged, this, + [&] { m_ui->searchButton->setStyleSheet("text-decoration: none"); }); } /******** Callbacks to events in the UI (set up in the derived classes) ********/ @@ -128,8 +125,8 @@ void ModPage::updateVersionList() for (int i = 0; i < current_pack.versions.size(); i++) { auto version = current_pack.versions[i]; bool valid = false; - for(auto& mcVer : m_filter->versions){ - //NOTE: Flame doesn't care about loader, so passing it changes nothing. + for (auto& mcVer : m_filter->versions) { + // NOTE: Flame doesn't care about loader, so passing it changes nothing. if (validateVersion(version, mcVer.toString(), packProfile->getModLoaders())) { valid = true; break; @@ -151,6 +148,7 @@ void ModPage::updateVersionList() void ModPage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version) { bool is_indexed = !APPLICATION->settings()->get("ModMetadataDisabled").toBool(); + m_model->addPack(pack); m_parent_dialog->addResource(pack, version, is_indexed); } diff --git a/launcher/ui/pages/modplatform/ResourceModel.cpp b/launcher/ui/pages/modplatform/ResourceModel.cpp index db7d26f86..c7c34a7cb 100644 --- a/launcher/ui/pages/modplatform/ResourceModel.cpp +++ b/launcher/ui/pages/modplatform/ResourceModel.cpp @@ -3,12 +3,14 @@ // SPDX-License-Identifier: GPL-3.0-only #include "ResourceModel.h" +#include #include #include #include #include #include +#include #include "Application.h" #include "BuildConfig.h" @@ -335,6 +337,14 @@ void ResourceModel::searchRequestSucceeded(QJsonDocument& doc) ModPlatform::IndexedPack pack; try { 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); } catch (const JSONValidationError& e) { qWarning() << "Error while loading resource from " << debugName() << ": " << e.cause(); @@ -398,6 +408,11 @@ void ResourceModel::versionRequestSucceeded(QJsonDocument& doc, ModPlatform::Ind try { auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array(); 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) { qDebug() << doc; qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause(); @@ -441,4 +456,19 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe 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 diff --git a/launcher/ui/pages/modplatform/ResourceModel.h b/launcher/ui/pages/modplatform/ResourceModel.h index 46a02d6ef..5eb639011 100644 --- a/launcher/ui/pages/modplatform/ResourceModel.h +++ b/launcher/ui/pages/modplatform/ResourceModel.h @@ -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. */ std::optional getIcon(QModelIndex&, const QUrl&); + void addPack(ModPlatform::IndexedPack& add) { m_selected.append(add); } + void removePack(QString& rem); + protected: /** Resets the model's data. */ void clearData(); @@ -124,6 +127,7 @@ class ResourceModel : public QAbstractListModel { QSet m_failed_icon_actions; QList m_packs; + QList m_selected; // 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? diff --git a/launcher/ui/pages/modplatform/ResourcePage.cpp b/launcher/ui/pages/modplatform/ResourcePage.cpp index bbd465bc1..24347dbd4 100644 --- a/launcher/ui/pages/modplatform/ResourcePage.cpp +++ b/launcher/ui/pages/modplatform/ResourcePage.cpp @@ -308,6 +308,7 @@ void ResourcePage::onVersionSelectionChanged(QString data) void ResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version) { + m_model->addPack(pack); m_parent_dialog->addResource(pack, version); } @@ -316,6 +317,11 @@ void ResourcePage::removeResourceFromDialog(ModPlatform::IndexedPack& pack, ModP m_parent_dialog->removeResource(pack, version); } +void ResourcePage::removeResourceFromPage(QString& name) +{ + m_model->removePack(name); +} + void ResourcePage::onResourceSelected() { if (m_selected_version_index < 0) diff --git a/launcher/ui/pages/modplatform/ResourcePage.h b/launcher/ui/pages/modplatform/ResourcePage.h index 1896d53ea..a9db52e30 100644 --- a/launcher/ui/pages/modplatform/ResourcePage.h +++ b/launcher/ui/pages/modplatform/ResourcePage.h @@ -74,10 +74,11 @@ class ResourcePage : public QWidget, public BasePage { virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&); virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&); + virtual void removeResourceFromPage(QString& name); protected slots: virtual void triggerSearch() {} - + void onSelectionChanged(QModelIndex first, QModelIndex second); void onVersionSelectionChanged(QString data); void onResourceSelected(); diff --git a/launcher/ui/pages/modplatform/ShaderPackPage.cpp b/launcher/ui/pages/modplatform/ShaderPackPage.cpp index 251c07e71..1b9b2b83e 100644 --- a/launcher/ui/pages/modplatform/ShaderPackPage.cpp +++ b/launcher/ui/pages/modplatform/ShaderPackPage.cpp @@ -13,8 +13,7 @@ namespace ResourceDownload { -ShaderPackResourcePage::ShaderPackResourcePage(ShaderPackDownloadDialog* dialog, BaseInstance& instance) - : ResourcePage(dialog, instance) +ShaderPackResourcePage::ShaderPackResourcePage(ShaderPackDownloadDialog* dialog, BaseInstance& instance) : ResourcePage(dialog, instance) { connect(m_ui->searchButton, &QPushButton::clicked, this, &ShaderPackResourcePage::triggerSearch); connect(m_ui->packView, &QListView::doubleClicked, this, &ShaderPackResourcePage::onResourceSelected); @@ -38,7 +37,8 @@ QMap ShaderPackResourcePage::urlHandlers() const { QMap map; 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"); return map; } @@ -47,7 +47,7 @@ void ShaderPackResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, { if (version.loaders.contains(QStringLiteral("canvas"))) version.custom_target_folder = QStringLiteral("resourcepacks"); - + m_model->addPack(pack); m_parent_dialog->addResource(pack, version); }