2022-05-15 16:50:05 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 16:55:58 +05:30
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-05-15 16:50:05 +05:30
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-03-03 07:31:23 +05:30
|
|
|
#pragma once
|
|
|
|
|
2022-05-15 16:13:02 +05:30
|
|
|
#include "BuildConfig.h"
|
2022-04-20 05:40:12 +05:30
|
|
|
#include "modplatform/ModIndex.h"
|
2022-11-25 17:53:46 +05:30
|
|
|
#include "modplatform/helpers/NetworkResourceAPI.h"
|
2022-03-03 07:31:23 +05:30
|
|
|
|
2022-03-07 01:15:39 +05:30
|
|
|
#include <QDebug>
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
class ModrinthAPI : public NetworkResourceAPI {
|
2022-06-04 03:34:49 +05:30
|
|
|
public:
|
|
|
|
auto currentVersion(QString hash,
|
|
|
|
QString hash_format,
|
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
|
|
|
auto currentVersions(const QStringList& hashes,
|
|
|
|
QString hash_format,
|
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
|
|
|
auto latestVersion(QString hash,
|
|
|
|
QString hash_format,
|
2022-11-25 17:53:46 +05:30
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
2022-06-04 03:34:49 +05:30
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
|
|
|
auto latestVersions(const QStringList& hashes,
|
|
|
|
QString hash_format,
|
2022-11-25 17:53:46 +05:30
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
2022-06-04 03:34:49 +05:30
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
|
2022-06-19 22:59:21 +05:30
|
|
|
|
2022-03-03 07:31:23 +05:30
|
|
|
public:
|
2022-03-08 19:42:35 +05:30
|
|
|
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
|
2022-03-08 00:52:57 +05:30
|
|
|
|
2022-05-19 12:10:28 +05:30
|
|
|
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
|
2022-04-18 16:42:42 +05:30
|
|
|
{
|
|
|
|
QStringList l;
|
2022-11-25 17:53:46 +05:30
|
|
|
for (auto loader : {Forge, Fabric, Quilt}) {
|
|
|
|
if (types & loader) {
|
|
|
|
l << getModLoaderString(loader);
|
2022-05-19 12:10:28 +05:30
|
|
|
}
|
2022-04-18 16:42:42 +05:30
|
|
|
}
|
2022-05-19 16:05:44 +05:30
|
|
|
if ((types & Quilt) && (~types & Fabric)) // Add Fabric if Quilt is in use, if Fabric isn't already there
|
2022-11-25 17:53:46 +05:30
|
|
|
l << getModLoaderString(Fabric);
|
2022-04-18 16:42:42 +05:30
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2022-05-19 12:10:28 +05:30
|
|
|
static auto getModLoaderFilters(ModLoaderTypes types) -> const QString
|
2022-04-18 16:42:42 +05:30
|
|
|
{
|
|
|
|
QStringList l;
|
2022-05-19 12:10:28 +05:30
|
|
|
for (auto loader : getModLoaderStrings(types))
|
2022-04-18 16:42:42 +05:30
|
|
|
{
|
|
|
|
l << QString("\"categories:%1\"").arg(loader);
|
|
|
|
}
|
|
|
|
return l.join(',');
|
|
|
|
}
|
|
|
|
|
2022-03-08 00:52:57 +05:30
|
|
|
private:
|
2022-11-25 17:53:46 +05:30
|
|
|
[[nodiscard]] static QString resourceTypeParameter(ModPlatform::ResourceType type)
|
2022-03-08 00:52:57 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
switch (type) {
|
|
|
|
case ModPlatform::ResourceType::MOD:
|
|
|
|
return "mod";
|
|
|
|
default:
|
|
|
|
qWarning() << "Invalid resource type for Modrinth API!";
|
|
|
|
break;
|
2022-03-07 01:15:39 +05:30
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
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())
|
|
|
|
get_arguments.append(QString("index=%1").arg(args.sorting.value()));
|
|
|
|
get_arguments.append(QString("facets=%1").arg(createFacets(args)));
|
|
|
|
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + "/search?" + get_arguments.join('&');
|
2022-03-03 07:31:23 +05:30
|
|
|
};
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
inline auto getInfoURL(QString const& id) const -> std::optional<QString> override
|
2022-05-24 18:08:48 +05:30
|
|
|
{
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + "/project/" + id;
|
|
|
|
};
|
|
|
|
|
2022-06-19 22:59:21 +05:30
|
|
|
inline auto getMultipleModInfoURL(QStringList ids) const -> QString
|
|
|
|
{
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + QString("/projects?ids=[\"%1\"]").arg(ids.join("\",\""));
|
|
|
|
};
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
inline auto getVersionsURL(VersionSearchArgs const& args) const -> std::optional<QString> override
|
2022-03-03 07:31:23 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
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")
|
|
|
|
.arg(BuildConfig.MODRINTH_PROD_URL, args.addonId, get_arguments.isEmpty() ? "" : "?", get_arguments.join('&'));
|
2022-03-03 07:31:23 +05:30
|
|
|
};
|
|
|
|
|
2022-04-03 03:51:02 +05:30
|
|
|
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 17:53:46 +05:30
|
|
|
return s.isEmpty() ? QString() : s;
|
2022-04-03 03:51:02 +05:30
|
|
|
}
|
|
|
|
|
2022-05-19 12:10:28 +05:30
|
|
|
inline auto validateModLoaders(ModLoaderTypes loaders) const -> bool
|
2022-03-08 01:16:08 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
return loaders & (Forge | Fabric | Quilt);
|
2022-03-08 01:16:08 +05:30
|
|
|
}
|
|
|
|
|
2022-03-03 07:31:23 +05:30
|
|
|
};
|