pollymc/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2022-01-14 14:13:42 +05:30
#include "ModrinthModel.h"
#include "ModrinthPage.h"
2022-01-15 13:21:47 +05:30
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
2022-01-14 14:13:42 +05:30
#include <Json.h>
2022-01-14 14:13:42 +05:30
namespace Modrinth {
ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {}
2022-01-14 14:13:42 +05:30
ListModel::~ListModel() {}
2022-01-14 14:13:42 +05:30
const char* sorts[5]{ "relevance", "downloads", "follows", "updated", "newest" };
2022-01-14 14:13:42 +05:30
void ListModel::performPaginatedSearch()
{
QString mcVersion = ((MinecraftInstance*)((ModrinthPage*)parent())->m_instance)->getPackProfile()->getComponentVersion("net.minecraft");
bool hasFabric = !((MinecraftInstance*)((ModrinthPage*)parent())->m_instance)
->getPackProfile()
->getComponentVersion("net.fabricmc.fabric-loader")
.isEmpty();
2022-01-15 13:21:47 +05:30
auto netJob = new NetJob("Modrinth::Search", APPLICATION->network());
2022-01-14 14:13:42 +05:30
auto searchUrl = QString(
2022-01-31 21:48:11 +05:30
"https://api.modrinth.com/v2/search?"
2022-01-14 14:13:42 +05:30
"offset=%1&"
"limit=25&"
"query=%2&"
2022-01-15 13:21:47 +05:30
"index=%3&"
"facets=[[\"categories:%4\"],[\"versions:%5\"],[\"project_type:mod\"]]")
.arg(nextSearchOffset)
.arg(currentSearchTerm)
.arg(sorts[currentSort])
.arg(hasFabric ? "fabric" : "forge")
.arg(mcVersion);
2022-01-19 14:17:09 +05:30
2022-01-14 14:13:42 +05:30
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
jobPtr = netJob;
jobPtr->start();
QObject::connect(netJob, &NetJob::succeeded, this, &Modrinth::ListModel::searchRequestFinished);
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
2022-01-14 14:13:42 +05:30
}
void Modrinth::ListModel::searchRequestFinished()
{
jobPtr.reset();
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
<< " reason: " << parse_error.errorString();
2022-01-14 14:13:42 +05:30
qWarning() << response;
return;
}
QList<ModPlatform::IndexedPack> newList;
2022-01-14 14:13:42 +05:30
auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) {
2022-01-14 14:13:42 +05:30
auto packObj = packRaw.toObject();
ModPlatform::IndexedPack pack;
try {
2022-01-14 14:13:42 +05:30
Modrinth::loadIndexedPack(pack, packObj);
newList.append(pack);
} catch (const JSONValidationError& e) {
2022-01-14 14:13:42 +05:30
qWarning() << "Error while loading mod from Modrinth: " << e.cause();
continue;
}
}
if (packs.size() < 25) {
2022-01-14 14:13:42 +05:30
searchState = Finished;
} else {
nextSearchOffset += 25;
searchState = CanPossiblyFetchMore;
}
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
modpacks.append(newList);
endInsertRows();
}
} // namespace Modrinth