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);
        }