aboutsummaryrefslogtreecommitdiff
path: root/launcher/minecraft/mod
diff options
context:
space:
mode:
authorSefa Eyeoglu <contact@scrumplex.net>2022-08-28 16:52:53 +0200
committerGitHub <noreply@github.com>2022-08-28 16:52:53 +0200
commitf371ec210c091b9ebc91930be74752383cb941c6 (patch)
treef3b13e1015285e76145552d21503ff641bf4ce11 /launcher/minecraft/mod
parentafcd669d2f6934c2b6076939d7665f791d495994 (diff)
parent0b81b283bfdd16b409127f22eac7b51ce0142929 (diff)
downloadPrismLauncher-f371ec210c091b9ebc91930be74752383cb941c6.tar.gz
PrismLauncher-f371ec210c091b9ebc91930be74752383cb941c6.tar.bz2
PrismLauncher-f371ec210c091b9ebc91930be74752383cb941c6.zip
Merge pull request #1052 from flowln/resource_model
Diffstat (limited to 'launcher/minecraft/mod')
-rw-r--r--launcher/minecraft/mod/Mod.cpp180
-rw-r--r--launcher/minecraft/mod/Mod.h70
-rw-r--r--launcher/minecraft/mod/ModDetails.h61
-rw-r--r--launcher/minecraft/mod/ModFolderModel.cpp628
-rw-r--r--launcher/minecraft/mod/ModFolderModel.h99
-rw-r--r--launcher/minecraft/mod/ModFolderModel_test.cpp92
-rw-r--r--launcher/minecraft/mod/Resource.cpp147
-rw-r--r--launcher/minecraft/mod/Resource.h115
-rw-r--r--launcher/minecraft/mod/ResourceFolderModel.cpp522
-rw-r--r--launcher/minecraft/mod/ResourceFolderModel.h326
-rw-r--r--launcher/minecraft/mod/ResourceFolderModel_test.cpp275
-rw-r--r--launcher/minecraft/mod/ResourcePack.h13
-rw-r--r--launcher/minecraft/mod/ResourcePackFolderModel.cpp22
-rw-r--r--launcher/minecraft/mod/ResourcePackFolderModel.h9
-rw-r--r--launcher/minecraft/mod/ShaderPackFolderModel.h10
-rw-r--r--launcher/minecraft/mod/TexturePackFolderModel.cpp22
-rw-r--r--launcher/minecraft/mod/TexturePackFolderModel.h6
-rw-r--r--launcher/minecraft/mod/tasks/BasicFolderLoadTask.h53
-rw-r--r--launcher/minecraft/mod/tasks/LocalModParseTask.cpp140
-rw-r--r--launcher/minecraft/mod/tasks/LocalModParseTask.h22
-rw-r--r--launcher/minecraft/mod/tasks/ModFolderLoadTask.cpp10
-rw-r--r--launcher/minecraft/mod/tasks/ModFolderLoadTask.h12
-rw-r--r--launcher/minecraft/mod/testdata/supercoolmod.jar1
23 files changed, 1851 insertions, 984 deletions
diff --git a/launcher/minecraft/mod/Mod.cpp b/launcher/minecraft/mod/Mod.cpp
index 588d76e3..39023f69 100644
--- a/launcher/minecraft/mod/Mod.cpp
+++ b/launcher/minecraft/mod/Mod.cpp
@@ -36,130 +36,77 @@
#include "Mod.h"
+#include <QDebug>
#include <QDir>
#include <QString>
+#include <QRegularExpression>
-#include <FileSystem.h>
-#include <QDebug>
-
-#include "Application.h"
#include "MetadataHandler.h"
+#include "Version.h"
-namespace {
-
-ModDetails invalidDetails;
-
-}
-
-Mod::Mod(const QFileInfo& file)
+Mod::Mod(const QFileInfo& file) : Resource(file), m_local_details()
{
- repath(file);
- m_changedDateTime = file.lastModified();
+ m_enabled = (file.suffix() != "disabled");
}
Mod::Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata)
- : m_file(mods_dir.absoluteFilePath(metadata.filename))
- , m_internal_id(metadata.filename)
- , m_name(metadata.name)
-{
- if (m_file.isDir()) {
- m_type = MOD_FOLDER;
- } else {
- if (metadata.filename.endsWith(".zip") || metadata.filename.endsWith(".jar"))
- m_type = MOD_ZIPFILE;
- else if (metadata.filename.endsWith(".litemod"))
- m_type = MOD_LITEMOD;
- else
- m_type = MOD_SINGLEFILE;
- }
-
- m_enabled = true;
- m_changedDateTime = m_file.lastModified();
-
- m_temp_metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
+ : Mod(mods_dir.absoluteFilePath(metadata.filename))
+{
+ m_name = metadata.name;
+ m_local_details.metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
}
-void Mod::repath(const QFileInfo& file)
+void Mod::setStatus(ModStatus status)
{
- m_file = file;
- QString name_base = file.fileName();
-
- m_type = Mod::MOD_UNKNOWN;
-
- m_internal_id = name_base;
-
- if (m_file.isDir()) {
- m_type = MOD_FOLDER;
- m_name = name_base;
- } else if (m_file.isFile()) {
- if (name_base.endsWith(".disabled")) {
- m_enabled = false;
- name_base.chop(9);
- } else {
- m_enabled = true;
- }
- if (name_base.endsWith(".zip") || name_base.endsWith(".jar")) {
- m_type = MOD_ZIPFILE;
- name_base.chop(4);
- } else if (name_base.endsWith(".litemod")) {
- m_type = MOD_LITEMOD;
- name_base.chop(8);
- } else {
- m_type = MOD_SINGLEFILE;
- }
- m_name = name_base;
- }
+ m_local_details.status = status;
}
-
-auto Mod::enable(bool value) -> bool
+void Mod::setMetadata(std::shared_ptr<Metadata::ModStruct>&& metadata)
{
- if (m_type == Mod::MOD_UNKNOWN || m_type == Mod::MOD_FOLDER)
- return false;
-
- if (m_enabled == value)
- return false;
-
- QString path = m_file.absoluteFilePath();
- QFile file(path);
- if (value) {
- if (!path.endsWith(".disabled"))
- return false;
- path.chop(9);
-
- if (!file.rename(path))
- return false;
- } else {
- path += ".disabled";
-
- if (!file.rename(path))
- return false;
- }
-
if (status() == ModStatus::NoMetadata)
- repath(QFileInfo(path));
+ setStatus(ModStatus::Installed);
- m_enabled = value;
- return true;
+ m_local_details.metadata = metadata;
}
-void Mod::setStatus(ModStatus status)
+std::pair<int, bool> Mod::compare(const Resource& other, SortType type) const
{
- if (m_localDetails) {
- m_localDetails->status = status;
- } else {
- m_temp_status = status;
+ auto cast_other = dynamic_cast<Mod const*>(&other);
+ if (!cast_other)
+ return Resource::compare(other, type);
+
+ switch (type) {
+ default:
+ case SortType::ENABLED:
+ case SortType::NAME:
+ case SortType::DATE: {
+ auto res = Resource::compare(other, type);
+ if (res.first != 0)
+ return res;
+ }
+ case SortType::VERSION: {
+ auto this_ver = Version(version());
+ auto other_ver = Version(cast_other->version());
+ if (this_ver > other_ver)
+ return { 1, type == SortType::VERSION };
+ if (this_ver < other_ver)
+ return { -1, type == SortType::VERSION };
+ }
}
+ return { 0, false };
}
-void Mod::setMetadata(const Metadata::ModStruct& metadata)
+
+bool Mod::applyFilter(QRegularExpression filter) const
{
- if (status() == ModStatus::NoMetadata)
- setStatus(ModStatus::Installed);
+ if (filter.match(description()).hasMatch())
+ return true;
- if (m_localDetails) {
- m_localDetails->metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
- } else {
- m_temp_metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
+ for (auto& author : authors()) {
+ if (filter.match(author).hasMatch()) {
+ return true;
+ }
}
+
+ return Resource::applyFilter(filter);
}
auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool
@@ -175,13 +122,12 @@ auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool
}
}
- m_type = MOD_UNKNOWN;
- return FS::deletePath(m_file.filePath());
+ return Resource::destroy();
}
auto Mod::details() const -> const ModDetails&
{
- return m_localDetails ? *m_localDetails : invalidDetails;
+ return m_local_details;
}
auto Mod::name() const -> QString
@@ -218,35 +164,29 @@ auto Mod::authors() const -> QStringList
auto Mod::status() const -> ModStatus
{
- if (!m_localDetails)
- return m_temp_status;
return details().status;
}
auto Mod::metadata() -> std::shared_ptr<Metadata::ModStruct>
{
- if (m_localDetails)
- return m_localDetails->metadata;
- return m_temp_metadata;
+ return m_local_details.metadata;
}
auto Mod::metadata() const -> const std::shared_ptr<Metadata::ModStruct>
{
- if (m_localDetails)
- return m_localDetails->metadata;
- return m_temp_metadata;
+ return m_local_details.metadata;
}
-void Mod::finishResolvingWithDetails(std::shared_ptr<ModDetails> details)
+void Mod::finishResolvingWithDetails(ModDetails&& details)
{
- m_resolving = false;
- m_resolved = true;
- m_localDetails = details;
+ m_is_resolving = false;
+ m_is_resolved = true;
- setStatus(m_temp_status);
+ std::shared_ptr<Metadata::ModStruct> metadata = details.metadata;
+ if (details.status == ModStatus::Unknown)
+ details.status = m_local_details.status;
- if (m_localDetails && m_temp_metadata && m_temp_metadata->isValid()) {
- setMetadata(*m_temp_metadata);
- m_temp_metadata.reset();
- }
+ m_local_details = std::move(details);
+ if (metadata)
+ setMetadata(std::move(metadata));
}
diff --git a/launcher/minecraft/mod/Mod.h b/launcher/minecraft/mod/Mod.h
index 7a13e44b..f336bec4 100644
--- a/launcher/minecraft/mod/Mod.h
+++ b/launcher/minecraft/mod/Mod.h
@@ -39,38 +39,23 @@
#include <QFileInfo>
#include <QList>
-#include "QObjectPtr.h"
+#include "Resource.h"
#include "ModDetails.h"
-class Mod : public QObject
+class Mod : public Resource
{
Q_OBJECT
public:
- enum ModType
- {
- MOD_UNKNOWN, //!< Indicates an unspecified mod type.
- MOD_ZIPFILE, //!< The mod is a zip file containing the mod's class files.
- MOD_SINGLEFILE, //!< The mod is a single file (not a zip file).
- MOD_FOLDER, //!< The mod is in a folder on the filesystem.
- MOD_LITEMOD, //!< The mod is a litemod
- };
-
using Ptr = shared_qobject_ptr<Mod>;
+ using WeakPtr = QPointer<Mod>;
Mod() = default;
Mod(const QFileInfo &file);
- explicit Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata);
-
- auto fileinfo() const -> QFileInfo { return m_file; }
- auto dateTimeChanged() const -> QDateTime { return m_changedDateTime; }
- auto internal_id() const -> QString { return m_internal_id; }
- auto type() const -> ModType { return m_type; }
- auto enabled() const -> bool { return m_enabled; }
-
- auto valid() const -> bool { return m_type != MOD_UNKNOWN; }
+ Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata);
+ Mod(QString file_path) : Mod(QFileInfo(file_path)) {}
auto details() const -> const ModDetails&;
- auto name() const -> QString;
+ auto name() const -> QString override;
auto version() const -> QString;
auto homeurl() const -> QString;
auto description() const -> QString;
@@ -81,46 +66,17 @@ public:
auto metadata() const -> const std::shared_ptr<Metadata::ModStruct>;
void setStatus(ModStatus status);
- void setMetadata(const Metadata::ModStruct& metadata);
+ void setMetadata(std::shared_ptr<Metadata::ModStruct>&& metadata);
+ void setMetadata(const Metadata::ModStruct& metadata) { setMetadata(std::make_shared<Metadata::ModStruct>(metadata)); }
- auto enable(bool value) -> bool;
+ [[nodiscard]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override;
+ [[nodiscard]] bool applyFilter(QRegularExpression filter) const override;
- // delete all the files of this mod
+ // Delete all the files of this mod
auto destroy(QDir& index_dir, bool preserve_metadata = false) -> bool;
- // change the mod's filesystem path (used by mod lists for *MAGIC* purposes)
- void repath(const QFileInfo &file);
-
- auto shouldResolve() const -> bool { return !m_resolving && !m_resolved; }
- auto isResolving() const -> bool { return m_resolving; }
- auto resolutionTicket() const -> int { return m_resolutionTicket; }
-
- void setResolving(bool resolving, int resolutionTicket) {
- m_resolving = resolving;
- m_resolutionTicket = resolutionTicket;
- }
- void finishResolvingWithDetails(std::shared_ptr<ModDetails> details);
+ void finishResolvingWithDetails(ModDetails&& details);
protected:
- QFileInfo m_file;
- QDateTime m_changedDateTime;
-
- QString m_internal_id;
- /* Name as reported via the file name */
- QString m_name;
- ModType m_type = MOD_UNKNOWN;
-
- /* If the mod has metadata, this will be filled in the constructor, and passed to
- * the ModDetails when calling finishResolvingWithDetails */
- std::shared_ptr<Metadata::ModStruct> m_temp_metadata;
-
- /* Set the mod status while it doesn't have local details just yet */
- ModStatus m_temp_status = ModStatus::NoMetadata;
-
- std::shared_ptr<ModDetails> m_localDetails;
-
- bool m_enabled = true;
- bool m_resolving = false;
- bool m_resolved = false;
- int m_resolutionTicket = 0;
+ ModDetails m_local_details;
};
diff --git a/launcher/minecraft/mod/ModDetails.h b/launcher/minecraft/mod/ModDetails.h
index 3e0a7ab0..dd84b0a3 100644
--- a/launcher/minecraft/mod/ModDetails.h
+++ b/launcher/minecraft/mod/ModDetails.h
@@ -46,34 +46,77 @@ enum class ModStatus {
Installed, // Both JAR and Metadata are present
NotInstalled, // Only the Metadata is present
NoMetadata, // Only the JAR is present
+ Unknown, // Default status
};
struct ModDetails
{
/* Mod ID as defined in the ModLoader-specific metadata */
- QString mod_id;
+ QString mod_id = {};
/* Human-readable name */
- QString name;
+ QString name = {};
/* Human-readable mod version */
- QString version;
+ QString version = {};
/* Human-readable minecraft version */
- QString mcversion;
+ QString mcversion = {};
/* URL for mod's home page */
- QString homeurl;
+ QString homeurl = {};
/* Human-readable description */
- QString description;
+ QString description = {};
/* List of the author's names */
- QStringList authors;
+ QStringList authors = {};
/* Installation status of the mod */
- ModStatus status;
+ ModStatus status = ModStatus::Unknown;
/* Metadata information, if any */
- std::shared_ptr<Metadata::ModStruct> metadata;
+ std::shared_ptr<Metadata::ModStruct> metadata = nullptr;
+
+ ModDetails() = default;
+
+ /** Metadata should be handled manually to properly set the mod status. */
+ ModDetails(ModDetails& other)
+ : mod_id(other.mod_id)
+ , name(other.name)
+ , version(other.version)
+ , mcversion(other.mcversion)
+ , homeurl(other.homeurl)
+ , description(other.description)
+ , authors(other.authors)
+ , status(other.status)
+ {}
+
+ ModDetails& operator=(ModDetails& other)
+ {
+ this->mod_id = other.mod_id;
+ this->name = other.name;
+ this->version = other.version;
+ this->mcversion = other.mcversion;
+ this->homeurl = other.homeurl;
+ this->description = other.description;
+ this->authors = other.authors;
+ this->status = other.status;
+
+ return *this;
+ }
+
+ ModDetails& operator=(ModDetails&& other)
+ {
+ this->mod_id = other.mod_id;
+ this->name = other.name;
+ this->version = other.version;
+ this->mcversion = other.mcversion;
+ this->homeurl = other.homeurl;
+ this->description = other.description;
+ this->authors = other.authors;
+ this->status = other.status;
+
+ return *this;
+ }
};
diff --git a/launcher/minecraft/mod/ModFolderModel.cpp b/launcher/minecraft/mod/ModFolderModel.cpp
index d4c5e819..4e264a74 100644
--- a/launcher/minecraft/mod/ModFolderModel.cpp
+++ b/launcher/minecraft/mod/ModFolderModel.cpp
@@ -49,432 +49,53 @@
#include "minecraft/mod/tasks/LocalModParseTask.h"
#include "minecraft/mod/tasks/ModFolderLoadTask.h"
-ModFolderModel::ModFolderModel(const QString &dir, bool is_indexed) : QAbstractListModel(), m_dir(dir), m_is_indexed(is_indexed)
+ModFolderModel::ModFolderModel(const QString &dir, bool is_indexed) : ResourceFolderModel(QDir(dir)), m_is_indexed(is_indexed)
{
FS::ensureFolderPathExists(m_dir.absolutePath());
- m_dir.setFilter(QDir::Readable | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
- m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
- m_watcher = new QFileSystemWatcher(this);
- connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(directoryChanged(QString)));
-}
-
-void ModFolderModel::startWatching()
-{
- if(is_watching)
- return;
-
- // Remove orphaned metadata next time
- m_first_folder_load = true;
-
- update();
-
- // Watch the mods folder
- is_watching = m_watcher->addPath(m_dir.absolutePath());
- if (is_watching) {
- qDebug() << "Started watching " << m_dir.absolutePath();
- } else {
- qDebug() << "Failed to start watching " << m_dir.absolutePath();
- }
-
- // Watch the mods index folder
- is_watching = m_watcher->addPath(indexDir().absolutePath());
- if (is_watching) {
- qDebug() << "Started watching " << indexDir().absolutePath();
- } else {
- qDebug() << "Failed to start watching " << indexDir().absolutePath();
- }
-}
-
-void ModFolderModel::stopWatching()
-{
- if(!is_watching)
- return;
-
- is_watching = !m_watcher->removePath(m_dir.absolutePath());
- if (!is_watching) {
- qDebug() << "Stopped watching " << m_dir.absolutePath();
- } else {
- qDebug() << "Failed to stop watching " << m_dir.absolutePath();
- }
-
- is_watching = !m_watcher->removePath(indexDir().absolutePath());
- if (!is_watching) {
- qDebug() << "Stopped watching " << indexDir().absolutePath();
- } else {
- qDebug() << "Failed to stop watching " << indexDir().absolutePath();
- }
-}
-
-bool ModFolderModel::update()
-{
- if (!isValid()) {
- return false;
- }
- if(m_update) {
- scheduled_update = true;
- return true;
- }
-
- auto index_dir = indexDir();
- auto task = new ModFolderLoadTask(dir(), index_dir, m_is_indexed, m_first_folder_load);
- m_first_folder_load = false;
-
- m_update = task->result();
-
- QThreadPool *threadPool = QThreadPool::globalInstance();
- connect(task, &ModFolderLoadTask::succeeded, this, &ModFolderModel::finishUpdate);
-
- threadPool->start(task);
- return true;
-}
-
-void ModFolderModel::finishUpdate()
-{
-#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
- auto currentList = modsIndex.keys();
- QSet<QString> currentSet(currentList.begin(), currentList.end());
- auto & newMods = m_update->mods;
- auto newList = newMods.keys();
- QSet<QString> newSet(newList.begin(), newList.end());
-#else
- QSet<QString> currentSet = modsIndex.keys().toSet();
- auto& newMods = m_update->mods;
- QSet<QString> newSet = newMods.keys().toSet();
-#endif
-
- // see if the kept mods changed in some way
- {
- QSet<QString> kept = currentSet;
- kept.intersect(newSet);
- for(auto& keptMod : kept) {
- auto newMod = newMods[keptMod];
- auto row = modsIndex[keptMod];
- auto currentMod = mods[row];
- if(newMod->dateTimeChanged() == currentMod->dateTimeChanged()) {
- // no significant change, ignore...
- continue;
- }
- auto oldMod = mods[row];
- if(oldMod->isResolving()) {
- activeTickets.remove(oldMod->resolutionTicket());
- }
-
- mods[row] = newMod;
- resolveMod(mods[row]);
- emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1));
- }
- }
-
- // remove mods no longer present
- {
- QSet<QString> removed = currentSet;
- QList<int> removedRows;
- removed.subtract(newSet);
- for(auto & removedMod: removed) {
- removedRows.append(modsIndex[removedMod]);
- }
- std::sort(removedRows.begin(), removedRows.end(), std::greater<int>());
- for(auto iter = removedRows.begin(); iter != removedRows.end(); iter++) {
- int removedIndex = *iter;
- beginRemoveRows(QModelIndex(), removedIndex, removedIndex);
- auto removedIter = mods.begin() + removedIndex;
- if((*removedIter)->isResolving()) {
- activeTickets.remove((*removedIter)->resolutionTicket());
- }
-
- mods.erase(removedIter);
- endRemoveRows();
- }
- }
-
- // add new mods to the end
- {
- QSet<QString> added = newSet;
- added.subtract(currentSet);
-
- // When you have a Qt build with assertions turned on, proceeding here will abort the application
- if (added.size() > 0) {
- beginInsertRows(QModelIndex(), mods.size(), mods.size() + added.size() - 1);
- for (auto& addedMod : added) {
- mods.append(newMods[addedMod]);
- resolveMod(mods.last());
- }
- endInsertRows();
- }
- }
-
- // update index
- {
- modsIndex.clear();
- int idx = 0;
- for(auto mod: mods) {
- modsIndex[mod->internal_id()] = idx;
- idx++;
- }
- }
-
- m_update.reset();
-
- emit updateFinished();
-
- if(scheduled_update) {
- scheduled_update = false;
- update();
- }
-}
-
-void ModFolderModel::resolveMod(Mod::Ptr m)
-{
- if(!m->shouldResolve()) {
- return;
- }
-
- auto task = new LocalModParseTask(nextResolutionTicket, m->type(), m->fileinfo());
- auto result = task->result();
- result->id = m->internal_id();
- activeTickets.insert(nextResolutionTicket, result);
- m->setResolving(true, nextResolutionTicket);
- nextResolutionTicket++;
- QThreadPool *threadPool = QThreadPool::globalInstance();
- connect(task, &LocalModParseTask::finished, this, &ModFolderModel::finishModParse);
- threadPool->start(task);
-}
-
-void ModFolderModel::finishModParse(int token)
-{
- auto iter = activeTickets.find(token);
- if(iter == activeTickets.end()) {
- return;
- }
- auto result = *iter;
- activeTickets.remove(token);
- int row = modsIndex[result->id];
- auto mod = mods[row];
- mod->finishResolvingWithDetails(result->details);
- emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
-}
-
-void ModFolderModel::disableInteraction(bool disabled)
-{
- if (interaction_disabled == disabled) {
- return;
- }
- interaction_disabled = disabled;
- if(size()) {
- emit dataChanged(index(0), index(size() - 1));
- }
-}
-
-void ModFolderModel::directoryChanged(QString path)
-{
- update();
-}
-
-bool ModFolderModel::isValid()
-{
- return m_dir.exists() && m_dir.isReadable();
-}
-
-auto ModFolderModel::selectedMods(QModelIndexList& indexes) -> QList<Mod::Ptr>
-{
- QList<Mod::Ptr> selected_mods;
- for (auto i : indexes) {
- if(i.column() != 0)
- continue;
-
- selected_mods.push_back(mods[i.row()]);
- }
- return selected_mods;
-}
-
-// FIXME: this does not take disabled mod (with extra .disable extension) into account...
-bool ModFolderModel::installMod(const QString &filename)
-{
- if(interaction_disabled) {
- return false;
- }
-
- // NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName
- auto originalPath = FS::NormalizePath(filename);
- QFileInfo fileinfo(originalPath);
-
- if (!fileinfo.exists() || !fileinfo.isReadable())
- {
- qWarning() << "Caught attempt to install non-existing file or file-like object:" << originalPath;
- return false;
- }
- qDebug() << "installing: " << fileinfo.absoluteFilePath();
-
- Mod installedMod(fileinfo);
- if (!installedMod.valid())
- {
- qDebug() << originalPath << "is not a valid mod. Ignoring it.";
- return false;
- }
-
- auto type = installedMod.type();
- if (type == Mod::MOD_UNKNOWN)
- {
- qDebug() << "Cannot recognize mod type of" << originalPath << ", ignoring it.";
- return false;
- }
-
- auto newpath = FS::NormalizePath(FS::PathCombine(m_dir.path(), fileinfo.fileName()));
- if(originalPath == newpath)
- {
- qDebug() << "Overwriting the mod (" << originalPath << ") with itself makes no sense...";
- return false;
- }
-
- if (type == Mod::MOD_SINGLEFILE || type == Mod::MOD_ZIPFILE || type == Mod::MOD_LITEMOD)
- {
- if(QFile::exists(newpath) || QFile::exists(newpath + QString(".disabled")))
- {
- if(!QFile::remove(newpath))
- {
- // FIXME: report error in a user-visible way
- qWarning() << "Copy from" << originalPath << "to" << newpath << "has failed.";
- return false;
- }
- qDebug() << newpath << "has been deleted.";
- }
- if (!QFile::copy(fileinfo.filePath(), newpath))
- {
- qWarning() << "Copy from" << originalPath << "to" << newpath << "has failed.";
- // FIXME: report error in a user-visible way
- return false;
- }
- FS::updateTimestamp(newpath);
- QFileInfo newpathInfo(newpath);
- installedMod.repath(newpathInfo);
- update();
- return true;
- }
- else if (type == Mod::MOD_FOLDER)
- {
- QString from = fileinfo.filePath();
- if(QFile::exists(newpath))
- {
- qDebug() << "Ignoring folder " << from << ", it would merge with " << newpath;
- return false;
- }
-
- if (!FS::copy(from, newpath)())
- {
- qWarning() << "Copy of folder from" << originalPath << "to" << newpath << "has (potentially partially) failed.";
- return false;
- }
- QFileInfo newpathInfo(newpath);
- installedMod.repath(newpathInfo);
- update();
- return true;
- }
- return false;
-}
-
-bool ModFolderModel::uninstallMod(const QString& filename, bool preserve_metadata)
-{
-
- for(auto mod : allMods()){
- if(mod->fileinfo().fileName() == filename){
- auto index_dir = indexDir();
- mod->destroy(index_dir, preserve_metadata);
- return true;
- }
- }
-
- return false;
-}
-
-bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusAction enable)
-{
- if(interaction_disabled) {
- return false;
- }
-
- if(indexes.isEmpty())
- return true;
-
- for (auto index: indexes)
- {
- if(index.column() != 0) {
- continue;
- }
- setModStatus(index.row(), enable);
- }
- return true;
-}
-
-bool ModFolderModel::deleteMods(const QModelIndexList& indexes)
-{
- if(interaction_disabled) {
- return false;
- }
-
- if(indexes.isEmpty())
- return true;
-
- for (auto i: indexes)
- {
- if(i.column() != 0) {
- continue;
- }
- auto m = mods[i.row()];
- auto index_dir = indexDir();
- m->destroy(index_dir);
- }
- return true;
-}
-
-int ModFolderModel::columnCount(const QModelIndex &parent) const
-{
- return NUM_COLUMNS;
+ m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::VERSION, SortType::DATE };
}
QVariant ModFolderModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid())
- return QVariant();
+ if (!validateIndex(index))
+ return {};
int row = index.row();
int column = index.column();
- if (row < 0 || row >= mods.size())
- return QVariant();
-
switch (role)
{
case Qt::DisplayRole:
switch (column)
{
case NameColumn:
- return mods[row]->name();
+ return m_resources[row]->name();
case VersionColumn: {
- switch(mods[row]->type()) {
- case Mod::MOD_FOLDER:
+ switch(m_resources[row]->type()) {
+ case ResourceType::FOLDER:
return tr("Folder");
- case Mod::MOD_SINGLEFILE:
+ case ResourceType::SINGLEFILE:
return tr("File");
default:
break;
}
- return mods[row]->version();
+ return at(row)->version();
}
case DateColumn:
-