diff --git a/apps/openmw/mwlua/vfsbindings.cpp b/apps/openmw/mwlua/vfsbindings.cpp index d91f3f669c..ad32520649 100644 --- a/apps/openmw/mwlua/vfsbindings.cpp +++ b/apps/openmw/mwlua/vfsbindings.cpp @@ -160,25 +160,6 @@ namespace MWLua auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); - sol::usertype vfsIterator - = context.mLua->sol().new_usertype("VFSIterator"); - vfsIterator[sol::meta_function::to_string] = [](const VFS::Manager::StatefulIterator& vfsIterator) { - return "VFSIterator{'" + vfsIterator.getPath() + "'}"; - }; - vfsIterator["path"] = sol::readonly_property( - [](const VFS::Manager::StatefulIterator& vfsIterator) { return vfsIterator.getPath(); }); - - auto createIter = [](VFS::Manager::StatefulIterator& vfsIterator) { - return sol::as_function([vfsIterator, i = 1]() mutable { - if (auto v = vfsIterator.next()) - return std::tuple, sol::optional>(i++, *v); - else - return std::tuple, sol::optional>(sol::nullopt, sol::nullopt); - }); - }; - vfsIterator["__pairs"] = createIter; - vfsIterator["__ipairs"] = createIter; - sol::usertype handle = context.mLua->sol().new_usertype("FileHandle"); handle["fileName"] = sol::readonly_property([](const FileHandle& self) { return self.mFileName; }); handle[sol::meta_function::to_string] = [](const FileHandle& self) { @@ -346,8 +327,19 @@ namespace MWLua [](const sol::object&) -> sol::object { return sol::nil; }); api["fileExists"] = [vfs](std::string_view fileName) -> bool { return vfs->exists(fileName); }; - api["getIterator"] - = [vfs](std::string_view path) -> VFS::Manager::StatefulIterator { return vfs->getStatefulIterator(path); }; + api["pathsWithPrefix"] = [vfs](std::string_view prefix) { + auto iterator = vfs->getRecursiveDirectoryIterator(prefix); + return sol::as_function([iterator, current = iterator.begin()]() mutable -> sol::optional { + if (current != iterator.end()) + { + const std::string& result = *current; + ++current; + return result; + } + + return sol::nullopt; + }); + }; return LuaUtil::makeReadOnly(api); } diff --git a/components/vfs/manager.cpp b/components/vfs/manager.cpp index 0484e06c54..bfc001e4f2 100644 --- a/components/vfs/manager.cpp +++ b/components/vfs/manager.cpp @@ -89,17 +89,4 @@ namespace VFS ++normalized.back(); return { it, mIndex.lower_bound(normalized) }; } - - Manager::StatefulIterator Manager::getStatefulIterator(std::string_view path) const - { - if (path.empty()) - return { mIndex.begin(), mIndex.end(), std::string() }; - std::string normalized = Path::normalizeFilename(path); - const auto it = mIndex.lower_bound(normalized); - if (it == mIndex.end() || !startsWith(it->first, normalized)) - return { it, it, normalized }; - std::string upperBound = normalized; - ++upperBound.back(); - return { it, mIndex.lower_bound(upperBound), normalized }; - } } diff --git a/components/vfs/manager.hpp b/components/vfs/manager.hpp index bfb44c3fc2..db38e4b240 100644 --- a/components/vfs/manager.hpp +++ b/components/vfs/manager.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -49,18 +48,11 @@ namespace VFS const std::string& operator*() const { return mIt->first; } const std::string* operator->() const { return &mIt->first; } bool operator!=(const RecursiveDirectoryIterator& other) { return mIt != other.mIt; } - bool operator==(const RecursiveDirectoryIterator& other) const { return mIt == other.mIt; } RecursiveDirectoryIterator& operator++() { ++mIt; return *this; } - RecursiveDirectoryIterator operator++(int) - { - RecursiveDirectoryIterator old = *this; - mIt++; - return old; - } private: std::map::const_iterator mIt; @@ -69,31 +61,6 @@ namespace VFS using RecursiveDirectoryRange = IteratorPair; public: - class StatefulIterator : RecursiveDirectoryRange - { - public: - StatefulIterator(RecursiveDirectoryIterator first, RecursiveDirectoryIterator last, const std::string& path) - : RecursiveDirectoryRange(first, last) - , mCurrent(first) - , mPath(path) - { - } - - const std::string& getPath() const { return mPath; } - - std::optional next() - { - if (mCurrent == end()) - return std::nullopt; - - return *mCurrent++; - } - - private: - RecursiveDirectoryIterator mCurrent; - std::string mPath; - }; - // Empty the file index and unregister archives. void reset(); @@ -126,13 +93,6 @@ namespace VFS /// @note May be called from any thread once the index has been built. RecursiveDirectoryRange getRecursiveDirectoryIterator(std::string_view path) const; - /// Recursively iterate over the elements of the given path - /// In practice it return all files of the VFS starting with the given path - /// Stores iterator to current element. - /// @note the path is normalized - /// @note May be called from any thread once the index has been built. - StatefulIterator getStatefulIterator(std::string_view path) const; - /// Retrieve the absolute path to the file /// @note Throws an exception if the file can not be found. /// @note May be called from any thread once the index has been built. diff --git a/files/lua_api/openmw/vfs.lua b/files/lua_api/openmw/vfs.lua index f432273810..ba381a1249 100644 --- a/files/lua_api/openmw/vfs.lua +++ b/files/lua_api/openmw/vfs.lua @@ -6,10 +6,6 @@ ---- --- @type VFSIterator --- @field #string path VFS prefix path - --- -- @type FileHandle -- @field #string fileName VFS path to related file @@ -135,14 +131,18 @@ -- end --- --- Get iterator to fetch file names with given path prefix from VFS --- @function [parent=#vfs] getIterator +-- Get iterator function to fetch file names with given path prefix from VFS +-- @function [parent=#vfs] pathsWithPrefix -- @param #string path Path prefix --- @return #VFSIterator Opened iterator --- @usage local dir = vfs.getIterator("Music\\Explore"); --- for _, fileName in pairs(dir) do +-- @return #function Function to get next file name +-- @usage -- get all files with given prefix from VFS index +-- for fileName in vfs.pathsWithPrefix("Music\\Explore") do -- print(fileName); -- end +-- @usage -- get some first files +-- local getNextFile = vfs.pathsWithPrefix("Music\\Explore"); +-- local firstFile = getNextFile(); +-- local secondFile = getNextFile(); --- -- Detect a file handle type