2022-05-15 12:20:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
*
|
|
|
|
* 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-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-03-02 21:35:59 +00:00
|
|
|
void Modrinth::loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-01-31 16:18:11 +00:00
|
|
|
pack.addonId = Json::requireString(obj, "project_id");
|
2022-01-14 08:43:42 +00:00
|
|
|
pack.name = Json::requireString(obj, "title");
|
2022-04-03 23:06:44 +01:00
|
|
|
|
|
|
|
QString slug = Json::ensureString(obj, "slug", "");
|
|
|
|
if (!slug.isEmpty())
|
|
|
|
pack.websiteUrl = "https://modrinth.com/mod/" + Json::ensureString(obj, "slug", "");
|
|
|
|
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-01-24 06:23:01 +00:00
|
|
|
modAuthor.name = Json::requireString(obj, "author");
|
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-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-03-02 21:35:59 +00:00
|
|
|
ModPlatform::IndexedVersion file;
|
|
|
|
file.addonId = Json::requireString(obj, "project_id");
|
2022-01-14 08:43:42 +00:00
|
|
|
file.fileId = Json::requireString(obj, "id");
|
|
|
|
file.date = Json::requireString(obj, "date_published");
|
|
|
|
auto versionArray = Json::requireArray(obj, "game_versions");
|
2022-03-02 21:35:59 +00:00
|
|
|
if (versionArray.empty()) { continue; }
|
|
|
|
for (auto mcVer : versionArray) {
|
2022-01-15 07:51:47 +00:00
|
|
|
file.mcVersion.append(mcVer.toString());
|
|
|
|
}
|
2022-03-02 21:35:59 +00:00
|
|
|
auto loaders = Json::requireArray(obj, "loaders");
|
|
|
|
for (auto loader : loaders) {
|
2022-01-15 07:51:47 +00:00
|
|
|
file.loaders.append(loader.toString());
|
|
|
|
}
|
2022-01-14 08:43:42 +00:00
|
|
|
file.version = Json::requireString(obj, "name");
|
2022-01-15 09:25:24 +00:00
|
|
|
|
|
|
|
auto files = Json::requireArray(obj, "files");
|
|
|
|
int i = 0;
|
2022-03-18 13:52:47 +00: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)
|
2022-03-22 22:37:10 +00:00
|
|
|
// NOTE: files.count() is 1-indexed, so we need to subtract 1 to become 0-indexed
|
|
|
|
while (i < files.count() - 1){
|
2022-01-15 09:25:24 +00:00
|
|
|
auto parent = files[i].toObject();
|
|
|
|
auto fileName = Json::requireString(parent, "filename");
|
2022-03-18 13:52:47 +00:00
|
|
|
|
|
|
|
// Grab the primary file, if available
|
|
|
|
if(Json::requireBoolean(parent, "primary"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
i++;
|
2022-01-15 09:25:24 +00:00
|
|
|
}
|
2022-03-18 13:52:47 +00:00
|
|
|
|
2022-01-15 09:25:24 +00:00
|
|
|
auto parent = files[i].toObject();
|
2022-03-02 21:35:59 +00:00
|
|
|
if (parent.contains("url")) {
|
2022-02-01 20:56:52 +00:00
|
|
|
file.downloadUrl = Json::requireString(parent, "url");
|
|
|
|
file.fileName = Json::requireString(parent, "filename");
|
2022-01-15 07:51:47 +00:00
|
|
|
|
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;
|
|
|
|
}
|