From 6745bff132544e4b66cf39da9ae1e8a3666d86bf Mon Sep 17 00:00:00 2001 From: Ahmed Samy Date: Wed, 25 Dec 2013 21:29:38 +0200 Subject: [PATCH] Introduce g_resources.getDirectoryFiles This can recursively find files in a directory that's not in physfs search path, this is needed for mapeditor needs, esp the file browser. We do not want to limit users to the application run directory so we have to use this. This function uses boost filesystem. Prototype: g_resources.getDirectoryFiles(directory STRING, filenameOnly BOOL, recursive BOOL) filenameOnly is there if we want to just get the filenames in the directory. recursive if we want to loop directories in the directory. --- src/framework/CMakeLists.txt | 2 +- src/framework/core/resourcemanager.cpp | 31 ++++++++++++++++++++++++++ src/framework/core/resourcemanager.h | 8 +++++++ src/framework/luafunctions.cpp | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 5bbfe635..55a91ec9 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -197,7 +197,7 @@ message(STATUS "Build revision: ${BUILD_REVISION}") add_definitions(-D"BUILD_REVISION=\\\"${BUILD_REVISION}\\\"") # find boost -set(REQUIRED_BOOST_COMPONENTS system thread chrono) +set(REQUIRED_BOOST_COMPONENTS system thread filesystem chrono) if(WIN32) set(Boost_THREADAPI win32) set(framework_DEFINITIONS ${framework_DEFINITIONS} -DBOOST_THREAD_USE_LIB) # fix boost thread linkage diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 60a2d300..35d3160b 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -271,6 +271,37 @@ std::list ResourceManager::listDirectoryFiles(const std::string& di return files; } +std::vector ResourceManager::getDirectoryFiles(const std::string& path, bool filenameOnly, bool recursive) +{ + if(!fs::exists(path)) + return std::vector(); + + fs::path p(path); + return discoverPath(p, filenameOnly, recursive); +} + +std::vector ResourceManager::discoverPath(const fs::path& path, bool filenameOnly, bool recursive) +{ + std::vector files; + + /* Before doing anything, we have to add this directory to search path, + * this is needed so it works correctly when one wants to open a file. */ + addSearchPath(path.generic_string(), true); + for (fs::directory_iterator it(path), end; it != end; ++it) { + if(fs::is_directory(it->path().generic_string()) && recursive) { + std::vector subfiles = discoverPath(it->path(), filenameOnly, recursive); + files.insert(files.end(), subfiles.begin(), subfiles.end()); + } else { + if(filenameOnly) + files.push_back(it->path().filename().string()); + else + files.push_back(it->path().generic_string() + "/" + it->path().filename().string()); + } + } + + return files; +} + std::string ResourceManager::resolvePath(const std::string& path) { std::string fullPath; diff --git a/src/framework/core/resourcemanager.h b/src/framework/core/resourcemanager.h index c280c3c5..129ee42c 100644 --- a/src/framework/core/resourcemanager.h +++ b/src/framework/core/resourcemanager.h @@ -25,6 +25,10 @@ #include "declarations.h" +#include + +namespace fs = boost::filesystem; + // @bindsingleton g_resources class ResourceManager { @@ -61,6 +65,7 @@ public: bool makeDir(const std::string directory); std::list listDirectoryFiles(const std::string& directoryPath = ""); + std::vector getDirectoryFiles(const std::string& path, bool filenameOnly, bool recursive); std::string resolvePath(const std::string& path); std::string getRealDir(const std::string& path); @@ -75,6 +80,9 @@ public: bool isFileType(const std::string& filename, const std::string& type); ticks_t getFileTime(const std::string& filename); +protected: + std::vector discoverPath(const fs::path& path, bool filenameOnly, bool recursive); + private: std::string m_workDir; std::string m_writeDir; diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 71561ccf..5d7264b2 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -189,6 +189,7 @@ void Application::registerLuaFunctions() g_lua.bindSingletonFunction("g_resources", "getWorkDir", &ResourceManager::getWorkDir, &g_resources); g_lua.bindSingletonFunction("g_resources", "getSearchPaths", &ResourceManager::getSearchPaths, &g_resources); g_lua.bindSingletonFunction("g_resources", "listDirectoryFiles", &ResourceManager::listDirectoryFiles, &g_resources); + g_lua.bindSingletonFunction("g_resources", "getDirectoryFiles", &ResourceManager::getDirectoryFiles, &g_resources); g_lua.bindSingletonFunction("g_resources", "readFileContents", &ResourceManager::readFileContents, &g_resources); g_lua.bindSingletonFunction("g_resources", "guessFilePath", &ResourceManager::guessFilePath, &g_resources); g_lua.bindSingletonFunction("g_resources", "isFileType", &ResourceManager::isFileType, &g_resources);