Esempio n. 1
0
        private object GetAsDecimal(LuaState luaState, int stackPos)
        {
            decimal retVal = (decimal)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 2
0
        private object GetAsChar(LuaState luaState, int stackPos)
        {
            char retVal = (char)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 3
0
        private object GetAsFloat(LuaState luaState, int stackPos)
        {
            float retVal = (float)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 4
0
        private object GetAsUlong(LuaState luaState, int stackPos)
        {
            ulong retVal = (ulong)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 5
0
        private object GetAsDouble(LuaState luaState, int stackPos)
        {
            double retVal = LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 6
0
        private object GetAsInt(LuaState luaState, int stackPos)
        {
            if (!LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            int retVal = (int)LuaLib.LuaToNumber(luaState, stackPos);

            return(retVal);
        }
Esempio n. 7
0
        private object GetAsUshort(LuaState luaState, int stackPos)
        {
            ushort retVal = (ushort)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 8
0
        private object GetAsByte(LuaState luaState, int stackPos)
        {
            byte retVal = (byte)LuaLib.LuaToNumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.LuaIsNumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 9
0
        private int GetClassMethodInternal(LuaState luaState)
        {
            IReflect klass;
            object   obj = translator.GetRawNetObject(luaState, 1);

            if (obj == null || !(obj is IReflect))
            {
                translator.ThrowError(luaState, "trying to index an invalid type reference");
                LuaLib.LuaPushNil(luaState);
                return(1);
            }
            else
            {
                klass = (IReflect)obj;
            }

            if (LuaLib.LuaIsNumber(luaState, 2))
            {
                int size = (int)LuaLib.LuaToNumber(luaState, 2);
                translator.Push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size));
                return(1);
            }
            else
            {
                string methodName = LuaLib.LuaToString(luaState, 2).ToString();

                if (string.IsNullOrEmpty(methodName))
                {
                    LuaLib.LuaPushNil(luaState);
                    return(1);
                }                 //CP: Ignore case
                else
                {
                    return(GetMember(luaState, klass, null, methodName, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.IgnoreCase));
                }
            }
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        private int SetFieldOrPropertyInternal(LuaState luaState)
        {
            object target = translator.GetRawNetObject(luaState, 1);

            if (target == null)
            {
                translator.ThrowError(luaState, "trying to index and invalid object reference");
                return(0);
            }

            var type = target.GetType();

            // First try to look up the parameter as a property name
            string detailMessage;
            bool   didMember = TrySetMember(luaState, type, target, BindingFlags.Instance | BindingFlags.IgnoreCase, out detailMessage);

            if (didMember)
            {
                return(0);                         // Must have found the property name
            }
            // We didn't find a property name, now see if we can use a [] style this accessor to set array contents
            try {
                if (type.IsArray && LuaLib.LuaIsNumber(luaState, 2))
                {
                    int    index = (int)LuaLib.LuaToNumber(luaState, 2);
                    var    arr   = (Array)target;
                    object val   = translator.GetAsType(luaState, 3, arr.GetType().GetElementType());
                    arr.SetValue(val, index);
                }
                else
                {
                    // Try to see if we have a this[] accessor
                    var setter = type.GetMethod("set_Item");
                    if (setter != null)
                    {
                        var args      = setter.GetParameters();
                        var valueType = args [1].ParameterType;

                        // The new val ue the user specified
                        object val       = translator.GetAsType(luaState, 3, valueType);
                        var    indexType = args [0].ParameterType;
                        object index     = translator.GetAsType(luaState, 2, indexType);

                        object[] methodArgs = new object[2];

                        // Just call the indexer - if out of bounds an exception will happen
                        methodArgs [0] = index;
                        methodArgs [1] = val;
                        setter.Invoke(target, methodArgs);
                    }
                    else
                    {
                        translator.ThrowError(luaState, detailMessage);                          // Pass the original message from trySetMember because it is probably best
                    }
                }
#if !SILVERLIGHT
            } catch (SEHException) {
                // If we are seeing a C++ exception - this must actually be for Lua's private use.  Let it handle it
                throw;
#endif
            } catch (Exception e) {
                ThrowError(luaState, e);
            }

            return(0);
        }