feat: add some api calls to modrinth

Calls added:
- Get version from hash
- Get versions from hashes
- Latest version of a project from a hash, loader(s), and game version(s)
- Latest versions of multiple project from hashes, loader(s), and game version(s)

Some of those are not used yet, but may be of use later on, so we have
it if we need it :)

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-06-03 19:04:49 -03:00
parent 32a9545360
commit 0e52112016
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
6 changed files with 141 additions and 9 deletions

View File

@ -484,7 +484,7 @@ set(API_SOURCES
modplatform/flame/FlameAPI.h
modplatform/modrinth/ModrinthAPI.h
modplatform/modrinth/ModrinthAPI.cpp
modplatform/helpers/NetworkModAPI.h
modplatform/helpers/NetworkModAPI.cpp
)

View File

@ -61,6 +61,7 @@ struct IndexedVersion {
QVector<QString> loaders = {};
QString hash_type;
QString hash;
bool is_preferred = true;
};
struct ExtraPackData {

View File

@ -0,0 +1,97 @@
#include "ModrinthAPI.h"
#include "Application.h"
#include "Json.h"
#include "net/Upload.h"
auto ModrinthAPI::currentVersion(QString hash, QString hash_format, QByteArray* response) -> NetJob::Ptr
{
auto* netJob = new NetJob(QString("Modrinth::GetCurrentVersion"), APPLICATION->network());
netJob->addNetAction(Net::Download::makeByteArray(
QString(BuildConfig.MODRINTH_PROD_URL + "/version_file/%1?algorithm=%2").arg(hash, hash_format), response));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
return netJob;
}
auto ModrinthAPI::currentVersions(const QStringList& hashes, QString hash_format, QByteArray* response) -> NetJob::Ptr
{
auto* netJob = new NetJob(QString("Modrinth::GetCurrentVersions"), APPLICATION->network());
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));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
return netJob;
}
auto ModrinthAPI::latestVersion(QString hash,
QString hash_format,
std::list<Version> mcVersions,
ModLoaderTypes loaders,
QByteArray* response) -> NetJob::Ptr
{
auto* netJob = new NetJob(QString("Modrinth::GetLatestVersion"), APPLICATION->network());
QJsonObject body_obj;
Json::writeStringList(body_obj, "loaders", getModLoaderStrings(loaders));
QStringList game_versions;
for (auto& ver : mcVersions) {
game_versions.append(ver.toString());
}
Json::writeStringList(body_obj, "game_versions", game_versions);
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));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
return netJob;
}
auto ModrinthAPI::latestVersions(const QStringList& hashes,
QString hash_format,
std::list<Version> mcVersions,
ModLoaderTypes loaders,
QByteArray* response) -> NetJob::Ptr
{
auto* netJob = new NetJob(QString("Modrinth::GetLatestVersions"), APPLICATION->network());
QJsonObject body_obj;
Json::writeStringList(body_obj, "hashes", hashes);
Json::writeString(body_obj, "algorithm", hash_format);
Json::writeStringList(body_obj, "loaders", getModLoaderStrings(loaders));
QStringList game_versions;
for (auto& ver : mcVersions) {
game_versions.append(ver.toString());
}
Json::writeStringList(body_obj, "game_versions", game_versions);
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(Net::Upload::makeByteArray(
QString(BuildConfig.MODRINTH_PROD_URL + "/version_files/update"), response, body_raw));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
return netJob;
}

View File

@ -22,10 +22,32 @@
#include "modplatform/ModAPI.h"
#include "modplatform/ModIndex.h"
#include "modplatform/helpers/NetworkModAPI.h"
#include "net/NetJob.h"
#include <QDebug>
class ModrinthAPI : public NetworkModAPI {
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;
public:
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };

View File

@ -111,7 +111,7 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
pack.versionsLoaded = true;
}
auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedVersion
auto Modrinth::loadIndexedPackVersion(QJsonObject &obj, QString preferred_hash_type, QString preferred_file_name) -> ModPlatform::IndexedVersion
{
ModPlatform::IndexedVersion file;
@ -142,6 +142,11 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedV
auto parent = files[i].toObject();
auto fileName = Json::requireString(parent, "filename");
if (!preferred_file_name.isEmpty() && fileName.contains(preferred_file_name)) {
file.is_preferred = true;
break;
}
// Grab the primary file, if available
if (Json::requireBoolean(parent, "primary"))
break;
@ -153,13 +158,20 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedV
if (parent.contains("url")) {
file.downloadUrl = Json::requireString(parent, "url");
file.fileName = Json::requireString(parent, "filename");
file.is_preferred = Json::requireBoolean(parent, "primary");
auto hash_list = Json::requireObject(parent, "hashes");
auto hash_types = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH);
for (auto& hash_type : hash_types) {
if (hash_list.contains(hash_type)) {
file.hash = Json::requireString(hash_list, hash_type);
file.hash_type = hash_type;
break;
if (hash_list.contains(preferred_hash_type)) {
file.hash = Json::requireString(hash_list, preferred_hash_type);
file.hash_type = preferred_hash_type;
} else {
auto hash_types = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH);
for (auto& hash_type : hash_types) {
if (hash_list.contains(hash_type)) {
file.hash = Json::requireString(hash_list, hash_type);
file.hash_type = hash_type;
break;
}
}
}

View File

@ -30,6 +30,6 @@ void loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
QJsonArray& arr,
const shared_qobject_ptr<QNetworkAccessManager>& network,
BaseInstance* inst);
auto loadIndexedPackVersion(QJsonObject& obj) -> ModPlatform::IndexedVersion;
auto loadIndexedPackVersion(QJsonObject& obj, QString hash_type = "sha512", QString filename_prefer = "") -> ModPlatform::IndexedVersion;
} // namespace Modrinth