Esempio n. 1
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. 2
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
        }