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);
            });
        }
Exemplo n.º 2
0
        public object[] Call(object function, params object[] args)
        {
            int oldTop = LuaLib.LuaGetTop(this.luaState);

            if (function is string)
            {
                var fullPath = function as string;
                var path     = fullPath.Split('.');
                LuaLib.LuaGetGlobal(this.luaState, path[0]);
                function = LuaExecuter._ParseLuaValue(luaState, -1);
                if (path.Length > 1)
                {
                    var remainingPath = new string[path.Length - 1];
                    Array.Copy(path, 1, remainingPath, 0, path.Length - 1);
                    function = _GetObject(remainingPath);
                }
            }

            LuaExecuter._PushObject(this.luaState, function);
            if (null != args)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    LuaExecuter._PushObject(this.luaState, args[i]);
                }
            }

            try {
                int error = LuaLib.LuaPCall(this.luaState, args.Length, -1, 0);
                if (error != 0)
                {
                    _ThrowExceptionFromError(this.luaState, oldTop);
                }
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(_PopValues(this.luaState, oldTop));
        }