2022-05-15 12:20:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2022-10-27 23:05:11 +01:00
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
*
|
|
|
|
* 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-05-15 12:20:05 +01:00
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
#include "ModrinthPackIndex.h"
|
2022-03-03 02:01:23 +00:00
|
|
|
#include "ModrinthAPI.h"
|
2022-01-14 08:43:42 +00:00
|
|
|
|
|
|
|
#include "Json.h"
|
2022-01-15 09:25:24 +00:00
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-03-02 21:35:59 +00:00
|
|
|
#include "net/NetJob.h"
|
2022-01-15 09:25:24 +00:00
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
static ModrinthAPI api;
|
2022-04-21 19:45:20 +01:00
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
2022-03-03 02:01:23 +00:00
|
|
|
|
2022-03-02 21:35:59 +00:00
|
|
|
void Modrinth::loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-06-19 18:31:44 +01:00
|
|
|
pack.addonId = Json::ensureString(obj, "project_id");
|
|
|
|
if (pack.addonId.toString().isEmpty())
|
|
|
|
pack.addonId = Json::requireString(obj, "id");
|
|
|
|
|
2022-04-13 23:16:36 +01:00
|
|
|
pack.provider = ModPlatform::Provider::MODRINTH;
|
2022-01-14 08:43:42 +00:00
|
|
|
pack.name = Json::requireString(obj, "title");
|
2022-10-27 23:05:11 +01:00
|
|
|
|
2022-06-19 18:31:44 +01:00
|
|
|
pack.slug = Json::ensureString(obj, "slug", "");
|
|
|
|
if (!pack.slug.isEmpty())
|
|
|
|
pack.websiteUrl = "https://modrinth.com/mod/" + pack.slug;
|
2022-04-03 23:06:44 +01:00
|
|
|
else
|
|
|
|
pack.websiteUrl = "";
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
pack.description = Json::ensureString(obj, "description", "");
|
|
|
|
|
|
|
|
pack.logoUrl = Json::requireString(obj, "icon_url");
|
2022-03-02 21:35:59 +00:00
|
|
|
pack.logoName = pack.addonId.toString();
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-03-02 21:35:59 +00:00
|
|
|
ModPlatform::ModpackAuthor modAuthor;
|
2022-06-19 18:31:44 +01:00
|
|
|
modAuthor.name = Json::ensureString(obj, "author", QObject::tr("No author(s)"));
|
2022-03-03 02:01:23 +00:00
|
|
|
modAuthor.url = api.getAuthorURL(modAuthor.name);
|
2022-03-02 21:35:59 +00:00
|
|
|
pack.authors.append(modAuthor);
|
2022-05-24 13:38:48 +01:00
|
|
|
|
|
|
|
// Modrinth can have more data than what's provided by the basic search :)
|
|
|
|
pack.extraDataLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Modrinth::loadExtraPackData(ModPlatform::IndexedPack& pack, QJsonObject& obj)
|
|
|
|
{
|
2022-05-24 15:58:11 +01:00
|
|
|
pack.extraData.issuesUrl = Json::ensureString(obj, "issues_url");
|
2022-10-27 23:05:11 +01:00
|
|
|
if (pack.extraData.issuesUrl.endsWith('/'))
|
2022-05-24 15:58:11 +01:00
|
|
|
pack.extraData.issuesUrl.chop(1);
|
|
|
|
|
|
|
|
pack.extraData.sourceUrl = Json::ensureString(obj, "source_url");
|
2022-10-27 23:05:11 +01:00
|
|
|
if (pack.extraData.sourceUrl.endsWith('/'))
|
2022-05-24 15:58:11 +01:00
|
|
|
pack.extraData.sourceUrl.chop(1);
|
|
|
|
|
|
|
|
pack.extraData.wikiUrl = Json::ensureString(obj, "wiki_url");
|
2022-10-27 23:05:11 +01:00
|
|
|
if (pack.extraData.wikiUrl.endsWith('/'))
|
2022-05-24 15:58:11 +01:00
|
|
|
pack.extraData.wikiUrl.chop(1);
|
|
|
|
|
|
|
|
pack.extraData.discordUrl = Json::ensureString(obj, "discord_url");
|
2022-10-27 23:05:11 +01:00
|
|
|
if (pack.extraData.discordUrl.endsWith('/'))
|
2022-05-24 15:58:11 +01:00
|
|
|
pack.extraData.discordUrl.chop(1);
|
|
|
|
|
2022-05-24 13:38:48 +01:00
|
|
|
auto donate_arr = Json::ensureArray(obj, "donation_urls");
|
2022-10-27 23:05:11 +01:00
|
|
|
for (auto d : donate_arr) {
|
2022-05-24 13:38:48 +01:00
|
|
|
auto d_obj = Json::requireObject(d);
|
|
|
|
|
|
|
|
ModPlatform::DonationData donate;
|
|
|
|
|
|
|
|
donate.id = Json::ensureString(d_obj, "id");
|
|
|
|
donate.platform = Json::ensureString(d_obj, "platform");
|
|
|
|
donate.url = Json::ensureString(d_obj, "url");
|
|
|
|
|
|
|
|
pack.extraData.donate.append(donate);
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:38:16 +00:00
|
|
|
pack.extraData.body = Json::ensureString(obj, "body").remove("<br>");
|
2022-06-17 00:46:47 +01:00
|
|
|
|
2022-05-24 13:38:48 +01:00
|
|
|
pack.extraDataLoaded = true;
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 21:35:59 +00:00
|
|
|
void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|
|
|
QJsonArray& arr,
|
|
|
|
const shared_qobject_ptr<QNetworkAccessManager>& network,
|
|
|
|
BaseInstance* inst)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-03-02 21:35:59 +00:00
|
|
|
QVector<ModPlatform::IndexedVersion> unsortedVersions;
|
2022-03-08 14:12:35 +00:00
|
|
|
QString mcVersion = (static_cast<MinecraftInstance*>(inst))->getPackProfile()->getComponentVersion("net.minecraft");
|
2022-01-15 09:25:24 +00:00
|
|
|
|
2022-03-02 21:35:59 +00:00
|
|
|
for (auto versionIter : arr) {
|
2022-01-14 08:43:42 +00:00
|
|
|
auto obj = versionIter.toObject();
|
2022-04-22 17:23:47 +01:00
|
|
|
auto file = loadIndexedPackVersion(obj);
|
2022-01-15 07:51:47 +00:00
|
|
|
|
2022-10-27 23:05:11 +01:00
|
|
|
if (file.fileId.isValid()) // Heuristic to check if the returned value is valid
|
2022-02-01 20:56:52 +00:00
|
|
|
unsortedVersions.append(file);
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
2022-03-02 21:35:59 +00:00
|
|
|
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
|
|
|
|
// dates are in RFC 3339 format
|
2022-01-14 08:43:42 +00:00
|
|
|
return a.date > b.date;
|
|
|
|
};
|
|
|
|
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
|
|
|
|
pack.versions = unsortedVersions;
|
|
|
|
pack.versionsLoaded = true;
|
|
|
|
}
|
2022-04-22 17:23:47 +01:00
|
|
|
|
2022-10-27 23:05:11 +01:00
|
|
|
auto Modrinth::loadIndexedPackVersion(QJsonObject& obj, QString preferred_hash_type, QString preferred_file_name)
|
|
|
|
-> ModPlatform::IndexedVersion
|
2022-04-22 17:23:47 +01:00
|
|
|
{
|
|
|
|
ModPlatform::IndexedVersion file;
|
|
|
|
|
|
|
|
file.addonId = Json::requireString(obj, "project_id");
|
|
|
|
file.fileId = Json::requireString(obj, "id");
|
|
|
|
file.date = Json::requireString(obj, "date_published");
|
|
|
|
auto versionArray = Json::requireArray(obj, "game_versions");
|
|
|
|
if (versionArray.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
for (auto mcVer : versionArray) {
|
|
|
|
file.mcVersion.append(mcVer.toString());
|
|
|
|
}
|
|
|
|
auto loaders = Json::requireArray(obj, "loaders");
|
|
|
|
for (auto loader : loaders) {
|
|
|
|
file.loaders.append(loader.toString());
|
|
|
|
}
|
|
|
|
file.version = Json::requireString(obj, "name");
|
2022-06-04 01:26:26 +01:00
|
|
|
file.version_number = Json::requireString(obj, "version_number");
|
2022-06-04 00:45:44 +01:00
|
|
|
file.changelog = Json::requireString(obj, "changelog");
|
2022-04-22 17:23:47 +01:00
|
|
|
|
|
|
|
auto files = Json::requireArray(obj, "files");
|
|
|
|
int i = 0;
|
|
|
|
|
2022-10-27 23:05:11 +01:00
|
|
|
if (files.empty()) {
|
|
|
|
// This should not happen normally, but check just in case
|
|
|
|
qWarning() << "Modrinth returned an unexpected empty list of files:" << obj;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:23:47 +01:00
|
|
|
// Find correct file (needed in cases where one version may have multiple files)
|
|
|
|
// Will default to the last one if there's no primary (though I think Modrinth requires that
|
|
|
|
// at least one file is primary, idk)
|
|
|
|
// NOTE: files.count() is 1-indexed, so we need to subtract 1 to become 0-indexed
|
|
|
|
while (i < files.count() - 1) {
|
|
|
|
auto parent = files[i].toObject();
|
|
|
|
auto fileName = Json::requireString(parent, "filename");
|
|
|
|
|
2022-06-03 23:04:49 +01:00
|
|
|
if (!preferred_file_name.isEmpty() && fileName.contains(preferred_file_name)) {
|
|
|
|
file.is_preferred = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:23:47 +01:00
|
|
|
// Grab the primary file, if available
|
|
|
|
if (Json::requireBoolean(parent, "primary"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto parent = files[i].toObject();
|
|
|
|
if (parent.contains("url")) {
|
|
|
|
file.downloadUrl = Json::requireString(parent, "url");
|
|
|
|
file.fileName = Json::requireString(parent, "filename");
|
2022-06-04 01:26:26 +01:00
|
|
|
file.is_preferred = Json::requireBoolean(parent, "primary") || (files.count() == 1);
|
2022-04-22 17:23:47 +01:00
|
|
|
auto hash_list = Json::requireObject(parent, "hashes");
|
2022-10-27 23:05:11 +01:00
|
|
|
|
2022-06-03 23:04:49 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-05-06 16:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-22 17:23:47 +01:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|