Exemplo n.º 1
0
        public void Run(IServiceRunner runner)
        {
            this.luaState = LuaLib.LuaLNewState();
            LuaLib.LuaLOpenLibs(this.luaState);

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            this.panicCallback = new LuaNativeFunction(_PanicCallback);
            LuaLib.LuaAtPanic(this.luaState, this.panicCallback);

            // Inject print module with our own function
            // - mouguangyi
            this.printCallback = new LuaNativeFunction(_PrintCallback);
            LuaLib.LuaPushStdCallCFunction(luaState, this.printCallback);
            LuaLib.LuaSetGlobal(this.luaState, "print");

            // Inject dofile module with our own function
            // - mouguangyi
            this.dofileCallback = new LuaNativeFunction(_DoFileCallback);
            LuaLib.LuaPushStdCallCFunction(luaState, this.dofileCallback);
            LuaLib.LuaSetGlobal(this.luaState, "dofile");

            // Inject searcher module with our own function
            // - mouguangyi
            this.searcherCallback = new LuaNativeFunction(_SearcherCallback);
            LuaLib.LuaGetGlobal(this.luaState, "package");
            LuaLib.LuaGetField(this.luaState, -1, "loaders");
            LuaLib.LuaPushStdCallCFunction(this.luaState, this.searcherCallback);
            for (var i = (LuaLib.LuaObjLen(this.luaState, -2) + 1); i >= 2; --i)
            {
                LuaLib.LuaRawGetI(this.luaState, -2, i - 1);
                LuaLib.LuaRawSetI(this.luaState, -3, i);
            }
            LuaLib.LuaRawSetI(this.luaState, -2, 2);
            LuaLib.LuaPop(this.luaState, 2);

            // Create luaruntime metatable
            // - mouguangyi
            this.collectCallback = new LuaNativeFunction(LuaExecuter._CollectObject);
            LuaLib.LuaLNewMetatable(this.luaState, "luaruntime");
            LuaLib.LuaPushString(this.luaState, "__gc");
            LuaLib.LuaPushStdCallCFunction(this.luaState, this.collectCallback);
            LuaLib.LuaSetTable(this.luaState, -3);

            new ServiceTask("com.giant.service.assetmanager").Start().Continue(task =>
            {
                runner.Ready(_Terminate);
                return(null);
            });
        }