From 18d23653c4d29b9c8e8bf0b13dbceaff218827d0 Mon Sep 17 00:00:00 2001 From: BeniS Date: Sun, 12 May 2013 17:00:52 +1200 Subject: [PATCH] Added some control params to dofiles lua method. * File name contains string. * Recursive file checking for deep searches. --- src/framework/luaengine/luainterface.cpp | 49 ++++++++++++++++++------ src/framework/luaengine/luainterface.h | 2 + 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/framework/luaengine/luainterface.cpp b/src/framework/luaengine/luainterface.cpp index c2f4f253..bb214e7f 100644 --- a/src/framework/luaengine/luainterface.cpp +++ b/src/framework/luaengine/luainterface.cpp @@ -577,20 +577,19 @@ int LuaInterface::lua_dofile(lua_State* L) int LuaInterface::lua_dofiles(lua_State* L) { - std::string directory = g_lua.popString(); - - for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) { - if(!g_resources.isFileType(fileName, "lua")) - continue; + bool recursive = false; + if(g_lua.getTop() > 2) { + recursive = g_lua.popBoolean(); + } - try { - g_lua.loadScript(directory + "/" + fileName); - g_lua.call(0, 0); - } catch(stdext::exception& e) { - g_lua.pushString(e.what()); - g_lua.error(); - } + std::string contains = ""; + if(g_lua.getTop() > 1) { + contains = g_lua.popString(); } + + std::string directory = g_lua.popString(); + g_lua.loadFiles(directory, contains, recursive); + return 0; } @@ -1247,3 +1246,29 @@ int LuaInterface::getTop() { 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(); + } + } +} diff --git a/src/framework/luaengine/luainterface.h b/src/framework/luaengine/luainterface.h index 25a0cf0f..165cd787 100644 --- a/src/framework/luaengine/luainterface.h +++ b/src/framework/luaengine/luainterface.h @@ -321,6 +321,8 @@ public: void clearStack() { pop(stackSize()); } 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 template int polymorphicPush(const T& v, const Args&... args);