aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform
diff options
context:
space:
mode:
authorflow <flowlnlnln@gmail.com>2022-12-20 12:15:17 -0300
committerflow <flowlnlnln@gmail.com>2023-01-13 16:23:07 -0300
commit36571c5e2237c98e194cff326480ebe3e661c586 (patch)
tree7a7bb0f1df380ded2ed6be227f61ef01e86c6e77 /launcher/modplatform
parentc8eca4fb8508a22b9d4819d57627dd684f8d98c5 (diff)
downloadPrismLauncher-36571c5e2237c98e194cff326480ebe3e661c586.tar.gz
PrismLauncher-36571c5e2237c98e194cff326480ebe3e661c586.tar.bz2
PrismLauncher-36571c5e2237c98e194cff326480ebe3e661c586.zip
refactor(RD): clear up sorting methods
This refactors the sorting methods to join every bit of it into a single list, easing maintanance. It also removes the weird index contraint on the list of methods by adding an index field to the DS that holds the method. Lastly, it puts the available methods on their respective API, so other resources on the same API can re-use them later on. Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/modplatform')
-rw-r--r--launcher/modplatform/ResourceAPI.h17
-rw-r--r--launcher/modplatform/flame/FlameAPI.cpp15
-rw-r--r--launcher/modplatform/flame/FlameAPI.h17
-rw-r--r--launcher/modplatform/modrinth/ModrinthAPI.cpp12
-rw-r--r--launcher/modplatform/modrinth/ModrinthAPI.h4
5 files changed, 49 insertions, 16 deletions
diff --git a/launcher/modplatform/ResourceAPI.h b/launcher/modplatform/ResourceAPI.h
index 78441c34..a2078b94 100644
--- a/launcher/modplatform/ResourceAPI.h
+++ b/launcher/modplatform/ResourceAPI.h
@@ -54,12 +54,23 @@ class ResourceAPI {
enum ModLoaderType { Forge = 1 << 0, Cauldron = 1 << 1, LiteLoader = 1 << 2, Fabric = 1 << 3, Quilt = 1 << 4 };
Q_DECLARE_FLAGS(ModLoaderTypes, ModLoaderType)
+ struct SortingMethod {
+ // The index of the sorting method. Used to allow for arbitrary ordering in the list of methods.
+ // Used by Flame in the API request.
+ unsigned int index;
+ // The real name of the sorting, as used in the respective API specification.
+ // Used by Modrinth in the API request.
+ QString name;
+ // The human-readable name of the sorting, used for display in the UI.
+ QString readable_name;
+ };
+
struct SearchArgs {
ModPlatform::ResourceType type{};
int offset = 0;
std::optional<QString> search;
- std::optional<QString> sorting;
+ std::optional<SortingMethod> sorting;
std::optional<ModLoaderTypes> loaders;
std::optional<std::list<Version> > versions;
};
@@ -95,6 +106,10 @@ class ResourceAPI {
std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> on_succeed;
};
+ public:
+ /** Gets a list of available sorting methods for this API. */
+ [[nodiscard]] virtual auto getSortingMethods() const -> QList<SortingMethod> = 0;
+
public slots:
[[nodiscard]] virtual NetJob::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const
{
diff --git a/launcher/modplatform/flame/FlameAPI.cpp b/launcher/modplatform/flame/FlameAPI.cpp
index 89249c41..32729a14 100644
--- a/launcher/modplatform/flame/FlameAPI.cpp
+++ b/launcher/modplatform/flame/FlameAPI.cpp
@@ -212,3 +212,18 @@ NetJob::Ptr FlameAPI::getFiles(const QStringList& fileIds, QByteArray* response)
return netJob;
}
+
+// https://docs.curseforge.com/?python#tocS_ModsSearchSortField
+static QList<ResourceAPI::SortingMethod> s_sorts = { { 1, "Featured", QObject::tr("Sort by Featured") },
+ { 2, "Popularity", QObject::tr("Sort by Popularity") },
+ { 3, "LastUpdated", QObject::tr("Sort by Last Updated") },
+ { 4, "Name", QObject::tr("Sort by Name") },
+ { 5, "Author", QObject::tr("Sort by Author") },
+ { 6, "TotalDownloads", QObject::tr("Sort by Downloads") },
+ { 7, "Category", QObject::tr("Sort by Category") },
+ { 8, "GameVersion", QObject::tr("Sort by Game Version") } };
+
+QList<ResourceAPI::SortingMethod> FlameAPI::getSortingMethods() const
+{
+ return s_sorts;
+}
diff --git a/launcher/modplatform/flame/FlameAPI.h b/launcher/modplatform/flame/FlameAPI.h
index f3cc0bbf..2b288564 100644
--- a/launcher/modplatform/flame/FlameAPI.h
+++ b/launcher/modplatform/flame/FlameAPI.h
@@ -14,20 +14,9 @@ class FlameAPI : public NetworkResourceAPI {
NetJob::Ptr matchFingerprints(const QList<uint>& fingerprints, QByteArray* response);
NetJob::Ptr getFiles(const QStringList& fileIds, QByteArray* response) const;
- private:
- static int getSortFieldInt(QString const& sortString)
- {
- return sortString == "Featured" ? 1
- : sortString == "Popularity" ? 2
- : sortString == "LastUpdated" ? 3
- : sortString == "Name" ? 4
- : sortString == "Author" ? 5
- : sortString == "TotalDownloads" ? 6
- : sortString == "Category" ? 7
- : sortString == "GameVersion" ? 8
- : 1;
- }
+ [[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;
+ private:
static int getClassId(ModPlatform::ResourceType type)
{
switch (type) {
@@ -62,7 +51,7 @@ class FlameAPI : public NetworkResourceAPI {
if (args.search.has_value())
get_arguments.append(QString("searchFilter=%1").arg(args.search.value()));
if (args.sorting.has_value())
- get_arguments.append(QString("sortField=%1").arg(getSortFieldInt(args.sorting.value())));
+ get_arguments.append(QString("sortField=%1").arg(args.sorting.value().index));
get_arguments.append("sortOrder=desc");
if (args.loaders.has_value())
get_arguments.append(QString("modLoaderType=%1").arg(getMappedModLoader(args.loaders.value())));
diff --git a/launcher/modplatform/modrinth/ModrinthAPI.cpp b/launcher/modplatform/modrinth/ModrinthAPI.cpp
index 8e64be09..8d7e3acf 100644
--- a/launcher/modplatform/modrinth/ModrinthAPI.cpp
+++ b/launcher/modplatform/modrinth/ModrinthAPI.cpp
@@ -112,3 +112,15 @@ NetJob::Ptr ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response)
return netJob;
}
+
+// https://docs.modrinth.com/api-spec/#tag/projects/operation/searchProjects
+static QList<ResourceAPI::SortingMethod> s_sorts = { { 1, "relevance", QObject::tr("Sort by Relevance") },
+ { 2, "downloads", QObject::tr("Sort by Downloads") },
+ { 3, "follows", QObject::tr("Sort by Follows") },
+ { 4, "newest", QObject::tr("Sort by Last Updated") },
+ { 5, "updated", QObject::tr("Sort by Newest") } };
+
+QList<ResourceAPI::SortingMethod> ModrinthAPI::getSortingMethods() const
+{
+ return s_sorts;
+}
diff --git a/launcher/modplatform/modrinth/ModrinthAPI.h b/launcher/modplatform/modrinth/ModrinthAPI.h
index ec38d9ee..949fc46e 100644
--- a/launcher/modplatform/modrinth/ModrinthAPI.h
+++ b/launcher/modplatform/modrinth/ModrinthAPI.h
@@ -49,6 +49,8 @@ class ModrinthAPI : public NetworkResourceAPI {
NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
public:
+ [[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;
+
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
@@ -116,7 +118,7 @@ class ModrinthAPI : public NetworkResourceAPI {
if (args.search.has_value())
get_arguments.append(QString("query=%1").arg(args.search.value()));
if (args.sorting.has_value())
- get_arguments.append(QString("index=%1").arg(args.sorting.value()));
+ get_arguments.append(QString("index=%1").arg(args.sorting.value().name));
get_arguments.append(QString("facets=%1").arg(createFacets(args)));
return BuildConfig.MODRINTH_PROD_URL + "/search?" + get_arguments.join('&');