Filtering per mod loader & mc version

This commit is contained in:
timoreo 2022-01-15 08:51:47 +01:00
parent 2896f70cd8
commit 4b37c46889
No known key found for this signature in database
GPG Key ID: 121A72C3512BA288
6 changed files with 37 additions and 16 deletions

View File

@ -34,12 +34,19 @@ void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray
continue; continue;
} }
// pick the latest version supported // pick the latest version supported
file.mcVersion = versionArray[0].toString(); for(auto mcVer : versionArray){
file.mcVersion.append(mcVer.toString());
}
auto loaders = Json::requireArray(obj,"loaders");
for(auto loader : loaders){
file.loaders.append(loader.toString());
}
file.version = Json::requireString(obj, "name"); file.version = Json::requireString(obj, "name");
//TODO show all the files ? //TODO show all the files ?
auto parent = Json::requireArray(obj, "files")[0].toObject(); auto parent = Json::requireArray(obj, "files")[0].toObject();
file.downloadUrl = Json::requireString(parent, "url"); file.downloadUrl = Json::requireString(parent, "url");
file.fileName = Json::requireString(parent, "filename"); file.fileName = Json::requireString(parent, "filename");
unsortedVersions.append(file); unsortedVersions.append(file);
} }
auto orderSortPredicate = [](const IndexedVersion & a, const IndexedVersion & b) -> bool auto orderSortPredicate = [](const IndexedVersion & a, const IndexedVersion & b) -> bool

View File

@ -19,10 +19,11 @@ struct IndexedVersion {
QString addonId; QString addonId;
QString fileId; QString fileId;
QString version; QString version;
QString mcVersion; QVector<QString> mcVersion;
QString downloadUrl; QString downloadUrl;
QString date; QString date;
QString fileName; QString fileName;
QVector<QString> loaders;
}; };
struct IndexedPack struct IndexedPack

View File

@ -1,18 +1,19 @@
#include "ModrinthModel.h" #include "ModrinthModel.h"
#include "Application.h" #include "Application.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "ModrinthPage.h"
#include <Json.h> #include <Json.h>
#include <MMCStrings.h> #include <MMCStrings.h>
#include <Version.h> #include <Version.h>
#include <QtMath> #include <QtMath>
#include <QLabel>
#include <RWStorage.h>
namespace Modrinth { namespace Modrinth {
ListModel::ListModel(QObject *parent) : QAbstractListModel(parent) ListModel::ListModel(ModrinthPage *parent) : QAbstractListModel(parent)
{ {
} }
@ -158,14 +159,18 @@ const char* sorts[4]{"relevance","downloads","updated","newest"};
void ListModel::performPaginatedSearch() void ListModel::performPaginatedSearch()
{ {
NetJob *netJob = new NetJob("Modrinth::Search", APPLICATION->network());
QString mcVersion = ((MinecraftInstance *)((ModrinthPage *)parent())->m_instance)->getPackProfile()->getComponentVersion("net.minecraft");
bool hasFabric = !((MinecraftInstance *)((ModrinthPage *)parent())->m_instance)->getPackProfile()->getComponentVersion("net.fabricmc.fabric-loader").isEmpty();
auto netJob = new NetJob("Modrinth::Search", APPLICATION->network());
auto searchUrl = QString( auto searchUrl = QString(
"https://api.modrinth.com/api/v1/mod?" "https://api.modrinth.com/api/v1/mod?"
"offset=%1&" "offset=%1&"
"limit=25&" "limit=25&"
"query=%2&" "query=%2&"
"index=%3" "index=%3&"
).arg(nextSearchOffset).arg(currentSearchTerm).arg(sorts[currentSort]); "filters=categories=\"%4\" AND versions=\"%5\""
).arg(nextSearchOffset).arg(currentSearchTerm).arg(sorts[currentSort]).arg(hasFabric ? "fabric" : "forge").arg(mcVersion);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response)); netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
jobPtr = netJob; jobPtr = netJob;
jobPtr->start(); jobPtr->start();
@ -173,7 +178,7 @@ void ListModel::performPaginatedSearch()
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed); QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
} }
void ListModel::searchWithTerm(const QString& term, int sort) void ListModel::searchWithTerm(const QString &term, const int sort)
{ {
if(currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) { if(currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) {
return; return;

View File

@ -17,6 +17,8 @@
#include <modplatform/flame/FlamePackIndex.h> #include <modplatform/flame/FlamePackIndex.h>
#include "modplatform/modrinth/ModrinthPackIndex.h" #include "modplatform/modrinth/ModrinthPackIndex.h"
#include "BaseInstance.h"
#include "ModrinthPage.h"
namespace Modrinth { namespace Modrinth {
@ -29,7 +31,7 @@ class ListModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
public: public:
ListModel(QObject *parent); ListModel(ModrinthPage *parent);
virtual ~ListModel(); virtual ~ListModel();
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
@ -40,7 +42,7 @@ public:
void fetchMore(const QModelIndex & parent) override; void fetchMore(const QModelIndex & parent) override;
void getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback); void getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback);
void searchWithTerm(const QString & term, const int sort); void searchWithTerm(const QString &term, const int sort);
private slots: private slots:
void performPaginatedSearch(); void performPaginatedSearch();

View File

@ -9,11 +9,11 @@
#include "InstanceImportTask.h" #include "InstanceImportTask.h"
#include "ModrinthModel.h" #include "ModrinthModel.h"
#include "ModDownloadTask.h" #include "ModDownloadTask.h"
#include "ui/pages/instance/ModFolderPage.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
ModrinthPage::ModrinthPage(ModDownloadDialog *dialog, BaseInstance *instance) ModrinthPage::ModrinthPage(ModDownloadDialog *dialog, BaseInstance *instance)
: QWidget(dialog), ui(new Ui::ModrinthPage), dialog(dialog), m_instance(instance) : QWidget(dialog), m_instance(instance), ui(new Ui::ModrinthPage), dialog(dialog)
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->searchButton, &QPushButton::clicked, this, &ModrinthPage::triggerSearch); connect(ui->searchButton, &QPushButton::clicked, this, &ModrinthPage::triggerSearch);
@ -135,8 +135,13 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
qDebug() << *response; qDebug() << *response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause(); qWarning() << "Error while reading Modrinth mod version: " << e.cause();
} }
auto packProfile = ((MinecraftInstance *)m_instance)->getPackProfile();
for(auto version : current.versions) { QString mcVersion = packProfile->getComponentVersion("net.minecraft");
QString loaderString = (packProfile->getComponentVersion("net.minecraftforge").isEmpty()) ? "fabric" : "forge";
for(const auto& version : current.versions) {
if(!version.mcVersion.contains(mcVersion) || !version.loaders.contains(loaderString)){
continue;
}
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl)); ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
} }

View File

@ -47,6 +47,8 @@ public:
bool eventFilter(QObject * watched, QEvent * event) override; bool eventFilter(QObject * watched, QEvent * event) override;
BaseInstance *m_instance;
private: private:
void suggestCurrent(); void suggestCurrent();
@ -62,5 +64,4 @@ private:
Modrinth::IndexedPack current; Modrinth::IndexedPack current;
QString selectedVersion; QString selectedVersion;
BaseInstance *m_instance;
}; };