From 8efd04f4314e44c5b732e95163383b1911d279cb Mon Sep 17 00:00:00 2001
From: client_Wu Xijin <364452445@qq.com>
Date: 星期一, 29 十月 2018 17:30:38 +0800
Subject: [PATCH] 4340 【前端】支持xlua方便IOS提审

---
 Assets/XLua/Src/StaticLuaCallbacks.cs |  121 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 120 insertions(+), 1 deletions(-)

diff --git a/Assets/XLua/Src/StaticLuaCallbacks.cs b/Assets/XLua/Src/StaticLuaCallbacks.cs
index e3ee01a..afca52a 100644
--- a/Assets/XLua/Src/StaticLuaCallbacks.cs
+++ b/Assets/XLua/Src/StaticLuaCallbacks.cs
@@ -781,7 +781,6 @@
                         return 1;
                     }
                 }
-
                 LuaAPI.lua_pushstring(L, string.Format(
                     "\n\tno such file '{0}' in CustomLoaders!", filename));
                 return 1;
@@ -856,6 +855,46 @@
                 {
                     LuaAPI.lua_pushnil(L);
                 }
+                return 1;
+            }
+            catch (System.Exception e)
+            {
+                return LuaAPI.luaL_error(L, "c# exception in xlua.import_type:" + e);
+            }
+        }
+
+        [MonoPInvokeCallback(typeof(LuaCSFunction))]
+        public static int ImportGenericType(RealStatePtr L)
+        {
+            try
+            {
+                int top = LuaAPI.lua_gettop(L);
+                if (top < 2) return LuaAPI.luaL_error(L, "import generic type need at lease 2 arguments");
+                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
+                string className = LuaAPI.lua_tostring(L, 1);
+                if (className.EndsWith("<>")) className = className.Substring(0, className.Length - 2);
+                Type genericDef = translator.FindType(className + "`" + (top - 1));
+                if (genericDef == null || !genericDef.IsGenericTypeDefinition)
+                {
+                    LuaAPI.lua_pushnil(L);
+                }
+                else
+                {
+                    Type[] typeArguments = new Type[top - 1];
+                    for(int i = 2; i <= top; i++)
+                    {
+
+                        typeArguments[i - 2] = getType(L, translator, i);
+                        if (typeArguments[i - 2] == null)
+                        {
+                            return LuaAPI.luaL_error(L, "param need a type");
+                        }
+                    }
+                    Type genericInc = genericDef.MakeGenericType(typeArguments);
+                    translator.GetTypeId(L, genericInc);
+                    translator.PushAny(L, genericInc);
+                }
+
                 return 1;
             }
             catch (System.Exception e)
@@ -1091,6 +1130,86 @@
         }
 
         [MonoPInvokeCallback(typeof(LuaCSFunction))]
+        public static int GenericMethodWraper(RealStatePtr L)
+        {
+            try
+            {
+                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
+                MethodInfo genericMethod;
+                translator.Get(L, LuaAPI.xlua_upvalueindex(1), out genericMethod);
+                int n = LuaAPI.lua_gettop(L);
+                Type[] typeArguments = new Type[n];
+                for(int i = 0; i < n; i++)
+                {
+                    Type type = getType(L, translator, i + 1);
+                    if (type == null)
+                    {
+                        return LuaAPI.luaL_error(L, "param #" + (i + 1) + " is not a type");
+                    }
+                    typeArguments[i] = type;
+                }
+                var method = genericMethod.MakeGenericMethod(typeArguments);
+                translator.PushFixCSFunction(L,
+                        new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(method.DeclaringType, method.Name, new MethodBase[] { method }).Call));
+                return 1;
+            }
+            catch (Exception e)
+            {
+                return LuaAPI.luaL_error(L, "c# exception in GenericMethodWraper: " + e);
+            }
+        }
+
+        [MonoPInvokeCallback(typeof(LuaCSFunction))]
+        public static int GetGenericMethod(RealStatePtr L)
+        {
+            try
+            {
+                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
+                Type type = getType(L, translator, 1);
+                if (type == null)
+                {
+                    return LuaAPI.luaL_error(L, "xlua.get_generic_method, can not find c# type");
+                }
+                string methodName = LuaAPI.lua_tostring(L, 2);
+                if (string.IsNullOrEmpty(methodName))
+                {
+                    return LuaAPI.luaL_error(L, "xlua.get_generic_method, #2 param need a string");
+                }
+                System.Collections.Generic.List<MethodInfo> matchMethods = new System.Collections.Generic.List<MethodInfo>();
+                var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
+                for(int i = 0; i < allMethods.Length; i++)
+                {
+                    var method = allMethods[i];
+                    if (method.Name == methodName && method.IsGenericMethodDefinition)
+                    {
+                        matchMethods.Add(method);
+                    }
+                }
+
+                int methodIdx = 0;
+
+                if (matchMethods.Count == 0)
+                {
+                    LuaAPI.lua_pushnil(L);
+                }
+                else
+                {
+                    if (LuaAPI.lua_isinteger(L, 3))
+                    {
+                        methodIdx = LuaAPI.xlua_tointeger(L, 3);
+                    }
+                    translator.PushAny(L, matchMethods[methodIdx]);
+                    LuaAPI.lua_pushstdcallcfunction(L, GenericMethodWraper, 1);
+                }
+            }
+            catch (Exception e)
+            {
+                return LuaAPI.luaL_error(L, "c# exception in xlua.get_generic_method: " + e);
+            }
+            return 1;
+        }
+
+        [MonoPInvokeCallback(typeof(LuaCSFunction))]
         public static int ReleaseCsObject(RealStatePtr L)
         {
             try

--
Gitblit v1.8.0