refactor: generalize mod models and APIs to resources
Firstly, this abstract away behavior in the mod download models that can also be applied to other types of resources into a superclass, allowing other resource types to be implemented without so much code duplication. For that, this also generalizes the APIs used (currently, ModrinthAPI and FlameAPI) to be able to make requests to other types of resources. It also does a general cleanup of both of those. In particular, this makes use of std::optional instead of invalid values for errors and, well, optional values :p This is a squash of some commits that were becoming too interlaced together to be cleanly separated. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
@ -1,226 +1,81 @@
|
||||
#include "ModModel.h"
|
||||
|
||||
#include "BuildConfig.h"
|
||||
#include "Json.h"
|
||||
#include "ModPage.h"
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
#include "minecraft/PackProfile.h"
|
||||
#include "ui/dialogs/ModDownloadDialog.h"
|
||||
|
||||
#include "ui/widgets/ProjectItem.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
|
||||
namespace ModPlatform {
|
||||
|
||||
// HACK: We need this to prevent callbacks from calling the ListModel after it has already been deleted.
|
||||
// This leaks a tiny bit of memory per time the user has opened the mod dialog. How to make this better?
|
||||
static QHash<ListModel*, bool> s_running;
|
||||
|
||||
ListModel::ListModel(ModPage* parent) : QAbstractListModel(parent), m_parent(parent) { s_running.insert(this, true); }
|
||||
|
||||
ListModel::~ListModel()
|
||||
{
|
||||
s_running.find(this).value() = false;
|
||||
}
|
||||
|
||||
auto ListModel::debugName() const -> QString
|
||||
{
|
||||
return m_parent->debugName();
|
||||
}
|
||||
ListModel::ListModel(ModPage* parent, ResourceAPI* api) : ResourceModel(parent, api) {}
|
||||
|
||||
/******** Make data requests ********/
|
||||
|
||||
void ListModel::fetchMore(const QModelIndex& parent)
|
||||
ResourceAPI::SearchArgs ListModel::createSearchArguments()
|
||||
{
|
||||
if (parent.isValid())
|
||||
return;
|
||||
if (nextSearchOffset == 0) {
|
||||
qWarning() << "fetchMore with 0 offset is wrong...";
|
||||
return;
|
||||
}
|
||||
performPaginatedSearch();
|
||||
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() };
|
||||
}
|
||||
|
||||
auto ListModel::data(const QModelIndex& index, int role) const -> QVariant
|
||||
ResourceAPI::SearchCallbacks ListModel::createSearchCallbacks()
|
||||
{
|
||||
int pos = index.row();
|
||||
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
||||
return QString("INVALID INDEX %1").arg(pos);
|
||||
}
|
||||
|
||||
ModPlatform::IndexedPack pack = modpacks.at(pos);
|
||||
switch (role) {
|
||||
case Qt::ToolTipRole: {
|
||||
if (pack.description.length() > 100) {
|
||||
// some magic to prevent to long tooltips and replace html linebreaks
|
||||
QString edit = pack.description.left(97);
|
||||
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
|
||||
return edit;
|
||||
}
|
||||
return pack.description;
|
||||
}
|
||||
case Qt::DecorationRole: {
|
||||
if (m_logoMap.contains(pack.logoName)) {
|
||||
return m_logoMap.value(pack.logoName);
|
||||
}
|
||||
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
||||
// un-const-ify this
|
||||
((ListModel*)this)->requestLogo(pack.logoName, pack.logoUrl);
|
||||
return icon;
|
||||
}
|
||||
case Qt::SizeHintRole:
|
||||
return QSize(0, 58);
|
||||
case Qt::UserRole: {
|
||||
QVariant v;
|
||||
v.setValue(pack);
|
||||
return v;
|
||||
}
|
||||
// Custom data
|
||||
case UserDataTypes::TITLE:
|
||||
return pack.name;
|
||||
case UserDataTypes::DESCRIPTION:
|
||||
return pack.description;
|
||||
case UserDataTypes::SELECTED:
|
||||
return m_parent->getDialog()->isModSelected(pack.name);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
int pos = index.row();
|
||||
if (pos >= modpacks.size() || pos < 0 || !index.isValid())
|
||||
return false;
|
||||
|
||||
modpacks[pos] = value.value<ModPlatform::IndexedPack>();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current, QModelIndex index)
|
||||
{
|
||||
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
|
||||
|
||||
m_parent->apiProvider()->getVersions({ current.addonId.toString(), getMineVersions(), profile->getModLoaders() },
|
||||
[this, current, index](QJsonDocument& doc, QString addonId) {
|
||||
if (!s_running.constFind(this).value())
|
||||
return;
|
||||
versionRequestSucceeded(doc, addonId, index);
|
||||
});
|
||||
}
|
||||
|
||||
void ListModel::performPaginatedSearch()
|
||||
{
|
||||
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
|
||||
|
||||
m_parent->apiProvider()->searchMods(
|
||||
this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoaders(), getMineVersions() });
|
||||
}
|
||||
|
||||
void ListModel::requestModInfo(ModPlatform::IndexedPack& current, QModelIndex index)
|
||||
{
|
||||
m_parent->apiProvider()->getModInfo(current, [this, index](QJsonDocument& doc, ModPlatform::IndexedPack& pack) {
|
||||
if (!s_running.constFind(this).value())
|
||||
return { [this](auto& doc) {
|
||||
if (!s_running_models.constFind(this).value())
|
||||
return;
|
||||
infoRequestFinished(doc, pack, index);
|
||||
});
|
||||
searchRequestFinished(doc);
|
||||
} };
|
||||
}
|
||||
|
||||
void ListModel::refresh()
|
||||
ResourceAPI::VersionSearchArgs ListModel::createVersionsArguments(QModelIndex& entry)
|
||||
{
|
||||
if (jobPtr) {
|
||||
jobPtr->abort();
|
||||
searchState = ResetRequested;
|
||||
return;
|
||||
} else {
|
||||
beginResetModel();
|
||||
modpacks.clear();
|
||||
endResetModel();
|
||||
searchState = None;
|
||||
}
|
||||
nextSearchOffset = 0;
|
||||
performPaginatedSearch();
|
||||
auto const& pack = m_packs[entry.row()];
|
||||
auto profile = static_cast<MinecraftInstance&>(m_associated_page->m_base_instance).getPackProfile();
|
||||
|
||||
return { pack.addonId.toString(), getMineVersions(), profile->getModLoaders() };
|
||||
}
|
||||
ResourceAPI::VersionSearchCallbacks ListModel::createVersionsCallbacks(QModelIndex& entry)
|
||||
{
|
||||
auto const& pack = m_packs[entry.row()];
|
||||
|
||||
return { [this, pack, entry](auto& doc, auto addonId) {
|
||||
if (!s_running_models.constFind(this).value())
|
||||
return;
|
||||
versionRequestSucceeded(doc, addonId, entry);
|
||||
} };
|
||||
}
|
||||
|
||||
ResourceAPI::ProjectInfoArgs ListModel::createInfoArguments(QModelIndex& entry)
|
||||
{
|
||||
auto& pack = m_packs[entry.row()];
|
||||
return { pack };
|
||||
}
|
||||
ResourceAPI::ProjectInfoCallbacks ListModel::createInfoCallbacks(QModelIndex& entry)
|
||||
{
|
||||
return { [this, entry](auto& doc, auto& pack) {
|
||||
if (!s_running_models.constFind(this).value())
|
||||
return;
|
||||
infoRequestFinished(doc, pack, entry);
|
||||
} };
|
||||
}
|
||||
|
||||
void ListModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
|
||||
{
|
||||
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
|
||||
if (m_search_term == term && m_search_term.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentSearchTerm = term;
|
||||
setSearchTerm(term);
|
||||
currentSort = sort;
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
||||
{
|
||||
if (m_logoMap.contains(logo)) {
|
||||
callback(APPLICATION->metacache()
|
||||
->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))
|
||||
->getFullPath());
|
||||
} else {
|
||||
requestLogo(logo, logoUrl);
|
||||
}
|
||||
}
|
||||
|
||||
void ListModel::requestLogo(QString logo, QString url)
|
||||
{
|
||||
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo) || url.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
MetaEntryPtr entry =
|
||||
APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)));
|
||||
auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network());
|
||||
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
|
||||
|
||||
auto fullPath = entry->getFullPath();
|
||||
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath, job] {
|
||||
job->deleteLater();
|
||||
emit logoLoaded(logo, QIcon(fullPath));
|
||||
if (waitingCallbacks.contains(logo)) {
|
||||
waitingCallbacks.value(logo)(fullPath);
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
||||
job->deleteLater();
|
||||
emit logoFailed(logo);
|
||||
});
|
||||
|
||||
job->start();
|
||||
m_loadingLogos.append(logo);
|
||||
}
|
||||
|
||||
/******** Request callbacks ********/
|
||||
|
||||
void ListModel::logoLoaded(QString logo, QIcon out)
|
||||
{
|
||||
m_loadingLogos.removeAll(logo);
|
||||
m_logoMap.insert(logo, out);
|
||||
for (int i = 0; i < modpacks.size(); i++) {
|
||||
if (modpacks[i].logoName == logo) {
|
||||
emit dataChanged(createIndex(i, 0), createIndex(i, 0), { Qt::DecorationRole });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ListModel::logoFailed(QString logo)
|
||||
{
|
||||
m_failedLogos.append(logo);
|
||||
m_loadingLogos.removeAll(logo);
|
||||
}
|
||||
|
||||
void ListModel::searchRequestFinished(QJsonDocument& doc)
|
||||
{
|
||||
jobPtr.reset();
|
||||
|
||||
QList<ModPlatform::IndexedPack> newList;
|
||||
auto packs = documentToArray(doc);
|
||||
|
||||
@ -232,62 +87,27 @@ void ListModel::searchRequestFinished(QJsonDocument& doc)
|
||||
loadIndexedPack(pack, packObj);
|
||||
newList.append(pack);
|
||||
} catch (const JSONValidationError& e) {
|
||||
qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause();
|
||||
qWarning() << "Error while loading mod from " << m_associated_page->debugName() << ": " << e.cause();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (packs.size() < 25) {
|
||||
searchState = Finished;
|
||||
m_search_state = SearchState::Finished;
|
||||
} else {
|
||||
nextSearchOffset += 25;
|
||||
searchState = CanPossiblyFetchMore;
|
||||
m_next_search_offset += 25;
|
||||
m_search_state = SearchState::CanFetchMore;
|
||||
}
|
||||
|
||||
// When you have a Qt build with assertions turned on, proceeding here will abort the application
|
||||
if (newList.size() == 0)
|
||||
return;
|
||||
|
||||
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
||||
modpacks.append(newList);
|
||||
beginInsertRows(QModelIndex(), m_packs.size(), m_packs.size() + newList.size() - 1);
|
||||
m_packs.append(newList);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void ListModel::searchRequestFailed(QString reason)
|
||||
{
|
||||
auto failed_action = jobPtr->getFailedActions().at(0);
|
||||
if (!failed_action->m_reply) {
|
||||
// Network error
|
||||
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load mods."));
|
||||
} else if (failed_action->m_reply && failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
|
||||
// 409 Gone, notify user to update
|
||||
QMessageBox::critical(nullptr, tr("Error"),
|
||||
//: %1 refers to the launcher itself
|
||||
QString("%1 %2")
|
||||
.arg(m_parent->displayName())
|
||||
.arg(tr("API version too old!\nPlease update %1!").arg(BuildConfig.LAUNCHER_DISPLAYNAME)));
|
||||
}
|
||||
|
||||
jobPtr.reset();
|
||||
searchState = Finished;
|
||||
}
|
||||
|
||||
void ListModel::searchRequestAborted()
|
||||
{
|
||||
if (searchState != ResetRequested)
|
||||
qCritical() << "Search task in ModModel aborted by an unknown reason!";
|
||||
|
||||
// Retry fetching
|
||||
jobPtr.reset();
|
||||
|
||||
beginResetModel();
|
||||
modpacks.clear();
|
||||
endResetModel();
|
||||
|
||||
nextSearchOffset = 0;
|
||||
performPaginatedSearch();
|
||||
}
|
||||
|
||||
void ListModel::infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack& pack, const QModelIndex& index)
|
||||
{
|
||||
qDebug() << "Loading mod info";
|
||||
@ -310,12 +130,12 @@ void ListModel::infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack
|
||||
}
|
||||
}
|
||||
|
||||
m_parent->updateUi();
|
||||
m_associated_page->updateUi();
|
||||
}
|
||||
|
||||
void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId, const QModelIndex& index)
|
||||
{
|
||||
auto& current = m_parent->getCurrent();
|
||||
auto current = m_associated_page->getCurrentPack();
|
||||
if (addonId != current.addonId) {
|
||||
return;
|
||||
}
|
||||
@ -336,15 +156,19 @@ void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId, cons
|
||||
qWarning() << "Failed to cache mod versions!";
|
||||
}
|
||||
|
||||
|
||||
m_parent->updateModVersions();
|
||||
m_associated_page->updateVersionList();
|
||||
}
|
||||
|
||||
} // namespace ModPlatform
|
||||
|
||||
/******** Helpers ********/
|
||||
|
||||
auto ModPlatform::ListModel::getMineVersions() const -> std::list<Version>
|
||||
#define MOD_PAGE(x) static_cast<ModPage*>(x)
|
||||
|
||||
auto ModPlatform::ListModel::getMineVersions() const -> std::optional<std::list<Version>>
|
||||
{
|
||||
return m_parent->getFilter()->versions;
|
||||
auto versions = MOD_PAGE(m_associated_page)->getFilter()->versions;
|
||||
if (!versions.empty())
|
||||
return versions;
|
||||
return {};
|
||||
}
|
||||
|
Reference in New Issue
Block a user