Ejemplo n.º 1
0
        internal static object GetObject(lua_State L, int index)
        {
            LuaType type = lua_type(L, index);

            switch (type)
            {
            case LuaType.Number:
            {
                if (lua_isinteger(L, index))
                {
                    return(lua_tointeger(L, index));
                }

                return(lua_tonumber(L, index));
            }

            case LuaType.String:
                return(lua_tostring(L, index));

            case LuaType.Boolean:
                return((bool)(lua_toboolean(L, index) != 0));

            case LuaType.Table:
            {
                Get(L, index, out LuaRef luaref);
                return(luaref);
            }

            case LuaType.Function:
            {
                Get(L, index, out LuaRef luaref);
                return(luaref);
            }

            case LuaType.LightUserData:
                return(ToLightObject <object>(L, index));

            case LuaType.UserData:
                return(SharpObject.Get <object>(L, index));

            default:
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static T Opt <T>(lua_State L, int index, T def)
        {
            switch (def)
            {
            case bool bval:
                return(Converter.To <T>(luaL_optinteger(L, index, bval ? 1 : 0)));

            case sbyte ival:
                return(Converter.To <T>((sbyte)luaL_optinteger(L, index, ival)));

            case byte ival:
                return(Converter.To <T>((byte)luaL_optinteger(L, index, ival)));

            case short ival:
                return(Converter.To <T>((short)luaL_optinteger(L, index, ival)));

            case ushort ival:
                return(Converter.To <T>((ushort)luaL_optinteger(L, index, ival)));

            case char ival:
                return(Converter.To <T>((char)luaL_optinteger(L, index, ival)));

            case int ival:
                return(Converter.To <T>((int)luaL_optinteger(L, index, ival)));

            case uint ival:
                return(Converter.To <T>((uint)luaL_optinteger(L, index, ival)));

            case long ival:
                return(Converter.To <T>(luaL_optinteger(L, index, ival)));

            case ulong ival:
                return(Converter.To <T>((ulong)luaL_optinteger(L, index, (long)ival)));

            case IntPtr ival:
                return(Converter.To <T>((IntPtr)luaL_optinteger(L, index, (long)ival)));

            case UIntPtr ival:
                return(Converter.To <T>((UIntPtr)luaL_optinteger(L, index, (long)ival)));

            case float fval:
                return(Converter.To <T>((float)luaL_optnumber(L, index, fval)));

            case double fval:
                return(Converter.To <T>(luaL_optnumber(L, index, fval)));

            case string strval:
                return((T)(object)luaL_optstring(L, index, strval));

            case LuaNativeFunction fn:
                return(lua_isnoneornil(L, index) ? def : (T)(object)lua_tocfunction(L, index).ToLuaFunction());

            case LuaByteBuffer v:
            {
                if (lua_isnoneornil(L, index))
                {
                    return(def);
                }
                Get(L, index, out LuaByteBuffer buffer);
                return((T)(object)buffer);
            }

            case byte[] v:
            {
                if (lua_isnoneornil(L, index))
                {
                    return(def);
                }
                Get(L, index, out byte[] buffer);
                return((T)(object)buffer);
            }

            case LuaRef luaRef:
                return(lua_isnone(L, index) ? def : Converter.To <T>(new LuaRef(L, index)));

            default:
            {
                if (typeof(T).IsEnum)
                {
                    return(Converter.To <T>((int)luaL_optinteger(L, index, (long)(object)def)));
                }

                if (lua_isnoneornil(L, index))
                {
                    return(def);
                }

                if (typeof(T).IsUnManaged())
                {
                    return(SharpObject.GetUnmanaged <T>(L, index));
                }

                return(SharpObject.Get <T>(L, index));
            }
            }
        }
Ejemplo n.º 3
0
        public static T Get <T>(lua_State L, int index)
        {
            Type t = typeof(T);

            if (t.IsPrimitive)
            {
                if (t == typeof(bool))
                {
                    return(Converter.To <T>((bool)(lua_toboolean(L, index) != 0)));
                }
                else if (t == typeof(char))
                {
                    return(Converter.To <T>((char)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(sbyte))
                {
                    return(Converter.To <T>((sbyte)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(byte))
                {
                    return(Converter.To <T>((byte)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(short))
                {
                    return(Converter.To <T>((short)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(ushort))
                {
                    return(Converter.To <T>((ushort)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(int))
                {
                    return(Converter.To <T>((int)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(uint))
                {
                    return(Converter.To <T>((uint)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(long))
                {
                    return(Converter.To <T>((long)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(ulong))
                {
                    return(Converter.To <T>((ulong)luaL_checkinteger(L, index)));
                }
                else if (t == typeof(float))
                {
                    return(Converter.To <T>((float)luaL_checknumber(L, index)));
                }
                else if (t == typeof(double))
                {
                    return(Converter.To <T>(luaL_checknumber(L, index)));
                }
                else
                {
                    throw new Exception("Error type");
                }
            }
            else if (t == typeof(IntPtr))
            {
                return(Converter.To <T>((IntPtr)luaL_checkinteger(L, index)));
            }
            else if (t == typeof(UIntPtr))
            {
                return(Converter.To <T>((UIntPtr)luaL_checkinteger(L, index)));
            }
            else if (t == typeof(string))
            {
                return((T)(object)lua_checkstring(L, index));
            }
            else if (t == typeof(LuaNativeFunction))
            {
                return((T)(object)lua_tocfunction(L, index).ToLuaFunction());
            }
            else if (t == typeof(LuaByteBuffer))
            {
                Get(L, index, out LuaByteBuffer buffer);
                return((T)(object)buffer);
            }
            else if (t == typeof(byte[]))
            {
                Get(L, index, out byte[] buffer);
                return((T)(object)buffer);
            }
            else if (t == typeof(LuaRef))
            {
                if (lua_isnone(L, index))
                {
                    return((T)(object)null);// Convert.To<T>(LuaRef.None);
                }
                else
                {
                    return(Converter.To <T>(new LuaRef(L, index)));
                }
            }
            else
            {
                if (t == typeof(object))
                {
                    return((T)GetObject(L, index));
                }
                else if (t.IsEnum)
                {
                    if (lua_type(L, index) == LuaType.Number)
                    {
                        return(Converter.To <T>((int)luaL_checkinteger(L, index)));
                    }
                }
                else if (t.IsValueType)
                {
                    if (t.IsUnManaged())
                    {
                        return(SharpObject.GetUnmanaged <T>(L, index));
                    }
                }

                return(SharpObject.Get <T>(L, index));
            }
        }
Ejemplo n.º 4
0
 public static void Get <T>(lua_State L, int index, out T v) => v = SharpObject.Get <T>(L, index);
Ejemplo n.º 5
0
 public static void Get(lua_State L, int index, out object v) => v = SharpObject.Get <object>(L, index);
Ejemplo n.º 6
0
        internal static object GetObject(lua_State L, int index, Type objtype)
        {
            if (objtype == typeof(float))
            {
                return((float)lua_tonumber(L, index));
            }
            if (objtype == typeof(double))
            {
                return(lua_tonumber(L, index));
            }
            else if (objtype == typeof(int))
            {
                return((int)lua_tonumber(L, index));
            }
            else if (objtype == typeof(uint))
            {
                return((uint)lua_tonumber(L, index));
            }
            else if (objtype == typeof(short))
            {
                return((short)lua_tonumber(L, index));
            }
            else if (objtype == typeof(ushort))
            {
                return((ushort)lua_tonumber(L, index));
            }
            else if (objtype == typeof(sbyte))
            {
                return((sbyte)lua_tonumber(L, index));
            }
            else if (objtype == typeof(byte))
            {
                return((byte)lua_tonumber(L, index));
            }
            else if (objtype == typeof(string))
            {
                return(lua_tostring(L, index));
            }
            else if (objtype == typeof(bool))
            {
                return((bool)(lua_toboolean(L, index) != 0));
            }
            else if (objtype == typeof(IntPtr))
            {
                return((IntPtr)lua_tonumber(L, index));
            }
            else if (objtype == typeof(UIntPtr))
            {
                return((uint)lua_tonumber(L, index));
            }
            else if (objtype == typeof(LuaNativeFunction))
            {
                return(lua_tocfunction(L, index).ToLuaFunction());
            }
            else if (objtype == typeof(byte[]))
            {
                Get(L, index, out byte[] v);
                return(v);
            }
            else if (objtype == typeof(LuaByteBuffer))
            {
                Get(L, index, out LuaByteBuffer v);
                return(v);
            }
            else if (objtype == typeof(LuaRef))
            {
                LuaType type = lua_type(L, index);
                var     obj  = Converter.Convert(objtype, type, L, index);
                if (obj != null)
                {
                    return(obj);
                }
                Get(L, index, out LuaRef luaref);
                return(luaref);
            }
            else if (objtype == typeof(object))
            {
                return(GetObject(L, index));
            }
            else
            {
                if (objtype.IsEnum)
                {
                    return((int)lua_tonumber(L, index));
                }
                if (objtype.IsValueType)
                {
                    if (objtype.IsUnManaged())
                    {
                        return(SharpObject.GetUnmanaged(L, index, objtype));
                    }
                }

                return(SharpObject.Get(L, index, objtype));
            }
        }