2023-01-23 14:03:55 +00:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 flowln <flowlnlnln@gmail.com>
|
|
|
|
//
|
2022-05-15 12:20:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-15 11:43:02 +01:00
|
|
|
#include "BuildConfig.h"
|
2022-04-20 01:10:12 +01:00
|
|
|
#include "modplatform/ModIndex.h"
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "modplatform/helpers/NetworkResourceAPI.h"
|
2022-03-03 02:01:23 +00:00
|
|
|
|
2022-03-06 19:45:39 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
class ModrinthAPI : public NetworkResourceAPI {
|
2022-06-03 23:04:49 +01:00
|
|
|
public:
|
2023-06-15 20:59:41 +01:00
|
|
|
auto currentVersion(QString hash, QString hash_format, std::shared_ptr<QByteArray> response) -> Task::Ptr;
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
auto currentVersions(const QStringList& hashes, QString hash_format, std::shared_ptr<QByteArray> response) -> Task::Ptr;
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
auto latestVersion(QString hash,
|
|
|
|
QString hash_format,
|
2022-11-25 12:23:46 +00:00
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
2023-06-15 20:59:41 +01:00
|
|
|
std::shared_ptr<QByteArray> response) -> Task::Ptr;
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
auto latestVersions(const QStringList& hashes,
|
|
|
|
QString hash_format,
|
2023-06-15 20:59:41 +01:00
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
|
|
|
std::shared_ptr<QByteArray> response) -> Task::Ptr;
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
Task::Ptr getProjects(QStringList addonIds, std::shared_ptr<QByteArray> response) const override;
|
2022-06-19 18:29:21 +01:00
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
public:
|
2022-12-20 15:15:17 +00:00
|
|
|
[[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
|
2022-03-07 19:22:57 +00:00
|
|
|
|
2022-05-19 07:40:28 +01:00
|
|
|
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
|
2022-04-18 12:12:42 +01:00
|
|
|
{
|
|
|
|
QStringList l;
|
2022-11-25 12:23:46 +00:00
|
|
|
for (auto loader : {Forge, Fabric, Quilt}) {
|
|
|
|
if (types & loader) {
|
|
|
|
l << getModLoaderString(loader);
|
2022-05-19 07:40:28 +01:00
|
|
|
}
|
2022-04-18 12:12:42 +01:00
|
|
|
}
|
2022-05-19 11:35:44 +01:00
|
|
|
if ((types & Quilt) && (~types & Fabric)) // Add Fabric if Quilt is in use, if Fabric isn't already there
|
2022-11-25 12:23:46 +00:00
|
|
|
l << getModLoaderString(Fabric);
|
2022-04-18 12:12:42 +01:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:40:28 +01:00
|
|
|
static auto getModLoaderFilters(ModLoaderTypes types) -> const QString
|
2022-04-18 12:12:42 +01:00
|
|
|
{
|
|
|
|
QStringList l;
|
2022-05-19 07:40:28 +01:00
|
|
|
for (auto loader : getModLoaderStrings(types))
|
2022-04-18 12:12:42 +01:00
|
|
|
{
|
|
|
|
l << QString("\"categories:%1\"").arg(loader);
|
|
|
|
}
|
|
|
|
return l.join(',');
|
|
|
|
}
|
|
|
|
|
2022-03-07 19:22:57 +00:00
|
|
|
private:
|
2022-11-25 12:23:46 +00:00
|
|
|
[[nodiscard]] static QString resourceTypeParameter(ModPlatform::ResourceType type)
|
2022-03-07 19:22:57 +00:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
switch (type) {
|
|
|
|
case ModPlatform::ResourceType::MOD:
|
|
|
|
return "mod";
|
2022-12-16 23:26:10 +00:00
|
|
|
case ModPlatform::ResourceType::RESOURCE_PACK:
|
|
|
|
return "resourcepack";
|
2022-12-30 17:06:07 +00:00
|
|
|
case ModPlatform::ResourceType::SHADER_PACK:
|
|
|
|
return "shader";
|
2022-11-25 12:23:46 +00:00
|
|
|
default:
|
|
|
|
qWarning() << "Invalid resource type for Modrinth API!";
|
|
|
|
break;
|
2022-03-06 19:45:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
[[nodiscard]] QString createFacets(SearchArgs const& args) const
|
|
|
|
{
|
|
|
|
QStringList facets_list;
|
|
|
|
|
|
|
|
if (args.loaders.has_value())
|
|
|
|
facets_list.append(QString("[%1]").arg(getModLoaderFilters(args.loaders.value())));
|
|
|
|
if (args.versions.has_value())
|
|
|
|
facets_list.append(QString("[%1]").arg(getGameVersionsArray(args.versions.value())));
|
|
|
|
facets_list.append(QString("[\"project_type:%1\"]").arg(resourceTypeParameter(args.type)));
|
|
|
|
|
|
|
|
return QString("[%1]").arg(facets_list.join(','));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
[[nodiscard]] inline auto getSearchURL(SearchArgs const& args) const -> std::optional<QString> override
|
|
|
|
{
|
|
|
|
if (args.loaders.has_value()) {
|
|
|
|
if (!validateModLoaders(args.loaders.value())) {
|
|
|
|
qWarning() << "Modrinth only have Forge and Fabric-compatible mods!";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList get_arguments;
|
|
|
|
get_arguments.append(QString("offset=%1").arg(args.offset));
|
|
|
|
get_arguments.append(QString("limit=25"));
|
|
|
|
if (args.search.has_value())
|
|
|
|
get_arguments.append(QString("query=%1").arg(args.search.value()));
|
|
|
|
if (args.sorting.has_value())
|
2022-12-20 15:15:17 +00:00
|
|
|
get_arguments.append(QString("index=%1").arg(args.sorting.value().name));
|
2022-11-25 12:23:46 +00:00
|
|
|
get_arguments.append(QString("facets=%1").arg(createFacets(args)));
|
|
|
|
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + "/search?" + get_arguments.join('&');
|
2022-03-03 02:01:23 +00:00
|
|
|
};
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
inline auto getInfoURL(QString const& id) const -> std::optional<QString> override
|
2022-05-24 13:38:48 +01:00
|
|
|
{
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + "/project/" + id;
|
|
|
|
};
|
|
|
|
|
2022-06-19 18:29:21 +01:00
|
|
|
inline auto getMultipleModInfoURL(QStringList ids) const -> QString
|
|
|
|
{
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + QString("/projects?ids=[\"%1\"]").arg(ids.join("\",\""));
|
|
|
|
};
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
inline auto getVersionsURL(VersionSearchArgs const& args) const -> std::optional<QString> override
|
2022-03-03 02:01:23 +00:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
QStringList get_arguments;
|
|
|
|
if (args.mcVersions.has_value())
|
|
|
|
get_arguments.append(QString("game_versions=[%1]").arg(getGameVersionsString(args.mcVersions.value())));
|
|
|
|
if (args.loaders.has_value())
|
|
|
|
get_arguments.append(QString("loaders=[\"%1\"]").arg(getModLoaderStrings(args.loaders.value()).join("\",\"")));
|
|
|
|
|
|
|
|
return QString("%1/project/%2/version%3%4")
|
2022-12-18 18:41:46 +00:00
|
|
|
.arg(BuildConfig.MODRINTH_PROD_URL, args.pack.addonId.toString(), get_arguments.isEmpty() ? "" : "?", get_arguments.join('&'));
|
2022-03-03 02:01:23 +00:00
|
|
|
};
|
|
|
|
|
2022-04-02 23:21:02 +01:00
|
|
|
auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
for(auto& ver : mcVersions){
|
|
|
|
s += QString("\"versions:%1\",").arg(ver.toString());
|
|
|
|
}
|
|
|
|
s.remove(s.length() - 1, 1); //remove last comma
|
2022-11-25 12:23:46 +00:00
|
|
|
return s.isEmpty() ? QString() : s;
|
2022-04-02 23:21:02 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:40:28 +01:00
|
|
|
inline auto validateModLoaders(ModLoaderTypes loaders) const -> bool
|
2022-03-07 19:46:08 +00:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
return loaders & (Forge | Fabric | Quilt);
|
2022-03-07 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
};
|