2022-05-15 16:50:05 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 16:55:58 +05:30
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-01-31 19:55:36 +05:30
|
|
|
*
|
2022-05-15 16:50:05 +05:30
|
|
|
* 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.
|
2022-01-31 19:55:36 +05:30
|
|
|
*
|
2022-05-15 16:50:05 +05:30
|
|
|
* 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.
|
2022-01-31 19:55:36 +05:30
|
|
|
*
|
2022-05-15 16:50:05 +05:30
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
* Copyright 2022 kb1000
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2022-01-31 19:55:36 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ModrinthPackManifest.h"
|
2022-05-15 04:47:05 +05:30
|
|
|
#include "Json.h"
|
|
|
|
|
2022-05-15 15:42:31 +05:30
|
|
|
#include "modplatform/modrinth/ModrinthAPI.h"
|
|
|
|
|
2022-05-15 04:47:05 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
|
|
|
|
2022-05-29 00:24:00 +05:30
|
|
|
#include <QSet>
|
|
|
|
|
2022-05-15 15:42:31 +05:30
|
|
|
static ModrinthAPI api;
|
|
|
|
|
2022-05-15 04:47:05 +05:30
|
|
|
namespace Modrinth {
|
|
|
|
|
|
|
|
void loadIndexedPack(Modpack& pack, QJsonObject& obj)
|
|
|
|
{
|
|
|
|
pack.id = Json::ensureString(obj, "project_id");
|
|
|
|
|
|
|
|
pack.name = Json::ensureString(obj, "title");
|
|
|
|
pack.description = Json::ensureString(obj, "description");
|
2022-05-15 15:42:31 +05:30
|
|
|
auto temp_author_name = Json::ensureString(obj, "author");
|
|
|
|
pack.author = std::make_tuple(temp_author_name, api.getAuthorURL(temp_author_name));
|
2022-05-15 04:47:05 +05:30
|
|
|
pack.iconName = QString("modrinth_%1").arg(Json::ensureString(obj, "slug"));
|
|
|
|
pack.iconUrl = Json::ensureString(obj, "icon_url");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadIndexedInfo(Modpack& pack, QJsonObject& obj)
|
|
|
|
{
|
|
|
|
pack.extra.body = Json::ensureString(obj, "body");
|
2022-05-15 06:17:35 +05:30
|
|
|
pack.extra.projectUrl = QString("https://modrinth.com/modpack/%1").arg(Json::ensureString(obj, "slug"));
|
2022-05-15 04:47:05 +05:30
|
|
|
pack.extra.sourceUrl = Json::ensureString(obj, "source_url");
|
|
|
|
pack.extra.wikiUrl = Json::ensureString(obj, "wiki_url");
|
|
|
|
|
|
|
|
pack.extraInfoLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadIndexedVersions(Modpack& pack, QJsonDocument& doc)
|
|
|
|
{
|
|
|
|
QVector<ModpackVersion> unsortedVersions;
|
|
|
|
|
|
|
|
auto arr = Json::requireArray(doc);
|
|
|
|
|
|
|
|
for (auto versionIter : arr) {
|
|
|
|
auto obj = Json::requireObject(versionIter);
|
|
|
|
auto file = loadIndexedVersion(obj);
|
|
|
|
|
|
|
|
if(!file.id.isEmpty()) // Heuristic to check if the returned value is valid
|
|
|
|
unsortedVersions.append(file);
|
|
|
|
}
|
|
|
|
auto orderSortPredicate = [](const ModpackVersion& a, const ModpackVersion& b) -> bool {
|
|
|
|
// dates are in RFC 3339 format
|
|
|
|
return a.date > b.date;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
|
|
|
|
|
|
|
|
pack.versions.swap(unsortedVersions);
|
|
|
|
|
|
|
|
pack.versionsLoaded = true;
|
|
|
|
}
|
|
|
|
|
2022-05-17 01:35:54 +05:30
|
|
|
auto validateDownloadUrl(QUrl url) -> bool
|
2022-05-16 00:28:23 +05:30
|
|
|
{
|
2022-05-29 00:24:00 +05:30
|
|
|
static QSet<QString> domainWhitelist{
|
|
|
|
"cdn.modrinth.com",
|
|
|
|
"github.com",
|
|
|
|
"raw.githubusercontent.com",
|
|
|
|
"gitlab.com"
|
|
|
|
};
|
|
|
|
|
2022-05-16 00:28:23 +05:30
|
|
|
auto domain = url.host();
|
2022-05-29 00:24:00 +05:30
|
|
|
return domainWhitelist.contains(domain);
|
2022-05-16 00:28:23 +05:30
|
|
|
}
|
|
|
|
|
2022-05-15 04:47:05 +05:30
|
|
|
auto loadIndexedVersion(QJsonObject &obj) -> ModpackVersion
|
|
|
|
{
|
|
|
|
ModpackVersion file;
|
|
|
|
|
|
|
|
file.name = Json::requireString(obj, "name");
|
|
|
|
file.version = Json::requireString(obj, "version_number");
|
|
|
|
|
|
|
|
file.id = Json::requireString(obj, "id");
|
|
|
|
file.project_id = Json::requireString(obj, "project_id");
|
|
|
|
|
|
|
|
file.date = Json::requireString(obj, "date_published");
|
|
|
|
|
|
|
|
auto files = Json::requireArray(obj, "files");
|
|
|
|
|
2022-05-15 06:42:51 +05:30
|
|
|
|
2022-05-15 04:47:05 +05:30
|
|
|
for (auto file_iter : files) {
|
|
|
|
File indexed_file;
|
|
|
|
auto parent = Json::requireObject(file_iter);
|
2022-05-15 06:42:51 +05:30
|
|
|
auto is_primary = Json::ensureBoolean(parent, "primary", false);
|
|
|
|
if (!is_primary) {
|
|
|
|
auto filename = Json::ensureString(parent, "filename");
|
|
|
|
// Checking suffix here is fine because it's the response from Modrinth,
|
|
|
|
// so one would assume it will always be in English.
|
|
|
|
if(!filename.endsWith("mrpack") && !filename.endsWith("zip"))
|
|
|
|
continue;
|
2022-05-15 04:47:05 +05:30
|
|
|
}
|
|
|
|
|
2022-05-16 00:28:23 +05:30
|
|
|
auto url = Json::requireString(parent, "url");
|
|
|
|
|
2022-05-17 01:35:54 +05:30
|
|
|
if(!validateDownloadUrl(url))
|
2022-05-16 00:28:23 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
file.download_url = url;
|
2022-05-15 06:42:51 +05:30
|
|
|
if(is_primary)
|
|
|
|
break;
|
2022-05-15 04:47:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if(file.download_url.isEmpty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Modrinth
|