예제 #1
0
        /// <summary>
        /// Unpack and override target from packet
        /// </summary>
        /// <typeparam name="T">unpackable type</typeparam>
        public static T Unpack <T>(GSFPacket packet)
        {
            Type          type   = typeof(T);
            PacketUtility packer = GetUtil(type);

            return(packer.unpack <T>(packet));
        }
예제 #2
0
        /// <summary>
        /// Unpack and override target from packet
        /// </summary>
        /// <typeparam name="T">unpackable type</typeparam>
        /// <param name="target">unpackable target</param>
        public static void Unpack <T>(ref T target, GSFPacket packet)
        {
            Type          type   = typeof(T);
            PacketUtility packer = GetUtil(type);

            packer.unpack(ref target, packet);
        }
예제 #3
0
        /// <summary>
        /// Pack object with unknown type
        /// </summary>
        public static GSFPacket Pack(object obj)
        {
            Type          type   = obj.GetType();
            PacketUtility packer = GetUtil(type);

            return(packer.pack(obj));
        }
예제 #4
0
        /// <summary>
        /// Pack object with assigned type
        /// </summary>
        public static GSFPacket Pack <T>(T obj)
        {
            Type          type   = typeof(T);
            PacketUtility packer = GetUtil(type);

            return(packer.pack(obj));
        }
        public override GSFPacket Pack(object obj)
        {
            Type          type = obj.GetType();
            PacketUtility util = GetUtil(type);

            return(util.pack(obj));
        }
예제 #6
0
        public MemberUtility(byte memberID, MemberInfo memberInfo)
        {
            this.memberID   = memberID;
            this.memberInfo = memberInfo;
            isField         = memberInfo.MemberType == MemberTypes.Field;

            Type memType = GetMemberType();

            proxy = GetUtil(memType);
        }
예제 #7
0
 protected static void CacheUtil(PacketUtility util, bool isDefault)
 {
     if (defaults == null && others == null)
     {
         initUtilities();
     }
     if (isDefault)
     {
         defaults.Add(util.classID, util);
     }
     if (util.classType != null)
     {
         others.Add(util.classType, util);
     }
 }
        public override GSFPacket pack(object obj)
        {
            Type          type        = obj.GetType();
            Type          elementType = type.GetElementType();
            TypeInfo      typeInfo    = PackType(type);
            PacketUtility util        = GetUtil(elementType);
            int           length      = (int)type.GetProperty("Length").GetValue(obj);
            MethodInfo    getMethod   = type.GetMethod("Get");

            GSFPacket[] values = new GSFPacket[length];
            for (int i = 0; i < length; i++)
            {
                values[i] = util.pack(getMethod.Invoke(obj, new object[] { i }));
            }
            return(new GSFPacket(classID, new object[] { typeInfo, values }));
        }
        public override object unpack(GSFPacket packet)
        {
            object[] data = (object[])packet.data;
            Type     type = UnpackType((TypeInfo)data[0]);

            GSFPacket[] values = (GSFPacket[])data[1];
            // get utility of element type
            PacketUtility util      = GetUtil(type.GetElementType());
            object        array     = Activator.CreateInstance(type, new object[] { values.Length });
            MethodInfo    setMethod = type.GetMethod("Set");

            for (int i = 0; i < values.Length; i++)
            {
                setMethod.Invoke(array, new object[] { i, util.unpack(values[i]) });
            }
            return(array);
        }
        public override object unpack(GSFPacket packet, Type type)
        {
            object[]      data    = (object[])packet.data;
            Type          keyType = type.GetGenericArguments()[0];
            Type          valType = type.GetGenericArguments()[1];
            PacketUtility keyUtil = GetUtil(keyType);
            PacketUtility valUtil = GetUtil(valType);
            object        dict    = Activator.CreateInstance(type);

            object[]   values    = (object[])data[1];
            MethodInfo addMethod = type.GetMethod("Add");

            for (int i = 0; i < values.Length; i += 2)
            {
                addMethod.Invoke(dict, new object[] { keyUtil.unpack((GSFPacket)values[i]), valUtil.unpack((GSFPacket)values[i + 1]) });
            }
            return(dict);
        }
        public override void unpack(ref object target, GSFPacket packet)
        {
            object[]      data     = (object[])packet.data;
            Type          dictType = target.GetType();
            Type          keyType  = dictType.GetGenericArguments()[0];
            Type          valType  = dictType.GetGenericArguments()[1];
            PacketUtility keyUtil  = GetUtil(keyType);
            PacketUtility valUtil  = GetUtil(valType);

            object[]   values      = (object[])data[1];
            MethodInfo clearMethod = dictType.GetMethod("Clear");

            clearMethod.Invoke(target, new object[0]);
            MethodInfo addMethod = dictType.GetMethod("Add");

            for (int i = 0; i < values.Length; i += 2)
            {
                addMethod.Invoke(target, new object[] { keyUtil.unpack((GSFPacket)values[i], keyType), valUtil.unpack((GSFPacket)values[i + 1], valType) });
            }
        }
        public override GSFPacket pack(object dict)
        {
            Type dictType = dict.GetType();

            Type[]        genericArgs = dictType.GetGenericArguments();
            TypeInfo      typeArgs    = PackType(dictType);
            int           length      = (int)dictType.GetProperty("Count").GetValue(dict);
            Type          pairType    = typeof(KeyValuePair <,>).MakeGenericType(genericArgs);
            PropertyInfo  keyProp     = pairType.GetProperty("Key");
            PacketUtility keyUtil     = GetUtil(keyProp.PropertyType);
            PropertyInfo  valProp     = pairType.GetProperty("Value");
            PacketUtility valUtil     = GetUtil(valProp.PropertyType);
            IEnumerable   e           = (IEnumerable)dict;

            object[] values = new object[length * 2];
            int      p      = 0;

            foreach (var pair in e)
            {
                values[p++] = keyUtil.pack(keyProp.GetValue(pair));
                values[p++] = valUtil.pack(valProp.GetValue(pair));
            }
            return(new GSFPacket(100, new object[] { typeArgs, values }));
        }
예제 #13
0
        public virtual object unpack(GSFPacket packet, Type type)
        {
            PacketUtility util = GetUtil(type);

            return(util.unpack(packet));
        }
        public override object Unpack(GSFPacket packet)
        {
            PacketUtility util = GetUtil(packet.classID);

            return(util.unpack(packet));
        }