From 39bd04f06ff42623f7349096d707c4a877fc7cd7 Mon Sep 17 00:00:00 2001 From: flow Date: Sun, 6 Mar 2022 16:45:39 -0300 Subject: [PATCH] refactor: use Enum instead of raw int for ModLoaderType --- launcher/modplatform/ModAPI.h | 11 +++++++- launcher/modplatform/flame/FlameAPI.h | 5 ++-- launcher/modplatform/modrinth/ModrinthAPI.h | 29 +++++++++++++++++++-- launcher/ui/pages/modplatform/ModModel.cpp | 3 ++- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/launcher/modplatform/ModAPI.h b/launcher/modplatform/ModAPI.h index e60fa8e0..4d22a63d 100644 --- a/launcher/modplatform/ModAPI.h +++ b/launcher/modplatform/ModAPI.h @@ -6,7 +6,16 @@ class ModAPI { public: virtual ~ModAPI() = default; - inline virtual QString getModSearchURL(int, QString, QString, bool, QString) const { return ""; }; + // https://docs.curseforge.com/?http#tocS_ModLoaderType + enum ModLoaderType { + Any = 0, + Forge = 1, + Cauldron = 2, + LiteLoader = 3, + Fabric = 4 + }; + + inline virtual QString getModSearchURL(int, QString, QString, ModLoaderType, QString) const { return ""; }; inline virtual QString getVersionsURL(const QString& addonId) const { return ""; }; inline virtual QString getAuthorURL(const QString& name) const { return ""; }; }; diff --git a/launcher/modplatform/flame/FlameAPI.h b/launcher/modplatform/flame/FlameAPI.h index 6e2b9e25..b49aeb24 100644 --- a/launcher/modplatform/flame/FlameAPI.h +++ b/launcher/modplatform/flame/FlameAPI.h @@ -4,7 +4,8 @@ class FlameAPI : public ModAPI { public: - inline QString getModSearchURL(int index, QString searchFilter, QString sort, bool fabricCompatible, QString version) const override + + inline QString getModSearchURL(int index, QString searchFilter, QString sort, ModLoaderType modLoader, QString version) const override { return QString("https://addons-ecs.forgesvc.net/api/v2/addon/search?" "gameId=432&" "categoryId=0&" "sectionId=6&" @@ -14,7 +15,7 @@ class FlameAPI : public ModAPI { .arg(index) .arg(searchFilter) .arg(sort) - .arg(fabricCompatible ? 4 : 1) // Enum: https://docs.curseforge.com/?http#tocS_ModLoaderType + .arg(modLoader) .arg(version); }; diff --git a/launcher/modplatform/modrinth/ModrinthAPI.h b/launcher/modplatform/modrinth/ModrinthAPI.h index 4ae8b8f9..44a362c8 100644 --- a/launcher/modplatform/modrinth/ModrinthAPI.h +++ b/launcher/modplatform/modrinth/ModrinthAPI.h @@ -2,17 +2,24 @@ #include "modplatform/ModAPI.h" +#include + class ModrinthAPI : public ModAPI { public: - inline QString getModSearchURL(int offset, QString query, QString sort, bool fabricCompatible, QString version) const override + inline QString getModSearchURL(int offset, QString query, QString sort, ModLoaderType modLoader, QString version) const override { + if(!validateModLoader(modLoader)){ + qWarning() << "Modrinth only have Forge and Fabric-compatible mods!"; + return ""; + } + return QString("https://api.modrinth.com/v2/search?" "offset=%1&" "limit=25&" "query=%2&" "index=%3&" "facets=[[\"categories:%4\"],[\"versions:%5\"],[\"project_type:mod\"]]") .arg(offset) .arg(query) .arg(sort) - .arg(fabricCompatible ? "fabric" : "forge") + .arg(getModLoaderString(modLoader)) .arg(version); }; @@ -22,4 +29,22 @@ class ModrinthAPI : public ModAPI { }; inline QString getAuthorURL(const QString& name) const override { return "https://modrinth.com/user/" + name; }; + + private: + inline bool validateModLoader(ModLoaderType modLoader) const{ + return modLoader == Any || modLoader == Forge || modLoader == Fabric; + } + + inline QString getModLoaderString(ModLoaderType modLoader) const{ + switch(modLoader){ + case Any: + return "fabric, forge"; + case Forge: + return "forge"; + case Fabric: + return "fabric"; + default: + return ""; + } + } }; diff --git a/launcher/ui/pages/modplatform/ModModel.cpp b/launcher/ui/pages/modplatform/ModModel.cpp index 4462c20b..5be578c1 100644 --- a/launcher/ui/pages/modplatform/ModModel.cpp +++ b/launcher/ui/pages/modplatform/ModModel.cpp @@ -125,7 +125,8 @@ void ListModel::performPaginatedSearch() ->getComponentVersion("net.fabricmc.fabric-loader") .isEmpty(); auto netJob = new NetJob(QString("%1::Search").arg(m_parent->debugName()), APPLICATION->network()); - auto searchUrl = m_parent->apiProvider()->getModSearchURL(nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric, mcVersion); + auto searchUrl = m_parent->apiProvider()->getModSearchURL( + nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion); netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response)); jobPtr = netJob;