chore(clang-tidy): fix clang tidy warnings

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2026-05-07 10:22:37 +03:00
parent 4c9081a934
commit bac959bc6f
No known key found for this signature in database
GPG key ID: 55EF5DA53DB36318
23 changed files with 650 additions and 546 deletions

View file

@ -216,7 +216,7 @@ static std::pair<Version, Version> map(int format, const QMap<std::pair<int, int
int DataPack::compare(const Resource& other, SortType type) const int DataPack::compare(const Resource& other, SortType type) const
{ {
const auto& cast_other = static_cast<const DataPack&>(other); const auto& cast_other = static_cast<const DataPack&>(other);
if (type == SortType::PACK_FORMAT) { if (type == SortType::PackFormat) {
auto this_ver = packFormat(); auto this_ver = packFormat();
auto other_ver = cast_other.packFormat(); auto other_ver = cast_other.packFormat();

View file

@ -40,26 +40,25 @@
#include <QIcon> #include <QIcon>
#include <QStyle> #include <QStyle>
#include "Version.h"
#include "minecraft/mod/tasks/LocalDataPackParseTask.h" #include "minecraft/mod/tasks/LocalDataPackParseTask.h"
DataPackFolderModel::DataPackFolderModel(const QString& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) DataPackFolderModel::DataPackFolderModel(const QString& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent)
{ {
m_column_names = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "File Name" }); m_columnNames = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "File Name" });
m_column_names_translated = m_columnNamesTranslated =
QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Pack Format"), tr("Last Modified"), tr("File Name") }); QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Pack Format"), tr("Last Modified"), tr("File Name") });
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::PACK_FORMAT, SortType::DATE, SortType::FILENAME }; m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::PackFormat, SortType::Date, SortType::Filename };
m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive };
m_columnsHideable = { false, true, false, true, true, true }; m_columnsHideable = { false, true, false, true, true, true };
} }
QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const
{ {
if (!validateIndex(index)) if (!validateIndex(index)) {
return {}; return {};
}
int row = index.row(); int row = index.row();
int column = index.column(); int column = index.column();
@ -68,12 +67,10 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const
case Qt::BackgroundRole: case Qt::BackgroundRole:
return rowBackground(row); return rowBackground(row);
case Qt::DisplayRole: case Qt::DisplayRole:
switch (column) { if (column == PackFormatColumn) {
case PackFormatColumn: {
const auto& resource = at(row); const auto& resource = at(row);
return resource.packFormatStr(); return resource.packFormatStr();
} }
}
break; break;
case Qt::DecorationRole: { case Qt::DecorationRole: {
if (column == ImageColumn) { if (column == ImageColumn) {
@ -93,6 +90,8 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const
return QSize(32, 32); return QSize(32, 32);
} }
break; break;
default:
break;
} }
// map the columns to the base equivilents // map the columns to the base equivilents
@ -114,6 +113,8 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const
mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn);
break; break;
// FIXME: there is no size column due to an oversight // FIXME: there is no size column due to an oversight
default:
break;
} }
if (mappedIndex.isValid()) { if (mappedIndex.isValid()) {
@ -167,7 +168,7 @@ QVariant DataPackFolderModel::headerData(int section, [[maybe_unused]] Qt::Orien
int DataPackFolderModel::columnCount(const QModelIndex& parent) const int DataPackFolderModel::columnCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : NUM_COLUMNS; return parent.isValid() ? 0 : NumColumns;
} }
Resource* DataPackFolderModel::createResource(const QFileInfo& file) Resource* DataPackFolderModel::createResource(const QFileInfo& file)
@ -177,5 +178,5 @@ Resource* DataPackFolderModel::createResource(const QFileInfo& file)
Task* DataPackFolderModel::createParseTask(Resource& resource) Task* DataPackFolderModel::createParseTask(Resource& resource)
{ {
return new LocalDataPackParseTask(m_next_resolution_ticket, static_cast<DataPack*>(&resource)); return new LocalDataPackParseTask(m_nextResolutionTicket, static_cast<DataPack*>(&resource));
} }

View file

@ -39,16 +39,15 @@
#include "ResourceFolderModel.h" #include "ResourceFolderModel.h"
#include "DataPack.h" #include "DataPack.h"
#include "ResourcePack.h"
class DataPackFolderModel : public ResourceFolderModel { class DataPackFolderModel : public ResourceFolderModel {
Q_OBJECT Q_OBJECT
public: public:
enum Columns { ActiveColumn = 0, ImageColumn, NameColumn, PackFormatColumn, DateColumn, FileNameColumn, NUM_COLUMNS }; enum Columns : std::uint8_t { ActiveColumn = 0, ImageColumn, NameColumn, PackFormatColumn, DateColumn, FileNameColumn, NumColumns };
explicit DataPackFolderModel(const QString& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent = nullptr); explicit DataPackFolderModel(const QString& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent = nullptr);
virtual QString id() const override { return "datapacks"; } QString id() const override { return "datapacks"; }
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
@ -56,7 +55,7 @@ class DataPackFolderModel : public ResourceFolderModel {
int columnCount(const QModelIndex& parent) const override; int columnCount(const QModelIndex& parent) const override;
[[nodiscard]] Resource* createResource(const QFileInfo& file) override; [[nodiscard]] Resource* createResource(const QFileInfo& file) override;
[[nodiscard]] Task* createParseTask(Resource&) override; [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override;
RESOURCE_HELPERS(DataPack) RESOURCE_HELPERS(DataPack)
}; };

View file

@ -61,18 +61,18 @@ void Mod::setDetails(const ModDetails& details)
int Mod::compare(const Resource& other, SortType type) const int Mod::compare(const Resource& other, SortType type) const
{ {
auto cast_other = dynamic_cast<Mod const*>(&other); auto cast_other = dynamic_cast<const Mod*>(&other);
if (!cast_other) if (!cast_other)
return Resource::compare(other, type); return Resource::compare(other, type);
switch (type) { switch (type) {
default: default:
case SortType::ENABLED: case SortType::Enabled:
case SortType::NAME: case SortType::Name:
case SortType::DATE: case SortType::Date:
case SortType::SIZE: case SortType::Size:
return Resource::compare(other, type); return Resource::compare(other, type);
case SortType::VERSION: { case SortType::Version: {
auto this_ver = Version(version()); auto this_ver = Version(version());
auto other_ver = Version(cast_other->version()); auto other_ver = Version(cast_other->version());
if (this_ver > other_ver) if (this_ver > other_ver)
@ -81,38 +81,38 @@ int Mod::compare(const Resource& other, SortType type) const
return -1; return -1;
break; break;
} }
case SortType::SIDE: { case SortType::Side: {
auto compare_result = QString::compare(side(), cast_other->side(), Qt::CaseInsensitive); auto compare_result = QString::compare(side(), cast_other->side(), Qt::CaseInsensitive);
if (compare_result != 0) if (compare_result != 0)
return compare_result; return compare_result;
break; break;
} }
case SortType::MC_VERSIONS: { case SortType::McVersions: {
auto compare_result = QString::compare(mcVersions(), cast_other->mcVersions(), Qt::CaseInsensitive); auto compare_result = QString::compare(mcVersions(), cast_other->mcVersions(), Qt::CaseInsensitive);
if (compare_result != 0) if (compare_result != 0)
return compare_result; return compare_result;
break; break;
} }
case SortType::LOADERS: { case SortType::Loaders: {
auto compare_result = QString::compare(loaders(), cast_other->loaders(), Qt::CaseInsensitive); auto compare_result = QString::compare(loaders(), cast_other->loaders(), Qt::CaseInsensitive);
if (compare_result != 0) if (compare_result != 0)
return compare_result; return compare_result;
break; break;
} }
case SortType::RELEASE_TYPE: { case SortType::ReleaseType: {
auto compare_result = QString::compare(releaseType(), cast_other->releaseType(), Qt::CaseInsensitive); auto compare_result = QString::compare(releaseType(), cast_other->releaseType(), Qt::CaseInsensitive);
if (compare_result != 0) if (compare_result != 0)
return compare_result; return compare_result;
break; break;
} }
case SortType::REQUIRED_BY: { case SortType::RequiredBy: {
if (requiredByCount() > cast_other->requiredByCount()) if (requiredByCount() > cast_other->requiredByCount())
return 1; return 1;
if (requiredByCount() < cast_other->requiredByCount()) if (requiredByCount() < cast_other->requiredByCount())
return -1; return -1;
break; break;
} }
case SortType::REQUIRES: { case SortType::Requires: {
if (requiresCount() > cast_other->requiresCount()) if (requiresCount() > cast_other->requiresCount())
return 1; return 1;
if (requiresCount() < cast_other->requiresCount()) if (requiresCount() < cast_other->requiresCount())

View file

@ -52,25 +52,24 @@
#include <QUuid> #include <QUuid>
#include <algorithm> #include <algorithm>
#include "minecraft/Component.h"
#include "minecraft/mod/Resource.h" #include "minecraft/mod/Resource.h"
#include "minecraft/mod/ResourceFolderModel.h" #include "minecraft/mod/ResourceFolderModel.h"
#include "minecraft/mod/tasks/LocalModParseTask.h" #include "minecraft/mod/tasks/LocalModParseTask.h"
#include "modplatform/ModIndex.h" #include "modplatform/ModIndex.h"
#include "ui/dialogs/CustomMessageBox.h" #include "ui/dialogs/CustomMessageBox.h"
ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent)
{ {
m_column_names = QStringList({ "Enable", "Image", "Name", "Version", "Last Modified", "Provider", "Size", "Side", "Loaders", m_columnNames = QStringList({ "Enable", "Image", "Name", "Version", "Last Modified", "Provider", "Size", "Side", "Loaders",
"Minecraft Versions", "Release Type", "Requires", "Required By", "File Name" }); "Minecraft Versions", "Release Type", "Requires", "Required By", "File Name" });
m_column_names_translated = m_columnNamesTranslated =
QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Version"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("Side"), QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Version"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("Side"),
tr("Loaders"), tr("Minecraft Versions"), tr("Release Type"), tr("Requires"), tr("Required By"), tr("File Name") }); tr("Loaders"), tr("Minecraft Versions"), tr("Release Type"), tr("Requires"), tr("Required By"), tr("File Name") });
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::VERSION, SortType::DATE, m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::Version, SortType::Date,
SortType::PROVIDER, SortType::SIZE, SortType::SIDE, SortType::LOADERS, SortType::MC_VERSIONS, SortType::Provider, SortType::Size, SortType::Side, SortType::Loaders, SortType::McVersions,
SortType::RELEASE_TYPE, SortType::REQUIRES, SortType::REQUIRED_BY, SortType::FILENAME }; SortType::ReleaseType, SortType::Requires, SortType::RequiredBy, SortType::Filename };
m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive }; QHeaderView::Interactive, QHeaderView::Interactive };
@ -81,8 +80,9 @@ ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool is_
QVariant ModFolderModel::data(const QModelIndex& index, int role) const QVariant ModFolderModel::data(const QModelIndex& index, int role) const
{ {
if (!validateIndex(index)) if (!validateIndex(index)) {
return {}; return {};
}
int row = index.row(); int row = index.row();
int column = index.column(); int column = index.column();
@ -120,6 +120,8 @@ QVariant ModFolderModel::data(const QModelIndex& index, int role) const
case RequiresColumn: { case RequiresColumn: {
return at(row).requiresCount(); return at(row).requiresCount();
} }
default:
break;
} }
break; break;
case Qt::DecorationRole: { case Qt::DecorationRole: {
@ -158,6 +160,8 @@ QVariant ModFolderModel::data(const QModelIndex& index, int role) const
case FileNameColumn: case FileNameColumn:
mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn);
break; break;
default:
break;
} }
if (mappedIndex.isValid()) { if (mappedIndex.isValid()) {
@ -229,12 +233,12 @@ QVariant ModFolderModel::headerData(int section, [[maybe_unused]] Qt::Orientatio
int ModFolderModel::columnCount(const QModelIndex& parent) const int ModFolderModel::columnCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : NUM_COLUMNS; return parent.isValid() ? 0 : NumColumns;
} }
Task* ModFolderModel::createParseTask(Resource& resource) Task* ModFolderModel::createParseTask(Resource& resource)
{ {
return new LocalModParseTask(m_next_resolution_ticket, resource.type(), resource.fileinfo()); return new LocalModParseTask(m_nextResolutionTicket, resource.type(), resource.fileinfo());
} }
bool ModFolderModel::isValid() bool ModFolderModel::isValid()
@ -242,35 +246,37 @@ bool ModFolderModel::isValid()
return m_dir.exists() && m_dir.isReadable(); return m_dir.exists() && m_dir.isReadable();
} }
void ModFolderModel::onParseSucceeded(int ticket, QString mod_id) void ModFolderModel::onParseSucceeded(int ticket, const QString& resourceId)
{ {
auto iter = m_active_parse_tasks.constFind(ticket); auto iter = m_activeParseTasks.constFind(ticket);
if (iter == m_active_parse_tasks.constEnd()) if (iter == m_activeParseTasks.constEnd()) {
return; return;
}
int row = m_resources_index[mod_id]; int row = m_resourcesIndex[resourceId];
auto parse_task = *iter; const auto& parseTask = *iter;
auto cast_task = static_cast<LocalModParseTask*>(parse_task.get()); auto* castTask = static_cast<LocalModParseTask*>(parseTask.get());
Q_ASSERT(cast_task->token() == ticket); Q_ASSERT(castTask->token() == ticket);
auto resource = find(mod_id); auto resource = find(resourceId);
auto result = cast_task->result(); auto result = castTask->result();
if (result && resource) { if (result && resource) {
auto* mod = static_cast<Mod*>(resource.get()); auto* mod = static_cast<Mod*>(resource.get());
mod->finishResolvingWithDetails(std::move(result->details)); mod->finishResolvingWithDetails(std::move(result->details));
} }
emit dataChanged(index(row, RequiresColumn), index(row, RequiredByColumn)); emit dataChanged(index(row, RequiresColumn), index(row, RequiredByColumn));
} }
Mod* findById(QSet<Mod*> mods, QString modId) namespace {
Mod* findById(QSet<Mod*> mods, const QString& resourceId)
{ {
auto found = std::find_if(mods.begin(), mods.end(), [modId](Mod* m) { return m->mod_id() == modId; }); auto found = std::ranges::find_if(mods, [resourceId](Mod* m) { return m->mod_id() == resourceId; });
return found != mods.end() ? *found : nullptr; return found != mods.end() ? *found : nullptr;
} }
} // namespace
void ModFolderModel::onParseFinished() void ModFolderModel::onParseFinished()
{ {
@ -283,25 +289,25 @@ void ModFolderModel::onParseFinished()
m_requires.clear(); m_requires.clear();
m_requiredBy.clear(); m_requiredBy.clear();
auto findByProjectID = [mods](QVariant modId, ModPlatform::ResourceProvider provider) -> Mod* { auto findByProjectID = [mods](const QVariant& modId, ModPlatform::ResourceProvider provider) -> Mod* {
auto found = std::find_if(mods.begin(), mods.end(), [modId, provider](Mod* m) { auto found = std::ranges::find_if(mods, [modId, provider](Mod* m) {
return m->metadata() && m->metadata()->provider == provider && m->metadata()->project_id == modId; return m->metadata() && m->metadata()->provider == provider && m->metadata()->project_id == modId;
}); });
return found != mods.end() ? *found : nullptr; return found != mods.end() ? *found : nullptr;
}; };
for (auto mod : mods) { for (auto* mod : mods) {
auto id = mod->mod_id(); auto id = mod->mod_id();
for (auto dep : mod->dependencies()) { for (const auto& dep : mod->dependencies()) {
auto d = findById(mods, dep); auto* d = findById(mods, dep);
if (d) { if (d) {
m_requires[id] << d; m_requires[id] << d;
m_requiredBy[d->mod_id()] << mod; m_requiredBy[d->mod_id()] << mod;
} }
} }
if (mod->metadata()) { if (mod->metadata()) {
for (auto dep : mod->metadata()->dependencies) { for (const auto& dep : mod->metadata()->dependencies) {
if (dep.type == ModPlatform::DependencyType::REQUIRED) { if (dep.type == ModPlatform::DependencyType::REQUIRED) {
auto d = findByProjectID(dep.addonId, mod->metadata()->provider); auto* d = findByProjectID(dep.addonId, mod->metadata()->provider);
if (d) { if (d) {
m_requires[id] << d; m_requires[id] << d;
m_requiredBy[d->mod_id()] << mod; m_requiredBy[d->mod_id()] << mod;
@ -310,29 +316,31 @@ void ModFolderModel::onParseFinished()
} }
} }
} }
for (auto mod : mods) { for (auto* mod : mods) {
auto id = mod->mod_id(); auto id = mod->mod_id();
if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) { if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) {
mod->setRequiredByCount(m_requiredBy[id].count()); mod->setRequiredByCount(m_requiredBy[id].count());
mod->setRequiresCount(m_requires[id].count()); mod->setRequiresCount(m_requires[id].count());
int row = m_resources_index[mod->internal_id()]; int row = m_resourcesIndex[mod->internalId()];
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1)); emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
} }
} }
} }
QSet<Mod*> collectMods(QSet<Mod*> mods, QHash<QString, QSet<Mod*>> relation, std::set<QString>& seen, bool shouldBeEnabled) namespace {
QSet<Mod*> collectMods(const QSet<Mod*>& mods, QHash<QString, QSet<Mod*>> relation, std::set<QString>& seen, bool shouldBeEnabled)
{ {
QSet<Mod*> affectedList = {}; QSet<Mod*> affectedList = {};
QSet<Mod*> needToCheck = {}; QSet<Mod*> needToCheck = {};
for (auto mod : mods) { for (auto* mod : mods) {
auto id = mod->mod_id(); auto id = mod->mod_id();
if (seen.count(id) == 0) { if (!seen.contains(id)) {
seen.insert(id); seen.insert(id);
for (auto affected : relation[id]) { for (auto* affected : relation[id]) {
auto affectedId = affected->mod_id(); auto affectedId = affected->mod_id();
if (findById(mods, affectedId) == nullptr && seen.count(affectedId) == 0) { if (findById(mods, affectedId) == nullptr && !seen.contains(affectedId)) {
seen.insert(affectedId); seen.insert(affectedId);
if (shouldBeEnabled != affected->enabled()) { if (shouldBeEnabled != affected->enabled()) {
affectedList << affected; affectedList << affected;
@ -348,11 +356,13 @@ QSet<Mod*> collectMods(QSet<Mod*> mods, QHash<QString, QSet<Mod*>> relation, std
} }
return affectedList; return affectedList;
} }
} // namespace
QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, EnableAction action) QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, EnableAction action)
{ {
if (indexes.isEmpty()) if (indexes.isEmpty()) {
return {}; return {};
}
QModelIndexList affectedList = {}; QModelIndexList affectedList = {};
auto affectedModsList = selectedMods(indexes); auto affectedModsList = selectedMods(indexes);
@ -372,9 +382,9 @@ QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes,
return {}; // this function should not be called with TOGGLE return {}; // this function should not be called with TOGGLE
} }
} }
for (auto affected : affectedMods) { for (auto* affected : affectedMods) {
auto affectedId = affected->mod_id(); auto affectedId = affected->mod_id();
auto row = m_resources_index[affected->internal_id()]; auto row = m_resourcesIndex[affected->internalId()];
affectedList << index(row, 0); affectedList << index(row, 0);
} }
return affectedList; return affectedList;
@ -382,8 +392,9 @@ QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes,
bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAction action) bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAction action)
{ {
if (indexes.isEmpty()) if (indexes.isEmpty()) {
return {}; return {};
}
auto indexedModsList = selectedMods(indexes); auto indexedModsList = selectedMods(indexes);
auto indexedMods = QSet(indexedModsList.begin(), indexedModsList.end()); auto indexedMods = QSet(indexedModsList.begin(), indexedModsList.end());
@ -402,7 +413,7 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc
break; break;
} }
case EnableAction::TOGGLE: { case EnableAction::TOGGLE: {
for (auto mod : indexedMods) { for (auto* mod : indexedMods) {
if (mod->enabled()) { if (mod->enabled()) {
toDisable << mod; toDisable << mod;
} else { } else {
@ -417,10 +428,10 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc
auto requiredToDisable = collectMods(toDisable, m_requiredBy, seen, false); auto requiredToDisable = collectMods(toDisable, m_requiredBy, seen, false);
toDisable.removeIf([toEnable](Mod* m) { return toEnable.contains(m); }); toDisable.removeIf([toEnable](Mod* m) { return toEnable.contains(m); });
auto toList = [this](QSet<Mod*> mods) { auto toList = [this](const QSet<Mod*>& mods) {
QModelIndexList list; QModelIndexList list;
for (auto mod : mods) { for (auto* mod : mods) {
auto row = m_resources_index[mod->internal_id()]; auto row = m_resourcesIndex[mod->internalId()];
list << index(row, 0); list << index(row, 0);
} }
return list; return list;
@ -453,7 +464,7 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc
yesButton = tr("Disable Required"); yesButton = tr("Disable Required");
} }
auto box = CustomMessageBox::selectable(nullptr, title, message, QMessageBox::Warning, auto* box = CustomMessageBox::selectable(nullptr, title, message, QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No); QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No);
box->button(QMessageBox::No)->setText(noButton); box->button(QMessageBox::No)->setText(noButton);
box->button(QMessageBox::Yes)->setText(yesButton); box->button(QMessageBox::Yes)->setText(yesButton);
@ -472,21 +483,23 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc
return disableStatus && enableStatus; return disableStatus && enableStatus;
} }
QStringList reqToList(QSet<Mod*> l) namespace {
QStringList reqToList(const QSet<Mod*>& l)
{ {
QStringList req; QStringList req;
for (auto m : l) { for (auto* m : l) {
req << m->name(); req << m->name();
} }
return req; return req;
} }
} // namespace
QStringList ModFolderModel::requiresList(QString id) QStringList ModFolderModel::requiresList(const QString& id)
{ {
return reqToList(m_requires[id]); return reqToList(m_requires[id]);
} }
QStringList ModFolderModel::requiredByList(QString id) QStringList ModFolderModel::requiredByList(const QString& id)
{ {
return reqToList(m_requiredBy[id]); return reqToList(m_requiredBy[id]);
} }
@ -495,7 +508,7 @@ bool ModFolderModel::deleteResources(const QModelIndexList& indexes)
{ {
auto deleteInvalid = [](QSet<Mod*>& mods) { auto deleteInvalid = [](QSet<Mod*>& mods) {
for (auto it = mods.begin(); it != mods.end();) { for (auto it = mods.begin(); it != mods.end();) {
auto mod = *it; auto* mod = *it;
// the QFileInfo::exists is used instead of mod->fileinfo().exists // the QFileInfo::exists is used instead of mod->fileinfo().exists
// because the later somehow caches that the file exists // because the later somehow caches that the file exists
if (!mod || !QFileInfo::exists(mod->fileinfo().absoluteFilePath())) { if (!mod || !QFileInfo::exists(mod->fileinfo().absoluteFilePath())) {
@ -506,14 +519,14 @@ bool ModFolderModel::deleteResources(const QModelIndexList& indexes)
} }
}; };
auto rsp = ResourceFolderModel::deleteResources(indexes); auto rsp = ResourceFolderModel::deleteResources(indexes);
for (auto mod : allMods()) { for (auto* mod : allMods()) {
auto id = mod->mod_id(); auto id = mod->mod_id();
deleteInvalid(m_requiredBy[id]); deleteInvalid(m_requiredBy[id]);
deleteInvalid(m_requires[id]); deleteInvalid(m_requires[id]);
if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) { if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) {
mod->setRequiredByCount(m_requiredBy[id].count()); mod->setRequiredByCount(m_requiredBy[id].count());
mod->setRequiresCount(m_requires[id].count()); mod->setRequiresCount(m_requires[id].count());
int row = m_resources_index[mod->internal_id()]; int row = m_resourcesIndex[mod->internalId()];
emit dataChanged(index(row, RequiresColumn), index(row, RequiredByColumn)); emit dataChanged(index(row, RequiresColumn), index(row, RequiredByColumn));
} }
} }

View file

@ -46,7 +46,6 @@
#include "Mod.h" #include "Mod.h"
#include "ResourceFolderModel.h" #include "ResourceFolderModel.h"
#include "minecraft/Component.h"
#include "minecraft/mod/Resource.h" #include "minecraft/mod/Resource.h"
class BaseInstance; class BaseInstance;
@ -59,7 +58,7 @@ class QFileSystemWatcher;
class ModFolderModel : public ResourceFolderModel { class ModFolderModel : public ResourceFolderModel {
Q_OBJECT Q_OBJECT
public: public:
enum Columns { enum Columns : std::uint8_t {
ActiveColumn = 0, ActiveColumn = 0,
ImageColumn, ImageColumn,
NameColumn, NameColumn,
@ -74,11 +73,11 @@ class ModFolderModel : public ResourceFolderModel {
RequiresColumn, RequiresColumn,
RequiredByColumn, RequiredByColumn,
FileNameColumn, FileNameColumn,
NUM_COLUMNS NumColumns
}; };
ModFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent = nullptr); ModFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent = nullptr);
virtual QString id() const override { return "mods"; } QString id() const override { return "mods"; }
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
@ -86,7 +85,7 @@ class ModFolderModel : public ResourceFolderModel {
int columnCount(const QModelIndex& parent) const override; int columnCount(const QModelIndex& parent) const override;
[[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new Mod(file); } [[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new Mod(file); }
[[nodiscard]] Task* createParseTask(Resource&) override; [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override;
bool isValid(); bool isValid();
@ -98,11 +97,11 @@ class ModFolderModel : public ResourceFolderModel {
RESOURCE_HELPERS(Mod) RESOURCE_HELPERS(Mod)
public: public:
QStringList requiresList(QString id); QStringList requiresList(const QString& id);
QStringList requiredByList(QString id); QStringList requiredByList(const QString& id);
private slots: private slots:
void onParseSucceeded(int ticket, QString resource_id) override; void onParseSucceeded(int ticket, const QString& resourceId) override;
void onParseFinished(); void onParseFinished();
private: private:

View file

@ -4,71 +4,75 @@
#include <QFileInfo> #include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <tuple> #include <tuple>
#include <utility>
#include "FileSystem.h" #include "FileSystem.h"
#include "StringUtils.h" #include "StringUtils.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
Resource::Resource(QObject* parent) : QObject(parent) {} Resource::Resource(QObject* parent) : QObject(parent), m_size_info(0) {}
Resource::Resource(QFileInfo file_info) : QObject() Resource::Resource(QFileInfo fileInfo) : m_size_info(0)
{ {
setFile(file_info); setFile(fileInfo);
} }
void Resource::setFile(QFileInfo file_info) void Resource::setFile(QFileInfo fileInfo)
{ {
m_file_info = file_info; m_file_info = std::move(fileInfo);
parseFile(); parseFile();
} }
static std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file) namespace {
std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file)
{ {
if (file.isDir()) { if (file.isDir()) {
auto dir = QDir(file.absoluteFilePath()); auto dir = QDir(file.absoluteFilePath());
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
auto count = dir.count(); auto count = dir.count();
auto str = QObject::tr("item"); auto str = QObject::tr("item");
if (count != 1) if (count != 1) {
str = QObject::tr("items"); str = QObject::tr("items");
}
return { QString("%1 %2").arg(QString::number(count), str), count }; return { QString("%1 %2").arg(QString::number(count), str), count };
} }
return { StringUtils::humanReadableFileSize(file.size(), true), file.size() }; return { StringUtils::humanReadableFileSize(file.size(), true), file.size() };
} }
} // namespace
void Resource::parseFile() void Resource::parseFile()
{ {
QString file_name{ m_file_info.fileName() }; QString fileName{ m_file_info.fileName() };
m_type = ResourceType::UNKNOWN; m_type = ResourceType::UNKNOWN;
m_internal_id = file_name; m_internal_id = fileName;
std::tie(m_size_str, m_size_info) = calculateFileSize(m_file_info); std::tie(m_size_str, m_size_info) = calculateFileSize(m_file_info);
if (m_file_info.isDir()) { if (m_file_info.isDir()) {
m_type = ResourceType::FOLDER; m_type = ResourceType::FOLDER;
m_name = file_name; m_name = fileName;
} else if (m_file_info.isFile()) { } else if (m_file_info.isFile()) {
if (file_name.endsWith(".disabled")) { if (fileName.endsWith(".disabled")) {
file_name.chop(9); fileName.chop(9);
m_enabled = false; m_enabled = false;
} }
if (file_name.endsWith(".zip") || file_name.endsWith(".jar")) { if (fileName.endsWith(".zip") || fileName.endsWith(".jar")) {
m_type = ResourceType::ZIPFILE; m_type = ResourceType::ZIPFILE;
file_name.chop(4); fileName.chop(4);
} else if (file_name.endsWith(".nilmod")) { } else if (fileName.endsWith(".nilmod")) {
m_type = ResourceType::ZIPFILE; m_type = ResourceType::ZIPFILE;
file_name.chop(7); fileName.chop(7);
} else if (file_name.endsWith(".litemod")) { } else if (fileName.endsWith(".litemod")) {
m_type = ResourceType::LITEMOD; m_type = ResourceType::LITEMOD;
file_name.chop(8); fileName.chop(8);
} else { } else {
m_type = ResourceType::SINGLEFILE; m_type = ResourceType::SINGLEFILE;
} }
m_name = file_name; m_name = fileName;
} }
m_changed_date_time = m_file_info.lastModified(); m_changed_date_time = m_file_info.lastModified();
@ -76,39 +80,45 @@ void Resource::parseFile()
auto Resource::name() const -> QString auto Resource::name() const -> QString
{ {
if (metadata()) if (metadata()) {
return metadata()->name; return metadata()->name;
}
return m_name; return m_name;
} }
static void removeThePrefix(QString& string) namespace {
void removeThePrefix(QString& string)
{ {
static const QRegularExpression s_regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption); static const QRegularExpression s_regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption);
string.remove(s_regex); string.remove(s_regex);
string = string.trimmed(); string = string.trimmed();
} }
} // namespace
auto Resource::provider() const -> QString auto Resource::provider() const -> QString
{ {
if (metadata()) if (metadata()) {
return ModPlatform::ProviderCapabilities::readableName(metadata()->provider); return ModPlatform::ProviderCapabilities::readableName(metadata()->provider);
}
return tr("Unknown"); return tr("Unknown");
} }
auto Resource::homepage() const -> QString auto Resource::homepage() const -> QString
{ {
if (metadata()) if (metadata()) {
return ModPlatform::getMetaURL(metadata()->provider, metadata()->project_id); return ModPlatform::getMetaURL(metadata()->provider, metadata()->project_id);
}
return {}; return {};
} }
void Resource::setMetadata(std::shared_ptr<Metadata::ModStruct>&& metadata) void Resource::setMetadata(std::shared_ptr<Metadata::ModStruct>&& metadata)
{ {
if (status() == ResourceStatus::NO_METADATA) if (status() == ResourceStatus::NoMetadata) {
setStatus(ResourceStatus::INSTALLED); setStatus(ResourceStatus::Installed);
}
m_metadata = metadata; m_metadata = metadata;
} }
@ -133,12 +143,12 @@ void Resource::updateIssues(const BaseInstance* inst)
return; return;
} }
auto mcInst = dynamic_cast<const MinecraftInstance*>(inst); const auto* mcInst = dynamic_cast<const MinecraftInstance*>(inst);
if (mcInst == nullptr) { if (mcInst == nullptr) {
return; return;
} }
auto profile = mcInst->getPackProfile(); auto* profile = mcInst->getPackProfile();
QString mcVersion = profile->getComponentVersion("net.minecraft"); QString mcVersion = profile->getComponentVersion("net.minecraft");
if (!m_metadata->mcVersions.empty() && !m_metadata->mcVersions.contains(mcVersion)) { if (!m_metadata->mcVersions.empty() && !m_metadata->mcVersions.contains(mcVersion)) {
@ -151,50 +161,59 @@ int Resource::compare(const Resource& other, SortType type) const
{ {
switch (type) { switch (type) {
default: default:
case SortType::ENABLED: case SortType::Enabled:
if (enabled() && !other.enabled()) if (enabled() && !other.enabled()) {
return 1; return 1;
if (!enabled() && other.enabled()) }
if (!enabled() && other.enabled()) {
return -1; return -1;
}
break; break;
case SortType::NAME: { case SortType::Name: {
QString this_name{ name() }; QString thisName{ name() };
QString other_name{ other.name() }; QString otherName{ other.name() };
// TODO do we need this? it could result in 0 being returned // TODO do we need this? it could result in 0 being returned
removeThePrefix(this_name); removeThePrefix(thisName);
removeThePrefix(other_name); removeThePrefix(otherName);
return QString::compare(this_name, other_name, Qt::CaseInsensitive); return QString::compare(thisName, otherName, Qt::CaseInsensitive);
} }
case SortType::DATE: case SortType::Date:
if (dateTimeChanged() > other.dateTimeChanged()) if (dateTimeChanged() > other.dateTimeChanged()) {
return 1; return 1;
if (dateTimeChanged() < other.dateTimeChanged()) }
if (dateTimeChanged() < other.dateTimeChanged()) {
return -1; return -1;
}
break; break;
case SortType::FILENAME: case SortType::Filename:
return fileinfo().fileName().localeAwareCompare(other.fileinfo().fileName()); return fileinfo().fileName().localeAwareCompare(other.fileinfo().fileName());
case SortType::SIZE: { case SortType::Size: {
if (this->type() != other.type()) { if (this->type() != other.type()) {
if (this->type() == ResourceType::FOLDER) if (this->type() == ResourceType::FOLDER) {
return -1; return -1;
if (other.type() == ResourceType::FOLDER) }
if (other.type() == ResourceType::FOLDER) {
return 1; return 1;
} }
}
if (sizeInfo() > other.sizeInfo()) if (sizeInfo() > other.sizeInfo()) {
return 1; return 1;
if (sizeInfo() < other.sizeInfo()) }
if (sizeInfo() < other.sizeInfo()) {
return -1; return -1;
}
break; break;
} }
case SortType::PROVIDER: { case SortType::Provider: {
auto compare_result = QString::compare(provider(), other.provider(), Qt::CaseInsensitive); auto compareResult = QString::compare(provider(), other.provider(), Qt::CaseInsensitive);
if (compare_result != 0) if (compareResult != 0) {
return compare_result; return compareResult;
}
break; break;
} }
} }
@ -204,17 +223,20 @@ int Resource::compare(const Resource& other, SortType type) const
bool Resource::applyFilter(QRegularExpression filter) const bool Resource::applyFilter(QRegularExpression filter) const
{ {
if (filter.match(name()).hasMatch()) if (filter.match(name()).hasMatch()) {
return true; return true;
if (filter.match(fileinfo().fileName()).hasMatch()) }
if (filter.match(fileinfo().fileName()).hasMatch()) {
return true; return true;
}
return false; return false;
} }
bool Resource::enable(EnableAction action) bool Resource::enable(EnableAction action)
{ {
if (m_type == ResourceType::UNKNOWN || m_type == ResourceType::FOLDER) if (m_type == ResourceType::UNKNOWN || m_type == ResourceType::FOLDER) {
return false; return false;
}
QString path = m_file_info.absoluteFilePath(); QString path = m_file_info.absoluteFilePath();
QFile file(path); QFile file(path);
@ -233,14 +255,16 @@ bool Resource::enable(EnableAction action)
break; break;
} }
if (m_enabled == enable) if (m_enabled == enable) {
return false; return false;
}
if (enable) { if (enable) {
// m_enabled is false, but there's no '.disabled' suffix. // m_enabled is false, but there's no '.disabled' suffix.
// TODO: Report error? // TODO: Report error?
if (!path.endsWith(".disabled")) if (!path.endsWith(".disabled")) {
return false; return false;
}
path.chop(9); path.chop(9);
} else { } else {
path += ".disabled"; path += ".disabled";
@ -248,8 +272,9 @@ bool Resource::enable(EnableAction action)
path = FS::getUniqueResourceName(path); path = FS::getUniqueResourceName(path);
} }
} }
if (!file.rename(path)) if (!file.rename(path)) {
return false; return false;
}
setFile(QFileInfo(path)); setFile(QFileInfo(path));
@ -257,33 +282,34 @@ bool Resource::enable(EnableAction action)
return true; return true;
} }
auto Resource::destroy(const QDir& index_dir, bool preserve_metadata, bool attempt_trash) -> bool auto Resource::destroy(const QDir& indexDir, bool preserveMetadata, bool attemptTrash) -> bool
{ {
m_type = ResourceType::UNKNOWN; m_type = ResourceType::UNKNOWN;
if (!preserve_metadata) { if (!preserveMetadata) {
qDebug() << QString("Destroying metadata for '%1' on purpose").arg(name()); qDebug() << QString("Destroying metadata for '%1' on purpose").arg(name());
destroyMetadata(index_dir); destroyMetadata(indexDir);
} }
return (attempt_trash && FS::trash(m_file_info.filePath())) || FS::deletePath(m_file_info.filePath()); return (attemptTrash && FS::trash(m_file_info.filePath())) || FS::deletePath(m_file_info.filePath());
} }
auto Resource::destroyMetadata(const QDir& index_dir) -> void auto Resource::destroyMetadata(const QDir& indexDir) -> void
{ {
if (metadata()) { if (metadata()) {
Metadata::remove(index_dir, metadata()->slug); Metadata::remove(indexDir, metadata()->slug);
} else { } else {
auto n = name(); auto n = name();
Metadata::remove(index_dir, n); Metadata::remove(indexDir, n);
} }
m_metadata = nullptr; m_metadata = nullptr;
} }
bool Resource::isSymLinkUnder(const QString& instPath) const bool Resource::isSymLinkUnder(const QString& instPath) const
{ {
if (isSymLink()) if (isSymLink()) {
return true; return true;
}
auto instDir = QDir(instPath); auto instDir = QDir(instPath);
@ -301,8 +327,9 @@ bool Resource::isMoreThanOneHardLink() const
auto Resource::getOriginalFileName() const -> QString auto Resource::getOriginalFileName() const -> QString
{ {
auto fileName = m_file_info.fileName(); auto fileName = m_file_info.fileName();
if (!m_enabled) if (!m_enabled) {
fileName.chop(9); fileName.chop(9);
}
return fileName; return fileName;
} }
@ -332,16 +359,16 @@ QDebug operator<<(QDebug debug, ResourceType type)
QDebug operator<<(QDebug debug, ResourceStatus status) QDebug operator<<(QDebug debug, ResourceStatus status)
{ {
switch (status) { switch (status) {
case ResourceStatus::INSTALLED: case ResourceStatus::Installed:
debug << "INSTALLED"; debug << "INSTALLED";
break; break;
case ResourceStatus::NOT_INSTALLED: case ResourceStatus::NotInstalled:
debug << "NOT_INSTALLED"; debug << "NOT_INSTALLED";
break; break;
case ResourceStatus::NO_METADATA: case ResourceStatus::NoMetadata:
debug << "NO_METADATA"; debug << "NO_METADATA";
break; break;
case ResourceStatus::UNKNOWN: case ResourceStatus::Unknown:
default: default:
debug << "UNKNOWN"; debug << "UNKNOWN";
break; break;

View file

@ -45,7 +45,7 @@
class BaseInstance; class BaseInstance;
enum class ResourceType { enum class ResourceType : std::uint8_t {
UNKNOWN, //!< Indicates an unspecified resource type. UNKNOWN, //!< Indicates an unspecified resource type.
ZIPFILE, //!< The resource is a zip file containing the resource's class files. ZIPFILE, //!< The resource is a zip file containing the resource's class files.
SINGLEFILE, //!< The resource is a single file (not a zip file). SINGLEFILE, //!< The resource is a single file (not a zip file).
@ -55,33 +55,33 @@ enum class ResourceType {
QDebug operator<<(QDebug debug, ResourceType type); QDebug operator<<(QDebug debug, ResourceType type);
enum class ResourceStatus { enum class ResourceStatus : std::uint8_t {
INSTALLED, // Both JAR and Metadata are present Installed, // Both JAR and Metadata are present
NOT_INSTALLED, // Only the Metadata is present NotInstalled, // Only the Metadata is present
NO_METADATA, // Only the JAR is present NoMetadata, // Only the JAR is present
UNKNOWN, // Default status Unknown, // Default status
}; };
QDebug operator<<(QDebug debug, ResourceStatus status); QDebug operator<<(QDebug debug, ResourceStatus status);
enum class SortType { enum class SortType : std::uint8_t {
NAME, Name,
DATE, Date,
VERSION, Version,
ENABLED, Enabled,
PACK_FORMAT, PackFormat,
PROVIDER, Provider,
SIZE, Size,
SIDE, Side,
MC_VERSIONS, McVersions,
LOADERS, Loaders,
RELEASE_TYPE, ReleaseType,
REQUIRES, Requires,
REQUIRED_BY, RequiredBy,
FILENAME, Filename,
}; };
enum class EnableAction { ENABLE, DISABLE, TOGGLE }; enum class EnableAction : std::uint8_t { ENABLE, DISABLE, TOGGLE };
/** General class for managed resources. It mirrors a file in disk, with some more info /** General class for managed resources. It mirrors a file in disk, with some more info
* for display and house-keeping purposes. * for display and house-keeping purposes.
@ -93,20 +93,19 @@ class Resource : public QObject {
Q_DISABLE_COPY(Resource) Q_DISABLE_COPY(Resource)
public: public:
using Ptr = shared_qobject_ptr<Resource>; using Ptr = shared_qobject_ptr<Resource>;
using WeakPtr = QPointer<Resource>;
Resource(QObject* parent = nullptr); Resource(QObject* parent = nullptr);
Resource(QFileInfo file_info); Resource(QFileInfo fileInfo);
Resource(QString file_path) : Resource(QFileInfo(file_path)) {} Resource(const QString& filePath) : Resource(QFileInfo(filePath)) {}
~Resource() override = default; ~Resource() override = default;
void setFile(QFileInfo file_info); void setFile(QFileInfo fileInfo);
void parseFile(); void parseFile();
auto fileinfo() const -> QFileInfo { return m_file_info; } auto fileinfo() const -> QFileInfo { return m_file_info; }
auto dateTimeChanged() const -> QDateTime { return m_changed_date_time; } auto dateTimeChanged() const -> QDateTime { return m_changed_date_time; }
auto internal_id() const -> QString { return m_internal_id; } auto internalId() const -> QString { return m_internal_id; }
auto type() const -> ResourceType { return m_type; } auto type() const -> ResourceType { return m_type; }
bool enabled() const { return m_enabled; } bool enabled() const { return m_enabled; }
auto getOriginalFileName() const -> QString; auto getOriginalFileName() const -> QString;
@ -139,7 +138,7 @@ class Resource : public QObject {
* = 0: 'this' is equal to 'other' * = 0: 'this' is equal to 'other'
* < 0: 'this' comes before 'other' * < 0: 'this' comes before 'other'
*/ */
virtual int compare(const Resource& other, SortType type = SortType::NAME) const; virtual int compare(const Resource& other, SortType type = SortType::Name) const;
/** Returns whether the given filter should filter out 'this' (false), /** Returns whether the given filter should filter out 'this' (false),
* or if such filter includes the Resource (true). * or if such filter includes the Resource (true).
@ -164,9 +163,9 @@ class Resource : public QObject {
} }
// Delete all files of this resource. // Delete all files of this resource.
auto destroy(const QDir& index_dir, bool preserve_metadata = false, bool attempt_trash = true) -> bool; auto destroy(const QDir& indexDir, bool preserveMetadata = false, bool attemptTrash = true) -> bool;
// Delete the metadata only. // Delete the metadata only.
auto destroyMetadata(const QDir& index_dir) -> void; auto destroyMetadata(const QDir& indexDir) -> void;
auto isSymLink() const -> bool { return m_file_info.isSymLink(); } auto isSymLink() const -> bool { return m_file_info.isSymLink(); }
@ -196,7 +195,7 @@ class Resource : public QObject {
ResourceType m_type = ResourceType::UNKNOWN; ResourceType m_type = ResourceType::UNKNOWN;
/* Installation status of the resource. */ /* Installation status of the resource. */
ResourceStatus m_status = ResourceStatus::UNKNOWN; ResourceStatus m_status = ResourceStatus::Unknown;
std::shared_ptr<Metadata::ModStruct> m_metadata = nullptr; std::shared_ptr<Metadata::ModStruct> m_metadata = nullptr;

View file

@ -11,6 +11,7 @@
#include <QStyle> #include <QStyle>
#include <QThreadPool> #include <QThreadPool>
#include <QUrl> #include <QUrl>
#include <algorithm>
#include <utility> #include <utility>
#include "Application.h" #include "Application.h"
@ -27,10 +28,10 @@
#include "tasks/Task.h" #include "tasks/Task.h"
#include "ui/dialogs/CustomMessageBox.h" #include "ui/dialogs/CustomMessageBox.h"
ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: QAbstractListModel(parent), m_dir(dir), m_instance(instance), m_watcher(this), m_is_indexed(is_indexed) : QAbstractListModel(parent), m_dir(dir), m_instance(instance), m_watcher(this), m_isIndexed(isIndexed)
{ {
if (create_dir) { if (createDir) {
FS::ensureFolderPathExists(m_dir.absolutePath()); FS::ensureFolderPathExists(m_dir.absolutePath());
} }
@ -49,70 +50,75 @@ ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance
ResourceFolderModel::~ResourceFolderModel() ResourceFolderModel::~ResourceFolderModel()
{ {
while (!QThreadPool::globalInstance()->waitForDone(100)) while (!QThreadPool::globalInstance()->waitForDone(100)) {
QCoreApplication::processEvents(); QCoreApplication::processEvents();
} }
}
bool ResourceFolderModel::startWatching(const QStringList& paths) bool ResourceFolderModel::startWatching(const QStringList& paths)
{ {
// Remove orphaned metadata next time // Remove orphaned metadata next time
m_first_folder_load = true; m_firstFolderLoad = true;
if (m_is_watching) if (m_isWatching) {
return false; return false;
}
auto couldnt_be_watched = m_watcher.addPaths(paths); auto couldntBeWatched = m_watcher.addPaths(paths);
for (auto path : paths) { for (const auto& path : paths) {
if (couldnt_be_watched.contains(path)) if (couldntBeWatched.contains(path)) {
qDebug() << "Failed to start watching" << path; qDebug() << "Failed to start watching" << path;
else } else {
qDebug() << "Started watching" << path; qDebug() << "Started watching" << path;
} }
}
update(); update();
m_is_watching = !m_is_watching; m_isWatching = !m_isWatching;
return m_is_watching; return m_isWatching;
} }
bool ResourceFolderModel::stopWatching(const QStringList& paths) bool ResourceFolderModel::stopWatching(const QStringList& paths)
{ {
if (!m_is_watching) if (!m_isWatching) {
return false; return false;
}
auto couldnt_be_stopped = m_watcher.removePaths(paths); auto couldntBeStopped = m_watcher.removePaths(paths);
for (auto path : paths) { for (const auto& path : paths) {
if (couldnt_be_stopped.contains(path)) if (couldntBeStopped.contains(path)) {
qDebug() << "Failed to stop watching" << path; qDebug() << "Failed to stop watching" << path;
else } else {
qDebug() << "Stopped watching" << path; qDebug() << "Stopped watching" << path;
} }
m_is_watching = !m_is_watching;
return !m_is_watching;
} }
bool ResourceFolderModel::installResource(QString original_path) m_isWatching = !m_isWatching;
return !m_isWatching;
}
bool ResourceFolderModel::installResource(QString originalPath)
{ {
// NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName // NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName
original_path = FS::NormalizePath(original_path); originalPath = FS::NormalizePath(originalPath);
QFileInfo file_info(original_path); QFileInfo fileInfo(originalPath);
if (!file_info.exists() || !file_info.isReadable()) { if (!fileInfo.exists() || !fileInfo.isReadable()) {
qWarning() << "Caught attempt to install non-existing file or file-like object:" << original_path; qWarning() << "Caught attempt to install non-existing file or file-like object:" << originalPath;
return false; return false;
} }
qDebug() << "Installing:" << file_info.absoluteFilePath(); qDebug() << "Installing:" << fileInfo.absoluteFilePath();
Resource resource(file_info); Resource resource(fileInfo);
if (!resource.valid()) { if (!resource.valid()) {
qWarning() << original_path << "is not a valid resource. Ignoring it."; qWarning() << originalPath << "is not a valid resource. Ignoring it.";
return false; return false;
} }
auto new_path = FS::NormalizePath(m_dir.filePath(file_info.fileName())); auto newPath = FS::NormalizePath(m_dir.filePath(fileInfo.fileName()));
if (original_path == new_path) { if (originalPath == newPath) {
qWarning() << "Overwriting the mod (" << original_path << ") with itself makes no sense..."; qWarning() << "Overwriting the mod (" << originalPath << ") with itself makes no sense...";
return false; return false;
} }
@ -120,45 +126,47 @@ bool ResourceFolderModel::installResource(QString original_path)
case ResourceType::SINGLEFILE: case ResourceType::SINGLEFILE:
case ResourceType::ZIPFILE: case ResourceType::ZIPFILE:
case ResourceType::LITEMOD: { case ResourceType::LITEMOD: {
if (QFile::exists(new_path) || QFile::exists(new_path + QString(".disabled"))) { if (QFile::exists(newPath) || QFile::exists(newPath + QString(".disabled"))) {
if (!FS::deletePath(new_path)) { if (!FS::deletePath(newPath)) {
qCritical() << "Cleaning up new location (" << new_path << ") was unsuccessful!"; qCritical() << "Cleaning up new location (" << newPath << ") was unsuccessful!";
return false; return false;
} }
qDebug() << new_path << "has been deleted."; qDebug() << newPath << "has been deleted.";
} }
if (!QFile::copy(original_path, new_path)) { if (!QFile::copy(originalPath, newPath)) {
qCritical() << "Copy from" << original_path << "to" << new_path << "has failed."; qCritical() << "Copy from" << originalPath << "to" << newPath << "has failed.";
return false; return false;
} }
FS::updateTimestamp(new_path); FS::updateTimestamp(newPath);
QFileInfo new_path_file_info(new_path); QFileInfo newPathFileInfo(newPath);
resource.setFile(new_path_file_info); resource.setFile(newPathFileInfo);
if (!m_is_watching) if (!m_isWatching) {
return update(); return update();
}
return true; return true;
} }
case ResourceType::FOLDER: { case ResourceType::FOLDER: {
if (QFile::exists(new_path)) { if (QFile::exists(newPath)) {
qDebug() << "Ignoring folder '" << original_path << "', it would merge with" << new_path; qDebug() << "Ignoring folder '" << originalPath << "', it would merge with" << newPath;
return false; return false;
} }
if (!FS::copy(original_path, new_path)()) { if (!FS::copy(originalPath, newPath)()) {
qWarning() << "Copy of folder from" << original_path << "to" << new_path << "has (potentially partially) failed."; qWarning() << "Copy of folder from" << originalPath << "to" << newPath << "has (potentially partially) failed.";
return false; return false;
} }
QFileInfo newpathInfo(new_path); QFileInfo newpathInfo(newPath);
resource.setFile(newpathInfo); resource.setFile(newpathInfo);
if (!m_is_watching) if (!m_isWatching) {
return update(); return update();
}
return true; return true;
} }
@ -168,24 +176,24 @@ bool ResourceFolderModel::installResource(QString original_path)
return false; return false;
} }
void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlatform::IndexedVersion& vers) void ResourceFolderModel::installResourceWithFlameMetadata(const QString& path, ModPlatform::IndexedVersion& vers)
{ {
auto install = [this, path] { installResource(std::move(path)); }; auto install = [this, path] { installResource(path); };
if (vers.addonId.isValid()) { if (vers.addonId.isValid()) {
ModPlatform::IndexedPack pack{ ModPlatform::IndexedPack pack{
vers.addonId, .addonId = vers.addonId,
ModPlatform::ResourceProvider::FLAME, .provider = ModPlatform::ResourceProvider::FLAME,
}; };
auto [job, response] = FlameAPI().getProject(vers.addonId.toString()); auto [job, response] = FlameAPI().getProject(vers.addonId.toString());
connect(job.get(), &Task::failed, this, install); connect(job.get(), &Task::failed, this, install);
connect(job.get(), &Task::aborted, this, install); connect(job.get(), &Task::aborted, this, install);
connect(job.get(), &Task::succeeded, [response, this, &vers, install, &pack] { connect(job.get(), &Task::succeeded, [response, this, &vers, install, &pack] {
QJsonParseError parse_error{}; QJsonParseError parseError{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); QJsonDocument doc = QJsonDocument::fromJson(*response, &parseError);
if (parse_error.error != QJsonParseError::NoError) { if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response for mod info at" << parse_error.offset qWarning() << "Error while parsing JSON response for mod info at" << parseError.offset
<< "reason:" << parse_error.errorString(); << "reason:" << parseError.errorString();
qDebug() << *response; qDebug() << *response;
return; return;
} }
@ -196,9 +204,9 @@ void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlat
qDebug() << doc; qDebug() << doc;
qWarning() << "Error while reading mod info:" << e.cause(); qWarning() << "Error while reading mod info:" << e.cause();
} }
LocalResourceUpdateTask update_metadata(indexDir(), pack, vers); LocalResourceUpdateTask updateMetadata(indexDir(), pack, vers);
connect(&update_metadata, &Task::finished, this, install); connect(&updateMetadata, &Task::finished, this, install);
update_metadata.start(); updateMetadata.start();
}); });
job->start(); job->start();
@ -207,7 +215,7 @@ void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlat
} }
} }
bool ResourceFolderModel::uninstallResource(const QString& file_name, bool preserve_metadata) bool ResourceFolderModel::uninstallResource(const QString& fileName, bool preserveMetadata)
{ {
for (auto& resource : m_resources) { for (auto& resource : m_resources) {
auto resourceFileInfo = resource->fileinfo(); auto resourceFileInfo = resource->fileinfo();
@ -216,8 +224,8 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese
resourceFileName.chop(9); resourceFileName.chop(9);
} }
if (resourceFileName == file_name) { if (resourceFileName == fileName) {
auto res = resource->destroy(indexDir(), preserve_metadata, false); auto res = resource->destroy(indexDir(), preserveMetadata, false);
update(); update();
@ -229,14 +237,16 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese
bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes) bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes)
{ {
if (indexes.isEmpty()) if (indexes.isEmpty()) {
return true; return true;
}
for (auto i : indexes) { for (auto i : indexes) {
if (i.column() != 0) if (i.column() != 0) {
continue; continue;
}
auto& resource = m_resources.at(i.row()); const auto& resource = m_resources.at(i.row());
resource->destroy(indexDir()); resource->destroy(indexDir());
} }
@ -247,14 +257,16 @@ bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes)
void ResourceFolderModel::deleteMetadata(const QModelIndexList& indexes) void ResourceFolderModel::deleteMetadata(const QModelIndexList& indexes)
{ {
if (indexes.isEmpty()) if (indexes.isEmpty()) {
return; return;
}
for (auto i : indexes) { for (auto i : indexes) {
if (i.column() != 0) if (i.column() != 0) {
continue; continue;
}
auto& resource = m_resources.at(i.row()); const auto& resource = m_resources.at(i.row());
resource->destroyMetadata(indexDir()); resource->destroyMetadata(indexDir());
} }
@ -271,33 +283,36 @@ bool ResourceFolderModel::setResourceEnabled(const QModelIndexList& indexes, Ena
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec(); ->exec();
if (response != QMessageBox::Yes) if (response != QMessageBox::Yes) {
return false; return false;
} }
}
if (indexes.isEmpty()) if (indexes.isEmpty()) {
return true; return true;
}
bool succeeded = true; bool succeeded = true;
for (auto const& idx : indexes) { for (const auto& idx : indexes) {
if (!validateIndex(idx) || idx.column() != 0) if (!validateIndex(idx) || idx.column() != 0) {
continue; continue;
}
int row = idx.row(); int row = idx.row();
auto& resource = m_resources[row]; auto& resource = m_resources[row];
// Preserve the row, but change its ID // Preserve the row, but change its ID
auto old_id = resource->internal_id(); auto oldId = resource->internalId();
if (!resource->enable(action)) { if (!resource->enable(action)) {
succeeded = false; succeeded = false;
continue; continue;
} }
auto new_id = resource->internal_id(); auto newId = resource->internalId();
m_resources_index.remove(old_id); m_resourcesIndex.remove(oldId);
m_resources_index[new_id] = row; m_resourcesIndex[newId] = row;
emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1)); emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1));
} }
@ -312,24 +327,25 @@ bool ResourceFolderModel::update()
QMutexLocker lock(&s_update_task_mutex); QMutexLocker lock(&s_update_task_mutex);
// Already updating, so we schedule a future update and return. // Already updating, so we schedule a future update and return.
if (m_current_update_task) { if (m_currentUpdateTask) {
m_scheduled_update = true; m_scheduledUpdate = true;
return false; return false;
} }
m_current_update_task.reset(createUpdateTask()); m_currentUpdateTask.reset(createUpdateTask());
if (!m_current_update_task) if (!m_currentUpdateTask) {
return false; return false;
}
connect(m_current_update_task.get(), &Task::succeeded, this, &ResourceFolderModel::onUpdateSucceeded, connect(m_currentUpdateTask.get(), &Task::succeeded, this, &ResourceFolderModel::onUpdateSucceeded,
Qt::ConnectionType::QueuedConnection); Qt::ConnectionType::QueuedConnection);
connect(m_current_update_task.get(), &Task::failed, this, &ResourceFolderModel::onUpdateFailed, Qt::ConnectionType::QueuedConnection); connect(m_currentUpdateTask.get(), &Task::failed, this, &ResourceFolderModel::onUpdateFailed, Qt::ConnectionType::QueuedConnection);
connect( connect(
m_current_update_task.get(), &Task::finished, this, m_currentUpdateTask.get(), &Task::finished, this,
[this] { [this] {
m_current_update_task.reset(); m_currentUpdateTask.reset();
if (m_scheduled_update) { if (m_scheduledUpdate) {
m_scheduled_update = false; m_scheduledUpdate = false;
update(); update();
} else { } else {
emit updateFinished(); emit updateFinished();
@ -340,16 +356,16 @@ bool ResourceFolderModel::update()
Task::Ptr preUpdate{ createPreUpdateTask() }; Task::Ptr preUpdate{ createPreUpdateTask() };
if (preUpdate != nullptr) { if (preUpdate != nullptr) {
auto task = new SequentialTask("ResourceFolderModel::update"); auto* task = new SequentialTask("ResourceFolderModel::update");
task->addTask(preUpdate); task->addTask(preUpdate);
task->addTask(m_current_update_task); task->addTask(m_currentUpdateTask);
connect(task, &Task::finished, [task] { task->deleteLater(); }); connect(task, &Task::finished, [task] { task->deleteLater(); });
QThreadPool::globalInstance()->start(task); QThreadPool::globalInstance()->start(task);
} else { } else {
QThreadPool::globalInstance()->start(m_current_update_task.get()); QThreadPool::globalInstance()->start(m_currentUpdateTask.get());
} }
return true; return true;
@ -362,24 +378,25 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res)
} }
Task::Ptr task{ createParseTask(*res) }; Task::Ptr task{ createParseTask(*res) };
if (!task) if (!task) {
return; return;
}
int ticket = m_next_resolution_ticket.fetch_add(1); int ticket = m_nextResolutionTicket.fetch_add(1);
res->setResolving(true, ticket); res->setResolving(true, ticket);
m_active_parse_tasks.insert(ticket, task); m_activeParseTasks.insert(ticket, task);
connect( connect(
task.get(), &Task::succeeded, this, [this, ticket, res] { onParseSucceeded(ticket, res->internal_id()); }, task.get(), &Task::succeeded, this, [this, ticket, res] { onParseSucceeded(ticket, res->internalId()); },
Qt::ConnectionType::QueuedConnection); Qt::ConnectionType::QueuedConnection);
connect( connect(
task.get(), &Task::failed, this, [this, ticket, res] { onParseFailed(ticket, res->internal_id()); }, task.get(), &Task::failed, this, [this, ticket, res] { onParseFailed(ticket, res->internalId()); },
Qt::ConnectionType::QueuedConnection); Qt::ConnectionType::QueuedConnection);
connect( connect(
task.get(), &Task::finished, this, task.get(), &Task::finished, this,
[this, ticket] { [this, ticket] {
m_active_parse_tasks.remove(ticket); m_activeParseTasks.remove(ticket);
emit parseFinished(); emit parseFinished();
}, },
Qt::ConnectionType::QueuedConnection); Qt::ConnectionType::QueuedConnection);
@ -394,44 +411,45 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res)
void ResourceFolderModel::onUpdateSucceeded() void ResourceFolderModel::onUpdateSucceeded()
{ {
auto update_results = static_cast<ResourceFolderLoadTask*>(m_current_update_task.get())->result(); auto updateResults = static_cast<ResourceFolderLoadTask*>(m_currentUpdateTask.get())->result();
auto& new_resources = update_results->resources; auto& newResources = updateResults->resources;
auto current_list = m_resources_index.keys(); auto currentList = m_resourcesIndex.keys();
QSet<QString> current_set(current_list.begin(), current_list.end()); QSet<QString> currentSet(currentList.begin(), currentList.end());
auto new_list = new_resources.keys(); auto newList = newResources.keys();
QSet<QString> new_set(new_list.begin(), new_list.end()); QSet<QString> newSet(newList.begin(), newList.end());
applyUpdates(current_set, new_set, new_resources); applyUpdates(currentSet, newSet, newResources);
} }
void ResourceFolderModel::onParseSucceeded(int ticket, QString resource_id) void ResourceFolderModel::onParseSucceeded(int ticket, const QString& resourceId)
{ {
auto iter = m_active_parse_tasks.constFind(ticket); auto iter = m_activeParseTasks.constFind(ticket);
if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id)) if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) {
return; return;
}
int row = m_resources_index[resource_id]; int row = m_resourcesIndex[resourceId];
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1)); emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
} }
Task* ResourceFolderModel::createUpdateTask() Task* ResourceFolderModel::createUpdateTask()
{ {
auto index_dir = indexDir(); auto indexDir2 = indexDir();
auto task = new ResourceFolderLoadTask(dir(), index_dir, m_is_indexed, m_first_folder_load, auto* task = new ResourceFolderLoadTask(dir(), indexDir2, m_isIndexed, m_firstFolderLoad,
[this](const QFileInfo& file) { return createResource(file); }); [this](const QFileInfo& file) { return createResource(file); });
m_first_folder_load = false; m_firstFolderLoad = false;
return task; return task;
} }
bool ResourceFolderModel::hasPendingParseTasks() const bool ResourceFolderModel::hasPendingParseTasks() const
{ {
return !m_active_parse_tasks.isEmpty(); return !m_activeParseTasks.isEmpty();
} }
void ResourceFolderModel::directoryChanged(QString path) void ResourceFolderModel::directoryChanged(const QString& /*path*/)
{ {
update(); update();
} }
@ -446,8 +464,9 @@ Qt::ItemFlags ResourceFolderModel::flags(const QModelIndex& index) const
{ {
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
auto flags = defaultFlags | Qt::ItemIsDropEnabled; auto flags = defaultFlags | Qt::ItemIsDropEnabled;
if (index.isValid()) if (index.isValid()) {
flags |= Qt::ItemIsUserCheckable; flags |= Qt::ItemIsUserCheckable;
}
return flags; return flags;
} }
@ -458,21 +477,25 @@ QStringList ResourceFolderModel::mimeTypes() const
return types; return types;
} }
bool ResourceFolderModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&) bool ResourceFolderModel::dropMimeData(const QMimeData* data,
Qt::DropAction action,
int /*row*/,
int /*column*/,
const QModelIndex& /*parent*/)
{ {
if (action == Qt::IgnoreAction) { if (action == Qt::IgnoreAction) {
return true; return true;
} }
// check if the action is supported // check if the action is supported
if (!data || !(action & supportedDropActions())) { if ((data == nullptr) || !(action & supportedDropActions())) {
return false; return false;
} }
// files dropped from outside? // files dropped from outside?
if (data->hasUrls()) { if (data->hasUrls()) {
auto urls = data->urls(); auto urls = data->urls();
for (auto url : urls) { for (const auto& url : urls) {
// only local files may be dropped... // only local files may be dropped...
if (!url.isLocalFile()) { if (!url.isLocalFile()) {
continue; continue;
@ -488,14 +511,12 @@ bool ResourceFolderModel::dropMimeData(const QMimeData* data, Qt::DropAction act
bool ResourceFolderModel::validateIndex(const QModelIndex& index) const bool ResourceFolderModel::validateIndex(const QModelIndex& index) const
{ {
if (!index.isValid()) if (!index.isValid()) {
return false; return false;
}
int row = index.row(); int row = index.row();
if (row < 0 || row >= m_resources.size()) return row >= 0 && row < m_resources.size();
return false;
return true;
} }
// HACK: all subclasses need to call this to have the whole row painted // HACK: all subclasses need to call this to have the whole row painted
@ -504,15 +525,15 @@ QBrush ResourceFolderModel::rowBackground(int row) const
{ {
if (APPLICATION->settings()->get("ShowModIncompat").toBool() && m_resources[row]->hasIssues()) { if (APPLICATION->settings()->get("ShowModIncompat").toBool() && m_resources[row]->hasIssues()) {
return { QColor(255, 0, 0, 40) }; return { QColor(255, 0, 0, 40) };
} else {
return {};
} }
return {};
} }
QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
{ {
if (!validateIndex(index)) if (!validateIndex(index)) {
return {}; return {};
}
int row = index.row(); int row = index.row();
int column = index.column(); int column = index.column();
@ -536,7 +557,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
return {}; return {};
} }
case Qt::ToolTipRole: { case Qt::ToolTipRole: {
QString tooltip = m_resources[row]->internal_id(); QString tooltip = m_resources[row]->internalId();
if (column == NameColumn) { if (column == NameColumn) {
if (APPLICATION->settings()->get("ShowModIncompat").toBool()) { if (APPLICATION->settings()->get("ShowModIncompat").toBool()) {
@ -547,7 +568,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
if (at(row).isSymLinkUnder(instDirPath())) { if (at(row).isSymLinkUnder(instDirPath())) {
tooltip += tooltip +=
m_resources[row]->internal_id() + m_resources[row]->internalId() +
tr("\nWarning: This resource is symbolically linked from elsewhere. Editing it will also change the original." tr("\nWarning: This resource is symbolically linked from elsewhere. Editing it will also change the original."
"\nCanonical Path: %1") "\nCanonical Path: %1")
.arg(at(row).fileinfo().canonicalFilePath()); .arg(at(row).fileinfo().canonicalFilePath());
@ -564,7 +585,8 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
if (column == NameColumn) { if (column == NameColumn) {
if (APPLICATION->settings()->get("ShowModIncompat").toBool() && at(row).hasIssues()) { if (APPLICATION->settings()->get("ShowModIncompat").toBool() && at(row).hasIssues()) {
return QIcon::fromTheme("status-bad"); return QIcon::fromTheme("status-bad");
} else if (at(row).isSymLinkUnder(instDirPath()) || at(row).isMoreThanOneHardLink()) { }
if (at(row).isSymLinkUnder(instDirPath()) || at(row).isMoreThanOneHardLink()) {
return QIcon::fromTheme("status-yellow"); return QIcon::fromTheme("status-yellow");
} }
} }
@ -572,8 +594,9 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
return {}; return {};
} }
case Qt::CheckStateRole: case Qt::CheckStateRole:
if (column == ActiveColumn) if (column == ActiveColumn) {
return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked; return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked;
}
return {}; return {};
default: default:
return {}; return {};
@ -583,8 +606,9 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
bool ResourceFolderModel::setData(const QModelIndex& index, [[maybe_unused]] const QVariant& value, int role) bool ResourceFolderModel::setData(const QModelIndex& index, [[maybe_unused]] const QVariant& value, int role)
{ {
int row = index.row(); int row = index.row();
if (row < 0 || row >= rowCount(index.parent()) || !index.isValid()) if (row < 0 || row >= rowCount(index.parent()) || !index.isValid()) {
return false; return false;
}
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
return setResourceEnabled({ index }, EnableAction::TOGGLE); return setResourceEnabled({ index }, EnableAction::TOGGLE);
@ -643,22 +667,22 @@ void ResourceFolderModel::setupHeaderAction(QAction* act, int column)
void ResourceFolderModel::saveColumns(QTreeView* tree) void ResourceFolderModel::saveColumns(QTreeView* tree)
{ {
auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id()); const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id());
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
auto stateSetting = m_instance->settings()->getSetting(stateSettingName); auto stateSetting = m_instance->settings()->getSetting(stateSettingName);
stateSetting->set(QString::fromUtf8(tree->header()->saveState().toBase64())); stateSetting->set(QString::fromUtf8(tree->header()->saveState().toBase64()));
// neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false // neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false
auto settings = m_instance->settings(); auto* settings = m_instance->settings();
if (!settings->get(overrideSettingName).toBool()) { if (!settings->get(overrideSettingName).toBool()) {
settings = APPLICATION->settings(); settings = APPLICATION->settings();
} }
auto visibility = Json::toMap(settings->get(visibilitySettingName).toString()); auto visibility = Json::toMap(settings->get(visibilitySettingName).toString());
for (auto i = 0; i < m_column_names.size(); ++i) { for (auto i = 0; i < m_columnNames.size(); ++i) {
if (m_columnsHideable[i]) { if (m_columnsHideable[i]) {
auto name = m_column_names[i]; auto name = m_columnNames[i];
visibility[name] = !tree->isColumnHidden(i); visibility[name] = !tree->isColumnHidden(i);
} }
} }
@ -667,24 +691,24 @@ void ResourceFolderModel::saveColumns(QTreeView* tree)
void ResourceFolderModel::loadColumns(QTreeView* tree) void ResourceFolderModel::loadColumns(QTreeView* tree)
{ {
auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id()); const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id());
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
auto stateSetting = m_instance->settings()->getOrRegisterSetting(stateSettingName, ""); auto stateSetting = m_instance->settings()->getOrRegisterSetting(stateSettingName, "");
tree->header()->restoreState(QByteArray::fromBase64(stateSetting->get().toString().toUtf8())); tree->header()->restoreState(QByteArray::fromBase64(stateSetting->get().toString().toUtf8()));
auto setVisible = [this, tree](QVariant value) { auto setVisible = [this, tree](const QVariant& value) {
auto visibility = Json::toMap(value.toString()); auto visibility = Json::toMap(value.toString());
for (auto i = 0; i < m_column_names.size(); ++i) { for (auto i = 0; i < m_columnNames.size(); ++i) {
if (m_columnsHideable[i]) { if (m_columnsHideable[i]) {
auto name = m_column_names[i]; auto name = m_columnNames[i];
tree->setColumnHidden(i, !visibility.value(name, false).toBool()); tree->setColumnHidden(i, !visibility.value(name, false).toBool());
} }
} }
}; };
auto const defaultValue = Json::fromMap({ const auto defaultValue = Json::fromMap({
{ "Image", true }, { "Image", true },
{ "Version", true }, { "Version", true },
{ "Last Modified", true }, { "Last Modified", true },
@ -692,7 +716,7 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
{ "Pack Format", true }, { "Pack Format", true },
}); });
// neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false // neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false
auto settings = m_instance->settings(); auto* settings = m_instance->settings();
if (!settings->getOrRegisterSetting(overrideSettingName, false)->get().toBool()) { if (!settings->getOrRegisterSetting(overrideSettingName, false)->get().toBool()) {
settings = APPLICATION->settings(); settings = APPLICATION->settings();
} }
@ -701,7 +725,7 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
// allways connect the signal in case the setting is toggled on and off // allways connect the signal in case the setting is toggled on and off
auto gSetting = APPLICATION->settings()->getOrRegisterSetting(visibilitySettingName, defaultValue); auto gSetting = APPLICATION->settings()->getOrRegisterSetting(visibilitySettingName, defaultValue);
connect(gSetting.get(), &Setting::SettingChanged, tree, [this, setVisible, overrideSettingName](const Setting&, QVariant value) { connect(gSetting.get(), &Setting::SettingChanged, tree, [this, setVisible, overrideSettingName](const Setting&, const QVariant& value) {
if (!m_instance->settings()->get(overrideSettingName).toBool()) { if (!m_instance->settings()->get(overrideSettingName).toBool()) {
setVisible(value); setVisible(value);
} }
@ -710,11 +734,11 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree) QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
{ {
auto menu = new QMenu(tree); auto* menu = new QMenu(tree);
{ // action to decide if the visibility is per instance or not { // action to decide if the visibility is per instance or not
auto act = new QAction(tr("Override Columns Visibility"), menu); auto* act = new QAction(tr("Override Columns Visibility"), menu);
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
act->setCheckable(true); act->setCheckable(true);
act->setChecked(m_instance->settings()->getOrRegisterSetting(overrideSettingName, false)->get().toBool()); act->setChecked(m_instance->settings()->getOrRegisterSetting(overrideSettingName, false)->get().toBool());
@ -730,9 +754,10 @@ QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
for (int col = 0; col < columnCount(); ++col) { for (int col = 0; col < columnCount(); ++col) {
// Skip creating actions for columns that should not be hidden // Skip creating actions for columns that should not be hidden
if (!m_columnsHideable.at(col)) if (!m_columnsHideable.at(col)) {
continue; continue;
auto act = new QAction(menu); }
auto* act = new QAction(menu);
setupHeaderAction(act, col); setupHeaderAction(act, col);
act->setCheckable(true); act->setCheckable(true);
@ -741,9 +766,10 @@ QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
connect(act, &QAction::toggled, tree, [this, col, tree](bool toggled) { connect(act, &QAction::toggled, tree, [this, col, tree](bool toggled) {
tree->setColumnHidden(col, !toggled); tree->setColumnHidden(col, !toggled);
for (int c = 0; c < columnCount(); ++c) { for (int c = 0; c < columnCount(); ++c) {
if (m_column_resize_modes.at(c) == QHeaderView::ResizeToContents) if (m_columnResizeModes.at(c) == QHeaderView::ResizeToContents) {
tree->resizeColumnToContents(c); tree->resizeColumnToContents(c);
} }
}
saveColumns(tree); saveColumns(tree);
}); });
@ -760,41 +786,43 @@ QSortFilterProxyModel* ResourceFolderModel::createFilterProxyModel(QObject* pare
SortType ResourceFolderModel::columnToSortKey(size_t column) const SortType ResourceFolderModel::columnToSortKey(size_t column) const
{ {
Q_ASSERT(m_column_sort_keys.size() == columnCount()); Q_ASSERT(m_columnSortKeys.size() == columnCount());
return m_column_sort_keys.at(column); return m_columnSortKeys.at(column);
} }
/* Standard Proxy Model for createFilterProxyModel */ /* Standard Proxy Model for createFilterProxyModel */
bool ResourceFolderModel::ProxyModel::filterAcceptsRow(int source_row, [[maybe_unused]] const QModelIndex& source_parent) const bool ResourceFolderModel::ProxyModel::filterAcceptsRow(int sourceRow, [[maybe_unused]] const QModelIndex& sourceParent) const
{ {
auto* model = qobject_cast<ResourceFolderModel*>(sourceModel()); auto* model = qobject_cast<ResourceFolderModel*>(sourceModel());
if (!model) if (!model) {
return true; return true;
}
const auto& resource = model->at(source_row); const auto& resource = model->at(sourceRow);
return resource.applyFilter(filterRegularExpression()); return resource.applyFilter(filterRegularExpression());
} }
bool ResourceFolderModel::ProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const bool ResourceFolderModel::ProxyModel::lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const
{ {
auto* model = qobject_cast<ResourceFolderModel*>(sourceModel()); auto* model = qobject_cast<ResourceFolderModel*>(sourceModel());
if (!model || !source_left.isValid() || !source_right.isValid() || source_left.column() != source_right.column()) { if (!model || !sourceLeft.isValid() || !sourceRight.isValid() || sourceLeft.column() != sourceRight.column()) {
return QSortFilterProxyModel::lessThan(source_left, source_right); return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
} }
// we are now guaranteed to have two valid indexes in the same column... we love the provided invariants unconditionally and // we are now guaranteed to have two valid indexes in the same column... we love the provided invariants unconditionally and
// proceed. // proceed.
auto column_sort_key = model->columnToSortKey(source_left.column()); auto columnSortKey = model->columnToSortKey(sourceLeft.column());
auto const& resource_left = model->at(source_left.row()); const auto& resourceLeft = model->at(sourceLeft.row());
auto const& resource_right = model->at(source_right.row()); const auto& resourceRight = model->at(sourceRight.row());
auto compare_result = resource_left.compare(resource_right, column_sort_key); auto compareResult = resourceLeft.compare(resourceRight, columnSortKey);
if (compare_result == 0) if (compareResult == 0) {
return QSortFilterProxyModel::lessThan(source_left, source_right); return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
}
return compare_result < 0; return compareResult < 0;
} }
QString ResourceFolderModel::instDirPath() const QString ResourceFolderModel::instDirPath() const
@ -802,50 +830,51 @@ QString ResourceFolderModel::instDirPath() const
return QFileInfo(m_instance->instanceRoot()).absoluteFilePath(); return QFileInfo(m_instance->instanceRoot()).absoluteFilePath();
} }
void ResourceFolderModel::onParseFailed(int ticket, QString resource_id) void ResourceFolderModel::onParseFailed(int ticket, const QString& resourceId)
{ {
auto iter = m_active_parse_tasks.constFind(ticket); auto iter = m_activeParseTasks.constFind(ticket);
if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id)) if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) {
return; return;
}
auto removed_index = m_resources_index[resource_id]; auto removedIndex = m_resourcesIndex[resourceId];
auto removed_it = m_resources.begin() + removed_index; auto removedIt = m_resources.begin() + removedIndex;
Q_ASSERT(removed_it != m_resources.end()); Q_ASSERT(removedIt != m_resources.end());
beginRemoveRows(QModelIndex(), removed_index, removed_index); beginRemoveRows(QModelIndex(), removedIndex, removedIndex);
m_resources.erase(removed_it); m_resources.erase(removedIt);
// update index // update index
m_resources_index.clear(); m_resourcesIndex.clear();
int idx = 0; int idx = 0;
for (auto const& mod : qAsConst(m_resources)) { for (const auto& mod : qAsConst(m_resources)) {
m_resources_index[mod->internal_id()] = idx; m_resourcesIndex[mod->internalId()] = idx;
idx++; idx++;
} }
endRemoveRows(); endRemoveRows();
} }
void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>& new_set, QMap<QString, Resource::Ptr>& new_resources) void ResourceFolderModel::applyUpdates(QSet<QString>& currentSet, QSet<QString>& newSet, QMap<QString, Resource::Ptr>& newResources)
{ {
// see if the kept resources changed in some way // see if the kept resources changed in some way
{ {
QSet<QString> kept_set = current_set; QSet<QString> keptSet = currentSet;
kept_set.intersect(new_set); keptSet.intersect(newSet);
for (auto const& kept : kept_set) { for (const auto& kept : keptSet) {
auto row_it = m_resources_index.constFind(kept); auto rowIt = m_resourcesIndex.constFind(kept);
Q_ASSERT(row_it != m_resources_index.constEnd()); Q_ASSERT(rowIt != m_resourcesIndex.constEnd());
auto row = row_it.value(); auto row = rowIt.value();
auto& new_resource = new_resources[kept]; auto& newResource = newResources[kept];
auto const& current_resource = m_resources.at(row); const auto& currentResource = m_resources.at(row);
if (new_resource->dateTimeChanged() == current_resource->dateTimeChanged()) { if (newResource->dateTimeChanged() == currentResource->dateTimeChanged()) {
// no significant change // no significant change
bool hadIssues = !current_resource->hasIssues(); bool hadIssues = !currentResource->hasIssues();
current_resource->updateIssues(m_instance); currentResource->updateIssues(m_instance);
if (hadIssues != current_resource->hasIssues()) { if (hadIssues != currentResource->hasIssues()) {
emit dataChanged(index(row, 0), index(row, columnCount({}) - 1)); emit dataChanged(index(row, 0), index(row, columnCount({}) - 1));
} }
continue; continue;
@ -853,16 +882,16 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// If the resource is resolving, but something about it changed, we don't want to // If the resource is resolving, but something about it changed, we don't want to
// continue the resolving. // continue the resolving.
if (current_resource->isResolving()) { if (currentResource->isResolving()) {
auto ticket = current_resource->resolutionTicket(); auto ticket = currentResource->resolutionTicket();
if (m_active_parse_tasks.contains(ticket)) { if (m_activeParseTasks.contains(ticket)) {
auto task = (*m_active_parse_tasks.find(ticket)).get(); auto* task = (*m_activeParseTasks.find(ticket)).get();
task->abort(); task->abort();
} }
} }
m_resources[row].reset(new_resource); m_resources[row].reset(newResource);
new_resource->updateIssues(m_instance); newResource->updateIssues(m_instance);
resolveResource(m_resources.at(row)); resolveResource(m_resources.at(row));
emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1)); emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1));
@ -871,46 +900,47 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// remove resources no longer present // remove resources no longer present
{ {
QSet<QString> removed_set = current_set; QSet<QString> removedSet = currentSet;
removed_set.subtract(new_set); removedSet.subtract(newSet);
QList<int> removed_rows; QList<int> removedRows;
for (auto& removed : removed_set) for (const auto& removed : removedSet) {
removed_rows.append(m_resources_index[removed]); removedRows.append(m_resourcesIndex[removed]);
}
std::sort(removed_rows.begin(), removed_rows.end(), std::greater<int>()); std::ranges::sort(removedRows, std::greater());
for (auto& removed_index : removed_rows) { for (auto& removedIndex : removedRows) {
auto removed_it = m_resources.begin() + removed_index; auto removedIt = m_resources.begin() + removedIndex;
Q_ASSERT(removed_it != m_resources.end()); Q_ASSERT(removedIt != m_resources.end());
if ((*removed_it)->isResolving()) { if ((*removedIt)->isResolving()) {
auto ticket = (*removed_it)->resolutionTicket(); auto ticket = (*removedIt)->resolutionTicket();
if (m_active_parse_tasks.contains(ticket)) { if (m_activeParseTasks.contains(ticket)) {
auto task = (*m_active_parse_tasks.find(ticket)).get(); auto* task = (*m_activeParseTasks.find(ticket)).get();
task->abort(); task->abort();
} }
} }
beginRemoveRows(QModelIndex(), removed_index, removed_index); beginRemoveRows(QModelIndex(), removedIndex, removedIndex);
m_resources.erase(removed_it); m_resources.erase(removedIt);
endRemoveRows(); endRemoveRows();
} }
} }
// add new resources to the end // add new resources to the end
{ {
QSet<QString> added_set = new_set; QSet<QString> addedSet = newSet;
added_set.subtract(current_set); addedSet.subtract(currentSet);
// When you have a Qt build with assertions turned on, proceeding here will abort the application // When you have a Qt build with assertions turned on, proceeding here will abort the application
if (added_set.size() > 0) { if (addedSet.size() > 0) {
beginInsertRows(QModelIndex(), static_cast<int>(m_resources.size()), beginInsertRows(QModelIndex(), static_cast<int>(m_resources.size()),
static_cast<int>(m_resources.size() + added_set.size() - 1)); static_cast<int>(m_resources.size() + addedSet.size() - 1));
for (auto& added : added_set) { for (const auto& added : addedSet) {
auto res = new_resources[added]; auto res = newResources[added];
res->updateIssues(m_instance); res->updateIssues(m_instance);
m_resources.append(res); m_resources.append(res);
resolveResource(m_resources.last()); resolveResource(m_resources.last());
@ -922,10 +952,10 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// update index // update index
{ {
m_resources_index.clear(); m_resourcesIndex.clear();
int idx = 0; int idx = 0;
for (auto const& mod : qAsConst(m_resources)) { for (const auto& mod : qAsConst(m_resources)) {
m_resources_index[mod->internal_id()] = idx; m_resourcesIndex[mod->internalId()] = idx;
idx++; idx++;
} }
} }
@ -933,17 +963,19 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
Resource::Ptr ResourceFolderModel::find(QString id) Resource::Ptr ResourceFolderModel::find(QString id)
{ {
auto iter = auto iter =
std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](Resource::Ptr const& r) { return r->internal_id() == id; }); std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](const Resource::Ptr& r) { return r->internalId() == id; });
if (iter == m_resources.constEnd()) if (iter == m_resources.constEnd()) {
return nullptr; return nullptr;
}
return *iter; return *iter;
} }
QList<Resource*> ResourceFolderModel::allResources() QList<Resource*> ResourceFolderModel::allResources()
{ {
QList<Resource*> result; QList<Resource*> result;
result.reserve(m_resources.size()); result.reserve(m_resources.size());
for (const Resource ::Ptr& resource : m_resources) for (const Resource ::Ptr& resource : m_resources) {
result.append((resource.get())); result.append((resource.get()));
}
return result; return result;
} }
@ -951,8 +983,9 @@ QList<Resource*> ResourceFolderModel::selectedResources(const QModelIndexList& i
{ {
QList<Resource*> result; QList<Resource*> result;
for (const QModelIndex& index : indexes) { for (const QModelIndex& index : indexes) {
if (index.column() != 0) if (index.column() != 0) {
continue; continue;
}
result.append(&at(index.row())); result.append(&at(index.row()));
} }
return result; return result;

View file

@ -61,7 +61,7 @@ class QSortFilterProxyModel;
class ResourceFolderModel : public QAbstractListModel { class ResourceFolderModel : public QAbstractListModel {
Q_OBJECT Q_OBJECT
public: public:
ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent = nullptr); ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent = nullptr);
~ResourceFolderModel() override; ~ResourceFolderModel() override;
virtual QString id() const { return "resource"; } virtual QString id() const { return "resource"; }
@ -93,13 +93,13 @@ class ResourceFolderModel : public QAbstractListModel {
*/ */
virtual bool installResource(QString path); virtual bool installResource(QString path);
virtual void installResourceWithFlameMetadata(QString path, ModPlatform::IndexedVersion& vers); virtual void installResourceWithFlameMetadata(const QString& path, ModPlatform::IndexedVersion& vers);
/** Uninstall (i.e. remove all data about it) a resource, given its file name. /** Uninstall (i.e. remove all data about it) a resource, given its file name.
* *
* Returns whether the removal was successful. * Returns whether the removal was successful.
*/ */
virtual bool uninstallResource(const QString& file_name, bool preserve_metadata = false); virtual bool uninstallResource(const QString& fileName, bool preserveMetadata = false);
virtual bool deleteResources(const QModelIndexList&); virtual bool deleteResources(const QModelIndexList&);
virtual void deleteMetadata(const QModelIndexList&); virtual void deleteMetadata(const QModelIndexList&);
@ -125,7 +125,7 @@ class ResourceFolderModel : public QAbstractListModel {
Resource::Ptr find(QString id); Resource::Ptr find(QString id);
QDir const& dir() const { return m_dir; } const QDir& dir() const { return m_dir; }
/** Checks whether there's any parse tasks being done. /** Checks whether there's any parse tasks being done.
* *
@ -137,12 +137,12 @@ class ResourceFolderModel : public QAbstractListModel {
/* Qt behavior */ /* Qt behavior */
/* Basic columns */ /* Basic columns */
enum Columns { ActiveColumn = 0, NameColumn, DateColumn, ProviderColumn, SizeColumn, FileNameColumn, NUM_COLUMNS }; enum Columns : std::uint8_t { ActiveColumn = 0, NameColumn, DateColumn, ProviderColumn, SizeColumn, FileNameColumn, NumColumns };
QStringList columnNames(bool translated = true) const { return translated ? m_column_names_translated : m_column_names; } QStringList columnNames(bool translated = true) const { return translated ? m_columnNamesTranslated : m_columnNames; }
int rowCount(const QModelIndex& parent = {}) const override { return parent.isValid() ? 0 : static_cast<int>(size()); } int rowCount(const QModelIndex& parent = {}) const override { return parent.isValid() ? 0 : static_cast<int>(size()); }
int columnCount(const QModelIndex& parent = {}) const override { return parent.isValid() ? 0 : NUM_COLUMNS; } int columnCount(const QModelIndex& parent = {}) const override { return parent.isValid() ? 0 : NumColumns; }
Qt::DropActions supportedDropActions() const override; Qt::DropActions supportedDropActions() const override;
@ -171,15 +171,15 @@ class ResourceFolderModel : public QAbstractListModel {
QSortFilterProxyModel* createFilterProxyModel(QObject* parent = nullptr); QSortFilterProxyModel* createFilterProxyModel(QObject* parent = nullptr);
SortType columnToSortKey(size_t column) const; SortType columnToSortKey(size_t column) const;
QList<QHeaderView::ResizeMode> columnResizeModes() const { return m_column_resize_modes; } QList<QHeaderView::ResizeMode> columnResizeModes() const { return m_columnResizeModes; }
class ProxyModel : public QSortFilterProxyModel { class ProxyModel : public QSortFilterProxyModel {
public: public:
explicit ProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {} explicit ProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}
protected: protected:
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override; bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override;
}; };
QString instDirPath() const; QString instDirPath() const;
@ -207,7 +207,7 @@ class ResourceFolderModel : public QAbstractListModel {
* This task should load and parse all heavy info needed by a resource, such as parsing a manifest. It gets executed * This task should load and parse all heavy info needed by a resource, such as parsing a manifest. It gets executed
* in the background, so it slowly updates the UI as tasks get done. * in the background, so it slowly updates the UI as tasks get done.
*/ */
[[nodiscard]] virtual Task* createParseTask(Resource&) { return nullptr; } [[nodiscard]] virtual Task* createParseTask(Resource& /*unused*/) { return nullptr; }
/** Standard implementation of the model update logic. /** Standard implementation of the model update logic.
* *
@ -215,10 +215,10 @@ class ResourceFolderModel : public QAbstractListModel {
* to act only on those disparities. * to act only on those disparities.
* *
*/ */
void applyUpdates(QSet<QString>& current_set, QSet<QString>& new_set, QMap<QString, Resource::Ptr>& new_resources); void applyUpdates(QSet<QString>& currentSet, QSet<QString>& newSet, QMap<QString, Resource::Ptr>& newResources);
protected slots: protected slots:
void directoryChanged(QString); void directoryChanged(const QString&);
/** Called when the update task is successful. /** Called when the update task is successful.
* *
@ -234,40 +234,40 @@ class ResourceFolderModel : public QAbstractListModel {
* This is just a simple reference implementation. You probably want to override it with your own logic in a subclass * This is just a simple reference implementation. You probably want to override it with your own logic in a subclass
* if the resource is complex and has more stuff to parse. * if the resource is complex and has more stuff to parse.
*/ */
virtual void onParseSucceeded(int ticket, QString resource_id); virtual void onParseSucceeded(int ticket, const QString& resourceId);
virtual void onParseFailed(int ticket, QString resource_id); virtual void onParseFailed(int ticket, const QString& resourceId);
protected: protected:
// Represents the relationship between a column's index (represented by the list index), and it's sorting key. // Represents the relationship between a column's index (represented by the list index), and it's sorting key.
// As such, the order in with they appear is very important! // As such, the order in with they appear is very important!
QList<SortType> m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::DATE, SortType::PROVIDER, SortType::SIZE, QList<SortType> m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Date,
SortType::FILENAME }; SortType::Provider, SortType::Size, SortType::Filename };
QStringList m_column_names = { "Enable", "Name", "Last Modified", "Provider", "Size", "File Name" }; QStringList m_columnNames = { "Enable", "Name", "Last Modified", "Provider", "Size", "File Name" };
QStringList m_column_names_translated = { tr("Enable"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") }; QStringList m_columnNamesTranslated = { tr("Enable"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") };
QList<QHeaderView::ResizeMode> m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, QList<QHeaderView::ResizeMode> m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive };
QList<bool> m_columnsHideable = { false, false, true, true, true, true }; QList<bool> m_columnsHideable = { false, false, true, true, true, true };
QDir m_dir; QDir m_dir;
BaseInstance* m_instance; BaseInstance* m_instance;
QFileSystemWatcher m_watcher; QFileSystemWatcher m_watcher;
bool m_is_watching = false; bool m_isWatching = false;
bool m_is_indexed; bool m_isIndexed;
bool m_first_folder_load = true; bool m_firstFolderLoad = true;
Task::Ptr m_current_update_task = nullptr; Task::Ptr m_currentUpdateTask = nullptr;
bool m_scheduled_update = false; bool m_scheduledUpdate = false;
QList<Resource::Ptr> m_resources; QList<Resource::Ptr> m_resources;
// Represents the relationship between a resource's internal ID and it's row position on the model. // Represents the relationship between a resource's internal ID and it's row position on the model.
QMap<QString, int> m_resources_index; QMap<QString, int> m_resourcesIndex;
// Runs off-thread // Runs off-thread
ConcurrentTask m_resourceResolver; ConcurrentTask m_resourceResolver;
bool m_resourceResolverRunning = false; bool m_resourceResolverRunning = false;
QMap<int, Task::Ptr> m_active_parse_tasks; QMap<int, Task::Ptr> m_activeParseTasks;
std::atomic<int> m_next_resolution_ticket = 0; std::atomic<int> m_nextResolutionTicket = 0;
}; };

View file

@ -39,27 +39,26 @@
#include <QIcon> #include <QIcon>
#include <QStyle> #include <QStyle>
#include "Version.h"
#include "minecraft/mod/tasks/LocalDataPackParseTask.h" #include "minecraft/mod/tasks/LocalDataPackParseTask.h"
ResourcePackFolderModel::ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) ResourcePackFolderModel::ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: ResourceFolderModel(dir, instance, is_indexed, create_dir, parent) : ResourceFolderModel(dir, instance, isIndexed, createDir, parent)
{ {
m_column_names = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "Provider", "Size", "File Name" }); m_columnNames = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "Provider", "Size", "File Name" });
m_column_names_translated = QStringList( m_columnNamesTranslated = QStringList(
{ tr("Enable"), tr("Image"), tr("Name"), tr("Pack Format"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") }); { tr("Enable"), tr("Image"), tr("Name"), tr("Pack Format"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") });
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::PACK_FORMAT, m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::PackFormat,
SortType::DATE, SortType::PROVIDER, SortType::SIZE, SortType::FILENAME }; SortType::Date, SortType::Provider, SortType::Size, SortType::Filename };
m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive };
m_columnsHideable = { false, true, false, true, true, true, true, true }; m_columnsHideable = { false, true, false, true, true, true, true, true };
} }
QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const
{ {
if (!validateIndex(index)) if (!validateIndex(index)) {
return {}; return {};
}
int row = index.row(); int row = index.row();
int column = index.column(); int column = index.column();
@ -92,6 +91,8 @@ QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const
return QSize(32, 32); return QSize(32, 32);
} }
break; break;
default:
break;
} }
// map the columns to the base equivilents // map the columns to the base equivilents
@ -115,6 +116,8 @@ QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const
case FileNameColumn: case FileNameColumn:
mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn);
break; break;
default:
break;
} }
if (mappedIndex.isValid()) { if (mappedIndex.isValid()) {
@ -174,10 +177,10 @@ QVariant ResourcePackFolderModel::headerData(int section, [[maybe_unused]] Qt::O
int ResourcePackFolderModel::columnCount(const QModelIndex& parent) const int ResourcePackFolderModel::columnCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : NUM_COLUMNS; return parent.isValid() ? 0 : NumColumns;
} }
Task* ResourcePackFolderModel::createParseTask(Resource& resource) Task* ResourcePackFolderModel::createParseTask(Resource& resource)
{ {
return new LocalDataPackParseTask(m_next_resolution_ticket, dynamic_cast<ResourcePack*>(&resource)); return new LocalDataPackParseTask(m_nextResolutionTicket, dynamic_cast<ResourcePack*>(&resource));
} }

View file

@ -7,9 +7,19 @@
class ResourcePackFolderModel : public ResourceFolderModel { class ResourcePackFolderModel : public ResourceFolderModel {
Q_OBJECT Q_OBJECT
public: public:
enum Columns { ActiveColumn = 0, ImageColumn, NameColumn, PackFormatColumn, DateColumn, ProviderColumn, SizeColumn, FileNameColumn, NUM_COLUMNS }; enum Columns : std::uint8_t {
ActiveColumn = 0,
ImageColumn,
NameColumn,
PackFormatColumn,
DateColumn,
ProviderColumn,
SizeColumn,
FileNameColumn,
NumColumns
};
explicit ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent = nullptr); explicit ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent = nullptr);
QString id() const override { return "resourcepacks"; } QString id() const override { return "resourcepacks"; }
@ -19,7 +29,7 @@ class ResourcePackFolderModel : public ResourceFolderModel {
int columnCount(const QModelIndex& parent) const override; int columnCount(const QModelIndex& parent) const override;
[[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new ResourcePack(file); } [[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new ResourcePack(file); }
[[nodiscard]] Task* createParseTask(Resource&) override; [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override;
RESOURCE_HELPERS(ResourcePack) RESOURCE_HELPERS(ResourcePack)
}; };

View file

@ -18,7 +18,7 @@ class ShaderPackFolderModel : public ResourceFolderModel {
[[nodiscard]] Task* createParseTask(Resource& resource) override [[nodiscard]] Task* createParseTask(Resource& resource) override
{ {
return new LocalShaderPackParseTask(m_next_resolution_ticket, static_cast<ShaderPack&>(resource)); return new LocalShaderPackParseTask(m_nextResolutionTicket, static_cast<ShaderPack&>(resource));
} }
QDir indexDir() const override { return m_dir; } QDir indexDir() const override { return m_dir; }

View file

@ -36,28 +36,30 @@
#include "TexturePackFolderModel.h" #include "TexturePackFolderModel.h"
#include "minecraft/mod/tasks/LocalTexturePackParseTask.h" #include "minecraft/mod/tasks/LocalTexturePackParseTask.h"
#include "minecraft/mod/tasks/ResourceFolderLoadTask.h"
TexturePackFolderModel::TexturePackFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) TexturePackFolderModel::TexturePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent)
{ {
m_column_names = QStringList({ "Enable", "Image", "Name", "Last Modified", "Provider", "Size", "File Name" }); m_columnNames = QStringList({ "Enable", "Image", "Name", "Last Modified", "Provider", "Size", "File Name" });
m_column_names_translated = QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") }); m_columnNamesTranslated =
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::DATE, SortType::PROVIDER, SortType::SIZE, SortType::FILENAME }; QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") });
m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::Date,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; SortType::Provider, SortType::Size, SortType::Filename };
m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive,
QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive };
m_columnsHideable = { false, true, false, true, true, true, true }; m_columnsHideable = { false, true, false, true, true, true, true };
} }
Task* TexturePackFolderModel::createParseTask(Resource& resource) Task* TexturePackFolderModel::createParseTask(Resource& resource)
{ {
return new LocalTexturePackParseTask(m_next_resolution_ticket, static_cast<TexturePack&>(resource)); return new LocalTexturePackParseTask(m_nextResolutionTicket, static_cast<TexturePack&>(resource));
} }
QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const
{ {
if (!validateIndex(index)) if (!validateIndex(index)) {
return {}; return {};
}
int row = index.row(); int row = index.row();
int column = index.column(); int column = index.column();
@ -76,6 +78,8 @@ QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const
return QSize(32, 32); return QSize(32, 32);
} }
break; break;
default:
break;
} }
// map the columns to the base equivilents // map the columns to the base equivilents
@ -99,6 +103,8 @@ QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const
case FileNameColumn: case FileNameColumn:
mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn);
break; break;
default:
break;
} }
if (mappedIndex.isValid()) { if (mappedIndex.isValid()) {
@ -151,5 +157,5 @@ QVariant TexturePackFolderModel::headerData(int section, [[maybe_unused]] Qt::Or
int TexturePackFolderModel::columnCount(const QModelIndex& parent) const int TexturePackFolderModel::columnCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : NUM_COLUMNS; return parent.isValid() ? 0 : NumColumns;
} }

View file

@ -44,11 +44,20 @@ class TexturePackFolderModel : public ResourceFolderModel {
Q_OBJECT Q_OBJECT
public: public:
enum Columns { ActiveColumn = 0, ImageColumn, NameColumn, DateColumn, ProviderColumn, SizeColumn, FileNameColumn, NUM_COLUMNS }; enum Columns : std::uint8_t {
ActiveColumn = 0,
ImageColumn,
NameColumn,
DateColumn,
ProviderColumn,
SizeColumn,
FileNameColumn,
NumColumns
};
explicit TexturePackFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent = nullptr); explicit TexturePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent = nullptr);
virtual QString id() const override { return "texturepacks"; } QString id() const override { return "texturepacks"; }
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
@ -56,7 +65,7 @@ class TexturePackFolderModel : public ResourceFolderModel {
int columnCount(const QModelIndex& parent) const override; int columnCount(const QModelIndex& parent) const override;
[[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new TexturePack(file); } [[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new TexturePack(file); }
[[nodiscard]] Task* createParseTask(Resource&) override; [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override;
RESOURCE_HELPERS(TexturePack) RESOURCE_HELPERS(TexturePack)
}; };

View file

@ -41,26 +41,28 @@
#include "minecraft/mod/MetadataHandler.h" #include "minecraft/mod/MetadataHandler.h"
#include <QThread> #include <QThread>
#include <utility>
ResourceFolderLoadTask::ResourceFolderLoadTask(const QDir& resource_dir, ResourceFolderLoadTask::ResourceFolderLoadTask(const QDir& resourceDir,
const QDir& index_dir, const QDir& indexDir,
bool is_indexed, bool isIndexed,
bool clean_orphan, bool cleanOrphan,
std::function<Resource*(const QFileInfo&)> create_function) std::function<Resource*(const QFileInfo&)> createFunction)
: Task(false) : Task(false)
, m_resource_dir(resource_dir) , m_resource_dir(resourceDir)
, m_index_dir(index_dir) , m_index_dir(indexDir)
, m_is_indexed(is_indexed) , m_is_indexed(isIndexed)
, m_clean_orphan(clean_orphan) , m_clean_orphan(cleanOrphan)
, m_create_func(create_function) , m_create_func(std::move(createFunction))
, m_result(new Result()) , m_result(new Result())
, m_thread_to_spawn_into(thread()) , m_thread_to_spawn_into(thread())
{} {}
void ResourceFolderLoadTask::executeTask() void ResourceFolderLoadTask::executeTask()
{ {
if (thread() != m_thread_to_spawn_into) if (thread() != m_thread_to_spawn_into) {
connect(this, &Task::finished, this->thread(), &QThread::quit); connect(this, &Task::finished, this->thread(), &QThread::quit);
}
if (m_is_indexed) { if (m_is_indexed) {
// Read metadata first // Read metadata first
@ -71,7 +73,7 @@ void ResourceFolderLoadTask::executeTask()
m_resource_dir.refresh(); m_resource_dir.refresh();
for (auto entry : m_resource_dir.entryInfoList()) { for (auto entry : m_resource_dir.entryInfoList()) {
auto filePath = entry.absoluteFilePath(); auto filePath = entry.absoluteFilePath();
if (auto app = APPLICATION_DYN; app && app->checkQSavePath(filePath)) { if (auto* app = APPLICATION_DYN; (app != nullptr) && app->checkQSavePath(filePath)) {
continue; continue;
} }
auto newFilePath = FS::getUniqueResourceName(filePath); auto newFilePath = FS::getUniqueResourceName(filePath);
@ -83,29 +85,29 @@ void ResourceFolderLoadTask::executeTask()
Resource* resource = m_create_func(entry); Resource* resource = m_create_func(entry);
if (resource->enabled()) { if (resource->enabled()) {
if (m_result->resources.contains(resource->internal_id())) { if (m_result->resources.contains(resource->internalId())) {
m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::INSTALLED); m_result->resources[resource->internalId()]->setStatus(ResourceStatus::Installed);
// Delete the object we just created, since a valid one is already in the mods list. // Delete the object we just created, since a valid one is already in the mods list.
delete resource; delete resource;
} else { } else {
m_result->resources[resource->internal_id()].reset(resource); m_result->resources[resource->internalId()].reset(resource);
m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::NO_METADATA); m_result->resources[resource->internalId()]->setStatus(ResourceStatus::NoMetadata);
} }
} else { } else {
QString chopped_id = resource->internal_id().chopped(9); QString choppedId = resource->internalId().chopped(9);
if (m_result->resources.contains(chopped_id)) { if (m_result->resources.contains(choppedId)) {
m_result->resources[resource->internal_id()].reset(resource); m_result->resources[resource->internalId()].reset(resource);
auto metadata = m_result->resources[chopped_id]->metadata(); auto metadata = m_result->resources[choppedId]->metadata();
if (metadata) { if (metadata) {
resource->setMetadata(*metadata); resource->setMetadata(*metadata);
m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::INSTALLED); m_result->resources[resource->internalId()]->setStatus(ResourceStatus::Installed);
m_result->resources.remove(chopped_id); m_result->resources.remove(choppedId);
} }
} else { } else {
m_result->resources[resource->internal_id()].reset(resource); m_result->resources[resource->internalId()].reset(resource);
m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::NO_METADATA); m_result->resources[resource->internalId()]->setStatus(ResourceStatus::NoMetadata);
} }
} }
} }
@ -116,38 +118,41 @@ void ResourceFolderLoadTask::executeTask()
QMutableMapIterator iter(m_result->resources); QMutableMapIterator iter(m_result->resources);
while (iter.hasNext()) { while (iter.hasNext()) {
auto resource = iter.next().value(); auto resource = iter.next().value();
if (resource->status() == ResourceStatus::NOT_INSTALLED) { if (resource->status() == ResourceStatus::NotInstalled) {
resource->destroy(m_index_dir, false, false); resource->destroy(m_index_dir, false, false);
iter.remove(); iter.remove();
} }
} }
} }
for (auto mod : m_result->resources) for (const auto& mod : m_result->resources) {
mod->moveToThread(m_thread_to_spawn_into); mod->moveToThread(m_thread_to_spawn_into);
}
if (m_aborted) if (m_aborted) {
emit finished(); emit finished();
else } else {
emitSucceeded(); emitSucceeded();
} }
}
void ResourceFolderLoadTask::getFromMetadata() void ResourceFolderLoadTask::getFromMetadata()
{ {
m_index_dir.refresh(); m_index_dir.refresh();
for (auto entry : m_index_dir.entryList(QDir::Files)) { for (const auto& entry : m_index_dir.entryList(QDir::Files)) {
if (!entry.endsWith(".pw.toml")) { if (!entry.endsWith(".pw.toml")) {
continue; continue;
} }
auto metadata = Metadata::get(m_index_dir, entry); auto metadata = Metadata::get(m_index_dir, entry);
if (!metadata.isValid()) if (!metadata.isValid()) {
continue; continue;
}
auto* resource = m_create_func(QFileInfo(m_resource_dir.filePath(metadata.filename))); auto* resource = m_create_func(QFileInfo(m_resource_dir.filePath(metadata.filename)));
resource->setMetadata(metadata); resource->setMetadata(metadata);
resource->setStatus(ResourceStatus::NOT_INSTALLED); resource->setStatus(ResourceStatus::NotInstalled);
m_result->resources[resource->internal_id()].reset(resource); m_result->resources[resource->internalId()].reset(resource);
} }
} }

View file

@ -41,7 +41,7 @@
#include <QObject> #include <QObject>
#include <QRunnable> #include <QRunnable>
#include <memory> #include <memory>
#include "minecraft/mod/Mod.h" #include "minecraft/mod/Resource.h"
#include "tasks/Task.h" #include "tasks/Task.h"
class ResourceFolderLoadTask : public Task { class ResourceFolderLoadTask : public Task {
@ -54,11 +54,11 @@ class ResourceFolderLoadTask : public Task {
ResultPtr result() const { return m_result; } ResultPtr result() const { return m_result; }
public: public:
ResourceFolderLoadTask(const QDir& resource_dir, ResourceFolderLoadTask(const QDir& resourceDir,
const QDir& index_dir, const QDir& indexDir,
bool is_indexed, bool isIndexed,
bool clean_orphan, bool cleanOrphan,
std::function<Resource*(const QFileInfo&)> create_function); std::function<Resource*(const QFileInfo&)> createFunction);
bool canAbort() const override { return true; } bool canAbort() const override { return true; }
bool abort() override bool abort() override
@ -76,7 +76,7 @@ class ResourceFolderLoadTask : public Task {
QDir m_resource_dir, m_index_dir; QDir m_resource_dir, m_index_dir;
bool m_is_indexed; bool m_is_indexed;
bool m_clean_orphan; bool m_clean_orphan;
std::function<Resource*(QFileInfo const&)> m_create_func; std::function<Resource*(const QFileInfo&)> m_create_func;
ResultPtr m_result; ResultPtr m_result;
std::atomic<bool> m_aborted = false; std::atomic<bool> m_aborted = false;

View file

@ -99,7 +99,7 @@ void EnsureMetadataTask::executeTask()
} }
// They already have the right metadata :o // They already have the right metadata :o
if (resource->status() != ResourceStatus::NO_METADATA && resource->metadata() && resource->metadata()->provider == m_provider) { if (resource->status() != ResourceStatus::NoMetadata && resource->metadata() && resource->metadata()->provider == m_provider) {
qDebug() << "Resource" << resource->name() << "already has metadata!"; qDebug() << "Resource" << resource->name() << "already has metadata!";
emitReady(resource); emitReady(resource);
continue; continue;
@ -263,7 +263,7 @@ Task::Ptr EnsureMetadataTask::modrinthVersionsTask()
Task::Ptr EnsureMetadataTask::modrinthProjectsTask() Task::Ptr EnsureMetadataTask::modrinthProjectsTask()
{ {
QHash<QString, QString> addonIds; QHash<QString, QString> addonIds;
for (auto const& data : m_tempVersions) for (const auto& data : m_tempVersions)
addonIds.insert(data.addonId.toString(), data.hash); addonIds.insert(data.addonId.toString(), data.hash);
Task::Ptr proj_task; Task::Ptr proj_task;
@ -404,7 +404,7 @@ Task::Ptr EnsureMetadataTask::flameVersionsTask()
Task::Ptr EnsureMetadataTask::flameProjectsTask() Task::Ptr EnsureMetadataTask::flameProjectsTask()
{ {
QHash<QString, QString> addonIds; QHash<QString, QString> addonIds;
for (auto const& hash : m_resources.keys()) { for (const auto& hash : m_resources.keys()) {
if (m_tempVersions.contains(hash)) { if (m_tempVersions.contains(hash)) {
auto data = m_tempVersions.find(hash).value(); auto data = m_tempVersions.find(hash).value();

View file

@ -111,10 +111,10 @@ void FlameCheckUpdate::getLatestVersionCallback(Resource* resource, QByteArray*
} }
if (!latestVer->hash.isEmpty() && if (!latestVer->hash.isEmpty() &&
(resource->metadata()->hash != latestVer->hash || resource->status() == ResourceStatus::NOT_INSTALLED)) { (resource->metadata()->hash != latestVer->hash || resource->status() == ResourceStatus::NotInstalled)) {
auto oldVersion = resource->metadata()->version_number; auto oldVersion = resource->metadata()->version_number;
if (oldVersion.isEmpty()) { if (oldVersion.isEmpty()) {
if (resource->status() == ResourceStatus::NOT_INSTALLED) { if (resource->status() == ResourceStatus::NotInstalled) {
oldVersion = tr("Not installed"); oldVersion = tr("Not installed");
} else { } else {
oldVersion = tr("Unknown"); oldVersion = tr("Unknown");

View file

@ -178,12 +178,12 @@ void ModrinthCheckUpdate::checkVersionsResponse(QByteArray* response, std::optio
pack->slug = resource->metadata()->slug; pack->slug = resource->metadata()->slug;
pack->addonId = resource->metadata()->project_id; pack->addonId = resource->metadata()->project_id;
pack->provider = ModPlatform::ResourceProvider::MODRINTH; pack->provider = ModPlatform::ResourceProvider::MODRINTH;
if ((projectVer.hash != hash && projectVer.is_preferred) || (resource->status() == ResourceStatus::NOT_INSTALLED)) { if ((projectVer.hash != hash && projectVer.is_preferred) || (resource->status() == ResourceStatus::NotInstalled)) {
auto downloadTask = makeShared<ResourceDownloadTask>(pack, projectVer, m_resourceModel, true, "update"); auto downloadTask = makeShared<ResourceDownloadTask>(pack, projectVer, m_resourceModel, true, "update");
QString oldVersion = resource->metadata()->version_number; QString oldVersion = resource->metadata()->version_number;
if (oldVersion.isEmpty()) { if (oldVersion.isEmpty()) {
if (resource->status() == ResourceStatus::NOT_INSTALLED) { if (resource->status() == ResourceStatus::NotInstalled) {
oldVersion = tr("Not installed"); oldVersion = tr("Not installed");
} else { } else {
oldVersion = tr("Unknown"); oldVersion = tr("Unknown");

View file

@ -306,7 +306,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool
// ask the user on what provider to seach for the mod first // ask the user on what provider to seach for the mod first
for (auto* candidate : m_candidates) { for (auto* candidate : m_candidates) {
if (candidate->status() != ResourceStatus::NO_METADATA) { if (candidate->status() != ResourceStatus::NoMetadata) {
onMetadataEnsured(candidate); onMetadataEnsured(candidate);
continue; continue;
} }
@ -321,7 +321,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool
if (confirmRest) { if (confirmRest) {
addToTmp(candidate, providerRest); addToTmp(candidate, providerRest);
shouldTryOthers.insert(candidate->internal_id(), tryOthersRest); shouldTryOthers.insert(candidate->internalId(), tryOthersRest);
continue; continue;
} }
@ -343,7 +343,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool
tryOthersRest = response.try_others; tryOthersRest = response.try_others;
} }
shouldTryOthers.insert(candidate->internal_id(), response.try_others); shouldTryOthers.insert(candidate->internalId(), response.try_others);
if (confirmed) { if (confirmed) {
addToTmp(candidate, response.chosen); addToTmp(candidate, response.chosen);
@ -355,7 +355,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool
auto modrinthTask = makeShared<EnsureMetadataTask>(modrinthTmp, indexDir2, ModPlatform::ResourceProvider::MODRINTH); auto modrinthTask = makeShared<EnsureMetadataTask>(modrinthTmp, indexDir2, ModPlatform::ResourceProvider::MODRINTH);
connect(modrinthTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(candidate); }); connect(modrinthTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(candidate); });
connect(modrinthTask.get(), &EnsureMetadataTask::metadataFailed, [this, &shouldTryOthers](Resource* candidate) { connect(modrinthTask.get(), &EnsureMetadataTask::metadataFailed, [this, &shouldTryOthers](Resource* candidate) {
onMetadataFailed(candidate, shouldTryOthers.find(candidate->internal_id()).value(), ModPlatform::ResourceProvider::MODRINTH); onMetadataFailed(candidate, shouldTryOthers.find(candidate->internalId()).value(), ModPlatform::ResourceProvider::MODRINTH);
}); });
connect(modrinthTask.get(), &EnsureMetadataTask::failed, connect(modrinthTask.get(), &EnsureMetadataTask::failed,
[this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); }); [this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); });
@ -372,7 +372,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool
auto flameTask = makeShared<EnsureMetadataTask>(flameTmp, indexDir2, ModPlatform::ResourceProvider::FLAME); auto flameTask = makeShared<EnsureMetadataTask>(flameTmp, indexDir2, ModPlatform::ResourceProvider::FLAME);
connect(flameTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(candidate); }); connect(flameTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(candidate); });
connect(flameTask.get(), &EnsureMetadataTask::metadataFailed, [this, &shouldTryOthers](Resource* candidate) { connect(flameTask.get(), &EnsureMetadataTask::metadataFailed, [this, &shouldTryOthers](Resource* candidate) {
onMetadataFailed(candidate, shouldTryOthers.find(candidate->internal_id()).value(), ModPlatform::ResourceProvider::FLAME); onMetadataFailed(candidate, shouldTryOthers.find(candidate->internalId()).value(), ModPlatform::ResourceProvider::FLAME);
}); });
connect(flameTask.get(), &EnsureMetadataTask::failed, connect(flameTask.get(), &EnsureMetadataTask::failed,
[this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); }); [this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); });

View file

@ -87,7 +87,7 @@ void InfoFrame::updateWithMod(const Mod& m)
QString name = ""; QString name = "";
QString link = m.homepage(); QString link = m.homepage();
if (m.name().isEmpty()) if (m.name().isEmpty())
name = m.internal_id(); name = m.internalId();
else else
name = renderColorCodes(m.name()); name = renderColorCodes(m.name());

View file

@ -217,8 +217,8 @@ class ResourceFolderModelTest : public QObject {
auto& res_1 = model.at(0).type() != ResourceType::FOLDER ? model.at(0) : model.at(1); auto& res_1 = model.at(0).type() != ResourceType::FOLDER ? model.at(0) : model.at(1);
auto& res_2 = model.at(0).type() == ResourceType::FOLDER ? model.at(0) : model.at(1); auto& res_2 = model.at(0).type() == ResourceType::FOLDER ? model.at(0) : model.at(1);
auto id_1 = res_1.internal_id(); auto id_1 = res_1.internalId();
auto id_2 = res_2.internal_id(); auto id_2 = res_2.internalId();
bool initial_enabled_res_2 = res_2.enabled(); bool initial_enabled_res_2 = res_2.enabled();
bool initial_enabled_res_1 = res_1.enabled(); bool initial_enabled_res_1 = res_1.enabled();
@ -236,12 +236,12 @@ class ResourceFolderModelTest : public QObject {
qDebug() << "res_1 got successfully toggled again."; qDebug() << "res_1 got successfully toggled again.";
QVERIFY(res_1.enabled() == initial_enabled_res_1); QVERIFY(res_1.enabled() == initial_enabled_res_1);
QVERIFY(res_1.internal_id() == id_1); QVERIFY(res_1.internalId() == id_1);
qDebug() << "res_1 got back to its initial state."; qDebug() << "res_1 got back to its initial state.";
QVERIFY(!res_2.enable(initial_enabled_res_2 ? EnableAction::ENABLE : EnableAction::DISABLE)); QVERIFY(!res_2.enable(initial_enabled_res_2 ? EnableAction::ENABLE : EnableAction::DISABLE));
QVERIFY(res_2.enabled() == initial_enabled_res_2); QVERIFY(res_2.enabled() == initial_enabled_res_2);
QVERIFY(res_2.internal_id() == id_2); QVERIFY(res_2.internalId() == id_2);
} }
}; };