Beispiel #1
0
        public static T[] CheckParamsObject <T>(IntPtr L, int stackPos, int count)
        {
            List <T> list = new List <T>(count);
            T        obj  = default(T);
            Type     type = typeof(T);

            while (count > 0)
            {
                object tmp = ToVarObject(L, stackPos);

                if (TypeChecker.CheckType(L, type, stackPos))
                {
                    obj = (T)tmp;
                    list.Add(obj);
                }
                else
                {
                    LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(type));
                    break;
                }

                --count;
                ++stackPos;
            }

            return(list.ToArray());
        }
Beispiel #2
0
        public static void PushValue(IntPtr L, ValueType v)
        {
            if (v == null)
            {
                LuaDLL.lua_pushnil(L);
                return;
            }
            Type             type             = v.GetType();
            int              metaReference    = LuaStatic.GetMetaReference(L, type);
            ObjectTranslator objectTranslator = ObjectTranslator.Get(L);

            if (metaReference > 0)
            {
                int index = objectTranslator.AddObject(v);
                LuaDLL.tolua_pushnewudata(L, metaReference, index);
            }
            else
            {
                LuaCSFunction preModule = LuaStatic.GetPreModule(L, type);
                if (preModule != null)
                {
                    preModule(L);
                    metaReference = LuaStatic.GetMetaReference(L, type);
                    if (metaReference > 0)
                    {
                        int index2 = objectTranslator.AddObject(v);
                        LuaDLL.tolua_pushnewudata(L, metaReference, index2);
                        return;
                    }
                }
                LuaDLL.lua_pushnil(L);
                Debugger.LogError("Type {0} not wrap to lua", LuaMisc.GetTypeName(type));
            }
        }
Beispiel #3
0
        public static string GetTypeName(Type t)
        {
            if (t.get_IsArray())
            {
                t = t.GetElementType();
                string typeName = LuaMisc.GetTypeName(t);
                return(typeName + "[]");
            }
            if (t.get_IsByRef())
            {
                t = t.GetElementType();
                return(LuaMisc.GetTypeName(t));
            }
            if (t.get_IsGenericType())
            {
                return(LuaMisc.GetGenericName(t));
            }
            if (t == typeof(void))
            {
                return("void");
            }
            string primitiveStr = LuaMisc.GetPrimitiveStr(t);

            return(primitiveStr.Replace('+', '.'));
        }
Beispiel #4
0
        static public string GetTypeName()
        {
            if (typeName == string.Empty)
            {
                typeName = LuaMisc.GetTypeName(type);
            }

            return(typeName);
        }
Beispiel #5
0
 public static string[] GetGenericName(Type[] types, int offset, int count)
 {
     string[] array = new string[count];
     for (int i = 0; i < count; i++)
     {
         int num = i + offset;
         if (types[num].get_IsGenericType())
         {
             array[i] = LuaMisc.GetGenericName(types[num]);
         }
         else
         {
             array[i] = LuaMisc.GetTypeName(types[num]);
         }
     }
     return(array);
 }
Beispiel #6
0
        public static T[] CheckNumberArray <T>(IntPtr L, int stackPos)
        {
            LuaTypes luaTypes = LuaDLL.lua_type(L, stackPos);

            if (luaTypes == LuaTypes.LUA_TTABLE)
            {
                int      num  = 1;
                T        t    = default(T);
                List <T> list = new List <T>();
                LuaDLL.lua_pushvalue(L, stackPos);
                while (true)
                {
                    LuaDLL.lua_rawgeti(L, -1, num);
                    luaTypes = LuaDLL.lua_type(L, -1);
                    if (luaTypes == LuaTypes.LUA_TNIL)
                    {
                        break;
                    }
                    if (luaTypes != LuaTypes.LUA_TNUMBER)
                    {
                        goto Block_3;
                    }
                    T t2 = (T)((object)Convert.ChangeType(LuaDLL.lua_tonumber(L, -1), typeof(T)));
                    list.Add(t2);
                    LuaDLL.lua_pop(L, 1);
                    num++;
                }
                LuaDLL.lua_pop(L, 1);
                return(list.ToArray());

                Block_3 :;
            }
            else
            {
                if (luaTypes == LuaTypes.LUA_TUSERDATA)
                {
                    return((T[])ToLua.CheckObject(L, stackPos, typeof(T[])));
                }
                if (luaTypes == LuaTypes.LUA_TNIL)
                {
                    return(null);
                }
            }
            LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T[])), null);
            return(null);
        }
Beispiel #7
0
        private static void PushPreLoadType(IntPtr L, object o, Type type)
        {
            LuaCSFunction preModule = LuaStatic.GetPreModule(L, type);

            if (preModule != null)
            {
                ToLua.LuaPCall(L, preModule);
                int metaReference = LuaStatic.GetMetaReference(L, type);
                if (metaReference > 0)
                {
                    ToLua.PushUserData(L, o, metaReference);
                    return;
                }
            }
            LuaDLL.lua_pushnil(L);
            Debugger.LogError("Type {0} not wrap to lua", LuaMisc.GetTypeName(type));
        }
Beispiel #8
0
        public static T[] CheckParamsNumber <T>(IntPtr L, int stackPos, int count)
        {
            double[] array = new double[count];
            int      i     = 0;

            while (i < count)
            {
                if (LuaDLL.lua_isnumber(L, stackPos) == 0)
                {
                    LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T)), null);
                    return(null);
                }
                array[i] = LuaDLL.lua_tonumber(L, stackPos);
                i++;
                stackPos++;
            }
            return((T[])Convert.ChangeType(array, typeof(T[])));
        }
Beispiel #9
0
        public static T[] CheckNumberArray <T>(IntPtr L, int stackPos)
        {
            LuaTypes luatype = LuaDLL.lua_type(L, stackPos);

            if (luatype == LuaTypes.LUA_TTABLE)
            {
                int      index = 1;
                T        ret   = default(T);
                List <T> list  = new List <T>();
                LuaDLL.lua_pushvalue(L, stackPos);

                while (true)
                {
                    LuaDLL.lua_rawgeti(L, -1, index);
                    luatype = LuaDLL.lua_type(L, -1);

                    if (luatype == LuaTypes.LUA_TNIL)
                    {
                        LuaDLL.lua_pop(L, 1);
                        return(list.ToArray());
                    }
                    else if (luatype != LuaTypes.LUA_TNUMBER)
                    {
                        break;
                    }

                    ret = (T)Convert.ChangeType(LuaDLL.lua_tonumber(L, -1), typeof(T));
                    list.Add(ret);
                    LuaDLL.lua_pop(L, 1);
                    ++index;
                }
            }
            else if (luatype == LuaTypes.LUA_TUSERDATA)
            {
                return((T[])CheckObject(L, stackPos, typeof(T[])));
            }
            else if (luatype == LuaTypes.LUA_TNIL)
            {
                return(null);
            }

            LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T[])));
            return(null);
        }
Beispiel #10
0
        public static void PushValue(IntPtr L, ValueType v)
        {
            if (v == null)
            {
                LuaDLL.lua_pushnil(L);
                return;
            }

            Type             type       = v.GetType();
            int              reference  = LuaStatic.GetMetaReference(L, type);
            ObjectTranslator translator = ObjectTranslator.Get(L);

            if (reference > 0)
            {
                int index = translator.AddObject(v);
                LuaDLL.tolua_pushnewudata(L, reference, index);
            }
            else
            {
                LuaCSFunction LuaOpenLib = LuaStatic.GetPreModule(L, type);

                if (LuaOpenLib != null)
                {
#if UNITY_EDITOR
                    Debugger.LogWarning("register PreLoad type {0} to lua", LuaMisc.GetTypeName(type));
#endif
                    LuaOpenLib(L);
                    reference = LuaStatic.GetMetaReference(L, type);

                    if (reference > 0)
                    {
                        int index = translator.AddObject(v);
                        LuaDLL.tolua_pushnewudata(L, reference, index);
                        return;
                    }
                }

                //类型未Wrap
                LuaDLL.lua_pushnil(L);
                Debugger.LogError("Type {0} not wrap to lua", LuaMisc.GetTypeName(type));
            }
        }
Beispiel #11
0
        public static T[] CheckParamsObject <T>(IntPtr L, int stackPos, int count)
        {
            List <T> list           = new List <T>(count);
            T        t              = default(T);
            Type     typeFromHandle = typeof(T);

            while (count > 0)
            {
                object obj = ToLua.ToVarObject(L, stackPos);
                if (!TypeChecker.CheckType(L, typeFromHandle, stackPos))
                {
                    LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeFromHandle), null);
                    break;
                }
                T t2 = (T)((object)obj);
                list.Add(t2);
                count--;
                stackPos++;
            }
            return(list.ToArray());
        }
Beispiel #12
0
        public static T[] CheckParamsNumber <T>(IntPtr L, int stackPos, int count)
        {
            double[] buffer = new double[count];
            int      pos    = 0;

            while (pos < count)
            {
                if (LuaDLL.lua_isnumber(L, stackPos) != 0)
                {
                    buffer[pos] = LuaDLL.lua_tonumber(L, stackPos);
                }
                else
                {
                    LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T)));
                    return(null);
                }

                ++pos;
                ++stackPos;
            }

            return((T[])Convert.ChangeType(buffer, typeof(T[])));
        }
Beispiel #13
0
        static void PushPreLoadType(IntPtr L, object o, Type type)
        {
            LuaCSFunction LuaOpenLib = LuaStatic.GetPreModule(L, type);

            if (LuaOpenLib != null)
            {
#if UNITY_EDITOR
                Debugger.LogWarning("register PreLoad type {0} to lua", LuaMisc.GetTypeName(type));
#endif
                LuaPCall(L, LuaOpenLib);
                int reference = LuaStatic.GetMetaReference(L, type);

                if (reference > 0)
                {
                    PushUserData(L, o, reference);
                    return;
                }
            }

            //类型未Wrap
            LuaDLL.lua_pushnil(L);
            Debugger.LogError("Type {0} not wrap to lua", LuaMisc.GetTypeName(type));
        }