2022-05-15 12:20:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 12:25:58 +01:00
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-05-15 12:20:05 +01:00
|
|
|
*
|
|
|
|
* 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 02:01:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-15 11:43:02 +01:00
|
|
|
#include "BuildConfig.h"
|
2022-04-18 12:12:42 +01:00
|
|
|
#include "modplatform/ModAPI.h"
|
2022-04-20 01:10:12 +01:00
|
|
|
#include "modplatform/ModIndex.h"
|
2022-03-07 19:46:08 +00:00
|
|
|
#include "modplatform/helpers/NetworkModAPI.h"
|
2022-06-03 23:04:49 +01:00
|
|
|
#include "net/NetJob.h"
|
2022-03-03 02:01:23 +00:00
|
|
|
|
2022-03-06 19:45:39 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2022-03-07 19:46:08 +00:00
|
|
|
class ModrinthAPI : public NetworkModAPI {
|
2022-06-03 23:04:49 +01:00
|
|
|
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,
|
|
|
|
std::list<Version> mcVersions,
|
|
|
|
ModLoaderTypes loaders,
|
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
|
|
|
auto latestVersions(const QStringList& hashes,
|
|
|
|
QString hash_format,
|
|
|
|
std::list<Version> mcVersions,
|
|
|
|
ModLoaderTypes loaders,
|
|
|
|
QByteArray* response) -> NetJob::Ptr;
|
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
public:
|
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-05-19 07:40:28 +01:00
|
|
|
for (auto loader : {Forge, Fabric, Quilt})
|
2022-04-18 12:12:42 +01:00
|
|
|
{
|
2022-05-19 11:35:44 +01:00
|
|
|
if ((types & loader) || types == Unspecified)
|
2022-05-19 07:40:28 +01:00
|
|
|
{
|
|
|
|
l << ModAPI::getModLoaderString(loader);
|
|
|
|
}
|
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-05-19 07:40:28 +01:00
|
|
|
l << ModAPI::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-03-08 14:12:35 +00:00
|
|
|
inline auto getModSearchURL(SearchArgs& args) const -> QString override
|
2022-03-07 19:22:57 +00:00
|
|
|
{
|
2022-05-19 07:40:28 +01:00
|
|
|
if (!validateModLoaders(args.loaders)) {
|
2022-03-06 19:45:39 +00:00
|
|
|
qWarning() << "Modrinth only have Forge and Fabric-compatible mods!";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-05-15 11:43:02 +01:00
|
|
|
return QString(BuildConfig.MODRINTH_PROD_URL +
|
|
|
|
"/search?"
|
|
|
|
"offset=%1&"
|
|
|
|
"limit=25&"
|
|
|
|
"query=%2&"
|
|
|
|
"index=%3&"
|
|
|
|
"facets=[[%4],%5[\"project_type:mod\"]]")
|
2022-03-07 19:22:57 +00:00
|
|
|
.arg(args.offset)
|
|
|
|
.arg(args.search)
|
|
|
|
.arg(args.sorting)
|
2022-05-19 07:40:28 +01:00
|
|
|
.arg(getModLoaderFilters(args.loaders))
|
2022-04-02 23:21:02 +01:00
|
|
|
.arg(getGameVersionsArray(args.versions));
|
2022-03-03 02:01:23 +00:00
|
|
|
};
|
|
|
|
|
2022-05-24 13:38:48 +01:00
|
|
|
inline auto getModInfoURL(QString& id) const -> QString override
|
|
|
|
{
|
|
|
|
return BuildConfig.MODRINTH_PROD_URL + "/project/" + id;
|
|
|
|
};
|
|
|
|
|
2022-03-24 21:39:53 +00:00
|
|
|
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override
|
2022-03-03 02:01:23 +00:00
|
|
|
{
|
2022-05-15 11:43:02 +01:00
|
|
|
return QString(BuildConfig.MODRINTH_PROD_URL +
|
|
|
|
"/project/%1/version?"
|
2022-05-29 17:23:34 +01:00
|
|
|
"game_versions=[%2]&"
|
2022-05-15 11:43:02 +01:00
|
|
|
"loaders=[\"%3\"]")
|
2022-05-29 17:23:34 +01:00
|
|
|
.arg(args.addonId,
|
|
|
|
getGameVersionsString(args.mcVersions),
|
|
|
|
getModLoaderStrings(args.loaders).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-04-03 00:08:37 +01:00
|
|
|
return s.isEmpty() ? QString() : QString("[%1],").arg(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-05-19 11:35:44 +01:00
|
|
|
return (loaders == Unspecified) || (loaders & (Forge | Fabric | Quilt));
|
2022-03-07 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
};
|