Merge branch 'Fix_Assert' of github.com:Trial97/PrismLauncher into develop

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-04-22 01:32:33 +03:00
commit 1d167f8fda
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
9 changed files with 120 additions and 51 deletions

View File

@ -1,20 +1,20 @@
// 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/>.
*/
* 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
@ -104,6 +104,7 @@ struct IndexedPack {
bool versionsLoaded = false;
QVector<IndexedVersion> 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;
@ -119,11 +120,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; });
}
};

View File

@ -196,25 +196,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<ResourcePage*>(m_container->getPage(Modrinth::id()))->removeResourceFromPage(pack.name);
dynamic_cast<ResourcePage*>(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;

View File

@ -6,12 +6,23 @@
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/ModIndex.h"
#include <QMessageBox>
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;
addPack(pack);
}
}
/******** Make data requests ********/
@ -24,7 +35,7 @@ ResourceAPI::SearchArgs ModModel::createSearchArguments()
std::optional<std::list<Version>> versions{};
{ // Version filter
{ // Version filter
if (!m_filter->versions.empty())
versions = m_filter->versions;
}

View File

@ -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<ModFilterWidget>& widget)
m_filter_widget->setInstance(&static_cast<MinecraftInstance&>(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);
}

View File

@ -6,9 +6,11 @@
#include <QCryptographicHash>
#include <QIcon>
#include <QList>
#include <QMessageBox>
#include <QPixmapCache>
#include <QUrl>
#include <algorithm>
#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(),
[&current_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,51 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe
emit projectInfoUpdated();
}
void ResourceModel::removePack(QString& rem)
{
auto pred = [&rem](ModPlatform::IndexedPack i) { return rem == i.name; };
#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
m_selected.removeIf(pred);
#else
{ // well partial implementation of remove_if in case the QT Version is not high enough
const auto cbegin = m_selected.cbegin();
const auto cend = m_selected.cend();
const auto t_it = std::find_if(cbegin, cend, pred);
auto result = std::distance(cbegin, t_it);
if (result != m_selected.size()) {
// now detach:
const auto e = m_selected.end();
auto it = std::next(m_selected.begin(), result);
auto dest = it;
// Loop Invariants:
// - it != e
// - [next(it), e[ still to be checked
// - [c.begin(), dest[ are result
while (++it != e) {
if (!pred(*it)) {
*dest = std::move(*it);
++dest;
}
}
result = std::distance(dest, e);
m_selected.erase(dest, e);
}
}
#endif
// m_selected.removeAt(qsizetype i)
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

View File

@ -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<QIcon> 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<QUrl> m_failed_icon_actions;
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.
// This leaks a tiny bit of memory per time the user has opened a resource dialog. How to make this better?

View File

@ -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)

View File

@ -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();

View File

@ -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<QString, QString> ShaderPackResourcePage::urlHandlers() const
{
QMap<QString, QString> 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);
}