Added some control params to dofiles lua method.

* File name contains string.
* Recursive file checking for deep searches.
This commit is contained in:
BeniS 2013-05-12 17:00:52 +12:00
parent 6c119627bb
commit 18d23653c4
2 changed files with 40 additions and 13 deletions

View File

@ -577,20 +577,19 @@ int LuaInterface::lua_dofile(lua_State* L)
int LuaInterface::lua_dofiles(lua_State* L) int LuaInterface::lua_dofiles(lua_State* L)
{ {
bool recursive = false;
if(g_lua.getTop() > 2) {
recursive = g_lua.popBoolean();
}
std::string contains = "";
if(g_lua.getTop() > 1) {
contains = g_lua.popString();
}
std::string directory = g_lua.popString(); std::string directory = g_lua.popString();
g_lua.loadFiles(directory, contains, recursive);
for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) {
if(!g_resources.isFileType(fileName, "lua"))
continue;
try {
g_lua.loadScript(directory + "/" + fileName);
g_lua.call(0, 0);
} catch(stdext::exception& e) {
g_lua.pushString(e.what());
g_lua.error();
}
}
return 0; return 0;
} }
@ -1247,3 +1246,29 @@ int LuaInterface::getTop()
{ {
return lua_gettop(L); return lua_gettop(L);
} }
void LuaInterface::loadFiles(std::string directory, std::string contains, bool recursive)
{
for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) {
std::string fullPath = directory + "/" + fileName;
if(recursive && g_resources.directoryExists(fullPath)) {
loadFiles(fullPath, contains, true);
continue;
}
if(!g_resources.isFileType(fileName, "lua"))
continue;
if(!contains.empty() && fileName.find(contains) == std::string::npos)
continue;
try {
g_lua.loadScript(fullPath);
g_lua.call(0, 0);
} catch(stdext::exception& e) {
g_lua.pushString(e.what());
g_lua.error();
}
}
}

View File

@ -321,6 +321,8 @@ public:
void clearStack() { pop(stackSize()); } void clearStack() { pop(stackSize()); }
bool hasIndex(int index) { return (stackSize() >= (index < 0 ? -index : index) && index != 0); } bool hasIndex(int index) { return (stackSize() >= (index < 0 ? -index : index) && index != 0); }
void loadFiles(std::string directory, std::string contains, bool recursive = false);
/// Pushes any type onto the stack /// Pushes any type onto the stack
template<typename T, typename... Args> template<typename T, typename... Args>
int polymorphicPush(const T& v, const Args&... args); int polymorphicPush(const T& v, const Args&... args);