Exemple #1
0
        internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramType)
        {
            var luatype = LuaLib.LuaType(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            var underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;                      // Silently convert nullable types to their non null requics
            }

            var extractKey = GetExtractDictionaryKey(paramType);

            bool netParamIsNumeric = paramType == typeof(int) ||
                                     paramType == typeof(uint) ||
                                     paramType == typeof(long) ||
                                     paramType == typeof(ulong) ||
                                     paramType == typeof(short) ||
                                     paramType == typeof(ushort) ||
                                     paramType == typeof(float) ||
                                     paramType == typeof(double) ||
                                     paramType == typeof(decimal) ||
                                     paramType == typeof(byte);

            // If it is a nullable
            if (underlyingType != null)
            {
                // null can always be assigned to nullable
                if (luatype == LuaTypes.Nil)
                {
                    // Return the correct extractor anyways
                    if (netParamIsNumeric || paramType == typeof(bool))
                    {
                        return(extractValues [extractKey]);
                    }
                    return(extractNetObject);
                }
            }

            if (paramType.Equals(typeof(object)))
            {
                return(extractValues [extractKey]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.Boolean)
                {
                    return(extractValues [GetExtractDictionaryKey(typeof(bool))]);
                }
                else if (luatype == LuaTypes.String)
                {
                    return(extractValues[GetExtractDictionaryKey(typeof(string))]);
                }
                else if (luatype == LuaTypes.Table)
                {
                    return(extractValues [GetExtractDictionaryKey(typeof(LuaTable))]);
                }
                else if (luatype == LuaTypes.UserData)
                {
                    return(extractValues [GetExtractDictionaryKey(typeof(object))]);
                }
                else if (luatype == LuaTypes.Function)
                {
                    return(extractValues [GetExtractDictionaryKey(typeof(LuaFunction))]);
                }
                else if (luatype == LuaTypes.Number)
                {
                    return(extractValues [GetExtractDictionaryKey(typeof(double))]);
                }
            }
            bool netParamIsString = paramType == typeof(string) || paramType == typeof(char []);

            if (netParamIsNumeric)
            {
                if (LuaLib.LuaIsNumber(luaState, stackPos) && !netParamIsString)
                {
                    return(extractValues [extractKey]);
                }
            }
            else if (paramType == typeof(bool))
            {
                if (LuaLib.LuaIsBoolean(luaState, stackPos))
                {
                    return(extractValues [extractKey]);
                }
            }
            else if (netParamIsString)
            {
                if (LuaLib.LuaNetIsStringStrict(luaState, stackPos))
                {
                    return(extractValues [extractKey]);
                }
                else if (luatype == LuaTypes.Nil)
                {
                    return(extractNetObject);                    // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.Table || luatype == LuaTypes.Nil)
                {
                    return(extractValues [extractKey]);
                }
            }
            else if (paramType == typeof(LuaUserData))
            {
                if (luatype == LuaTypes.UserData || luatype == LuaTypes.Nil)
                {
                    return(extractValues [extractKey]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.Function || luatype == LuaTypes.Nil)
                {
                    return(extractValues [extractKey]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.Function)
            {
                return(new ExtractValue(new DelegateGenerator(translator, paramType).ExtractGenerated));
            }
            else if (paramType.IsInterface() && luatype == LuaTypes.Table)
            {
                return(new ExtractValue(new ClassGenerator(translator, paramType).ExtractGenerated));
            }
            else if ((paramType.IsInterface() || paramType.IsClass()) && luatype == LuaTypes.Nil)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaLib.LuaType(luaState, stackPos) == LuaTypes.Table)
            {
                if (LuaLib.LuaLGetMetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.GetNetObject(luaState, -1);
                    LuaLib.LuaSetTop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                object obj = translator.GetNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }
Exemple #2
0
        internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramType)
        {
            LuaType luatype = luaState.Type(stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            var underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;  // Silently convert nullable types to their non null requics
            }


            bool netParamIsNumeric = paramType == typeof(int) ||
                                     paramType == typeof(uint) ||
                                     paramType == typeof(long) ||
                                     paramType == typeof(ulong) ||
                                     paramType == typeof(short) ||
                                     paramType == typeof(ushort) ||
                                     paramType == typeof(float) ||
                                     paramType == typeof(double) ||
                                     paramType == typeof(decimal) ||
                                     paramType == typeof(byte);

            // If it is a nullable
            if (underlyingType != null)
            {
                // null can always be assigned to nullable
                if (luatype == LuaType.Nil)
                {
                    // Return the correct extractor anyways
                    if (netParamIsNumeric || paramType == typeof(bool))
                    {
                        return(_extractValues[paramType]);
                    }
                    return(_extractNetObject);
                }
            }

            if (paramType == typeof(object))
            {
                return(_extractValues[paramType]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaType.Boolean)
                {
                    return(_extractValues[typeof(bool)]);
                }
                if (luatype == LuaType.String)
                {
                    return(_extractValues[typeof(string)]);
                }
                if (luatype == LuaType.Table)
                {
                    return(_extractValues[typeof(LuaTable)]);
                }
                if (luatype == LuaType.UserData)
                {
                    return(_extractValues[typeof(object)]);
                }
                if (luatype == LuaType.Function)
                {
                    return(_extractValues[typeof(LuaFunction)]);
                }
                if (luatype == LuaType.Number)
                {
                    return(_extractValues[typeof(double)]);
                }
            }
            bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]);

            if (netParamIsNumeric)
            {
                if (luaState.IsNumber(stackPos) && !netParamIsString)
                {
                    return(_extractValues[paramType]);
                }
            }
            else if (paramType == typeof(bool))
            {
                if (luaState.IsBoolean(stackPos))
                {
                    return(_extractValues[paramType]);
                }
            }
            else if (netParamIsString)
            {
                if (luaState.IsString(stackPos))
                {
                    return(_extractValues[paramType]);
                }
                if (luatype == LuaType.Nil)
                {
                    return(_extractNetObject); // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaType.Table || luatype == LuaType.Nil)
                {
                    return(_extractValues[paramType]);
                }
            }
            else if (paramType == typeof(LuaUserData))
            {
                if (luatype == LuaType.UserData || luatype == LuaType.Nil)
                {
                    return(_extractValues[paramType]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaType.Function || luatype == LuaType.Nil)
                {
                    return(_extractValues[paramType]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaType.Function)
            {
                return(new DelegateGenerator(_translator, paramType).ExtractGenerated);
            }
            else if (paramType.IsInterface && luatype == LuaType.Table)
            {
                return(new ClassGenerator(_translator, paramType).ExtractGenerated);
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaType.Nil)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(_extractNetObject);
            }
            else if (luaState.Type(stackPos) == LuaType.Table)
            {
                if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
                {
                    object obj = _translator.GetNetObject(luaState, -1);
                    luaState.SetTop(-2);
                    if (obj != null && paramType.IsInstanceOfType(obj))
                    {
                        return(_extractNetObject);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                object obj = _translator.GetNetObject(luaState, stackPos);
                if (obj != null && paramType.IsInstanceOfType(obj))
                {
                    return(_extractNetObject);
                }
            }

            return(null);
        }