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.
This commit is contained in:
Ahmed Samy 2013-12-25 21:29:38 +02:00
parent 27db78567b
commit 6745bff132
4 changed files with 41 additions and 1 deletions

View File

@ -197,7 +197,7 @@ message(STATUS "Build revision: ${BUILD_REVISION}")
add_definitions(-D"BUILD_REVISION=\\\"${BUILD_REVISION}\\\"") add_definitions(-D"BUILD_REVISION=\\\"${BUILD_REVISION}\\\"")
# find boost # find boost
set(REQUIRED_BOOST_COMPONENTS system thread chrono) set(REQUIRED_BOOST_COMPONENTS system thread filesystem chrono)
if(WIN32) if(WIN32)
set(Boost_THREADAPI win32) set(Boost_THREADAPI win32)
set(framework_DEFINITIONS ${framework_DEFINITIONS} -DBOOST_THREAD_USE_LIB) # fix boost thread linkage set(framework_DEFINITIONS ${framework_DEFINITIONS} -DBOOST_THREAD_USE_LIB) # fix boost thread linkage

View File

@ -271,6 +271,37 @@ std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& di
return files; return files;
} }
std::vector<std::string> ResourceManager::getDirectoryFiles(const std::string& path, bool filenameOnly, bool recursive)
{
if(!fs::exists(path))
return std::vector<std::string>();
fs::path p(path);
return discoverPath(p, filenameOnly, recursive);
}
std::vector<std::string> ResourceManager::discoverPath(const fs::path& path, bool filenameOnly, bool recursive)
{
std::vector<std::string> 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<std::string> 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 ResourceManager::resolvePath(const std::string& path)
{ {
std::string fullPath; std::string fullPath;

View File

@ -25,6 +25,10 @@
#include "declarations.h" #include "declarations.h"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
// @bindsingleton g_resources // @bindsingleton g_resources
class ResourceManager class ResourceManager
{ {
@ -61,6 +65,7 @@ public:
bool makeDir(const std::string directory); bool makeDir(const std::string directory);
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = ""); std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
std::vector<std::string> getDirectoryFiles(const std::string& path, bool filenameOnly, bool recursive);
std::string resolvePath(const std::string& path); std::string resolvePath(const std::string& path);
std::string getRealDir(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); bool isFileType(const std::string& filename, const std::string& type);
ticks_t getFileTime(const std::string& filename); ticks_t getFileTime(const std::string& filename);
protected:
std::vector<std::string> discoverPath(const fs::path& path, bool filenameOnly, bool recursive);
private: private:
std::string m_workDir; std::string m_workDir;
std::string m_writeDir; std::string m_writeDir;

View File

@ -189,6 +189,7 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_resources", "getWorkDir", &ResourceManager::getWorkDir, &g_resources); 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", "getSearchPaths", &ResourceManager::getSearchPaths, &g_resources);
g_lua.bindSingletonFunction("g_resources", "listDirectoryFiles", &ResourceManager::listDirectoryFiles, &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", "readFileContents", &ResourceManager::readFileContents, &g_resources);
g_lua.bindSingletonFunction("g_resources", "guessFilePath", &ResourceManager::guessFilePath, &g_resources); g_lua.bindSingletonFunction("g_resources", "guessFilePath", &ResourceManager::guessFilePath, &g_resources);
g_lua.bindSingletonFunction("g_resources", "isFileType", &ResourceManager::isFileType, &g_resources); g_lua.bindSingletonFunction("g_resources", "isFileType", &ResourceManager::isFileType, &g_resources);