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);