From d147d1d250d7ec19cc59a4c25d1d946eab9e6bc9 Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 30 Jan 2024 01:19:42 +0100 Subject: [PATCH] Initialize FileSystemArchive index in constructor It should be initialize for each created archive anyway. There is no good reason to have additional complexity for lazy initialization. And it helps to catch problems with specific directory when it's added to the VFS not when all are added and index is built. --- components/vfs/filesystemarchive.cpp | 62 +++++++++++----------------- components/vfs/filesystemarchive.hpp | 1 - 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/components/vfs/filesystemarchive.cpp b/components/vfs/filesystemarchive.cpp index c72798e7ea..b8374eaba9 100644 --- a/components/vfs/filesystemarchive.cpp +++ b/components/vfs/filesystemarchive.cpp @@ -12,50 +12,38 @@ namespace VFS { FileSystemArchive::FileSystemArchive(const std::filesystem::path& path) - : mBuiltIndex(false) - , mPath(path) - { - } - - void FileSystemArchive::listResources(FileMap& out) + : mPath(path) { - if (!mBuiltIndex) - { - const auto str = mPath.u8string(); - size_t prefix = str.size(); - - if (!mPath.empty() && str[prefix - 1] != '\\' && str[prefix - 1] != '/') - ++prefix; - - for (const auto& i : std::filesystem::recursive_directory_iterator(mPath)) - { - if (std::filesystem::is_directory(i)) - continue; + const auto str = mPath.u8string(); + std::size_t prefix = str.size(); - const auto& path = i.path(); - const std::string proper = Files::pathToUnicodeString(path); + if (prefix > 0 && str[prefix - 1] != '\\' && str[prefix - 1] != '/') + ++prefix; - FileSystemArchiveFile file(path); - - VFS::Path::Normalized searchable(std::string_view{ proper }.substr(prefix)); - - const auto inserted = mIndex.emplace(std::move(searchable), std::move(file)); - if (!inserted.second) - Log(Debug::Warning) - << "Warning: found duplicate file for '" << proper - << "', please check your file system for two files with the same name in different cases."; - else - out[inserted.first->first] = &inserted.first->second; - } - mBuiltIndex = true; - } - else + for (const auto& i : std::filesystem::recursive_directory_iterator(mPath)) { - for (auto& [k, v] : mIndex) - out[k] = &v; + if (std::filesystem::is_directory(i)) + continue; + + const std::filesystem::path& filePath = i.path(); + const std::string proper = Files::pathToUnicodeString(filePath); + VFS::Path::Normalized searchable(std::string_view{ proper }.substr(prefix)); + FileSystemArchiveFile file(filePath); + + const auto inserted = mIndex.emplace(std::move(searchable), std::move(file)); + if (!inserted.second) + Log(Debug::Warning) + << "Found duplicate file for '" << proper + << "', please check your file system for two files with the same name in different cases."; } } + void FileSystemArchive::listResources(FileMap& out) + { + for (auto& [k, v] : mIndex) + out[k] = &v; + } + bool FileSystemArchive::contains(Path::NormalizedView file) const { return mIndex.find(file) != mIndex.end(); diff --git a/components/vfs/filesystemarchive.hpp b/components/vfs/filesystemarchive.hpp index b158ef3472..215c443b58 100644 --- a/components/vfs/filesystemarchive.hpp +++ b/components/vfs/filesystemarchive.hpp @@ -36,7 +36,6 @@ namespace VFS private: std::map> mIndex; - bool mBuiltIndex; std::filesystem::path mPath; };