Esempio n. 1
0
        private static IntPtr _classMethodHandler(int contextId, int classId, string methodName, IntPtr arguments, int size)
        {
            if (_exportsClassMethods.ContainsKey(classId) && _exportsClassMethods[classId].ContainsKey(methodName))
            {
                LuaContext context = LuaContext.getContext(contextId);
                //存在该方法, 进行调用
                MethodInfo m = _exportsClassMethods [classId][methodName];

                ArrayList argsArr  = parseMethodParameters(context, m, arguments, size);
                object    ret      = m.Invoke(null, argsArr != null ? argsArr.ToArray() : null);
                LuaValue  retValue = new LuaValue(ret);

                LuaObjectEncoder encoder = new LuaObjectEncoder(context);
                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. 2
0
        private static IntPtr _instanceMethodHandler(int contextId, int classId, Int64 instancePtr, string methodName, IntPtr argumentsBuffer, int bufferSize)
        {
            if (instancePtr != 0 &&
                _exportsInstanceMethods.ContainsKey(classId) &&
                _exportsInstanceMethods[classId].ContainsKey(methodName))
            {
                LuaContext         context  = LuaContext.getContext(contextId);
                LuaObjectReference objRef   = LuaObjectReference.findObject(instancePtr);
                object             instance = objRef.target;
                MethodInfo         m        = _exportsInstanceMethods[classId][methodName];
                if (instance != null && m != null)
                {
                    ArrayList argsArr = parseMethodParameters(context, m, argumentsBuffer, bufferSize);
                    object    ret     = m.Invoke(instance, argsArr != null ? argsArr.ToArray() : null);

                    LuaValue retValue = new LuaValue(ret);

                    LuaObjectEncoder encoder = new LuaObjectEncoder(context);
                    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. 3
0
        private static IntPtr _fieldGetter(int contextId, int classId, Int64 instancePtr, string fieldName)
        {
            IntPtr retValuePtr = IntPtr.Zero;

            if (instancePtr != 0 &&
                _exportsFields.ContainsKey(classId) &&
                _exportsFields[classId].ContainsKey(fieldName))
            {
                LuaContext         context      = LuaContext.getContext(contextId);
                LuaObjectReference objRef       = LuaObjectReference.findObject(instancePtr);
                object             instance     = objRef.target;
                PropertyInfo       propertyInfo = _exportsFields[classId][fieldName];
                if (instance != null && propertyInfo != null && propertyInfo.CanRead)
                {
                    object   retValue = propertyInfo.GetValue(instance, null);
                    LuaValue value    = new LuaValue(retValue);

                    LuaObjectEncoder encoder = new LuaObjectEncoder(context);
                    encoder.writeObject(value);
                    byte[] bytes = encoder.bytes;
                    retValuePtr = Marshal.AllocHGlobal(bytes.Length);
                    Marshal.Copy(bytes, 0, retValuePtr, bytes.Length);
                }
            }

            return(retValuePtr);
        }
        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. 5
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaFunction(LuaObjectDecoder decoder)
            : base(decoder)
        {
            int contextId = decoder.readInt32();

            _context    = LuaContext.getContext(contextId);
            luaObjectId = decoder.readString();
        }
Esempio n. 6
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;
            }
        }
        private static Int64 _createInstance(int contextId, int nativeClassId, IntPtr argumentsBuffer, int bufferSize)
        {
            Int64 refId = -1;

            if (_exportsClass.ContainsKey(nativeClassId))
            {
                Type t = _exportsClass [nativeClassId];
                if (t != null)
                {
                    LuaContext      context   = LuaContext.getContext(contextId);
                    List <LuaValue> arguments = getArgumentList(context, argumentsBuffer, bufferSize);

                    ConstructorInfo ci = getConstructor(t, t, arguments);
                    if (ci != null)
                    {
                        ArrayList       argsArr    = new ArrayList();
                        ParameterInfo[] parameters = ci.GetParameters();
                        if (parameters.Length > 0 && arguments != null)
                        {
                            int i = 0;
                            foreach (ParameterInfo p in parameters)
                            {
                                if (i >= arguments.Count)
                                {
                                    break;
                                }

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

                                i++;
                            }
                        }

                        object instance = ci.Invoke(argsArr.ToArray());
                        if (instance != null)
                        {
                            LuaObjectReference objRef = new LuaObjectReference(instance);
                            //添加引用避免被GC进行回收
                            _instances.Add(objRef);

                            refId = objRef.referenceId;
                        }
                    }
                }
            }

            return(refId);
        }
Esempio n. 8
0
        private static void exportsNativeType(int contextId, string typeName)
        {
            if (_assemblys == null)
            {
                //初始化名称空间
                _assemblys = new HashSet <Assembly> ();

                Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
                foreach (Assembly assembly in assemblys)
                {
                    bool   needAdd      = true;
                    string assemblyName = assembly.GetName().Name;
                    foreach (Regex regex in _excludeAssemblyNames)
                    {
                        if (regex.IsMatch(assemblyName))
                        {
                            needAdd = false;
                            break;
                        }
                    }

                    if (needAdd)
                    {
                        _assemblys.Add(assembly);
                    }
                }
            }

            LuaContext context = LuaContext.getContext(contextId);

            if (context != null)
            {
                //查找类型
                Type   targetType   = null;
                string fullTypeName = typeName.Replace("_", ".");
                foreach (Assembly assembly in _assemblys)
                {
                    targetType = Type.GetType(string.Format("{0},{1}", typeName, assembly.GetName().Name));
                    if (targetType == null)
                    {
                        targetType = Type.GetType(string.Format("{0},{1}", fullTypeName, assembly.GetName().Name));

                        if (targetType == null)
                        {
                            foreach (Type type in assembly.GetTypes())
                            {
                                if (type.Name == typeName)
                                {
                                    targetType = type;
                                    break;
                                }
                            }
                        }
                    }

                    if (targetType != null)
                    {
                        break;
                    }
                }

                if (targetType != null)
                {
                    //为导出类型
                    context.exportsNativeType(targetType);
                }
            }
        }