2022-01-16 15:50:21 +05:30
|
|
|
#include "FlameModModel.h"
|
2022-03-03 05:47:10 +05:30
|
|
|
#include "FlameModPage.h"
|
2022-01-16 15:50:21 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
#include <Json.h>
|
2022-01-16 15:50:21 +05:30
|
|
|
|
|
|
|
namespace FlameMod {
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {}
|
2022-01-16 15:50:21 +05:30
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
ListModel::~ListModel() {}
|
2022-01-16 15:50:21 +05:30
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
const char* sorts[6]{ "Featured", "Popularity", "LastUpdated", "Name", "Author", "TotalDownloads" };
|
2022-01-16 15:50:21 +05:30
|
|
|
|
|
|
|
void ListModel::performPaginatedSearch()
|
|
|
|
{
|
2022-03-03 05:47:10 +05:30
|
|
|
QString mcVersion = ((MinecraftInstance*)((FlameModPage*)parent())->m_instance)->getPackProfile()->getComponentVersion("net.minecraft");
|
|
|
|
bool hasFabric = !((MinecraftInstance*)((FlameModPage*)parent())->m_instance)
|
|
|
|
->getPackProfile()
|
|
|
|
->getComponentVersion("net.fabricmc.fabric-loader")
|
|
|
|
.isEmpty();
|
2022-01-16 15:50:21 +05:30
|
|
|
auto netJob = new NetJob("Flame::Search", APPLICATION->network());
|
|
|
|
auto searchUrl = QString(
|
|
|
|
"https://addons-ecs.forgesvc.net/api/v2/addon/search?"
|
|
|
|
"gameId=432&"
|
|
|
|
"categoryId=0&"
|
|
|
|
"sectionId=6&"
|
|
|
|
|
|
|
|
"index=%1&"
|
|
|
|
"pageSize=25&"
|
|
|
|
"searchFilter=%2&"
|
|
|
|
"sort=%3&"
|
2022-02-27 23:35:38 +05:30
|
|
|
"modLoaderType=%4&"
|
2022-03-03 05:47:10 +05:30
|
|
|
"gameVersion=%5")
|
|
|
|
.arg(nextSearchOffset)
|
|
|
|
.arg(currentSearchTerm)
|
|
|
|
.arg(sorts[currentSort])
|
|
|
|
.arg(hasFabric ? 4 : 1) // Enum: https://docs.curseforge.com/?http#tocS_ModLoaderType
|
|
|
|
.arg(mcVersion);
|
2022-01-19 14:17:09 +05:30
|
|
|
|
2022-01-16 15:50:21 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
|
|
|
|
jobPtr = netJob;
|
|
|
|
jobPtr->start();
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &FlameMod::ListModel::searchRequestFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
|
2022-01-16 15:50:21 +05:30
|
|
|
}
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
void FlameMod::ListModel::searchRequestFinished()
|
2022-01-16 15:50:21 +05:30
|
|
|
{
|
|
|
|
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 Flame at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
QList<ModPlatform::IndexedPack> newList;
|
2022-01-16 15:50:21 +05:30
|
|
|
auto packs = doc.array();
|
|
|
|
for(auto packRaw : packs) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
ModPlatform::IndexedPack pack;
|
2022-01-16 15:50:21 +05:30
|
|
|
try
|
|
|
|
{
|
|
|
|
FlameMod::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
|
|
|
}
|
|
|
|
catch(const JSONValidationError &e)
|
|
|
|
{
|
|
|
|
qWarning() << "Error while loading mod from Flame: " << e.cause();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(packs.size() < 25) {
|
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
|
|
|
nextSearchOffset += 25;
|
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
} // namespace FlameMod
|