Exemplo n.º 1
0
        public SharpClass RegMethod(string name, MethodBase[] methodInfo, bool isAsync = false)
        {
            MethodReflection method = new MethodReflection(methodInfo);

#if !IL2CPP
            for (int i = 0; i < methodInfo.Length; i++)
            {
                var mi = methodInfo[i] as MethodInfo;
                if (mi != null)
                {
                    if (DelegateCache.GetMethodDelegate(classType, mi, "CallDel", out CallDel fn, out Delegate del))
                    {
                        method.luaFunc[i] = fn;
                        method.del[i]     = del;
                    }
                }
            }
#endif
            //Luna.Log("反射方式实现");
            LuaRef luaFun = LuaRef.CreateFunction(State, MethodReflection.Call, method);
            if (IsTagMethod(name, out var tag))
            {
                meta.RawSet(tag, luaFun);
            }
            else
            {
                meta.RawSet(name, luaFun);
            }

            if (classType.IsArray)
            {
                if (name == "Get")
                {
                    SetMemberFunction(LunaNative.___get_indexed, luaFun);
                }

                if (name == "Set")
                {
                    SetMemberFunction(LunaNative.___set_indexed, luaFun);
                }
            }

            if (isAsync)
            {
                LuaRef r = new LuaRef(State, "coroutine.__async");
                luaFun = r.Call <LuaRef>(luaFun);

                meta.RawSet("_async_" + name, luaFun);
            }

            return(this);
        }
Exemplo n.º 2
0
        static int _Call(lua_State L)
        {
            try
            {
                int              n             = lua_gettop(L);
                MethodBase       methodInfo    = null;
                ParameterInfo[]  parameterInfo = null;
                object[]         args          = null;
                MethodReflection method        = ToLightObject <MethodReflection>(L, lua_upvalueindex(1), false);
                CallDel          callDel       = null;
                Delegate         del           = null;
                for (int i = 0; i < method.methodInfo.Length; i++)
                {
                    var m          = method.methodInfo[i];
                    var paramInfo  = method.parameters[i];
                    int paramCount = n - 1;
#if !LUNA_SCRIPT
                    if (m.IsStatic)
                    {
                        paramCount = n;
                    }
#endif
                    if (paramInfo.Length == paramCount && CheckParameterType(L, n - paramCount + 1, paramInfo))
                    {
                        //todo:CheckType
                        methodInfo    = m;
                        parameterInfo = paramInfo;
                        args          = method.args[i];
                        callDel       = method.luaFunc[i];
                        del           = method.del[i];
                        break;
                    }
                }

                if (callDel != null)
                {
                    return(callDel(L, methodInfo.IsStatic ? 2 : 1, del));
                }

#if LUNA_SCRIPT
                int StackStart = 2;
#else
                int StackStart = methodInfo.IsStatic ? 1 : 2;
#endif

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = GetObject(L, i + StackStart, parameterInfo[i].ParameterType);
                }

                if (methodInfo.IsConstructor)
                {
                    object ret = ((ConstructorInfo)methodInfo).Invoke(args);
                    Array.Clear(args, 0, args.Length);
                    Push(L, ret);
                    return(1);
                }
                else
                {
                    object     obj = methodInfo.IsStatic ? null : GetObject(L, 1, methodInfo.ReflectedType);
                    MethodInfo mi  = methodInfo as MethodInfo;
                    object     ret = methodInfo.Invoke(obj, args);
                    Array.Clear(args, 0, args.Length);

                    if (mi.ReturnType != typeof(void))
                    {
                        Push(L, ret);
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
            }
            catch (Exception e)
            {
                return(luaL_error(L, e.Message));
            }
        }