private MessagePackSerializer <T> GetDelegatedSerializer()
#endif
        {
            var result = this._delegated;

            if (result == null)
            {
#if UNITY_IOS
                result = this._context.GetSerializer(TargetType);
                if (result is LazyDelegatingMessagePackSerializer)
#else
                result = this._context.GetSerializer <T>();
                var TargetType = typeof(T);

                if (result is LazyDelegatingMessagePackSerializer <T> )
#endif
                {
                    throw new InvalidOperationException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  "MessagePack serializer for the type '{0}' is not constructed yet.",
                                  TargetType
                                  )
                              );
                }

                // Duplicated assignment is accepttable.
                this._delegated = result;
            }

            return(result);
        }
Exemple #2
0
        public void TestIMessagePackSingleObjectSerializer_PackSingleObject_ValueTypeButNull_Fail()
        {
            var target = CreateTarget <TimeSpan>();
            IMessagePackSingleObjectSerializer iface = target;

            Assert.Throws <ArgumentException>(() => iface.PackSingleObject(null));
        }
    protected override void OnSerializeBinaryModel(PacketMemoryStream stream, Coop_Model_Base model)
    {
        Type modelType = ((PACKET_TYPE)model.c).GetModelType();
        IMessagePackSingleObjectSerializer serializer = context.GetSerializer(modelType);

        serializer.Pack(stream, model);
    }
Exemple #4
0
        /// <summary>
        /// The unpack from message.
        /// </summary>
        /// <param name="unpacker">
        /// The unpacker.
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public void UnpackFromMessage(Unpacker unpacker)
        {
            // Read the type name
            this.TypeName = unpacker.LastReadData.AsString();

            // Read the data object as byte array
            byte[] temp;
            unpacker.ReadBinary(out temp);

            // Create a message serializer object
            Type type = Type.GetType(this.TypeName);

            if (type == null)
            {
                type = Assembly.GetCallingAssembly().GetType(this.TypeName);
                if (type == null)
                {
                    throw new ArgumentException(string.Format("Type '{0}' not found.", this.TypeName));
                }
            }

            IMessagePackSingleObjectSerializer ser = BaseMessage.serializers[type];

            // Unpack the message's data object
            this.dataObject = (BaseMessage)ser.UnpackSingleObject(temp);
        }
    protected override Coop_Model_Base OnDeserializeBinaryModel(PacketMemoryStream stream, Type type, CoopPacketHeader header)
    {
        Type modelType = ((PACKET_TYPE)header.packetType).GetModelType();
        IMessagePackSingleObjectSerializer serializer = context.GetSerializer(modelType);

        return((Coop_Model_Base)serializer.Unpack(stream));
    }
Exemple #6
0
        public void TestIMessagePackSingleObjectSerializer_PackSingleObject_InvalidType_Fail()
        {
            var target = CreateTarget <TimeSpan>();
            var value  = TimeSpan.FromTicks(12345);
            IMessagePackSingleObjectSerializer iface = target;

            Assert.Throws <ArgumentException>(() => iface.PackSingleObject(value.ToString()));
        }
        public static void PackCharArraySegmentTo(Packer packer, object obj, IMessagePackSingleObjectSerializer itemSerializer)
        {
            var objectTree = (ArraySegment <char>)obj;
#endif // !UNITY
            // TODO: More efficient
            packer.PackStringHeader(objectTree.Count);
            packer.PackRawBody(MessagePackConvert.EncodeString(new string( objectTree.Array.Skip(objectTree.Offset).Take(objectTree.Count).ToArray())));
        }
Exemple #8
0
        public void TestIMessagePackSingleObjectSerializer_PackSingleObject_ReferenceTypeNull_AsNil()
        {
            var target = CreateTarget <string>();
            IMessagePackSingleObjectSerializer iface = target;
            var result = iface.PackSingleObject(null);

            Assert.That(result, Is.EqualTo(new byte[] { 0xC0 }));                // nil
        }
        protected NonGenericDictionaryMessagePackSerializer(SerializationContext ownerContext, PolymorphismSchema schema)
            : base(ownerContext)
        {
            var safeSchema = schema ?? PolymorphismSchema.Default;

            this._keySerializer   = ownerContext.GetSerializer(typeof(object), safeSchema.KeySchema);
            this._valueSerializer = ownerContext.GetSerializer(typeof(object), safeSchema.ItemSchema);
        }
Exemple #10
0
        public void TestIMessagePackSingleObjectSerializer_PackSingleObject_ValueTypeButNull_Fail()
        {
            var target   = CreateTarget <TimeSpan>();
            var value    = TimeSpan.FromTicks(12345);
            var expected = GetBytes(target, value);
            IMessagePackSingleObjectSerializer iface = target;

            Assert.Throws <ArgumentException>(() => iface.PackSingleObject(null));
        }
Exemple #11
0
        public void TestIMessagePackSingleObjectSerializer_UnpackSingleObject_Success()
        {
            var target   = CreateTarget <TimeSpan>();
            var expected = TimeSpan.FromTicks(12345);
            var input    = GetBytes(target, expected);
            IMessagePackSingleObjectSerializer iface = target;
            var actual = target.UnpackSingleObject(input);

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemple #12
0
        public void TestIMessagePackSingleObjectSerializer_PackSingleObject_Normal_Success()
        {
            var target   = CreateTarget <TimeSpan>();
            var value    = TimeSpan.FromTicks(12345);
            var expected = GetBytes(target, value);
            IMessagePackSingleObjectSerializer iface = target;
            var actual = iface.PackSingleObject(value);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public static void PackByteArraySegmentTo(Packer packer, object obj, IMessagePackSingleObjectSerializer itemSerializer)
        {
            var objectTree = (ArraySegment <byte>)obj;
#endif // !UNITY
            if (objectTree.Array == null)
            {
                packer.PackBinaryHeader(0);
                return;
            }

            packer.PackBinaryHeader(objectTree.Count);
            packer.PackRawBody(objectTree.Array.Skip(objectTree.Offset).Take(objectTree.Count));
        }
        /// <summary>
        ///		Initializes a new instance of the <see cref="AutoMessagePackSerializer&lt;T&gt;"/> class.
        /// </summary>
        public AutoMessagePackSerializer(Type type, SerializationContext context, Func <SerializationContext, ISerializerBuilder> builderProvider)
            : base(type, context.CompatibilityOptions.PackerCompatibilityOptions)
        {
            Contract.Assert(context != null);

            var serializer = context.Serializers.Get(type, context);

            if (serializer != null)
            {
                this._underlying = serializer;
                return;
            }

            var traits = type.GetCollectionTraits();

            switch (traits.CollectionType)
            {
            case CollectionKind.Array:
            {
                serializer = builderProvider(context).CreateArraySerializer();
                break;
            }

            case CollectionKind.Map:
            {
                serializer = builderProvider(context).CreateMapSerializer();
                break;
            }

            case CollectionKind.NotCollection:
            {
                serializer = builderProvider(context).CreateSerializer();

                break;
            }
            }

            if (serializer != null)
            {
                if (!context.Serializers.Register(type, serializer))
                {
                    serializer = context.Serializers.Get(type, context);
                    Contract.Assert(serializer != null);
                }

                this._underlying = serializer;
                return;
            }

            throw SerializationExceptions.NewTypeCannotSerialize(type);
        }
Exemple #15
0
    private void Test4()
    {
        //IL_0050: Unknown result type (might be due to invalid IL or missing references)
        //IL_0055: Unknown result type (might be due to invalid IL or missing references)
        MemoryStream memoryStream = new MemoryStream();
        Coop_Model_EnemyTargetShotEvent coop_Model_EnemyTargetShotEvent = new Coop_Model_EnemyTargetShotEvent();

        int[] array = new int[3]
        {
            1,
            12,
            123
        };
        int[] array2 = array;
        foreach (int num in array2)
        {
            Enemy.RandomShotInfo.TargetInfo targetInfo = new Enemy.RandomShotInfo.TargetInfo();
            targetInfo.rot      = new Quaternion((float)num, (float)(num * 2), (float)(num * 3), (float)(num * 4));
            targetInfo.targetId = num;
            coop_Model_EnemyTargetShotEvent.targets.Add(targetInfo);
        }
        string log = string.Empty;

        log = string.Empty;
        coop_Model_EnemyTargetShotEvent.targets.ForEach(delegate(Enemy.RandomShotInfo.TargetInfo r)
        {
            //IL_001d: Unknown result type (might be due to invalid IL or missing references)
            string text2 = log;
            log          = text2 + "[" + r.rot + "," + r.targetId + "],";
        });
        Log("before target:" + log);
        Type typeFromHandle = typeof(Coop_Model_EnemyTargetShotEvent);
        IMessagePackSingleObjectSerializer serializer = context.GetSerializer(typeFromHandle);

        serializer.Pack(memoryStream, coop_Model_EnemyTargetShotEvent);
        memoryStream.Position = 0L;
        Coop_Model_EnemyTargetShotEvent coop_Model_EnemyTargetShotEvent2 = (Coop_Model_EnemyTargetShotEvent)serializer.Unpack(memoryStream);

        log = string.Empty;
        coop_Model_EnemyTargetShotEvent2.targets.ForEach(delegate(Enemy.RandomShotInfo.TargetInfo r)
        {
            //IL_001d: Unknown result type (might be due to invalid IL or missing references)
            string text = log;
            log         = text + "[" + r.rot + "," + r.targetId + "],";
        });
        Log("after target:" + log);
        string str = JSONSerializer.Serialize(coop_Model_EnemyTargetShotEvent, typeFromHandle);

        Log("json stream:" + str);
    }
        /// <summary>
        /// </summary>
        /// <param name="unpacker">
        /// </param>
        public void UnpackFromMessage(Unpacker unpacker)
        {
            // Read the type name
            this.typeName = unpacker.LastReadData.AsString();

            // Read the data object as byte array
            byte[] temp;
            unpacker.ReadBinary(out temp);

            // Create a message serializer object
            IMessagePackSingleObjectSerializer ser = MessagePackSerializer.Create(Type.GetType(this.typeName));

            // Unpack the message's data object
            this.dataObject = (MessageBase)ser.UnpackSingleObject(temp);
        }
Exemple #17
0
    private void MsgPack(Type type)
    {
        MemoryStream memoryStream = new MemoryStream();
        object       obj          = Activator.CreateInstance(type);

        Log($"MsgPack:     {obj.GetType().FullName}\nValue:    {obj.ToString()}\nHashCode: {obj.GetHashCode()}\n");
        IMessagePackSingleObjectSerializer serializer = context.GetSerializer(type);

        serializer.Pack(memoryStream, obj);
        Log($"MsgPacked:     {obj.GetType().FullName}\nValue:    {obj.ToString()}\nHashCode: {obj.GetHashCode()}\n");
        memoryStream.Position = 0L;
        object obj2 = serializer.Unpack(memoryStream);

        Log($"MsgUnpack:     {obj2.GetType().FullName}\nValue:    {obj2.ToString()}\nHashCode: {obj2.GetHashCode()}\n");
    }
Exemple #18
0
        protected UnityDictionaryMessagePackSerializer(
            SerializationContext ownerContext,
            Type targetType,
            Type keyType,
            Type valueType,
            CollectionTraits traits,
            PolymorphismSchema schema
            )
            : base(ownerContext, targetType)
        {
            var safeSchema = schema ?? PolymorphismSchema.Default;

            this._keySerializer   = ownerContext.GetSerializer(keyType, safeSchema.KeySchema);
            this._valueSerializer = ownerContext.GetSerializer(valueType, safeSchema.ItemSchema);
            this._add             = traits.AddMethod;
            this._getCount        = traits.CountPropertyGetter;
            this._getKey          = traits.ElementType.GetProperty("Key").GetGetMethod();
            this._getValue        = traits.ElementType.GetProperty("Value").GetGetMethod();
        }
Exemple #19
0
        public AbstractReadOnlyCollectionMessagePackSerializer(
            SerializationContext ownerContext,
            Type targetType,
            PolymorphismSchema schema
            )
            : base(ownerContext, schema)
        {
            IMessagePackSingleObjectSerializer serializer;

            AbstractCollectionSerializerHelper.GetConcreteSerializer(
                ownerContext,
                schema,
                typeof(TCollection),
                targetType,
                typeof(EnumerableMessagePackSerializerBase <,>),
                out this._concreteCollectionInstanceFactory,
                out serializer
                );
            this._polymorphicDeserializer = serializer as IPolymorphicDeserializer;
            this._concreteDeserializer    = serializer;
        }
		public static void GetConcreteSerializer(
			SerializationContext context,
			PolymorphismSchema schema,
			Type abstractType,
			Type targetType,
			Type exampleType,
			out ICollectionInstanceFactory factory,
			out IMessagePackSingleObjectSerializer serializer
		)
		{
			if ( abstractType == targetType )
			{
				throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType( abstractType );
			}

			serializer = context.GetSerializer( targetType, schema );
			factory = serializer as ICollectionInstanceFactory;
			if ( factory == null && (serializer as IPolymorphicDeserializer) == null )
			{
				throw SerializationExceptions.NewIncompatibleCollectionSerializer( abstractType, serializer.GetType(), exampleType );
			}
		}
		public DateTimeOffsetMessagePackSerializerProvider( SerializationContext context, bool isNullable )
		{
			if ( isNullable )
			{
#if !UNITY
				this._unixEpoc =
					new NullableMessagePackSerializer<DateTimeOffset>( context, new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.UnixEpoc ) );
				this._native =
					new NullableMessagePackSerializer<DateTimeOffset>( context, new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.Native ) );
#else
				this._unixEpoc =
					new NullableMessagePackSerializer( context, typeof( DateTimeOffset? ), new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.UnixEpoc ) );
				this._native =
					new NullableMessagePackSerializer( context, typeof( DateTimeOffset? ), new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.Native ) );
#endif // !UNITY
			}
			else
			{
				this._unixEpoc = new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.UnixEpoc );
				this._native = new DateTimeOffsetMessagePackSerializer( context, DateTimeConversionMethod.Native );
			}
		}
        public static void GetConcreteSerializer(
            SerializationContext context,
            PolymorphismSchema schema,
            Type abstractType,
            Type targetType,
            Type exampleType,
            out ICollectionInstanceFactory factory,
            out IMessagePackSingleObjectSerializer serializer
            )
        {
            if (abstractType == targetType)
            {
                throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType(abstractType);
            }

            serializer = context.GetSerializer(targetType, schema);
            factory    = serializer as ICollectionInstanceFactory;
            if (factory == null && (serializer as IPolymorphicDeserializer) == null)
            {
                throw SerializationExceptions.NewIncompatibleCollectionSerializer(abstractType, serializer.GetType(), exampleType);
            }
        }
		public DateTimeMessagePackSerializerProvider( SerializationContext context, bool isNullable )
		{
			if ( isNullable )
			{
#if !UNITY
				this._unixEpoc =
					new NullableMessagePackSerializer<DateTime>( context, new UnixEpocDateTimeMessagePackSerializer( context ) );
				this._native =
					new NullableMessagePackSerializer<DateTime>( context, new NativeDateTimeMessagePackSerializer( context ) );
#else
				this._unixEpoc =
					new NullableMessagePackSerializer( context, typeof( DateTime? ), new UnixEpocDateTimeMessagePackSerializer( context ));
				this._native =
					new NullableMessagePackSerializer( context, typeof( DateTime? ), new NativeDateTimeMessagePackSerializer( context ));
#endif // !UNITY
			}
			else
			{
				this._unixEpoc = new UnixEpocDateTimeMessagePackSerializer( context );
				this._native = new NativeDateTimeMessagePackSerializer( context );
			}
		}
        public DateTimeMessagePackSerializerProvider(SerializationContext context, bool isNullable)
        {
            if (isNullable)
            {
#if !UNITY
                this._unixEpoc =
                    new NullableMessagePackSerializer <DateTime>(context, new UnixEpocDateTimeMessagePackSerializer(context));
                this._native =
                    new NullableMessagePackSerializer <DateTime>(context, new NativeDateTimeMessagePackSerializer(context));
#else
                this._unixEpoc =
                    new NullableMessagePackSerializer(context, typeof(DateTime? ), new UnixEpocDateTimeMessagePackSerializer(context));
                this._native =
                    new NullableMessagePackSerializer(context, typeof(DateTime? ), new NativeDateTimeMessagePackSerializer(context));
#endif // !UNITY
            }
            else
            {
                this._unixEpoc = new UnixEpocDateTimeMessagePackSerializer(context);
                this._native   = new NativeDateTimeMessagePackSerializer(context);
            }
        }
        public DateTimeOffsetMessagePackSerializerProvider(SerializationContext context, bool isNullable)
        {
            if (isNullable)
            {
#if !UNITY
                this._unixEpoc =
                    new NullableMessagePackSerializer <DateTimeOffset>(context, new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.UnixEpoc));
                this._native =
                    new NullableMessagePackSerializer <DateTimeOffset>(context, new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.Native));
#else
                this._unixEpoc =
                    new NullableMessagePackSerializer(context, typeof(DateTimeOffset? ), new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.UnixEpoc));
                this._native =
                    new NullableMessagePackSerializer(context, typeof(DateTimeOffset? ), new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.Native));
#endif // !UNITY
            }
            else
            {
                this._unixEpoc = new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.UnixEpoc);
                this._native   = new DateTimeOffsetMessagePackSerializer(context, DateTimeConversionMethod.Native);
            }
        }
Exemple #26
0
    private void Test2()
    {
        //IL_0043: Unknown result type (might be due to invalid IL or missing references)
        //IL_0095: Unknown result type (might be due to invalid IL or missing references)
        MemoryStream memoryStream = new MemoryStream();
        Coop_Model_ObjectAttackedHitFix coop_Model_ObjectAttackedHitFix = new Coop_Model_ObjectAttackedHitFix();

        coop_Model_ObjectAttackedHitFix.hitPos.x = 1f;
        coop_Model_ObjectAttackedHitFix.hitPos.y = 2.3f;
        coop_Model_ObjectAttackedHitFix.hitPos.z = 3.45f;
        Log("before pos=" + coop_Model_ObjectAttackedHitFix.hitPos);
        Type typeFromHandle = typeof(Coop_Model_ObjectAttackedHitFix);
        IMessagePackSingleObjectSerializer serializer = context.GetSerializer(typeFromHandle);

        serializer.Pack(memoryStream, coop_Model_ObjectAttackedHitFix);
        memoryStream.Position = 0L;
        Coop_Model_ObjectAttackedHitFix coop_Model_ObjectAttackedHitFix2 = (Coop_Model_ObjectAttackedHitFix)serializer.Unpack(memoryStream);

        Log("after pos=" + coop_Model_ObjectAttackedHitFix2.hitPos);
        string str = JSONSerializer.Serialize(coop_Model_ObjectAttackedHitFix, typeFromHandle);

        Log("json stream:" + str);
    }
 public PolymorphicSerializerProvider(SerializationContext context, IMessagePackSingleObjectSerializer defaultSerializer)
 {
     this._defaultSerializer = MessagePackSerializer.Wrap <T>(context, defaultSerializer);
 }
Exemple #28
0
 protected UnityEnumerableMessagePackSerializerBase(SerializationContext ownerContext, Type targetType, Type itemType, PolymorphismSchema schema)
     : base(ownerContext, targetType)
 {
     this._itemSerializer = ownerContext.GetSerializer(itemType, (schema ?? PolymorphismSchema.Default).ItemSchema);
 }
 protected NonGenericEnumerableMessagePackSerializerBase(SerializationContext ownerContext, PolymorphismSchema schema)
     : base(ownerContext)
 {
     this._itemSerializer = ownerContext.GetSerializer(typeof(object), (schema ?? PolymorphismSchema.Default).ItemSchema);
 }
        public static object UnpackByteArraySegmentFrom(Unpacker unpacker, Type elementType, IMessagePackSingleObjectSerializer itemSerializer)
#endif // !UNITY
        {
            return(new ArraySegment <byte>(unpacker.LastReadData.AsBinary()));
        }
        public static void PackGenericArraySegmentTo(Packer packer, object objectTree, IMessagePackSingleObjectSerializer itemSerializer)
        {
            var count  = ( int )objectTree.GetType().GetProperty("Count").GetGetMethod().InvokePreservingExceptionType(objectTree);
            var offset = ( int )objectTree.GetType().GetProperty("Offset").GetGetMethod().InvokePreservingExceptionType(objectTree);
            var array  = objectTree.GetType().GetProperty("Array").GetGetMethod().InvokePreservingExceptionType(objectTree) as Array;

            packer.PackArrayHeader(count);
            for (int i = 0; i < count; i++)
            {
                itemSerializer.PackTo(packer, array.GetValue(i + offset));
            }
        }
        public static object UnpackCharArraySegmentFrom(Unpacker unpacker, Type elementType, IMessagePackSingleObjectSerializer itemSerializer)
#endif // !UNITY
        {
            // TODO: More efficient
            return(new ArraySegment <char>(unpacker.LastReadData.AsCharArray()));
        }
        public static object UnpackGenericArraySegmentFrom(Unpacker unpacker, Type elementType, IMessagePackSingleObjectSerializer itemSerializer)
        {
            Array array = Array.CreateInstance(elementType, unpacker.ItemsCount);

            for (int i = 0; i < array.Length; i++)
            {
                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                array.SetValue(itemSerializer.UnpackFrom(unpacker), i);
            }

            return
                (ReflectionExtensions.CreateInstancePreservingExceptionType(
                     typeof(ArraySegment <>).MakeGenericType(elementType),
                     array
                     ));
        }