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-05-04 22:35:16 +03:00
commit 8b14b946e2
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
16 changed files with 133 additions and 129 deletions

View File

@ -27,8 +27,9 @@
ResourceDownloadTask::ResourceDownloadTask(ModPlatform::IndexedPack pack,
ModPlatform::IndexedVersion version,
const std::shared_ptr<ResourceFolderModel> packs,
bool is_indexed)
: m_pack(std::move(pack)), m_pack_version(std::move(version)), m_pack_model(packs)
bool is_indexed,
QString custom_target_folder)
: m_pack(std::move(pack)), m_pack_version(std::move(version)), m_pack_model(packs), m_custom_target_folder(custom_target_folder)
{
if (auto model = dynamic_cast<ModFolderModel*>(m_pack_model.get()); model && is_indexed) {
m_update_task.reset(new LocalModUpdateTask(model->indexDir(), m_pack, m_pack_version));
@ -40,13 +41,13 @@ ResourceDownloadTask::ResourceDownloadTask(ModPlatform::IndexedPack pack,
m_filesNetJob.reset(new NetJob(tr("Resource download"), APPLICATION->network()));
m_filesNetJob->setStatus(tr("Downloading resource:\n%1").arg(m_pack_version.downloadUrl));
QDir dir { m_pack_model->dir() };
QDir dir{ m_pack_model->dir() };
{
// FIXME: Make this more generic. May require adding additional info to IndexedVersion,
// or adquiring a reference to the base instance.
if (!m_pack_version.custom_target_folder.isEmpty()) {
if (!m_custom_target_folder.isEmpty()) {
dir.cdUp();
dir.cd(m_pack_version.custom_target_folder);
dir.cd(m_custom_target_folder);
}
}

View File

@ -33,18 +33,21 @@ class ResourceDownloadTask : public SequentialTask {
explicit ResourceDownloadTask(ModPlatform::IndexedPack pack,
ModPlatform::IndexedVersion version,
const std::shared_ptr<ResourceFolderModel> packs,
bool is_indexed = true);
bool is_indexed = true,
QString custom_target_folder = {});
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_custom_target_folder; }
const QVariant& getVersionID() const { return m_pack_version.fileId; }
const ModPlatform::IndexedVersion& getVersion() const { return m_pack_version; }
ModPlatform::IndexedPack& getPack() { return m_pack; }
const ModPlatform::ResourceProvider& getProvider() const { return m_pack.provider; }
const QString& getName() const { return m_pack.name; }
private:
ModPlatform::IndexedPack m_pack;
ModPlatform::IndexedVersion m_pack_version;
const std::shared_ptr<ResourceFolderModel> m_pack_model;
QString m_custom_target_folder;
NetJob::Ptr m_filesNetJob;
LocalModUpdateTask::Ptr m_update_task;

View File

@ -78,7 +78,6 @@ struct IndexedVersion {
// For internal use, not provided by APIs
bool is_currently_selected = false;
QString custom_target_folder;
QList<QVariant> required_by;
};
@ -106,7 +105,6 @@ 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;
@ -122,8 +120,6 @@ struct IndexedPack {
}
[[nodiscard]] bool isAnyVersionSelected() const
{
if (loadedFileId.isValid())
return true;
if (!versionsLoaded)
return false;

View File

@ -23,7 +23,6 @@
#include <QPushButton>
#include <algorithm>
#include <memory>
#include "Application.h"
#include "ResourceDownloadTask.h"
@ -126,13 +125,12 @@ void ResourceDownloadDialog::connectButtons()
static ModPlatform::ProviderCapabilities ProviderCaps;
QStringList ResourceDownloadDialog::getReqiredBy(QList<QVariant> req_by)
QStringList getReqiredBy(QList<ResourceDownloadDialog::DownloadTaskPtr> tasks, QList<QVariant> req_by)
{
auto req = QStringList();
auto keys = m_selected.keys();
for (auto r : req_by) {
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value()->getPack();
for (auto& task : tasks) {
auto selected = task->getPack();
if (selected.addonId == r) {
req.append(selected.name);
break;
@ -170,23 +168,25 @@ void ResourceDownloadDialog::confirm()
return;
} else
for (auto dep : task->getDependecies()) {
addResource(dep->pack, dep->version, true);
addResource(dep->pack, dep->version);
}
}
auto keys = m_selected.keys();
keys.sort(Qt::CaseInsensitive);
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value();
auto required_by = getReqiredBy(selected->getVersion().required_by);
confirm_dialog->appendResource(
{ task, selected->getFilename(), selected->getCustomPath(), ProviderCaps.name(selected->getProvider()), required_by });
auto selected = getTasks();
std::sort(selected.begin(), selected.end(), [](const DownloadTaskPtr& a, const DownloadTaskPtr& b) {
return QString::compare(a->getName(), b->getName(), Qt::CaseInsensitive) < 0;
});
for (auto& task : selected) {
confirm_dialog->appendResource({ task->getName(), task->getFilename(), task->getCustomPath(),
ProviderCaps.name(task->getProvider()), getReqiredBy(selected, task->getVersion().required_by) });
}
if (confirm_dialog->exec()) {
auto deselected = confirm_dialog->deselectedResources();
for (auto name : deselected) {
m_selected.remove(name);
for (auto page : m_container->getPages()) {
auto res = static_cast<ResourcePage*>(page);
for (auto name : deselected)
res->removeResourceFromPage(name);
}
this->accept();
@ -203,32 +203,39 @@ ResourcePage* ResourceDownloadDialog::getSelectedPage()
return m_selectedPage;
}
void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, bool is_indexed)
void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver)
{
removeResource(pack, ver);
ver.is_currently_selected = true;
m_selected.insert(pack.name, makeShared<ResourceDownloadTask>(pack, ver, getBaseModel(), is_indexed));
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(!m_selected.isEmpty());
removeResource(pack.name);
m_selectedPage->addResourceToPage(pack, ver, getBaseModel());
setButtonStatus();
}
void ResourceDownloadDialog::removeResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver)
void ResourceDownloadDialog::removeResource(const QString& pack_name)
{
dynamic_cast<ResourcePage*>(m_container->getPage(Modrinth::id()))->removeResourceFromPage(pack.name);
dynamic_cast<ResourcePage*>(m_container->getPage(Flame::id()))->removeResourceFromPage(pack.name);
for (auto page : m_container->getPages()) {
static_cast<ResourcePage*>(page)->removeResourceFromPage(pack_name);
}
setButtonStatus();
}
// Deselect the new version too, since all versions of that pack got removed.
ver.is_currently_selected = false;
m_selected.remove(pack.name);
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(!m_selected.isEmpty());
void ResourceDownloadDialog::setButtonStatus()
{
auto selected = false;
for (auto page : m_container->getPages()) {
auto res = static_cast<ResourcePage*>(page);
selected = selected || res->hasSelectedPacks();
}
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(selected);
}
const QList<ResourceDownloadDialog::DownloadTaskPtr> ResourceDownloadDialog::getTasks()
{
return m_selected.values();
QList<DownloadTaskPtr> selected;
for (auto page : m_container->getPages()) {
auto res = static_cast<ResourcePage*>(page);
selected.append(res->selectedPacks());
}
return selected;
}
void ResourceDownloadDialog::selectedPageChanged(BasePage* previous, BasePage* selected)
@ -277,10 +284,8 @@ QList<BasePage*> ModDownloadDialog::getPages()
GetModDependenciesTask::Ptr ModDownloadDialog::getModDependenciesTask()
{
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
auto keys = m_selected.keys();
QList<std::shared_ptr<GetModDependenciesTask::PackDependency>> selectedVers;
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value();
for (auto& selected : getTasks()) {
selectedVers.append(std::make_shared<GetModDependenciesTask::PackDependency>(selected->getPack(), selected->getVersion()));
}

View File

@ -63,8 +63,8 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
bool selectPage(QString pageId);
ResourcePage* getSelectedPage();
void addResource(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&, bool is_indexed = false);
void removeResource(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
void addResource(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
void removeResource(const QString&);
const QList<DownloadTaskPtr> getTasks();
[[nodiscard]] const std::shared_ptr<ResourceFolderModel> getBaseModel() const { return m_base_model; }
@ -80,11 +80,10 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
protected:
[[nodiscard]] virtual QString geometrySaveKey() const { return ""; }
void setButtonStatus();
[[nodiscard]] virtual GetModDependenciesTask::Ptr getModDependenciesTask() { return nullptr; }
QStringList getReqiredBy(QList<QVariant> req_by);
protected:
const std::shared_ptr<ResourceFolderModel> m_base_model;
@ -93,8 +92,6 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
QDialogButtonBox m_buttons;
QVBoxLayout m_vertical_layout;
QHash<QString, DownloadTaskPtr> m_selected;
};
class ModDownloadDialog final : public ResourceDownloadDialog {

View File

@ -6,23 +6,12 @@
#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)
{
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);
}
}
ModModel::ModModel(BaseInstance const& base_inst, ResourceAPI* api) : ResourceModel(api), m_base_instance(base_inst) {}
/******** Make data requests ********/
@ -35,7 +24,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

@ -145,11 +145,12 @@ void ModPage::updateVersionList()
updateSelectionButton();
}
void ModPage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
void ModPage::addResourceToPage(ModPlatform::IndexedPack& pack,
ModPlatform::IndexedVersion& version,
const std::shared_ptr<ResourceFolderModel> base_model)
{
bool is_indexed = !APPLICATION->settings()->get("ModMetadataDisabled").toBool();
m_model->addPack(pack);
m_parent_dialog->addResource(pack, version, is_indexed);
m_model->addPack(pack, version, base_model, is_indexed);
}
} // namespace ResourceDownload

View File

@ -50,7 +50,7 @@ class ModPage : public ResourcePage {
[[nodiscard]] QMap<QString, QString> urlHandlers() const override;
void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&) override;
void addResourceToPage(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&, const std::shared_ptr<ResourceFolderModel>) override;
virtual auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, std::optional<ResourceAPI::ModLoaderTypes> loaders = {}) const -> bool = 0;

View File

@ -337,15 +337,15 @@ 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](const ModPlatform::IndexedPack& i) { return i.provider == pack.provider && i.addonId == pack.addonId; });
if (auto sel = std::find_if(m_selected.begin(), m_selected.end(),
[&pack](const DownloadTaskPtr i) {
const auto ipack = i->getPack();
return ipack.provider == pack.provider && ipack.addonId == pack.addonId;
});
sel != m_selected.end()) {
pack.versionsLoaded = sel->versionsLoaded;
pack.versions = sel->versions;
pack.loadedFileId = sel->loadedFileId;
}
newList.append(pack);
newList.append(sel->get()->getPack());
} else
newList.append(pack);
} catch (const JSONValidationError& e) {
qWarning() << "Error while loading resource from " << debugName() << ": " << e.cause();
continue;
@ -408,12 +408,6 @@ 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](const 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();
@ -457,47 +451,35 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe
emit projectInfoUpdated();
}
void ResourceModel::removePack(QString& rem)
void ResourceModel::addPack(ModPlatform::IndexedPack& pack,
ModPlatform::IndexedVersion& version,
const std::shared_ptr<ResourceFolderModel> packs,
bool is_indexed,
QString custom_target_folder)
{
auto pred = [&rem](const ModPlatform::IndexedPack& i) { return rem == i.name; };
version.is_currently_selected = true;
m_selected.append(makeShared<ResourceDownloadTask>(pack, version, packs, is_indexed, custom_target_folder));
}
void ResourceModel::removePack(const QString& rem)
{
auto pred = [&rem](const DownloadTaskPtr i) { return rem == i->getName(); };
#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);
}
{
for (auto it = m_selected.begin(); it != m_selected.end();)
if (pred(*it))
it = m_selected.erase(it);
else
++it;
}
#endif
// m_selected.removeAt(qsizetype i)
auto pack = std::find_if(m_packs.begin(), m_packs.end(), [&rem](const 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)

View File

@ -10,6 +10,7 @@
#include "QObjectPtr.h"
#include "ResourceDownloadTask.h"
#include "modplatform/ResourceAPI.h"
#include "tasks/ConcurrentTask.h"
@ -29,6 +30,8 @@ class ResourceModel : public QAbstractListModel {
Q_PROPERTY(QString search_term MEMBER m_search_term WRITE setSearchTerm)
public:
using DownloadTaskPtr = shared_qobject_ptr<ResourceDownloadTask>;
ResourceModel(ResourceAPI* api);
~ResourceModel() override;
@ -80,8 +83,13 @@ 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);
void addPack(ModPlatform::IndexedPack& pack,
ModPlatform::IndexedVersion& version,
const std::shared_ptr<ResourceFolderModel> packs,
bool is_indexed = false,
QString custom_target_folder = {});
void removePack(const QString& rem);
QList<DownloadTaskPtr> selectedPacks() { return m_selected; }
protected:
/** Resets the model's data. */
@ -127,7 +135,7 @@ class ResourceModel : public QAbstractListModel {
QSet<QUrl> m_failed_icon_actions;
QList<ModPlatform::IndexedPack> m_packs;
QList<ModPlatform::IndexedPack> m_selected;
QList<DownloadTaskPtr> 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

@ -37,6 +37,7 @@
*/
#include "ResourcePage.h"
#include "modplatform/ModIndex.h"
#include "ui_ResourcePage.h"
#include <QDesktopServices>
@ -308,16 +309,22 @@ void ResourcePage::onVersionSelectionChanged(QString data)
void ResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
{
m_model->addPack(pack);
m_parent_dialog->addResource(pack, version);
}
void ResourcePage::removeResourceFromDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
void ResourcePage::removeResourceFromDialog(const QString& pack_name)
{
m_parent_dialog->removeResource(pack, version);
m_parent_dialog->removeResource(pack_name);
}
void ResourcePage::removeResourceFromPage(QString& name)
void ResourcePage::addResourceToPage(ModPlatform::IndexedPack& pack,
ModPlatform::IndexedVersion& ver,
const std::shared_ptr<ResourceFolderModel> base_model)
{
m_model->addPack(pack, ver, base_model);
}
void ResourcePage::removeResourceFromPage(const QString& name)
{
m_model->removePack(name);
}
@ -333,7 +340,7 @@ void ResourcePage::onResourceSelected()
auto& version = current_pack.versions[m_selected_version_index];
if (version.is_currently_selected)
removeResourceFromDialog(current_pack, version);
removeResourceFromDialog(current_pack.name);
else
addResourceToDialog(current_pack, version);

View File

@ -7,10 +7,12 @@
#include <QTimer>
#include <QWidget>
#include "ResourceDownloadTask.h"
#include "modplatform/ModIndex.h"
#include "modplatform/ResourceAPI.h"
#include "ui/pages/BasePage.h"
#include "ui/pages/modplatform/ResourceModel.h"
#include "ui/widgets/ProgressWidget.h"
namespace Ui {
@ -27,6 +29,7 @@ class ResourceModel;
class ResourcePage : public QWidget, public BasePage {
Q_OBJECT
public:
using DownloadTaskPtr = shared_qobject_ptr<ResourceDownloadTask>;
~ResourcePage() override;
/* Affects what the user sees */
@ -72,9 +75,13 @@ class ResourcePage : public QWidget, public BasePage {
virtual void updateSelectionButton();
virtual void updateVersionList();
virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
virtual void removeResourceFromPage(QString& name);
void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
void removeResourceFromDialog(const QString& pack_name);
virtual void removeResourceFromPage(const QString& name);
virtual void addResourceToPage(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&, const std::shared_ptr<ResourceFolderModel>);
QList<DownloadTaskPtr> selectedPacks() { return m_model->selectedPacks(); }
bool hasSelectedPacks() { return !(m_model->selectedPacks().isEmpty()); }
protected slots:
virtual void triggerSearch() {}

View File

@ -43,12 +43,14 @@ QMap<QString, QString> ShaderPackResourcePage::urlHandlers() const
return map;
}
void ShaderPackResourcePage::addResourceToDialog(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& version)
void ShaderPackResourcePage::addResourceToPage(ModPlatform::IndexedPack& pack,
ModPlatform::IndexedVersion& version,
const std::shared_ptr<ResourceFolderModel> base_model)
{
QString custom_target_folder;
if (version.loaders.contains(QStringLiteral("canvas")))
version.custom_target_folder = QStringLiteral("resourcepacks");
m_model->addPack(pack);
m_parent_dialog->addResource(pack, version);
custom_target_folder = QStringLiteral("resourcepacks");
m_model->addPack(pack, version, base_model, false, custom_target_folder);
}
} // namespace ResourceDownload

View File

@ -40,7 +40,7 @@ class ShaderPackResourcePage : public ResourcePage {
[[nodiscard]] bool supportsFiltering() const override { return false; };
void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&) override;
void addResourceToPage(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&, const std::shared_ptr<ResourceFolderModel>) override;
[[nodiscard]] QMap<QString, QString> urlHandlers() const override;

View File

@ -135,6 +135,11 @@ BasePage* PageContainer::getPage(QString pageId)
return m_model->findPageEntryById(pageId);
}
const QList<BasePage*> PageContainer::getPages() const
{
return m_model->pages();
}
void PageContainer::refreshContainer()
{
m_proxyModel->invalidate();

View File

@ -80,6 +80,7 @@ public:
virtual bool selectPage(QString pageId) override;
BasePage* getPage(QString pageId) override;
const QList<BasePage*> getPages() const;
void refreshContainer() override;
virtual void setParentContainer(BasePageContainer * container)