Exemplo n.º 1
0
        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="arguments">参数列表</param>
        /// <param name="scriptController">脚本控制器</param>
        public LuaValue invoke(List <LuaValue> arguments, LuaScriptController scriptController)
        {
            int scriptControllerId = 0;

            if (scriptController != null)
            {
                scriptControllerId = scriptController.objectId;
            }

            IntPtr funcPtr   = IntPtr.Zero;
            IntPtr argsPtr   = IntPtr.Zero;
            IntPtr resultPtr = IntPtr.Zero;

            LuaObjectEncoder funcEncoder = new LuaObjectEncoder(_context);

            funcEncoder.writeObject(this);

            byte[] bytes = funcEncoder.bytes;
            funcPtr = Marshal.AllocHGlobal(bytes.Length);
            Marshal.Copy(bytes, 0, funcPtr, bytes.Length);

            if (arguments != null)
            {
                LuaObjectEncoder argEncoder = new LuaObjectEncoder(_context);
                argEncoder.writeInt32(arguments.Count);
                foreach (LuaValue value in arguments)
                {
                    argEncoder.writeObject(value);
                }

                bytes   = argEncoder.bytes;
                argsPtr = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, argsPtr, bytes.Length);
            }

            int size = NativeUtils.invokeLuaFunction(_context.objectId, funcPtr, argsPtr, scriptControllerId, out resultPtr);

            if (argsPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(argsPtr);
            }
            if (funcPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(funcPtr);
            }

            if (size > 0)
            {
                return(LuaObjectDecoder.DecodeObject(resultPtr, size, _context) as LuaValue);
            }

            return(new LuaValue());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解析Lua脚本
        /// </summary>
        /// <returns>返回值.</returns>
        /// <param name="script">脚本内容.</param>
        /// <param name="scriptController">脚本控制器.</param>
        public LuaValue evalScript(string script, LuaScriptController scriptController)
        {
            IntPtr resultPtr = IntPtr.Zero;

            int scriptControllerId = 0;

            if (scriptController != null)
            {
                scriptControllerId = scriptController.objectId;
            }

            int size = NativeUtils.evalScript(_nativeObjectId, script, scriptControllerId, out resultPtr);

            return(LuaObjectDecoder.DecodeObject(resultPtr, size, this) as LuaValue);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 执行线程
        /// </summary>
        /// <param name="handler">线程处理器.</param>
        /// <param name="arguments">参数列表.</param>
        /// <param name="scriptController">脚本控制器.</param>
        public void runThread(LuaFunction handler, List <LuaValue> arguments, LuaScriptController scriptController)
        {
            int scriptControllerId = 0;

            if (scriptController != null)
            {
                scriptControllerId = scriptController.objectId;
            }

            IntPtr funcPtr = IntPtr.Zero;
            IntPtr argsPtr = IntPtr.Zero;

            LuaObjectEncoder funcEncoder = new LuaObjectEncoder(this);

            funcEncoder.writeObject(handler);

            byte[] bytes = funcEncoder.bytes;
            funcPtr = Marshal.AllocHGlobal(bytes.Length);
            Marshal.Copy(bytes, 0, funcPtr, bytes.Length);

            if (arguments != null)
            {
                LuaObjectEncoder argEncoder = new LuaObjectEncoder(this);
                argEncoder.writeInt32(arguments.Count);
                foreach (LuaValue value in arguments)
                {
                    argEncoder.writeObject(value);
                }

                bytes   = argEncoder.bytes;
                argsPtr = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, argsPtr, bytes.Length);
            }

            NativeUtils.runThread(this.objectId, funcPtr, argsPtr, scriptControllerId);

            if (argsPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(argsPtr);
            }
            if (funcPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(funcPtr);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 从Lua脚本文件中解析Lua脚本
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="filePath">Lua脚本文件路径</param>
        /// <param name="scriptController">脚本控制器</param>
        public LuaValue evalScriptFromFile(string filePath, LuaScriptController scriptController)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            if (!filePath.StartsWith("/") || filePath.StartsWith(Application.streamingAssetsPath, true, null))
            {
                if (filePath.StartsWith(Application.streamingAssetsPath, true, null))
                {
                    filePath = filePath.Substring(Application.streamingAssetsPath.Length + 1);
                }

                //初始化lua的缓存目录
                setupLuaCacheDir();

                filePath = getLuaCacheFilePath(filePath);
            }
#elif UNITY_EDITOR_WIN
            Regex regex = new Regex("^[a-zA-Z]:/.*");
            if (!regex.IsMatch(filePath))
            {
                //Window下不带盘符的路径为相对路径,需要拼接streamingAssetsPath
                filePath = string.Format("{0}/{1}", Application.streamingAssetsPath, filePath);
            }
#else
            if (!filePath.StartsWith("/"))
            {
                filePath = string.Format("{0}/{1}", Application.streamingAssetsPath, filePath);
            }
#endif

            int scriptControllerId = 0;
            if (scriptController != null)
            {
                scriptControllerId = scriptController.objectId;
            }

            IntPtr   resultPtr;
            int      size     = NativeUtils.evalScriptFromFile(_nativeObjectId, filePath, scriptControllerId, out resultPtr);
            LuaValue retValue = LuaObjectDecoder.DecodeObject(resultPtr, size, this) as LuaValue;

            return(retValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 调用Lua方法
        /// </summary>
        /// <returns>返回值.</returns>
        /// <param name="methodName">方法名称.</param>
        /// <param name="arguments">参数列表</param>
        /// <param name="scriptController">脚本控制器</param>
        public LuaValue callMethod(string methodName, List <LuaValue> arguments, LuaScriptController scriptController)
        {
            int scriptControllerId = 0;

            if (scriptController != null)
            {
                scriptControllerId = scriptController.objectId;
            }

            IntPtr argsPtr   = IntPtr.Zero;
            IntPtr resultPtr = IntPtr.Zero;

            if (arguments != null)
            {
                LuaObjectEncoder encoder = new LuaObjectEncoder(this);
                encoder.writeInt32(arguments.Count);
                foreach (LuaValue value in arguments)
                {
                    encoder.writeObject(value);
                }

                byte[] bytes = encoder.bytes;
                argsPtr = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, argsPtr, bytes.Length);
            }

            int size = NativeUtils.callMethod(_nativeObjectId, methodName, argsPtr, scriptControllerId, out resultPtr);

            if (argsPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(argsPtr);
            }

            if (size > 0)
            {
                return(LuaObjectDecoder.DecodeObject(resultPtr, size, this) as LuaValue);
            }

            return(new LuaValue());
        }