2022-03-03 05:47:10 +05:30
|
|
|
#include "ModModel.h"
|
|
|
|
|
2022-03-08 02:16:18 +05:30
|
|
|
#include "Json.h"
|
2022-07-15 18:04:11 +05:30
|
|
|
#include "ModPage.h"
|
2022-03-03 07:31:23 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-07-15 06:53:41 +05:30
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
namespace ModPlatform {
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
ListModel::ListModel(ModPage* parent, ResourceAPI* api) : ResourceModel(parent, api) {}
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
/******** Make data requests ********/
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::SearchArgs ListModel::createSearchArguments()
|
2022-03-08 03:59:59 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto profile = static_cast<MinecraftInstance&>(m_associated_page->m_base_instance).getPackProfile();
|
|
|
|
return { ModPlatform::ResourceType::MOD, m_next_search_offset, m_search_term,
|
|
|
|
getSorts()[currentSort], profile->getModLoaders(), getMineVersions() };
|
2022-03-08 03:59:59 +05:30
|
|
|
}
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::SearchCallbacks ListModel::createSearchCallbacks()
|
2022-03-03 05:47:10 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
return { [this](auto& doc) {
|
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
|
|
|
searchRequestFinished(doc);
|
|
|
|
} };
|
2022-03-03 05:47:10 +05:30
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::VersionSearchArgs ListModel::createVersionsArguments(QModelIndex& entry)
|
2022-07-19 03:47:44 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto const& pack = m_packs[entry.row()];
|
|
|
|
auto profile = static_cast<MinecraftInstance&>(m_associated_page->m_base_instance).getPackProfile();
|
2022-07-19 03:47:44 +05:30
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
return { pack.addonId.toString(), getMineVersions(), profile->getModLoaders() };
|
2022-07-19 03:47:44 +05:30
|
|
|
}
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::VersionSearchCallbacks ListModel::createVersionsCallbacks(QModelIndex& entry)
|
2022-03-08 01:16:08 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto const& pack = m_packs[entry.row()];
|
2022-02-21 01:25:26 +05:30
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
return { [this, pack, entry](auto& doc, auto addonId) {
|
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
|
|
|
versionRequestSucceeded(doc, addonId, entry);
|
|
|
|
} };
|
2022-03-03 07:31:23 +05:30
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::ProjectInfoArgs ListModel::createInfoArguments(QModelIndex& entry)
|
2022-05-24 18:08:48 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto& pack = m_packs[entry.row()];
|
|
|
|
return { pack };
|
2022-05-24 18:08:48 +05:30
|
|
|
}
|
2022-11-25 17:53:46 +05:30
|
|
|
ResourceAPI::ProjectInfoCallbacks ListModel::createInfoCallbacks(QModelIndex& entry)
|
2022-03-03 05:47:10 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
return { [this, entry](auto& doc, auto& pack) {
|
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
|
|
|
infoRequestFinished(doc, pack, entry);
|
|
|
|
} };
|
2022-03-03 05:47:10 +05:30
|
|
|
}
|
|
|
|
|
2022-04-14 18:57:03 +05:30
|
|
|
void ListModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
|
2022-04-03 03:51:02 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
if (m_search_term == term && m_search_term.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
|
2022-05-08 12:52:50 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-04-14 18:57:03 +05:30
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
setSearchTerm(term);
|
2022-04-03 03:51:02 +05:30
|
|
|
currentSort = sort;
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:59:59 +05:30
|
|
|
/******** Request callbacks ********/
|
|
|
|
|
2022-03-08 02:16:18 +05:30
|
|
|
void ListModel::searchRequestFinished(QJsonDocument& doc)
|
|
|
|
{
|
|
|
|
QList<ModPlatform::IndexedPack> newList;
|
|
|
|
auto packs = documentToArray(doc);
|
|
|
|
|
|
|
|
for (auto packRaw : packs) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
try {
|
|
|
|
loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
|
|
|
} catch (const JSONValidationError& e) {
|
2022-11-25 17:53:46 +05:30
|
|
|
qWarning() << "Error while loading mod from " << m_associated_page->debugName() << ": " << e.cause();
|
2022-03-08 02:16:18 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packs.size() < 25) {
|
2022-11-25 17:53:46 +05:30
|
|
|
m_search_state = SearchState::Finished;
|
2022-03-08 02:16:18 +05:30
|
|
|
} else {
|
2022-11-25 17:53:46 +05:30
|
|
|
m_next_search_offset += 25;
|
|
|
|
m_search_state = SearchState::CanFetchMore;
|
2022-03-08 02:16:18 +05:30
|
|
|
}
|
|
|
|
|
2022-06-25 04:39:44 +05:30
|
|
|
// When you have a Qt build with assertions turned on, proceeding here will abort the application
|
|
|
|
if (newList.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
beginInsertRows(QModelIndex(), m_packs.size(), m_packs.size() + newList.size() - 1);
|
|
|
|
m_packs.append(newList);
|
2022-03-08 02:16:18 +05:30
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-07-19 03:47:44 +05:30
|
|
|
void ListModel::infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack& pack, const QModelIndex& index)
|
2022-05-24 18:08:48 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Loading mod info";
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto obj = Json::requireObject(doc);
|
|
|
|
loadExtraPackInfo(pack, obj);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading " << debugName() << " mod info: " << e.cause();
|
|
|
|
}
|
|
|
|
|
2022-07-19 03:47:44 +05:30
|
|
|
// Check if the index is still valid for this mod or not
|
|
|
|
if (pack.addonId == data(index, Qt::UserRole).value<ModPlatform::IndexedPack>().addonId) {
|
|
|
|
// Cache info :^)
|
|
|
|
QVariant new_pack;
|
|
|
|
new_pack.setValue(pack);
|
|
|
|
if (!setData(index, new_pack, Qt::UserRole)) {
|
|
|
|
qWarning() << "Failed to cache mod info!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
m_associated_page->updateUi();
|
2022-05-24 18:08:48 +05:30
|
|
|
}
|
|
|
|
|
2022-07-19 03:58:46 +05:30
|
|
|
void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId, const QModelIndex& index)
|
2022-03-08 00:52:57 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto current = m_associated_page->getCurrentPack();
|
2022-05-08 12:52:50 +05:30
|
|
|
if (addonId != current.addonId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
2022-03-25 03:09:53 +05:30
|
|
|
|
2022-03-08 03:59:59 +05:30
|
|
|
try {
|
|
|
|
loadIndexedPackVersions(current, arr);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading " << debugName() << " mod version: " << e.cause();
|
|
|
|
}
|
2022-03-03 05:47:10 +05:30
|
|
|
|
2022-07-19 03:58:46 +05:30
|
|
|
// Cache info :^)
|
|
|
|
QVariant new_pack;
|
|
|
|
new_pack.setValue(current);
|
|
|
|
if (!setData(index, new_pack, Qt::UserRole)) {
|
|
|
|
qWarning() << "Failed to cache mod versions!";
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
m_associated_page->updateVersionList();
|
2022-03-03 05:47:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ModPlatform
|
2022-03-25 03:09:53 +05:30
|
|
|
|
|
|
|
/******** Helpers ********/
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
#define MOD_PAGE(x) static_cast<ModPage*>(x)
|
|
|
|
|
|
|
|
auto ModPlatform::ListModel::getMineVersions() const -> std::optional<std::list<Version>>
|
2022-03-25 03:09:53 +05:30
|
|
|
{
|
2022-11-25 17:53:46 +05:30
|
|
|
auto versions = MOD_PAGE(m_associated_page)->getFilter()->versions;
|
|
|
|
if (!versions.empty())
|
|
|
|
return versions;
|
|
|
|
return {};
|
2022-03-25 03:09:53 +05:30
|
|
|
}
|