2022-03-03 00:17:10 +00:00
|
|
|
#include "ModModel.h"
|
|
|
|
|
2022-03-07 20:46:18 +00:00
|
|
|
#include "Json.h"
|
2022-12-18 18:41:46 +00:00
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-07-15 02:23:41 +01:00
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
namespace ResourceDownload {
|
2022-03-03 00:17:10 +00:00
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
ModModel::ModModel(BaseInstance const& base_inst, ResourceAPI* api) : ResourceModel(base_inst, api) {}
|
2022-03-07 22:29:59 +00:00
|
|
|
|
|
|
|
/******** Make data requests ********/
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::SearchArgs ModModel::createSearchArguments()
|
2022-03-07 22:29:59 +00:00
|
|
|
{
|
2022-12-18 18:41:46 +00:00
|
|
|
auto profile = static_cast<MinecraftInstance const&>(m_base_instance).getPackProfile();
|
|
|
|
|
|
|
|
Q_ASSERT(profile);
|
|
|
|
Q_ASSERT(m_filter);
|
|
|
|
|
|
|
|
std::optional<std::list<Version>> versions {};
|
|
|
|
if (!m_filter->versions.empty())
|
|
|
|
versions = m_filter->versions;
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
return { ModPlatform::ResourceType::MOD, m_next_search_offset, m_search_term,
|
2022-12-18 18:41:46 +00:00
|
|
|
getSorts()[currentSort], profile->getModLoaders(), versions };
|
2022-03-07 22:29:59 +00:00
|
|
|
}
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::SearchCallbacks ModModel::createSearchCallbacks()
|
2022-03-03 00:17:10 +00:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
return { [this](auto& doc) {
|
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
|
|
|
searchRequestFinished(doc);
|
|
|
|
} };
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::VersionSearchArgs ModModel::createVersionsArguments(QModelIndex& entry)
|
2022-07-18 23:17:44 +01:00
|
|
|
{
|
2022-12-18 18:41:46 +00:00
|
|
|
auto& pack = m_packs[entry.row()];
|
|
|
|
auto profile = static_cast<MinecraftInstance const&>(m_base_instance).getPackProfile();
|
|
|
|
|
|
|
|
Q_ASSERT(profile);
|
|
|
|
Q_ASSERT(m_filter);
|
2022-07-18 23:17:44 +01:00
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
std::optional<std::list<Version>> versions {};
|
|
|
|
if (!m_filter->versions.empty())
|
|
|
|
versions = m_filter->versions;
|
|
|
|
|
|
|
|
return { pack, versions, profile->getModLoaders() };
|
2022-07-18 23:17:44 +01:00
|
|
|
}
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::VersionSearchCallbacks ModModel::createVersionsCallbacks(QModelIndex& entry)
|
2022-03-07 19:46:08 +00:00
|
|
|
{
|
2022-12-18 18:41:46 +00:00
|
|
|
return { [this, entry](auto& doc, auto& pack) {
|
2022-11-25 12:23:46 +00:00
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
2022-12-18 18:41:46 +00:00
|
|
|
versionRequestSucceeded(doc, pack, entry);
|
2022-11-25 12:23:46 +00:00
|
|
|
} };
|
2022-03-03 02:01:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::ProjectInfoArgs ModModel::createInfoArguments(QModelIndex& entry)
|
2022-05-24 13:38:48 +01:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
auto& pack = m_packs[entry.row()];
|
|
|
|
return { pack };
|
2022-05-24 13:38:48 +01:00
|
|
|
}
|
2022-12-16 22:03:52 +00:00
|
|
|
ResourceAPI::ProjectInfoCallbacks ModModel::createInfoCallbacks(QModelIndex& entry)
|
2022-03-03 00:17:10 +00:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
return { [this, entry](auto& doc, auto& pack) {
|
|
|
|
if (!s_running_models.constFind(this).value())
|
|
|
|
return;
|
|
|
|
infoRequestFinished(doc, pack, entry);
|
|
|
|
} };
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
void ModModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
|
2022-04-02 23:21:02 +01:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
if (m_search_term == term && m_search_term.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
|
2022-05-08 08:22:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-04-14 14:27:03 +01:00
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
setSearchTerm(term);
|
2022-04-02 23:21:02 +01:00
|
|
|
currentSort = sort;
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
/******** Request callbacks ********/
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
void ModModel::searchRequestFinished(QJsonDocument& doc)
|
2022-03-07 20:46:18 +00:00
|
|
|
{
|
|
|
|
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-12-18 18:41:46 +00:00
|
|
|
qWarning() << "Error while loading mod from " << debugName() << ": " << e.cause();
|
2022-03-07 20:46:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packs.size() < 25) {
|
2022-11-25 12:23:46 +00:00
|
|
|
m_search_state = SearchState::Finished;
|
2022-03-07 20:46:18 +00:00
|
|
|
} else {
|
2022-11-25 12:23:46 +00:00
|
|
|
m_next_search_offset += 25;
|
|
|
|
m_search_state = SearchState::CanFetchMore;
|
2022-03-07 20:46:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 00:09:44 +01:00
|
|
|
// When you have a Qt build with assertions turned on, proceeding here will abort the application
|
|
|
|
if (newList.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
beginInsertRows(QModelIndex(), m_packs.size(), m_packs.size() + newList.size() - 1);
|
|
|
|
m_packs.append(newList);
|
2022-03-07 20:46:18 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
void ModModel::infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack& pack, const QModelIndex& index)
|
2022-05-24 13:38:48 +01:00
|
|
|
{
|
|
|
|
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-18 23:17:44 +01:00
|
|
|
// 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-12-18 18:41:46 +00:00
|
|
|
return;
|
2022-07-18 23:17:44 +01:00
|
|
|
}
|
2022-12-18 18:41:46 +00:00
|
|
|
|
|
|
|
emit projectInfoUpdated();
|
2022-07-18 23:17:44 +01:00
|
|
|
}
|
2022-05-24 13:38:48 +01:00
|
|
|
}
|
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
void ModModel::versionRequestSucceeded(QJsonDocument doc, ModPlatform::IndexedPack& pack, const QModelIndex& index)
|
2022-03-07 19:22:57 +00:00
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
2022-03-24 21:39:53 +00:00
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
try {
|
2022-12-18 18:41:46 +00:00
|
|
|
loadIndexedPackVersions(pack, arr);
|
2022-03-07 22:29:59 +00:00
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading " << debugName() << " mod version: " << e.cause();
|
|
|
|
}
|
2022-03-03 00:17:10 +00:00
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
// 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 versions!";
|
|
|
|
return;
|
|
|
|
}
|
2022-11-25 12:23:46 +00:00
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
emit versionListUpdated();
|
|
|
|
}
|
2022-03-24 21:39:53 +00:00
|
|
|
}
|
2022-12-16 22:03:52 +00:00
|
|
|
|
|
|
|
} // namespace ResourceDownload
|