2023-01-23 14:03:55 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 flowln <flowlnlnln@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
2022-06-03 23:04:49 +01:00
|
|
|
#include "ModrinthAPI.h"
|
|
|
|
|
|
|
|
#include "Application.h"
|
|
|
|
#include "Json.h"
|
2023-01-24 13:37:40 +00:00
|
|
|
#include "net/NetJob.h"
|
2022-06-03 23:04:49 +01:00
|
|
|
#include "net/Upload.h"
|
2023-06-02 00:39:04 +01:00
|
|
|
#include "net/ApiDownload.h"
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr ModrinthAPI::currentVersion(QString hash, QString hash_format, QByteArray* response)
|
2022-06-03 23:04:49 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("Modrinth::GetCurrentVersion"), APPLICATION->network());
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
netJob->addNetAction(Net::ApiDownload::makeByteArray(
|
2022-06-03 23:04:49 +01:00
|
|
|
QString(BuildConfig.MODRINTH_PROD_URL + "/version_file/%1?algorithm=%2").arg(hash, hash_format), response));
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr ModrinthAPI::currentVersions(const QStringList& hashes, QString hash_format, QByteArray* response)
|
2022-06-03 23:04:49 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("Modrinth::GetCurrentVersions"), APPLICATION->network());
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
QJsonObject body_obj;
|
|
|
|
|
|
|
|
Json::writeStringList(body_obj, "hashes", hashes);
|
|
|
|
Json::writeString(body_obj, "algorithm", hash_format);
|
|
|
|
|
|
|
|
QJsonDocument body(body_obj);
|
|
|
|
auto body_raw = body.toJson();
|
|
|
|
|
|
|
|
netJob->addNetAction(Net::Upload::makeByteArray(QString(BuildConfig.MODRINTH_PROD_URL + "/version_files"), response, body_raw));
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr ModrinthAPI::latestVersion(QString hash,
|
|
|
|
QString hash_format,
|
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
|
|
|
QByteArray* response)
|
2022-06-03 23:04:49 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("Modrinth::GetLatestVersion"), APPLICATION->network());
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
QJsonObject body_obj;
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
if (loaders.has_value())
|
|
|
|
Json::writeStringList(body_obj, "loaders", getModLoaderStrings(loaders.value()));
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
if (mcVersions.has_value()) {
|
|
|
|
QStringList game_versions;
|
|
|
|
for (auto& ver : mcVersions.value()) {
|
|
|
|
game_versions.append(ver.toString());
|
|
|
|
}
|
|
|
|
Json::writeStringList(body_obj, "game_versions", game_versions);
|
2022-06-03 23:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument body(body_obj);
|
|
|
|
auto body_raw = body.toJson();
|
|
|
|
|
|
|
|
netJob->addNetAction(Net::Upload::makeByteArray(
|
|
|
|
QString(BuildConfig.MODRINTH_PROD_URL + "/version_file/%1/update?algorithm=%2").arg(hash, hash_format), response, body_raw));
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr ModrinthAPI::latestVersions(const QStringList& hashes,
|
|
|
|
QString hash_format,
|
|
|
|
std::optional<std::list<Version>> mcVersions,
|
|
|
|
std::optional<ModLoaderTypes> loaders,
|
|
|
|
QByteArray* response)
|
2022-06-03 23:04:49 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("Modrinth::GetLatestVersions"), APPLICATION->network());
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
QJsonObject body_obj;
|
|
|
|
|
|
|
|
Json::writeStringList(body_obj, "hashes", hashes);
|
|
|
|
Json::writeString(body_obj, "algorithm", hash_format);
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
if (loaders.has_value())
|
|
|
|
Json::writeStringList(body_obj, "loaders", getModLoaderStrings(loaders.value()));
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
if (mcVersions.has_value()) {
|
|
|
|
QStringList game_versions;
|
|
|
|
for (auto& ver : mcVersions.value()) {
|
|
|
|
game_versions.append(ver.toString());
|
|
|
|
}
|
|
|
|
Json::writeStringList(body_obj, "game_versions", game_versions);
|
2022-06-03 23:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument body(body_obj);
|
|
|
|
auto body_raw = body.toJson();
|
|
|
|
|
2022-06-19 18:29:21 +01:00
|
|
|
netJob->addNetAction(Net::Upload::makeByteArray(QString(BuildConfig.MODRINTH_PROD_URL + "/version_files/update"), response, body_raw));
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-06-19 18:29:21 +01:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response) const
|
2022-06-19 18:29:21 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("Modrinth::GetProjects"), APPLICATION->network());
|
2022-06-19 18:29:21 +01:00
|
|
|
auto searchUrl = getMultipleModInfoURL(addonIds);
|
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
netJob->addNetAction(Net::ApiDownload::makeByteArray(QUrl(searchUrl), response));
|
2022-06-03 23:04:49 +01:00
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response, netJob] {
|
2023-01-03 16:58:27 +00:00
|
|
|
delete response;
|
|
|
|
});
|
2022-06-03 23:04:49 +01:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
2022-12-20 15:15:17 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|