Browse Source

Added some control params to dofiles lua method.

* File name contains string.
* Recursive file checking for deep searches.
BeniS 7 years ago
parent
commit
18d23653c4
2 changed files with 39 additions and 12 deletions
  1. 37
    12
      src/framework/luaengine/luainterface.cpp
  2. 2
    0
      src/framework/luaengine/luainterface.h

+ 37
- 12
src/framework/luaengine/luainterface.cpp View File

@@ -577,20 +577,19 @@ int LuaInterface::lua_dofile(lua_State* L)
577 577
 
578 578
 int LuaInterface::lua_dofiles(lua_State* L)
579 579
 {
580
-    std::string directory = g_lua.popString();
581
-
582
-    for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) {
583
-        if(!g_resources.isFileType(fileName, "lua"))
584
-            continue;
580
+    bool recursive = false;
581
+    if(g_lua.getTop() > 2) {
582
+        recursive = g_lua.popBoolean();
583
+    }
585 584
 
586
-        try {
587
-            g_lua.loadScript(directory + "/" + fileName);
588
-            g_lua.call(0, 0);
589
-        } catch(stdext::exception& e) {
590
-            g_lua.pushString(e.what());
591
-            g_lua.error();
592
-        }
585
+    std::string contains = "";
586
+    if(g_lua.getTop() > 1) {
587
+        contains = g_lua.popString();
593 588
     }
589
+
590
+    std::string directory = g_lua.popString();
591
+    g_lua.loadFiles(directory, contains, recursive);
592
+
594 593
     return 0;
595 594
 }
596 595
 
@@ -1247,3 +1246,29 @@ int LuaInterface::getTop()
1247 1246
 {
1248 1247
     return lua_gettop(L);
1249 1248
 }
1249
+
1250
+void LuaInterface::loadFiles(std::string directory, std::string contains, bool recursive)
1251
+{
1252
+    for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) {
1253
+        std::string fullPath = directory + "/" + fileName;
1254
+
1255
+        if(recursive && g_resources.directoryExists(fullPath)) {
1256
+            loadFiles(fullPath, contains, true);
1257
+            continue;
1258
+        }
1259
+
1260
+        if(!g_resources.isFileType(fileName, "lua"))
1261
+            continue;
1262
+
1263
+        if(!contains.empty() && fileName.find(contains) == std::string::npos)
1264
+            continue;
1265
+
1266
+        try {
1267
+            g_lua.loadScript(fullPath);
1268
+            g_lua.call(0, 0);
1269
+        } catch(stdext::exception& e) {
1270
+            g_lua.pushString(e.what());
1271
+            g_lua.error();
1272
+        }
1273
+    }
1274
+}

+ 2
- 0
src/framework/luaengine/luainterface.h View File

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

Loading…
Cancel
Save