Exemplo n.º 1
0
        public object Read(object value, ProtoReader source)
        {
            UnityEngine.Vector2 data = (UnityEngine.Vector2)value;

            int fieldNumber = 0;

            SubItemToken token = ProtoReader.StartSubItem(source);

            while ((fieldNumber = source.ReadFieldHeader()) != 0)
            {
                switch (fieldNumber)
                {
                case 1:
                    data.x = source.ReadSingle();
                    break;

                case 2:
                    data.y = source.ReadSingle();
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }

            ProtoReader.EndSubItem(token, source);

            return(data);
        }
Exemplo n.º 2
0
        public object Read(object value, ProtoReader source)
        {
            object[] array = new object[members.Length];
            bool     flag  = false;

            if (value == null)
            {
                flag = true;
            }
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = GetValue(value, i);
            }
            int num;

            while ((num = source.ReadFieldHeader()) > 0)
            {
                flag = true;
                if (num <= tails.Length)
                {
                    IProtoSerializer protoSerializer = tails[num - 1];
                    array[num - 1] = tails[num - 1].Read(protoSerializer.RequiresOldValue ? array[num - 1] : null, source);
                }
                else
                {
                    source.SkipField();
                }
            }
            if (!flag)
            {
                return(value);
            }
            return(ctor.Invoke(array));
        }
Exemplo n.º 3
0
        public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            TDictionary typed = (AppendToCollection ? ((TDictionary)value) : null)
                                ?? (TDictionary)Activator.CreateInstance(concreteType);

            do
            {
                var          key        = DefaultKey;
                var          typedValue = DefaultValue;
                SubItemToken token      = ProtoReader.StartSubItem(source, ref state);
                int          field;
                while ((field = source.ReadFieldHeader(ref state)) > 0)
                {
                    switch (field)
                    {
                    case 1:
                        key = (TKey)keyTail.Read(source, ref state, null);
                        break;

                    case 2:
                        typedValue = (TValue)Tail.Read(source, ref state, Tail.RequiresOldValue ? (object)typedValue : null);
                        break;

                    default:
                        source.SkipField(ref state);
                        break;
                    }
                }

                ProtoReader.EndSubItem(token, source, ref state);
                typed[key] = typedValue;
            } while (source.TryReadFieldHeader(ref state, fieldNumber));

            return(typed);
        }
 public object Read(object value, ProtoReader source)
 {
     if (source.ReadFieldHeader() != _number)
     {
         throw new ProtoException("Expected tag " + _number);
     }
     return(_serializer.Read(value, source));
 }
Exemplo n.º 5
0
        private static ActiveCodeReq Read(ActiveCodeReq acReq, ProtoReader protoReader)
        {
            int fieldNum;

            while ((fieldNum = protoReader.ReadFieldHeader()) > 0)
            {
                if (fieldNum != 1)
                {
                    if (fieldNum != 2)
                    {
                        if (acReq == null)
                        {
                            ActiveCodeReq activeCodeReq = new ActiveCodeReq();
                            ProtoReader.NoteObject(activeCodeReq, protoReader);
                            acReq = activeCodeReq;
                        }

                        protoReader.AppendExtensionData(acReq);
                    }
                    else
                    {
                        if (acReq == null)
                        {
                            ActiveCodeReq activeCodeReq = new ActiveCodeReq();
                            ProtoReader.NoteObject(activeCodeReq, protoReader);
                            acReq = activeCodeReq;
                        }

                        string activeCode = protoReader.ReadString();
                        if (activeCode != null)
                        {
                            acReq.ActiveCode = activeCode;
                        }
                    }
                }
                else
                {
                    if (acReq == null)
                    {
                        ActiveCodeReq activeCodeReq = new ActiveCodeReq();
                        ProtoReader.NoteObject(activeCodeReq, protoReader);
                        acReq = activeCodeReq;
                    }

                    long accountId = protoReader.ReadInt64();
                    acReq.AccountId = accountId;
                }
            }

            if (acReq == null)
            {
                ActiveCodeReq activeCodeReq = new ActiveCodeReq();
                ProtoReader.NoteObject(activeCodeReq, protoReader);
                acReq = activeCodeReq;
            }

            return(acReq);
        }
Exemplo n.º 6
0
 public bool TryReadType(ArraySegment <byte> data, out Type type)
 {
     using (var stream = new MemoryStream(data.Array, data.Offset, data.Count))
     {
         var reader = new ProtoReader(stream, null, null);
         int typeId = reader.ReadFieldHeader();
         return(_messageIdToType.TryGetValue(typeId, out type));
     }
 }
Exemplo n.º 7
0
        private void ReadStream(Stream stream, Dictionary <int, int> remoteToLocalRowIds)
        {
            var len = 0;

            if (WithLengthPrefix)
            {
                // Read the length of the message from the stream.
                // DirectReadVarintInt32 calls Stream.ReadByte() which allocates a one byte array on every call - yuck!
                var header = ProtoReader.DirectReadVarintInt32(stream);
                len = ProtoReader.DirectReadVarintInt32(stream);
            }
            // Allocation of protoreader for each message - blurghh!
            using (var reader = new ProtoReader(stream, null, null, len))
            {
                int fieldId;
                while ((fieldId = reader.ReadFieldHeader()) != 0)
                {
                    if (fieldId == ProtobufOperationTypes.Add)
                    {
                        var rowId = ReadRowId(reader);
                        if (rowId >= 0)
                        {
                            remoteToLocalRowIds.Add(rowId, _table.AddRow());
                            var dummyFieldId = reader.ReadFieldHeader();
                            ReadUpdate(remoteToLocalRowIds, reader);
                        }
                    }
                    else if (fieldId == ProtobufOperationTypes.Update)
                    {
                        ReadUpdate(remoteToLocalRowIds, reader);
                    }
                    else if (fieldId == ProtobufOperationTypes.Delete)
                    {
                        var rowId = ReadRowId(reader);
                        if (rowId >= 0)
                        {
                            _table.DeleteRow(remoteToLocalRowIds[rowId]);
                            remoteToLocalRowIds.Remove(rowId);
                        }
                    }
                }
            }
        }
    public object Deserialize(object obj, ProtoReader reader, ProtobufSerializerPrecompiled model)
    {
        var scale = obj as ScaleFixer;

        reader.ReadFieldHeader();
        var token = ProtoReader.StartSubItem(reader);

        scale.scale = (Vector3)AlternativeSerializer.PrecompDeserialize(model, AlternativeSerializer.PrecompGetKey(model, typeof(Vector3)), scale.scale, reader);
        ProtoReader.EndSubItem(token, reader);
        return(scale);
    }
Exemplo n.º 9
0
            public void ShouldNotSerializeIfValueIsNull()
            {
                // Arrange
                string value      = null;
                var    dataReader = this.CreateDataReader(value);

                // Act
                var reader = new ProtoReader(this.Serialize(dataReader), null, null);

                // Assert
                var readerContext = new ProtoReaderContext(reader);

                readerContext.ReadUntilField();
                readerContext.ReadExpectedFieldHeader(3);

                Assert.Equal(0, reader.ReadFieldHeader());
            }
Exemplo n.º 10
0
        public object Read(object value, ProtoReader source)
        {
            UnityEngine.Gradient data = (UnityEngine.Gradient)(value == null ? CreateInstance(source) : value);

            int fieldNumber = 0;

            SubItemToken token = ProtoReader.StartSubItem(source);

            while ((fieldNumber = source.ReadFieldHeader()) != 0)
            {
                switch (fieldNumber)
                {
                case 1:
                    data.mode = (UnityEngine.GradientMode)source.ReadInt32();
                    break;

                case 2:
                    alphaKeys.Clear();
                    do
                    {
                        alphaKeys.Add((UnityEngine.GradientAlphaKey) this.alphaKeySerializer.Read(
                                          new UnityEngine.GradientAlphaKey(), source));
                    } while (source.TryReadFieldHeader(2));
                    data.alphaKeys = alphaKeys.ToArray();
                    break;

                case 3:
                    colorKeys.Clear();
                    do
                    {
                        colorKeys.Add((UnityEngine.GradientColorKey) this.colorKeySerializer.Read(
                                          new UnityEngine.GradientColorKey(), source));
                    } while (source.TryReadFieldHeader(3));
                    data.colorKeys = colorKeys.ToArray();
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }

            ProtoReader.EndSubItem(token, source);

            return(data);
        }
Exemplo n.º 11
0
        public void TestRandomDataWithReader()
        {
            var input  = File.ReadAllBytes("protobuf-net.dll");
            var stream = new MemoryStream(input);

            stream.Seek(0, SeekOrigin.Begin);
            Assert.Greater(3, 0); // I always double-check the param order
            Assert.Greater(stream.Length, 0);

            using (var reader = new ProtoReader(stream, null, null))
            {
                while (reader.ReadFieldHeader() > 0)
                {
                    reader.SkipField();
                }
            }
        }
Exemplo n.º 12
0
 public override object Read(object value, ProtoReader source)
 {
     SubItemToken token = ProtoReader.StartSubItem(source);
     int num;
     while ((num = source.ReadFieldHeader()) > 0)
     {
         if (num == 1)
         {
             value = this.Tail.Read(value, source);
         }
         else
         {
             source.SkipField();
         }
     }
     ProtoReader.EndSubItem(token, source);
     return value;
 }
Exemplo n.º 13
0
        public object Read(object value, ProtoReader source)
        {
            object[] values       = new object[_members.Length];
            bool     invokeCtor   = false;
            int      reservedTrap = -1;

            if (value == null)
            {
                reservedTrap = ProtoReader.ReserveNoteObject(source);
                invokeCtor   = true;
            }
            var token = ProtoReader.StartSubItem(source);

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = GetValue(value, i);
            }
            int field;

            while ((field = source.ReadFieldHeader()) > 0)
            {
                invokeCtor = true;
                if (field <= _tails.Length)
                {
                    IProtoSerializer tail = _tails[field - 1];
                    values[field - 1] = _tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
                }
                else
                {
                    source.SkipField();
                }
            }
            ProtoReader.EndSubItem(token, source);
            if (invokeCtor)
            {
                var r = _ctor.Invoke(values);
                // inside references won't work, but from outside will
                // this is a common problem when deserializing immutable types
                ProtoReader.NoteReservedTrappedObject(reservedTrap, r, source);
                return(r);
            }
            return(value);
        }
Exemplo n.º 14
0
        public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            SubItemToken tok = ProtoReader.StartSubItem(source, ref state);
            int          field;

            while ((field = source.ReadFieldHeader(ref state)) > 0)
            {
                if (field == Tag)
                {
                    value = Tail.Read(source, ref state, value);
                }
                else
                {
                    source.SkipField(ref state);
                }
            }
            ProtoReader.EndSubItem(tok, source, ref state);
            return(value);
        }
Exemplo n.º 15
0
        /// <summary>
        /// The read.
        /// </summary>
        /// <param name="mxPayload">
        /// The mx payload.
        /// </param>
        /// <param name="protoReader">
        /// The proto reader.
        /// </param>
        /// <returns>
        /// The <see cref="MxPayload"/>.
        /// </returns>
        private static MxPayload Read(MxPayload mxPayload, ProtoReader protoReader)
        {
            int num;

            while ((num = protoReader.ReadFieldHeader()) > 0)
            {
                if (num != 1)
                {
                    if (mxPayload == null)
                    {
                        var expr_49 = new MxPayload();
                        ProtoReader.NoteObject(expr_49, protoReader);
                        mxPayload = expr_49;
                    }

                    protoReader.SkipField();
                }
                else
                {
                    if (mxPayload == null)
                    {
                        var expr_19 = new MxPayload();
                        ProtoReader.NoteObject(expr_19, protoReader);
                        mxPayload = expr_19;
                    }

                    byte[] array = ProtoReader.AppendBytes(mxPayload.Data, protoReader);
                    if (array != null)
                    {
                        mxPayload.Data = array;
                    }
                }
            }

            if (mxPayload == null)
            {
                var expr_71 = new MxPayload();
                ProtoReader.NoteObject(expr_71, protoReader);
                mxPayload = expr_71;
            }

            return(mxPayload);
        }
Exemplo n.º 16
0
        // Token: 0x060035AC RID: 13740 RVA: 0x00134CD4 File Offset: 0x001330D4
        public override object Read(object value, ProtoReader source)
        {
            SubItemToken token = ProtoReader.StartSubItem(source);
            int          num;

            while ((num = source.ReadFieldHeader()) > 0)
            {
                if (num == 1)
                {
                    value = Tail.Read(value, source);
                }
                else
                {
                    source.SkipField();
                }
            }
            ProtoReader.EndSubItem(token, source);
            return(value);
        }
Exemplo n.º 17
0
        private void ReadUpdate(Dictionary <int, int> remoteToLocalRowIds, ProtoReader reader)
        {
            var token = ProtoReader.StartSubItem(reader);

            var fieldId = reader.ReadFieldHeader();

            if (fieldId == ProtobufFieldIds.RowId) // Check for row id
            {
                var rowId = reader.ReadInt32();

                int localRowId;
                if (remoteToLocalRowIds.TryGetValue(rowId, out localRowId))
                {
                    WriteFieldsToTable(_table, _fieldIdsToColumns, reader, remoteToLocalRowIds[rowId]);
                }
            }

            ProtoReader.EndSubItem(token, reader);
        }
Exemplo n.º 18
0
        public object Read(object value, ProtoReader source)
        {
            UnityEngine.Keyframe data = (UnityEngine.Keyframe)value;

            int fieldNumber = 0;

            SubItemToken token = ProtoReader.StartSubItem(source);

            while ((fieldNumber = source.ReadFieldHeader()) != 0)
            {
                switch (fieldNumber)
                {
                case 1:
                    data.time = source.ReadSingle();
                    break;

                case 2:
                    data.value = source.ReadSingle();
                    break;

                case 3:
                    data.inTangent = source.ReadSingle();
                    break;

                case 4:
                    data.outTangent = source.ReadSingle();
                    break;

                case 5:
                    data.tangentMode = source.ReadInt32();
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }

            ProtoReader.EndSubItem(token, source);

            return(data);
        }
Exemplo n.º 19
0
        public object Read(object value, ProtoReader source)

        {
            object[] values = new object[members.Length];

            bool invokeCtor = false;

            if (value == null)

            {
                invokeCtor = true;
            }

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = GetValue(value, i);
            }

            int field;

            while ((field = source.ReadFieldHeader()) > 0)

            {
                invokeCtor = true;

                if (field <= tails.Length)

                {
                    IProtoSerializer tail = tails[field - 1];

                    values[field - 1] = tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
                }

                else

                {
                    source.SkipField();
                }
            }

            return(invokeCtor ? ctor.Invoke(values) : value);
        }
Exemplo n.º 20
0
        public static DataSet ProtoRead(Stream stream)
        {
            DataSet ds = new DataSet();

            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int num;
                while ((num = reader.ReadFieldHeader()) != 0)
                {
                    if (num <= 0)
                    {
                        continue;
                    }
                    switch (num)
                    {
                    case 1:
                    {
                        SubItemToken token = ProtoReader.StartSubItem(reader);
                        ProtoReadTable(ds, reader);
                        ProtoReader.EndSubItem(token, reader);
                        continue;
                    }

                    case 3:
                    {
                        ds.DataSetName = reader.ReadString();
                        continue;
                    }

                    case 4:
                    {
                        string       s       = reader.ReadString();
                        MemoryStream stream2 = new MemoryStream(Encoding.UTF8.GetBytes(s));
                        ds.ReadXmlSchema(stream2);
                        continue;
                    }
                    }
                    reader.SkipField();
                }
            }
            return(ds);
        }
Exemplo n.º 21
0
        public void TestRandomDataWithReader()
        {
            Program.ExpectFailure <ProtoException>(() =>
            {
                var bytes = new byte[1024];
                new Random(123456).NextBytes(bytes);
                var stream = new MemoryStream(bytes);
                stream.Seek(0, SeekOrigin.Begin);
                Assert.Greater(3, 0); // I always double-check the param order
                Assert.Greater(stream.Length, 0);

                using (var reader = new ProtoReader(stream, null, null))
                {
                    while (reader.ReadFieldHeader() > 0)
                    {
                        reader.SkipField();
                    }
                }
            });
        }
        public object Read(object value, ProtoReader source)
        {
            UnityEngine.AnimationCurve data = (UnityEngine.AnimationCurve)
                                                  (value == null ? CreateInstance(source) : value);

            int fieldNumber = 0;

            SubItemToken token = ProtoReader.StartSubItem(source);

            while ((fieldNumber = source.ReadFieldHeader()) != 0)
            {
                switch (fieldNumber)
                {
                case 1:
                    data.preWrapMode = (UnityEngine.WrapMode)source.ReadInt32();
                    break;

                case 2:
                    data.postWrapMode = (UnityEngine.WrapMode)source.ReadInt32();
                    break;

                case 3:
                    keys.Clear();
                    do
                    {
                        keys.Add((UnityEngine.Keyframe) this.keyframeSerializer.Read(
                                     new UnityEngine.Keyframe(), source));
                    } while (source.TryReadFieldHeader(3));
                    data.keys = keys.ToArray();
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }

            ProtoReader.EndSubItem(token, source);

            return(data);
        }
Exemplo n.º 23
0
        public override object Read(object untyped, ProtoReader source)
        {
            TDictionary typed = AppendToCollection ? ((TDictionary)untyped) : null;

            //if (typed == null) typed = (TDictionary)Activator.CreateInstance(concreteType);
            if (typed == null)
            {
                typed = (TDictionary)ActivatorUtils.FastCreateInstance(concreteType);
            }

            do
            {
                var          key   = DefaultKey;
                var          value = DefaultValue;
                SubItemToken token = ProtoReader.StartSubItem(source);
                int          field;
                while ((field = source.ReadFieldHeader()) > 0)
                {
                    switch (field)
                    {
                    case 1:
                        key = (TKey)keyTail.Read(null, source);
                        break;

                    case 2:
                        value = (TValue)Tail.Read(Tail.RequiresOldValue ? (object)value : null, source);
                        break;

                    default:
                        source.SkipField();
                        break;
                    }
                }

                ProtoReader.EndSubItem(token, source);
                typed[key] = value;
            } while (source.TryReadFieldHeader(fieldNumber));

            return(typed);
        }
            public void ShouldSerializeUsingMultipleIterations()
            {
                // Arrange
                var stream       = new ProtoDataStream(this.CreateDataReader("foo", 1000));
                var outputStream = new MemoryStream();

                var buffer = new byte[1];
                int read;

                // Act
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputStream.Write(buffer, 0, read);
                }

                // Assert
                outputStream.Position = 0;

                var reader = new ProtoReader(outputStream, null, null);

                Assert.Equal(ResultFieldHeader, reader.ReadFieldHeader());
            }
Exemplo n.º 25
0
            public void ShouldNotSerializeIfValueIsEmptyArray()
            {
                // Arrange
                var value      = new char[0];
                var dataReader = this.CreateDataReader(value);

                var options = new ProtoDataWriterOptions()
                {
                    SerializeEmptyArraysAsNull = true
                };

                // Act
                var reader = new ProtoReader(this.Serialize(dataReader, options), null, null);

                // Assert
                var readerContext = new ProtoReaderContext(reader);

                readerContext.ReadUntilField();
                readerContext.ReadExpectedFieldHeader(3);

                Assert.Equal(0, reader.ReadFieldHeader());
            }
Exemplo n.º 26
0
            public void ShouldSerializeEmptyCharArrayAsNull()
            {
                // Arrange
                var dataReader = this.CreateDataReader(new char[0]);
                var options    = new ProtoDataWriterOptions()
                {
                    SerializeEmptyArraysAsNull = true
                };

                // Act
                var reader = new ProtoReader(this.Serialize(dataReader, options), null, null);

                // Assert
                var readerContext = new ProtoReaderContext(reader);

                readerContext.ReadUntilField();

                readerContext.ReadExpectedFieldHeader(3);
                readerContext.StartSubItem();

                Assert.Equal(0, reader.ReadFieldHeader());
            }
Exemplo n.º 27
0
        private static void Merge(ProtoReader reader, ref ProtoReader.State state, ref protogen.OrderLine obj)
        {
            int field;

            if (obj == null)
            {
                obj = new protogen.OrderLine();
            }
            while ((field = reader.ReadFieldHeader(ref state)) != 0)
            {
                switch (field)
                {
                case 1:
                    obj.OrderID = reader.ReadInt32(ref state);
                    break;

                case 2:
                    obj.ProductID = reader.ReadInt32(ref state);
                    break;

                case 3:
                    obj.UnitPrice = reader.ReadDouble(ref state);
                    break;

                case 4:
                    reader.Hint(WireType.SignedVariant);
                    obj.Quantity = reader.ReadInt32(ref state);
                    break;

                case 5:
                    obj.Discount = reader.ReadSingle(ref state);
                    break;

                default:
                    reader.AppendExtensionData(ref state, obj);
                    break;
                }
            }
        }
        public object Read(object value, ProtoReader source)
        {
            object[] objArray = new object[(int)this.members.Length];
            bool     flag     = false;

            if (value == null)
            {
                flag = true;
            }
            for (int i = 0; i < (int)objArray.Length; i++)
            {
                objArray[i] = this.GetValue(value, i);
            }
            while (true)
            {
                int num  = source.ReadFieldHeader();
                int num1 = num;
                if (num <= 0)
                {
                    break;
                }
                flag = true;
                if (num1 > (int)this.tails.Length)
                {
                    source.SkipField();
                }
                else
                {
                    IProtoSerializer protoSerializer = this.tails[num1 - 1];
                    objArray[num1 - 1] = this.tails[num1 - 1].Read((protoSerializer.RequiresOldValue ? objArray[num1 - 1] : null), source);
                }
            }
            if (!flag)
            {
                return(value);
            }
            return(this.ctor.Invoke(objArray));
        }
Exemplo n.º 29
0
            public void ShouldNotSerializeIfValueIsDBNull()
            {
                // Arrange
                var value     = DBNull.Value;
                var dataTable = new DataTable();

                dataTable.Columns.Add("foo", typeof(string));

                dataTable.Rows.Add(value);

                var dataReader = dataTable.CreateDataReader();

                // Act
                var reader = new ProtoReader(this.Serialize(dataReader), null, null);

                // Assert
                var readerContext = new ProtoReaderContext(reader);

                readerContext.ReadUntilField();
                readerContext.ReadExpectedFieldHeader(3);

                Assert.Equal(0, reader.ReadFieldHeader());
            }
Exemplo n.º 30
0
        /// <summary>
        /// Parses a decimal from a protobuf stream
        /// </summary>
        public static decimal ReadDecimal(ProtoReader reader)
        {
            ulong low       = 0;
            uint  high      = 0;
            uint  signScale = 0;

            int          fieldNumber;
            SubItemToken token = ProtoReader.StartSubItem(reader);

            while ((fieldNumber = reader.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case FieldDecimalLow: low = reader.ReadUInt64(); break;

                case FieldDecimalHigh: high = reader.ReadUInt32(); break;

                case FieldDecimalSignScale: signScale = reader.ReadUInt32(); break;

                default: reader.SkipField(); break;
                }
            }
            ProtoReader.EndSubItem(token, reader);

            if (low == 0 && high == 0)
            {
                return(decimal.Zero);
            }

            int lo     = (int)(low & 0xFFFFFFFFL),
                mid    = (int)((low >> 32) & 0xFFFFFFFFL),
                hi     = (int)high;
            bool isNeg = (signScale & 0x0001) == 0x0001;
            byte scale = (byte)((signScale & 0x01FE) >> 1);

            return(new decimal(lo, mid, hi, isNeg, scale));
        }
Exemplo n.º 31
0
        public override object Read(object value, ProtoReader source)
        {
            SubItemToken subItemToken = ProtoReader.StartSubItem(source);

            while (true)
            {
                int num  = source.ReadFieldHeader();
                int num1 = num;
                if (num <= 0)
                {
                    break;
                }
                if (num1 != 1)
                {
                    source.SkipField();
                }
                else
                {
                    value = this.Tail.Read(value, source);
                }
            }
            ProtoReader.EndSubItem(subItemToken, source);
            return(value);
        }
        /// <summary>
        /// This is the more "complete" version of Deserialize, which handles single instances of mapped types.
        /// The value is read as a complete field, including field-header and (for sub-objects) a
        /// length-prefix..kmc  
        /// 
        /// In addition to that, this provides support for:
        ///  - basic values; individual int / string / Guid / etc
        ///  - IList sets of any type handled by TryDeserializeAuxiliaryType
        /// </summary>
        private bool TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, int tag, Type type, ref object value, bool skipOtherFields, bool asListItem)
        {
            if (type == null) throw new ArgumentNullException("type");
            Type itemType = null;
            TypeCode typecode = Type.GetTypeCode(type);
            int modelKey;
            WireType wiretype = GetWireType(typecode, format, ref type, out modelKey);

            bool found = false;
            if (wiretype == WireType.None)
            {
                itemType = GetListItemType(type);

                if (itemType != null)
                {
                    return TryDeserializeList(reader, format, tag, type, itemType, ref value);                    
                }
                // otherwise, not a happy bunny...
                ThrowUnexpectedType(type);
            }
            
            // to treat correctly, should read all values

            while (true)
            {
                // for convenience (re complex exit conditions), additional exit test here:
                // if we've got the value, are only looking for one, and we aren't a list - then exit
                if (found && asListItem) break;

                // read the next item
                int fieldNumber = reader.ReadFieldHeader();
                if (fieldNumber <= 0) break;
                if (fieldNumber != tag)
                {
                    if (skipOtherFields)
                    {
                        reader.SkipField();
                        continue;
                    }
                    throw ProtoReader.AddErrorData(new InvalidOperationException(
                        "Expected field " + tag + ", but found " + fieldNumber), reader);
                }
                found = true;
                reader.Hint(wiretype); // handle signed data etc

                if (modelKey >= 0)
                {
                    switch (wiretype)
                    {
                        case WireType.String:
                        case WireType.StartGroup:
                            SubItemToken token = ProtoReader.StartSubItem(reader);
                            value = Deserialize(modelKey, value, reader);
                            ProtoReader.EndSubItem(token, reader);
                            continue;
                        default:
                            value = Deserialize(modelKey, value, reader);
                            continue;
                    }
                }
                switch (typecode)
                {
                    case TypeCode.Int16: value = reader.ReadInt16(); continue;
                    case TypeCode.Int32: value = reader.ReadInt32(); continue;
                    case TypeCode.Int64: value = reader.ReadInt64(); continue;
                    case TypeCode.UInt16: value = reader.ReadUInt16(); continue;
                    case TypeCode.UInt32: value = reader.ReadUInt32(); continue;
                    case TypeCode.UInt64: value = reader.ReadUInt64(); continue;
                    case TypeCode.Boolean: value = reader.ReadBoolean(); continue;
                    case TypeCode.SByte: value = reader.ReadSByte(); continue;
                    case TypeCode.Byte: value = reader.ReadByte(); continue;
                    case TypeCode.Char: value = (char)reader.ReadUInt16(); continue;
                    case TypeCode.Double: value = reader.ReadDouble(); continue;
                    case TypeCode.Single: value = reader.ReadSingle(); continue;
                    case TypeCode.DateTime: value = BclHelpers.ReadDateTime(reader); continue;
                    case TypeCode.Decimal: BclHelpers.ReadDecimal(reader); continue;
                    case TypeCode.String: value = reader.ReadString(); continue;
                }
                if (type == typeof(byte[])) { value = ProtoReader.AppendBytes((byte[])value, reader); continue; }
                if (type == typeof(TimeSpan)) { value = BclHelpers.ReadTimeSpan(reader); continue; }
                if (type == typeof(Guid)) { value = BclHelpers.ReadGuid(reader); continue; }
                if (type == typeof(Uri)) { value = new Uri(reader.ReadString()); continue; }

            }
            if (!found && !asListItem) { value = Activator.CreateInstance(type); }
            return found;
        }
Exemplo n.º 33
0
 public object Read(object value, ProtoReader source)
 {
     object[] values = new object[members.Length];
     bool invokeCtor = false;
     if (value == null)
     {
         invokeCtor = true;
     }
     for (int i = 0; i < values.Length; i++)
         values[i] = GetValue(value, i);
     int field;
     while ((field = source.ReadFieldHeader()) > 0)
     {
         invokeCtor = true;
         if (field <= tails.Length)
         {
             IProtoSerializer tail = tails[field - 1];
             values[field - 1] = tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
         }
         else
         {
             source.SkipField();
         }
     }
     return invokeCtor ? ctor.Invoke(values) : value;
 }
Exemplo n.º 34
0
 public override object Read(object value, ProtoReader source)
 {
     SubItemToken tok = ProtoReader.StartSubItem(source);
     int field;
     while((field = source.ReadFieldHeader()) > 0)
     {
         if(field == Tag) {
             value = Tail.Read(value, source);
         } else {
             source.SkipField();
         }
     }
     ProtoReader.EndSubItem(tok, source);
     return value;
 }
        public object Read(object value, ProtoReader source)
        {
			object[] values = new object[members.Length];
        	int tupleKey = 0;
        	object oldTuple = null;

        	bool issueReferenceDirectives = !baseTupleAsReference && asReference;

        	if (issueReferenceDirectives)
			{
				tupleKey = (int)source.ReadUInt32();

				if (tupleKey > 0)
				{
					return source.NetCache.GetKeyedObject(tupleKey);
				}
				else
				{
					bool dummy;
					oldTuple = new object();

					tupleKey = source.NetCache.AddObjectKey(oldTuple, out dummy);
				}
			}

			bool invokeCtor = false;
			if (value == null)
			{
				invokeCtor = true;
			}
			for (int i = 0; i < values.Length; i++)
				values[i] = GetValue(value, i);

        	int j = 0;
			
        	int field;
			while (j++ < values.Length && source.ReadNextFieldHack() > 0)
			{
				field = source.ReadFieldHeader();

				invokeCtor = true;
                if(field <= tails.Length)
                {
                    IProtoSerializer tail = tails[field - 1];
                    values[field - 1] = tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
                }
                else
                {
                    source.SkipField();
                }
            }

        	object result = invokeCtor ? ctor.Invoke(values) : value;

			if (issueReferenceDirectives)
			{
				source.NetCache.UpdateKeyedObject(tupleKey, oldTuple, result);
			}

			if (forceIssueFakeHeader)
			{
				source.ReadEndGroupFieldHeaderHack();
			}

        	return result;
        }
Exemplo n.º 36
0
 public object Read(object value, ProtoReader source)
 {
     if (this.isRootType && value != null)
     {
         this.Callback(value, TypeModel.CallbackType.BeforeDeserialize, source.Context);
     }
     int num = 0;
     int num2 = 0;
     int num3;
     while ((num3 = source.ReadFieldHeader()) > 0)
     {
         bool flag = false;
         if (num3 < num)
         {
             num2 = (num = 0);
         }
         for (int i = num2; i < this.fieldNumbers.Length; i++)
         {
             if (this.fieldNumbers[i] == num3)
             {
                 IProtoSerializer protoSerializer = this.serializers[i];
                 Type expectedType = protoSerializer.ExpectedType;
                 if (value == null)
                 {
                     if (expectedType == this.forType)
                     {
                         value = this.CreateInstance(source, true);
                     }
                 }
                 else if (expectedType != this.forType && ((IProtoTypeSerializer)protoSerializer).CanCreateInstance() && expectedType.IsSubclassOf(value.GetType()))
                 {
                     value = ProtoReader.Merge(source, value, ((IProtoTypeSerializer)protoSerializer).CreateInstance(source));
                 }
                 if (protoSerializer.ReturnsValue)
                 {
                     value = protoSerializer.Read(value, source);
                 }
                 else
                 {
                     protoSerializer.Read(value, source);
                 }
                 num2 = i;
                 num = num3;
                 flag = true;
                 break;
             }
         }
         if (!flag)
         {
             if (value == null)
             {
                 value = this.CreateInstance(source, true);
             }
             if (this.isExtensible)
             {
                 source.AppendExtensionData((IExtensible)value);
             }
             else
             {
                 source.SkipField();
             }
         }
     }
     if (value == null)
     {
         value = this.CreateInstance(source, true);
     }
     if (this.isRootType)
     {
         this.Callback(value, TypeModel.CallbackType.AfterDeserialize, source.Context);
     }
     return value;
 }
Exemplo n.º 37
0
 public object Read(object value, ProtoReader source)
 {
     object[] array = new object[this.members.Length];
     bool flag = false;
     if (value == null)
     {
         flag = true;
     }
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = this.GetValue(value, i);
     }
     int num;
     while ((num = source.ReadFieldHeader()) > 0)
     {
         flag = true;
         if (num <= this.tails.Length)
         {
             IProtoSerializer protoSerializer = this.tails[num - 1];
             array[num - 1] = this.tails[num - 1].Read((!protoSerializer.RequiresOldValue) ? null : array[num - 1], source);
         }
         else
         {
             source.SkipField();
         }
     }
     return (!flag) ? value : this.ctor.Invoke(array);
 }
        public override object Read(object value, ProtoReader source)
        {
			if (nested)
			{
				source.ReadFieldHeader();
			}

            int field = source.FieldNumber;
            BasicList list = new BasicList();

			if (packedWireType != WireType.None && source.WireType == WireType.String)
            {
                SubItemToken token = ProtoReader.StartSubItem(source);
                while (ProtoReader.HasSubValue(packedWireType, source))
                {
                    list.Add(Tail.Read(null, source));
                }
                ProtoReader.EndSubItem(token, source);

				int oldLen = AppendToCollection ? ((value == null ? 0 : ((Array)value).Length)) : 0;
				Array result = Array.CreateInstance(itemType, oldLen + list.Count);
				if (oldLen != 0) ((Array)value).CopyTo(result, 0);
				list.CopyTo(result, oldLen);

            	return result;
            }
            else
            {
				bool readObject = true;
            	int arrayKey = 0;

				value = value ?? Array.CreateInstance(itemType, 0);

				if (AsReference)
            	{
					int objectKey = source.ReadInt32();

					if (objectKey > 0)
					{
						value = source.NetCache.GetKeyedObject(objectKey);
						readObject = false;
					}
					else
					{
						bool dummy;
						arrayKey = source.NetCache.AddObjectKey(value, out dummy);
					}
            	}
				else
				{
					bool isEmpty = source.ReadInt32() == 1;
					if (isEmpty)
					{
						return value; 
					}
				}

				if (readObject)
				{
					while (source.TryReadFieldHeader(field)) 
					{
						list.Add(Tail.Read(null, source));
					}

					Array newArray = Array.CreateInstance(itemType, list.Count);
					list.CopyTo(newArray, 0);

					if (AsReference)
					{
						source.NetCache.UpdateKeyedObject(arrayKey, value, newArray);	
					}

					value = newArray;
				}
            }

            return value;
        }
Exemplo n.º 39
0
        public object Read(object value, ProtoReader source)
        {
            if (isRootType && value != null) { Callback(value, TypeModel.CallbackType.BeforeDeserialize, source.Context); }
            int fieldNumber, lastFieldNumber = 0, lastFieldIndex = 0;
            bool fieldHandled;

            //Helpers.DebugWriteLine(">> Reading fields for " + forType.FullName);
            while ((fieldNumber = source.ReadFieldHeader()) > 0)
            {
                fieldHandled = false;
                if (fieldNumber < lastFieldNumber)
                {
                    lastFieldNumber = lastFieldIndex = 0;
                }
                for (int i = lastFieldIndex; i < fieldNumbers.Length; i++)
                {
                    if (fieldNumbers[i] == fieldNumber)
                    {
                        IProtoSerializer ser = serializers[i];
                        //Helpers.DebugWriteLine(": " + ser.ToString());
                        if (value == null && ser.ExpectedType == forType) value = CreateInstance(source);
                        if (ser.ReturnsValue) {
                            value = ser.Read(value, source);
                        } else { // pop
                            ser.Read(value, source);
                        }

                        lastFieldIndex = i;
                        lastFieldNumber = fieldNumber;
                        fieldHandled = true;
                        break;
                    }
                }
                if (!fieldHandled)
                {
                    //Helpers.DebugWriteLine(": [" + fieldNumber + "] (unknown)");
                    if (value == null) value = CreateInstance(source);
                    if (isExtensible) {
                        source.AppendExtensionData((IExtensible)value);
                    } else {
                        source.SkipField();
                    }
                }
            }
            //Helpers.DebugWriteLine("<< Reading fields for " + forType.FullName);
            if(value == null) value = CreateInstance(source);
            if (isRootType) { Callback(value, TypeModel.CallbackType.AfterDeserialize, source.Context); }
            return value;
        }