From ac3837164893c52c6e943843337659c2e0a8bd40 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Fri, 14 May 2021 14:40:15 +0200 Subject: Add IOrganizer::virtualFileTree(). --- src/CMakeLists.txt | 1 + src/organizercore.cpp | 3 +++ src/organizercore.h | 2 ++ src/organizerproxy.cpp | 5 +++++ src/organizerproxy.h | 5 +++-- src/virtualfiletree.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ src/virtualfiletree.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/virtualfiletree.cpp create mode 100644 src/virtualfiletree.h (limited to 'src') 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 m_DirectoryRefresher; MOShared::DirectoryEntry *m_DirectoryStructure; + MOBase::MemoizedLocked> 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 OrganizerProxy::findFileInfos(const QString return m_Proxied->findFileInfos(path, filter); } +std::shared_ptr 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 &filter) const override; virtual QStringList findFiles(const QString &path, const QStringList &globFilters) const override; - virtual QStringList getFileOrigins(const QString &fileName) const; - virtual QList findFileInfos(const QString &path, const std::function &filter) const; + virtual QStringList getFileOrigins(const QString &fileName) const override; + virtual QList findFileInfos(const QString &path, const std::function &filter) const override; + virtual std::shared_ptr 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 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 makeFile(std::shared_ptr parent, QString name) const override { return nullptr; } + std::shared_ptr makeDirectory(std::shared_ptr parent, QString name) const override { return nullptr; } + + bool doPopulate(std::shared_ptr parent, std::vector>& entries) const override { + for (auto* subdirEntry : m_dirEntry->getSubDirectories()) { + entries.push_back(std::make_shared(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 doClone() const { + return std::make_shared(nullptr, m_dirEntry); + } + +private: + const DirectoryEntry* m_dirEntry; +}; + +/** + * + */ +std::shared_ptr VirtualFileTree::makeTree(const DirectoryEntry* rootEntry) { + return std::make_shared(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 . +*/ + +#ifndef ARCHIVEFILENETRY_H +#define ARCHIVEFILENTRY_H + +#include + +#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 makeTree(const MOShared::DirectoryEntry* root); + +protected: + + using IFileTree::IFileTree; + + virtual bool doPopulate(std::shared_ptr parent, std::vector>& entries) const = 0; +}; + +#endif -- cgit v1.3.1