diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-05-14 14:40:15 +0200 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-05-14 14:40:15 +0200 |
| commit | ac3837164893c52c6e943843337659c2e0a8bd40 (patch) | |
| tree | b3a57952d7a4e35cec208e5bb068f56c17e4e40b | |
| parent | 2a5dd6c7cfe3c4b70491c4ababcaa67ae4264f41 (diff) | |
Add IOrganizer::virtualFileTree().
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/organizercore.cpp | 3 | ||||
| -rw-r--r-- | src/organizercore.h | 2 | ||||
| -rw-r--r-- | src/organizerproxy.cpp | 5 | ||||
| -rw-r--r-- | src/organizerproxy.h | 5 | ||||
| -rw-r--r-- | src/virtualfiletree.cpp | 59 | ||||
| -rw-r--r-- | src/virtualfiletree.h | 60 |
7 files changed, 133 insertions, 2 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7f8da6f2..7f1f642a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,7 @@ add_filter(NAME src/core GROUPS apiuseraccount processrunner qdirfiletree + virtualfiletree uilocker ) diff --git a/src/organizercore.cpp b/src/organizercore.cpp index e47a6e30..7cf3212a 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -32,6 +32,7 @@ #include "envmodule.h" #include "envfs.h" #include "directoryrefresher.h" +#include "virtualfiletree.h" #include "shared/directoryentry.h" #include "shared/filesorigin.h" #include "shared/fileentry.h" @@ -97,6 +98,7 @@ OrganizerCore::OrganizerCore(Settings &settings) , m_PluginList(*this) , m_DirectoryRefresher(new DirectoryRefresher(settings.refreshThreadCount())) , m_DirectoryStructure(new DirectoryEntry(L"data", nullptr, 0)) + , m_VirtualFileTree([this]() { return VirtualFileTree::makeTree(m_DirectoryStructure); }) , m_DownloadManager(&NexusInterface::instance(), this) , m_DirectoryUpdate(false) , m_ArchivesInit(false) @@ -1521,6 +1523,7 @@ void OrganizerCore::directory_refreshed() } std::swap(m_DirectoryStructure, newStructure); + m_VirtualFileTree.invalidate(); if (m_StructureDeleter.joinable()) { m_StructureDeleter.join(); diff --git a/src/organizercore.h b/src/organizercore.h index e6274e36..c9e2bdb3 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -11,6 +11,7 @@ #include "executableslist.h"
#include "usvfsconnector.h"
#include "moshortcut.h"
+#include "memoizedlock.h"
#include "processrunner.h"
#include "uilocker.h"
#include "envdump.h"
@@ -476,6 +477,7 @@ private: std::unique_ptr<DirectoryRefresher> m_DirectoryRefresher;
MOShared::DirectoryEntry *m_DirectoryStructure;
+ MOBase::MemoizedLocked<std::shared_ptr<const MOBase::IFileTree>> m_VirtualFileTree;
DownloadManager m_DownloadManager;
InstallationManager m_InstallationManager;
diff --git a/src/organizerproxy.cpp b/src/organizerproxy.cpp index b416153b..e9fe61c2 100644 --- a/src/organizerproxy.cpp +++ b/src/organizerproxy.cpp @@ -272,6 +272,11 @@ QList<MOBase::IOrganizer::FileInfo> OrganizerProxy::findFileInfos(const QString return m_Proxied->findFileInfos(path, filter);
}
+std::shared_ptr<const MOBase::IFileTree> OrganizerProxy::virtualFileTree() const
+{
+ return m_Proxied->m_VirtualFileTree.value();
+}
+
MOBase::IDownloadManager *OrganizerProxy::downloadManager() const
{
return m_DownloadManagerProxy.get();
diff --git a/src/organizerproxy.h b/src/organizerproxy.h index 7a91a347..f419ba59 100644 --- a/src/organizerproxy.h +++ b/src/organizerproxy.h @@ -49,8 +49,9 @@ public: // IOrganizer interface virtual QStringList listDirectories(const QString &directoryName) const;
virtual QStringList findFiles(const QString &path, const std::function<bool(const QString &)> &filter) const override;
virtual QStringList findFiles(const QString &path, const QStringList &globFilters) const override;
- virtual QStringList getFileOrigins(const QString &fileName) const;
- virtual QList<FileInfo> findFileInfos(const QString &path, const std::function<bool(const FileInfo&)> &filter) const;
+ virtual QStringList getFileOrigins(const QString &fileName) const override;
+ virtual QList<FileInfo> findFileInfos(const QString &path, const std::function<bool(const FileInfo&)> &filter) const override;
+ virtual std::shared_ptr<const MOBase::IFileTree> virtualFileTree() const override;
virtual MOBase::IDownloadManager *downloadManager() const;
virtual MOBase::IPluginList *pluginList() const;
diff --git a/src/virtualfiletree.cpp b/src/virtualfiletree.cpp new file mode 100644 index 00000000..983523e6 --- /dev/null +++ b/src/virtualfiletree.cpp @@ -0,0 +1,59 @@ +#include "virtualfiletree.h" + +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" +#include "shared/fileentry.h" +// #include "shared/util.h" + +using namespace MOBase; +using namespace MOShared; + +class VirtualFileTreeImpl : public VirtualFileTree { +public: + + + /** + * + */ + VirtualFileTreeImpl(std::shared_ptr<const IFileTree> parent, const DirectoryEntry* dir) : + FileTreeEntry(parent, parent ? QString::fromStdWString(dir->getName()) : ""), + VirtualFileTree(), + m_dirEntry(dir) { } + +protected: + + /** + * No mutable operations allowed. + */ + bool beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) override { return false; } + bool beforeInsert(IFileTree const* entry, FileTreeEntry const* name) override { return false; } + bool beforeRemove(IFileTree const* entry, FileTreeEntry const* name) override { return false; } + std::shared_ptr<FileTreeEntry> makeFile(std::shared_ptr<const IFileTree> parent, QString name) const override { return nullptr; } + std::shared_ptr<IFileTree> makeDirectory(std::shared_ptr<const IFileTree> parent, QString name) const override { return nullptr; } + + bool doPopulate(std::shared_ptr<const IFileTree> parent, std::vector<std::shared_ptr<FileTreeEntry>>& entries) const override { + for (auto* subdirEntry : m_dirEntry->getSubDirectories()) { + entries.push_back(std::make_shared<VirtualFileTreeImpl>(parent, subdirEntry)); + } + for (auto& file : m_dirEntry->getFiles()) { + entries.push_back(createFileEntry(parent, QString::fromStdWString(file->getName()))); + } + + // Vector is already sorted: + return true; + } + + std::shared_ptr<IFileTree> doClone() const { + return std::make_shared<VirtualFileTreeImpl>(nullptr, m_dirEntry); + } + +private: + const DirectoryEntry* m_dirEntry; +}; + +/** + * + */ +std::shared_ptr<const VirtualFileTree> VirtualFileTree::makeTree(const DirectoryEntry* rootEntry) { + return std::make_shared<VirtualFileTreeImpl>(nullptr, rootEntry); +} diff --git a/src/virtualfiletree.h b/src/virtualfiletree.h new file mode 100644 index 00000000..ebce5486 --- /dev/null +++ b/src/virtualfiletree.h @@ -0,0 +1,60 @@ +/* +Copyright (C) MO2 Team. All rights reserved. + +This file is part of Mod Organizer. + +Mod Organizer is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Mod Organizer is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef ARCHIVEFILENETRY_H +#define ARCHIVEFILENTRY_H + +#include <QDir> + +#include "ifiletree.h" + +namespace MOShared +{ + class DirectoryEntry; +} + +/** + * @brief Class that expose the VFS main directory structure as a `MOBase::IFileTree`. + * + * The tree is lazily populated: each subtree is only populated when needed, + * as specified by IFileTree. + * + * This class does not expose mutable operations, so any mutable operations will + * fail. + */ +class VirtualFileTree : public MOBase::IFileTree { +public: + + /** + * @brief Create a new file tree representing the given VFS directory. + * + * @param root Root directory. + * + * @return a file tree representing the VFS directory. + */ + static std::shared_ptr<const VirtualFileTree> makeTree(const MOShared::DirectoryEntry* root); + +protected: + + using IFileTree::IFileTree; + + virtual bool doPopulate(std::shared_ptr<const IFileTree> parent, std::vector<std::shared_ptr<FileTreeEntry>>& entries) const = 0; +}; + +#endif |
