Example #1
0
        public static bool CheckArray <T>(IntPtr ptr, int p, out T[] ta)
        {
            if (LuaNativeMethods.lua_type(ptr, p) == LuaTypes.TYPE_TABLE)
            {
                int n = LuaNativeMethods.lua_rawlen(ptr, p);
                ta = new T[n];
                for (int k = 0; k < n; k++)
                {
                    LuaNativeMethods.lua_rawgeti(ptr, p, k + 1);
                    object o     = CheckVar(ptr, -1);
                    Type   fromT = o.GetType();
                    Type   toT   = typeof(T);

                    if (toT.IsAssignableFrom(fromT))
                    {
                        ta[k] = (T)o;
                    }
                    else
                    {
                        ta[k] = (T)Convert.ChangeType(o, typeof(T));
                    }

                    LuaNativeMethods.lua_pop(ptr, 1);
                }

                return(true);
            }
            else
            {
                Array array = CheckObj(ptr, p) as Array;
                ta = array as T[];
                return(ta != null);
            }
        }
Example #2
0
 public static int LuaIndex(IntPtr ptr)
 {
     try
     {
         Array a = (Array)CheckSelf(ptr);
         if (LuaNativeMethods.lua_type(ptr, 2) == LuaTypes.TYPE_STRING)
         {
             string mn;
             CheckType(ptr, 2, out mn);
             ArrayPropFunction fun;
             if (propertyMethods.TryGetValue(mn, out fun))
             {
                 LuaObject.PushValue(ptr, true);
                 return(fun(ptr, a) + 1);
             }
             else
             {
                 throw new Exception("Can't find property named " + mn);
             }
         }
         else
         {
             int i;
             CheckType(ptr, 2, out i);
             LuaObject.Assert(i > 0, "index base 1");
             LuaObject.PushValue(ptr, true);
             LuaObject.PushVar(ptr, a.GetValue(i - 1));
             return(2);
         }
     }
     catch (Exception e)
     {
         return(Error(ptr, e));
     }
 }
Example #3
0
        public static void luaL_checktype(IntPtr luaState, int p, LuaTypes t)
        {
            LuaTypes ct = LuaNativeMethods.lua_type(luaState, p);

            if (ct != t)
            {
                throw new Exception(string.Format("arg {0} expect {1}, got {2}", p, lua_typenamestr(luaState, t), lua_typenamestr(luaState, ct)));
            }
        }
        public static bool CheckType(IntPtr ptr, int p, out Type t)
        {
            string   tname = null;
            LuaTypes lt    = LuaNativeMethods.lua_type(ptr, p);

            switch (lt)
            {
            case LuaTypes.TYPE_USERDATA:
                object o = CheckObj(ptr, p);
                if (o.GetType() != monoType)
                {
                    throw new Exception(string.Format("{0} expect Type, got {1}", p, o.GetType().Name));
                }

                t = (Type)o;
                return(true);

            case LuaTypes.TYPE_TABLE:
                LuaNativeMethods.lua_pushstring(ptr, "__type");
                LuaNativeMethods.lua_rawget(ptr, p);
                if (!LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    t = (Type)CheckObj(ptr, -1);
                    LuaNativeMethods.lua_pop(ptr, 1);
                    return(true);
                }
                else
                {
                    LuaNativeMethods.lua_pushstring(ptr, "__fullname");
                    LuaNativeMethods.lua_rawget(ptr, p);
                    tname = LuaNativeMethods.lua_tostring(ptr, -1);
                    LuaNativeMethods.lua_pop(ptr, 2);
                }

                break;

            case LuaTypes.TYPE_STRING:
                CheckType(ptr, p, out tname);
                break;
            }

            if (tname == null)
            {
                throw new Exception("expect string or type table");
            }

            t = LuaObject.FindType(tname);
            if (t != null && lt == LuaTypes.TYPE_TABLE)
            {
                LuaNativeMethods.lua_pushstring(ptr, "__type");
                PushLightObject(ptr, t);
                LuaNativeMethods.lua_rawset(ptr, p);
            }

            return(t != null);
        }
Example #5
0
        public static int ProtectedCall(IntPtr ptr)
        {
            int status;

            if (LuaNativeMethods.lua_type(ptr, 1) != LuaTypes.TYPE_FUNCTION)
            {
                return(LuaObject.Error(ptr, "arg 1 expect function"));
            }

            LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_FUNCTION);
            status = LuaNativeMethods.lua_pcall(ptr, LuaNativeMethods.lua_gettop(ptr) - 1, LuaNativeMethods.LUAMultRet, 0);
            LuaNativeMethods.lua_pushboolean(ptr, status == 0);
            LuaNativeMethods.lua_insert(ptr, 1);
            return(LuaNativeMethods.lua_gettop(ptr));  /* return status + all results */
        }
Example #6
0
        public static bool IsTypeTable(IntPtr ptr, int p)
        {
            if (LuaNativeMethods.lua_type(ptr, p) != LuaTypes.TYPE_TABLE)
            {
                return(false);
            }

            LuaNativeMethods.lua_pushstring(ptr, "__fullname");
            LuaNativeMethods.lua_rawget(ptr, p);
            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                return(false);
            }

            return(true);
        }
Example #7
0
        public static bool MatchType(IntPtr ptr, int total, int from, ParameterInfo[] pars)
        {
            if (total - from + 1 != pars.Length)
            {
                return(false);
            }

            for (int n = 0; n < pars.Length; n++)
            {
                int      p = n + from;
                LuaTypes t = LuaNativeMethods.lua_type(ptr, p);
                if (!MatchType(ptr, p, t, pars[n].ParameterType))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #8
0
        public static int ExtractFunction(IntPtr ptr, int p)
        {
            int      op = 0;
            LuaTypes t  = LuaNativeMethods.lua_type(ptr, p);

            switch (t)
            {
            case LuaTypes.TYPE_NIL:
            case LuaTypes.TYPE_USERDATA:
                op = 0;
                break;

            case LuaTypes.TYPE_TABLE:

                LuaNativeMethods.lua_rawgeti(ptr, p, 1);
                LuaNativeMethods.lua_pushstring(ptr, "+=");
                if (LuaNativeMethods.lua_rawequal(ptr, -1, -2) == 1)
                {
                    op = 1;
                }
                else
                {
                    op = 2;
                }

                LuaNativeMethods.lua_pop(ptr, 2);
                LuaNativeMethods.lua_rawgeti(ptr, p, 2);
                break;

            case LuaTypes.TYPE_FUNCTION:
                LuaNativeMethods.lua_pushvalue(ptr, p);
                break;

            default:
                throw new Exception("expect valid Delegate");
            }

            return(op);
        }
Example #9
0
        public static int IsNull(IntPtr ptr)
        {
            try
            {
                LuaTypes t = LuaNativeMethods.lua_type(ptr, 1);
                LuaObject.PushValue(ptr, true);

                if (t == LuaTypes.TYPE_NIL)
                {
                    LuaObject.PushValue(ptr, true);
                }
                else if (t == LuaTypes.TYPE_USERDATA || LuaObject.IsLuaClass(ptr, 1))
                {
                    // LUA_TUSERDATA or LUA_TTABLE(Class inherited from Unity Native)
                    object o = LuaObject.CheckObj(ptr, 1);
                    if (o is UnityEngine.Object)
                    {
                        LuaObject.PushValue(ptr, ((UnityEngine.Object)o) == null);
                    }
                    else
                    {
                        LuaObject.PushValue(ptr, o.Equals(null));
                    }
                }
                else
                {
                    LuaObject.PushValue(ptr, false);
                }

                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
Example #10
0
 public static bool lua_isboolean(IntPtr luaState, int index)
 {
     return(LuaNativeMethods.lua_type(luaState, index) == LuaTypes.TYPE_BOOLEAN);
 }
Example #11
0
 public static bool lua_isnil(IntPtr luaState, int index)
 {
     return(LuaNativeMethods.lua_type(luaState, index) == LuaTypes.TYPE_NIL);
 }
Example #12
0
 public static string luaL_typename(IntPtr luaState, int stackPos)
 {
     return(LuaNativeMethods.lua_typenamestr(luaState, LuaNativeMethods.lua_type(luaState, stackPos)));
 }
Example #13
0
        public static bool MatchType(IntPtr ptr, int p, Type t1)
        {
            LuaTypes t = LuaNativeMethods.lua_type(ptr, p);

            return(MatchType(ptr, p, t, t1));
        }
Example #14
0
        public static object CheckVar(IntPtr ptr, int p)
        {
            LuaTypes type = LuaNativeMethods.lua_type(ptr, p);

            switch (type)
            {
            case LuaTypes.TYPE_NUMBER:
                return(LuaNativeMethods.lua_tonumber(ptr, p));

            case LuaTypes.TYPE_STRING:
                return(LuaNativeMethods.lua_tostring(ptr, p));

            case LuaTypes.TYPE_BOOLEAN:
                return(LuaNativeMethods.lua_toboolean(ptr, p));

            case LuaTypes.TYPE_FUNCTION:
            {
                LuaFunction v;
                CheckType(ptr, p, out v);
                return(v);
            }

            case LuaTypes.TYPE_TABLE:
            {
                if (IsLuaValueType(ptr, p))
                {
                    if (LuaTypeCheck(ptr, p, "Vector2"))
                    {
                        Vector2 v;
                        CheckType(ptr, p, out v);
                        return(v);
                    }
                    else if (LuaTypeCheck(ptr, p, "Vector3"))
                    {
                        Vector3 v;
                        CheckType(ptr, p, out v);
                        return(v);
                    }
                    else if (LuaTypeCheck(ptr, p, "Vector4"))
                    {
                        Vector4 v;
                        CheckType(ptr, p, out v);
                        return(v);
                    }
                    else if (LuaTypeCheck(ptr, p, "Quaternion"))
                    {
                        Quaternion v;
                        CheckType(ptr, p, out v);
                        return(v);
                    }
                    else if (LuaTypeCheck(ptr, p, "Color"))
                    {
                        Color c;
                        CheckType(ptr, p, out c);
                        return(c);
                    }

                    Logger.LogError("unknown lua value type");
                    return(null);
                }
                else if (IsLuaClass(ptr, p))
                {
                    return(CheckObj(ptr, p));
                }
                else
                {
                    LuaTable v;
                    CheckType(ptr, p, out v);
                    return(v);
                }
            }

            case LuaTypes.TYPE_USERDATA:
                return(LuaObject.CheckObj(ptr, p));

            case LuaTypes.TYPE_THREAD:
            {
                LuaThread lt;
                CheckType(ptr, p, out lt);
                return(lt);
            }

            default:
                return(null);
            }
        }