luaL_loadbuffer() public static method

public static luaL_loadbuffer ( IntPtr luaState, byte buff, int size, string name ) : int
luaState System.IntPtr
buff byte
size int
name string
return int
Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LuaFunction LoadString(string chunk, string name, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            if (LuaDLL.luaL_loadbuffer(L, chunk, Encoding.UTF8.GetByteCount(chunk), name) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            if (env != null)
            {
                env.push(L);
                LuaDLL.lua_setfenv(L, -2);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Example #2
0
        public static int loader(IntPtr L)
        {
            // Get script to load
            string fileName = String.Empty;

            fileName  = LuaDLL.lua_tostring(L, 1);
            fileName  = fileName.Replace('.', '/');
            fileName += ".lua";

            // Load with Unity3D resources
            byte[] text = Load(fileName);

            if (text == null)
            {
                return(0);
            }

            LuaDLL.luaL_loadbuffer(L, text, text.Length, fileName);

            return(1);
        }
Example #3
0
        /*
         * Excutes a Lua file and returns all the chunk's return
         * values in an array
         */
        public object[] DoFile(string fileName, LuaTable env)
        {
            LuaDLL.lua_pushstdcallcfunction(L, tracebackFunction);
            int oldTop = LuaDLL.lua_gettop(L);

            // Load with Unity3D resources
            TextAsset file = (TextAsset)Resources.Load(fileName);

            if (file == null)
            {
                ThrowExceptionFromError(oldTop);
            }

            if (LuaDLL.luaL_loadbuffer(L, file.bytes, file.bytes.Length, fileName) == 0)
            {
                if (env != null)
                {
                    env.push(L);
                    //LuaDLL.lua_setfenv(L, -1);
                    LuaDLL.lua_setfenv(L, -2);
                }

                if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0)
                {
                    object[] results = translator.popValues(L, oldTop);
                    LuaDLL.lua_pop(L, 1);
                    return(results);
                }
                else
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            else
            {
                ThrowExceptionFromError(oldTop);
            }

            return(null);            // Never reached - keeps compiler happy
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public LuaFunction LoadFile(string fileName)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            byte[] chunk = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);
                chunk = br.ReadBytes((int)fs.Length);
            }

            if (LuaDLL.luaL_loadbuffer(L, chunk, chunk.Length, fileName) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Example #5
0
        /*
         * Excutes a Lua chunk and returns all the chunk's return
         * values in an array
         */
        public object[] DoString(string chunk)
        {
            int oldTop = LuaDLL.lua_gettop(luaState);

            if (LuaDLL.luaL_loadbuffer(luaState, chunk, "chunk") == 0)
            {
                if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
                {
                    return(translator.popValues(luaState, oldTop));
                }
                else
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            else
            {
                ThrowExceptionFromError(oldTop);
            }

            return(null);            // Never reached - keeps compiler happy
        }
Example #6
0
        public object[] DoString(string chunk, string chunkName)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            byte[] data = Encoding.UTF8.GetBytes(chunk);
            if (LuaDLL.luaL_loadbuffer(L, data, data.Length, chunkName) == 0)
            {
                if (LuaDLL.lua_pcall(L, 0, -1, 0) == 0)
                {
                    return(LuaStatic.PopValues(L, oldTop));
                }
                else
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            else
            {
                ThrowExceptionFromError(oldTop);
            }
            return(null);            // Never reached - keeps compiler happy
        }
Example #7
0
        public LuaFunction LoadFile(string fileName)
        {
            int oldTop = LuaDLL.lua_gettop(this.L);

            byte[] array = null;
            string path  = Util.LuaPath(fileName);

            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                BinaryReader binaryReader = new BinaryReader(fileStream);
                array = binaryReader.ReadBytes((int)fileStream.Length);
                fileStream.Close();
            }
            if (LuaDLL.luaL_loadbuffer(this.L, array, array.Length, fileName) != 0)
            {
                this.ThrowExceptionFromError(oldTop);
            }
            LuaFunction function = this.translator.getFunction(this.L, -1);

            this.translator.popValues(this.L, oldTop);
            return(function);
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public LuaFunction LoadFile(string fileName)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            // Load with Unity3D resources
            //string text = LuaHelper.Load(fileName);


            string path = Util.LuaPath(fileName);

            byte[] bt = File.ReadAllBytes(path);

            /* modify by jarjin
             * using (FileStream fs = new FileStream(path, FileMode.Open))
             * {
             * BinaryReader br = new BinaryReader(fs);
             * bt = br.ReadBytes((int)fs.Length);
             * fs.Close();
             * }
             */


            //if( text == null )
            //{
            //    ThrowExceptionFromError(oldTop);
            //}

            if (LuaDLL.luaL_loadbuffer(L, bt, bt.Length, fileName) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Example #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public LuaFunction LoadFile(string fileName)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            // Load with Unity3D resources
            TextAsset file = (TextAsset)Resources.Load(fileName);

            if (file == null)
            {
                ThrowExceptionFromError(oldTop);
            }

            if (LuaDLL.luaL_loadbuffer(L, file.bytes, file.bytes.Length, fileName) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LuaFunction LoadString(string chunk, string name)
        {
            int oldTop = LuaDLL.lua_gettop(luaState);

            executing = true;
            try
            {
                // Somehow, on OS X and Linux, we need to use the UTF-8 byte count rather than the string length
                var length = enableLengthWorkaround ? System.Text.Encoding.UTF8.GetByteCount(chunk) : chunk.Length;

                if (LuaDLL.luaL_loadbuffer(luaState, chunk, length, name) != 0)
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            finally { executing = false; }

            LuaFunction result = translator.getFunction(luaState, -1);

            translator.popValues(luaState, oldTop);

            return(result);
        }
Example #11
0
        public static int loader(IntPtr L)
        {
            try
            {
                int    top      = LuaDLL.lua_gettop(L);
                string fileName = String.Empty;
                fileName  = LuaDLL.lua_tostring(L, 1);
                fileName  = fileName.Replace('.', '/');
                fileName += ".lua";

                string fullPath = Path.Combine(luaPath, fileName);
                if (File.Exists(fullPath))
                {
                    byte[] fileData = StreamingAssetsHelper.ReadAllBytes(fullPath);
                    if (LuaDLL.luaL_loadbuffer(L, fileData, fileData.Length, "@" + fileName) != LuaDLL.LUA_OK)
                    {
                        UnityEngine.Debug.LogWarning("lua2:" + LuaDLL.lua_tostring(L, -1));
                        throw new LuaException(L, LuaDLL.lua_tostring(L, -1));
                    }

                    LuaDLL.lua_pushvalue(L, 1);
                    if (!LuaExtend.PCall(L, 1, LuaDLL.LUA_MULTRET))
                    {
                        throw new LuaException(L, "require failed:" + fileName);                         //avoid redundancy error msg log, because PCall has log error msg
                    }
                    return(LuaDLL.lua_gettop(L) - top);
                }
                else
                {
                    throw new LuaException(L, "file is not found:" + fileName);
                }
            }
            catch (Exception e)
            {
                return(LuaDLL.wluaL_error(L, e));
            }
        }
Example #12
0
        /*
         * Excutes a Lua chunk and returns all the chunk's return
         * values in an array
         */
        public object[] DoString(string chunk)
        {
            int oldTop = LuaDLL.lua_gettop(luaState);

            //if(LuaDLL.luaL_loadbuffer(luaState,chunk,chunk.Length,"chunk")==0)
            //if (LuaDLL.luaL_loadstring(luaState, chunk) == 0)
            if (LuaDLL.luaL_loadbuffer(luaState, chunk, System.Text.Encoding.Default.GetByteCount(chunk), "chunk") == 0)
            {
                if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
                {
                    return(translator.popValues(luaState, oldTop));
                }
                else
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            else
            {
                ThrowExceptionFromError(oldTop);
            }

            return(null);            // Never reached - keeps compiler happy
        }
Example #13
0
        public object[] DoString(string chunk, string chunkName, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(this.L);

            byte[] bytes = Encoding.UTF8.GetBytes(chunk);
            if (LuaDLL.luaL_loadbuffer(this.L, bytes, bytes.Length, chunkName) == 0)
            {
                if (env != null)
                {
                    env.push(this.L);
                    LuaDLL.lua_setfenv(this.L, -2);
                }
                if (LuaDLL.lua_pcall(this.L, 0, -1, 0) == 0)
                {
                    return(this.translator.popValues(this.L, oldTop));
                }
                this.ThrowExceptionFromError(oldTop);
            }
            else
            {
                this.ThrowExceptionFromError(oldTop);
            }
            return(null);
        }
Example #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public LuaFunction LoadFile(string fileName)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            // Load with Unity3D resources
            //string text = LuaHelper.Load(fileName);

            // byte[] bt = null;
            // string path = Util.LuaPath(fileName);

            // using (FileStream fs = new FileStream(path, FileMode.Open))
            // {
            //     BinaryReader br = new BinaryReader(fs);
            //     bt = br.ReadBytes((int)fs.Length);
            //     fs.Close();
            // }
            byte[] bt  = ioo.ResourceManager.LoadLuaBytes(fileName);
            int    len = LuaStatic.getBytesLength(bt);


            //if( text == null )
            //{
            //    ThrowExceptionFromError(oldTop);
            //}

            if (LuaDLL.luaL_loadbuffer(L, bt, len, fileName) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Example #15
0
 public int LuaLoadBuffer(byte[] buff, int size, string name)
 {
     return(LuaDLL.luaL_loadbuffer(L, buff, size, name));
 }