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