Пример #1
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            encoder.writeInt64(_objRef.referenceId);
            encoder.writeString(luaObjectId);
            encoder.writeInt32(_nativeTypeId);

            //写入自定义数据
            encoder.writeInt32(1);
            encoder.writeString("NativeClass");
            encoder.writeString(this.obj.GetType().FullName);
        }
Пример #2
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());
        }
Пример #3
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            encoder.writeInt32(_context.objectId);
            encoder.writeString(luaObjectId);
        }
Пример #4
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            encoder.writeInt64(_objRef.referenceId);
            encoder.writeString(luaObjectId);

            //写入本地类型标识
            int nativeTypeId = encoder.context.exportsTypemanager.getNativeTypeId(_objRef.target.GetType());

            encoder.writeInt32(nativeTypeId);

            //写入自定义数据
            encoder.writeInt32(1);
            encoder.writeString("NativeClass");
            encoder.writeString(this.obj.GetType().FullName);
        }
Пример #5
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            encoder.writeInt32(_returnValues.Count);
            foreach (LuaValue value in _returnValues)
            {
                encoder.writeObject(value);
            }
        }
Пример #6
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());
        }
Пример #7
0
        /// <summary>
        /// 注册模块
        /// </summary>
        /// <param name="context">Lua上下文.</param>
        /// <param name="moduleName">模块名称.</param>
        /// <param name="t">类型.</param>
        protected static void _register(LuaContext context, string moduleName, Type t)
        {
            //初始化模块导出信息
            Dictionary <string, MethodInfo> exportMethods = new Dictionary <string, MethodInfo> ();
            List <string> exportMethodNames = new List <string> ();

            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo m in methods)
            {
                if (m.IsStatic && m.IsPublic)
                {
                    //静态和公开的方法会导出到Lua
                    exportMethodNames.Add(m.Name);
                    exportMethods.Add(m.Name, m);
                }
            }

            IntPtr exportMethodNamesPtr = IntPtr.Zero;

            if (exportMethodNames.Count > 0)
            {
                LuaObjectEncoder encoder = new LuaObjectEncoder();
                encoder.writeInt32(exportMethodNames.Count);
                foreach (string name in exportMethodNames)
                {
                    encoder.writeString(name);
                }

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

            if (_moduleMethodHandleDelegate == null)
            {
                _moduleMethodHandleDelegate = new LuaModuleMethodHandleDelegate(luaModuleMethodRoute);
            }
            IntPtr fp = Marshal.GetFunctionPointerForDelegate(_moduleMethodHandleDelegate);

            //注册模块
            int moduleId = NativeUtils.registerModule(context.objectId, moduleName, exportMethodNamesPtr, fp);

            //关联注册模块的注册方法
            _exportsModuleMethods.Add(moduleId, exportMethods);

            if (exportMethodNamesPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(exportMethodNamesPtr);
            }
        }
Пример #8
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);
            }
        }
Пример #9
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            if (_context != null)
            {
                encoder.writeInt32(_context.objectId);
            }
            else
            {
                encoder.writeInt32(0);
            }

            encoder.writeString(_tableId);
            encoder.writeInt16((Int16)type);

            switch (type)
            {
            case LuaValueType.Number:
            {
                encoder.writeDouble(toNumber());
                break;
            }

            case LuaValueType.Integer:
            {
                encoder.writeInt32(toInteger());
                break;
            }

            case LuaValueType.String:
            {
                encoder.writeString(toString());
                break;
            }

            case LuaValueType.Data:
            {
                encoder.writeBytes(toData());
                break;
            }

            case LuaValueType.Array:
            {
                List <LuaValue> list = toArray();

                encoder.writeInt32(list.Count);
                foreach (LuaValue value in list)
                {
                    encoder.writeObject(value);
                }
                break;
            }

            case LuaValueType.Map:
            {
                Dictionary <string, LuaValue> map = toMap();
                encoder.writeInt32(map.Count);

                foreach (KeyValuePair <string, LuaValue> kv in map)
                {
                    encoder.writeString(kv.Key);
                    encoder.writeObject(kv.Value);
                }
                break;
            }

            case LuaValueType.Object:
            {
                encoder.writeObject(_value);
                break;
            }

            case LuaValueType.Boolean:
            {
                encoder.writeByte(Convert.ToByte(toBoolean()));
                break;
            }

            case LuaValueType.Function:
            {
                encoder.writeObject(toFunction());
                break;
            }

            case LuaValueType.Ptr:
            {
                encoder.writeObject(toPointer());
                break;
            }

            case LuaValueType.Tuple:
            {
                encoder.writeObject(toTuple());
                break;
            }

            default:
                break;
            }
        }
Пример #10
0
        /// <summary>
        /// 导出类型
        /// </summary>
        /// <param name="type">类型.</param>
        /// <param name="context">上下文对象.</param>
        public void exportType(Type t, LuaContext context)
        {
            LuaExportTypeAnnotation typeAnnotation     = Attribute.GetCustomAttribute(t, typeof(LuaExportTypeAnnotation), false) as LuaExportTypeAnnotation;
            LuaExportTypeAnnotation baseTypeAnnotation = Attribute.GetCustomAttribute(t.BaseType, typeof(LuaExportTypeAnnotation), false) as LuaExportTypeAnnotation;

            //获取导出的类/实例方法
            Dictionary <string, MethodInfo> exportClassMethods = new Dictionary <string, MethodInfo> ();
            List <string> exportClassMethodNames = new List <string> ();

            Dictionary <string, MethodInfo> exportInstanceMethods = new Dictionary <string, MethodInfo> ();
            List <string> exportInstanceMethodNames = new List <string> ();

            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo m in methods)
            {
                if (m.IsPublic || (m.IsStatic && m.IsPublic))
                {
                    StringBuilder methodSignature = new StringBuilder();
                    foreach (ParameterInfo paramInfo in m.GetParameters())
                    {
                        Type paramType = paramInfo.ParameterType;
                        if (baseTypesMapping.ContainsKey(paramType))
                        {
                            methodSignature.Append(baseTypesMapping [paramType]);
                        }
                        else
                        {
                            methodSignature.Append("@");
                        }
                    }

                    string methodName = string.Format("{0}_{1}", m.Name, methodSignature.ToString());

                    if (m.IsStatic && m.IsPublic)
                    {
                        if (typeAnnotation != null &&
                            typeAnnotation.excludeExportClassMethodNames != null &&
                            Array.IndexOf(typeAnnotation.excludeExportClassMethodNames, m.Name) >= 0)
                        {
                            //为过滤方法
                            continue;
                        }

                        //静态和公开的方法会导出到Lua
                        exportClassMethodNames.Add(methodName);
                        exportClassMethods.Add(methodName, m);
                    }
                    else if (m.IsPublic)
                    {
                        if (typeAnnotation != null &&
                            typeAnnotation.excludeExportInstanceMethodNames != null &&
                            Array.IndexOf(typeAnnotation.excludeExportInstanceMethodNames, m.Name) >= 0)
                        {
                            //为过滤方法
                            continue;
                        }

                        //实例方法
                        exportInstanceMethodNames.Add(methodName);
                        exportInstanceMethods.Add(methodName, m);
                    }
                }
            }

            //获取导出的字段
            Dictionary <string, PropertyInfo> exportFields = new Dictionary <string, PropertyInfo> ();
            List <string> exportPropertyNames = new List <string> ();

            PropertyInfo[] propertys = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo p in propertys)
            {
                if (typeAnnotation != null &&
                    typeAnnotation.excludeExportPropertyNames != null &&
                    Array.IndexOf(typeAnnotation.excludeExportPropertyNames, p.Name) >= 0)
                {
                    //在过滤列表中
                    continue;
                }

                StringBuilder actStringBuilder = new StringBuilder();
                if (p.CanRead)
                {
                    actStringBuilder.Append("r");
                }
                if (p.CanWrite)
                {
                    actStringBuilder.Append("w");
                }

                exportPropertyNames.Add(string.Format("{0}_{1}", p.Name, actStringBuilder.ToString()));
                exportFields.Add(p.Name, p);
            }

            //创建导出的字段数据
            IntPtr exportPropertyNamesPtr = IntPtr.Zero;

            if (exportPropertyNames.Count > 0)
            {
                LuaObjectEncoder fieldEncoder = new LuaObjectEncoder(context);
                fieldEncoder.writeInt32(exportPropertyNames.Count);
                foreach (string name in exportPropertyNames)
                {
                    fieldEncoder.writeString(name);
                }

                byte[] fieldNameBytes = fieldEncoder.bytes;
                exportPropertyNamesPtr = Marshal.AllocHGlobal(fieldNameBytes.Length);
                Marshal.Copy(fieldNameBytes, 0, exportPropertyNamesPtr, fieldNameBytes.Length);
            }


            //创建导出的实例方法数据
            IntPtr exportInstanceMethodNamesPtr = IntPtr.Zero;

            if (exportInstanceMethodNames.Count > 0)
            {
                LuaObjectEncoder instanceMethodEncoder = new LuaObjectEncoder(context);
                instanceMethodEncoder.writeInt32(exportInstanceMethodNames.Count);
                foreach (string name in exportInstanceMethodNames)
                {
                    instanceMethodEncoder.writeString(name);
                }

                byte[] instMethodNameBytes = instanceMethodEncoder.bytes;
                exportInstanceMethodNamesPtr = Marshal.AllocHGlobal(instMethodNameBytes.Length);
                Marshal.Copy(instMethodNameBytes, 0, exportInstanceMethodNamesPtr, instMethodNameBytes.Length);
            }


            //创建导出类方法数据
            IntPtr exportClassMethodNamesPtr = IntPtr.Zero;

            if (exportClassMethodNames.Count > 0)
            {
                LuaObjectEncoder classMethodEncoder = new LuaObjectEncoder(context);
                classMethodEncoder.writeInt32(exportClassMethodNames.Count);
                foreach (string name in exportClassMethodNames)
                {
                    classMethodEncoder.writeString(name);
                }

                byte[] classMethodNameBytes = classMethodEncoder.bytes;
                exportClassMethodNamesPtr = Marshal.AllocHGlobal(classMethodNameBytes.Length);
                Marshal.Copy(classMethodNameBytes, 0, exportClassMethodNamesPtr, classMethodNameBytes.Length);
            }

            //创建实例方法
            if (_createInstanceDelegate == null)
            {
                _createInstanceDelegate = new LuaInstanceCreateHandleDelegate(_createInstance);
            }

            //销毁实例方法
            if (_destroyInstanceDelegate == null)
            {
                _destroyInstanceDelegate = new LuaInstanceDestroyHandleDelegate(_destroyInstance);
            }

            //类描述
            if (_instanceDescriptionDelegate == null)
            {
                _instanceDescriptionDelegate = new LuaInstanceDescriptionHandleDelegate(_instanceDescription);
            }

            //字段获取器
            if (_fieldGetterDelegate == null)
            {
                _fieldGetterDelegate = new LuaInstanceFieldGetterHandleDelegate(_fieldGetter);
            }

            //字段设置器
            if (_fieldSetterDelegate == null)
            {
                _fieldSetterDelegate = new LuaInstanceFieldSetterHandleDelegate(_fieldSetter);
            }

            //实例方法处理器
            if (_instanceMethodHandlerDelegate == null)
            {
                _instanceMethodHandlerDelegate = new LuaInstanceMethodHandleDelegate(_instanceMethodHandler);
            }

            //类方法处理器
            if (_classMethodHandleDelegate == null)
            {
                _classMethodHandleDelegate = new LuaModuleMethodHandleDelegate(_classMethodHandler);
            }

            string typeName = t.Name;

            if (typeAnnotation != null && typeAnnotation.typeName != null)
            {
                typeName = typeAnnotation.typeName;
            }

            string baseTypeName = t.BaseType.Name;

            if (baseTypeAnnotation != null && baseTypeAnnotation.typeName != null)
            {
                baseTypeName = baseTypeAnnotation.typeName;
            }

            int typeId = NativeUtils.registerType(
                context.objectId,
                typeName,
                baseTypeName,
                exportPropertyNamesPtr,
                exportInstanceMethodNamesPtr,
                exportClassMethodNamesPtr,
                Marshal.GetFunctionPointerForDelegate(_createInstanceDelegate),
                Marshal.GetFunctionPointerForDelegate(_destroyInstanceDelegate),
                Marshal.GetFunctionPointerForDelegate(_instanceDescriptionDelegate),
                Marshal.GetFunctionPointerForDelegate(_fieldGetterDelegate),
                Marshal.GetFunctionPointerForDelegate(_fieldSetterDelegate),
                Marshal.GetFunctionPointerForDelegate(_instanceMethodHandlerDelegate),
                Marshal.GetFunctionPointerForDelegate(_classMethodHandleDelegate));

            //关联注册模块的注册方法
            _exportsClass[typeId]           = t;
            _exportsClassIdMapping [t]      = typeId;
            _exportsClassMethods[typeId]    = exportClassMethods;
            _exportsInstanceMethods[typeId] = exportInstanceMethods;
            _exportsFields[typeId]          = exportFields;

            if (exportPropertyNamesPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(exportPropertyNamesPtr);
            }
            if (exportInstanceMethodNamesPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(exportInstanceMethodNamesPtr);
            }
            if (exportClassMethodNamesPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(exportClassMethodNamesPtr);
            }
        }
Пример #11
0
 /// <summary>
 /// 序列化对象
 /// </summary>
 /// <param name="encoder">对象编码器.</param>
 public virtual void serialization(LuaObjectEncoder encoder)
 {
     encoder.writeInt32(_nativeObjectId);
 }