Exemple #1
0
        public int call(IntPtr luaState)
        {
            MethodBase method = this._Method;
            object     obj    = this._Target;
            bool       flag   = true;
            int        num    = 0;

            if (!LuaDLL.lua_checkstack(luaState, 5))
            {
                throw new LuaException("Lua stack overflow");
            }
            bool flag2 = (this._BindingType & 8) == 8;

            this.SetPendingException(null);
            if (method == null)
            {
                if (flag2)
                {
                    obj = null;
                }
                else
                {
                    obj = this._ExtractTarget(luaState, 1);
                }
                if (this._LastCalledMethod.cachedMethod != null)
                {
                    int        num2         = flag2 ? 0 : 1;
                    int        num3         = LuaDLL.lua_gettop(luaState) - num2;
                    MethodBase cachedMethod = this._LastCalledMethod.cachedMethod;
                    if (num3 == this._LastCalledMethod.argTypes.Length)
                    {
                        if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                        {
                            throw new LuaException("Lua stack overflow");
                        }
                        object[] args = this._LastCalledMethod.args;
                        try
                        {
                            for (int i = 0; i < this._LastCalledMethod.argTypes.Length; i++)
                            {
                                MethodArgs methodArgs = this._LastCalledMethod.argTypes[i];
                                object     obj2       = methodArgs.extractValue(luaState, i + 1 + num2);
                                if (this._LastCalledMethod.argTypes[i].isParamsArray)
                                {
                                    args[methodArgs.index] = this._Translator.tableToArray(obj2, methodArgs.paramsArrayType);
                                }
                                else
                                {
                                    args[methodArgs.index] = obj2;
                                }
                                if (args[methodArgs.index] == null && !LuaDLL.lua_isnil(luaState, i + 1 + num2))
                                {
                                    throw new LuaException("argument number " + (i + 1) + " is invalid");
                                }
                            }
                            if ((this._BindingType & 8) == 8)
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(null, args));
                            }
                            else if (this._LastCalledMethod.cachedMethod.get_IsConstructor())
                            {
                                this._Translator.push(luaState, ((ConstructorInfo)cachedMethod).Invoke(args));
                            }
                            else
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(obj, args));
                            }
                            flag = false;
                        }
                        catch (TargetInvocationException ex)
                        {
                            int num4   = this.SetPendingException(ex.GetBaseException());
                            int result = num4;
                            return(result);
                        }
                        catch (Exception pendingException)
                        {
                            if (this._Members.Length == 1)
                            {
                                int num5   = this.SetPendingException(pendingException);
                                int result = num5;
                                return(result);
                            }
                        }
                    }
                }
                if (flag)
                {
                    if (!flag2)
                    {
                        if (obj == null)
                        {
                            this._Translator.throwError(luaState, string.Format("instance method '{0}' requires a non null target object", this._MethodName));
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }
                        LuaDLL.lua_remove(luaState, 1);
                    }
                    bool         flag3   = false;
                    string       text    = null;
                    MemberInfo[] members = this._Members;
                    for (int j = 0; j < members.Length; j++)
                    {
                        MemberInfo memberInfo = members[j];
                        text = memberInfo.get_ReflectedType().get_Name() + "." + memberInfo.get_Name();
                        MethodBase method2 = (MethodInfo)memberInfo;
                        bool       flag4   = this._Translator.matchParameters(luaState, method2, ref this._LastCalledMethod);
                        if (flag4)
                        {
                            flag3 = true;
                            break;
                        }
                    }
                    if (!flag3)
                    {
                        string message = (text == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + text);
                        LuaDLL.luaL_error(luaState, message);
                        LuaDLL.lua_pushnil(luaState);
                        this.ClearCachedArgs();
                        return(1);
                    }
                }
            }
            else if (method.get_ContainsGenericParameters())
            {
                this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod);
                if (method.get_IsGenericMethodDefinition())
                {
                    List <Type> list  = new List <Type>();
                    object[]    args2 = this._LastCalledMethod.args;
                    for (int k = 0; k < args2.Length; k++)
                    {
                        object obj3 = args2[k];
                        list.Add(obj3.GetType());
                    }
                    MethodInfo methodInfo = (method as MethodInfo).MakeGenericMethod(list.ToArray());
                    this._Translator.push(luaState, methodInfo.Invoke(obj, this._LastCalledMethod.args));
                    flag = false;
                }
                else if (method.get_ContainsGenericParameters())
                {
                    LuaDLL.luaL_error(luaState, "unable to invoke method on generic class as the current method is an open generic method");
                    LuaDLL.lua_pushnil(luaState);
                    this.ClearCachedArgs();
                    return(1);
                }
            }
            else
            {
                if (!method.get_IsStatic() && !method.get_IsConstructor() && obj == null)
                {
                    obj = this._ExtractTarget(luaState, 1);
                    LuaDLL.lua_remove(luaState, 1);
                }
                if (!this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod))
                {
                    LuaDLL.luaL_error(luaState, "invalid arguments to method call");
                    LuaDLL.lua_pushnil(luaState);
                    this.ClearCachedArgs();
                    return(1);
                }
            }
            if (flag)
            {
                if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                {
                    this.ClearCachedArgs();
                    throw new LuaException("Lua stack overflow");
                }
                try
                {
                    if (flag2)
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(null, this._LastCalledMethod.args));
                    }
                    else if (this._LastCalledMethod.cachedMethod.get_IsConstructor())
                    {
                        this._Translator.push(luaState, ((ConstructorInfo)this._LastCalledMethod.cachedMethod).Invoke(this._LastCalledMethod.args));
                    }
                    else
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(obj, this._LastCalledMethod.args));
                    }
                }
                catch (TargetInvocationException ex2)
                {
                    this.ClearCachedArgs();
                    int num6   = this.SetPendingException(ex2.GetBaseException());
                    int result = num6;
                    return(result);
                }
                catch (Exception pendingException2)
                {
                    this.ClearCachedArgs();
                    int num7   = this.SetPendingException(pendingException2);
                    int result = num7;
                    return(result);
                }
            }
            for (int l = 0; l < this._LastCalledMethod.outList.Length; l++)
            {
                num++;
                this._Translator.push(luaState, this._LastCalledMethod.args[this._LastCalledMethod.outList[l]]);
            }
            if (!this._LastCalledMethod.IsReturnVoid && num > 0)
            {
                num++;
            }
            this.ClearCachedArgs();
            return((num < 1) ? 1 : num);
        }
        public int call(IntPtr luaState)
        {
            MethodBase method = this._Method;
            object     obj2   = this._Target;
            bool       flag   = true;
            int        num    = 0;

            if (!LuaDLL.lua_checkstack(luaState, 5))
            {
                throw new LuaException("Lua stack overflow");
            }
            bool flag2 = (this._BindingType & BindingFlags.Static) == BindingFlags.Static;

            this.SetPendingException(null);
            if (method != null)
            {
                if (method.ContainsGenericParameters)
                {
                    this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod);
                    if (!method.IsGenericMethodDefinition)
                    {
                        if (method.ContainsGenericParameters)
                        {
                            LuaDLL.luaL_error(luaState, "unable to invoke method on generic class as the current method is an open generic method");
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }
                    }
                    else
                    {
                        List <Type> list = new List <Type>();
                        foreach (object obj4 in this._LastCalledMethod.args)
                        {
                            list.Add(obj4.GetType());
                        }
                        MethodInfo info2 = (method as MethodInfo).MakeGenericMethod(list.ToArray());
                        this._Translator.push(luaState, info2.Invoke(obj2, this._LastCalledMethod.args));
                        flag = false;
                    }
                }
                else
                {
                    if ((!method.IsStatic && !method.IsConstructor) && (obj2 == null))
                    {
                        obj2 = this._ExtractTarget(luaState, 1);
                        LuaDLL.lua_remove(luaState, 1);
                    }
                    if (!this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod))
                    {
                        LuaDLL.luaL_error(luaState, "invalid arguments to method call");
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
            }
            else
            {
                if (flag2)
                {
                    obj2 = null;
                }
                else
                {
                    obj2 = this._ExtractTarget(luaState, 1);
                }
                if (this._LastCalledMethod.cachedMethod != null)
                {
                    int        num2         = !flag2 ? 1 : 0;
                    int        num3         = LuaDLL.lua_gettop(luaState) - num2;
                    MethodBase cachedMethod = this._LastCalledMethod.cachedMethod;
                    if (num3 == this._LastCalledMethod.argTypes.Length)
                    {
                        if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                        {
                            throw new LuaException("Lua stack overflow");
                        }
                        object[] parameters = this._LastCalledMethod.args;
                        try
                        {
                            for (int j = 0; j < this._LastCalledMethod.argTypes.Length; j++)
                            {
                                MethodArgs args          = this._LastCalledMethod.argTypes[j];
                                object     luaParamValue = args.extractValue(luaState, (j + 1) + num2);
                                if (this._LastCalledMethod.argTypes[j].isParamsArray)
                                {
                                    parameters[args.index] = this._Translator.tableToArray(luaParamValue, args.paramsArrayType);
                                }
                                else
                                {
                                    parameters[args.index] = luaParamValue;
                                }
                                if ((parameters[args.index] == null) && !LuaDLL.lua_isnil(luaState, (j + 1) + num2))
                                {
                                    throw new LuaException("argument number " + (j + 1) + " is invalid");
                                }
                            }
                            if ((this._BindingType & BindingFlags.Static) == BindingFlags.Static)
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(null, parameters));
                            }
                            else if (this._LastCalledMethod.cachedMethod.IsConstructor)
                            {
                                this._Translator.push(luaState, ((ConstructorInfo)cachedMethod).Invoke(parameters));
                            }
                            else
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(obj2, parameters));
                            }
                            flag = false;
                        }
                        catch (TargetInvocationException exception)
                        {
                            return(this.SetPendingException(exception.GetBaseException()));
                        }
                        catch (Exception exception2)
                        {
                            if (this._Members.Length == 1)
                            {
                                return(this.SetPendingException(exception2));
                            }
                        }
                    }
                }
                if (flag)
                {
                    if (!flag2)
                    {
                        if (obj2 == null)
                        {
                            this._Translator.throwError(luaState, string.Format("instance method '{0}' requires a non null target object", this._MethodName));
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }
                        LuaDLL.lua_remove(luaState, 1);
                    }
                    bool   flag3 = false;
                    string str   = null;
                    foreach (MemberInfo info in this._Members)
                    {
                        str = info.ReflectedType.Name + "." + info.Name;
                        MethodBase base4 = (MethodInfo)info;
                        if (this._Translator.matchParameters(luaState, base4, ref this._LastCalledMethod))
                        {
                            flag3 = true;
                            break;
                        }
                    }
                    if (!flag3)
                    {
                        string message = (str != null) ? ("invalid arguments to method: " + str) : "invalid arguments to method call";
                        LuaDLL.luaL_error(luaState, message);
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
            }
            if (flag)
            {
                if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                {
                    throw new LuaException("Lua stack overflow");
                }
                try
                {
                    if (flag2)
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(null, this._LastCalledMethod.args));
                    }
                    else if (this._LastCalledMethod.cachedMethod.IsConstructor)
                    {
                        this._Translator.push(luaState, ((ConstructorInfo)this._LastCalledMethod.cachedMethod).Invoke(this._LastCalledMethod.args));
                    }
                    else
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(obj2, this._LastCalledMethod.args));
                    }
                }
                catch (TargetInvocationException exception3)
                {
                    return(this.SetPendingException(exception3.GetBaseException()));
                }
                catch (Exception exception4)
                {
                    return(this.SetPendingException(exception4));
                }
            }
            for (int i = 0; i < this._LastCalledMethod.outList.Length; i++)
            {
                num++;
                this._Translator.push(luaState, this._LastCalledMethod.args[this._LastCalledMethod.outList[i]]);
            }
            if (!this._LastCalledMethod.IsReturnVoid && (num > 0))
            {
                num++;
            }
            return((num >= 1) ? num : 1);
        }
Exemple #3
0
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            bool flag = true;

            ParameterInfo[]   parameters      = method.GetParameters();
            int               currentLuaParam = 1;
            int               num2            = LuaDLL.lua_gettop(luaState);
            ArrayList         list            = new ArrayList();
            List <int>        list2           = new List <int>();
            List <MethodArgs> list3           = new List <MethodArgs>();

            foreach (ParameterInfo info in parameters)
            {
                if (!info.IsIn && info.IsOut)
                {
                    list2.Add(list.Add(null));
                }
                else
                {
                    ExtractValue value2;
                    if (currentLuaParam > num2)
                    {
                        if (info.IsOptional)
                        {
                            list.Add(info.DefaultValue);
                            goto Label_01A4;
                        }
                        flag = false;
                        break;
                    }
                    if (this._IsTypeCorrect(luaState, currentLuaParam, info, out value2))
                    {
                        int        item = list.Add(value2(luaState, currentLuaParam));
                        MethodArgs args = new MethodArgs {
                            index        = item,
                            extractValue = value2
                        };
                        list3.Add(args);
                        if (info.ParameterType.IsByRef)
                        {
                            list2.Add(item);
                        }
                        currentLuaParam++;
                    }
                    else if (this._IsParamsArray(luaState, currentLuaParam, info, out value2))
                    {
                        object     luaParamValue = value2(luaState, currentLuaParam);
                        Type       elementType   = info.ParameterType.GetElementType();
                        Array      array         = this.TableToArray(luaParamValue, elementType);
                        int        num5          = list.Add(array);
                        MethodArgs args2         = new MethodArgs {
                            index           = num5,
                            extractValue    = value2,
                            isParamsArray   = true,
                            paramsArrayType = elementType
                        };
                        list3.Add(args2);
                        currentLuaParam++;
                    }
                    else if (info.IsOptional)
                    {
                        list.Add(info.DefaultValue);
                    }
                    else
                    {
                        flag = false;
                        break;
                    }
                    Label_01A4 :;
                }
            }
            if (currentLuaParam != (num2 + 1))
            {
                flag = false;
            }
            if (flag)
            {
                methodCache.args         = list.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList      = list2.ToArray();
                methodCache.argTypes     = list3.ToArray();
            }
            return(flag);
        }