Ejemplo n.º 1
0
        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.xlua_pushasciistring(L, "private_accessible");
            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaPrivateAccessible);
            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);
        }
Ejemplo n.º 2
0
        internal static int Print(RealStatePtr L)
        {
            // For each argument we'll 'tostring' it
            int    n = LuaAPI.lua_gettop(L);
            string s = String.Empty;

            LuaAPI.lua_getglobal(L, "tostring");

            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 */
            }
            UnityEngine.Debug.Log("LUA: " + s);
            return(0);
        }
Ejemplo n.º 3
0
        public static void LoadCSTable(RealStatePtr L, Type type)
        {
            int oldTop = LuaAPI.lua_gettop(L);

            LuaAPI.lua_getglobal(L, "CS");

            List <string> path = getPathOfType(type);

            for (int i = 0; i < path.Count; ++i)
            {
                LuaAPI.xlua_pushasciistring(L, path[i]);
                if (0 != LuaAPI.xlua_pgettable(L, -2))
                {
                    LuaAPI.lua_settop(L, oldTop);
                    LuaAPI.lua_pushnil(L);
                    return;
                }
                if (!LuaAPI.lua_istable(L, -1) && i < path.Count - 1)
                {
                    LuaAPI.lua_settop(L, oldTop);
                    LuaAPI.lua_pushnil(L);
                    return;
                }
                LuaAPI.lua_remove(L, -2);
            }
        }
Ejemplo n.º 4
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);
            LuaAPI.lua_getglobal(L, "CS");

            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);
        }
Ejemplo n.º 5
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);
            LuaAPI.lua_setglobal(L, "print");

            //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");

            LuaAPI.lua_getglobal(L, "_G");
            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
        }