diff --git a/launcher/minecraft/mod/DataPack.cpp b/launcher/minecraft/mod/DataPack.cpp index 140596a29..c2b477d45 100644 --- a/launcher/minecraft/mod/DataPack.cpp +++ b/launcher/minecraft/mod/DataPack.cpp @@ -216,7 +216,7 @@ static std::pair map(int format, const QMap(other); - if (type == SortType::PACK_FORMAT) { + if (type == SortType::PackFormat) { auto this_ver = packFormat(); auto other_ver = cast_other.packFormat(); diff --git a/launcher/minecraft/mod/DataPackFolderModel.cpp b/launcher/minecraft/mod/DataPackFolderModel.cpp index 446e648d5..5f34ce6e1 100644 --- a/launcher/minecraft/mod/DataPackFolderModel.cpp +++ b/launcher/minecraft/mod/DataPackFolderModel.cpp @@ -40,26 +40,25 @@ #include #include -#include "Version.h" - #include "minecraft/mod/tasks/LocalDataPackParseTask.h" -DataPackFolderModel::DataPackFolderModel(const QString& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) - : ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) +DataPackFolderModel::DataPackFolderModel(const QString& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent) + : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent) { - m_column_names = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "File Name" }); - m_column_names_translated = + m_columnNames = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "File Name" }); + m_columnNamesTranslated = 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_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, - QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; + m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::PackFormat, SortType::Date, SortType::Filename }; + m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, + QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; m_columnsHideable = { false, true, false, true, true, true }; } QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const { - if (!validateIndex(index)) + if (!validateIndex(index)) { return {}; + } int row = index.row(); int column = index.column(); @@ -68,11 +67,9 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const case Qt::BackgroundRole: return rowBackground(row); case Qt::DisplayRole: - switch (column) { - case PackFormatColumn: { - const auto& resource = at(row); - return resource.packFormatStr(); - } + if (column == PackFormatColumn) { + const auto& resource = at(row); + return resource.packFormatStr(); } break; case Qt::DecorationRole: { @@ -93,6 +90,8 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const return QSize(32, 32); } break; + default: + break; } // map the columns to the base equivilents @@ -113,7 +112,9 @@ QVariant DataPackFolderModel::data(const QModelIndex& index, int role) const case FileNameColumn: mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); 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()) { @@ -167,7 +168,7 @@ QVariant DataPackFolderModel::headerData(int section, [[maybe_unused]] Qt::Orien int DataPackFolderModel::columnCount(const QModelIndex& parent) const { - return parent.isValid() ? 0 : NUM_COLUMNS; + return parent.isValid() ? 0 : NumColumns; } Resource* DataPackFolderModel::createResource(const QFileInfo& file) @@ -177,5 +178,5 @@ Resource* DataPackFolderModel::createResource(const QFileInfo& file) Task* DataPackFolderModel::createParseTask(Resource& resource) { - return new LocalDataPackParseTask(m_next_resolution_ticket, static_cast(&resource)); + return new LocalDataPackParseTask(m_nextResolutionTicket, static_cast(&resource)); } diff --git a/launcher/minecraft/mod/DataPackFolderModel.h b/launcher/minecraft/mod/DataPackFolderModel.h index 3eae4cf84..04153b893 100644 --- a/launcher/minecraft/mod/DataPackFolderModel.h +++ b/launcher/minecraft/mod/DataPackFolderModel.h @@ -39,16 +39,15 @@ #include "ResourceFolderModel.h" #include "DataPack.h" -#include "ResourcePack.h" class DataPackFolderModel : public ResourceFolderModel { Q_OBJECT 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; @@ -56,7 +55,7 @@ class DataPackFolderModel : public ResourceFolderModel { int columnCount(const QModelIndex& parent) const override; [[nodiscard]] Resource* createResource(const QFileInfo& file) override; - [[nodiscard]] Task* createParseTask(Resource&) override; + [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override; RESOURCE_HELPERS(DataPack) }; diff --git a/launcher/minecraft/mod/Mod.cpp b/launcher/minecraft/mod/Mod.cpp index 661192d67..d9e9e901a 100644 --- a/launcher/minecraft/mod/Mod.cpp +++ b/launcher/minecraft/mod/Mod.cpp @@ -61,18 +61,18 @@ void Mod::setDetails(const ModDetails& details) int Mod::compare(const Resource& other, SortType type) const { - auto cast_other = dynamic_cast(&other); + auto cast_other = dynamic_cast(&other); if (!cast_other) return Resource::compare(other, type); switch (type) { default: - case SortType::ENABLED: - case SortType::NAME: - case SortType::DATE: - case SortType::SIZE: + case SortType::Enabled: + case SortType::Name: + case SortType::Date: + case SortType::Size: return Resource::compare(other, type); - case SortType::VERSION: { + case SortType::Version: { auto this_ver = Version(version()); auto other_ver = Version(cast_other->version()); if (this_ver > other_ver) @@ -81,38 +81,38 @@ int Mod::compare(const Resource& other, SortType type) const return -1; break; } - case SortType::SIDE: { + case SortType::Side: { auto compare_result = QString::compare(side(), cast_other->side(), Qt::CaseInsensitive); if (compare_result != 0) return compare_result; break; } - case SortType::MC_VERSIONS: { + case SortType::McVersions: { auto compare_result = QString::compare(mcVersions(), cast_other->mcVersions(), Qt::CaseInsensitive); if (compare_result != 0) return compare_result; break; } - case SortType::LOADERS: { + case SortType::Loaders: { auto compare_result = QString::compare(loaders(), cast_other->loaders(), Qt::CaseInsensitive); if (compare_result != 0) return compare_result; break; } - case SortType::RELEASE_TYPE: { + case SortType::ReleaseType: { auto compare_result = QString::compare(releaseType(), cast_other->releaseType(), Qt::CaseInsensitive); if (compare_result != 0) return compare_result; break; } - case SortType::REQUIRED_BY: { + case SortType::RequiredBy: { if (requiredByCount() > cast_other->requiredByCount()) return 1; if (requiredByCount() < cast_other->requiredByCount()) return -1; break; } - case SortType::REQUIRES: { + case SortType::Requires: { if (requiresCount() > cast_other->requiresCount()) return 1; if (requiresCount() < cast_other->requiresCount()) diff --git a/launcher/minecraft/mod/ModFolderModel.cpp b/launcher/minecraft/mod/ModFolderModel.cpp index befd3c9dd..b5e5b2e87 100644 --- a/launcher/minecraft/mod/ModFolderModel.cpp +++ b/launcher/minecraft/mod/ModFolderModel.cpp @@ -52,28 +52,27 @@ #include #include -#include "minecraft/Component.h" #include "minecraft/mod/Resource.h" #include "minecraft/mod/ResourceFolderModel.h" #include "minecraft/mod/tasks/LocalModParseTask.h" #include "modplatform/ModIndex.h" #include "ui/dialogs/CustomMessageBox.h" -ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) - : ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) +ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent) + : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent) { - m_column_names = QStringList({ "Enable", "Image", "Name", "Version", "Last Modified", "Provider", "Size", "Side", "Loaders", - "Minecraft Versions", "Release Type", "Requires", "Required By", "File Name" }); - m_column_names_translated = + m_columnNames = QStringList({ "Enable", "Image", "Name", "Version", "Last Modified", "Provider", "Size", "Side", "Loaders", + "Minecraft Versions", "Release Type", "Requires", "Required By", "File Name" }); + m_columnNamesTranslated = 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") }); - m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::VERSION, SortType::DATE, - SortType::PROVIDER, SortType::SIZE, SortType::SIDE, SortType::LOADERS, SortType::MC_VERSIONS, - SortType::RELEASE_TYPE, SortType::REQUIRES, SortType::REQUIRED_BY, SortType::FILENAME }; - m_column_resize_modes = { 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 }; + tr("Loaders"), tr("Minecraft Versions"), tr("Release Type"), tr("Requires"), tr("Required By"), tr("File Name") }); + m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::Version, SortType::Date, + SortType::Provider, SortType::Size, SortType::Side, SortType::Loaders, SortType::McVersions, + SortType::ReleaseType, SortType::Requires, SortType::RequiredBy, SortType::Filename }; + 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 }; m_columnsHideable = { false, true, false, true, true, true, true, true, true, true, true, true, true, true }; connect(this, &ModFolderModel::parseFinished, this, &ModFolderModel::onParseFinished); @@ -81,8 +80,9 @@ ModFolderModel::ModFolderModel(const QDir& dir, BaseInstance* instance, bool is_ QVariant ModFolderModel::data(const QModelIndex& index, int role) const { - if (!validateIndex(index)) + if (!validateIndex(index)) { return {}; + } int row = index.row(); int column = index.column(); @@ -120,6 +120,8 @@ QVariant ModFolderModel::data(const QModelIndex& index, int role) const case RequiresColumn: { return at(row).requiresCount(); } + default: + break; } break; case Qt::DecorationRole: { @@ -158,6 +160,8 @@ QVariant ModFolderModel::data(const QModelIndex& index, int role) const case FileNameColumn: mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); break; + default: + break; } if (mappedIndex.isValid()) { @@ -229,12 +233,12 @@ QVariant ModFolderModel::headerData(int section, [[maybe_unused]] Qt::Orientatio int ModFolderModel::columnCount(const QModelIndex& parent) const { - return parent.isValid() ? 0 : NUM_COLUMNS; + return parent.isValid() ? 0 : NumColumns; } 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() @@ -242,35 +246,37 @@ bool ModFolderModel::isValid() 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); - if (iter == m_active_parse_tasks.constEnd()) + auto iter = m_activeParseTasks.constFind(ticket); + if (iter == m_activeParseTasks.constEnd()) { return; + } - int row = m_resources_index[mod_id]; + int row = m_resourcesIndex[resourceId]; - auto parse_task = *iter; - auto cast_task = static_cast(parse_task.get()); + const auto& parseTask = *iter; + auto* castTask = static_cast(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) { auto* mod = static_cast(resource.get()); mod->finishResolvingWithDetails(std::move(result->details)); - } emit dataChanged(index(row, RequiresColumn), index(row, RequiredByColumn)); } -Mod* findById(QSet mods, QString modId) +namespace { +Mod* findById(QSet 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; } +} // namespace void ModFolderModel::onParseFinished() { @@ -283,25 +289,25 @@ void ModFolderModel::onParseFinished() m_requires.clear(); m_requiredBy.clear(); - auto findByProjectID = [mods](QVariant modId, ModPlatform::ResourceProvider provider) -> Mod* { - auto found = std::find_if(mods.begin(), mods.end(), [modId, provider](Mod* m) { + auto findByProjectID = [mods](const QVariant& modId, ModPlatform::ResourceProvider provider) -> Mod* { + auto found = std::ranges::find_if(mods, [modId, provider](Mod* m) { return m->metadata() && m->metadata()->provider == provider && m->metadata()->project_id == modId; }); return found != mods.end() ? *found : nullptr; }; - for (auto mod : mods) { + for (auto* mod : mods) { auto id = mod->mod_id(); - for (auto dep : mod->dependencies()) { - auto d = findById(mods, dep); + for (const auto& dep : mod->dependencies()) { + auto* d = findById(mods, dep); if (d) { m_requires[id] << d; m_requiredBy[d->mod_id()] << mod; } } if (mod->metadata()) { - for (auto dep : mod->metadata()->dependencies) { + for (const auto& dep : mod->metadata()->dependencies) { if (dep.type == ModPlatform::DependencyType::REQUIRED) { - auto d = findByProjectID(dep.addonId, mod->metadata()->provider); + auto* d = findByProjectID(dep.addonId, mod->metadata()->provider); if (d) { m_requires[id] << d; 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(); if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) { mod->setRequiredByCount(m_requiredBy[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)); } } } -QSet collectMods(QSet mods, QHash> relation, std::set& seen, bool shouldBeEnabled) +namespace { + +QSet collectMods(const QSet& mods, QHash> relation, std::set& seen, bool shouldBeEnabled) { QSet affectedList = {}; QSet needToCheck = {}; - for (auto mod : mods) { + for (auto* mod : mods) { auto id = mod->mod_id(); - if (seen.count(id) == 0) { + if (!seen.contains(id)) { seen.insert(id); - for (auto affected : relation[id]) { + for (auto* affected : relation[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); if (shouldBeEnabled != affected->enabled()) { affectedList << affected; @@ -348,11 +356,13 @@ QSet collectMods(QSet mods, QHash> relation, std } return affectedList; } +} // namespace QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, EnableAction action) { - if (indexes.isEmpty()) + if (indexes.isEmpty()) { return {}; + } QModelIndexList affectedList = {}; auto affectedModsList = selectedMods(indexes); @@ -372,9 +382,9 @@ QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, return {}; // this function should not be called with TOGGLE } } - for (auto affected : affectedMods) { + for (auto* affected : affectedMods) { auto affectedId = affected->mod_id(); - auto row = m_resources_index[affected->internal_id()]; + auto row = m_resourcesIndex[affected->internalId()]; affectedList << index(row, 0); } return affectedList; @@ -382,8 +392,9 @@ QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAction action) { - if (indexes.isEmpty()) + if (indexes.isEmpty()) { return {}; + } auto indexedModsList = selectedMods(indexes); auto indexedMods = QSet(indexedModsList.begin(), indexedModsList.end()); @@ -402,7 +413,7 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc break; } case EnableAction::TOGGLE: { - for (auto mod : indexedMods) { + for (auto* mod : indexedMods) { if (mod->enabled()) { toDisable << mod; } else { @@ -417,10 +428,10 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc auto requiredToDisable = collectMods(toDisable, m_requiredBy, seen, false); toDisable.removeIf([toEnable](Mod* m) { return toEnable.contains(m); }); - auto toList = [this](QSet mods) { + auto toList = [this](const QSet& mods) { QModelIndexList list; - for (auto mod : mods) { - auto row = m_resources_index[mod->internal_id()]; + for (auto* mod : mods) { + auto row = m_resourcesIndex[mod->internalId()]; list << index(row, 0); } return list; @@ -453,8 +464,8 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc yesButton = tr("Disable Required"); } - auto box = CustomMessageBox::selectable(nullptr, title, message, QMessageBox::Warning, - QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No); + auto* box = CustomMessageBox::selectable(nullptr, title, message, QMessageBox::Warning, + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No); box->button(QMessageBox::No)->setText(noButton); box->button(QMessageBox::Yes)->setText(yesButton); auto response = box->exec(); @@ -472,21 +483,23 @@ bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAc return disableStatus && enableStatus; } -QStringList reqToList(QSet l) +namespace { +QStringList reqToList(const QSet& l) { QStringList req; - for (auto m : l) { + for (auto* m : l) { req << m->name(); } return req; } +} // namespace -QStringList ModFolderModel::requiresList(QString id) +QStringList ModFolderModel::requiresList(const QString& id) { return reqToList(m_requires[id]); } -QStringList ModFolderModel::requiredByList(QString id) +QStringList ModFolderModel::requiredByList(const QString& id) { return reqToList(m_requiredBy[id]); } @@ -495,7 +508,7 @@ bool ModFolderModel::deleteResources(const QModelIndexList& indexes) { auto deleteInvalid = [](QSet& mods) { for (auto it = mods.begin(); it != mods.end();) { - auto mod = *it; + auto* mod = *it; // the QFileInfo::exists is used instead of mod->fileinfo().exists // because the later somehow caches that the file exists if (!mod || !QFileInfo::exists(mod->fileinfo().absoluteFilePath())) { @@ -506,14 +519,14 @@ bool ModFolderModel::deleteResources(const QModelIndexList& indexes) } }; auto rsp = ResourceFolderModel::deleteResources(indexes); - for (auto mod : allMods()) { + for (auto* mod : allMods()) { auto id = mod->mod_id(); deleteInvalid(m_requiredBy[id]); deleteInvalid(m_requires[id]); if (mod->requiredByCount() != m_requiredBy[id].count() || mod->requiresCount() != m_requires[id].count()) { mod->setRequiredByCount(m_requiredBy[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)); } } diff --git a/launcher/minecraft/mod/ModFolderModel.h b/launcher/minecraft/mod/ModFolderModel.h index fb1add9e0..a5c6ba483 100644 --- a/launcher/minecraft/mod/ModFolderModel.h +++ b/launcher/minecraft/mod/ModFolderModel.h @@ -46,7 +46,6 @@ #include "Mod.h" #include "ResourceFolderModel.h" -#include "minecraft/Component.h" #include "minecraft/mod/Resource.h" class BaseInstance; @@ -59,7 +58,7 @@ class QFileSystemWatcher; class ModFolderModel : public ResourceFolderModel { Q_OBJECT public: - enum Columns { + enum Columns : std::uint8_t { ActiveColumn = 0, ImageColumn, NameColumn, @@ -74,11 +73,11 @@ class ModFolderModel : public ResourceFolderModel { RequiresColumn, RequiredByColumn, 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; @@ -86,7 +85,7 @@ class ModFolderModel : public ResourceFolderModel { int columnCount(const QModelIndex& parent) const override; [[nodiscard]] Resource* createResource(const QFileInfo& file) override { return new Mod(file); } - [[nodiscard]] Task* createParseTask(Resource&) override; + [[nodiscard]] Task* createParseTask(Resource& /*unused*/) override; bool isValid(); @@ -98,11 +97,11 @@ class ModFolderModel : public ResourceFolderModel { RESOURCE_HELPERS(Mod) public: - QStringList requiresList(QString id); - QStringList requiredByList(QString id); + QStringList requiresList(const QString& id); + QStringList requiredByList(const QString& id); private slots: - void onParseSucceeded(int ticket, QString resource_id) override; + void onParseSucceeded(int ticket, const QString& resourceId) override; void onParseFinished(); private: diff --git a/launcher/minecraft/mod/Resource.cpp b/launcher/minecraft/mod/Resource.cpp index 196ea75e6..06d9a0af7 100644 --- a/launcher/minecraft/mod/Resource.cpp +++ b/launcher/minecraft/mod/Resource.cpp @@ -4,71 +4,75 @@ #include #include #include +#include #include "FileSystem.h" #include "StringUtils.h" #include "minecraft/MinecraftInstance.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(); } -static std::tuple calculateFileSize(const QFileInfo& file) +namespace { +std::tuple calculateFileSize(const QFileInfo& file) { if (file.isDir()) { auto dir = QDir(file.absoluteFilePath()); dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); auto count = dir.count(); auto str = QObject::tr("item"); - if (count != 1) + if (count != 1) { str = QObject::tr("items"); + } return { QString("%1 %2").arg(QString::number(count), str), count }; } return { StringUtils::humanReadableFileSize(file.size(), true), file.size() }; } +} // namespace void Resource::parseFile() { - QString file_name{ m_file_info.fileName() }; + QString fileName{ m_file_info.fileName() }; 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); if (m_file_info.isDir()) { m_type = ResourceType::FOLDER; - m_name = file_name; + m_name = fileName; } else if (m_file_info.isFile()) { - if (file_name.endsWith(".disabled")) { - file_name.chop(9); + if (fileName.endsWith(".disabled")) { + fileName.chop(9); m_enabled = false; } - if (file_name.endsWith(".zip") || file_name.endsWith(".jar")) { + if (fileName.endsWith(".zip") || fileName.endsWith(".jar")) { m_type = ResourceType::ZIPFILE; - file_name.chop(4); - } else if (file_name.endsWith(".nilmod")) { + fileName.chop(4); + } else if (fileName.endsWith(".nilmod")) { m_type = ResourceType::ZIPFILE; - file_name.chop(7); - } else if (file_name.endsWith(".litemod")) { + fileName.chop(7); + } else if (fileName.endsWith(".litemod")) { m_type = ResourceType::LITEMOD; - file_name.chop(8); + fileName.chop(8); } else { m_type = ResourceType::SINGLEFILE; } - m_name = file_name; + m_name = fileName; } m_changed_date_time = m_file_info.lastModified(); @@ -76,39 +80,45 @@ void Resource::parseFile() auto Resource::name() const -> QString { - if (metadata()) + if (metadata()) { return metadata()->name; + } return m_name; } -static void removeThePrefix(QString& string) +namespace { +void removeThePrefix(QString& string) { static const QRegularExpression s_regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption); string.remove(s_regex); string = string.trimmed(); } +} // namespace auto Resource::provider() const -> QString { - if (metadata()) + if (metadata()) { return ModPlatform::ProviderCapabilities::readableName(metadata()->provider); + } return tr("Unknown"); } auto Resource::homepage() const -> QString { - if (metadata()) + if (metadata()) { return ModPlatform::getMetaURL(metadata()->provider, metadata()->project_id); + } return {}; } void Resource::setMetadata(std::shared_ptr&& metadata) { - if (status() == ResourceStatus::NO_METADATA) - setStatus(ResourceStatus::INSTALLED); + if (status() == ResourceStatus::NoMetadata) { + setStatus(ResourceStatus::Installed); + } m_metadata = metadata; } @@ -133,12 +143,12 @@ void Resource::updateIssues(const BaseInstance* inst) return; } - auto mcInst = dynamic_cast(inst); + const auto* mcInst = dynamic_cast(inst); if (mcInst == nullptr) { return; } - auto profile = mcInst->getPackProfile(); + auto* profile = mcInst->getPackProfile(); QString mcVersion = profile->getComponentVersion("net.minecraft"); 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) { default: - case SortType::ENABLED: - if (enabled() && !other.enabled()) + case SortType::Enabled: + if (enabled() && !other.enabled()) { return 1; - if (!enabled() && other.enabled()) + } + if (!enabled() && other.enabled()) { return -1; + } break; - case SortType::NAME: { - QString this_name{ name() }; - QString other_name{ other.name() }; + case SortType::Name: { + QString thisName{ name() }; + QString otherName{ other.name() }; // TODO do we need this? it could result in 0 being returned - removeThePrefix(this_name); - removeThePrefix(other_name); + removeThePrefix(thisName); + removeThePrefix(otherName); - return QString::compare(this_name, other_name, Qt::CaseInsensitive); + return QString::compare(thisName, otherName, Qt::CaseInsensitive); } - case SortType::DATE: - if (dateTimeChanged() > other.dateTimeChanged()) + case SortType::Date: + if (dateTimeChanged() > other.dateTimeChanged()) { return 1; - if (dateTimeChanged() < other.dateTimeChanged()) + } + if (dateTimeChanged() < other.dateTimeChanged()) { return -1; + } break; - case SortType::FILENAME: + case SortType::Filename: return fileinfo().fileName().localeAwareCompare(other.fileinfo().fileName()); - case SortType::SIZE: { + case SortType::Size: { if (this->type() != other.type()) { - if (this->type() == ResourceType::FOLDER) + if (this->type() == ResourceType::FOLDER) { return -1; - if (other.type() == ResourceType::FOLDER) + } + if (other.type() == ResourceType::FOLDER) { return 1; + } } - if (sizeInfo() > other.sizeInfo()) + if (sizeInfo() > other.sizeInfo()) { return 1; - if (sizeInfo() < other.sizeInfo()) + } + if (sizeInfo() < other.sizeInfo()) { return -1; + } break; } - case SortType::PROVIDER: { - auto compare_result = QString::compare(provider(), other.provider(), Qt::CaseInsensitive); - if (compare_result != 0) - return compare_result; + case SortType::Provider: { + auto compareResult = QString::compare(provider(), other.provider(), Qt::CaseInsensitive); + if (compareResult != 0) { + return compareResult; + } break; } } @@ -204,17 +223,20 @@ int Resource::compare(const Resource& other, SortType type) const bool Resource::applyFilter(QRegularExpression filter) const { - if (filter.match(name()).hasMatch()) + if (filter.match(name()).hasMatch()) { return true; - if (filter.match(fileinfo().fileName()).hasMatch()) + } + if (filter.match(fileinfo().fileName()).hasMatch()) { return true; + } return false; } 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; + } QString path = m_file_info.absoluteFilePath(); QFile file(path); @@ -233,14 +255,16 @@ bool Resource::enable(EnableAction action) break; } - if (m_enabled == enable) + if (m_enabled == enable) { return false; + } if (enable) { // m_enabled is false, but there's no '.disabled' suffix. // TODO: Report error? - if (!path.endsWith(".disabled")) + if (!path.endsWith(".disabled")) { return false; + } path.chop(9); } else { path += ".disabled"; @@ -248,8 +272,9 @@ bool Resource::enable(EnableAction action) path = FS::getUniqueResourceName(path); } } - if (!file.rename(path)) + if (!file.rename(path)) { return false; + } setFile(QFileInfo(path)); @@ -257,33 +282,34 @@ bool Resource::enable(EnableAction action) 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; - if (!preserve_metadata) { + if (!preserveMetadata) { 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()) { - Metadata::remove(index_dir, metadata()->slug); + Metadata::remove(indexDir, metadata()->slug); } else { auto n = name(); - Metadata::remove(index_dir, n); + Metadata::remove(indexDir, n); } m_metadata = nullptr; } bool Resource::isSymLinkUnder(const QString& instPath) const { - if (isSymLink()) + if (isSymLink()) { return true; + } auto instDir = QDir(instPath); @@ -301,8 +327,9 @@ bool Resource::isMoreThanOneHardLink() const auto Resource::getOriginalFileName() const -> QString { auto fileName = m_file_info.fileName(); - if (!m_enabled) + if (!m_enabled) { fileName.chop(9); + } return fileName; } @@ -332,16 +359,16 @@ QDebug operator<<(QDebug debug, ResourceType type) QDebug operator<<(QDebug debug, ResourceStatus status) { switch (status) { - case ResourceStatus::INSTALLED: + case ResourceStatus::Installed: debug << "INSTALLED"; break; - case ResourceStatus::NOT_INSTALLED: + case ResourceStatus::NotInstalled: debug << "NOT_INSTALLED"; break; - case ResourceStatus::NO_METADATA: + case ResourceStatus::NoMetadata: debug << "NO_METADATA"; break; - case ResourceStatus::UNKNOWN: + case ResourceStatus::Unknown: default: debug << "UNKNOWN"; break; diff --git a/launcher/minecraft/mod/Resource.h b/launcher/minecraft/mod/Resource.h index 8f30c1bf3..694d74ec0 100644 --- a/launcher/minecraft/mod/Resource.h +++ b/launcher/minecraft/mod/Resource.h @@ -45,7 +45,7 @@ class BaseInstance; -enum class ResourceType { +enum class ResourceType : std::uint8_t { UNKNOWN, //!< Indicates an unspecified resource type. ZIPFILE, //!< The resource is a zip file containing the resource's class files. SINGLEFILE, //!< The resource is a single file (not a zip file). @@ -55,33 +55,33 @@ enum class ResourceType { QDebug operator<<(QDebug debug, ResourceType type); -enum class ResourceStatus { - INSTALLED, // Both JAR and Metadata are present - NOT_INSTALLED, // Only the Metadata is present - NO_METADATA, // Only the JAR is present - UNKNOWN, // Default status +enum class ResourceStatus : std::uint8_t { + Installed, // Both JAR and Metadata are present + NotInstalled, // Only the Metadata is present + NoMetadata, // Only the JAR is present + Unknown, // Default status }; QDebug operator<<(QDebug debug, ResourceStatus status); -enum class SortType { - NAME, - DATE, - VERSION, - ENABLED, - PACK_FORMAT, - PROVIDER, - SIZE, - SIDE, - MC_VERSIONS, - LOADERS, - RELEASE_TYPE, - REQUIRES, - REQUIRED_BY, - FILENAME, +enum class SortType : std::uint8_t { + Name, + Date, + Version, + Enabled, + PackFormat, + Provider, + Size, + Side, + McVersions, + Loaders, + ReleaseType, + Requires, + RequiredBy, + 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 * for display and house-keeping purposes. @@ -93,20 +93,19 @@ class Resource : public QObject { Q_DISABLE_COPY(Resource) public: using Ptr = shared_qobject_ptr; - using WeakPtr = QPointer; Resource(QObject* parent = nullptr); - Resource(QFileInfo file_info); - Resource(QString file_path) : Resource(QFileInfo(file_path)) {} + Resource(QFileInfo fileInfo); + Resource(const QString& filePath) : Resource(QFileInfo(filePath)) {} ~Resource() override = default; - void setFile(QFileInfo file_info); + void setFile(QFileInfo fileInfo); void parseFile(); auto fileinfo() const -> QFileInfo { return m_file_info; } 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; } bool enabled() const { return m_enabled; } auto getOriginalFileName() const -> QString; @@ -139,7 +138,7 @@ class Resource : public QObject { * = 0: 'this' is equal to '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), * or if such filter includes the Resource (true). @@ -164,9 +163,9 @@ class Resource : public QObject { } // 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. - auto destroyMetadata(const QDir& index_dir) -> void; + auto destroyMetadata(const QDir& indexDir) -> void; auto isSymLink() const -> bool { return m_file_info.isSymLink(); } @@ -196,7 +195,7 @@ class Resource : public QObject { ResourceType m_type = ResourceType::UNKNOWN; /* Installation status of the resource. */ - ResourceStatus m_status = ResourceStatus::UNKNOWN; + ResourceStatus m_status = ResourceStatus::Unknown; std::shared_ptr m_metadata = nullptr; diff --git a/launcher/minecraft/mod/ResourceFolderModel.cpp b/launcher/minecraft/mod/ResourceFolderModel.cpp index 2e91cebc3..ddc132089 100644 --- a/launcher/minecraft/mod/ResourceFolderModel.cpp +++ b/launcher/minecraft/mod/ResourceFolderModel.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "Application.h" @@ -27,10 +28,10 @@ #include "tasks/Task.h" #include "ui/dialogs/CustomMessageBox.h" -ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) - : QAbstractListModel(parent), m_dir(dir), m_instance(instance), m_watcher(this), m_is_indexed(is_indexed) +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_isIndexed(isIndexed) { - if (create_dir) { + if (createDir) { FS::ensureFolderPathExists(m_dir.absolutePath()); } @@ -49,70 +50,75 @@ ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance ResourceFolderModel::~ResourceFolderModel() { - while (!QThreadPool::globalInstance()->waitForDone(100)) + while (!QThreadPool::globalInstance()->waitForDone(100)) { QCoreApplication::processEvents(); + } } bool ResourceFolderModel::startWatching(const QStringList& paths) { // Remove orphaned metadata next time - m_first_folder_load = true; + m_firstFolderLoad = true; - if (m_is_watching) + if (m_isWatching) { return false; + } - auto couldnt_be_watched = m_watcher.addPaths(paths); - for (auto path : paths) { - if (couldnt_be_watched.contains(path)) + auto couldntBeWatched = m_watcher.addPaths(paths); + for (const auto& path : paths) { + if (couldntBeWatched.contains(path)) { qDebug() << "Failed to start watching" << path; - else + } else { qDebug() << "Started watching" << path; + } } update(); - m_is_watching = !m_is_watching; - return m_is_watching; + m_isWatching = !m_isWatching; + return m_isWatching; } bool ResourceFolderModel::stopWatching(const QStringList& paths) { - if (!m_is_watching) + if (!m_isWatching) { return false; - - auto couldnt_be_stopped = m_watcher.removePaths(paths); - for (auto path : paths) { - if (couldnt_be_stopped.contains(path)) - qDebug() << "Failed to stop watching" << path; - else - qDebug() << "Stopped watching" << path; } - m_is_watching = !m_is_watching; - return !m_is_watching; + auto couldntBeStopped = m_watcher.removePaths(paths); + for (const auto& path : paths) { + if (couldntBeStopped.contains(path)) { + qDebug() << "Failed to stop watching" << path; + } else { + qDebug() << "Stopped watching" << path; + } + } + + m_isWatching = !m_isWatching; + return !m_isWatching; } -bool ResourceFolderModel::installResource(QString original_path) +bool ResourceFolderModel::installResource(QString originalPath) { // 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); - QFileInfo file_info(original_path); + originalPath = FS::NormalizePath(originalPath); + QFileInfo fileInfo(originalPath); - if (!file_info.exists() || !file_info.isReadable()) { - qWarning() << "Caught attempt to install non-existing file or file-like object:" << original_path; + if (!fileInfo.exists() || !fileInfo.isReadable()) { + qWarning() << "Caught attempt to install non-existing file or file-like object:" << originalPath; return false; } - qDebug() << "Installing:" << file_info.absoluteFilePath(); + qDebug() << "Installing:" << fileInfo.absoluteFilePath(); - Resource resource(file_info); + Resource resource(fileInfo); if (!resource.valid()) { - qWarning() << original_path << "is not a valid resource. Ignoring it."; + qWarning() << originalPath << "is not a valid resource. Ignoring it."; return false; } - auto new_path = FS::NormalizePath(m_dir.filePath(file_info.fileName())); - if (original_path == new_path) { - qWarning() << "Overwriting the mod (" << original_path << ") with itself makes no sense..."; + auto newPath = FS::NormalizePath(m_dir.filePath(fileInfo.fileName())); + if (originalPath == newPath) { + qWarning() << "Overwriting the mod (" << originalPath << ") with itself makes no sense..."; return false; } @@ -120,45 +126,47 @@ bool ResourceFolderModel::installResource(QString original_path) case ResourceType::SINGLEFILE: case ResourceType::ZIPFILE: case ResourceType::LITEMOD: { - if (QFile::exists(new_path) || QFile::exists(new_path + QString(".disabled"))) { - if (!FS::deletePath(new_path)) { - qCritical() << "Cleaning up new location (" << new_path << ") was unsuccessful!"; + if (QFile::exists(newPath) || QFile::exists(newPath + QString(".disabled"))) { + if (!FS::deletePath(newPath)) { + qCritical() << "Cleaning up new location (" << newPath << ") was unsuccessful!"; return false; } - qDebug() << new_path << "has been deleted."; + qDebug() << newPath << "has been deleted."; } - if (!QFile::copy(original_path, new_path)) { - qCritical() << "Copy from" << original_path << "to" << new_path << "has failed."; + if (!QFile::copy(originalPath, newPath)) { + qCritical() << "Copy from" << originalPath << "to" << newPath << "has failed."; return false; } - FS::updateTimestamp(new_path); + FS::updateTimestamp(newPath); - QFileInfo new_path_file_info(new_path); - resource.setFile(new_path_file_info); + QFileInfo newPathFileInfo(newPath); + resource.setFile(newPathFileInfo); - if (!m_is_watching) + if (!m_isWatching) { return update(); + } return true; } case ResourceType::FOLDER: { - if (QFile::exists(new_path)) { - qDebug() << "Ignoring folder '" << original_path << "', it would merge with" << new_path; + if (QFile::exists(newPath)) { + qDebug() << "Ignoring folder '" << originalPath << "', it would merge with" << newPath; return false; } - if (!FS::copy(original_path, new_path)()) { - qWarning() << "Copy of folder from" << original_path << "to" << new_path << "has (potentially partially) failed."; + if (!FS::copy(originalPath, newPath)()) { + qWarning() << "Copy of folder from" << originalPath << "to" << newPath << "has (potentially partially) failed."; return false; } - QFileInfo newpathInfo(new_path); + QFileInfo newpathInfo(newPath); resource.setFile(newpathInfo); - if (!m_is_watching) + if (!m_isWatching) { return update(); + } return true; } @@ -168,24 +176,24 @@ bool ResourceFolderModel::installResource(QString original_path) 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()) { ModPlatform::IndexedPack pack{ - vers.addonId, - ModPlatform::ResourceProvider::FLAME, + .addonId = vers.addonId, + .provider = ModPlatform::ResourceProvider::FLAME, }; auto [job, response] = FlameAPI().getProject(vers.addonId.toString()); connect(job.get(), &Task::failed, this, install); connect(job.get(), &Task::aborted, this, install); connect(job.get(), &Task::succeeded, [response, this, &vers, install, &pack] { - QJsonParseError parse_error{}; - QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); - if (parse_error.error != QJsonParseError::NoError) { - qWarning() << "Error while parsing JSON response for mod info at" << parse_error.offset - << "reason:" << parse_error.errorString(); + QJsonParseError parseError{}; + QJsonDocument doc = QJsonDocument::fromJson(*response, &parseError); + if (parseError.error != QJsonParseError::NoError) { + qWarning() << "Error while parsing JSON response for mod info at" << parseError.offset + << "reason:" << parseError.errorString(); qDebug() << *response; return; } @@ -196,9 +204,9 @@ void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlat qDebug() << doc; qWarning() << "Error while reading mod info:" << e.cause(); } - LocalResourceUpdateTask update_metadata(indexDir(), pack, vers); - connect(&update_metadata, &Task::finished, this, install); - update_metadata.start(); + LocalResourceUpdateTask updateMetadata(indexDir(), pack, vers); + connect(&updateMetadata, &Task::finished, this, install); + updateMetadata.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) { auto resourceFileInfo = resource->fileinfo(); @@ -216,8 +224,8 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese resourceFileName.chop(9); } - if (resourceFileName == file_name) { - auto res = resource->destroy(indexDir(), preserve_metadata, false); + if (resourceFileName == fileName) { + auto res = resource->destroy(indexDir(), preserveMetadata, false); update(); @@ -229,14 +237,16 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes) { - if (indexes.isEmpty()) + if (indexes.isEmpty()) { return true; + } for (auto i : indexes) { - if (i.column() != 0) + if (i.column() != 0) { continue; + } - auto& resource = m_resources.at(i.row()); + const auto& resource = m_resources.at(i.row()); resource->destroy(indexDir()); } @@ -247,14 +257,16 @@ bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes) void ResourceFolderModel::deleteMetadata(const QModelIndexList& indexes) { - if (indexes.isEmpty()) + if (indexes.isEmpty()) { return; + } for (auto i : indexes) { - if (i.column() != 0) + if (i.column() != 0) { continue; + } - auto& resource = m_resources.at(i.row()); + const auto& resource = m_resources.at(i.row()); resource->destroyMetadata(indexDir()); } @@ -271,33 +283,36 @@ bool ResourceFolderModel::setResourceEnabled(const QModelIndexList& indexes, Ena QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ->exec(); - if (response != QMessageBox::Yes) + if (response != QMessageBox::Yes) { return false; + } } - if (indexes.isEmpty()) + if (indexes.isEmpty()) { return true; + } bool succeeded = true; - for (auto const& idx : indexes) { - if (!validateIndex(idx) || idx.column() != 0) + for (const auto& idx : indexes) { + if (!validateIndex(idx) || idx.column() != 0) { continue; + } int row = idx.row(); auto& resource = m_resources[row]; // Preserve the row, but change its ID - auto old_id = resource->internal_id(); + auto oldId = resource->internalId(); if (!resource->enable(action)) { succeeded = false; continue; } - auto new_id = resource->internal_id(); + auto newId = resource->internalId(); - m_resources_index.remove(old_id); - m_resources_index[new_id] = row; + m_resourcesIndex.remove(oldId); + m_resourcesIndex[newId] = row; emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1)); } @@ -312,24 +327,25 @@ bool ResourceFolderModel::update() QMutexLocker lock(&s_update_task_mutex); // Already updating, so we schedule a future update and return. - if (m_current_update_task) { - m_scheduled_update = true; + if (m_currentUpdateTask) { + m_scheduledUpdate = true; return false; } - m_current_update_task.reset(createUpdateTask()); - if (!m_current_update_task) + m_currentUpdateTask.reset(createUpdateTask()); + if (!m_currentUpdateTask) { 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); - 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( - m_current_update_task.get(), &Task::finished, this, + m_currentUpdateTask.get(), &Task::finished, this, [this] { - m_current_update_task.reset(); - if (m_scheduled_update) { - m_scheduled_update = false; + m_currentUpdateTask.reset(); + if (m_scheduledUpdate) { + m_scheduledUpdate = false; update(); } else { emit updateFinished(); @@ -340,16 +356,16 @@ bool ResourceFolderModel::update() Task::Ptr preUpdate{ createPreUpdateTask() }; if (preUpdate != nullptr) { - auto task = new SequentialTask("ResourceFolderModel::update"); + auto* task = new SequentialTask("ResourceFolderModel::update"); task->addTask(preUpdate); - task->addTask(m_current_update_task); + task->addTask(m_currentUpdateTask); connect(task, &Task::finished, [task] { task->deleteLater(); }); QThreadPool::globalInstance()->start(task); } else { - QThreadPool::globalInstance()->start(m_current_update_task.get()); + QThreadPool::globalInstance()->start(m_currentUpdateTask.get()); } return true; @@ -362,24 +378,25 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res) } Task::Ptr task{ createParseTask(*res) }; - if (!task) + if (!task) { return; + } - int ticket = m_next_resolution_ticket.fetch_add(1); + int ticket = m_nextResolutionTicket.fetch_add(1); res->setResolving(true, ticket); - m_active_parse_tasks.insert(ticket, task); + m_activeParseTasks.insert(ticket, task); 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); 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); connect( task.get(), &Task::finished, this, [this, ticket] { - m_active_parse_tasks.remove(ticket); + m_activeParseTasks.remove(ticket); emit parseFinished(); }, Qt::ConnectionType::QueuedConnection); @@ -394,44 +411,45 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res) void ResourceFolderModel::onUpdateSucceeded() { - auto update_results = static_cast(m_current_update_task.get())->result(); + auto updateResults = static_cast(m_currentUpdateTask.get())->result(); - auto& new_resources = update_results->resources; + auto& newResources = updateResults->resources; - auto current_list = m_resources_index.keys(); - QSet current_set(current_list.begin(), current_list.end()); + auto currentList = m_resourcesIndex.keys(); + QSet currentSet(currentList.begin(), currentList.end()); - auto new_list = new_resources.keys(); - QSet new_set(new_list.begin(), new_list.end()); + auto newList = newResources.keys(); + QSet 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); - if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id)) + auto iter = m_activeParseTasks.constFind(ticket); + if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) { return; + } - int row = m_resources_index[resource_id]; + int row = m_resourcesIndex[resourceId]; emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1)); } Task* ResourceFolderModel::createUpdateTask() { - auto index_dir = indexDir(); - auto task = new ResourceFolderLoadTask(dir(), index_dir, m_is_indexed, m_first_folder_load, - [this](const QFileInfo& file) { return createResource(file); }); - m_first_folder_load = false; + auto indexDir2 = indexDir(); + auto* task = new ResourceFolderLoadTask(dir(), indexDir2, m_isIndexed, m_firstFolderLoad, + [this](const QFileInfo& file) { return createResource(file); }); + m_firstFolderLoad = false; return task; } 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(); } @@ -446,8 +464,9 @@ Qt::ItemFlags ResourceFolderModel::flags(const QModelIndex& index) const { Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); auto flags = defaultFlags | Qt::ItemIsDropEnabled; - if (index.isValid()) + if (index.isValid()) { flags |= Qt::ItemIsUserCheckable; + } return flags; } @@ -458,21 +477,25 @@ QStringList ResourceFolderModel::mimeTypes() const 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) { return true; } // check if the action is supported - if (!data || !(action & supportedDropActions())) { + if ((data == nullptr) || !(action & supportedDropActions())) { return false; } // files dropped from outside? if (data->hasUrls()) { auto urls = data->urls(); - for (auto url : urls) { + for (const auto& url : urls) { // only local files may be dropped... if (!url.isLocalFile()) { continue; @@ -488,14 +511,12 @@ bool ResourceFolderModel::dropMimeData(const QMimeData* data, Qt::DropAction act bool ResourceFolderModel::validateIndex(const QModelIndex& index) const { - if (!index.isValid()) + if (!index.isValid()) { return false; + } int row = index.row(); - if (row < 0 || row >= m_resources.size()) - return false; - - return true; + return row >= 0 && row < m_resources.size(); } // 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()) { return { QColor(255, 0, 0, 40) }; - } else { - return {}; } + return {}; } QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const { - if (!validateIndex(index)) + if (!validateIndex(index)) { return {}; + } int row = index.row(); int column = index.column(); @@ -536,7 +557,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const return {}; } case Qt::ToolTipRole: { - QString tooltip = m_resources[row]->internal_id(); + QString tooltip = m_resources[row]->internalId(); if (column == NameColumn) { if (APPLICATION->settings()->get("ShowModIncompat").toBool()) { @@ -547,7 +568,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const if (at(row).isSymLinkUnder(instDirPath())) { 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." "\nCanonical Path: %1") .arg(at(row).fileinfo().canonicalFilePath()); @@ -564,7 +585,8 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const if (column == NameColumn) { if (APPLICATION->settings()->get("ShowModIncompat").toBool() && at(row).hasIssues()) { 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"); } } @@ -572,8 +594,9 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const return {}; } case Qt::CheckStateRole: - if (column == ActiveColumn) + if (column == ActiveColumn) { return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked; + } return {}; default: 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) { int row = index.row(); - if (row < 0 || row >= rowCount(index.parent()) || !index.isValid()) + if (row < 0 || row >= rowCount(index.parent()) || !index.isValid()) { return false; + } if (role == Qt::CheckStateRole) { return setResourceEnabled({ index }, EnableAction::TOGGLE); @@ -643,22 +667,22 @@ void ResourceFolderModel::setupHeaderAction(QAction* act, int column) void ResourceFolderModel::saveColumns(QTreeView* tree) { - auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id()); - auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); - auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); + const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id()); + const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); + const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); auto stateSetting = m_instance->settings()->getSetting(stateSettingName); 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 - auto settings = m_instance->settings(); + auto* settings = m_instance->settings(); if (!settings->get(overrideSettingName).toBool()) { settings = APPLICATION->settings(); } 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]) { - auto name = m_column_names[i]; + auto name = m_columnNames[i]; visibility[name] = !tree->isColumnHidden(i); } } @@ -667,24 +691,24 @@ void ResourceFolderModel::saveColumns(QTreeView* tree) void ResourceFolderModel::loadColumns(QTreeView* tree) { - auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id()); - auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); - auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); + const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id()); + const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); + const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id()); auto stateSetting = m_instance->settings()->getOrRegisterSetting(stateSettingName, ""); 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()); - for (auto i = 0; i < m_column_names.size(); ++i) { + for (auto i = 0; i < m_columnNames.size(); ++i) { if (m_columnsHideable[i]) { - auto name = m_column_names[i]; + auto name = m_columnNames[i]; tree->setColumnHidden(i, !visibility.value(name, false).toBool()); } } }; - auto const defaultValue = Json::fromMap({ + const auto defaultValue = Json::fromMap({ { "Image", true }, { "Version", true }, { "Last Modified", true }, @@ -692,7 +716,7 @@ void ResourceFolderModel::loadColumns(QTreeView* tree) { "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 - auto settings = m_instance->settings(); + auto* settings = m_instance->settings(); if (!settings->getOrRegisterSetting(overrideSettingName, false)->get().toBool()) { 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 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()) { setVisible(value); } @@ -710,11 +734,11 @@ void ResourceFolderModel::loadColumns(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 - auto act = new QAction(tr("Override Columns Visibility"), menu); - auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); + auto* act = new QAction(tr("Override Columns Visibility"), menu); + const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id()); act->setCheckable(true); 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) { // Skip creating actions for columns that should not be hidden - if (!m_columnsHideable.at(col)) + if (!m_columnsHideable.at(col)) { continue; - auto act = new QAction(menu); + } + auto* act = new QAction(menu); setupHeaderAction(act, col); act->setCheckable(true); @@ -741,8 +766,9 @@ QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree) connect(act, &QAction::toggled, tree, [this, col, tree](bool toggled) { tree->setColumnHidden(col, !toggled); 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); + } } saveColumns(tree); }); @@ -760,41 +786,43 @@ QSortFilterProxyModel* ResourceFolderModel::createFilterProxyModel(QObject* pare SortType ResourceFolderModel::columnToSortKey(size_t column) const { - Q_ASSERT(m_column_sort_keys.size() == columnCount()); - return m_column_sort_keys.at(column); + Q_ASSERT(m_columnSortKeys.size() == columnCount()); + return m_columnSortKeys.at(column); } /* 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(sourceModel()); - if (!model) + if (!model) { return true; + } - const auto& resource = model->at(source_row); + const auto& resource = model->at(sourceRow); 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(sourceModel()); - if (!model || !source_left.isValid() || !source_right.isValid() || source_left.column() != source_right.column()) { - return QSortFilterProxyModel::lessThan(source_left, source_right); + if (!model || !sourceLeft.isValid() || !sourceRight.isValid() || sourceLeft.column() != sourceRight.column()) { + 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 // proceed. - auto column_sort_key = model->columnToSortKey(source_left.column()); - auto const& resource_left = model->at(source_left.row()); - auto const& resource_right = model->at(source_right.row()); + auto columnSortKey = model->columnToSortKey(sourceLeft.column()); + const auto& resourceLeft = model->at(sourceLeft.row()); + const auto& resourceRight = model->at(sourceRight.row()); - auto compare_result = resource_left.compare(resource_right, column_sort_key); - if (compare_result == 0) - return QSortFilterProxyModel::lessThan(source_left, source_right); + auto compareResult = resourceLeft.compare(resourceRight, columnSortKey); + if (compareResult == 0) { + return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight); + } - return compare_result < 0; + return compareResult < 0; } QString ResourceFolderModel::instDirPath() const @@ -802,50 +830,51 @@ QString ResourceFolderModel::instDirPath() const 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); - if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id)) + auto iter = m_activeParseTasks.constFind(ticket); + if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) { return; + } - auto removed_index = m_resources_index[resource_id]; - auto removed_it = m_resources.begin() + removed_index; - Q_ASSERT(removed_it != m_resources.end()); + auto removedIndex = m_resourcesIndex[resourceId]; + auto removedIt = m_resources.begin() + removedIndex; + Q_ASSERT(removedIt != m_resources.end()); - beginRemoveRows(QModelIndex(), removed_index, removed_index); - m_resources.erase(removed_it); + beginRemoveRows(QModelIndex(), removedIndex, removedIndex); + m_resources.erase(removedIt); // update index - m_resources_index.clear(); + m_resourcesIndex.clear(); int idx = 0; - for (auto const& mod : qAsConst(m_resources)) { - m_resources_index[mod->internal_id()] = idx; + for (const auto& mod : qAsConst(m_resources)) { + m_resourcesIndex[mod->internalId()] = idx; idx++; } endRemoveRows(); } -void ResourceFolderModel::applyUpdates(QSet& current_set, QSet& new_set, QMap& new_resources) +void ResourceFolderModel::applyUpdates(QSet& currentSet, QSet& newSet, QMap& newResources) { // see if the kept resources changed in some way { - QSet kept_set = current_set; - kept_set.intersect(new_set); + QSet keptSet = currentSet; + keptSet.intersect(newSet); - for (auto const& kept : kept_set) { - auto row_it = m_resources_index.constFind(kept); - Q_ASSERT(row_it != m_resources_index.constEnd()); - auto row = row_it.value(); + for (const auto& kept : keptSet) { + auto rowIt = m_resourcesIndex.constFind(kept); + Q_ASSERT(rowIt != m_resourcesIndex.constEnd()); + auto row = rowIt.value(); - auto& new_resource = new_resources[kept]; - auto const& current_resource = m_resources.at(row); + auto& newResource = newResources[kept]; + const auto& currentResource = m_resources.at(row); - if (new_resource->dateTimeChanged() == current_resource->dateTimeChanged()) { + if (newResource->dateTimeChanged() == currentResource->dateTimeChanged()) { // no significant change - bool hadIssues = !current_resource->hasIssues(); - current_resource->updateIssues(m_instance); + bool hadIssues = !currentResource->hasIssues(); + currentResource->updateIssues(m_instance); - if (hadIssues != current_resource->hasIssues()) { + if (hadIssues != currentResource->hasIssues()) { emit dataChanged(index(row, 0), index(row, columnCount({}) - 1)); } continue; @@ -853,16 +882,16 @@ void ResourceFolderModel::applyUpdates(QSet& current_set, QSet // If the resource is resolving, but something about it changed, we don't want to // continue the resolving. - if (current_resource->isResolving()) { - auto ticket = current_resource->resolutionTicket(); - if (m_active_parse_tasks.contains(ticket)) { - auto task = (*m_active_parse_tasks.find(ticket)).get(); + if (currentResource->isResolving()) { + auto ticket = currentResource->resolutionTicket(); + if (m_activeParseTasks.contains(ticket)) { + auto* task = (*m_activeParseTasks.find(ticket)).get(); task->abort(); } } - m_resources[row].reset(new_resource); - new_resource->updateIssues(m_instance); + m_resources[row].reset(newResource); + newResource->updateIssues(m_instance); resolveResource(m_resources.at(row)); emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1)); @@ -871,46 +900,47 @@ void ResourceFolderModel::applyUpdates(QSet& current_set, QSet // remove resources no longer present { - QSet removed_set = current_set; - removed_set.subtract(new_set); + QSet removedSet = currentSet; + removedSet.subtract(newSet); - QList removed_rows; - for (auto& removed : removed_set) - removed_rows.append(m_resources_index[removed]); + QList removedRows; + for (const auto& removed : removedSet) { + removedRows.append(m_resourcesIndex[removed]); + } - std::sort(removed_rows.begin(), removed_rows.end(), std::greater()); + std::ranges::sort(removedRows, std::greater()); - for (auto& removed_index : removed_rows) { - auto removed_it = m_resources.begin() + removed_index; + for (auto& removedIndex : removedRows) { + auto removedIt = m_resources.begin() + removedIndex; - Q_ASSERT(removed_it != m_resources.end()); + Q_ASSERT(removedIt != m_resources.end()); - if ((*removed_it)->isResolving()) { - auto ticket = (*removed_it)->resolutionTicket(); - if (m_active_parse_tasks.contains(ticket)) { - auto task = (*m_active_parse_tasks.find(ticket)).get(); + if ((*removedIt)->isResolving()) { + auto ticket = (*removedIt)->resolutionTicket(); + if (m_activeParseTasks.contains(ticket)) { + auto* task = (*m_activeParseTasks.find(ticket)).get(); task->abort(); } } - beginRemoveRows(QModelIndex(), removed_index, removed_index); - m_resources.erase(removed_it); + beginRemoveRows(QModelIndex(), removedIndex, removedIndex); + m_resources.erase(removedIt); endRemoveRows(); } } // add new resources to the end { - QSet added_set = new_set; - added_set.subtract(current_set); + QSet addedSet = newSet; + addedSet.subtract(currentSet); // 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(m_resources.size()), - static_cast(m_resources.size() + added_set.size() - 1)); + static_cast(m_resources.size() + addedSet.size() - 1)); - for (auto& added : added_set) { - auto res = new_resources[added]; + for (const auto& added : addedSet) { + auto res = newResources[added]; res->updateIssues(m_instance); m_resources.append(res); resolveResource(m_resources.last()); @@ -922,10 +952,10 @@ void ResourceFolderModel::applyUpdates(QSet& current_set, QSet // update index { - m_resources_index.clear(); + m_resourcesIndex.clear(); int idx = 0; - for (auto const& mod : qAsConst(m_resources)) { - m_resources_index[mod->internal_id()] = idx; + for (const auto& mod : qAsConst(m_resources)) { + m_resourcesIndex[mod->internalId()] = idx; idx++; } } @@ -933,17 +963,19 @@ void ResourceFolderModel::applyUpdates(QSet& current_set, QSet Resource::Ptr ResourceFolderModel::find(QString id) { auto iter = - std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](Resource::Ptr const& r) { return r->internal_id() == id; }); - if (iter == m_resources.constEnd()) + std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](const Resource::Ptr& r) { return r->internalId() == id; }); + if (iter == m_resources.constEnd()) { return nullptr; + } return *iter; } QList ResourceFolderModel::allResources() { QList result; result.reserve(m_resources.size()); - for (const Resource ::Ptr& resource : m_resources) + for (const Resource ::Ptr& resource : m_resources) { result.append((resource.get())); + } return result; } @@ -951,8 +983,9 @@ QList ResourceFolderModel::selectedResources(const QModelIndexList& i { QList result; for (const QModelIndex& index : indexes) { - if (index.column() != 0) + if (index.column() != 0) { continue; + } result.append(&at(index.row())); } return result; diff --git a/launcher/minecraft/mod/ResourceFolderModel.h b/launcher/minecraft/mod/ResourceFolderModel.h index e30485d3e..7699394ab 100644 --- a/launcher/minecraft/mod/ResourceFolderModel.h +++ b/launcher/minecraft/mod/ResourceFolderModel.h @@ -61,7 +61,7 @@ class QSortFilterProxyModel; class ResourceFolderModel : public QAbstractListModel { Q_OBJECT 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; virtual QString id() const { return "resource"; } @@ -93,13 +93,13 @@ class ResourceFolderModel : public QAbstractListModel { */ 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. * * 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 void deleteMetadata(const QModelIndexList&); @@ -125,7 +125,7 @@ class ResourceFolderModel : public QAbstractListModel { 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. * @@ -137,12 +137,12 @@ class ResourceFolderModel : public QAbstractListModel { /* Qt behavior */ /* 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(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; @@ -171,15 +171,15 @@ class ResourceFolderModel : public QAbstractListModel { QSortFilterProxyModel* createFilterProxyModel(QObject* parent = nullptr); SortType columnToSortKey(size_t column) const; - QList columnResizeModes() const { return m_column_resize_modes; } + QList columnResizeModes() const { return m_columnResizeModes; } class ProxyModel : public QSortFilterProxyModel { public: explicit ProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {} protected: - bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; - bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override; + bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override; + bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override; }; 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 * 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. * @@ -215,10 +215,10 @@ class ResourceFolderModel : public QAbstractListModel { * to act only on those disparities. * */ - void applyUpdates(QSet& current_set, QSet& new_set, QMap& new_resources); + void applyUpdates(QSet& currentSet, QSet& newSet, QMap& newResources); protected slots: - void directoryChanged(QString); + void directoryChanged(const QString&); /** 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 * if the resource is complex and has more stuff to parse. */ - virtual void onParseSucceeded(int ticket, QString resource_id); - virtual void onParseFailed(int ticket, QString resource_id); + virtual void onParseSucceeded(int ticket, const QString& resourceId); + virtual void onParseFailed(int ticket, const QString& resourceId); protected: // 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! - QList m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::DATE, SortType::PROVIDER, SortType::SIZE, - SortType::FILENAME }; - QStringList m_column_names = { "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") }; - QList m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, - QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; + QList m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Date, + SortType::Provider, SortType::Size, SortType::Filename }; + QStringList m_columnNames = { "Enable", "Name", "Last Modified", "Provider", "Size", "File Name" }; + QStringList m_columnNamesTranslated = { tr("Enable"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") }; + QList m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, + QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; QList m_columnsHideable = { false, false, true, true, true, true }; QDir m_dir; BaseInstance* m_instance; QFileSystemWatcher m_watcher; - bool m_is_watching = false; + bool m_isWatching = false; - bool m_is_indexed; - bool m_first_folder_load = true; + bool m_isIndexed; + bool m_firstFolderLoad = true; - Task::Ptr m_current_update_task = nullptr; - bool m_scheduled_update = false; + Task::Ptr m_currentUpdateTask = nullptr; + bool m_scheduledUpdate = false; QList m_resources; // Represents the relationship between a resource's internal ID and it's row position on the model. - QMap m_resources_index; + QMap m_resourcesIndex; // Runs off-thread ConcurrentTask m_resourceResolver; bool m_resourceResolverRunning = false; - QMap m_active_parse_tasks; - std::atomic m_next_resolution_ticket = 0; + QMap m_activeParseTasks; + std::atomic m_nextResolutionTicket = 0; }; diff --git a/launcher/minecraft/mod/ResourcePackFolderModel.cpp b/launcher/minecraft/mod/ResourcePackFolderModel.cpp index 84bc5ee98..4dd8e314c 100644 --- a/launcher/minecraft/mod/ResourcePackFolderModel.cpp +++ b/launcher/minecraft/mod/ResourcePackFolderModel.cpp @@ -39,27 +39,26 @@ #include #include -#include "Version.h" - #include "minecraft/mod/tasks/LocalDataPackParseTask.h" -ResourcePackFolderModel::ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent) - : ResourceFolderModel(dir, instance, is_indexed, create_dir, parent) +ResourcePackFolderModel::ResourcePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent) + : ResourceFolderModel(dir, instance, isIndexed, createDir, parent) { - m_column_names = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "Provider", "Size", "File Name" }); - m_column_names_translated = QStringList( + m_columnNames = QStringList({ "Enable", "Image", "Name", "Pack Format", "Last Modified", "Provider", "Size", "File Name" }); + m_columnNamesTranslated = QStringList( { 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, - SortType::DATE, SortType::PROVIDER, SortType::SIZE, SortType::FILENAME }; - m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, - QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; + m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::PackFormat, + SortType::Date, SortType::Provider, SortType::Size, SortType::Filename }; + m_columnResizeModes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, QHeaderView::Interactive, + QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; m_columnsHideable = { false, true, false, true, true, true, true, true }; } QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const { - if (!validateIndex(index)) + if (!validateIndex(index)) { return {}; + } int row = index.row(); int column = index.column(); @@ -92,6 +91,8 @@ QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const return QSize(32, 32); } break; + default: + break; } // map the columns to the base equivilents @@ -115,6 +116,8 @@ QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const case FileNameColumn: mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); break; + default: + break; } if (mappedIndex.isValid()) { @@ -174,10 +177,10 @@ QVariant ResourcePackFolderModel::headerData(int section, [[maybe_unused]] Qt::O int ResourcePackFolderModel::columnCount(const QModelIndex& parent) const { - return parent.isValid() ? 0 : NUM_COLUMNS; + return parent.isValid() ? 0 : NumColumns; } Task* ResourcePackFolderModel::createParseTask(Resource& resource) { - return new LocalDataPackParseTask(m_next_resolution_ticket, dynamic_cast(&resource)); + return new LocalDataPackParseTask(m_nextResolutionTicket, dynamic_cast(&resource)); } diff --git a/launcher/minecraft/mod/ResourcePackFolderModel.h b/launcher/minecraft/mod/ResourcePackFolderModel.h index 02029cd00..186bbb75d 100644 --- a/launcher/minecraft/mod/ResourcePackFolderModel.h +++ b/launcher/minecraft/mod/ResourcePackFolderModel.h @@ -7,9 +7,19 @@ class ResourcePackFolderModel : public ResourceFolderModel { Q_OBJECT 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"; } @@ -19,7 +29,7 @@ class ResourcePackFolderModel : public ResourceFolderModel { int columnCount(const QModelIndex& parent) const override; [[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) }; diff --git a/launcher/minecraft/mod/ShaderPackFolderModel.h b/launcher/minecraft/mod/ShaderPackFolderModel.h index 9b0180180..ded641770 100644 --- a/launcher/minecraft/mod/ShaderPackFolderModel.h +++ b/launcher/minecraft/mod/ShaderPackFolderModel.h @@ -18,7 +18,7 @@ class ShaderPackFolderModel : public ResourceFolderModel { [[nodiscard]] Task* createParseTask(Resource& resource) override { - return new LocalShaderPackParseTask(m_next_resolution_ticket, static_cast(resource)); + return new LocalShaderPackParseTask(m_nextResolutionTicket, static_cast(resource)); } QDir indexDir() const override { return m_dir; } diff --git a/launcher/minecraft/mod/TexturePackFolderModel.cpp b/launcher/minecraft/mod/TexturePackFolderModel.cpp index 59c46fcea..976bf3854 100644 --- a/launcher/minecraft/mod/TexturePackFolderModel.cpp +++ b/launcher/minecraft/mod/TexturePackFolderModel.cpp @@ -36,28 +36,30 @@ #include "TexturePackFolderModel.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) - : ResourceFolderModel(QDir(dir), instance, is_indexed, create_dir, parent) +TexturePackFolderModel::TexturePackFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent) + : ResourceFolderModel(QDir(dir), instance, isIndexed, createDir, parent) { - m_column_names = 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_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::NAME, SortType::DATE, SortType::PROVIDER, SortType::SIZE, SortType::FILENAME }; - m_column_resize_modes = { QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Stretch, - QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive, QHeaderView::Interactive }; + m_columnNames = QStringList({ "Enable", "Image", "Name", "Last Modified", "Provider", "Size", "File Name" }); + m_columnNamesTranslated = + QStringList({ tr("Enable"), tr("Image"), tr("Name"), tr("Last Modified"), tr("Provider"), tr("Size"), tr("File Name") }); + m_columnSortKeys = { SortType::Enabled, SortType::Name, SortType::Name, SortType::Date, + 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 }; } Task* TexturePackFolderModel::createParseTask(Resource& resource) { - return new LocalTexturePackParseTask(m_next_resolution_ticket, static_cast(resource)); + return new LocalTexturePackParseTask(m_nextResolutionTicket, static_cast(resource)); } QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const { - if (!validateIndex(index)) + if (!validateIndex(index)) { return {}; + } int row = index.row(); int column = index.column(); @@ -76,6 +78,8 @@ QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const return QSize(32, 32); } break; + default: + break; } // map the columns to the base equivilents @@ -99,6 +103,8 @@ QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const case FileNameColumn: mappedIndex = index.siblingAtColumn(ResourceFolderModel::FileNameColumn); break; + default: + break; } if (mappedIndex.isValid()) { @@ -151,5 +157,5 @@ QVariant TexturePackFolderModel::headerData(int section, [[maybe_unused]] Qt::Or int TexturePackFolderModel::columnCount(const QModelIndex& parent) const { - return parent.isValid() ? 0 : NUM_COLUMNS; + return parent.isValid() ? 0 : NumColumns; } diff --git a/launcher/minecraft/mod/TexturePackFolderModel.h b/launcher/minecraft/mod/TexturePackFolderModel.h index feef2f5d5..3e7343092 100644 --- a/launcher/minecraft/mod/TexturePackFolderModel.h +++ b/launcher/minecraft/mod/TexturePackFolderModel.h @@ -44,11 +44,20 @@ class TexturePackFolderModel : public ResourceFolderModel { Q_OBJECT 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; @@ -56,7 +65,7 @@ class TexturePackFolderModel : public ResourceFolderModel { int columnCount(const QModelIndex& parent) const override; [[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) }; diff --git a/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.cpp b/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.cpp index 3b98e053b..a90e9ca5e 100644 --- a/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.cpp +++ b/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.cpp @@ -41,26 +41,28 @@ #include "minecraft/mod/MetadataHandler.h" #include +#include -ResourceFolderLoadTask::ResourceFolderLoadTask(const QDir& resource_dir, - const QDir& index_dir, - bool is_indexed, - bool clean_orphan, - std::function create_function) +ResourceFolderLoadTask::ResourceFolderLoadTask(const QDir& resourceDir, + const QDir& indexDir, + bool isIndexed, + bool cleanOrphan, + std::function createFunction) : Task(false) - , m_resource_dir(resource_dir) - , m_index_dir(index_dir) - , m_is_indexed(is_indexed) - , m_clean_orphan(clean_orphan) - , m_create_func(create_function) + , m_resource_dir(resourceDir) + , m_index_dir(indexDir) + , m_is_indexed(isIndexed) + , m_clean_orphan(cleanOrphan) + , m_create_func(std::move(createFunction)) , m_result(new Result()) , m_thread_to_spawn_into(thread()) {} 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); + } if (m_is_indexed) { // Read metadata first @@ -71,7 +73,7 @@ void ResourceFolderLoadTask::executeTask() m_resource_dir.refresh(); for (auto entry : m_resource_dir.entryInfoList()) { auto filePath = entry.absoluteFilePath(); - if (auto app = APPLICATION_DYN; app && app->checkQSavePath(filePath)) { + if (auto* app = APPLICATION_DYN; (app != nullptr) && app->checkQSavePath(filePath)) { continue; } auto newFilePath = FS::getUniqueResourceName(filePath); @@ -83,29 +85,29 @@ void ResourceFolderLoadTask::executeTask() Resource* resource = m_create_func(entry); if (resource->enabled()) { - if (m_result->resources.contains(resource->internal_id())) { - m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::INSTALLED); + if (m_result->resources.contains(resource->internalId())) { + 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 resource; } else { - m_result->resources[resource->internal_id()].reset(resource); - m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::NO_METADATA); + m_result->resources[resource->internalId()].reset(resource); + m_result->resources[resource->internalId()]->setStatus(ResourceStatus::NoMetadata); } } else { - QString chopped_id = resource->internal_id().chopped(9); - if (m_result->resources.contains(chopped_id)) { - m_result->resources[resource->internal_id()].reset(resource); + QString choppedId = resource->internalId().chopped(9); + if (m_result->resources.contains(choppedId)) { + m_result->resources[resource->internalId()].reset(resource); - auto metadata = m_result->resources[chopped_id]->metadata(); + auto metadata = m_result->resources[choppedId]->metadata(); if (metadata) { resource->setMetadata(*metadata); - m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::INSTALLED); - m_result->resources.remove(chopped_id); + m_result->resources[resource->internalId()]->setStatus(ResourceStatus::Installed); + m_result->resources.remove(choppedId); } } else { - m_result->resources[resource->internal_id()].reset(resource); - m_result->resources[resource->internal_id()]->setStatus(ResourceStatus::NO_METADATA); + m_result->resources[resource->internalId()].reset(resource); + m_result->resources[resource->internalId()]->setStatus(ResourceStatus::NoMetadata); } } } @@ -116,38 +118,41 @@ void ResourceFolderLoadTask::executeTask() QMutableMapIterator iter(m_result->resources); while (iter.hasNext()) { auto resource = iter.next().value(); - if (resource->status() == ResourceStatus::NOT_INSTALLED) { + if (resource->status() == ResourceStatus::NotInstalled) { resource->destroy(m_index_dir, false, false); iter.remove(); } } } - for (auto mod : m_result->resources) + for (const auto& mod : m_result->resources) { mod->moveToThread(m_thread_to_spawn_into); + } - if (m_aborted) + if (m_aborted) { emit finished(); - else + } else { emitSucceeded(); + } } void ResourceFolderLoadTask::getFromMetadata() { 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")) { continue; } auto metadata = Metadata::get(m_index_dir, entry); - if (!metadata.isValid()) + if (!metadata.isValid()) { continue; + } auto* resource = m_create_func(QFileInfo(m_resource_dir.filePath(metadata.filename))); resource->setMetadata(metadata); - resource->setStatus(ResourceStatus::NOT_INSTALLED); - m_result->resources[resource->internal_id()].reset(resource); + resource->setStatus(ResourceStatus::NotInstalled); + m_result->resources[resource->internalId()].reset(resource); } } diff --git a/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.h b/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.h index 7c872c13d..6489176b7 100644 --- a/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.h +++ b/launcher/minecraft/mod/tasks/ResourceFolderLoadTask.h @@ -41,7 +41,7 @@ #include #include #include -#include "minecraft/mod/Mod.h" +#include "minecraft/mod/Resource.h" #include "tasks/Task.h" class ResourceFolderLoadTask : public Task { @@ -54,11 +54,11 @@ class ResourceFolderLoadTask : public Task { ResultPtr result() const { return m_result; } public: - ResourceFolderLoadTask(const QDir& resource_dir, - const QDir& index_dir, - bool is_indexed, - bool clean_orphan, - std::function create_function); + ResourceFolderLoadTask(const QDir& resourceDir, + const QDir& indexDir, + bool isIndexed, + bool cleanOrphan, + std::function createFunction); bool canAbort() const override { return true; } bool abort() override @@ -76,7 +76,7 @@ class ResourceFolderLoadTask : public Task { QDir m_resource_dir, m_index_dir; bool m_is_indexed; bool m_clean_orphan; - std::function m_create_func; + std::function m_create_func; ResultPtr m_result; std::atomic m_aborted = false; diff --git a/launcher/modplatform/EnsureMetadataTask.cpp b/launcher/modplatform/EnsureMetadataTask.cpp index 1ef3a56c9..2c7e485e5 100644 --- a/launcher/modplatform/EnsureMetadataTask.cpp +++ b/launcher/modplatform/EnsureMetadataTask.cpp @@ -99,7 +99,7 @@ void EnsureMetadataTask::executeTask() } // 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!"; emitReady(resource); continue; @@ -263,7 +263,7 @@ Task::Ptr EnsureMetadataTask::modrinthVersionsTask() Task::Ptr EnsureMetadataTask::modrinthProjectsTask() { QHash addonIds; - for (auto const& data : m_tempVersions) + for (const auto& data : m_tempVersions) addonIds.insert(data.addonId.toString(), data.hash); Task::Ptr proj_task; @@ -404,7 +404,7 @@ Task::Ptr EnsureMetadataTask::flameVersionsTask() Task::Ptr EnsureMetadataTask::flameProjectsTask() { QHash addonIds; - for (auto const& hash : m_resources.keys()) { + for (const auto& hash : m_resources.keys()) { if (m_tempVersions.contains(hash)) { auto data = m_tempVersions.find(hash).value(); diff --git a/launcher/modplatform/flame/FlameCheckUpdate.cpp b/launcher/modplatform/flame/FlameCheckUpdate.cpp index 5bd13220a..577f9967a 100644 --- a/launcher/modplatform/flame/FlameCheckUpdate.cpp +++ b/launcher/modplatform/flame/FlameCheckUpdate.cpp @@ -111,10 +111,10 @@ void FlameCheckUpdate::getLatestVersionCallback(Resource* resource, QByteArray* } 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; if (oldVersion.isEmpty()) { - if (resource->status() == ResourceStatus::NOT_INSTALLED) { + if (resource->status() == ResourceStatus::NotInstalled) { oldVersion = tr("Not installed"); } else { oldVersion = tr("Unknown"); diff --git a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp index cb2d57aba..0fda37e57 100644 --- a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp +++ b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp @@ -178,12 +178,12 @@ void ModrinthCheckUpdate::checkVersionsResponse(QByteArray* response, std::optio pack->slug = resource->metadata()->slug; pack->addonId = resource->metadata()->project_id; 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(pack, projectVer, m_resourceModel, true, "update"); QString oldVersion = resource->metadata()->version_number; if (oldVersion.isEmpty()) { - if (resource->status() == ResourceStatus::NOT_INSTALLED) { + if (resource->status() == ResourceStatus::NotInstalled) { oldVersion = tr("Not installed"); } else { oldVersion = tr("Unknown"); diff --git a/launcher/ui/dialogs/ResourceUpdateDialog.cpp b/launcher/ui/dialogs/ResourceUpdateDialog.cpp index e229bd672..e9776c660 100644 --- a/launcher/ui/dialogs/ResourceUpdateDialog.cpp +++ b/launcher/ui/dialogs/ResourceUpdateDialog.cpp @@ -306,7 +306,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool // ask the user on what provider to seach for the mod first for (auto* candidate : m_candidates) { - if (candidate->status() != ResourceStatus::NO_METADATA) { + if (candidate->status() != ResourceStatus::NoMetadata) { onMetadataEnsured(candidate); continue; } @@ -321,7 +321,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool if (confirmRest) { addToTmp(candidate, providerRest); - shouldTryOthers.insert(candidate->internal_id(), tryOthersRest); + shouldTryOthers.insert(candidate->internalId(), tryOthersRest); continue; } @@ -343,7 +343,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool tryOthersRest = response.try_others; } - shouldTryOthers.insert(candidate->internal_id(), response.try_others); + shouldTryOthers.insert(candidate->internalId(), response.try_others); if (confirmed) { addToTmp(candidate, response.chosen); @@ -355,7 +355,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool auto modrinthTask = makeShared(modrinthTmp, indexDir2, ModPlatform::ResourceProvider::MODRINTH); connect(modrinthTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(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, [this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); }); @@ -372,7 +372,7 @@ auto ResourceUpdateDialog::ensureMetadata() -> bool auto flameTask = makeShared(flameTmp, indexDir2, ModPlatform::ResourceProvider::FLAME); connect(flameTask.get(), &EnsureMetadataTask::metadataReady, [this](Resource* candidate) { onMetadataEnsured(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, [this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); }); diff --git a/launcher/ui/widgets/InfoFrame.cpp b/launcher/ui/widgets/InfoFrame.cpp index 354211439..3bf9fef82 100644 --- a/launcher/ui/widgets/InfoFrame.cpp +++ b/launcher/ui/widgets/InfoFrame.cpp @@ -87,7 +87,7 @@ void InfoFrame::updateWithMod(const Mod& m) QString name = ""; QString link = m.homepage(); if (m.name().isEmpty()) - name = m.internal_id(); + name = m.internalId(); else name = renderColorCodes(m.name()); diff --git a/tests/ResourceFolderModel_test.cpp b/tests/ResourceFolderModel_test.cpp index 145e6b3d7..26fe5ab16 100644 --- a/tests/ResourceFolderModel_test.cpp +++ b/tests/ResourceFolderModel_test.cpp @@ -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_2 = model.at(0).type() == ResourceType::FOLDER ? model.at(0) : model.at(1); - auto id_1 = res_1.internal_id(); - auto id_2 = res_2.internal_id(); + auto id_1 = res_1.internalId(); + auto id_2 = res_2.internalId(); bool initial_enabled_res_2 = res_2.enabled(); bool initial_enabled_res_1 = res_1.enabled(); @@ -236,12 +236,12 @@ class ResourceFolderModelTest : public QObject { qDebug() << "res_1 got successfully toggled again."; 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."; QVERIFY(!res_2.enable(initial_enabled_res_2 ? EnableAction::ENABLE : EnableAction::DISABLE)); QVERIFY(res_2.enabled() == initial_enabled_res_2); - QVERIFY(res_2.internal_id() == id_2); + QVERIFY(res_2.internalId() == id_2); } };