From 3423b0a59337cf4cf99a24a1421ea33c4c641a22 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 10 Feb 2020 14:53:04 -0500 Subject: threaded refresher --- src/directoryrefresher.h | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'src/directoryrefresher.h') diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index a4fc5dbc..c531ba39 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -39,12 +39,23 @@ class DirectoryRefresher : public QObject Q_OBJECT public: + struct EntryInfo { + EntryInfo(const QString &modName, const QString &absolutePath, + const QStringList &stealFiles, const QStringList &archives, int priority) + : modName(modName), absolutePath(absolutePath), stealFiles(stealFiles) + , archives(archives), priority(priority) {} + QString modName; + QString absolutePath; + QStringList stealFiles; + QStringList archives; + int priority; + }; /** * @brief constructor * **/ - DirectoryRefresher(); + DirectoryRefresher(std::size_t threadCount); ~DirectoryRefresher(); @@ -53,7 +64,7 @@ public: * * returns a pointer to the updated directory structure. DirectoryRefresher * deletes its own pointer and the caller takes custody of the pointer - * + * * @return updated directory structure **/ MOShared::DirectoryEntry *getDirectoryStructure(); @@ -107,7 +118,13 @@ public: * @param directory * @param stealFiles */ - void addModFilesToStructure(MOShared::DirectoryEntry *directoryStructure, const QString &modName, int priority, const QString &directory, const QStringList &stealFiles); + void addModFilesToStructure( + MOShared::DirectoryEntry *directoryStructure, const QString &modName, + int priority, const QString &directory, const QStringList &stealFiles); + + void addMultipleModsFilesToStructure( + MOShared::DirectoryEntry *directoryStructure, + const std::vector& entries, bool emitProgress=false); public slots: @@ -123,26 +140,15 @@ signals: void refreshed(); private: - - struct EntryInfo { - EntryInfo(const QString &modName, const QString &absolutePath, - const QStringList &stealFiles, const QStringList &archives, int priority) - : modName(modName), absolutePath(absolutePath), stealFiles(stealFiles) - , archives(archives), priority(priority) {} - QString modName; - QString absolutePath; - QStringList stealFiles; - QStringList archives; - int priority; - }; - -private: - std::vector m_Mods; std::set m_EnabledArchives; MOShared::DirectoryEntry *m_DirectoryStructure; QMutex m_RefreshLock; + std::size_t m_threadCount; + void stealModFilesIntoStructure( + MOShared::DirectoryEntry *directoryStructure, const QString &modName, + int priority, const QString &directory, const QStringList &stealFiles); }; #endif // DIRECTORYREFRESHER_H -- cgit v1.3.1 From 567fe019ac21ee78a07beae1a387ab5688e7f97b Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 14 Feb 2020 01:53:59 -0500 Subject: removed some unnecessary counters delete old structure in thread --- src/directoryrefresher.cpp | 69 ++++++++++++++++++------------------------- src/directoryrefresher.h | 2 +- src/organizercore.cpp | 68 +++++++++++++++++++++++++++++++----------- src/organizercore.h | 2 ++ src/shared/directoryentry.cpp | 48 ------------------------------ src/shared/directoryentry.h | 3 -- 6 files changed, 82 insertions(+), 110 deletions(-) (limited to 'src/directoryrefresher.h') diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index a59ddf9d..cb5bfc5b 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -48,7 +48,7 @@ DirectoryRefresher::~DirectoryRefresher() delete m_DirectoryStructure; } -DirectoryEntry *DirectoryRefresher::getDirectoryStructure() +DirectoryEntry *DirectoryRefresher::stealDirectoryStructure() { QMutexLocker locker(&m_RefreshLock); DirectoryEntry *result = m_DirectoryStructure; @@ -279,58 +279,50 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( g_threads.setMax(m_threadCount); - { - TimeThis tt("walk dirs"); - - for (std::size_t i=0; i(i + 1); + for (std::size_t i=0; i(i + 1); - stats[i].mod = entries[i].modName.toStdString(); + stats[i].mod = entries[i].modName.toStdString(); - try - { - if (e.stealFiles.length() > 0) { - stealModFilesIntoStructure( - directoryStructure, e.modName, prio, e.absolutePath, e.stealFiles); - } else { - auto& mt = g_threads.request(); - - mt.ds = directoryStructure; - mt.modName = entries[i].modName.toStdWString(); - mt.path = QDir::toNativeSeparators(e.absolutePath).toStdWString(); - mt.prio = prio; - mt.stats = &stats[i]; - - mt.wakeup(); - } - } catch (const std::exception& ex) { - emit error(tr("failed to read mod (%1): %2").arg(e.modName, ex.what())); - } - - if (emitProgress) { - emit progress((static_cast(i) * 100) / static_cast(entries.size()) + 1); + try + { + if (e.stealFiles.length() > 0) { + stealModFilesIntoStructure( + directoryStructure, e.modName, prio, e.absolutePath, e.stealFiles); + } else { + auto& mt = g_threads.request(); + + mt.ds = directoryStructure; + mt.modName = entries[i].modName.toStdWString(); + mt.path = QDir::toNativeSeparators(e.absolutePath).toStdWString(); + mt.prio = prio; + mt.stats = &stats[i]; + + mt.wakeup(); } + } catch (const std::exception& ex) { + emit error(tr("failed to read mod (%1): %2").arg(e.modName, ex.what())); } - g_threads.waitForAll(); + if (emitProgress) { + emit progress((static_cast(i) * 100) / static_cast(entries.size()) + 1); + } } + g_threads.waitForAll(); + dumpStats(stats); } -namespace MOShared{ void logcounts(std::string w); } - void DirectoryRefresher::refresh() { SetThisThreadName("DirectoryRefresher"); + TimeThis tt("refresh"); for (int i=0; i<1; ++i) { QMutexLocker locker(&m_RefreshLock); - - //logcounts("before delete"); delete m_DirectoryStructure; - //logcounts("after delete"); m_DirectoryStructure = new DirectoryEntry(L"data", nullptr, 0); @@ -355,9 +347,6 @@ void DirectoryRefresher::refresh() cleanStructure(m_DirectoryStructure); } - emit progress(100); - + emit progress(100); emit refreshed(); - - //logcounts("after refresh"); } diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index c531ba39..18bfed4d 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -67,7 +67,7 @@ public: * * @return updated directory structure **/ - MOShared::DirectoryEntry *getDirectoryStructure(); + MOShared::DirectoryEntry* stealDirectoryStructure(); /** * @brief sets up the mods to be included in the directory structure diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 86abeb35..a3202153 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -144,6 +144,10 @@ OrganizerCore::~OrganizerCore() m_RefresherThread.exit(); m_RefresherThread.wait(); + if (m_StructureDeleter.joinable()) { + m_StructureDeleter.join(); + } + saveCurrentProfile(); // profile has to be cleaned up before the modinfo-buffer is cleared @@ -1379,46 +1383,74 @@ std::vector OrganizerCore::enabledArchives() void OrganizerCore::refreshDirectoryStructure() { - if (!m_DirectoryUpdate) { - m_CurrentProfile->writeModlistNow(true); + if (m_DirectoryUpdate) { + log::debug("can't refresh, already in progress"); + return; + } - m_DirectoryUpdate = true; - std::vector> activeModList - = m_CurrentProfile->getActiveMods(); - auto archives = enabledArchives(); - m_DirectoryRefresher.setMods( - activeModList, std::set(archives.begin(), archives.end())); + log::debug("refreshing structure"); + m_DirectoryUpdate = true; - QTimer::singleShot(0, &m_DirectoryRefresher, SLOT(refresh())); - } + m_CurrentProfile->writeModlistNow(true); + const auto activeModList = m_CurrentProfile->getActiveMods(); + const auto archives = enabledArchives(); + + m_DirectoryRefresher.setMods( + activeModList, std::set(archives.begin(), archives.end())); + + // runs refresh() in a thread + QTimer::singleShot(0, &m_DirectoryRefresher, SLOT(refresh())); } void OrganizerCore::directory_refreshed() { - DirectoryEntry *newStructure = m_DirectoryRefresher.getDirectoryStructure(); + log::debug("structure refreshed"); + + DirectoryEntry *newStructure = m_DirectoryRefresher.stealDirectoryStructure(); Q_ASSERT(newStructure != m_DirectoryStructure); - if (newStructure != nullptr) { - std::swap(m_DirectoryStructure, newStructure); - delete newStructure; - } else { + + if (newStructure == nullptr) { // TODO: don't know why this happens, this slot seems to get called twice // with only one emit return; } + + std::swap(m_DirectoryStructure, newStructure); + + if (m_StructureDeleter.joinable()) { + m_StructureDeleter.join(); + } + + m_StructureDeleter = std::thread([=]{ + log::debug("structure deleter thread start"); + delete newStructure; + log::debug("structure deleter thread done"); + }); + m_DirectoryUpdate = false; + log::debug("clearing caches"); for (int i = 0; i < m_ModList.rowCount(); ++i) { ModInfo::Ptr modInfo = ModInfo::getByIndex(i); modInfo->clearCaches(); } - for (auto task : m_PostRefreshTasks) { - task(); + + if (!m_PostRefreshTasks.empty()) { + log::debug("running {} post refresh tasks", m_PostRefreshTasks.size()); + + for (auto task : m_PostRefreshTasks) { + task(); + } + + m_PostRefreshTasks.clear(); } - m_PostRefreshTasks.clear(); if (m_CurrentProfile != nullptr) { + log::debug("refreshing lists"); refreshLists(); } + + log::debug("refresh done"); } void OrganizerCore::profileRefresh() diff --git a/src/organizercore.h b/src/organizercore.h index a63dc959..223eb6cb 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -346,6 +346,8 @@ private: QThread m_RefresherThread; + std::thread m_StructureDeleter; + bool m_DirectoryUpdate; bool m_ArchivesInit; bool m_ArchiveParsing{ m_Settings.archiveParsing() }; diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 4e64d1c1..c5702026 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -40,13 +40,6 @@ namespace MOShared using namespace MOBase; static const int MAXPATH_UNICODE = 32767; - -static std::atomic FileEntryCount(0); -static std::atomic FilesOriginCount(0); -static std::atomic FileRegisterCount(0); -static std::atomic DirectoryEntryCount(0); -static std::atomic OriginConnectionCount(0); - template std::chrono::nanoseconds elapsed(F&& f) { @@ -56,13 +49,6 @@ std::chrono::nanoseconds elapsed(F&& f) return (end - start); } -void logcounts(std::string w) -{ - log::debug( - "{}: FileEntry={} FilesOrigin={} FileRegister={} DirectoryEntry={} OriginConnection={}", - w, FileEntryCount, FilesOriginCount, FileRegisterCount, DirectoryEntryCount, OriginConnectionCount); -} - static std::wstring tail(const std::wstring &source, const size_t count) { @@ -203,12 +189,6 @@ public: OriginConnection() : m_NextID(0) { - ++OriginConnectionCount; - } - - ~OriginConnection() - { - --OriginConnectionCount; } std::pair getOrCreate( @@ -350,7 +330,6 @@ FileEntry::FileEntry() : m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize), m_LastAccessed(time(nullptr)) { - ++FileEntryCount; } FileEntry::FileEntry(Index index, std::wstring name, DirectoryEntry *parent) : @@ -358,16 +337,6 @@ FileEntry::FileEntry(Index index, std::wstring name, DirectoryEntry *parent) : m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize), m_LastAccessed(time(nullptr)) { - ++FileEntryCount; -} - -FileEntry::~FileEntry() -{ - while (!m_Alternatives.empty()) { - m_Alternatives.pop_back(); - } - - --FileEntryCount; } void FileEntry::addOrigin( @@ -606,7 +575,6 @@ bool FileEntry::recurseParents(std::wstring &path, const DirectoryEntry *parent) FilesOrigin::FilesOrigin() : m_ID(0), m_Disabled(false), m_Name(), m_Path(), m_Priority(0) { - ++FilesOriginCount; } FilesOrigin::FilesOrigin(const FilesOrigin &reference) @@ -618,7 +586,6 @@ FilesOrigin::FilesOrigin(const FilesOrigin &reference) , m_FileRegister(reference.m_FileRegister) , m_OriginConnection(reference.m_OriginConnection) { - ++FilesOriginCount; } FilesOrigin::FilesOrigin( @@ -629,12 +596,6 @@ FilesOrigin::FilesOrigin( m_Priority(priority), m_FileRegister(fileRegister), m_OriginConnection(originConnection) { - ++FilesOriginCount; -} - -FilesOrigin::~FilesOrigin() -{ - --FilesOriginCount; } void FilesOrigin::setPriority(int priority) @@ -733,12 +694,6 @@ bool FilesOrigin::containsArchive(std::wstring archiveName) FileRegister::FileRegister(boost::shared_ptr originConnection) : m_OriginConnection(originConnection) { - ++FileRegisterCount; -} - -FileRegister::~FileRegister() -{ - --FileRegisterCount; } bool FileRegister::indexValid(FileEntry::Index index) const @@ -907,7 +862,6 @@ DirectoryEntry::DirectoryEntry( m_OriginConnection(new OriginConnection), m_Name(std::move(name)), m_Parent(parent), m_Populated(false), m_TopLevel(true) { - ++DirectoryEntryCount; m_FileRegister.reset(new FileRegister(m_OriginConnection)); m_Origins.insert(originID); } @@ -919,13 +873,11 @@ DirectoryEntry::DirectoryEntry( m_FileRegister(fileRegister), m_OriginConnection(originConnection), m_Name(std::move(name)), m_Parent(parent), m_Populated(false), m_TopLevel(false) { - ++DirectoryEntryCount; m_Origins.insert(originID); } DirectoryEntry::~DirectoryEntry() { - --DirectoryEntryCount; clear(); } diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index 2a70d486..94ea068d 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -121,7 +121,6 @@ public: FileEntry(); FileEntry(Index index, std::wstring name, DirectoryEntry *parent); - ~FileEntry(); Index getIndex() const { @@ -237,7 +236,6 @@ class FilesOrigin public: FilesOrigin(); FilesOrigin(const FilesOrigin &reference); - ~FilesOrigin(); // sets priority for this origin, but it will overwrite the existing mapping // for this priority, the previous origin will no longer be referenced @@ -307,7 +305,6 @@ class FileRegister { public: FileRegister(boost::shared_ptr originConnection); - ~FileRegister(); bool indexValid(FileEntry::Index index) const; -- cgit v1.3.1 From fb3fbd34211a28b27214abe070c46951057258ca Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 14 Feb 2020 03:19:21 -0500 Subject: removed lastAccessed from FileEntry, not used FileRegister keeps a vector instead of a map --- src/directoryrefresher.cpp | 13 ++- src/directoryrefresher.h | 1 + src/shared/directoryentry.cpp | 178 ++++++++++++++++++++++-------------------- src/shared/directoryentry.h | 25 +++--- 4 files changed, 118 insertions(+), 99 deletions(-) (limited to 'src/directoryrefresher.h') diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index cb5bfc5b..f3285cfc 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -39,7 +39,7 @@ using namespace MOShared; DirectoryRefresher::DirectoryRefresher(std::size_t threadCount) - : m_DirectoryStructure(nullptr), m_threadCount(threadCount) + : m_DirectoryStructure(nullptr), m_threadCount(threadCount), m_lastFileCount(0) { } @@ -283,7 +283,9 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( const auto& e = entries[i]; const int prio = static_cast(i + 1); - stats[i].mod = entries[i].modName.toStdString(); + if constexpr (DirectoryStats::EnableInstrumentation) { + stats[i].mod = entries[i].modName.toStdString(); + } try { @@ -312,7 +314,9 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( g_threads.waitForAll(); - dumpStats(stats); + if constexpr (DirectoryStats::EnableInstrumentation) { + dumpStats(stats); + } } void DirectoryRefresher::refresh() @@ -325,6 +329,7 @@ void DirectoryRefresher::refresh() delete m_DirectoryStructure; m_DirectoryStructure = new DirectoryEntry(L"data", nullptr, 0); + m_DirectoryStructure->getFileRegister()->reserve(m_lastFileCount); IPluginGame *game = qApp->property("managed_game").value(); @@ -347,6 +352,8 @@ void DirectoryRefresher::refresh() cleanStructure(m_DirectoryStructure); } + m_lastFileCount = m_DirectoryStructure->getFileRegister()->highestCount(); + emit progress(100); emit refreshed(); } diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index 18bfed4d..5c829980 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -145,6 +145,7 @@ private: MOShared::DirectoryEntry *m_DirectoryStructure; QMutex m_RefreshLock; std::size_t m_threadCount; + std::size_t m_lastFileCount; void stealModFilesIntoStructure( MOShared::DirectoryEntry *directoryStructure, const QString &modName, diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index c5702026..0be5e870 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -38,17 +38,26 @@ namespace MOShared { using namespace MOBase; -static const int MAXPATH_UNICODE = 32767; +const int MAXPATH_UNICODE = 32767; template -std::chrono::nanoseconds elapsed(F&& f) +void elapsedImpl(std::chrono::nanoseconds& out, F&& f) { - const auto start = std::chrono::high_resolution_clock::now(); - f(); - const auto end = std::chrono::high_resolution_clock::now(); - return (end - start); + if constexpr (DirectoryStats::EnableInstrumentation) { + const auto start = std::chrono::high_resolution_clock::now(); + f(); + const auto end = std::chrono::high_resolution_clock::now(); + out += (end - start); + } else { + f(); + } } +// elapsed() is not optimized out when EnableInstrumentation is false even +// though it's equivalent that this macro +//#define elapsed(OUT, F) (F)(); +#define elapsed(OUT, F) elapsedImpl(OUT, F); + static std::wstring tail(const std::wstring &source, const size_t count) { @@ -327,15 +336,13 @@ private: FileEntry::FileEntry() : m_Index(UINT_MAX), m_Name(), m_Origin(-1), m_Parent(nullptr), - m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize), - m_LastAccessed(time(nullptr)) + m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) { } FileEntry::FileEntry(Index index, std::wstring name, DirectoryEntry *parent) : m_Index(index), m_Name(std::move(name)), m_Origin(-1), m_Archive(L"", -1), m_Parent(parent), - m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize), - m_LastAccessed(time(nullptr)) + m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) { } @@ -344,7 +351,6 @@ void FileEntry::addOrigin( { std::scoped_lock lock(m_OriginsMutex); - m_LastAccessed = time(nullptr); if (m_Parent != nullptr) { m_Parent->propagateOrigin(origin); } @@ -639,13 +645,13 @@ FileEntry::Ptr FilesOrigin::findFile(FileEntry::Index index) const return m_FileRegister.lock()->getFile(index); } -void FilesOrigin::enable(bool enabled, time_t notAfter) +void FilesOrigin::enable(bool enabled) { DirectoryStats dummy; - enable(enabled, dummy, notAfter); + enable(enabled, dummy); } -void FilesOrigin::enable(bool enabled, DirectoryStats& stats, time_t notAfter) +void FilesOrigin::enable(bool enabled, DirectoryStats& stats) { if (!enabled) { ++stats.originsNeededEnabled; @@ -658,7 +664,7 @@ void FilesOrigin::enable(bool enabled, DirectoryStats& stats, time_t notAfter) m_Files.clear(); } - m_FileRegister.lock()->removeOriginMulti(copy, m_ID, notAfter); + m_FileRegister.lock()->removeOriginMulti(copy, m_ID); } m_Disabled = !enabled; @@ -692,57 +698,53 @@ bool FilesOrigin::containsArchive(std::wstring archiveName) FileRegister::FileRegister(boost::shared_ptr originConnection) - : m_OriginConnection(originConnection) + : m_OriginConnection(originConnection), m_NextIndex(0) { } bool FileRegister::indexValid(FileEntry::Index index) const { std::scoped_lock lock(m_Mutex); - return (m_Files.find(index) != m_Files.end()); + + if (index < m_Files.size()) { + return (m_Files[index].get() != nullptr); + } + + return false; } FileEntry::Ptr FileRegister::createFile( std::wstring name, DirectoryEntry *parent, DirectoryStats& stats) { const auto index = generateIndex(); - FileEntry::Ptr p; + auto p = FileEntry::Ptr(new FileEntry(index, std::move(name), parent)); - stats.addFileToRegisterTimes += elapsed([&]{ - bool inserted = false; - p = FileEntry::Ptr(new FileEntry(index, std::move(name), parent)); + { + std::scoped_lock lock(m_Mutex); - { - std::scoped_lock lock(m_Mutex); - inserted = m_Files.insert_or_assign(index, p).second; + if (index >= m_Files.size()) { + m_Files.resize(index + 1); } - if (inserted) { - ++stats.filesInsertedInRegister; - } else { - ++stats.filesAssignedInRegister; - } - }); + m_Files[index] = p; + } return p; } FileEntry::Index FileRegister::generateIndex() { - static std::atomic sIndex(0); - return sIndex++; + return m_NextIndex++; } FileEntry::Ptr FileRegister::getFile(FileEntry::Index index) const { std::scoped_lock lock(m_Mutex); - auto iter = m_Files.find(index); - - if (iter != m_Files.end()) { - return iter->second; + if (index < m_Files.size()) { + return m_Files[index]; } else { - return FileEntry::Ptr(); + return {}; } } @@ -750,37 +752,42 @@ bool FileRegister::removeFile(FileEntry::Index index) { std::scoped_lock lock(m_Mutex); - auto iter = m_Files.find(index); + if (index < m_Files.size()) { + FileEntry::Ptr p; + m_Files[index].swap(p); - if (iter != m_Files.end()) { - unregisterFile(iter->second); - m_Files.erase(index); - return true; - } else { - log::error(QObject::tr("invalid file index for remove: {}").toStdString(), index); - return false; + if (p) { + unregisterFile(p); + return true; + } } + + log::error(QObject::tr("invalid file index for remove: {}").toStdString(), index); + return false; } void FileRegister::removeOrigin(FileEntry::Index index, int originID) { std::unique_lock lock(m_Mutex); - auto iter = m_Files.find(index); + if (index < m_Files.size()) { + FileEntry::Ptr& p = m_Files[index]; - if (iter != m_Files.end()) { - if (iter->second->removeOrigin(originID)) { - m_Files.erase(iter); - lock.unlock(); - unregisterFile(iter->second); + if (p) { + if (p->removeOrigin(originID)) { + m_Files[index] = {}; + lock.unlock(); + unregisterFile(p); + return; + } } - } else { - log::error(QObject::tr("invalid file index for remove (for origin): {}").toStdString(), index); } + + log::error(QObject::tr("invalid file index for remove (for origin): {}").toStdString(), index); } void FileRegister::removeOriginMulti( - std::set indices, int originID, time_t notAfter) + std::set indices, int originID) { std::vector removedFiles; @@ -788,17 +795,20 @@ void FileRegister::removeOriginMulti( std::scoped_lock lock(m_Mutex); for (auto iter = indices.begin(); iter != indices.end(); ) { - auto pos = m_Files.find(*iter); - - if (pos != m_Files.end() - && (pos->second->lastAccessed() < notAfter) - && pos->second->removeOrigin(originID)) { - removedFiles.push_back(pos->second); - m_Files.erase(pos); - ++iter; - } else { - indices.erase(iter++); + const auto index = *iter; + + if (index < m_Files.size()) { + const auto& p = m_Files[index]; + + if (p && p->removeOrigin(originID)) { + removedFiles.push_back(p); + m_Files[index] = {}; + ++iter; + continue; + } } + + iter = indices.erase(iter); } } @@ -832,8 +842,8 @@ void FileRegister::sortOrigins() { std::scoped_lock lock(m_Mutex); - for (auto iter = m_Files.begin(); iter != m_Files.end(); ++iter) { - iter->second->sortOrigins(); + for (auto&& p : m_Files) { + p->sortOrigins(); } } @@ -927,20 +937,20 @@ void DirectoryEntry::addFromList( void DirectoryEntry::addDir( FilesOrigin& origin, env::Directory& d, DirectoryStats& stats) { - stats.dirTimes += elapsed([&]{ + elapsed(stats.dirTimes, [&]{ for (auto& sd : d.dirs) { auto* sdirEntry = getSubDirectory(sd, true, stats, origin.getID()); sdirEntry->addDir(origin, sd, stats); } }); - stats.fileTimes += elapsed([&]{ + elapsed(stats.fileTimes, [&]{ for (auto& f : d.files) { insert(f, origin, L"", -1, stats); } }); - stats.sortTimes += elapsed([&]{ + elapsed(stats.sortTimes, [&]{ std::sort( m_SubDirectories.begin(), m_SubDirectories.end(), @@ -1293,7 +1303,7 @@ FileEntry::Ptr DirectoryEntry::insert( FilesLookup::iterator itor; - stats.filesLookupTimes += elapsed([&]{ + elapsed(stats.filesLookupTimes, [&]{ itor = m_FilesLookup.find(key); }); @@ -1306,7 +1316,7 @@ FileEntry::Ptr DirectoryEntry::insert( fe = m_FileRegister->createFile( std::wstring(fileName.begin(), fileName.end()), this, stats); - stats.addFileTimes += elapsed([&] { + elapsed(stats.addFileTimes, [&] { addFileToList(std::move(key.value), fe->getIndex()); }); @@ -1314,11 +1324,11 @@ FileEntry::Ptr DirectoryEntry::insert( } } - stats.addOriginToFileTimes += elapsed([&]{ + elapsed(stats.addOriginToFileTimes, [&]{ fe->addOrigin(origin.getID(), fileTime, archive, order); }); - stats.addFileToOriginTimes += elapsed([&]{ + elapsed(stats.addFileToOriginTimes, [&]{ origin.addFile(fe->getIndex()); }); @@ -1336,7 +1346,7 @@ FileEntry::Ptr DirectoryEntry::insert( FilesMap::iterator itor; - stats.filesLookupTimes += elapsed([&]{ + elapsed(stats.filesLookupTimes, [&]{ itor = m_Files.find(file.lcname); }); @@ -1349,7 +1359,7 @@ FileEntry::Ptr DirectoryEntry::insert( fe = m_FileRegister->createFile(std::move(file.name), this, stats); // file.name has been moved from this point - stats.addFileTimes += elapsed([&]{ + elapsed(stats.addFileTimes, [&]{ addFileToList(std::move(file.lcname), fe->getIndex()); }); @@ -1357,11 +1367,11 @@ FileEntry::Ptr DirectoryEntry::insert( } } - stats.addOriginToFileTimes += elapsed([&]{ + elapsed(stats.addOriginToFileTimes, [&]{ fe->addOrigin(origin.getID(), file.lastModified, archive, order); }); - stats.addFileToOriginTimes += elapsed([&]{ + elapsed(stats.addFileToOriginTimes, [&]{ origin.addFile(fe->getIndex()); }); @@ -1386,7 +1396,7 @@ void DirectoryEntry::addFiles( [](void* pcx, std::wstring_view path) { Context* cx = (Context*)pcx; - cx->stats.dirTimes += elapsed([&] { + elapsed(cx->stats.dirTimes, [&] { auto* sd = cx->current.top()->getSubDirectory( path, true, cx->stats, cx->origin.getID()); @@ -1398,7 +1408,7 @@ void DirectoryEntry::addFiles( { Context* cx = (Context*)pcx; - cx->stats.dirTimes += elapsed([&] { + elapsed(cx->stats.dirTimes, [&] { auto* current= cx->current.top(); { @@ -1418,7 +1428,7 @@ void DirectoryEntry::addFiles( { Context* cx = (Context*)pcx; - cx->stats.fileTimes += elapsed([&]{ + elapsed(cx->stats.fileTimes, [&]{ cx->current.top()->insert(path, cx->origin, ft, L"", -1, cx->stats); }); } @@ -1466,7 +1476,7 @@ DirectoryEntry *DirectoryEntry::getSubDirectory( std::scoped_lock lock(m_SubDirMutex); SubDirectoriesLookup::iterator itor; - stats.subdirLookupTimes += elapsed([&] { + elapsed(stats.subdirLookupTimes, [&] { itor = m_SubDirectoriesLookup.find(nameLc); }); @@ -1482,7 +1492,7 @@ DirectoryEntry *DirectoryEntry::getSubDirectory( std::wstring(name.begin(), name.end()), this, originID, m_FileRegister, m_OriginConnection); - stats.addDirectoryTimes += elapsed([&] { + elapsed(stats.addDirectoryTimes, [&] { addDirectoryToList(entry, std::move(nameLc)); // nameLc is moved from this point }); @@ -1498,7 +1508,7 @@ DirectoryEntry *DirectoryEntry::getSubDirectory( { SubDirectoriesLookup::iterator itor; - stats.subdirLookupTimes += elapsed([&] { + elapsed(stats.subdirLookupTimes, [&] { itor = m_SubDirectoriesLookup.find(dir.lcname); }); @@ -1515,7 +1525,7 @@ DirectoryEntry *DirectoryEntry::getSubDirectory( m_FileRegister, m_OriginConnection); // dir.name is moved from this point - stats.addDirectoryTimes += elapsed([&]{ + elapsed(stats.addDirectoryTimes, [&]{ addDirectoryToList(entry, std::move(dir.lcname)); }); diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index 94ea068d..74d50625 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -64,6 +64,8 @@ class FileRegister; struct DirectoryStats { + static constexpr bool EnableInstrumentation = true; + std::string mod; std::chrono::nanoseconds dirTimes; @@ -127,11 +129,6 @@ public: return m_Index; } - time_t lastAccessed() const - { - return m_LastAccessed; - } - void addOrigin( int origin, FILETIME fileTime, std::wstring_view archive, int order); @@ -222,8 +219,6 @@ private: uint64_t m_FileSize, m_CompressedFileSize; mutable std::mutex m_OriginsMutex; - time_t m_LastAccessed; - bool recurseParents(std::wstring &path, const DirectoryEntry *parent) const; }; @@ -265,8 +260,8 @@ public: std::vector getFiles() const; FileEntry::Ptr findFile(FileEntry::Index index) const; - void enable(bool enabled, DirectoryStats& stats, time_t notAfter = LONG_MAX); - void enable(bool enabled, time_t notAfter = LONG_MAX); + void enable(bool enabled, DirectoryStats& stats); + void enable(bool enabled); bool isDisabled() const { @@ -313,24 +308,30 @@ public: FileEntry::Ptr getFile(FileEntry::Index index) const; - size_t size() const + size_t highestCount() const { std::scoped_lock lock(m_Mutex); return m_Files.size(); } + void reserve(std::size_t n) + { + m_Files.reserve(n); + } + bool removeFile(FileEntry::Index index); void removeOrigin(FileEntry::Index index, int originID); - void removeOriginMulti(std::set indices, int originID, time_t notAfter); + void removeOriginMulti(std::set indices, int originID); void sortOrigins(); private: - using FileMap = std::map; + using FileMap = std::vector; mutable std::mutex m_Mutex; FileMap m_Files; boost::shared_ptr m_OriginConnection; + std::atomic m_NextIndex; void unregisterFile(FileEntry::Ptr file); FileEntry::Index generateIndex(); -- cgit v1.3.1 From b1cf498924e461556c8f2fe961172685d92bb03f Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sat, 15 Feb 2020 13:24:56 -0500 Subject: split directoryentry made classes noncopyable, fixed a few unintended copies --- src/CMakeLists.txt | 21 +- src/datatab.cpp | 1 - src/directoryrefresher.cpp | 11 +- src/directoryrefresher.h | 5 +- src/filetree.cpp | 3 + src/filetreeitem.h | 6 +- src/filetreemodel.cpp | 12 +- src/filetreemodel.h | 6 +- src/loglist.h | 3 +- src/main.cpp | 1 + src/mainwindow.cpp | 14 +- src/modinfodialog.cpp | 2 + src/modinfodialogconflicts.cpp | 21 +- src/modinfodialogconflicts.h | 14 +- src/modinfodialogtab.cpp | 2 +- src/modinfowithconflictinfo.cpp | 11 +- src/modlist.cpp | 8 +- src/modlist.h | 1 - src/organizercore.cpp | 7 +- src/organizercore.h | 1 - src/pch.h | 4 + src/pluginlist.cpp | 16 +- src/pluginlist.h | 1 - src/shared/directoryentry.cpp | 745 ++-------------------------------------- src/shared/directoryentry.h | 368 ++------------------ src/shared/fileentry.cpp | 251 ++++++++++++++ src/shared/fileentry.h | 125 +++++++ src/shared/fileregister.cpp | 184 ++++++++++ src/shared/fileregister.h | 58 ++++ src/shared/fileregisterfwd.h | 89 +++++ src/shared/filesorigin.cpp | 137 ++++++++ src/shared/filesorigin.h | 87 +++++ src/shared/originconnection.cpp | 145 ++++++++ src/shared/originconnection.h | 59 ++++ src/syncoverwritedialog.cpp | 7 +- src/syncoverwritedialog.h | 11 +- 36 files changed, 1310 insertions(+), 1127 deletions(-) create mode 100644 src/shared/fileentry.cpp create mode 100644 src/shared/fileentry.h create mode 100644 src/shared/fileregister.cpp create mode 100644 src/shared/fileregister.h create mode 100644 src/shared/fileregisterfwd.h create mode 100644 src/shared/filesorigin.cpp create mode 100644 src/shared/filesorigin.h create mode 100644 src/shared/originconnection.cpp create mode 100644 src/shared/originconnection.h (limited to 'src/directoryrefresher.h') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e121c572..85d8af0f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -155,6 +155,10 @@ SET(organizer_SRCS shared/windows_error.cpp shared/error_report.cpp shared/directoryentry.cpp + shared/fileentry.cpp + shared/filesorigin.cpp + shared/fileregister.cpp + shared/originconnection.cpp shared/util.cpp shared/appconfig.cpp shared/leaktrace.cpp @@ -286,6 +290,10 @@ SET(organizer_HDRS shared/windows_error.h shared/error_report.h shared/directoryentry.h + shared/fileentry.h + shared/filesorigin.h + shared/fileregister.h + shared/originconnection.h shared/util.h shared/appconfig.h shared/appconfig.inc @@ -350,8 +358,6 @@ set(browser set(core categories - shared/directoryentry - directoryrefresher installationmanager instancemanager loadmechanism @@ -470,6 +476,15 @@ set(profiles profilesdialog ) +set(register + shared/directoryentry + shared/fileentry + shared/filesorigin + shared/fileregister + shared/originconnection + directoryrefresher +) + set(settings settings settingsutilities @@ -521,7 +536,7 @@ set(widgets set(src_filters application core browser dialogs downloads env executables loot mainwindow - modinfo modinfo\\dialog modlist plugins previews profiles settings + modinfo modinfo\\dialog modlist plugins previews profiles register settings settingsdialog utilities widgets ) diff --git a/src/datatab.cpp b/src/datatab.cpp index 19bfa134..b6119d52 100644 --- a/src/datatab.cpp +++ b/src/datatab.cpp @@ -2,7 +2,6 @@ #include "ui_mainwindow.h" #include "settings.h" #include "organizercore.h" -#include "directoryentry.h" #include "messagedialog.h" #include "filetree.h" #include "filetreemodel.h" diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index 8bf349f1..3b92389a 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -18,6 +18,9 @@ along with Mod Organizer. If not, see . */ #include "directoryrefresher.h" +#include "shared/fileentry.h" +#include "shared/filesorigin.h" +#include "shared/directoryentry.h" #include "iplugingame.h" #include "utility.h" @@ -26,12 +29,16 @@ along with Mod Organizer. If not, see . #include "settings.h" #include "envfs.h" #include "modinfodialogfwd.h" +#include "util.h" + +#include #include #include #include #include -#include + +#include using namespace MOBase; @@ -136,7 +143,7 @@ void DirectoryRefresher::stealModFilesIntoStructure( continue; } QFileInfo fileInfo(filename); - FileEntry::Ptr file = directoryStructure->findFile(ToWString(fileInfo.fileName())); + FileEntryPtr file = directoryStructure->findFile(ToWString(fileInfo.fileName())); if (file.get() != nullptr) { if (file->getOrigin() == 0) { // replace data as the origin on this bsa diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index 5c829980..2102140c 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -20,7 +20,6 @@ along with Mod Organizer. If not, see . #ifndef DIRECTORYREFRESHER_H #define DIRECTORYREFRESHER_H -#include #include #include #include @@ -59,6 +58,10 @@ public: ~DirectoryRefresher(); + // noncopyable + DirectoryRefresher(const DirectoryRefresher&) = delete; + DirectoryRefresher& operator=(const DirectoryRefresher&) = delete; + /** * @brief retrieve the updated directory structure * diff --git a/src/filetree.cpp b/src/filetree.cpp index f628dff3..cdcf2feb 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -3,6 +3,9 @@ #include "filetreeitem.h" #include "organizercore.h" #include "envshell.h" +#include "shared/fileentry.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" #include #include diff --git a/src/filetreeitem.h b/src/filetreeitem.h index 390d5499..2092782e 100644 --- a/src/filetreeitem.h +++ b/src/filetreeitem.h @@ -1,7 +1,7 @@ #ifndef MODORGANIZER_FILETREEITEM_INCLUDED #define MODORGANIZER_FILETREEITEM_INCLUDED -#include "directoryentry.h" +#include "shared/fileregisterfwd.h" #include class FileTreeModel; @@ -126,7 +126,7 @@ public: return m_wsLcFile; } - const MOShared::DirectoryEntry::FileKey& key() const + const MOShared::DirectoryEntryFileKey& key() const { return m_key; } @@ -288,7 +288,7 @@ private: const QString m_virtualParentPath; const std::wstring m_wsFile, m_wsLcFile; - const MOShared::DirectoryEntry::FileKey m_key; + const MOShared::DirectoryEntryFileKey m_key; const QString m_file; const bool m_isDirectory; diff --git a/src/filetreemodel.cpp b/src/filetreemodel.cpp index 2130bf89..1a5433c5 100644 --- a/src/filetreemodel.cpp +++ b/src/filetreemodel.cpp @@ -1,5 +1,9 @@ #include "filetreemodel.h" #include "organizercore.h" +#include "filesorigin.h" +#include "util.h" +#include "shared/directoryentry.h" +#include "shared/fileentry.h" #include using namespace MOBase; @@ -637,7 +641,7 @@ bool FileTreeModel::updateFiles( // removeDisappearingFiles() will add files that are in the tree and still on // the filesystem to this set; addNewFiless() will use this to figure out if // a file is new or not - std::unordered_set seen; + std::unordered_set seen; int firstFileRow = 0; @@ -647,7 +651,7 @@ bool FileTreeModel::updateFiles( void FileTreeModel::removeDisappearingFiles( FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry, - int& firstFileRow, std::unordered_set& seen) + int& firstFileRow, std::unordered_set& seen) { auto& children = parentItem.children(); auto itor = children.begin(); @@ -708,7 +712,7 @@ void FileTreeModel::removeDisappearingFiles( bool FileTreeModel::addNewFiles( FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry, const std::wstring& parentPath, const int firstFileRow, - const std::unordered_set& seen) + const std::unordered_set& seen) { // keeps track of the contiguous files that need to be added to // avoid calling beginAddRows(), etc. for each item @@ -958,7 +962,7 @@ std::wstring FileTreeModel::makeModName( { static const std::wstring Unmanaged = UnmanagedModName().toStdWString(); - const auto origin = m_core.directoryStructure()->getOriginByID(originID); + const auto& origin = m_core.directoryStructure()->getOriginByID(originID); if (origin.getID() == 0) { return Unmanaged; diff --git a/src/filetreemodel.h b/src/filetreemodel.h index 093407b0..5bfb75aa 100644 --- a/src/filetreemodel.h +++ b/src/filetreemodel.h @@ -3,7 +3,7 @@ #include "filetreeitem.h" #include "iconfetcher.h" -#include "directoryentry.h" +#include "shared/fileregisterfwd.h" #include class OrganizerCore; @@ -126,12 +126,12 @@ private: void removeDisappearingFiles( FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry, - int& firstFileRow, std::unordered_set& seen); + int& firstFileRow, std::unordered_set& seen); bool addNewFiles( FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry, const std::wstring& parentPath, int firstFileRow, - const std::unordered_set& seen); + const std::unordered_set& seen); FileTreeItem::Ptr createDirectoryItem( diff --git a/src/loglist.h b/src/loglist.h index 56b95d65..b26f1561 100644 --- a/src/loglist.h +++ b/src/loglist.h @@ -20,8 +20,9 @@ along with Mod Organizer. If not, see . #ifndef LOGBUFFER_H #define LOGBUFFER_H -#include #include +#include +#include class OrganizerCore; diff --git a/src/main.cpp b/src/main.cpp index 6e2220ae..105299a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,7 @@ along with Mod Organizer. If not, see . #include "organizercore.h" #include "env.h" #include "envmodule.h" +#include "util.h" #include #include diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f9f1dfe5..adbac94e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -20,8 +20,6 @@ along with Mod Organizer. If not, see . #include "mainwindow.h" #include "ui_mainwindow.h" -#include "directoryentry.h" -#include "directoryrefresher.h" #include "executableinfo.h" #include "executableslist.h" #include "guessedvalue.h" @@ -89,6 +87,10 @@ along with Mod Organizer. If not, see . #include "envshortcut.h" #include "browserdialog.h" +#include "shared/directoryentry.h" +#include "shared/fileentry.h" +#include "shared/filesorigin.h" + #include #include #include @@ -1870,7 +1872,7 @@ void MainWindow::updateBSAList(const QStringList &defaultArchives, const QString std::vector> items; BSAInvalidation * invalidation = m_OrganizerCore.managedGame()->feature(); - std::vector files = m_OrganizerCore.directoryStructure()->getFiles(); + std::vector files = m_OrganizerCore.directoryStructure()->getFiles(); QStringList plugins = m_OrganizerCore.findFiles("", [](const QString &fileName) -> bool { return fileName.endsWith(".esp", Qt::CaseInsensitive) @@ -1889,7 +1891,7 @@ void MainWindow::updateBSAList(const QStringList &defaultArchives, const QString return false; }; - for (FileEntry::Ptr current : files) { + for (FileEntryPtr current : files) { QFileInfo fileInfo(ToQString(current->getName().c_str())); if (fileInfo.suffix().toLower() == "bsa" || fileInfo.suffix().toLower() == "ba2") { @@ -1942,7 +1944,7 @@ void MainWindow::updateBSAList(const QStringList &defaultArchives, const QString for (auto iter = items.begin(); iter != items.end(); ++iter) { int originID = iter->second->data(1, Qt::UserRole).toInt(); - FilesOrigin origin = m_OrganizerCore.directoryStructure()->getOriginByID(originID); + const FilesOrigin& origin = m_OrganizerCore.directoryStructure()->getOriginByID(originID); QString modName; const unsigned int modIndex = ModInfo::getIndex(ToQString(origin.getName())); @@ -2504,7 +2506,7 @@ void MainWindow::modRenamed(const QString &oldName, const QString &newName) void MainWindow::fileMoved(const QString &filePath, const QString &oldOriginName, const QString &newOriginName) { - const FileEntry::Ptr filePtr = m_OrganizerCore.directoryStructure()->findFile(ToWString(filePath)); + const FileEntryPtr filePtr = m_OrganizerCore.directoryStructure()->findFile(ToWString(filePath)); if (filePtr.get() != nullptr) { try { if (m_OrganizerCore.directoryStructure()->originExists(ToWString(newOriginName))) { diff --git a/src/modinfodialog.cpp b/src/modinfodialog.cpp index bfb8d2c7..c3239e0d 100644 --- a/src/modinfodialog.cpp +++ b/src/modinfodialog.cpp @@ -29,6 +29,8 @@ along with Mod Organizer. If not, see . #include "modinfodialogcategories.h" #include "modinfodialognexus.h" #include "modinfodialogfiletree.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" #include using namespace MOBase; diff --git a/src/modinfodialogconflicts.cpp b/src/modinfodialogconflicts.cpp index 63d89a1a..b61dbb7b 100644 --- a/src/modinfodialogconflicts.cpp +++ b/src/modinfodialogconflicts.cpp @@ -4,6 +4,9 @@ #include "utility.h" #include "settings.h" #include "organizercore.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" +#include "shared/fileentry.h" using namespace MOShared; using namespace MOBase; @@ -17,7 +20,7 @@ class ConflictItem public: ConflictItem( QString before, QString relativeName, QString after, - FileEntry::Index index, QString fileName, + FileIndex index, QString fileName, bool hasAltOrigins, QString altOrigin, bool archive) : m_before(std::move(before)), m_relativeName(std::move(relativeName)), @@ -65,7 +68,7 @@ public: return m_isArchive; } - FileEntry::Index fileIndex() const + FileIndex fileIndex() const { return m_index; } @@ -104,7 +107,7 @@ private: QString m_before; QString m_relativeName; QString m_after; - FileEntry::Index m_index; + FileIndex m_index; QString m_fileName; bool m_hasAltOrigins; QString m_altOrigin; @@ -1006,8 +1009,8 @@ bool GeneralConflictsTab::update() } ConflictItem GeneralConflictsTab::createOverwriteItem( - FileEntry::Index index, bool archive, QString fileName, QString relativeName, - const FileEntry::AlternativesVector& alternatives) + FileIndex index, bool archive, QString fileName, QString relativeName, + const MOShared::AlternativesVector& alternatives) { const auto& ds = *m_core.directoryStructure(); std::wstring altString; @@ -1028,7 +1031,7 @@ ConflictItem GeneralConflictsTab::createOverwriteItem( } ConflictItem GeneralConflictsTab::createNoConflictItem( - FileEntry::Index index, bool archive, QString fileName, QString relativeName) + FileIndex index, bool archive, QString fileName, QString relativeName) { return ConflictItem( QString(), std::move(relativeName), QString(), index, @@ -1036,7 +1039,7 @@ ConflictItem GeneralConflictsTab::createNoConflictItem( } ConflictItem GeneralConflictsTab::createOverwrittenItem( - FileEntry::Index index, int fileOrigin, bool archive, + FileIndex index, int fileOrigin, bool archive, QString fileName, QString relativeName) { const auto& ds = *m_core.directoryStructure(); @@ -1205,9 +1208,9 @@ void AdvancedConflictsTab::update() } std::optional AdvancedConflictsTab::createItem( - FileEntry::Index index, int fileOrigin, bool archive, + FileIndex index, int fileOrigin, bool archive, QString fileName, QString relativeName, - const MOShared::FileEntry::AlternativesVector& alternatives) + const MOShared::AlternativesVector& alternatives) { const auto& ds = *m_core.directoryStructure(); diff --git a/src/modinfodialogconflicts.h b/src/modinfodialogconflicts.h index c4845019..945f464d 100644 --- a/src/modinfodialogconflicts.h +++ b/src/modinfodialogconflicts.h @@ -4,7 +4,7 @@ #include "modinfodialogtab.h" #include "expanderwidget.h" #include "filterwidget.h" -#include "directoryentry.h" +#include "shared/fileregisterfwd.h" #include #include @@ -52,16 +52,16 @@ private: FilterWidget m_filterNoConflicts; ConflictItem createOverwriteItem( - MOShared::FileEntry::Index index, bool archive, + MOShared::FileIndex index, bool archive, QString fileName, QString relativeName, - const MOShared::FileEntry::AlternativesVector& alternatives); + const MOShared::AlternativesVector& alternatives); ConflictItem createNoConflictItem( - MOShared::FileEntry::Index index, bool archive, + MOShared::FileIndex index, bool archive, QString fileName, QString relativeName); ConflictItem createOverwrittenItem( - MOShared::FileEntry::Index index, int fileOrigin, bool archive, + MOShared::FileIndex index, int fileOrigin, bool archive, QString fileName, QString relativeName); void onOverwriteActivated(const QModelIndex& index); @@ -94,9 +94,9 @@ private: ConflictListModel* m_model; std::optional createItem( - MOShared::FileEntry::Index index, int fileOrigin, bool archive, + MOShared::FileIndex index, int fileOrigin, bool archive, QString fileName, QString relativeName, - const MOShared::FileEntry::AlternativesVector& alternatives); + const MOShared::AlternativesVector& alternatives); }; diff --git a/src/modinfodialogtab.cpp b/src/modinfodialogtab.cpp index 9748d059..d662e2b2 100644 --- a/src/modinfodialogtab.cpp +++ b/src/modinfodialogtab.cpp @@ -1,8 +1,8 @@ #include "modinfodialogtab.h" #include "ui_modinfodialog.h" #include "texteditor.h" -#include "directoryentry.h" #include "modinfo.h" +#include "shared/filesorigin.h" ModInfoDialogTab::ModInfoDialogTab(ModInfoDialogTabContext cx) : ui(cx.ui), m_core(cx.core), m_plugin(cx.plugin), m_parent(cx.parent), diff --git a/src/modinfowithconflictinfo.cpp b/src/modinfowithconflictinfo.cpp index 861782d9..2b4fa11c 100644 --- a/src/modinfowithconflictinfo.cpp +++ b/src/modinfowithconflictinfo.cpp @@ -1,7 +1,8 @@ #include "modinfowithconflictinfo.h" - -#include "directoryentry.h" #include "utility.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" +#include "shared/fileentry.h" #include using namespace MOBase; @@ -98,11 +99,11 @@ void ModInfoWithConflictInfo::doConflictCheck() const if ((*m_DirectoryStructure)->originExists(name)) { FilesOrigin &origin = (*m_DirectoryStructure)->getOriginByName(name); - std::vector files = origin.getFiles(); + std::vector files = origin.getFiles(); std::set checkedDirs; // for all files in this origin - for (FileEntry::Ptr file : files) { + for (FileEntryPtr file : files) { // skip hiidden file check if already found one if (!hasHiddenFiles) { @@ -267,7 +268,7 @@ bool ModInfoWithConflictInfo::isRedundant() const std::wstring name = ToWString(this->name()); if ((*m_DirectoryStructure)->originExists(name)) { FilesOrigin &origin = (*m_DirectoryStructure)->getOriginByName(name); - std::vector files = origin.getFiles(); + std::vector files = origin.getFiles(); bool ignore = false; for (auto iter = files.begin(); iter != files.end(); ++iter) { if ((*iter)->getOrigin(ignore) == origin.getID()) { diff --git a/src/modlist.cpp b/src/modlist.cpp index afc1b65f..e84910fd 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -27,6 +27,10 @@ along with Mod Organizer. If not, see . #include "pluginlist.h" #include "settings.h" #include "modinforegular.h" +#include "shared/directoryentry.h" +#include "shared/fileentry.h" +#include "shared/filesorigin.h" + #include #include #include @@ -872,7 +876,7 @@ void ModList::highlightMods(const QItemSelectionModel *selection, const MOShared for (QModelIndex idx : selection->selectedRows(PluginList::COL_NAME)) { QString modName = idx.data().toString(); - const MOShared::FileEntry::Ptr fileEntry = directoryEntry.findFile(modName.toStdWString()); + const MOShared::FileEntryPtr fileEntry = directoryEntry.findFile(modName.toStdWString()); if (fileEntry.get() != nullptr) { bool archive = false; std::vector>> origins; @@ -881,7 +885,7 @@ void ModList::highlightMods(const QItemSelectionModel *selection, const MOShared origins.insert(origins.end(), std::pair>(fileEntry->getOrigin(archive), fileEntry->getArchive())); } for (auto originInfo : origins) { - MOShared::FilesOrigin &origin = directoryEntry.getOriginByID(originInfo.first); + MOShared::FilesOrigin& origin = directoryEntry.getOriginByID(originInfo.first); for (unsigned int i = 0; i < ModInfo::getNumMods(); ++i) { if (ModInfo::getByIndex(i)->internalName() == QString::fromStdWString(origin.getName())) { ModInfo::getByIndex(i)->setPluginSelected(true); diff --git a/src/modlist.h b/src/modlist.h index 7452b28b..d8980dec 100644 --- a/src/modlist.h +++ b/src/modlist.h @@ -27,7 +27,6 @@ along with Mod Organizer. If not, see . #include "profile.h" #include -#include #include #include diff --git a/src/organizercore.cpp b/src/organizercore.cpp index a3202153..c9c0bee5 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -34,6 +33,10 @@ #include "previewdialog.h" #include "env.h" #include "envmodule.h" +#include "envfs.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" +#include "shared/fileentry.h" #include #include @@ -810,7 +813,7 @@ QString OrganizerCore::resolvePath(const QString &fileName) const if (m_DirectoryStructure == nullptr) { return QString(); } - const FileEntry::Ptr file + const FileEntryPtr file = m_DirectoryStructure->searchFile(ToWString(fileName), nullptr); if (file.get() != nullptr) { return ToQString(file->getFullPath()); diff --git a/src/organizercore.h b/src/organizercore.h index 223eb6cb..980156d3 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -15,7 +15,6 @@ #include "moshortcut.h" #include "processrunner.h" #include "uilocker.h" -#include #include #include #include diff --git a/src/pch.h b/src/pch.h index 030ee634..c66550be 100644 --- a/src/pch.h +++ b/src/pch.h @@ -5,14 +5,17 @@ #include #include #include +#include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -21,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/src/pluginlist.cpp b/src/pluginlist.cpp index f348f64c..4b2eedbd 100644 --- a/src/pluginlist.cpp +++ b/src/pluginlist.cpp @@ -23,6 +23,10 @@ along with Mod Organizer. If not, see . #include "modinfo.h" #include "modlist.h" #include "viewmarkingscrollbar.h" +#include "shared/directoryentry.h" +#include "shared/filesorigin.h" +#include "shared/fileentry.h" + #include #include #include @@ -144,10 +148,10 @@ void PluginList::highlightPlugins(const QItemSelectionModel *selection, const MO if (!selectedMod.isNull() && profile.modEnabled(modIndex)) { QDir dir(selectedMod->absolutePath()); QStringList plugins = dir.entryList(QStringList() << "*.esp" << "*.esm" << "*.esl"); - MOShared::FilesOrigin origin = directoryEntry.getOriginByName(selectedMod->internalName().toStdWString()); + const MOShared::FilesOrigin& origin = directoryEntry.getOriginByName(selectedMod->internalName().toStdWString()); if (plugins.size() > 0) { for (auto plugin : plugins) { - MOShared::FileEntry::Ptr file = directoryEntry.findFile(plugin.toStdWString()); + MOShared::FileEntryPtr file = directoryEntry.findFile(plugin.toStdWString()); if (file && file->getOrigin() != origin.getID()) { const std::vector>> alternatives = file->getAlternatives(); if (std::find_if(alternatives.begin(), alternatives.end(), [&](const std::pair>& element) { return element.first == origin.getID(); }) == alternatives.end()) @@ -185,8 +189,8 @@ void PluginList::refresh(const QString &profileName QStringList availablePlugins; - std::vector files = baseDirectory.getFiles(); - for (FileEntry::Ptr current : files) { + std::vector files = baseDirectory.getFiles(); + for (FileEntryPtr current : files) { if (current.get() == nullptr) { continue; } @@ -217,7 +221,7 @@ void PluginList::refresh(const QString &profileName bool hasIni = baseDirectory.findFile(ToWString(iniPath)).get() != nullptr; std::set loadedArchives; QString candidateName; - for (FileEntry::Ptr archiveCandidate : files) { + for (FileEntryPtr archiveCandidate : files) { candidateName = ToQString(archiveCandidate->getName()); if (candidateName.startsWith(baseName, Qt::CaseInsensitive) && (candidateName.endsWith(".bsa", Qt::CaseInsensitive) || @@ -548,7 +552,7 @@ bool PluginList::saveLoadOrder(DirectoryEntry &directoryStructure) for (ESPInfo &esp : m_ESPs) { std::wstring espName = ToWString(esp.name); - const FileEntry::Ptr fileEntry = directoryStructure.findFile(espName); + const FileEntryPtr fileEntry = directoryStructure.findFile(espName); if (fileEntry.get() != nullptr) { QString fileName; bool archive = false; diff --git a/src/pluginlist.h b/src/pluginlist.h index fb6d0543..cdab18b5 100644 --- a/src/pluginlist.h +++ b/src/pluginlist.h @@ -20,7 +20,6 @@ along with Mod Organizer. If not, see . #ifndef PLUGINLIST_H #define PLUGINLIST_H -#include #include #include "profile.h" #include "loot.h" diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 6c5ad9ae..1036dfe6 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -18,21 +18,14 @@ along with Mod Organizer. If not, see . */ #include "directoryentry.h" -#include "windows_error.h" -#include "error_report.h" +#include "originconnection.h" +#include "filesorigin.h" +#include "fileentry.h" #include "envfs.h" -#include +#include "util.h" +#include "windows_error.h" #include -#include -#include -#include -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include -#include -#include +#include namespace MOShared { @@ -58,16 +51,6 @@ void elapsedImpl(std::chrono::nanoseconds& out, F&& f) #define elapsed(OUT, F) (F)(); //#define elapsed(OUT, F) elapsedImpl(OUT, F); - -static std::wstring tail(const std::wstring &source, const size_t count) -{ - if (count >= source.length()) { - return source; - } - - return source.substr(source.length() - count); -} - static bool SupportOptimizedFind() { // large fetch and basic info for FindFirstFileEx is supported on win server 2008 r2, win 7 and newer @@ -189,686 +172,6 @@ std::string DirectoryStats::toCsv() const } -class OriginConnection -{ -public: - typedef int Index; - static const int INVALID_INDEX = INT_MIN; - - OriginConnection() - : m_NextID(0) - { - } - - std::pair getOrCreate( - const std::wstring &originName, const std::wstring &directory, int priority, - const boost::shared_ptr& fileRegister, - const boost::shared_ptr& originConnection, - DirectoryStats& stats) - { - std::unique_lock lock(m_Mutex); - - auto itor = m_OriginsNameMap.find(originName); - - if (itor == m_OriginsNameMap.end()) { - FilesOrigin& origin = createOriginNoLock( - originName, directory, priority, fileRegister, originConnection); - - return {origin, true}; - } else { - FilesOrigin& origin = m_Origins[itor->second]; - lock.unlock(); - - origin.enable(true, stats); - return {origin, false}; - } - } - - FilesOrigin& createOrigin( - const std::wstring &originName, const std::wstring &directory, int priority, - boost::shared_ptr fileRegister, - boost::shared_ptr originConnection) - { - std::scoped_lock lock(m_Mutex); - - return createOriginNoLock( - originName, directory, priority, fileRegister, originConnection); - } - - bool exists(const std::wstring &name) - { - std::scoped_lock lock(m_Mutex); - return m_OriginsNameMap.find(name) != m_OriginsNameMap.end(); - } - - FilesOrigin &getByID(Index ID) - { - std::scoped_lock lock(m_Mutex); - return m_Origins[ID]; - } - - const FilesOrigin* findByID(Index ID) const - { - std::scoped_lock lock(m_Mutex); - - auto itor = m_Origins.find(ID); - - if (itor == m_Origins.end()) { - return nullptr; - } else { - return &itor->second; - } - } - - FilesOrigin &getByName(const std::wstring &name) - { - std::scoped_lock lock(m_Mutex); - - std::map::iterator iter = m_OriginsNameMap.find(name); - - if (iter != m_OriginsNameMap.end()) { - return m_Origins[iter->second]; - } else { - std::ostringstream stream; - stream << QObject::tr("invalid origin name: ").toStdString() << ToString(name, true); - throw std::runtime_error(stream.str()); - } - } - - void changePriorityLookup(int oldPriority, int newPriority) - { - std::scoped_lock lock(m_Mutex); - - auto iter = m_OriginsPriorityMap.find(oldPriority); - - if (iter != m_OriginsPriorityMap.end()) { - Index idx = iter->second; - m_OriginsPriorityMap.erase(iter); - m_OriginsPriorityMap[newPriority] = idx; - } - } - - void changeNameLookup(const std::wstring &oldName, const std::wstring &newName) - { - std::scoped_lock lock(m_Mutex); - - auto iter = m_OriginsNameMap.find(oldName); - - if (iter != m_OriginsNameMap.end()) { - Index idx = iter->second; - m_OriginsNameMap.erase(iter); - m_OriginsNameMap[newName] = idx; - } else { - log::error(QObject::tr("failed to change name lookup from {} to {}").toStdString(), oldName, newName); - } - } - -private: - Index m_NextID; - std::map m_Origins; - std::map m_OriginsNameMap; - std::map m_OriginsPriorityMap; - mutable std::mutex m_Mutex; - - Index createID() - { - return m_NextID++; - } - - FilesOrigin& createOriginNoLock( - const std::wstring &originName, const std::wstring &directory, int priority, - boost::shared_ptr fileRegister, - boost::shared_ptr originConnection) - { - int newID = createID(); - - auto itor = m_Origins.insert({newID, FilesOrigin( - newID, originName, directory, priority, - fileRegister, originConnection)}).first; - - m_OriginsNameMap.insert({originName, newID}); - m_OriginsPriorityMap.insert({priority, newID}); - - return itor->second; - } -}; - - -FileEntry::FileEntry() : - m_Index(UINT_MAX), m_Name(), m_Origin(-1), m_Parent(nullptr), - m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) -{ -} - -FileEntry::FileEntry(Index index, std::wstring name, DirectoryEntry *parent) : - m_Index(index), m_Name(std::move(name)), m_Origin(-1), m_Archive(L"", -1), m_Parent(parent), - m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) -{ -} - -void FileEntry::addOrigin( - int origin, FILETIME fileTime, std::wstring_view archive, int order) -{ - std::scoped_lock lock(m_OriginsMutex); - - if (m_Parent != nullptr) { - m_Parent->propagateOrigin(origin); - } - - if (m_Origin == -1) { - // If this file has no previous origin, this mod is now the origin with no - // alternatives - m_Origin = origin; - m_FileTime = fileTime; - m_Archive = std::pair(std::wstring(archive.begin(), archive.end()), order); - } - else if ( - (m_Parent != nullptr) && ( - (m_Parent->getOriginByID(origin).getPriority() > m_Parent->getOriginByID(m_Origin).getPriority()) || - (archive.size() == 0 && m_Archive.first.size() > 0 )) - ) { - // If this mod has a higher priority than the origin mod OR - // this mod has a loose file and the origin mod has an archived file, - // this mod is now the origin and the previous origin is the first alternative - - auto itor = std::find_if( - m_Alternatives.begin(), m_Alternatives.end(), - [&](auto&& i) { return i.first == m_Origin; }); - - if (itor == m_Alternatives.end()) { - m_Alternatives.push_back({m_Origin, m_Archive}); - } - - m_Origin = origin; - m_FileTime = fileTime; - m_Archive = std::pair(std::wstring(archive.begin(), archive.end()), order); - } - else { - // This mod is just an alternative - bool found = false; - - if (m_Origin == origin) { - // already an origin - return; - } - - for (auto iter = m_Alternatives.begin(); iter != m_Alternatives.end(); ++iter) { - if (iter->first == origin) { - // already an origin - return; - } - - if ((m_Parent != nullptr) && - (m_Parent->getOriginByID(iter->first).getPriority() < m_Parent->getOriginByID(origin).getPriority())) { - m_Alternatives.insert(iter, {origin, {std::wstring(archive.begin(), archive.end()), order}}); - found = true; - break; - } - } - - if (!found) { - m_Alternatives.push_back({origin, {std::wstring(archive.begin(), archive.end()), order}}); - } - } -} - -bool FileEntry::removeOrigin(int origin) -{ - std::scoped_lock lock(m_OriginsMutex); - - if (m_Origin == origin) { - if (!m_Alternatives.empty()) { - // find alternative with the highest priority - auto currentIter = m_Alternatives.begin(); - for (auto iter = m_Alternatives.begin(); iter != m_Alternatives.end(); ++iter) { - if (iter->first != origin) { - //Both files are not from archives. - if (!iter->second.first.size() && !currentIter->second.first.size()) { - if ((m_Parent->getOriginByID(iter->first).getPriority() > m_Parent->getOriginByID(currentIter->first).getPriority())) { - currentIter = iter; - } - } - else { - //Both files are from archives - if (iter->second.first.size() && currentIter->second.first.size()) { - if (iter->second.second > currentIter->second.second) { - currentIter = iter; - } - } - else { - //Only one of the two is an archive, so we change currentIter only if he is the archive one. - if (currentIter->second.first.size()) { - currentIter = iter; - } - } - } - } - } - - int currentID = currentIter->first; - m_Archive = currentIter->second; - m_Alternatives.erase(currentIter); - - m_Origin = currentID; - } else { - m_Origin = -1; - m_Archive = std::pair(L"", -1); - return true; - } - } else { - auto newEnd = std::remove_if( - m_Alternatives.begin(), m_Alternatives.end(), - [&](auto &i) { return i.first == origin; }); - - if (newEnd != m_Alternatives.end()) { - m_Alternatives.erase(newEnd, m_Alternatives.end()); - } - } - return false; -} - -void FileEntry::sortOrigins() -{ - std::scoped_lock lock(m_OriginsMutex); - - m_Alternatives.push_back({m_Origin, m_Archive}); - - std::sort(m_Alternatives.begin(), m_Alternatives.end(), [&](auto&& LHS, auto&& RHS) { - if (!LHS.second.first.size() && !RHS.second.first.size()) { - int l = m_Parent->getOriginByID(LHS.first).getPriority(); - if (l < 0) { - l = INT_MAX; - } - - int r = m_Parent->getOriginByID(RHS.first).getPriority(); - if (r < 0) { - r = INT_MAX; - } - - return l < r; - } - - if (LHS.second.first.size() && RHS.second.first.size()) { - int l = LHS.second.second; if (l < 0) l = INT_MAX; - int r = RHS.second.second; if (r < 0) r = INT_MAX; - - return l < r; - } - - if (RHS.second.first.size()) { - return false; - } - - return true; - }); - - if (!m_Alternatives.empty()) { - m_Origin = m_Alternatives.back().first; - m_Archive = m_Alternatives.back().second; - m_Alternatives.pop_back(); - } -} - -bool FileEntry::isFromArchive(std::wstring archiveName) const -{ - std::scoped_lock lock(m_OriginsMutex); - - if (archiveName.length() == 0) { - return m_Archive.first.length() != 0; - } - - if (m_Archive.first.compare(archiveName) == 0) { - return true; - } - - for (auto alternative : m_Alternatives) { - if (alternative.second.first.compare(archiveName) == 0) { - return true; - } - } - - return false; -} - -std::wstring FileEntry::getFullPath(int originID) const -{ - std::scoped_lock lock(m_OriginsMutex); - - if (originID == -1) { - bool ignore = false; - originID = getOrigin(ignore); - } - - // base directory for origin - const auto* o = m_Parent->findOriginByID(originID); - if (!o) { - return {}; - } - - std::wstring result = o->getPath(); - - // all intermediate directories - recurseParents(result, m_Parent); - - return result + L"\\" + m_Name; -} - -std::wstring FileEntry::getRelativePath() const -{ - std::wstring result; - - // all intermediate directories - recurseParents(result, m_Parent); - - return result + L"\\" + m_Name; -} - -bool FileEntry::recurseParents(std::wstring &path, const DirectoryEntry *parent) const -{ - if (parent == nullptr) { - return false; - } else { - // don't append the topmost parent because it is the virtual data-root - if (recurseParents(path, parent->getParent())) { - path.append(L"\\").append(parent->getName()); - } - - return true; - } -} - - -FilesOrigin::FilesOrigin() - : m_ID(0), m_Disabled(false), m_Name(), m_Path(), m_Priority(0) -{ -} - -FilesOrigin::FilesOrigin(const FilesOrigin &reference) - : m_ID(reference.m_ID) - , m_Disabled(reference.m_Disabled) - , m_Name(reference.m_Name) - , m_Path(reference.m_Path) - , m_Priority(reference.m_Priority) - , m_FileRegister(reference.m_FileRegister) - , m_OriginConnection(reference.m_OriginConnection) -{ -} - -FilesOrigin::FilesOrigin( - int ID, const std::wstring &name, const std::wstring &path, int priority, - boost::shared_ptr fileRegister, - boost::shared_ptr originConnection) : - m_ID(ID), m_Disabled(false), m_Name(name), m_Path(path), - m_Priority(priority), m_FileRegister(fileRegister), - m_OriginConnection(originConnection) -{ -} - -void FilesOrigin::setPriority(int priority) -{ - m_OriginConnection.lock()->changePriorityLookup(m_Priority, priority); - - m_Priority = priority; -} - -void FilesOrigin::setName(const std::wstring &name) -{ - m_OriginConnection.lock()->changeNameLookup(m_Name, name); - - // change path too - if (tail(m_Path, m_Name.length()) == m_Name) { - m_Path = m_Path.substr(0, m_Path.length() - m_Name.length()).append(name); - } - - m_Name = name; -} - -std::vector FilesOrigin::getFiles() const -{ - std::vector result; - - { - std::scoped_lock lock(m_Mutex); - - for (FileEntry::Index fileIdx : m_Files) { - if (FileEntry::Ptr p = m_FileRegister.lock()->getFile(fileIdx)) { - result.push_back(p); - } - } - } - - return result; -} - -FileEntry::Ptr FilesOrigin::findFile(FileEntry::Index index) const -{ - return m_FileRegister.lock()->getFile(index); -} - -void FilesOrigin::enable(bool enabled) -{ - DirectoryStats dummy; - enable(enabled, dummy); -} - -void FilesOrigin::enable(bool enabled, DirectoryStats& stats) -{ - if (!enabled) { - ++stats.originsNeededEnabled; - - std::set copy; - - { - std::scoped_lock lock(m_Mutex); - copy = m_Files; - m_Files.clear(); - } - - m_FileRegister.lock()->removeOriginMulti(copy, m_ID); - } - - m_Disabled = !enabled; -} - -void FilesOrigin::removeFile(FileEntry::Index index) -{ - std::scoped_lock lock(m_Mutex); - - auto iter = m_Files.find(index); - - if (iter != m_Files.end()) { - m_Files.erase(iter); - } -} - -bool FilesOrigin::containsArchive(std::wstring archiveName) -{ - std::scoped_lock lock(m_Mutex); - - for (FileEntry::Index fileIdx : m_Files) { - if (FileEntry::Ptr p = m_FileRegister.lock()->getFile(fileIdx)) { - if (p->isFromArchive(archiveName)) { - return true; - } - } - } - - return false; -} - - -FileRegister::FileRegister(boost::shared_ptr originConnection) - : m_OriginConnection(originConnection), m_NextIndex(0) -{ -} - -bool FileRegister::indexValid(FileEntry::Index index) const -{ - std::scoped_lock lock(m_Mutex); - - if (index < m_Files.size()) { - return (m_Files[index].get() != nullptr); - } - - return false; -} - -FileEntry::Ptr FileRegister::createFile( - std::wstring name, DirectoryEntry *parent, DirectoryStats& stats) -{ - const auto index = generateIndex(); - auto p = FileEntry::Ptr(new FileEntry(index, std::move(name), parent)); - - { - std::scoped_lock lock(m_Mutex); - - if (index >= m_Files.size()) { - m_Files.resize(index + 1); - } - - m_Files[index] = p; - } - - return p; -} - -FileEntry::Index FileRegister::generateIndex() -{ - return m_NextIndex++; -} - -FileEntry::Ptr FileRegister::getFile(FileEntry::Index index) const -{ - std::scoped_lock lock(m_Mutex); - - if (index < m_Files.size()) { - return m_Files[index]; - } else { - return {}; - } -} - -bool FileRegister::removeFile(FileEntry::Index index) -{ - std::scoped_lock lock(m_Mutex); - - if (index < m_Files.size()) { - FileEntry::Ptr p; - m_Files[index].swap(p); - - if (p) { - unregisterFile(p); - return true; - } - } - - log::error(QObject::tr("invalid file index for remove: {}").toStdString(), index); - return false; -} - -void FileRegister::removeOrigin(FileEntry::Index index, int originID) -{ - std::unique_lock lock(m_Mutex); - - if (index < m_Files.size()) { - FileEntry::Ptr& p = m_Files[index]; - - if (p) { - if (p->removeOrigin(originID)) { - m_Files[index] = {}; - lock.unlock(); - unregisterFile(p); - return; - } - } - } - - log::error(QObject::tr("invalid file index for remove (for origin): {}").toStdString(), index); -} - -void FileRegister::removeOriginMulti( - std::set indices, int originID) -{ - std::vector removedFiles; - - { - std::scoped_lock lock(m_Mutex); - - for (auto iter = indices.begin(); iter != indices.end(); ) { - const auto index = *iter; - - if (index < m_Files.size()) { - const auto& p = m_Files[index]; - - if (p && p->removeOrigin(originID)) { - removedFiles.push_back(p); - m_Files[index] = {}; - ++iter; - continue; - } - } - - iter = indices.erase(iter); - } - } - - // optimization: this is only called when disabling an origin and in this case - // we don't have to remove the file from the origin - - // need to remove files from their parent directories. multiple ways to go - // about this: - // a) for each file, search its parents file-list (preferably by name) and - // remove what is found - // b) gather the parent directories, go through the file list for each once - // and remove all files that have been removed - // - // the latter should be faster when there are many files in few directories. - // since this is called only when disabling an origin that is probably - // frequently the case - - std::set parents; - for (const FileEntry::Ptr &file : removedFiles) { - if (file->getParent() != nullptr) { - parents.insert(file->getParent()); - } - } - - for (DirectoryEntry *parent : parents) { - parent->removeFiles(indices); - } -} - -void FileRegister::sortOrigins() -{ - std::scoped_lock lock(m_Mutex); - - for (auto&& p : m_Files) { - if (p) { - p->sortOrigins(); - } - } -} - -void FileRegister::unregisterFile(FileEntry::Ptr file) -{ - bool ignore; - - // unregister from origin - int originID = file->getOrigin(ignore); - m_OriginConnection->getByID(originID).removeFile(file->getIndex()); - const auto& alternatives = file->getAlternatives(); - - for (auto iter = alternatives.begin(); iter != alternatives.end(); ++iter) { - m_OriginConnection->getByID(iter->first).removeFile(file->getIndex()); - } - - // unregister from directory - if (file->getParent() != nullptr) { - file->getParent()->removeFile(file->getIndex()); - } -} - - DirectoryEntry::DirectoryEntry( std::wstring name, DirectoryEntry *parent, int originID) : m_OriginConnection(new OriginConnection), @@ -1046,7 +349,7 @@ int DirectoryEntry::anyOrigin() const bool ignore; for (auto iter = m_Files.begin(); iter != m_Files.end(); ++iter) { - FileEntry::Ptr entry = m_FileRegister->getFile(iter->second); + FileEntryPtr entry = m_FileRegister->getFile(iter->second); if ((entry.get() != nullptr) && !entry->isFromArchive()) { return entry->getOrigin(ignore); } @@ -1064,9 +367,9 @@ int DirectoryEntry::anyOrigin() const return *(m_Origins.begin()); } -std::vector DirectoryEntry::getFiles() const +std::vector DirectoryEntry::getFiles() const { - std::vector result; + std::vector result; for (auto iter = m_Files.begin(); iter != m_Files.end(); ++iter) { result.push_back(m_FileRegister->getFile(iter->second)); @@ -1098,7 +401,7 @@ DirectoryEntry *DirectoryEntry::findSubDirectoryRecursive(const std::wstring &pa return getSubDirectoryRecursive(path, false, -1); } -const FileEntry::Ptr DirectoryEntry::findFile( +const FileEntryPtr DirectoryEntry::findFile( const std::wstring &name, bool alreadyLowerCase) const { FilesLookup::const_iterator iter; @@ -1112,18 +415,18 @@ const FileEntry::Ptr DirectoryEntry::findFile( if (iter != m_FilesLookup.end()) { return m_FileRegister->getFile(iter->second); } else { - return FileEntry::Ptr(); + return FileEntryPtr(); } } -const FileEntry::Ptr DirectoryEntry::findFile(const FileKey& key) const +const FileEntryPtr DirectoryEntry::findFile(const FileKey& key) const { auto iter = m_FilesLookup.find(key); if (iter != m_FilesLookup.end()) { return m_FileRegister->getFile(iter->second); } else { - return FileEntry::Ptr(); + return FileEntryPtr(); } } @@ -1135,7 +438,7 @@ bool DirectoryEntry::hasFile(const std::wstring& name) const bool DirectoryEntry::containsArchive(std::wstring archiveName) { for (auto iter = m_Files.begin(); iter != m_Files.end(); ++iter) { - FileEntry::Ptr entry = m_FileRegister->getFile(iter->second); + FileEntryPtr entry = m_FileRegister->getFile(iter->second); if (entry->isFromArchive(archiveName)) { return true; } @@ -1144,7 +447,7 @@ bool DirectoryEntry::containsArchive(std::wstring archiveName) return false; } -const FileEntry::Ptr DirectoryEntry::searchFile( +const FileEntryPtr DirectoryEntry::searchFile( const std::wstring &path, const DirectoryEntry **directory) const { if (directory != nullptr) { @@ -1157,7 +460,7 @@ const FileEntry::Ptr DirectoryEntry::searchFile( *directory = this; } - return FileEntry::Ptr(); + return FileEntryPtr(); } const size_t len = path.find_first_of(L"\\/"); @@ -1182,14 +485,14 @@ const FileEntry::Ptr DirectoryEntry::searchFile( if (temp != nullptr) { if (len >= path.size()) { log::error(QObject::tr("unexpected end of path").toStdString()); - return FileEntry::Ptr(); + return FileEntryPtr(); } return temp->searchFile(path.substr(len + 1), directory); } } - return FileEntry::Ptr(); + return FileEntryPtr(); } void DirectoryEntry::removeFile(FileEntry::Index index) @@ -1251,7 +554,7 @@ bool DirectoryEntry::remove(const std::wstring &fileName, int *origin) if (iter != m_Files.end()) { if (origin != nullptr) { - FileEntry::Ptr entry = m_FileRegister->getFile(iter->second); + FileEntryPtr entry = m_FileRegister->getFile(iter->second); if (entry.get() != nullptr) { bool ignore; *origin = entry->getOrigin(ignore); @@ -1291,12 +594,12 @@ void DirectoryEntry::removeFiles(const std::set &indices) removeFilesFromList(indices); } -FileEntry::Ptr DirectoryEntry::insert( +FileEntryPtr DirectoryEntry::insert( std::wstring_view fileName, FilesOrigin &origin, FILETIME fileTime, std::wstring_view archive, int order, DirectoryStats& stats) { std::wstring fileNameLower = ToLowerCopy(fileName); - FileEntry::Ptr fe; + FileEntryPtr fe; FileKey key(std::move(fileNameLower)); @@ -1337,11 +640,11 @@ FileEntry::Ptr DirectoryEntry::insert( return fe; } -FileEntry::Ptr DirectoryEntry::insert( +FileEntryPtr DirectoryEntry::insert( env::File& file, FilesOrigin &origin, std::wstring_view archive, int order, DirectoryStats& stats) { - FileEntry::Ptr fe; + FileEntryPtr fe; { std::unique_lock lock(m_FilesMutex); @@ -1730,7 +1033,7 @@ void DirectoryEntry::dump(std::FILE* f, const std::wstring& parentPath) const continue; } - const auto o = m_OriginConnection->getByID(file->getOrigin()); + const auto& o = m_OriginConnection->getByID(file->getOrigin()); const auto path = parentPath + L"\\" + file->getName(); const auto line = path + L"\t(" + o.getName() + L")\r\n"; diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index ce50da44..df946946 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -17,29 +17,18 @@ You should have received a copy of the GNU General Public License along with Mod Organizer. If not, see . */ -#ifndef DIRECTORYENTRY_H -#define DIRECTORYENTRY_H +#ifndef MO_REGISTER_DIRECTORYENTRY_INCLUDED +#define MO_REGISTER_DIRECTORYENTRY_INCLUDED - -#include -#include -#include -#include -#include -#include - -#define WIN32_MEAN_AND_LEAN -#include +#include "fileregister.h" #include -#ifndef Q_MOC_RUN -#include -#include -#endif - -#include "util.h" -#include "envfs.h" -namespace MOShared { struct DirectoryEntryFileKey; } +namespace env +{ + class DirectoryWalker; + struct Directory; + struct File; +} namespace std { @@ -57,309 +46,6 @@ namespace std namespace MOShared { -class DirectoryEntry; -class OriginConnection; -class FileRegister; - - -struct DirectoryStats -{ - static constexpr bool EnableInstrumentation = false; - - std::string mod; - - std::chrono::nanoseconds dirTimes; - std::chrono::nanoseconds fileTimes; - std::chrono::nanoseconds sortTimes; - - std::chrono::nanoseconds subdirLookupTimes; - std::chrono::nanoseconds addDirectoryTimes; - - std::chrono::nanoseconds filesLookupTimes; - std::chrono::nanoseconds addFileTimes; - std::chrono::nanoseconds addOriginToFileTimes; - std::chrono::nanoseconds addFileToOriginTimes; - std::chrono::nanoseconds addFileToRegisterTimes; - - int64_t originExists; - int64_t originCreate; - int64_t originsNeededEnabled; - - int64_t subdirExists; - int64_t subdirCreate; - - int64_t fileExists; - int64_t fileCreate; - int64_t filesInsertedInRegister; - int64_t filesAssignedInRegister; - - DirectoryStats(); - - DirectoryStats& operator+=(const DirectoryStats& o); - - static std::string csvHeader(); - std::string toCsv() const; -}; - - -class FileEntry -{ -public: - static constexpr uint64_t NoFileSize = - std::numeric_limits::max(); - - typedef unsigned int Index; - typedef boost::shared_ptr Ptr; - - // a vector of {originId, {archiveName, order}} - // - // if a file is in an archive, archiveName is the name of the bsa and order - // is the order of the associated plugin in the plugins list - // - // is a file is not in an archive, archiveName is empty and order is usually - // -1 - typedef std::vector>> - AlternativesVector; - - FileEntry(); - FileEntry(Index index, std::wstring name, DirectoryEntry *parent); - - Index getIndex() const - { - return m_Index; - } - - void addOrigin( - int origin, FILETIME fileTime, std::wstring_view archive, int order); - - // remove the specified origin from the list of origins that contain this - // file. if no origin is left, the file is effectively deleted and true is - // returned. otherwise, false is returned - bool removeOrigin(int origin); - - void sortOrigins(); - - // gets the list of alternative origins (origins with lower priority than - // the primary one). if sortOrigins has been called, it is sorted by priority - // (ascending) - const AlternativesVector &getAlternatives() const - { - return m_Alternatives; - } - - const std::wstring &getName() const - { - return m_Name; - } - - int getOrigin() const - { - return m_Origin; - } - - int getOrigin(bool &archive) const - { - archive = (m_Archive.first.length() != 0); - return m_Origin; - } - - const std::pair &getArchive() const - { - return m_Archive; - } - - bool isFromArchive(std::wstring archiveName = L"") const; - - // if originID is -1, uses the main origin; if this file doesn't exist in the - // given origin, returns an empty string - // - std::wstring getFullPath(int originID=-1) const; - - std::wstring getRelativePath() const; - - DirectoryEntry *getParent() - { - return m_Parent; - } - - void setFileTime(FILETIME fileTime) const - { - m_FileTime = fileTime; - } - - FILETIME getFileTime() const - { - return m_FileTime; - } - - void setFileSize(uint64_t size, uint64_t compressedSize) - { - m_FileSize = size; - m_CompressedFileSize = compressedSize; - } - - uint64_t getFileSize() const - { - return m_FileSize; - } - - uint64_t getCompressedFileSize() const - { - return m_CompressedFileSize; - } - -private: - Index m_Index; - std::wstring m_Name; - int m_Origin; - std::pair m_Archive; - AlternativesVector m_Alternatives; - DirectoryEntry *m_Parent; - mutable FILETIME m_FileTime; - uint64_t m_FileSize, m_CompressedFileSize; - mutable std::mutex m_OriginsMutex; - - bool recurseParents(std::wstring &path, const DirectoryEntry *parent) const; -}; - - -// represents a mod or the data directory, providing files to the tree -class FilesOrigin -{ - friend class OriginConnection; - -public: - FilesOrigin(); - FilesOrigin(const FilesOrigin &reference); - - // sets priority for this origin, but it will overwrite the existing mapping - // for this priority, the previous origin will no longer be referenced - void setPriority(int priority); - - int getPriority() const - { - return m_Priority; - } - - void setName(const std::wstring &name); - const std::wstring &getName() const - { - return m_Name; - } - - int getID() const - { - return m_ID; - } - - const std::wstring &getPath() const - { - return m_Path; - } - - std::vector getFiles() const; - FileEntry::Ptr findFile(FileEntry::Index index) const; - - void enable(bool enabled, DirectoryStats& stats); - void enable(bool enabled); - - bool isDisabled() const - { - return m_Disabled; - } - - void addFile(FileEntry::Index index) - { - std::scoped_lock lock(m_Mutex); - m_Files.insert(index); - } - - void removeFile(FileEntry::Index index); - - bool containsArchive(std::wstring archiveName); - -private: - int m_ID; - bool m_Disabled; - std::set m_Files; - std::wstring m_Name; - std::wstring m_Path; - int m_Priority; - boost::weak_ptr m_FileRegister; - boost::weak_ptr m_OriginConnection; - mutable std::mutex m_Mutex; - - FilesOrigin( - int ID, const std::wstring &name, const std::wstring &path, int priority, - boost::shared_ptr fileRegister, - boost::shared_ptr originConnection); -}; - - -class FileRegister -{ -public: - FileRegister(boost::shared_ptr originConnection); - - bool indexValid(FileEntry::Index index) const; - - FileEntry::Ptr createFile( - std::wstring name, DirectoryEntry *parent, DirectoryStats& stats); - - FileEntry::Ptr getFile(FileEntry::Index index) const; - - size_t highestCount() const - { - std::scoped_lock lock(m_Mutex); - return m_Files.size(); - } - - void reserve(std::size_t n) - { - m_Files.reserve(n); - } - - bool removeFile(FileEntry::Index index); - void removeOrigin(FileEntry::Index index, int originID); - void removeOriginMulti(std::set indices, int originID); - - void sortOrigins(); - -private: - using FileMap = std::vector; - - mutable std::mutex m_Mutex; - FileMap m_Files; - boost::shared_ptr m_OriginConnection; - std::atomic m_NextIndex; - - void unregisterFile(FileEntry::Ptr file); - FileEntry::Index generateIndex(); -}; - - -struct DirectoryEntryFileKey -{ - DirectoryEntryFileKey(std::wstring v) - : value(std::move(v)), hash(getHash(value)) - { - } - - bool operator==(const DirectoryEntryFileKey& o) const - { - return (value == o.value); - } - - static std::size_t getHash(const std::wstring& value) - { - return std::hash()(value); - } - - std::wstring value; - const std::size_t hash; -}; - - class DirectoryEntry { public: @@ -373,6 +59,10 @@ public: boost::shared_ptr fileRegister, boost::shared_ptr originConnection); + // noncopyable + DirectoryEntry(const DirectoryEntry&) = delete; + DirectoryEntry& operator=(const DirectoryEntry&) = delete; + ~DirectoryEntry(); void clear(); @@ -439,7 +129,7 @@ public: int anyOrigin() const; - std::vector getFiles() const; + std::vector getFiles() const; void getSubDirectories( std::vector::const_iterator &begin, @@ -486,7 +176,7 @@ public: } } - FileEntry::Ptr getFileByIndex(FileEntry::Index index) const + FileEntryPtr getFileByIndex(FileIndex index) const { return m_FileRegister->getFile(index); } @@ -500,8 +190,8 @@ public: * @param name name of the file * @return fileentry object for the file or nullptr if no file matches */ - const FileEntry::Ptr findFile(const std::wstring &name, bool alreadyLowerCase=false) const; - const FileEntry::Ptr findFile(const FileKey& key) const; + const FileEntryPtr findFile(const std::wstring &name, bool alreadyLowerCase=false) const; + const FileEntryPtr findFile(const FileKey& key) const; bool hasFile(const std::wstring& name) const; bool containsArchive(std::wstring archiveName); @@ -512,10 +202,10 @@ public: // if directory is not nullptr, the referenced variable will be set to the // path containing the file // - const FileEntry::Ptr searchFile( + const FileEntryPtr searchFile( const std::wstring &path, const DirectoryEntry **directory=nullptr) const; - void removeFile(FileEntry::Index index); + void removeFile(FileIndex index); // remove the specified file from the tree. This can be a path leading to a // file in a subdirectory @@ -535,13 +225,13 @@ public: const std::wstring &originName, const std::wstring &directory, int priority, DirectoryStats& stats); - void removeFiles(const std::set &indices); + void removeFiles(const std::set &indices); void dump(const std::wstring& file) const; private: - using FilesMap = std::map; - using FilesLookup = std::unordered_map; + using FilesMap = std::map; + using FilesLookup = std::unordered_map; using SubDirectories = std::vector; using SubDirectoriesLookup = std::unordered_map; @@ -563,13 +253,11 @@ private: mutable std::mutex m_OriginsMutex; - DirectoryEntry(const DirectoryEntry &reference); - - FileEntry::Ptr insert( + FileEntryPtr insert( std::wstring_view fileName, FilesOrigin &origin, FILETIME fileTime, std::wstring_view archive, int order, DirectoryStats& stats); - FileEntry::Ptr insert( + FileEntryPtr insert( env::File& file, FilesOrigin &origin, std::wstring_view archive, int order, DirectoryStats& stats); @@ -599,9 +287,9 @@ private: void addDirectoryToList(DirectoryEntry* e, std::wstring nameLc); void removeDirectoryFromList(SubDirectories::iterator itor); - void addFileToList(std::wstring fileNameLower, FileEntry::Index index); - void removeFileFromList(FileEntry::Index index); - void removeFilesFromList(const std::set& indices); + void addFileToList(std::wstring fileNameLower, FileIndex index); + void removeFileFromList(FileIndex index); + void removeFilesFromList(const std::set& indices); struct Context; static void onDirectoryStart(Context* cx, std::wstring_view path); @@ -624,4 +312,4 @@ namespace std } } -#endif // DIRECTORYENTRY_H +#endif // MO_REGISTER_DIRECTORYENTRY_INCLUDED diff --git a/src/shared/fileentry.cpp b/src/shared/fileentry.cpp new file mode 100644 index 00000000..d4e1beb4 --- /dev/null +++ b/src/shared/fileentry.cpp @@ -0,0 +1,251 @@ +#include "fileentry.h" +#include "directoryentry.h" +#include "filesorigin.h" + +namespace MOShared +{ + +FileEntry::FileEntry() : + m_Index(UINT_MAX), m_Name(), m_Origin(-1), m_Parent(nullptr), + m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) +{ +} + +FileEntry::FileEntry(Index index, std::wstring name, DirectoryEntry *parent) : + m_Index(index), m_Name(std::move(name)), m_Origin(-1), m_Archive(L"", -1), m_Parent(parent), + m_FileSize(NoFileSize), m_CompressedFileSize(NoFileSize) +{ +} + +void FileEntry::addOrigin( + int origin, FILETIME fileTime, std::wstring_view archive, int order) +{ + std::scoped_lock lock(m_OriginsMutex); + + if (m_Parent != nullptr) { + m_Parent->propagateOrigin(origin); + } + + if (m_Origin == -1) { + // If this file has no previous origin, this mod is now the origin with no + // alternatives + m_Origin = origin; + m_FileTime = fileTime; + m_Archive = std::pair(std::wstring(archive.begin(), archive.end()), order); + } + else if ( + (m_Parent != nullptr) && ( + (m_Parent->getOriginByID(origin).getPriority() > m_Parent->getOriginByID(m_Origin).getPriority()) || + (archive.size() == 0 && m_Archive.first.size() > 0 )) + ) { + // If this mod has a higher priority than the origin mod OR + // this mod has a loose file and the origin mod has an archived file, + // this mod is now the origin and the previous origin is the first alternative + + auto itor = std::find_if( + m_Alternatives.begin(), m_Alternatives.end(), + [&](auto&& i) { return i.first == m_Origin; }); + + if (itor == m_Alternatives.end()) { + m_Alternatives.push_back({m_Origin, m_Archive}); + } + + m_Origin = origin; + m_FileTime = fileTime; + m_Archive = std::pair(std::wstring(archive.begin(), archive.end()), order); + } + else { + // This mod is just an alternative + bool found = false; + + if (m_Origin == origin) { + // already an origin + return; + } + + for (auto iter = m_Alternatives.begin(); iter != m_Alternatives.end(); ++iter) { + if (iter->first == origin) { + // already an origin + return; + } + + if ((m_Parent != nullptr) && + (m_Parent->getOriginByID(iter->first).getPriority() < m_Parent->getOriginByID(origin).getPriority())) { + m_Alternatives.insert(iter, {origin, {std::wstring(archive.begin(), archive.end()), order}}); + found = true; + break; + } + } + + if (!found) { + m_Alternatives.push_back({origin, {std::wstring(archive.begin(), archive.end()), order}}); + } + } +} + +bool FileEntry::removeOrigin(int origin) +{ + std::scoped_lock lock(m_OriginsMutex); + + if (m_Origin == origin) { + if (!m_Alternatives.empty()) { + // find alternative with the highest priority + auto currentIter = m_Alternatives.begin(); + for (auto iter = m_Alternatives.begin(); iter != m_Alternatives.end(); ++iter) { + if (iter->first != origin) { + //Both files are not from archives. + if (!iter->second.first.size() && !currentIter->second.first.size()) { + if ((m_Parent->getOriginByID(iter->first).getPriority() > m_Parent->getOriginByID(currentIter->first).getPriority())) { + currentIter = iter; + } + } + else { + //Both files are from archives + if (iter->second.first.size() && currentIter->second.first.size()) { + if (iter->second.second > currentIter->second.second) { + currentIter = iter; + } + } + else { + //Only one of the two is an archive, so we change currentIter only if he is the archive one. + if (currentIter->second.first.size()) { + currentIter = iter; + } + } + } + } + } + + int currentID = currentIter->first; + m_Archive = currentIter->second; + m_Alternatives.erase(currentIter); + + m_Origin = currentID; + } else { + m_Origin = -1; + m_Archive = std::pair(L"", -1); + return true; + } + } else { + auto newEnd = std::remove_if( + m_Alternatives.begin(), m_Alternatives.end(), + [&](auto &i) { return i.first == origin; }); + + if (newEnd != m_Alternatives.end()) { + m_Alternatives.erase(newEnd, m_Alternatives.end()); + } + } + return false; +} + +void FileEntry::sortOrigins() +{ + std::scoped_lock lock(m_OriginsMutex); + + m_Alternatives.push_back({m_Origin, m_Archive}); + + std::sort(m_Alternatives.begin(), m_Alternatives.end(), [&](auto&& LHS, auto&& RHS) { + if (!LHS.second.first.size() && !RHS.second.first.size()) { + int l = m_Parent->getOriginByID(LHS.first).getPriority(); + if (l < 0) { + l = INT_MAX; + } + + int r = m_Parent->getOriginByID(RHS.first).getPriority(); + if (r < 0) { + r = INT_MAX; + } + + return l < r; + } + + if (LHS.second.first.size() && RHS.second.first.size()) { + int l = LHS.second.second; if (l < 0) l = INT_MAX; + int r = RHS.second.second; if (r < 0) r = INT_MAX; + + return l < r; + } + + if (RHS.second.first.size()) { + return false; + } + + return true; + }); + + if (!m_Alternatives.empty()) { + m_Origin = m_Alternatives.back().first; + m_Archive = m_Alternatives.back().second; + m_Alternatives.pop_back(); + } +} + +bool FileEntry::isFromArchive(std::wstring archiveName) const +{ + std::scoped_lock lock(m_OriginsMutex); + + if (archiveName.length() == 0) { + return m_Archive.first.length() != 0; + } + + if (m_Archive.first.compare(archiveName) == 0) { + return true; + } + + for (auto alternative : m_Alternatives) { + if (alternative.second.first.compare(archiveName) == 0) { + return true; + } + } + + return false; +} + +std::wstring FileEntry::getFullPath(int originID) const +{ + std::scoped_lock lock(m_OriginsMutex); + + if (originID == -1) { + bool ignore = false; + originID = getOrigin(ignore); + } + + // base directory for origin + const auto* o = m_Parent->findOriginByID(originID); + if (!o) { + return {}; + } + + std::wstring result = o->getPath(); + + // all intermediate directories + recurseParents(result, m_Parent); + + return result + L"\\" + m_Name; +} + +std::wstring FileEntry::getRelativePath() const +{ + std::wstring result; + + // all intermediate directories + recurseParents(result, m_Parent); + + return result + L"\\" + m_Name; +} + +bool FileEntry::recurseParents(std::wstring &path, const DirectoryEntry *parent) const +{ + if (parent == nullptr) { + return false; + } else { + // don't append the topmost parent because it is the virtual data-root + if (recurseParents(path, parent->getParent())) { + path.append(L"\\").append(parent->getName()); + } + + return true; + } +} + +} // namespace diff --git a/src/shared/fileentry.h b/src/shared/fileentry.h new file mode 100644 index 00000000..52ce89cd --- /dev/null +++ b/src/shared/fileentry.h @@ -0,0 +1,125 @@ +#ifndef MO_REGISTER_FILEENTRY_INCLUDED +#define MO_REGISTER_FILEENTRY_INCLUDED + +#include "fileregisterfwd.h" + +namespace MOShared +{ + +class FileEntry +{ +public: + static constexpr uint64_t NoFileSize = + std::numeric_limits::max(); + + typedef unsigned int Index; + typedef boost::shared_ptr Ptr; + + FileEntry(); + FileEntry(Index index, std::wstring name, DirectoryEntry *parent); + + // noncopyable + FileEntry(const FileEntry&) = delete; + FileEntry& operator=(const FileEntry&) = delete; + + Index getIndex() const + { + return m_Index; + } + + void addOrigin( + int origin, FILETIME fileTime, std::wstring_view archive, int order); + + // remove the specified origin from the list of origins that contain this + // file. if no origin is left, the file is effectively deleted and true is + // returned. otherwise, false is returned + bool removeOrigin(int origin); + + void sortOrigins(); + + // gets the list of alternative origins (origins with lower priority than + // the primary one). if sortOrigins has been called, it is sorted by priority + // (ascending) + const AlternativesVector &getAlternatives() const + { + return m_Alternatives; + } + + const std::wstring &getName() const + { + return m_Name; + } + + int getOrigin() const + { + return m_Origin; + } + + int getOrigin(bool &archive) const + { + archive = (m_Archive.first.length() != 0); + return m_Origin; + } + + const std::pair &getArchive() const + { + return m_Archive; + } + + bool isFromArchive(std::wstring archiveName = L"") const; + + // if originID is -1, uses the main origin; if this file doesn't exist in the + // given origin, returns an empty string + // + std::wstring getFullPath(int originID=-1) const; + + std::wstring getRelativePath() const; + + DirectoryEntry *getParent() + { + return m_Parent; + } + + void setFileTime(FILETIME fileTime) const + { + m_FileTime = fileTime; + } + + FILETIME getFileTime() const + { + return m_FileTime; + } + + void setFileSize(uint64_t size, uint64_t compressedSize) + { + m_FileSize = size; + m_CompressedFileSize = compressedSize; + } + + uint64_t getFileSize() const + { + return m_FileSize; + } + + uint64_t getCompressedFileSize() const + { + return m_CompressedFileSize; + } + +private: + Index m_Index; + std::wstring m_Name; + int m_Origin; + std::pair m_Archive; + AlternativesVector m_Alternatives; + DirectoryEntry *m_Parent; + mutable FILETIME m_FileTime; + uint64_t m_FileSize, m_CompressedFileSize; + mutable std::mutex m_OriginsMutex; + + bool recurseParents(std::wstring &path, const DirectoryEntry *parent) const; +}; + +} // namespace + +#endif // MO_REGISTER_FILEENTRY_INCLUDED diff --git a/src/shared/fileregister.cpp b/src/shared/fileregister.cpp new file mode 100644 index 00000000..4add0455 --- /dev/null +++ b/src/shared/fileregister.cpp @@ -0,0 +1,184 @@ +#include "fileregister.h" +#include "fileentry.h" +#include "directoryentry.h" +#include "originconnection.h" +#include "filesorigin.h" +#include + +namespace MOShared +{ + +using namespace MOBase; + +FileRegister::FileRegister(boost::shared_ptr originConnection) + : m_OriginConnection(originConnection), m_NextIndex(0) +{ +} + +bool FileRegister::indexValid(FileIndex index) const +{ + std::scoped_lock lock(m_Mutex); + + if (index < m_Files.size()) { + return (m_Files[index].get() != nullptr); + } + + return false; +} + +FileEntryPtr FileRegister::createFile( + std::wstring name, DirectoryEntry *parent, DirectoryStats& stats) +{ + const auto index = generateIndex(); + auto p = FileEntryPtr(new FileEntry(index, std::move(name), parent)); + + { + std::scoped_lock lock(m_Mutex); + + if (index >= m_Files.size()) { + m_Files.resize(index + 1); + } + + m_Files[index] = p; + } + + return p; +} + +FileIndex FileRegister::generateIndex() +{ + return m_NextIndex++; +} + +FileEntryPtr FileRegister::getFile(FileIndex index) const +{ + std::scoped_lock lock(m_Mutex); + + if (index < m_Files.size()) { + return m_Files[index]; + } else { + return {}; + } +} + +bool FileRegister::removeFile(FileIndex index) +{ + std::scoped_lock lock(m_Mutex); + + if (index < m_Files.size()) { + FileEntryPtr p; + m_Files[index].swap(p); + + if (p) { + unregisterFile(p); + return true; + } + } + + log::error(QObject::tr("invalid file index for remove: {}").toStdString(), index); + return false; +} + +void FileRegister::removeOrigin(FileIndex index, int originID) +{ + std::unique_lock lock(m_Mutex); + + if (index < m_Files.size()) { + FileEntryPtr& p = m_Files[index]; + + if (p) { + if (p->removeOrigin(originID)) { + m_Files[index] = {}; + lock.unlock(); + unregisterFile(p); + return; + } + } + } + + log::error(QObject::tr("invalid file index for remove (for origin): {}").toStdString(), index); +} + +void FileRegister::removeOriginMulti( + std::set indices, int originID) +{ + std::vector removedFiles; + + { + std::scoped_lock lock(m_Mutex); + + for (auto iter = indices.begin(); iter != indices.end(); ) { + const auto index = *iter; + + if (index < m_Files.size()) { + const auto& p = m_Files[index]; + + if (p && p->removeOrigin(originID)) { + removedFiles.push_back(p); + m_Files[index] = {}; + ++iter; + continue; + } + } + + iter = indices.erase(iter); + } + } + + // optimization: this is only called when disabling an origin and in this case + // we don't have to remove the file from the origin + + // need to remove files from their parent directories. multiple ways to go + // about this: + // a) for each file, search its parents file-list (preferably by name) and + // remove what is found + // b) gather the parent directories, go through the file list for each once + // and remove all files that have been removed + // + // the latter should be faster when there are many files in few directories. + // since this is called only when disabling an origin that is probably + // frequently the case + + std::set parents; + for (const FileEntryPtr &file : removedFiles) { + if (file->getParent() != nullptr) { + parents.insert(file->getParent()); + } + } + + for (DirectoryEntry *parent : parents) { + parent->removeFiles(indices); + } +} + +void FileRegister::sortOrigins() +{ + std::scoped_lock lock(m_Mutex); + + for (auto&& p : m_Files) { + if (p) { + p->sortOrigins(); + } + } +} + +void FileRegister::unregisterFile(FileEntryPtr file) +{ + bool ignore; + + // unregister from origin + int originID = file->getOrigin(ignore); + m_OriginConnection->getByID(originID).removeFile(file->getIndex()); + const auto& alternatives = file->getAlternatives(); + + for (auto iter = alternatives.begin(); iter != alternatives.end(); ++iter) { + m_OriginConnection->getByID(iter->first).removeFile(file->getIndex()); + } + + // unregister from directory + if (file->getParent() != nullptr) { + file->getParent()->removeFile(file->getIndex()); + } +} + +} // namespace diff --git a/src/shared/fileregister.h b/src/shared/fileregister.h new file mode 100644 index 00000000..d5e360c1 --- /dev/null +++ b/src/shared/fileregister.h @@ -0,0 +1,58 @@ +#ifndef MO_REGISTER_FILESREGISTER_INCLUDED +#define MO_REGISTER_FILESREGISTER_INCLUDED + +#include "fileregisterfwd.h" +#include +#include + +namespace MOShared +{ + +class FileRegister +{ +public: + FileRegister(boost::shared_ptr originConnection); + + // noncopyable + FileRegister(const FileRegister&) = delete; + FileRegister& operator=(const FileRegister&) = delete; + + bool indexValid(FileIndex index) const; + + FileEntryPtr createFile( + std::wstring name, DirectoryEntry *parent, DirectoryStats& stats); + + FileEntryPtr getFile(FileIndex index) const; + + size_t highestCount() const + { + std::scoped_lock lock(m_Mutex); + return m_Files.size(); + } + + void reserve(std::size_t n) + { + m_Files.reserve(n); + } + + bool removeFile(FileIndex index); + void removeOrigin(FileIndex index, int originID); + void removeOriginMulti(std::set indices, int originID); + + void sortOrigins(); + +private: + using FileMap = std::vector; + + mutable std::mutex m_Mutex; + FileMap m_Files; + boost::shared_ptr m_OriginConnection; + std::atomic m_NextIndex; + + void unregisterFile(FileEntryPtr file); + FileIndex generateIndex(); +}; + +} // namespace + +#endif // MO_REGISTER_FILESREGISTER_INCLUDED diff --git a/src/shared/fileregisterfwd.h b/src/shared/fileregisterfwd.h new file mode 100644 index 00000000..b8d33378 --- /dev/null +++ b/src/shared/fileregisterfwd.h @@ -0,0 +1,89 @@ +#ifndef MO_REGISTER_FILEREGISTERFWD_INCLUDED +#define MO_REGISTER_FILEREGISTERFWD_INCLUDED + +namespace MOShared +{ + +struct DirectoryEntryFileKey +{ + DirectoryEntryFileKey(std::wstring v) + : value(std::move(v)), hash(getHash(value)) + { + } + + bool operator==(const DirectoryEntryFileKey& o) const + { + return (value == o.value); + } + + static std::size_t getHash(const std::wstring& value) + { + return std::hash()(value); + } + + std::wstring value; + const std::size_t hash; +}; + + +class DirectoryEntry; +class OriginConnection; +class FileRegister; +class FilesOrigin; +class FileEntry; +struct DirectoryStats; + +using FileEntryPtr = boost::shared_ptr; +using FileIndex = unsigned int; + +// a vector of {originId, {archiveName, order}} +// +// if a file is in an archive, archiveName is the name of the bsa and order +// is the order of the associated plugin in the plugins list +// +// is a file is not in an archive, archiveName is empty and order is usually +// -1 +using AlternativesVector = std::vector>>; + +struct DirectoryStats +{ + static constexpr bool EnableInstrumentation = false; + + std::string mod; + + std::chrono::nanoseconds dirTimes; + std::chrono::nanoseconds fileTimes; + std::chrono::nanoseconds sortTimes; + + std::chrono::nanoseconds subdirLookupTimes; + std::chrono::nanoseconds addDirectoryTimes; + + std::chrono::nanoseconds filesLookupTimes; + std::chrono::nanoseconds addFileTimes; + std::chrono::nanoseconds addOriginToFileTimes; + std::chrono::nanoseconds addFileToOriginTimes; + std::chrono::nanoseconds addFileToRegisterTimes; + + int64_t originExists; + int64_t originCreate; + int64_t originsNeededEnabled; + + int64_t subdirExists; + int64_t subdirCreate; + + int64_t fileExists; + int64_t fileCreate; + int64_t filesInsertedInRegister; + int64_t filesAssignedInRegister; + + DirectoryStats(); + + DirectoryStats& operator+=(const DirectoryStats& o); + + static std::string csvHeader(); + std::string toCsv() const; +}; + +} // namespace + +#endif // MO_REGISTER_FILEREGISTERFWD_INCLUDED diff --git a/src/shared/filesorigin.cpp b/src/shared/filesorigin.cpp new file mode 100644 index 00000000..47ae4b94 --- /dev/null +++ b/src/shared/filesorigin.cpp @@ -0,0 +1,137 @@ +#include "filesorigin.h" +#include "originconnection.h" +#include "fileregister.h" +#include "fileentry.h" + +namespace MOShared +{ + +std::wstring tail(const std::wstring &source, const size_t count) +{ + if (count >= source.length()) { + return source; + } + + return source.substr(source.length() - count); +} + + +FilesOrigin::FilesOrigin() + : m_ID(0), m_Disabled(false), m_Name(), m_Path(), m_Priority(0) +{ +} + +/*FilesOrigin::FilesOrigin(const FilesOrigin &reference) + : m_ID(reference.m_ID) + , m_Disabled(reference.m_Disabled) + , m_Name(reference.m_Name) + , m_Path(reference.m_Path) + , m_Priority(reference.m_Priority) + , m_FileRegister(reference.m_FileRegister) + , m_OriginConnection(reference.m_OriginConnection) +{ +}*/ + +FilesOrigin::FilesOrigin( + int ID, const std::wstring &name, const std::wstring &path, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection) : + m_ID(ID), m_Disabled(false), m_Name(name), m_Path(path), + m_Priority(priority), m_FileRegister(fileRegister), + m_OriginConnection(originConnection) +{ +} + +void FilesOrigin::setPriority(int priority) +{ + m_OriginConnection.lock()->changePriorityLookup(m_Priority, priority); + + m_Priority = priority; +} + +void FilesOrigin::setName(const std::wstring &name) +{ + m_OriginConnection.lock()->changeNameLookup(m_Name, name); + + // change path too + if (tail(m_Path, m_Name.length()) == m_Name) { + m_Path = m_Path.substr(0, m_Path.length() - m_Name.length()).append(name); + } + + m_Name = name; +} + +std::vector FilesOrigin::getFiles() const +{ + std::vector result; + + { + std::scoped_lock lock(m_Mutex); + + for (FileIndex fileIdx : m_Files) { + if (FileEntryPtr p = m_FileRegister.lock()->getFile(fileIdx)) { + result.push_back(p); + } + } + } + + return result; +} + +FileEntryPtr FilesOrigin::findFile(FileIndex index) const +{ + return m_FileRegister.lock()->getFile(index); +} + +void FilesOrigin::enable(bool enabled) +{ + DirectoryStats dummy; + enable(enabled, dummy); +} + +void FilesOrigin::enable(bool enabled, DirectoryStats& stats) +{ + if (!enabled) { + ++stats.originsNeededEnabled; + + std::set copy; + + { + std::scoped_lock lock(m_Mutex); + copy = m_Files; + m_Files.clear(); + } + + m_FileRegister.lock()->removeOriginMulti(copy, m_ID); + } + + m_Disabled = !enabled; +} + +void FilesOrigin::removeFile(FileIndex index) +{ + std::scoped_lock lock(m_Mutex); + + auto iter = m_Files.find(index); + + if (iter != m_Files.end()) { + m_Files.erase(iter); + } +} + +bool FilesOrigin::containsArchive(std::wstring archiveName) +{ + std::scoped_lock lock(m_Mutex); + + for (FileIndex fileIdx : m_Files) { + if (FileEntryPtr p = m_FileRegister.lock()->getFile(fileIdx)) { + if (p->isFromArchive(archiveName)) { + return true; + } + } + } + + return false; +} + +} // namespace diff --git a/src/shared/filesorigin.h b/src/shared/filesorigin.h new file mode 100644 index 00000000..4ea21f57 --- /dev/null +++ b/src/shared/filesorigin.h @@ -0,0 +1,87 @@ +#ifndef MO_REGISTER_FILESORIGIN_INCLUDED +#define MO_REGISTER_FILESORIGIN_INCLUDED + +#include "fileregisterfwd.h" + +namespace MOShared +{ + +// represents a mod or the data directory, providing files to the tree +class FilesOrigin +{ +public: + FilesOrigin(); +// FilesOrigin(const FilesOrigin &reference); + + FilesOrigin( + int ID, const std::wstring &name, const std::wstring &path, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection); + + // noncopyable + FilesOrigin(const FilesOrigin&) = delete; + FilesOrigin& operator=(const FilesOrigin&) = delete; + FilesOrigin(FilesOrigin&&) = default; + FilesOrigin& operator=(FilesOrigin&&) = default; + + // sets priority for this origin, but it will overwrite the existing mapping + // for this priority, the previous origin will no longer be referenced + void setPriority(int priority); + + int getPriority() const + { + return m_Priority; + } + + void setName(const std::wstring &name); + const std::wstring &getName() const + { + return m_Name; + } + + int getID() const + { + return m_ID; + } + + const std::wstring &getPath() const + { + return m_Path; + } + + std::vector getFiles() const; + FileEntryPtr findFile(FileIndex index) const; + + void enable(bool enabled, DirectoryStats& stats); + void enable(bool enabled); + + bool isDisabled() const + { + return m_Disabled; + } + + void addFile(FileIndex index) + { + std::scoped_lock lock(m_Mutex); + m_Files.insert(index); + } + + void removeFile(FileIndex index); + + bool containsArchive(std::wstring archiveName); + +private: + int m_ID; + bool m_Disabled; + std::set m_Files; + std::wstring m_Name; + std::wstring m_Path; + int m_Priority; + boost::weak_ptr m_FileRegister; + boost::weak_ptr m_OriginConnection; + mutable std::mutex m_Mutex; +}; + +} // namespace + +#endif // MO_REGISTER_FILESORIGIN_INCLUDED diff --git a/src/shared/originconnection.cpp b/src/shared/originconnection.cpp new file mode 100644 index 00000000..a56126cc --- /dev/null +++ b/src/shared/originconnection.cpp @@ -0,0 +1,145 @@ +#include "originconnection.h" +#include "filesorigin.h" +#include "util.h" +#include + +namespace MOShared +{ + +using namespace MOBase; + +OriginConnection::OriginConnection() + : m_NextID(0) +{ +} + +std::pair OriginConnection::getOrCreate( + const std::wstring &originName, const std::wstring &directory, int priority, + const boost::shared_ptr& fileRegister, + const boost::shared_ptr& originConnection, + DirectoryStats& stats) +{ + std::unique_lock lock(m_Mutex); + + auto itor = m_OriginsNameMap.find(originName); + + if (itor == m_OriginsNameMap.end()) { + FilesOrigin& origin = createOriginNoLock( + originName, directory, priority, fileRegister, originConnection); + + return {origin, true}; + } else { + FilesOrigin& origin = m_Origins[itor->second]; + lock.unlock(); + + origin.enable(true, stats); + return {origin, false}; + } +} + +FilesOrigin& OriginConnection::createOrigin( + const std::wstring &originName, const std::wstring &directory, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection) +{ + std::scoped_lock lock(m_Mutex); + + return createOriginNoLock( + originName, directory, priority, fileRegister, originConnection); +} + +bool OriginConnection::exists(const std::wstring &name) +{ + std::scoped_lock lock(m_Mutex); + return m_OriginsNameMap.find(name) != m_OriginsNameMap.end(); +} + +FilesOrigin& OriginConnection::getByID(Index ID) +{ + std::scoped_lock lock(m_Mutex); + return m_Origins[ID]; +} + +const FilesOrigin* OriginConnection::findByID(Index ID) const +{ + std::scoped_lock lock(m_Mutex); + + auto itor = m_Origins.find(ID); + + if (itor == m_Origins.end()) { + return nullptr; + } else { + return &itor->second; + } +} + +FilesOrigin& OriginConnection::getByName(const std::wstring &name) +{ + std::scoped_lock lock(m_Mutex); + + std::map::iterator iter = m_OriginsNameMap.find(name); + + if (iter != m_OriginsNameMap.end()) { + return m_Origins[iter->second]; + } else { + std::ostringstream stream; + stream << QObject::tr("invalid origin name: ").toStdString() << ToString(name, true); + throw std::runtime_error(stream.str()); + } +} + +void OriginConnection::changePriorityLookup(int oldPriority, int newPriority) +{ + std::scoped_lock lock(m_Mutex); + + auto iter = m_OriginsPriorityMap.find(oldPriority); + + if (iter != m_OriginsPriorityMap.end()) { + Index idx = iter->second; + m_OriginsPriorityMap.erase(iter); + m_OriginsPriorityMap[newPriority] = idx; + } +} + +void OriginConnection::changeNameLookup(const std::wstring &oldName, const std::wstring &newName) +{ + std::scoped_lock lock(m_Mutex); + + auto iter = m_OriginsNameMap.find(oldName); + + if (iter != m_OriginsNameMap.end()) { + Index idx = iter->second; + m_OriginsNameMap.erase(iter); + m_OriginsNameMap[newName] = idx; + } else { + log::error(QObject::tr("failed to change name lookup from {} to {}").toStdString(), oldName, newName); + } +} + +OriginConnection::Index OriginConnection::createID() +{ + return m_NextID++; +} + +FilesOrigin& OriginConnection::createOriginNoLock( + const std::wstring &originName, const std::wstring &directory, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection) +{ + int newID = createID(); + + auto itor = m_Origins.emplace( + std::piecewise_construct, + std::forward_as_tuple(newID), + std::forward_as_tuple( + newID, originName, directory, priority, + fileRegister, originConnection)) + .first; + + m_OriginsNameMap.insert({originName, newID}); + m_OriginsPriorityMap.insert({priority, newID}); + + return itor->second; +} + +} // namespace diff --git a/src/shared/originconnection.h b/src/shared/originconnection.h new file mode 100644 index 00000000..1fbb07ac --- /dev/null +++ b/src/shared/originconnection.h @@ -0,0 +1,59 @@ +#ifndef MO_REGISTER_ORIGINCONNECTION_INCLUDED +#define MO_REGISTER_ORIGINCONNECTION_INCLUDED + +#include "fileregisterfwd.h" + +namespace MOShared +{ + +class OriginConnection +{ +public: + typedef int Index; + static const int INVALID_INDEX = INT_MIN; + + OriginConnection(); + + // noncopyable + OriginConnection(const OriginConnection&) = delete; + OriginConnection& operator=(const OriginConnection&) = delete; + + std::pair getOrCreate( + const std::wstring &originName, const std::wstring &directory, int priority, + const boost::shared_ptr& fileRegister, + const boost::shared_ptr& originConnection, + DirectoryStats& stats); + + FilesOrigin& createOrigin( + const std::wstring &originName, const std::wstring &directory, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection); + + bool exists(const std::wstring &name); + + FilesOrigin &getByID(Index ID); + const FilesOrigin* findByID(Index ID) const; + FilesOrigin &getByName(const std::wstring &name); + + void changePriorityLookup(int oldPriority, int newPriority); + + void changeNameLookup(const std::wstring &oldName, const std::wstring &newName); + +private: + Index m_NextID; + std::map m_Origins; + std::map m_OriginsNameMap; + std::map m_OriginsPriorityMap; + mutable std::mutex m_Mutex; + + Index createID(); + + FilesOrigin& createOriginNoLock( + const std::wstring &originName, const std::wstring &directory, int priority, + boost::shared_ptr fileRegister, + boost::shared_ptr originConnection); +}; + +} // namespace + +#endif // MO_REGISTER_ORIGINCONNECTION_INCLUDED diff --git a/src/syncoverwritedialog.cpp b/src/syncoverwritedialog.cpp index b1643b2d..0afb1121 100644 --- a/src/syncoverwritedialog.cpp +++ b/src/syncoverwritedialog.cpp @@ -18,8 +18,11 @@ along with Mod Organizer. If not, see . */ #include "syncoverwritedialog.h" - +#include "shared/directoryentry.h" +#include "shared/fileentry.h" +#include "shared/filesorigin.h" #include "ui_syncoverwritedialog.h" + #include #include #include @@ -92,7 +95,7 @@ void SyncOverwriteDialog::readTree(const QString &path, DirectoryEntry *director newItem = nullptr; } } else { - const FileEntry::Ptr entry = directoryStructure->findFile(ToWString(file)); + const FileEntryPtr entry = directoryStructure->findFile(ToWString(file)); QComboBox* combo = new QComboBox(ui->syncTree); combo->addItem(tr(""), -1); if (entry.get() != nullptr) { diff --git a/src/syncoverwritedialog.h b/src/syncoverwritedialog.h index 79b30a8d..cb4c92fb 100644 --- a/src/syncoverwritedialog.h +++ b/src/syncoverwritedialog.h @@ -20,11 +20,9 @@ along with Mod Organizer. If not, see . #ifndef SYNCOVERWRITEDIALOG_H #define SYNCOVERWRITEDIALOG_H - #include "tutorabledialog.h" +#include "shared/fileregisterfwd.h" #include -#include - namespace Ui { class SyncOverwriteDialog; @@ -33,9 +31,12 @@ class SyncOverwriteDialog; class SyncOverwriteDialog : public MOBase::TutorableDialog { Q_OBJECT - + public: - explicit SyncOverwriteDialog(const QString &path, MOShared::DirectoryEntry *directoryStructure, QWidget *parent = 0); + explicit SyncOverwriteDialog( + const QString &path, MOShared::DirectoryEntry *directoryStructure, + QWidget *parent = 0); + ~SyncOverwriteDialog(); void apply(const QString &modDirectory); -- cgit v1.3.1 From dbb3f5117a3f977bbef34ebdab217f379429d1f2 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sat, 15 Feb 2020 14:22:42 -0500 Subject: removed directoryrefresher.h from organizercore.h --- src/directoryrefresher.cpp | 174 ++++++++++++++++++++++++++++++++---------- src/directoryrefresher.h | 8 +- src/organizercore.cpp | 19 ++--- src/organizercore.h | 41 ++++------ src/shared/directoryentry.cpp | 99 ------------------------ 5 files changed, 160 insertions(+), 181 deletions(-) (limited to 'src/directoryrefresher.h') diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index 3b92389a..8a64d4a9 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -45,22 +45,141 @@ using namespace MOBase; using namespace MOShared; -DirectoryRefresher::DirectoryRefresher(std::size_t threadCount) - : m_DirectoryStructure(nullptr), m_threadCount(threadCount), m_lastFileCount(0) +DirectoryStats::DirectoryStats() +{ + std::memset(this, 0, sizeof(DirectoryStats)); +} + +DirectoryStats& DirectoryStats::operator+=(const DirectoryStats& o) +{ + dirTimes += o.dirTimes; + fileTimes += o.fileTimes; + sortTimes += o.sortTimes; + + subdirLookupTimes += o.subdirLookupTimes; + addDirectoryTimes += o.addDirectoryTimes; + + filesLookupTimes += o.filesLookupTimes; + addFileTimes += o.addFileTimes; + addOriginToFileTimes += o.addOriginToFileTimes; + addFileToOriginTimes += o.addFileToOriginTimes; + addFileToRegisterTimes += o.addFileToRegisterTimes; + + originExists += o.originExists; + originCreate += o.originCreate; + originsNeededEnabled += o.originsNeededEnabled; + + subdirExists += o.subdirExists; + subdirCreate += o.subdirCreate; + + fileExists += o.fileExists; + fileCreate += o.fileCreate; + filesInsertedInRegister += o.filesInsertedInRegister; + filesAssignedInRegister += o.filesAssignedInRegister; + + return *this; +} + +std::string DirectoryStats::csvHeader() +{ + QStringList sl = { + "dirTimes", + "fileTimes", + "sortTimes", + "subdirLookupTimes", + "addDirectoryTimes", + "filesLookupTimes", + "addFileTimes", + "addOriginToFileTimes", + "addFileToOriginTimes", + "addFileToRegisterTimes", + "originExists", + "originCreate", + "originsNeededEnabled", + "subdirExists", + "subdirCreate", + "fileExists", + "fileCreate", + "filesInsertedInRegister", + "filesAssignedInRegister"}; + + return sl.join(",").toStdString(); +} + +std::string DirectoryStats::toCsv() const { + QStringList oss; + + auto s = [](auto ns) { + return ns.count() / 1000.0 / 1000.0 / 1000.0; + }; + + oss + << QString::number(s(dirTimes)) + << QString::number(s(fileTimes)) + << QString::number(s(sortTimes)) + + << QString::number(s(subdirLookupTimes)) + << QString::number(s(addDirectoryTimes)) + + << QString::number(s(filesLookupTimes)) + << QString::number(s(addFileTimes)) + << QString::number(s(addOriginToFileTimes)) + << QString::number(s(addFileToOriginTimes)) + << QString::number(s(addFileToRegisterTimes)) + + << QString::number(originExists) + << QString::number(originCreate) + << QString::number(originsNeededEnabled) + + << QString::number(subdirExists) + << QString::number(subdirCreate) + + << QString::number(fileExists) + << QString::number(fileCreate) + << QString::number(filesInsertedInRegister) + << QString::number(filesAssignedInRegister); + + return oss.join(",").toStdString(); +} + +void dumpStats(std::vector& stats) +{ + static int run = 0; + static const std::string file("c:\\tmp\\data.csv"); + + if (run == 0) { + std::ofstream out(file, std::ios::out|std::ios::trunc); + out << fmt::format("what,run,{}", DirectoryStats::csvHeader()) << "\n"; + } + + std::sort(stats.begin(), stats.end(), [](auto&& a, auto&& b){ + return (naturalCompare(QString::fromStdString(a.mod), QString::fromStdString(b.mod)) < 0); + }); + + std::ofstream out(file, std::ios::app); + + DirectoryStats total; + for (const auto& s : stats) { + out << fmt::format("{},{},{}", s.mod, run, s.toCsv()) << "\n"; + total += s; + } + + out << fmt::format("total,{},{}", run, total.toCsv()) << "\n"; + + ++run; } -DirectoryRefresher::~DirectoryRefresher() + +DirectoryRefresher::DirectoryRefresher(std::size_t threadCount) + : m_threadCount(threadCount), m_lastFileCount(0) { - delete m_DirectoryStructure; } DirectoryEntry *DirectoryRefresher::stealDirectoryStructure() { QMutexLocker locker(&m_RefreshLock); - DirectoryEntry *result = m_DirectoryStructure; - m_DirectoryStructure = nullptr; - return result; + return m_Root.release(); } void DirectoryRefresher::setMods(const std::vector > &mods @@ -251,32 +370,6 @@ struct ModThread env::ThreadPool g_threads; -void dumpStats(std::vector& stats) -{ - static int run = 0; - static const std::string file("c:\\tmp\\data.csv"); - - if (run == 0) { - std::ofstream out(file, std::ios::out|std::ios::trunc); - out << fmt::format("what,run,{}", DirectoryStats::csvHeader()) << "\n"; - } - - std::sort(stats.begin(), stats.end(), [](auto&& a, auto&& b){ - return (naturalCompare(QString::fromStdString(a.mod), QString::fromStdString(b.mod)) < 0); - }); - - std::ofstream out(file, std::ios::app); - - DirectoryStats total; - for (const auto& s : stats) { - out << fmt::format("{},{},{}", s.mod, run, s.toCsv()) << "\n"; - total += s; - } - - out << fmt::format("total,{},{}", run, total.toCsv()) << "\n"; - - ++run; -} void DirectoryRefresher::addMultipleModsFilesToStructure( MOShared::DirectoryEntry *directoryStructure, @@ -332,10 +425,9 @@ void DirectoryRefresher::refresh() TimeThis tt("refresh"); QMutexLocker locker(&m_RefreshLock); - delete m_DirectoryStructure; - m_DirectoryStructure = new DirectoryEntry(L"data", nullptr, 0); - m_DirectoryStructure->getFileRegister()->reserve(m_lastFileCount); + m_Root.reset(new DirectoryEntry(L"data", nullptr, 0)); + m_Root->getFileRegister()->reserve(m_lastFileCount); IPluginGame *game = qApp->property("managed_game").value(); @@ -344,20 +436,20 @@ void DirectoryRefresher::refresh() { DirectoryStats dummy; - m_DirectoryStructure->addFromOrigin(L"data", dataDirectory, 0, dummy); + m_Root->addFromOrigin(L"data", dataDirectory, 0, dummy); } std::sort(m_Mods.begin(), m_Mods.end(), [](auto lhs, auto rhs) { return lhs.priority < rhs.priority; }); - addMultipleModsFilesToStructure(m_DirectoryStructure, m_Mods, true); + addMultipleModsFilesToStructure(m_Root.get(), m_Mods, true); - m_DirectoryStructure->getFileRegister()->sortOrigins(); + m_Root->getFileRegister()->sortOrigins(); - cleanStructure(m_DirectoryStructure); + cleanStructure(m_Root.get()); - m_lastFileCount = m_DirectoryStructure->getFileRegister()->highestCount(); + m_lastFileCount = m_Root->getFileRegister()->highestCount(); log::debug("refresher saw {} files", m_lastFileCount); emit progress(100); diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index 2102140c..e81b55dd 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -50,14 +50,8 @@ public: int priority; }; - /** - * @brief constructor - * - **/ DirectoryRefresher(std::size_t threadCount); - ~DirectoryRefresher(); - // noncopyable DirectoryRefresher(const DirectoryRefresher&) = delete; DirectoryRefresher& operator=(const DirectoryRefresher&) = delete; @@ -145,7 +139,7 @@ signals: private: std::vector m_Mods; std::set m_EnabledArchives; - MOShared::DirectoryEntry *m_DirectoryStructure; + std::unique_ptr m_Root; QMutex m_RefreshLock; std::size_t m_threadCount; std::size_t m_lastFileCount; diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 2c680847..80df044a 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -34,6 +34,7 @@ #include "env.h" #include "envmodule.h" #include "envfs.h" +#include "directoryrefresher.h" #include "shared/directoryentry.h" #include "shared/filesorigin.h" #include "shared/fileentry.h" @@ -96,7 +97,7 @@ OrganizerCore::OrganizerCore(Settings &settings) , m_Updater(NexusInterface::instance(m_PluginContainer)) , m_ModList(m_PluginContainer, this) , m_PluginList(this) - , m_DirectoryRefresher(settings.refreshThreadCount()) + , m_DirectoryRefresher(new DirectoryRefresher(settings.refreshThreadCount())) , m_DirectoryStructure(new DirectoryEntry(L"data", nullptr, 0)) , m_DownloadManager(NexusInterface::instance(m_PluginContainer), this) , m_DirectoryUpdate(false) @@ -114,7 +115,7 @@ OrganizerCore::OrganizerCore(Settings &settings) connect(&m_DownloadManager, SIGNAL(downloadSpeed(QString, int)), this, SLOT(downloadSpeed(QString, int))); - connect(&m_DirectoryRefresher, SIGNAL(refreshed()), this, + connect(m_DirectoryRefresher.get(), SIGNAL(refreshed()), this, SLOT(directory_refreshed())); connect(&m_ModList, SIGNAL(removeOrigin(QString)), this, @@ -139,7 +140,7 @@ OrganizerCore::OrganizerCore(Settings &settings) // make directory refresher run in a separate thread m_RefresherThread.start(); - m_DirectoryRefresher.moveToThread(&m_RefresherThread); + m_DirectoryRefresher->moveToThread(&m_RefresherThread); } OrganizerCore::~OrganizerCore() @@ -1255,7 +1256,7 @@ void OrganizerCore::updateModsInDirectoryStructure(QMapstealFiles(), {}, m_CurrentProfile->getModPriority(idx)}); } - m_DirectoryRefresher.addMultipleModsFilesToStructure( + m_DirectoryRefresher->addMultipleModsFilesToStructure( m_DirectoryStructure, entries); DirectoryRefresher::cleanStructure(m_DirectoryStructure); @@ -1274,13 +1275,13 @@ void OrganizerCore::updateModsInDirectoryStructure(QMap archives = enabledArchives(); - m_DirectoryRefresher.setMods( + m_DirectoryRefresher->setMods( m_CurrentProfile->getActiveMods(), std::set(archives.begin(), archives.end())); // finally also add files from bsas to the directory structure for (auto idx : modInfo.keys()) { - m_DirectoryRefresher.addModBSAToStructure( + m_DirectoryRefresher->addModBSAToStructure( m_DirectoryStructure, modInfo[idx]->name(), m_CurrentProfile->getModPriority(idx), modInfo[idx]->absolutePath(), modInfo[idx]->archives()); @@ -1398,18 +1399,18 @@ void OrganizerCore::refreshDirectoryStructure() const auto activeModList = m_CurrentProfile->getActiveMods(); const auto archives = enabledArchives(); - m_DirectoryRefresher.setMods( + m_DirectoryRefresher->setMods( activeModList, std::set(archives.begin(), archives.end())); // runs refresh() in a thread - QTimer::singleShot(0, &m_DirectoryRefresher, SLOT(refresh())); + QTimer::singleShot(0, m_DirectoryRefresher.get(), SLOT(refresh())); } void OrganizerCore::directory_refreshed() { log::debug("structure refreshed"); - DirectoryEntry *newStructure = m_DirectoryRefresher.stealDirectoryStructure(); + DirectoryEntry *newStructure = m_DirectoryRefresher->stealDirectoryStructure(); Q_ASSERT(newStructure != m_DirectoryStructure); if (newStructure == nullptr) { diff --git a/src/organizercore.h b/src/organizercore.h index 980156d3..a4d1a799 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -1,13 +1,11 @@ #ifndef ORGANIZERCORE_H #define ORGANIZERCORE_H - #include "selfupdater.h" #include "settings.h" #include "modlist.h" #include "modinfo.h" #include "pluginlist.h" -#include "directoryrefresher.h" #include "installationmanager.h" #include "downloadmanager.h" #include "executableslist.h" @@ -23,17 +21,6 @@ #include "executableinfo.h" #include -class ModListSortProxy; -class PluginListSortProxy; -class Profile; -class IUserInterface; - -namespace MOBase { - template class GuessedValue; - class IModInterface; -} -namespace MOShared { class DirectoryEntry; } - #include #include #include @@ -44,21 +31,25 @@ namespace MOShared { class DirectoryEntry; } #include #include -class QNetworkReply; -class QUrl; -class QWidget; - -#include //for HANDLE, LPDWORD - -#include -#include - +class ModListSortProxy; +class PluginListSortProxy; +class Profile; +class IUserInterface; class PluginContainer; +class DirectoryRefresher; -namespace MOBase { +namespace MOBase +{ + template class GuessedValue; + class IModInterface; class IPluginGame; } +namespace MOShared +{ + class DirectoryEntry; +} + class OrganizerCore : public QObject, public MOBase::IPluginDiagnose { @@ -113,7 +104,7 @@ public: SelfUpdater *updater() { return &m_Updater; } InstallationManager *installationManager(); MOShared::DirectoryEntry *directoryStructure() { return m_DirectoryStructure; } - DirectoryRefresher *directoryRefresher() { return &m_DirectoryRefresher; } + DirectoryRefresher *directoryRefresher() { return m_DirectoryRefresher.get(); } ExecutablesList *executablesList() { return &m_ExecutablesList; } void setExecutablesList(const ExecutablesList &executablesList) { m_ExecutablesList = executablesList; @@ -337,7 +328,7 @@ private: QStringList m_DefaultArchives; QStringList m_ActiveArchives; - DirectoryRefresher m_DirectoryRefresher; + std::unique_ptr m_DirectoryRefresher; MOShared::DirectoryEntry *m_DirectoryStructure; DownloadManager m_DownloadManager; diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 14c8323e..7f140750 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -73,105 +73,6 @@ static bool DirCompareByName(const DirectoryEntry *lhs, const DirectoryEntry *rh } -DirectoryStats::DirectoryStats() -{ - std::memset(this, 0, sizeof(DirectoryStats)); -} - -DirectoryStats& DirectoryStats::operator+=(const DirectoryStats& o) -{ - dirTimes += o.dirTimes; - fileTimes += o.fileTimes; - sortTimes += o.sortTimes; - - subdirLookupTimes += o.subdirLookupTimes; - addDirectoryTimes += o.addDirectoryTimes; - - filesLookupTimes += o.filesLookupTimes; - addFileTimes += o.addFileTimes; - addOriginToFileTimes += o.addOriginToFileTimes; - addFileToOriginTimes += o.addFileToOriginTimes; - addFileToRegisterTimes += o.addFileToRegisterTimes; - - originExists += o.originExists; - originCreate += o.originCreate; - originsNeededEnabled += o.originsNeededEnabled; - - subdirExists += o.subdirExists; - subdirCreate += o.subdirCreate; - - fileExists += o.fileExists; - fileCreate += o.fileCreate; - filesInsertedInRegister += o.filesInsertedInRegister; - filesAssignedInRegister += o.filesAssignedInRegister; - - return *this; -} - -std::string DirectoryStats::csvHeader() -{ - QStringList sl = { - "dirTimes", - "fileTimes", - "sortTimes", - "subdirLookupTimes", - "addDirectoryTimes", - "filesLookupTimes", - "addFileTimes", - "addOriginToFileTimes", - "addFileToOriginTimes", - "addFileToRegisterTimes", - "originExists", - "originCreate", - "originsNeededEnabled", - "subdirExists", - "subdirCreate", - "fileExists", - "fileCreate", - "filesInsertedInRegister", - "filesAssignedInRegister"}; - - return sl.join(",").toStdString(); -} - -std::string DirectoryStats::toCsv() const -{ - QStringList oss; - - auto s = [](auto ns) { - return ns.count() / 1000.0 / 1000.0 / 1000.0; - }; - - oss - << QString::number(s(dirTimes)) - << QString::number(s(fileTimes)) - << QString::number(s(sortTimes)) - - << QString::number(s(subdirLookupTimes)) - << QString::number(s(addDirectoryTimes)) - - << QString::number(s(filesLookupTimes)) - << QString::number(s(addFileTimes)) - << QString::number(s(addOriginToFileTimes)) - << QString::number(s(addFileToOriginTimes)) - << QString::number(s(addFileToRegisterTimes)) - - << QString::number(originExists) - << QString::number(originCreate) - << QString::number(originsNeededEnabled) - - << QString::number(subdirExists) - << QString::number(subdirCreate) - - << QString::number(fileExists) - << QString::number(fileCreate) - << QString::number(filesInsertedInRegister) - << QString::number(filesAssignedInRegister); - - return oss.join(",").toStdString(); -} - - DirectoryEntry::DirectoryEntry( std::wstring name, DirectoryEntry *parent, int originID) : m_OriginConnection(new OriginConnection), -- cgit v1.3.1 From d3fe9ff1faed52da8f647d9294e9ce371b0361e4 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sun, 16 Feb 2020 18:57:38 -0500 Subject: moved TimeThis to uibase fixed progress bar --- src/directoryrefresher.cpp | 34 +++++++++++++++----- src/directoryrefresher.h | 74 ++++++++++++++++++++++++++++++++++++++++---- src/mainwindow.cpp | 16 +++++++--- src/mainwindow.h | 4 ++- src/shared/fileregisterfwd.h | 2 ++ src/shared/util.cpp | 20 ------------ src/shared/util.h | 15 +-------- 7 files changed, 113 insertions(+), 52 deletions(-) (limited to 'src/directoryrefresher.h') diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index 5e1c05a8..599dd815 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -326,8 +326,10 @@ void DirectoryRefresher::addModToStructure(DirectoryEntry *directoryStructure } } + struct ModThread { + DirectoryRefreshProgress* progress = nullptr; DirectoryEntry* ds = nullptr; std::wstring modName; std::wstring path; @@ -375,6 +377,11 @@ struct ModThread modName, path, prio, archives, enabledArchives, lo, *stats); } + if (progress) { + progress->addDone(); + } + + SetThisThreadName(QString::fromStdWString(L"idle refresher")); ready = false; } }; @@ -382,13 +389,22 @@ struct ModThread env::ThreadPool g_threads; +void DirectoryRefresher::updateProgress(const DirectoryRefreshProgress* p) +{ + // careful: called from multiple threads + emit progress(p); +} void DirectoryRefresher::addMultipleModsFilesToStructure( MOShared::DirectoryEntry *directoryStructure, - const std::vector& entries, bool emitProgress) + const std::vector& entries, DirectoryRefreshProgress* progress) { std::vector stats(entries.size()); + if (progress) { + progress->start(entries.size()); + } + log::debug("refresher: using {} threads", m_threadCount); g_threads.setMax(m_threadCount); @@ -405,9 +421,14 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( if (e.stealFiles.length() > 0) { stealModFilesIntoStructure( directoryStructure, e.modName, prio, e.absolutePath, e.stealFiles); + + if (progress) { + progress->addDone(); + } } else { auto& mt = g_threads.request(); + mt.progress = progress; mt.ds = directoryStructure; mt.modName = e.modName.toStdWString(); mt.path = QDir::toNativeSeparators(e.absolutePath).toStdWString(); @@ -430,10 +451,6 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( } catch (const std::exception& ex) { emit error(tr("failed to read mod (%1): %2").arg(e.modName, ex.what())); } - - if (emitProgress) { - emit progress((static_cast(i) * 100) / static_cast(entries.size()) + 1); - } } g_threads.waitForAll(); @@ -447,6 +464,7 @@ void DirectoryRefresher::refresh() { SetThisThreadName("DirectoryRefresher"); TimeThis tt("refresh"); + auto* p = new DirectoryRefreshProgress(this); { QMutexLocker locker(&m_RefreshLock); @@ -468,7 +486,7 @@ void DirectoryRefresher::refresh() return lhs.priority < rhs.priority; }); - addMultipleModsFilesToStructure(m_Root.get(), m_Mods, true); + addMultipleModsFilesToStructure(m_Root.get(), m_Mods, p); m_Root->getFileRegister()->sortOrigins(); @@ -478,6 +496,8 @@ void DirectoryRefresher::refresh() log::debug("refresher saw {} files", m_lastFileCount); } - emit progress(100); + p->finish(); + + emit progress(p); emit refreshed(); } diff --git a/src/directoryrefresher.h b/src/directoryrefresher.h index e81b55dd..bd08dad6 100644 --- a/src/directoryrefresher.h +++ b/src/directoryrefresher.h @@ -20,14 +20,14 @@ along with Mod Organizer. If not, see . #ifndef DIRECTORYREFRESHER_H #define DIRECTORYREFRESHER_H +#include "fileregisterfwd.h" +#include "profile.h" #include #include #include #include #include #include -#include "profile.h" - /** * @brief used to asynchronously generate the virtual view of the combined data directory @@ -38,11 +38,15 @@ class DirectoryRefresher : public QObject Q_OBJECT public: - struct EntryInfo { + struct EntryInfo + { EntryInfo(const QString &modName, const QString &absolutePath, const QStringList &stealFiles, const QStringList &archives, int priority) : modName(modName), absolutePath(absolutePath), stealFiles(stealFiles) - , archives(archives), priority(priority) {} + , archives(archives), priority(priority) + { + } + QString modName; QString absolutePath; QStringList stealFiles; @@ -121,7 +125,10 @@ public: void addMultipleModsFilesToStructure( MOShared::DirectoryEntry *directoryStructure, - const std::vector& entries, bool emitProgress=false); + const std::vector& entries, + DirectoryRefreshProgress* progress=nullptr); + + void updateProgress(const DirectoryRefreshProgress* p); public slots: @@ -132,7 +139,7 @@ public slots: signals: - void progress(int progress); + void progress(const DirectoryRefreshProgress* p); void error(const QString &error); void refreshed(); @@ -149,4 +156,59 @@ private: int priority, const QString &directory, const QStringList &stealFiles); }; + +class DirectoryRefreshProgress : QObject +{ + Q_OBJECT; + +public: + DirectoryRefreshProgress(DirectoryRefresher* r) : + QObject(r), m_refresher(r), m_modCount(0), m_modDone(0), m_finished(false) + { + } + + void start(std::size_t modCount) + { + m_modCount = modCount; + m_modDone = 0; + m_finished = false; + } + + + bool finished() const + { + return m_finished; + } + + int percentDone() const + { + int percent = 100; + + if (m_modCount > 0) { + const double d = static_cast(m_modDone) / m_modCount; + percent = static_cast(d * 100); + } + + return percent; + } + + + void finish() + { + m_finished = true; + } + + void addDone() + { + ++m_modDone; + m_refresher->updateProgress(this); + } + +private: + DirectoryRefresher* m_refresher; + std::size_t m_modCount; + std::atomic m_modDone; + bool m_finished; +}; + #endif // DIRECTORYREFRESHER_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f5749392..21a6fa41 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -367,7 +367,10 @@ MainWindow::MainWindow(Settings &settings connect(ui->espFilterEdit, SIGNAL(textChanged(QString)), this, SLOT(espFilterChanged(QString))); connect(m_OrganizerCore.directoryRefresher(), SIGNAL(refreshed()), this, SLOT(directory_refreshed())); - connect(m_OrganizerCore.directoryRefresher(), SIGNAL(progress(int)), this, SLOT(refresher_progress(int))); + connect( + m_OrganizerCore.directoryRefresher(), + &DirectoryRefresher::progress, + this, &MainWindow::refresherProgress); connect(m_OrganizerCore.directoryRefresher(), SIGNAL(error(QString)), this, SLOT(showError(QString))); connect(&m_SavesWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(refreshSavesIfOpen())); @@ -2382,10 +2385,15 @@ void MainWindow::setESPListSorting(int index) } } -void MainWindow::refresher_progress(int percent) +void MainWindow::refresherProgress(const DirectoryRefreshProgress* p) { - setEnabled(percent == 100); - ui->statusBar->setProgress(percent); + if (p->finished()) { + setEnabled(true); + ui->statusBar->setProgress(100); + } else { + setEnabled(false); + ui->statusBar->setProgress(p->percentDone()); + } } void MainWindow::directory_refreshed() diff --git a/src/mainwindow.h b/src/mainwindow.h index 6530d8ad..b839e85e 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -31,6 +31,7 @@ along with Mod Organizer. If not, see . #include "tutorialcontrol.h" #include "plugincontainer.h" //class PluginContainer; #include "iplugingame.h" //namespace MOBase { class IPluginGame; } +#include "shared/fileregisterfwd.h" #include class Executable; @@ -155,7 +156,8 @@ public: public slots: void modorder_changed(); void esplist_changed(); - void refresher_progress(int percent); + void refresherProgress(const DirectoryRefreshProgress* p); + void directory_refreshed(); void toolPluginInvoke(); diff --git a/src/shared/fileregisterfwd.h b/src/shared/fileregisterfwd.h index 6348fee1..720e6e30 100644 --- a/src/shared/fileregisterfwd.h +++ b/src/shared/fileregisterfwd.h @@ -1,6 +1,8 @@ #ifndef MO_REGISTER_FILEREGISTERFWD_INCLUDED #define MO_REGISTER_FILEREGISTERFWD_INCLUDED +class DirectoryRefreshProgress; + namespace MOShared { diff --git a/src/shared/util.cpp b/src/shared/util.cpp index 74e386a3..483b36a9 100644 --- a/src/shared/util.cpp +++ b/src/shared/util.cpp @@ -382,26 +382,6 @@ void checkDuplicateShortcuts(const QMenu& m) } // namespace MOShared -TimeThis::TimeThis(QString what) - : m_what(std::move(what)), m_start(Clock::now()) -{ -} - -TimeThis::~TimeThis() -{ - using namespace std::chrono; - - const auto end = Clock::now(); - const auto d = duration_cast(end - m_start).count(); - - if (m_what.isEmpty()) { - log::debug("{} ms", d); - } else { - log::debug("{} {} ms", m_what, d); - } -} - - static bool g_exiting = false; static bool g_canClose = false; diff --git a/src/shared/util.h b/src/shared/util.h index 1d3cce82..2761b64f 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -20,6 +20,7 @@ along with Mod Organizer. If not, see . #ifndef UTIL_H #define UTIL_H +#include #include #include #include @@ -64,20 +65,6 @@ inline FILETIME ToFILETIME(std::filesystem::file_time_type t) } // namespace MOShared -class TimeThis -{ -public: - TimeThis(QString what={}); - ~TimeThis(); - -private: - using Clock = std::chrono::high_resolution_clock; - - QString m_what; - Clock::time_point m_start; -}; - - enum class Exit { None = 0x00, -- cgit v1.3.1