Esempio n. 1
0
        /// <summary>
        /// 解析Lua脚本
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="script">Lua脚本</param>
        public LuaValue evalScript(string script)
        {
            IntPtr resultPtr = IntPtr.Zero;
            int    size      = NativeUtils.evalScript(_nativeObjectId, script, out resultPtr);

            return(LuaObjectDecoder.DecodeObject(resultPtr, size) as LuaValue);
        }
Esempio n. 2
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaPointer(LuaObjectDecoder decoder)
            : base(decoder)
        {
            Int64 objRefId = decoder.readInt64();

            _objReference = LuaObjectReference.findObject(objRefId);
        }
Esempio n. 3
0
        /// <summary>
        /// 从Lua脚本文件中解析Lua脚本
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="filePath">Lua脚本文件路径</param>
        public LuaValue evalScriptFromFile(string filePath)
        {
#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
            IntPtr   resultPtr;
            int      size     = NativeUtils.evalScriptFromFile(_nativeObjectId, filePath, out resultPtr);
            LuaValue retValue = LuaObjectDecoder.DecodeObject(resultPtr, size) as LuaValue;

            return(retValue);
        }
Esempio n. 4
0
        /// <summary>
        /// 调用Lua方法
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="methodName">方法名</param>
        /// <param name="arguments">调用参数列表</param>
        public LuaValue callMethod(string methodName, List <LuaValue> arguments)
        {
            IntPtr argsPtr   = IntPtr.Zero;
            IntPtr resultPtr = IntPtr.Zero;

            if (arguments != null)
            {
                LuaObjectEncoder encoder = new LuaObjectEncoder();
                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, out resultPtr);

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

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

            return(new LuaValue());
        }
Esempio n. 5
0
        /// <summary>
        /// 设置对象
        /// </summary>
        /// <param name="keyPath">键名路径.</param>
        /// <param name="value">值.</param>
        public void setObject(String keyPath, object value)
        {
            if (_context != null && _tableId > 0 && type == LuaValueType.Map)
            {
                LuaValue objectValue = new LuaValue(value);

                IntPtr resultPtr = IntPtr.Zero;

                IntPtr           valuePtr = IntPtr.Zero;
                LuaObjectEncoder encoder  = new LuaObjectEncoder(_context);
                encoder.writeObject(objectValue);

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

                int bufferLen = NativeUtils.tableSetObject(_tableId, keyPath, valuePtr, resultPtr);

                if (bufferLen > 0)
                {
                    LuaValue resultValue = LuaObjectDecoder.DecodeObject(resultPtr, bufferLen, _context) as LuaValue;
                    _value = resultValue._value;
                }

                if (valuePtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(valuePtr);
                }
            }
        }
        private static void _fieldSetter(int contextId, int classId, Int64 instancePtr, string fieldName, IntPtr valueBuffer, int bufferSize)
        {
            if (instancePtr != 0)
            {
                LuaContext         context  = LuaContext.getContext(contextId);
                LuaObjectReference objRef   = LuaObjectReference.findObject(instancePtr);
                object             instance = objRef.target;
                LuaObjectDecoder   decoder  = new LuaObjectDecoder(valueBuffer, bufferSize, context);
                LuaValue           value    = decoder.readObject() as LuaValue;


                if (_exportsProperties.ContainsKey(classId) &&
                    _exportsProperties [classId].ContainsKey(fieldName))
                {
                    PropertyInfo propertyInfo = _exportsProperties [classId] [fieldName];
                    if (instance != null && propertyInfo != null && propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(instance, getNativeValueForLuaValue(propertyInfo.PropertyType, value), null);
                    }
                }
                else if (_exportsFields.ContainsKey(classId) &&
                         _exportsFields [classId].ContainsKey(fieldName))
                {
                    FieldInfo fieldInfo = _exportsFields [classId] [fieldName];
                    if (instance != null && fieldInfo != null)
                    {
                        fieldInfo.SetValue(instance, getNativeValueForLuaValue(fieldInfo.FieldType, value));
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaFunction(LuaObjectDecoder decoder)
            : base(decoder)
        {
            int contextId = decoder.readInt32();

            _context    = LuaContext.getContext(contextId);
            luaObjectId = decoder.readString();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaObjectDescriptor(LuaObjectDecoder decoder)
            : base(decoder)
        {
            Int64 objRefId = decoder.readInt64();

            _objRef = LuaObjectReference.findObject(objRefId);

            luaObjectId = decoder.readString();
        }
Esempio n. 9
0
        /// <summary>
        /// 初始化LuaValue对象
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaValue(LuaObjectDecoder decoder)
            : base(decoder)
        {
            int contextId = decoder.readInt32();

            _context = LuaContext.getContext(contextId);
            _tableId = decoder.readString();
            _type    = (LuaValueType)decoder.readInt16();
            _value   = null;

            switch (_type)
            {
            case LuaValueType.Integer:
                _value = decoder.readInt32();
                break;

            case LuaValueType.Boolean:
                _value = decoder.readByte();
                break;

            case LuaValueType.Number:
                _value = decoder.readDouble();
                break;

            case LuaValueType.Data:
                _value = decoder.readBytes();
                break;

            case LuaValueType.String:
                _value = decoder.readString();
                break;

            case LuaValueType.Array:
                _value = readArrayList(decoder);
                break;

            case LuaValueType.Map:
                _value = readHashtable(decoder);
                break;

            case LuaValueType.Function:
                _value = decoder.readObject() as LuaFunction;
                break;

            case LuaValueType.Ptr:
                _value = decoder.readObject() as LuaPointer;
                break;

            case LuaValueType.Tuple:
                _value = decoder.readObject() as LuaTuple;
                break;

            case LuaValueType.Object:
                _value = decoder.readObject();
                break;
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaObjectDescriptor(LuaObjectDecoder decoder)
            : base(decoder)
        {
            IntPtr ptr = new IntPtr(decoder.readInt64());

            _obj = Marshal.GetObjectForIUnknown(ptr);

            _luaObjectId = decoder.readString();
        }
Esempio n. 11
0
        /// <summary>
        /// 解码对象
        /// </summary>
        /// <returns>对象</returns>
        /// <param name="objectPtr">表示C中的二进制数据缓冲区指针</param>
        /// <param name="size">缓冲区大小</param>
        public static object DecodeObject(IntPtr objectPtr, int size)
        {
            if (size > 0)
            {
                LuaObjectDecoder decoder = new LuaObjectDecoder(objectPtr, size);
                return(decoder.readObject());
            }

            return(null);
        }
Esempio n. 12
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());
        }
Esempio n. 13
0
        /// <summary>
        /// 获取全局变量
        /// </summary>
        /// <returns>变量值.</returns>
        /// <param name="name">变量名称.</param>
        public LuaValue getGlobal(string name)
        {
            IntPtr valuePtr = IntPtr.Zero;
            int    size     = NativeUtils.getGlobal(_nativeObjectId, name, out valuePtr);

            if (valuePtr != IntPtr.Zero && size > 0)
            {
                LuaObjectDecoder decoder = new LuaObjectDecoder(valuePtr, size, this);
                return(decoder.readObject() as LuaValue);
            }

            return(new LuaValue());
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaTuple(LuaObjectDecoder decoder)
            : base(decoder)
        {
            int size = decoder.readInt32();

            if (size > 0)
            {
                for (int i = 0; i < size; i++)
                {
                    LuaValue item = decoder.readObject() as LuaValue;
                    if (item != null)
                    {
                        _returnValues.Add(item);
                    }
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// 初始化LuaValue对象
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaValue(LuaObjectDecoder decoder)
        {
            _type  = (LuaValueType)decoder.readInt16();
            _value = null;

            switch (_type)
            {
            case LuaValueType.Integer:
                _value = decoder.readInt32();
                break;

            case LuaValueType.Boolean:
                _value = decoder.readByte();
                break;

            case LuaValueType.Number:
                _value = decoder.readDouble();
                break;

            case LuaValueType.Data:
                _value = decoder.readBytes();
                break;

            case LuaValueType.String:
                _value = decoder.readString();
                break;

            case LuaValueType.Array:
                _value = readArrayList(decoder);
                break;

            case LuaValueType.Map:
                _value = readHashtable(decoder);
                break;

            case LuaValueType.Object:
            {
                _value = decoder.readObject();
                break;
            }
            }
        }
        /// <summary>
        /// 从原生编码对象中获取参数列表
        /// </summary>
        /// <returns>参数列表.</returns>
        /// <param name="context">上下文</param>
        /// <param name="arguments">参数缓存.</param>
        /// <param name="size">缓存长度.</param>
        protected static List <LuaValue> getArgumentList(LuaContext context, IntPtr arguments, int size)
        {
            List <LuaValue> argumentsList = null;

            if (arguments != IntPtr.Zero)
            {
                //反序列化参数列表
                LuaObjectDecoder decoder = new LuaObjectDecoder(arguments, size, context);
                int argSize = decoder.readInt32();

                argumentsList = new List <LuaValue> ();
                for (int i = 0; i < argSize; i++)
                {
                    LuaValue value = decoder.readObject() as LuaValue;
                    argumentsList.Add(value);
                }
            }

            return(argumentsList);
        }
Esempio n. 18
0
        /// <summary>
        /// 解析方法的参数列表
        /// </summary>
        /// <returns>参数列表</returns>
        /// <param name="context">上下文对象</param>
        /// <param name="m">方法信息</param>
        /// <param name="arguments">参数列表数据</param>
        /// <param name="size">参数列表数据长度</param>
        protected static ArrayList parseMethodParameters(LuaContext context, MethodInfo m, IntPtr arguments, int size)
        {
            List <LuaValue> argumentsList = null;

            if (arguments != IntPtr.Zero)
            {
                //反序列化参数列表
                LuaObjectDecoder decoder = new LuaObjectDecoder(arguments, size, context);
                int argSize = decoder.readInt32();

                argumentsList = new List <LuaValue> ();
                for (int i = 0; i < argSize; i++)
                {
                    LuaValue value = decoder.readObject() as LuaValue;
                    argumentsList.Add(value);
                }
            }

            ArrayList argsArr = null;

            ParameterInfo[] parameters = m.GetParameters();
            if (parameters.Length > 0 && argumentsList != null)
            {
                int i = 0;
                argsArr = new ArrayList();
                foreach (ParameterInfo p in parameters)
                {
                    if (i >= argumentsList.Count)
                    {
                        break;
                    }

                    object value = getNativeValueForLuaValue(p.ParameterType, argumentsList[i]);
                    argsArr.Add(value);

                    i++;
                }
            }

            return(argsArr);
        }
Esempio n. 19
0
        /// <summary>
        /// 读取一个数组列表
        /// </summary>
        /// <returns>数组列表</returns>
        /// <param name="decoder">对象解码器</param>
        private List <LuaValue> readArrayList(LuaObjectDecoder decoder)
        {
            int size = decoder.readInt32();

            if (size > 0)
            {
                List <LuaValue> list = new List <LuaValue> ();
                for (int i = 0; i < size; i++)
                {
                    LuaValue item = decoder.readObject() as LuaValue;
                    if (item != null)
                    {
                        list.Add(item);
                    }
                }

                return(list);
            }

            return(null);
        }
Esempio n. 20
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaObjectDescriptor(LuaObjectDecoder decoder)
            : base(decoder)
        {
            Int64 objRefId = decoder.readInt64();

            _objRef = LuaObjectReference.findObject(objRefId);

            luaObjectId = decoder.readString();

            //原生类型标识读取
            decoder.readInt32();

            //读取自定义数据
            int userdataSize = decoder.readInt32();

            for (int i = 0; i < userdataSize; i++)
            {
                decoder.readString();
                decoder.readString();
            }
        }
Esempio n. 21
0
        /// <summary>
        /// 读取一个字典
        /// </summary>
        /// <returns>字典对象</returns>
        /// <param name="decoder">对象解码器</param>
        private Dictionary <string, LuaValue> readHashtable(LuaObjectDecoder decoder)
        {
            int size = decoder.readInt32();

            if (size > 0)
            {
                Dictionary <string, LuaValue> table = new Dictionary <string, LuaValue> ();
                for (int i = 0; i < size; i++)
                {
                    string   key  = decoder.readString();
                    LuaValue item = decoder.readObject() as LuaValue;
                    if (key != null && item != null)
                    {
                        table.Add(key, item);
                    }
                }

                return(table);
            }

            return(null);
        }
Esempio n. 22
0
        /// <summary>
        /// 从Lua脚本文件中解析Lua脚本
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="filePath">Lua脚本文件路径</param>
        public LuaValue evalScriptFromFile(string filePath)
        {
                        #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);
            }
                        #else
            if (!filePath.StartsWith("/"))
            {
                filePath = string.Format("{0}/{1}", Application.streamingAssetsPath, filePath);
            }
                        #endif

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

//			#if UNITY_ANDROID && !UNITY_EDITOR
//
//			if (needRemoveFile && File.Exists(filePath))
//			{
//				File.Delete(filePath);
//			}
//
//			#endif

            return(retValue);
        }
Esempio n. 23
0
        /// <summary>
        /// Lua方法处理器
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="methodName">方法名称</param>
        /// <param name="arguments">参数列表</param>
        private IntPtr luaMethodHandler(string methodName, IntPtr args, int size)
        {
            if (_methodHandlers.ContainsKey(methodName))
            {
                //反序列化参数列表
                LuaObjectDecoder decoder = new LuaObjectDecoder(args, size);
                int argSize = decoder.readInt32();

                List <LuaValue> argumentsList = new List <LuaValue> ();
                for (int i = 0; i < argSize; i++)
                {
                    LuaValue value = decoder.readObject() as LuaValue;
                    argumentsList.Add(value);
                }

                LuaMethodHandler handler  = _methodHandlers [methodName];
                LuaValue         retValue = handler(argumentsList);

                if (retValue == null)
                {
                    retValue = new LuaValue();
                }

                LuaObjectEncoder encoder = new LuaObjectEncoder();
                encoder.writeObject(retValue);

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

                return(retPtr);
            }

            return(IntPtr.Zero);
        }
Esempio n. 24
0
 /// <summary>
 /// 初始化LuaBaseObject
 /// </summary>
 /// <param name="decoder">对象解码器</param>
 public LuaBaseObject(LuaObjectDecoder decoder)
 {
 }
Esempio n. 25
0
 /// <summary>
 /// 初始化LuaBaseObject
 /// </summary>
 /// <param name="decoder">对象解码器</param>
 public LuaBaseObject(LuaObjectDecoder decoder)
 {
     _nativeObjectId = decoder.readInt32();
 }