2022-01-14 08:43:42 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include "ModrinthPackIndex.h"
|
|
|
|
|
|
|
|
#include "Json.h"
|
|
|
|
#include "net/NetJob.h"
|
2022-01-15 09:25:24 +00:00
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
|
|
|
|
void Modrinth::loadIndexedPack(Modrinth::IndexedPack & pack, QJsonObject & obj)
|
|
|
|
{
|
|
|
|
pack.addonId = Json::requireString(obj, "mod_id");
|
|
|
|
pack.name = Json::requireString(obj, "title");
|
|
|
|
pack.websiteUrl = Json::ensureString(obj, "page_url", "");
|
|
|
|
pack.description = Json::ensureString(obj, "description", "");
|
|
|
|
|
|
|
|
pack.logoUrl = Json::requireString(obj, "icon_url");
|
2022-01-14 19:22:15 +00:00
|
|
|
pack.logoName = pack.addonId;
|
2022-01-14 08:43:42 +00:00
|
|
|
|
|
|
|
Modrinth::ModpackAuthor packAuthor;
|
|
|
|
packAuthor.name = Json::requireString(obj, "author");
|
|
|
|
packAuthor.url = Json::requireString(obj, "author_url");
|
|
|
|
pack.authors.append(packAuthor); //TODO delete this ? only one author ever exists
|
|
|
|
}
|
|
|
|
|
2022-01-15 09:25:24 +00:00
|
|
|
void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray & arr, const shared_qobject_ptr<QNetworkAccessManager>& network, BaseInstance * inst)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
|
|
|
QVector<Modrinth::IndexedVersion> unsortedVersions;
|
2022-01-15 09:25:24 +00:00
|
|
|
bool hasFabric = !((MinecraftInstance *)inst)->getPackProfile()->getComponentVersion("net.fabricmc.fabric-loader").isEmpty();
|
|
|
|
QString mcVersion = ((MinecraftInstance *)inst)->getPackProfile()->getComponentVersion("net.minecraft");
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
for(auto versionIter: arr) {
|
|
|
|
auto obj = versionIter.toObject();
|
|
|
|
Modrinth::IndexedVersion file;
|
|
|
|
file.addonId = Json::requireString(obj,"mod_id") ;
|
|
|
|
file.fileId = Json::requireString(obj, "id");
|
|
|
|
file.date = Json::requireString(obj, "date_published");
|
|
|
|
auto versionArray = Json::requireArray(obj, "game_versions");
|
|
|
|
if (versionArray.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-15 07:51:47 +00:00
|
|
|
for(auto mcVer : versionArray){
|
|
|
|
file.mcVersion.append(mcVer.toString());
|
|
|
|
}
|
|
|
|
auto loaders = Json::requireArray(obj,"loaders");
|
|
|
|
for(auto loader : loaders){
|
|
|
|
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;
|
|
|
|
while (files.count() > 1 && i < files.count()){
|
|
|
|
//try to resolve the correct file
|
|
|
|
auto parent = files[i].toObject();
|
|
|
|
auto fileName = Json::requireString(parent, "filename");
|
|
|
|
//avoid grabbing "dev" files
|
|
|
|
if(fileName.contains("javadocs",Qt::CaseInsensitive) || fileName.contains("sources",Qt::CaseInsensitive)){
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//grab the correct mod loader
|
|
|
|
if(fileName.contains("forge",Qt::CaseInsensitive) || fileName.contains("fabric",Qt::CaseInsensitive) ){
|
|
|
|
if(hasFabric){
|
|
|
|
if(fileName.contains("forge",Qt::CaseInsensitive)){
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(fileName.contains("fabric",Qt::CaseInsensitive)){
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto parent = files[i].toObject();
|
2022-01-14 11:47:18 +00:00
|
|
|
file.downloadUrl = Json::requireString(parent, "url");
|
|
|
|
file.fileName = Json::requireString(parent, "filename");
|
2022-01-15 07:51:47 +00:00
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
unsortedVersions.append(file);
|
|
|
|
}
|
|
|
|
auto orderSortPredicate = [](const IndexedVersion & a, const IndexedVersion & b) -> bool
|
|
|
|
{
|
|
|
|
//dates are in RFC 3339 format
|
|
|
|
return a.date > b.date;
|
|
|
|
};
|
|
|
|
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
|
|
|
|
pack.versions = unsortedVersions;
|
|
|
|
pack.versionsLoaded = true;
|
|
|
|
}
|