Esempio n. 1
0
        public IEnumerable GetKeys()
        {
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop     = LuaAPI.lua_gettop(L);

            LuaAPI.lua_getref(L, luaReference);
            LuaAPI.lua_pushnil(L);
            while (LuaAPI.lua_next(L, -2) != 0)
            {
                yield return(translator.GetObject(L, -2));

                LuaAPI.lua_pop(L, 1);
            }
            LuaAPI.lua_settop(L, oldTop);
        }
Esempio n. 2
0
        public static void EndClassRegister(Type type, RealStatePtr L, ObjectTranslator translator)
        {
            int top            = LuaAPI.lua_gettop(L);
            int cls_idx        = abs_idx(top, CLS_IDX);
            int cls_getter_idx = abs_idx(top, CLS_GETTER_IDX);
            int cls_setter_idx = abs_idx(top, CLS_SETTER_IDX);
            int cls_meta_idx   = abs_idx(top, CLS_META_IDX);

            //begin cls index
            LuaAPI.xlua_pushasciistring(L, "__index");
            LuaAPI.lua_pushvalue(L, cls_getter_idx);
            LuaAPI.lua_pushvalue(L, cls_idx);
            translator.Push(L, type.BaseType);
            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaAPI.gen_cls_indexer(L);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua indexs function tables
            translator.Push(L, type);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);

            LuaAPI.lua_rawset(L, cls_meta_idx);
            //end cls index

            //begin cls newindex
            LuaAPI.xlua_pushasciistring(L, "__newindex");
            LuaAPI.lua_pushvalue(L, cls_setter_idx);
            translator.Push(L, type.BaseType);
            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaAPI.gen_cls_newindexer(L);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua newindexs function tables
            translator.Push(L, type);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);

            LuaAPI.lua_rawset(L, cls_meta_idx);
            //end cls newindex

            LuaAPI.lua_pop(L, 4);
        }
Esempio n. 3
0
        public bool TryDelayWrapLoader(RealStatePtr L, Type type)
        {
            if (loaded_types.ContainsKey(type))
            {
                return(true);
            }
            loaded_types.Add(type, true);

            LuaAPI.luaL_newmetatable(L, type.FullName); //先建一个metatable,因为加载过程可能会需要用到
            LuaAPI.lua_pop(L, 1);

            Action <RealStatePtr> loader;
            int top = LuaAPI.lua_gettop(L);

            if (delayWrap.TryGetValue(type, out loader))
            {
                delayWrap.Remove(type);
                loader(L);
            }
            else
            {
                Utils.ReflectionWrap(L, type);
#if NOT_GEN_WARNING
#if !XLUA_GENERAL
                UnityEngine.Debug.LogWarning(string.Format("{0} not gen, using reflection instead", type));
#else
                System.Console.WriteLine(string.Format("Warning: {0} not gen, using reflection instead", type));
#endif
#endif
            }
            if (top != LuaAPI.lua_gettop(L))
            {
                throw new Exception("top change, before:" + top + ", after:" + LuaAPI.lua_gettop(L));
            }

            foreach (var nested_type in type.GetNestedTypes())
            {
                if ((!nested_type.IsAbstract && typeof(Delegate).IsAssignableFrom(nested_type)) ||
                    nested_type.IsGenericTypeDefinition)
                {
                    continue;
                }
                TryDelayWrapLoader(L, nested_type);
            }

            return(true);
        }
Esempio n. 4
0
        internal static int Print(RealStatePtr L)
        {
            try
            {
                int    n = LuaAPI.lua_gettop(L);
                string s = String.Empty;

                if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
                {
                    return(LuaAPI.luaL_error(L, "can not get tostring in print:"));
                }

                for (int i = 1; i <= n; i++)
                {
                    LuaAPI.lua_pushvalue(L, -1);  /* function to be called */
                    LuaAPI.lua_pushvalue(L, i);   /* value to print */
                    if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
                    {
                        return(LuaAPI.lua_error(L));
                    }
                    s += LuaAPI.lua_tostring(L, -1);

                    if (i != n)
                    {
                        s += "\t";
                    }

                    LuaAPI.lua_pop(L, 1);  /* pop result */
                }

                LuaAPI.xlua_getglobal(L, "debug");
                LuaAPI.lua_getfield(L, -1, "traceback");
                LuaAPI.lua_pushstring(L, s);
                LuaAPI.lua_pushnumber(L, 2);
                LuaAPI.lua_pcall(L, 2, 1, 0);
                s = LuaAPI.lua_tostring(L, -1);
                LuaAPI.lua_pop(L, 1);

                UnityEngine.Debug.Log("LUA: " + s);
                return(0);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in print:" + e));
            }
        }
Esempio n. 5
0
        public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out PushAsTableStruct val)
        {
            val = new PushAsTableStruct();
            int top = LuaAPI.lua_gettop(L);

            if (Utils.LoadField(L, idx, "x"))
            {
                translator.Get(L, top + 1, out val.x);
            }
            LuaAPI.lua_pop(L, 1);

            if (Utils.LoadField(L, idx, "y"))
            {
                translator.Get(L, top + 1, out val.y);
            }
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 6
0
        public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out UnityEngine.Vector2 val)
        {
            val = new UnityEngine.Vector2();
            int top = LuaAPI.lua_gettop(L);

            if (XUtils.LoadField(L, idx, "x"))
            {
                translator.Get(L, top + 1, out val.x);
            }
            LuaAPI.lua_pop(L, 1);

            if (XUtils.LoadField(L, idx, "y"))
            {
                translator.Get(L, top + 1, out val.y);
            }
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 7
0
        internal static int Print(RealStatePtr L)
        {
            try
            {
                int    n = LuaAPI.lua_gettop(L);
                string s = String.Empty;

                if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
                {
                    return(LuaAPI.luaL_error(L, "can not get tostring in print:"));
                }

                for (int i = 1; i <= n; i++)
                {
                    LuaAPI.lua_pushvalue(L, -1);  /* function to be called */
                    LuaAPI.lua_pushvalue(L, i);   /* value to print */
                    if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
                    {
                        return(LuaAPI.lua_error(L));
                    }
                    s += LuaAPI.lua_tostring(L, -1);

                    if (i != n)
                    {
                        s += "\t";
                    }

                    LuaAPI.lua_pop(L, 1);  /* pop result */
                }

                if (onLuaCallPrintHandler != null)
                {
                    onLuaCallPrintHandler.Invoke(s);
                }
                else
                {
                    UnityEngine.Debug.Log("LUA: " + s);
                }
                return(0);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in print:" + e));
            }
        }
Esempio n. 8
0
		internal void createFunctionMetatable(RealStatePtr L)
		{
			LuaAPI.lua_newtable(L);
			LuaAPI.xlua_pushasciistring(L,"__gc");
			LuaAPI.lua_pushstdcallcfunction(L,metaFunctions.GcMeta);
			LuaAPI.lua_rawset(L,-3);
            LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag());
            LuaAPI.lua_pushnumber(L, 1);
            LuaAPI.lua_rawset(L, -3);

            LuaAPI.lua_pushvalue(L, -1);
            int type_id = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaAPI.lua_pushnumber(L, type_id);
            LuaAPI.xlua_rawseti(L, -2, 1);
            LuaAPI.lua_pop(L, 1);

            typeIdMap.Add(typeof(LuaCSFunction), type_id);
        }
Esempio n. 9
0
        public T Cast <T>()
        {
            var L          = luaEnv.L;
            var translator = luaEnv.translator;

#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            push(L);
            T ret = (T)translator.GetObject(L, -1, typeof(T));
            LuaAPI.lua_pop(luaEnv.L, 1);
            return(ret);

#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 10
0
        public void TryForEach <TKey>(Type type_of_value, Action <TKey, object, bool> action)
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop     = LuaAPI.lua_gettop(L);
            try
            {
                LuaAPI.lua_getref(L, luaReference);
                LuaAPI.lua_pushnil(L);
                while (LuaAPI.lua_next(L, -2) != 0)
                {
                    if (translator.Assignable <TKey>(L, -2))
                    {
                        bool isAssignable = translator.Assignable(L, -1, type_of_value);
                        if (isAssignable)
                        {
                            TKey key;
                            object val;
                            translator.Get(L, -2, out key);
                            translator.Get(L, -1, out val);
                            action(key, val, isAssignable);
                        }
                        else
                        {
                            action(default(TKey), null, isAssignable);
                        }
                    }
                    LuaAPI.lua_pop(L, 1);
                }
            }
            finally
            {
                LuaAPI.lua_settop(L, oldTop);
            }
#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 11
0
        public IEnumerable <T> GetKeys <T>()
        {
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop     = LuaAPI.lua_gettop(L);

            LuaAPI.lua_getref(L, luaReference);
            LuaAPI.lua_pushnil(L);
            while (LuaAPI.lua_next(L, -2) != 0)
            {
                if (translator.Assignable <T>(L, -2))
                {
                    T v;
                    translator.Get(L, -2, out v);
                    yield return(v);
                }
                LuaAPI.lua_pop(L, 1);
            }
            LuaAPI.lua_settop(L, oldTop);
        }
Esempio n. 12
0
        public static void SetCSTable(RealStatePtr L, Type type, int cls_table)
        {
            int oldTop = LuaAPI.lua_gettop(L);

            cls_table = abs_idx(oldTop, cls_table);
            if (0 != LuaAPI.xlua_getglobal(L, "CS"))
            {
                throw new Exception("call xlua_getglobal fail!");
            }

            List <string> path = getPathOfType(type);

            for (int i = 0; i < path.Count - 1; ++i)
            {
                LuaAPI.xlua_pushasciistring(L, path[i]);
                if (0 != LuaAPI.xlua_pgettable(L, -2))
                {
                    LuaAPI.lua_settop(L, oldTop);
                    throw new Exception("SetCSTable for [" + type + "] error: " + LuaAPI.lua_tostring(L, -1));
                }
                if (LuaAPI.lua_isnil(L, -1))
                {
                    LuaAPI.lua_pop(L, 1);
                    LuaAPI.lua_createtable(L, 0, 0);
                    LuaAPI.xlua_pushasciistring(L, path[i]);
                    LuaAPI.lua_pushvalue(L, -2);
                    LuaAPI.lua_rawset(L, -4);
                }
                else if (!LuaAPI.lua_istable(L, -1))
                {
                    LuaAPI.lua_settop(L, oldTop);
                    throw new Exception("SetCSTable for [" + type + "] error: ancestors is not a table!");
                }
                LuaAPI.lua_remove(L, -2);
            }

            LuaAPI.xlua_pushasciistring(L, path[path.Count - 1]);
            LuaAPI.lua_pushvalue(L, cls_table);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 13
0
        public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out UnityEngine.Bounds val)
        {
            val = new UnityEngine.Bounds();
            int top = LuaAPI.lua_gettop(L);

            if (XUtils.LoadField(L, idx, "center"))
            {
                var center = val.center;
                translator.Get(L, top + 1, out center);
                val.center = center;
            }
            LuaAPI.lua_pop(L, 1);

            if (XUtils.LoadField(L, idx, "extents"))
            {
                var extents = val.extents;
                translator.Get(L, top + 1, out extents);
                val.extents = extents;
            }
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 14
0
        public Delegate Cast(Type delType)
        {
            if (!delType.IsSubclassOf(typeof(Delegate)))
            {
                throw new InvalidOperationException(delType.Name + " is not a delegate type");
            }
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            push(L);
            Delegate ret = (Delegate)translator.GetObject(L, -1, delType);
            LuaAPI.lua_pop(luaEnv.L, 1);
            return(ret);

#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 15
0
        public T Cast <T>()
        {
            if (!typeof(T).IsSubclassOf(typeof(Delegate)))
            {
                throw new InvalidOperationException(typeof(T).Name + " is not a delegate type");
            }
#if THREAD_SAFT || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            push(L);
            T ret = (T)translator.GetObject(L, -1, typeof(T));
            LuaAPI.lua_pop(luaEnv.L, 1);
            return(ret);

#if THREAD_SAFT || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 16
0
        public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out UnityEngine.Ray2D val)
        {
            val = new UnityEngine.Ray2D();
            int top = LuaAPI.lua_gettop(L);

            if (XUtils.LoadField(L, idx, "origin"))
            {
                var origin = val.origin;
                translator.Get(L, top + 1, out origin);
                val.origin = origin;
            }
            LuaAPI.lua_pop(L, 1);

            if (XUtils.LoadField(L, idx, "direction"))
            {
                var direction = val.direction;
                translator.Get(L, top + 1, out direction);
                val.direction = direction;
            }
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 17
0
        internal static int Print(RealStatePtr L)
        {
            try
            {
                int    n = LuaAPI.lua_gettop(L);
                string s = String.Empty;

                if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
                {
                    return(LuaAPI.luaL_error(L, "can not get tostring in print:"));
                }

                for (int i = 1; i <= n; i++)
                {
                    LuaAPI.lua_pushvalue(L, -1);  /* function to be called */
                    LuaAPI.lua_pushvalue(L, i);   /* value to print */
                    if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
                    {
                        return(LuaAPI.lua_error(L));
                    }
                    s += LuaAPI.lua_tostring(L, -1);

                    if (i != n)
                    {
                        s += "\t";
                    }

                    LuaAPI.lua_pop(L, 1);  /* pop result */
                }
                //add by zhehua
                LuaTable debugTable = GameEntry.XLua.luaGlobal.Get <LuaTable>("debug");
                XLua.VoidReturnString tracebackFunc = debugTable.Get <XLua.VoidReturnString>("traceback");
                UnityEngine.Debug.Log(s + '\n' + tracebackFunc.Invoke());
                return(0);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in print:" + e));
            }
        }
        public void OpenLib(RealStatePtr L)
        {
            LuaAPI.lua_getglobal(L, "xlua");
            LuaAPI.xlua_pushasciistring(L, "import_type");
            LuaAPI.lua_pushstdcallcfunction(L, importTypeFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "cast");
            LuaAPI.lua_pushstdcallcfunction(L, castFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "load_assembly");
            LuaAPI.lua_pushstdcallcfunction(L, loadAssemblyFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "access");
            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaAccess);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);

            LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex
            common_array_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex
            common_delegate_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
        }
Esempio n. 19
0
        internal static int CheckNewType(IntPtr L)
        {
            if (!LuaAPI.lua_istable(L, 1))
            {
                Debug.Log("Need Table Here!");
                return(0);
            }

            // key值只能是数字或者字符串
            if (LuaAPI.lua_isnumber(L, 2) && LuaAPI.lua_isstring(L, 2))
            {
                Debug.Log("Need Number or String Here!");
                return(0);
            }

            // value值只能是数字、字符串、非循环table
            if (LuaAPI.lua_isnumber(L, 3) && LuaAPI.lua_isstring(L, 3) && !LuaAPI.lua_istable(L, 3) && !LuaAPI.lua_isboolean(L, 3))
            {
                Debug.Log("This Type Cannot Add to Cookies!Need[Table, Number, String]!");
                return(0);
            }

            // 如果是table,检查是否有循环结构或者是其他类型
            if (LuaAPI.lua_istable(L, 3))
            {
                int cycle_time = 0;
                if (!CheckCircleAndSetMeta(L, ref cycle_time) || !SetMetaTable(L, 3))
                {
                    Debug.Log("This Type Cannot Add to Cookies!Need[Table, Number, String]!");
                    LuaAPI.lua_pop(L, 1);
                    return(0);
                }
            }

            LuaAPI.lua_rawset(L, 1);

            return(0);
        }
Esempio n. 20
0
        internal static int Print(RealStatePtr L)
        {
            try
            {
                int n = LuaAPI.lua_gettop(L);
                System.Text.StringBuilder s = new System.Text.StringBuilder();
                s.AppendFormat("[Lua] {0} ", LuaWhere(L));

                if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
                {
                    return(LuaAPI.luaL_error(L, "can not get tostring in print:"));
                }

                for (int i = 1; i <= n; i++)
                {
                    LuaAPI.lua_pushvalue(L, -1);  /* function to be called */
                    LuaAPI.lua_pushvalue(L, i);   /* value to print */
                    if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
                    {
                        return(LuaAPI.lua_error(L));
                    }
                    s.Append(LuaAPI.lua_tostring(L, -1));

                    if (i != n)
                    {
                        s.Append("\t");
                    }

                    LuaAPI.lua_pop(L, 1);  /* pop result */
                }
                LoggerHelper.Debug(s.ToString());
                return(0);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in print:" + e));
            }
        }
Esempio n. 21
0
        public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out Core.Math.Vec3 val)
        {
            val = new Core.Math.Vec3();
            int top = LuaAPI.lua_gettop(L);

            if (Utils.LoadField(L, idx, "x"))
            {
                translator.Get(L, top + 1, out val.x);
            }
            LuaAPI.lua_pop(L, 1);

            if (Utils.LoadField(L, idx, "y"))
            {
                translator.Get(L, top + 1, out val.y);
            }
            LuaAPI.lua_pop(L, 1);

            if (Utils.LoadField(L, idx, "z"))
            {
                translator.Get(L, top + 1, out val.z);
            }
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 22
0
        public void ForEach <TKey, TValue>(Func <TKey, TValue, bool> action)
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop     = LuaAPI.lua_gettop(L);
            try
            {
                LuaAPI.lua_getref(L, luaReference);
                LuaAPI.lua_pushnil(L);
                while (LuaAPI.lua_next(L, -2) != 0)
                {
                    if (translator.Assignable <TKey>(L, -2))
                    {
                        TKey key;
                        TValue val;
                        translator.Get(L, -2, out key);
                        translator.Get(L, -1, out val);
                        if (action(key, val))
                        {
                            break;
                        }
                    }
                    LuaAPI.lua_pop(L, 1);
                }
            }
            finally
            {
                LuaAPI.lua_settop(L, oldTop);
            }
#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 23
0
        internal static int CollectLog(RealStatePtr L, out string s)
        {
            s = String.Empty;
            try
            {
                int n = LuaAPI.lua_gettop(L);

                if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
                {
                    return(LuaAPI.luaL_error(L, "can not get tostring in print:"));
                }

                for (int i = 1; i <= n; i++)
                {
                    LuaAPI.lua_pushvalue(L, -1);  /* function to be called */
                    LuaAPI.lua_pushvalue(L, i);   /* value to print */
                    if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
                    {
                        return(LuaAPI.lua_error(L));
                    }
                    s += LuaAPI.lua_tostring(L, -1);

                    if (i != n)
                    {
                        s += "    ";
                    }

                    LuaAPI.lua_pop(L, 1);  /* pop result */
                }
                return(0);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in print:" + e));
            }
        }
Esempio n. 24
0
        public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int num)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

            LuaAPI.xlua_pushasciistring(L, metafunc);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
            translator.Push(L, type);
            LuaAPI.lua_rawget(L, -2);
            for (int i = 1; i <= num; i++)
            {
                LuaAPI.lua_getupvalue(L, -i, i);
                if (LuaAPI.lua_isnil(L, -1))
                {
                    LuaAPI.lua_pop(L, 1);
                    LuaAPI.lua_newtable(L);
                    LuaAPI.lua_pushvalue(L, -1);
                    LuaAPI.lua_setupvalue(L, -i - 2, i);
                }
            }
            for (int i = 0; i < num; i++)
            {
                LuaAPI.lua_remove(L, -num - 1);
            }
        }
Esempio n. 25
0
        //meta: -4, method:-3, getter: -2, setter: -1
        public static void BeginObjectRegister(Type type, RealStatePtr L, ObjectTranslator translator, int meta_count, int method_count, int getter_count,
                                               int setter_count, int type_id = -1)
        {
            if (type == null)
            {
                if (type_id == -1)
                {
                    throw new Exception("Fatal: must provide a type of type_id");
                }
                LuaAPI.xlua_rawgeti(L, LuaIndexes.LUA_REGISTRYINDEX, type_id);
            }
            else
            {
                LuaAPI.luaL_getmetatable(L, type.FullName);
                if (LuaAPI.lua_isnil(L, -1))
                {
                    LuaAPI.lua_pop(L, 1);
                    LuaAPI.luaL_newmetatable(L, type.FullName);
                }
            }
            LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag());
            LuaAPI.lua_pushnumber(L, 1);
            LuaAPI.lua_rawset(L, -3);

            if ((type == null || !translator.HasCustomOp(type)) && type != typeof(decimal))
            {
                LuaAPI.xlua_pushasciistring(L, "__gc");
                LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.GcMeta);
                LuaAPI.lua_rawset(L, -3);
            }

            LuaAPI.xlua_pushasciistring(L, "__tostring");
            LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.ToStringMeta);
            LuaAPI.lua_rawset(L, -3);

            if (method_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, method_count);
            }

            if (getter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, getter_count);
            }

            if (setter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, setter_count);
            }
        }
Esempio n. 26
0
        int getTypeId(RealStatePtr L, Type type, out bool is_first, LOGLEVEL log_level = LOGLEVEL.WARN)
        {
            int type_id;

            is_first = false;
            if (!typeIdMap.TryGetValue(type, out type_id)) // no reference
            {
                if (type.IsArray)
                {
                    if (common_array_meta == -1)
                    {
                        throw new Exception("Fatal Exception! Array Metatable not inited!");
                    }
                    return(common_array_meta);
                }
                if (typeof(MulticastDelegate).IsAssignableFrom(type))
                {
                    if (common_delegate_meta == -1)
                    {
                        throw new Exception("Fatal Exception! Delegate Metatable not inited!");
                    }
                    return(common_delegate_meta);
                }

                is_first = true;
                Type alias_type = null;
                aliasCfg.TryGetValue(type, out alias_type);
                LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName);

                if (LuaAPI.lua_isnil(L, -1)) //no meta yet, try to use reflection meta
                {
                    LuaAPI.lua_pop(L, 1);

                    if (TryDelayWrapLoader(L, alias_type == null ? type : alias_type))
                    {
                        LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName);
                    }
                    else
                    {
                        throw new Exception("Fatal: can not load metatable of type:" + type);
                    }
                }

                //循环依赖,自身依赖自己的class,比如有个自身类型的静态readonly对象。
                if (typeIdMap.TryGetValue(type, out type_id))
                {
                    LuaAPI.lua_pop(L, 1);
                }
                else
                {
                    LuaAPI.lua_pushvalue(L, -1);
                    type_id = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
                    LuaAPI.lua_pushnumber(L, type_id);
                    LuaAPI.xlua_rawseti(L, -2, 1);
                    LuaAPI.lua_pop(L, 1);

                    if (type.IsValueType)
                    {
                        typeMap.Add(type_id, type);
                    }

                    typeIdMap.Add(type, type_id);
                }
            }
            return(type_id);
        }
Esempio n. 27
0
        public Delegate CreateDelegateBridge(RealStatePtr L, Type delegateType, int idx)
        {
            LuaAPI.lua_pushvalue(L, idx);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
            if (!LuaAPI.lua_isnil(L, -1))
            {
                int referenced = LuaAPI.xlua_tointeger(L, -1);
                LuaAPI.lua_pop(L, 1);

                if (delegate_bridges[referenced].IsAlive)
                {
                    DelegateBridgeBase exist_bridge = delegate_bridges[referenced].Target as DelegateBridgeBase;
                    Delegate           exist_delegate;
                    if (exist_bridge.TryGetDelegate(delegateType, out exist_delegate))
                    {
                        return(exist_delegate);
                    }
                    else
                    {
                        exist_delegate = exist_bridge.GetDelegateByType(delegateType);
                        exist_bridge.AddDelegate(delegateType, exist_delegate);
                        return(exist_delegate);
                    }
                }
            }
            else
            {
                LuaAPI.lua_pop(L, 1);
            }

            LuaAPI.lua_pushvalue(L, idx);
            int reference = LuaAPI.luaL_ref(L);

            LuaAPI.lua_pushvalue(L, idx);
            LuaAPI.lua_pushnumber(L, reference);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);
            DelegateBridgeBase bridge;

            try
            {
#if UNITY_EDITOR
                if (!DelegateBridge.Gen_Flag)
                {
                    bridge = Activator.CreateInstance(delegate_birdge_type, new object[] { reference, luaEnv }) as DelegateBridgeBase;
                }
                else
#endif
                {
                    bridge = new DelegateBridge(reference, luaEnv);
                }
            }
            catch (Exception e)
            {
                LuaAPI.lua_pushvalue(L, idx);
                LuaAPI.lua_pushnil(L);
                LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);
                LuaAPI.lua_pushnil(L);
                LuaAPI.xlua_rawseti(L, LuaIndexes.LUA_REGISTRYINDEX, reference);
                throw e;
            }
            try {
                var ret = bridge.GetDelegateByType(delegateType);
                bridge.AddDelegate(delegateType, ret);
                delegate_bridges[reference] = new WeakReference(bridge);
                return(ret);
            }
            catch (Exception e)
            {
                bridge.Dispose();
                throw e;
            }
        }
Esempio n. 28
0
        public LuaEnv()
        {
#if THREAD_SAFT || HOTFIX_ENABLE
            lock (luaEnvLock)
            {
#endif
            LuaIndexes.LUA_REGISTRYINDEX = LuaAPI.xlua_get_registry_index();;
            // Create State
            L = LuaAPI.luaL_newstate();

            //Init Base Libs
            LuaAPI.luaopen_xlua(L);
            LuaAPI.luaopen_i64lib(L);
            LuaAPI.luaopen_perflib(L);

            translator = new ObjectTranslator(this, L);
            translator.createFunctionMetatable(L);
            translator.OpenLib(L);
            ObjectTranslatorPool.Instance.Add(L, translator);

            LuaAPI.lua_atpanic(L, StaticLuaCallbacks.Panic);

            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.Print);
            if (0 != LuaAPI.xlua_setglobal(L, "print"))
            {
                throw new Exception("call xlua_setglobal fail!");
            }

            //template engine lib register
            TemplateEngine.LuaTemplate.OpenLib(L);

            AddSearcher(StaticLuaCallbacks.LoadBuiltinLib, 2);     // just after the preload searcher
            AddSearcher(StaticLuaCallbacks.LoadFromCustomLoaders, 3);
            AddSearcher(StaticLuaCallbacks.LoadFromResource, 4);
            AddSearcher(StaticLuaCallbacks.LoadFromStreamingAssetsPath, -1);
            DoString(init_xlua, "Init");
            init_xlua = null;

            AddBuildin("socket.core", StaticLuaCallbacks.LoadSocketCore);
            AddBuildin("socket", StaticLuaCallbacks.LoadSocketCore);

            LuaAPI.lua_newtable(L);     //metatable of indexs and newindexs functions
            LuaAPI.xlua_pushasciistring(L, "__index");
            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.MetaFuncIndex);
            LuaAPI.lua_rawset(L, -3);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName);
            LuaAPI.lua_newtable(L);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName);
            LuaAPI.lua_newtable(L);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName);
            LuaAPI.lua_newtable(L);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName);
            LuaAPI.lua_newtable(L);
            LuaAPI.lua_pushvalue(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.lua_pop(L, 1);     // pop metatable of indexs and newindexs functions

            LuaAPI.xlua_pushasciistring(L, "xlua_main_thread");
            LuaAPI.lua_pushthread(L);
            LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX);

            translator.Alias(typeof(Type), "System.MonoType");

            if (0 != LuaAPI.xlua_getglobal(L, "_G"))
            {
                throw new Exception("call xlua_getglobal fail!");
            }
            translator.Get(L, -1, out _G);
            LuaAPI.lua_pop(L, 1);

            errorFuncRef = LuaAPI.get_error_func_ref(L);

            if (initers != null)
            {
                for (int i = 0; i < initers.Count; i++)
                {
                    initers[i](this, translator);
                }
            }

            translator.CreateArrayMetatable(L);
            translator.CreateDelegateMetatable(L);
#if THREAD_SAFT || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 29
0
        public LuaEnv()
        {
            if (LuaAPI.xlua_get_lib_version() != LIB_VERSION_EXPECT)
            {
                throw new InvalidProgramException("wrong lib version expect:"
                                                  + LIB_VERSION_EXPECT + " but got:" + LuaAPI.xlua_get_lib_version());
            }

#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnvLock)
            {
#endif
            LuaIndexes.LUA_REGISTRYINDEX = LuaAPI.xlua_get_registry_index();
#if GEN_CODE_MINIMIZE
            LuaAPI.xlua_set_csharp_wrapper_caller(InternalGlobals.CSharpWrapperCallerPtr);
#endif
            // Create State
            rawL = LuaAPI.luaL_newstate();

            //Init Base Libs
            LuaAPI.luaopen_xlua(rawL);
            LuaAPI.luaopen_i64lib(rawL);

            translator = new ObjectTranslator(this, rawL);
            translator.createFunctionMetatable(rawL);
            translator.OpenLib(rawL);
            ObjectTranslatorPool.Instance.Add(rawL, translator);

            LuaAPI.lua_atpanic(rawL, StaticLuaCallbacks.Panic);

#if !XLUA_GENERAL
            LuaAPI.lua_pushstdcallcfunction(rawL, StaticLuaCallbacks.Print);
            if (0 != LuaAPI.xlua_setglobal(rawL, "print"))
            {
                throw new Exception("call xlua_setglobal fail!");
            }
#endif

            //template engine lib register
            TemplateEngine.LuaTemplate.OpenLib(rawL);

            AddSearcher(StaticLuaCallbacks.LoadBuiltinLib, 2); // just after the preload searcher
            AddSearcher(StaticLuaCallbacks.LoadFromCustomLoaders, 3);
#if !XLUA_GENERAL
            AddSearcher(StaticLuaCallbacks.LoadFromResource, 4);
            AddSearcher(StaticLuaCallbacks.LoadFromStreamingAssetsPath, -1);
#endif
            DoString(init_xlua, "Init");
            init_xlua = null;

#if !UNITY_SWITCH || UNITY_EDITOR
            AddBuildin("socket.core", StaticLuaCallbacks.LoadSocketCore);
            AddBuildin("socket", StaticLuaCallbacks.LoadSocketCore);
#endif

            AddBuildin("CS", StaticLuaCallbacks.LoadCS);
            AddBuildin("pb", XLua.LuaDLL.Lua.LoadPB);

            LuaAPI.lua_newtable(rawL); //metatable of indexs and newindexs functions
            LuaAPI.xlua_pushasciistring(rawL, "__index");
            LuaAPI.lua_pushstdcallcfunction(rawL, StaticLuaCallbacks.MetaFuncIndex);
            LuaAPI.lua_rawset(rawL, -3);

            LuaAPI.xlua_pushasciistring(rawL, Utils.LuaIndexsFieldName);
            LuaAPI.lua_newtable(rawL);
            LuaAPI.lua_pushvalue(rawL, -3);
            LuaAPI.lua_setmetatable(rawL, -2);
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(rawL, Utils.LuaNewIndexsFieldName);
            LuaAPI.lua_newtable(rawL);
            LuaAPI.lua_pushvalue(rawL, -3);
            LuaAPI.lua_setmetatable(rawL, -2);
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(rawL, Utils.LuaClassIndexsFieldName);
            LuaAPI.lua_newtable(rawL);
            LuaAPI.lua_pushvalue(rawL, -3);
            LuaAPI.lua_setmetatable(rawL, -2);
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(rawL, Utils.LuaClassNewIndexsFieldName);
            LuaAPI.lua_newtable(rawL);
            LuaAPI.lua_pushvalue(rawL, -3);
            LuaAPI.lua_setmetatable(rawL, -2);
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.lua_pop(rawL, 1); // pop metatable of indexs and newindexs functions

            LuaAPI.xlua_pushasciistring(rawL, MAIN_SHREAD);
            LuaAPI.lua_pushthread(rawL);
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

            LuaAPI.xlua_pushasciistring(rawL, CSHARP_NAMESPACE);
            if (0 != LuaAPI.xlua_getglobal(rawL, "CS"))
            {
                throw new Exception("get CS fail!");
            }
            LuaAPI.lua_rawset(rawL, LuaIndexes.LUA_REGISTRYINDEX);

#if !XLUA_GENERAL && (!UNITY_WSA || UNITY_EDITOR)
            translator.Alias(typeof(Type), "System.MonoType");
#endif

            if (0 != LuaAPI.xlua_getglobal(rawL, "_G"))
            {
                throw new Exception("get _G fail!");
            }
            translator.Get(rawL, -1, out _G);
            LuaAPI.lua_pop(rawL, 1);

            errorFuncRef = LuaAPI.get_error_func_ref(rawL);

            if (initers != null)
            {
                for (int i = 0; i < initers.Count; i++)
                {
                    initers[i](this, translator);
                }
            }

            translator.CreateArrayMetatable(rawL);
            translator.CreateDelegateMetatable(rawL);
            translator.CreateEnumerablePairs(rawL);
#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 30
0
        public static void EndObjectRegister(Type type, RealStatePtr L, ObjectTranslator translator, LuaCSFunction csIndexer,
                                             LuaCSFunction csNewIndexer, Type base_type, LuaCSFunction arrayIndexer, LuaCSFunction arrayNewIndexer)
        {
            int top        = LuaAPI.lua_gettop(L);
            int meta_idx   = abs_idx(top, OBJ_META_IDX);
            int method_idx = abs_idx(top, METHOD_IDX);
            int getter_idx = abs_idx(top, GETTER_IDX);
            int setter_idx = abs_idx(top, SETTER_IDX);

            //begin index gen
            LuaAPI.xlua_pushasciistring(L, "__index");
            LuaAPI.lua_pushvalue(L, method_idx);
            LuaAPI.lua_pushvalue(L, getter_idx);

            if (csIndexer == null)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_pushstdcallcfunction(L, csIndexer);
            }

            translator.Push(L, type == null ? base_type : type.BaseType);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
            if (arrayIndexer == null)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_pushstdcallcfunction(L, arrayIndexer);
            }

            LuaAPI.gen_obj_indexer(L);

            if (type != null)
            {
                LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName);
                LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua indexs function tables
                translator.Push(L, type);
                LuaAPI.lua_pushvalue(L, -3);
                LuaAPI.lua_rawset(L, -3);
                LuaAPI.lua_pop(L, 1);
            }

            LuaAPI.lua_rawset(L, meta_idx);
            //end index gen

            //begin newindex gen
            LuaAPI.xlua_pushasciistring(L, "__newindex");
            LuaAPI.lua_pushvalue(L, setter_idx);

            if (csNewIndexer == null)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_pushstdcallcfunction(L, csNewIndexer);
            }

            translator.Push(L, type == null ? base_type : type.BaseType);

            LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName);
            LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);

            if (arrayNewIndexer == null)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_pushstdcallcfunction(L, arrayNewIndexer);
            }

            LuaAPI.gen_obj_newindexer(L);

            if (type != null)
            {
                LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName);
                LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua newindexs function tables
                translator.Push(L, type);
                LuaAPI.lua_pushvalue(L, -3);
                LuaAPI.lua_rawset(L, -3);
                LuaAPI.lua_pop(L, 1);
            }

            LuaAPI.lua_rawset(L, meta_idx);
            //end new index gen
            LuaAPI.lua_pop(L, 4);
        }