aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform/modrinth
diff options
context:
space:
mode:
authorflow <flowlnlnln@gmail.com>2022-06-03 19:04:49 -0300
committerflow <flowlnlnln@gmail.com>2022-07-17 11:33:41 -0300
commit0e52112016fe9942f7448cd83914f6266904c311 (patch)
treefb873a3a6920d1ae4f5751f747864dd6283e0556 /launcher/modplatform/modrinth
parent32a9545360b10058cf84b951ee88959adf3bf374 (diff)
downloadPrismLauncher-0e52112016fe9942f7448cd83914f6266904c311.tar.gz
PrismLauncher-0e52112016fe9942f7448cd83914f6266904c311.tar.bz2
PrismLauncher-0e52112016fe9942f7448cd83914f6266904c311.zip
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>
Diffstat (limited to 'launcher/modplatform/modrinth')
-rw-r--r--launcher/modplatform/modrinth/ModrinthAPI.cpp97
-rw-r--r--launcher/modplatform/modrinth/ModrinthAPI.h22
-rw-r--r--launcher/modplatform/modrinth/ModrinthPackIndex.cpp26
-rw-r--r--launcher/modplatform/modrinth/ModrinthPackIndex.h2
4 files changed, 139 insertions, 8 deletions
diff --git a/launcher/modplatform/modrinth/ModrinthAPI.cpp b/launcher/modplatform/modrinth/ModrinthAPI.cpp
new file mode 100644
index 00000000..0d5d4bab
--- /dev/null
+++ b/launcher/modplatform/modrinth/ModrinthAPI.cpp
@@ -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;
+}
diff --git a/launcher/modplatform/modrinth/ModrinthAPI.h b/launcher/modplatform/modrinth/ModrinthAPI.h
index 89e52d6c..9694b85e 100644
--- a/launcher/modplatform/modrinth/ModrinthAPI.h
+++ b/launcher/modplatform/modrinth/ModrinthAPI.h
@@ -22,11 +22,33 @@
#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; };
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
diff --git a/launcher/modplatform/modrinth/ModrinthPackIndex.cpp b/launcher/modplatform/modrinth/ModrinthPackIndex.cpp
index b6f5490a..4e738819 100644
--- a/launcher/modplatform/modrinth/ModrinthPackIndex.cpp
+++ b/launcher/modplatform/modrinth/ModrinthPackIndex.cpp
@@ -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;
+ }
}
}
diff --git a/launcher/modplatform/modrinth/ModrinthPackIndex.h b/launcher/modplatform/modrinth/ModrinthPackIndex.h
index b7936204..31881414 100644
--- a/launcher/modplatform/modrinth/ModrinthPackIndex.h
+++ b/launcher/modplatform/modrinth/ModrinthPackIndex.h
@@ -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