Example #1
0
        private static int _DoFileCallback(LuaState luaState)
        {
            // Use asset manager to replace original dofile process.
            // - mouguangyi
            try {
                var fileName = LuaLib.LuaToString(luaState, 1);
                using (var asset = ServiceCenter.GetService <IAssetManager>().Load(fileName + ".txt", AssetType.BYTES)) {
                    var chunk = asset.Cast <byte[]>();
                    if (null == chunk)
                    {
                        throw new Exception("Can't load lua script: " + fileName);
                    }
                    else
                    {
                        int oldTop = LuaLib.LuaGetTop(luaState);
                        if (0 == LuaLib.LuaLLoadBuffer(luaState, chunk, fileName))
                        {
                            if (0 != LuaLib.LuaPCall(luaState, 0, -1, 0))
                            {
                                _ThrowExceptionFromError(luaState, oldTop);
                            }
                        }
                        else
                        {
                            _ThrowExceptionFromError(luaState, oldTop);
                        }
                    }
                }
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(0);
        }
Example #2
0
        public object[] DoFile(string fileName)
        {
            // Use asset manager to replace original load file process.
            // - mouguangyi
            try {
                using (var asset = ServiceCenter.GetService <IAssetManager>().Load(fileName + ".txt", AssetType.BYTES)) {
                    var chunk = asset.Cast <byte[]>();
                    if (null == chunk)
                    {
                        throw new Exception("Can't load lua script: " + fileName);
                    }
                    else
                    {
                        int oldTop = LuaLib.LuaGetTop(this.luaState);
                        if (0 == LuaLib.LuaLLoadBuffer(this.luaState, chunk, fileName))
                        {
                            if (0 == LuaLib.LuaPCall(this.luaState, 0, -1, 0))
                            {
                                return(_PopValues(this.luaState, oldTop));
                            }
                        }

                        _ThrowExceptionFromError(this.luaState, oldTop);
                    }
                }
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(null);
        }
Example #3
0
        public object[] DoString(byte[] chunk)
        {
            try {
                int oldTop = LuaLib.LuaGetTop(this.luaState);
                if (LuaLib.LuaLLoadBuffer(this.luaState, chunk, "chunkName") == 0)
                {
                    if (0 == LuaLib.LuaPCall(this.luaState, 0, -1, 0))
                    {
                        return(_PopValues(this.luaState, oldTop));
                    }
                }

                _ThrowExceptionFromError(this.luaState, oldTop);
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(null);
        }
Example #4
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));
        }