浏览代码

Add "const Vector<String>" support in Lua, now in Lua "const Vector<String>" is a Lua table with string elements.

aster2013 12 年之前
父节点
当前提交
4e0df133c0

+ 70 - 5
Source/Extras/LuaScript/ToluaUrho3DEx.cpp

@@ -21,7 +21,6 @@
 //
 
 #include "Precompiled.h"
-#include "Log.h"
 #include "ToluaUrho3DEx.h"
 
 const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def)
@@ -30,17 +29,83 @@ const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def)
     return s?s:"";
 }
 
-const char* tolua_tourho3dstring(lua_State* L, int narg, const Urho3D::String& def)
+const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def)
 {
     return tolua_tourho3dstring(L, narg, def.CString());
 }
 
-int tolua_pushurho3dstringvector(lua_State* L, const Urho3D::Vector<Urho3D::String>& strings)
+int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
 {
+    if (lua_istable(L, lo))
+    {
+        int length = lua_objlen(L, lo);
+        for (int i = 1; i <= length; ++i)
+        {
+            lua_pushinteger(L, i);
+            lua_gettable(L, lo);
+            if (!lua_isstring(L, -1))
+            {
+                lua_pop(L, 1);
+
+                err->index = lo;
+                err->array = 0;
+                err->type = type;
+
+                return 0;
+            }
+
+            lua_pop(L, 1);
+        }
+
+        return 1;
+    }
+
+    err->index = lo;
+    err->array = 0;
+    err->type = type;
+    return 0;
+}
+
+void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def)
+{
+    if (!lua_istable(L, narg))
+        return 0;
+
+    static Vector<String> result;
+    result.Clear();
+
+    int length = lua_objlen(L, narg);
+    for (int i = 1; i <= length; ++i)
+    {
+        lua_pushinteger(L, i);
+        lua_gettable(L, narg);
+
+        if (!lua_isstring(L, -1))
+        {
+            lua_pop(L, 1);
+            return 0;
+        }
+
+        String string = tolua_tourho3dstring(L, -1, "");
+        result.Push(string);
+
+        lua_pop(L, 1);
+    }
+
+    return &result;
+}
+
+int tolua_pushurho3dconstvectorstring(lua_State*L, void* data, const char* type)
+{
+#ifndef TOLUA_RELEASE
+    assert(strcmp(type, "const Vector<String>") == 0);
+#endif
+
+    const Vector<String>& vectorstring = *((const Vector<String>*)data);
     lua_newtable(L);
-    for (unsigned i = 0; i < strings.Size(); ++i)
+    for (unsigned i = 0; i < vectorstring.Size(); ++i)
     {
-        tolua_pushurho3dstring(L, strings[i]);
+        tolua_pushurho3dstring(L, vectorstring[i]);
         lua_rawseti(L, -2, i + 1);
     }
     return 1;

+ 10 - 10
Source/Extras/LuaScript/ToluaUrho3DEx.h

@@ -22,20 +22,20 @@
 
 #pragma once
 
-#include "Variant.h"
-
 struct lua_State;
 
+using namespace Urho3D;
+
 #define tolua_isurho3dstring tolua_isstring
 #define tolua_pushurho3dstring(x,y)	tolua_pushstring(x,y.CString())
 
 /// Convert Lua string to Urho3D string.
-const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def);;
-const char* tolua_tourho3dstring(lua_State* L, int narg, const Urho3D::String& def);
-
-/// Convert Lua table field to Urho3D string.
-const char* tolua_tofieldurho3dstring(lua_State* L, int lo, int index, const char* def);;
-const char* tolua_tofieldurho3dstring(lua_State* L, int lo, int index, const Urho3D::String& def);
+const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def);
+const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def);
 
-/// Push Urho3D string vector to Lua.
-int tolua_pushurho3dstringvector(lua_State* L, const Urho3D::Vector<Urho3D::String>& strings);
+/// Check is const Vector<String>.
+int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
+/// Convert to const Vector<String>.
+void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def);
+/// Push const Vector<String>.
+int tolua_pushurho3dconstvectorstring(lua_State* L, void* data, const char* type);

+ 0 - 49
Source/Extras/LuaScript/pkgs/Container/Vector.pkg

@@ -1,55 +1,6 @@
 $#include "Vector.h"
 $#include "Vector3.h"
 
-class Vector : public VectorBase
-{
-    TOLUA_TEMPLATE_BIND(T, String)
-
-    Vector();
-    Vector(const Vector<T>& vector);
-    ~Vector();
-    
-    Vector<T> operator + (const T& rhs) const;
-    Vector<T> operator + (const Vector<T>& rhs) const;
-    
-    bool operator == (const Vector<T>& rhs) const;
-    
-    T& operator [] (unsigned index);
-    const T& operator [] (unsigned index) const;
-    T& At(unsigned index);
-    const T& At(unsigned index) const;
-    
-    void Push(const T& value);
-    void Push(const Vector<T>& vector);
-    void Pop();
-    
-    void Insert(unsigned pos, const T& value);
-    void Insert(unsigned pos, const Vector<T>& vector);
-    void Erase(unsigned pos, unsigned length = 1);
-    
-    bool Remove(const T& value);
-    void Clear();
-    
-    void Resize(unsigned newSize);
-    void Reserve(unsigned newCapacity);
-    void Compact();
-    
-    bool Contains(const T& value) const;
-    
-    T& Front();
-    const T& Front() const;
-    T& Back();
-    const T& Back() const;
-    
-    unsigned Size() const;
-    unsigned Capacity() const;
-    bool Empty() const;
-    
-    tolua_readonly tolua_property__no_prefix unsigned size;
-    tolua_readonly tolua_property__no_prefix unsigned capacity;
-    tolua_readonly tolua_property__no_prefix bool empty;
-};
-
 class PODVector
 {
     TOLUA_TEMPLATE_BIND(T, Vector3)

+ 1 - 10
Source/Extras/LuaScript/pkgs/Core/ProcessUtils.pkg

@@ -5,19 +5,10 @@ void ErrorExit(const String message = String::EMPTY, int exitCode = EXIT_FAILURE
 void OpenConsoleWindow();
 void PrintLine(const String str, bool error = false);
 
-string* GetArguments();
+const Vector<String>& GetArguments();
 
 String GetConsoleInput();
 String GetPlatform();
 
 unsigned GetNumPhysicalCPUs();
 unsigned GetNumLogicalCPUs();
-
-${
-#define TOLUA_DISABLE_tolua_CoreLuaAPI_GetArguments00
-
-static int tolua_CoreLuaAPI_GetArguments00(lua_State* tolua_S)
-{
-    return tolua_pushurho3dstringvector(tolua_S, GetArguments());
-}
-$}

+ 7 - 40
Source/Extras/LuaScript/pkgs/IO/FileSystem.pkg

@@ -9,7 +9,7 @@ class FileSystem : public Object
     bool SetCurrentDir(const String pathName);
     bool CreateDir(const String pathName);
     int SystemCommand(const String commandLine);
-    // int SystemRun(const String fileName, const Vector<String>& arguments);
+    int SystemRun(const String fileName, const Vector<String>& arguments);
     bool SystemOpen(const String fileName, const String mode = String::EMPTY);
     bool Copy(const String srcFileName, const String destFileName);
     bool Rename(const String srcFileName, const String destFileName);
@@ -22,7 +22,7 @@ class FileSystem : public Object
     bool FileExists(const String fileName) const;
     bool DirExists(const String pathName) const;
     
-    Vector<String> ScanDir(const String pathName, const String filter, unsigned flags, bool recursive) const;
+    tolua_outside const Vector<String>& FileSystemScanDir @ ScanDir(const String pathName, const String filter, unsigned flags, bool recursive) const;
     
     String GetProgramDir() const;
     String GetUserDocumentsDir() const;
@@ -41,43 +41,10 @@ String GetNativePath(const String pathName);
 bool IsAbsolutePath(const String pathName);
 
 ${
-#define TOLUA_DISABLE_tolua_IOLuaAPI_FileSystem_ScanDir00
-
-static int tolua_IOLuaAPI_FileSystem_ScanDir00(lua_State* tolua_S)
+static const Vector<String>& FileSystemScanDir(const FileSystem* fileSystem, const String pathName, const String filter, unsigned flags, bool recursive)
 {
-#ifndef TOLUA_RELEASE
- tolua_Error tolua_err;
- if (
- !tolua_isusertype(tolua_S,1,"const FileSystem",0,&tolua_err) ||
- !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
- !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
- !tolua_isnumber(tolua_S,4,0,&tolua_err) ||
- !tolua_isboolean(tolua_S,5,0,&tolua_err) ||
- !tolua_isnoobj(tolua_S,6,&tolua_err)
- )
- goto tolua_lerror;
- else
-#endif
- {
-  const FileSystem* self = (const FileSystem*)  tolua_tousertype(tolua_S,1,0);
-  const String pathName = ((const String)  tolua_tourho3dstring(tolua_S,2,0));
-  const String filter = ((const String)  tolua_tourho3dstring(tolua_S,3,0));
-  unsigned flags = ((unsigned)  tolua_tonumber(tolua_S,4,0));
-  bool recursive = ((bool)  tolua_toboolean(tolua_S,5,0));
-#ifndef TOLUA_RELEASE
- if (!self) tolua_error(tolua_S,"invalid 'self' in function 'ScanDir'", NULL);
-#endif
- {
-  Vector<String> tolua_ret;
-  self->ScanDir(tolua_ret, pathName,filter,flags,recursive);
-  tolua_pushurho3dstringvector(tolua_S, tolua_ret);
- }
- }
- return 1;
-#ifndef TOLUA_RELEASE
- tolua_lerror:
- tolua_error(tolua_S,"#ferror in function 'ScanDir'.",&tolua_err);
- return 0;
-#endif
+    static Vector<String> result;
+    fileSystem->ScanDir(result, pathName, filter, flags, recursive);
+    return result;
 }
-$}
+$}

+ 2 - 2
Source/Extras/LuaScript/pkgs/UI/FileSelector.pkg

@@ -14,10 +14,10 @@ class FileSelector : public Object
     void SetDefaultStyle(XMLFile* style);
     
     void SetTitle(const String text);
-    void SetButtonTexts(const String okText, const String cancelText);    
+    void SetButtonTexts(const String okText, const String cancelText);
     void SetPath(const String path);
     void SetFileName(const String fileName);
-    // void SetFilters(const Vector<String>& filters, unsigned defaultIndex);
+    void SetFilters(const Vector<String>& filters, unsigned defaultIndex);
     void SetDirectoryMode(bool enable);
     void UpdateElements();
 

+ 4 - 0
Source/Extras/LuaScript/pkgs/basic.lua

@@ -3,6 +3,10 @@ local currentString = ''
 local out
 local WRITE, OUTPUT = write, output
 
+_is_functions["const Vector<String>"] = "tolua_isurho3dconstvectorstring"
+_to_functions["const Vector<String>"] = "tolua_tourho3dconstvectorstring"
+_push_functions["const Vector<String>"] = "tolua_pushurho3dconstvectorstring"
+
 _to_functions["HttpRequest"] = "tolua_tourho3dhttprequest"
 
 function output(s)