Ejemplo n.º 1
0
        private static object ReadObject(ProtoReader reader)
        {
            object obj = null;
            string str = reader.ReadString();

            if (str.IsNotEmpty())
            {
                IFormatter formatter = new BinaryFormatter();
                using (Stream stream = new MemoryStream(Convert.FromBase64String(reader.ReadString())))
                {
                    obj = formatter.Deserialize(stream);
                }
            }
            return(obj);
        }
Ejemplo n.º 2
0
        public object Read(object value, ProtoReader source)

        {
            Helpers.DebugAssert(value == null); // since replaces

            return(parse.Invoke(null, new object[] { source.ReadString() }));
        }
Ejemplo n.º 3
0
 public object Read(object value, ProtoReader source)
 {
     return this.parse.Invoke(null, new object[]
     {
         source.ReadString()
     });
 }
Ejemplo n.º 4
0
        private static Product Read(Product product1, ProtoReader reader1)
        {
            int num;

            while ((num = reader1.ReadFieldHeader()) > 0)
            {
                switch (num)
                {
                case 1:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.ProductID = reader1.ReadInt32();
                    continue;

                case 2:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.ProductName = reader1.ReadString();
                    continue;

                case 3:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.SupplierID = new int?(reader1.ReadInt32());
                    continue;
                }
            }
            return(product1);
        }
 // Token: 0x06000232 RID: 562 RVA: 0x00003610 File Offset: 0x00001810
 public object Read(object value, ProtoReader source)
 {
     return(this.parse.Invoke(null, new object[]
     {
         source.ReadString()
     }));
 }
            public void ShouldSerializeSuccessiveResultColumns()
            {
                // Arrange
                var dataSet = new DataSet();

                dataSet.Tables.Add(new DataTable());
                dataSet.Tables.Add(new DataTable());

                dataSet.Tables[0].Columns.Add("foo", typeof(int));
                dataSet.Tables[0].Rows.Add(1);

                var columnName = "bar";

                dataSet.Tables[1].Columns.Add(columnName, typeof(int));
                dataSet.Tables[1].Rows.Add(1);

                var dataReader = dataSet.CreateDataReader();

                var stream = new ProtoDataStream(dataReader);

                // Act
                var reader = new ProtoReader(this.CopyStream(stream), null, null);

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

                readerContext.ReadUntilResultEnd();
                readerContext.ReadUntilColumnName();

                Assert.Equal(columnName, reader.ReadString());
            }
            public void ShouldSerializeExpressionColumn()
            {
                // Arrange
                var columnName = "foo";
                var dataTable  = new DataTable();

                dataTable.Columns.Add(columnName, typeof(int));

                dataTable.Rows.Add(1);

                var dataReader = dataTable.CreateDataReader();

                dataReader.GetSchemaTable().Rows[0]["Expression"] = true;

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

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

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

                readerContext.ReadUntilColumnName();

                Assert.Equal(columnName, reader.ReadString());
            }
Ejemplo n.º 8
0
        public object Read(object value, ProtoReader source)

        {
            Helpers.DebugAssert(value == null); // since replaces

            return(source.ReadString());
        }
Ejemplo n.º 9
0
        public object Read(object value, ProtoReader source)
        {
            MethodInfo methodInfo = this.parse;

            object[] objArray = new object[] { source.ReadString() };
            return(methodInfo.Invoke(null, objArray));
        }
Ejemplo n.º 10
0
        public object Read(object value, ProtoReader source)
        {
            Helpers.DebugAssert(value == null); // since replaces
            var v = _parse.Invoke(null, new object[] { source.ReadString() });

            ProtoReader.NoteObject(v, source);
            return(v);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 public object Read(ProtoReader reader)
 {
     if (reader.WireType == WireType.Null)
     {
         return reader.ReadNull();
     }
     else
     {
         return reader.ReadString();
     }
 }
Ejemplo n.º 14
0
            public void ShouldSerializeCharArrayValue()
            {
                // Arrange
                var value      = new[] { 'f', 'o', 'o' };
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, reader.ReadString().ToCharArray());
            }
            public void ShouldSerializeField()
            {
                // Arrange
                var value      = "foo";
                var dataReader = this.CreateDataReader(value);

                var stream = new ProtoDataStream(dataReader);

                // Act
                var reader = new ProtoReader(this.CopyStream(stream), null, null);

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, reader.ReadString());
            }
            public void ShouldSerializeColumnName()
            {
                // Arrange
                var columnName = "foo";
                var dataTable  = new DataTable();

                dataTable.Columns.Add(columnName, typeof(int));

                dataTable.Rows.Add(1);

                var dataReader = dataTable.CreateDataReader();

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

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

                readerContext.ReadUntilColumnName();

                Assert.Equal(columnName, reader.ReadString());
            }
Ejemplo n.º 17
0
#pragma warning disable RCS1213
        private static Product Read(ref ProtoReader.State state, Product product1, ProtoReader reader1)
#pragma warning restore RCS1213
        {
            int num;

            while ((num = reader1.ReadFieldHeader(ref state)) > 0)
            {
                switch (num)
                {
                case 1:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.ProductID = reader1.ReadInt32(ref state);
                    continue;

                case 2:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.ProductName = reader1.ReadString(ref state);
                    continue;

                case 3:
                    if (product1 == null)
                    {
                        product1 = new Product();
                    }
                    product1.SupplierID = new int?(reader1.ReadInt32(ref state));
                    continue;
                }
            }
            return(product1);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// The read.
        /// </summary>
        /// <param name="compiledAsset">
        /// The compiled asset.
        /// </param>
        /// <param name="protoReader">
        /// The proto reader.
        /// </param>
        /// <returns>
        /// The <see cref="CompiledAsset"/>.
        /// </returns>
        private static CompiledAsset Read(CompiledAsset compiledAsset, ProtoReader protoReader)
        {
            int num;

            while ((num = protoReader.ReadFieldHeader()) > 0)
            {
                if (num != 1)
                {
                    if (num != 3)
                    {
                        if (compiledAsset == null)
                        {
                            var expr_8A = new CompiledAsset();
                            ProtoReader.NoteObject(expr_8A, protoReader);
                            compiledAsset = expr_8A;
                        }

                        protoReader.SkipField();
                    }
                    else
                    {
                        if (compiledAsset == null)
                        {
                            var expr_4C = new CompiledAsset();
                            ProtoReader.NoteObject(expr_4C, protoReader);
                            compiledAsset = expr_4C;
                        }

                        PlatformData arg_63_0 = compiledAsset.PlatformData;
                        SubItemToken token    = ProtoReader.StartSubItem(protoReader);
                        PlatformData arg_6F_0 = Read(arg_63_0, protoReader);
                        ProtoReader.EndSubItem(token, protoReader);
                        PlatformData platformData = arg_6F_0;
                        if (platformData != null)
                        {
                            compiledAsset.PlatformData = platformData;
                        }
                    }
                }
                else
                {
                    if (compiledAsset == null)
                    {
                        var expr_19 = new CompiledAsset();
                        ProtoReader.NoteObject(expr_19, protoReader);
                        compiledAsset = expr_19;
                    }

                    string text = protoReader.ReadString();
                    if (text != null)
                    {
                        compiledAsset.Loader = text;
                    }
                }
            }

            if (compiledAsset == null)
            {
                var expr_B2 = new CompiledAsset();
                ProtoReader.NoteObject(expr_B2, protoReader);
                compiledAsset = expr_B2;
            }

            return(compiledAsset);
        }
Ejemplo n.º 19
0
 public object Read(object value, ProtoReader source)
 {
     return source.ReadString();
 }
Ejemplo n.º 20
0
 public object Read(ProtoReader source, ref ProtoReader.State state, object value)
 {
     Helpers.DebugAssert(value == null); // since replaces
     return(parse.Invoke(null, new object[] { source.ReadString(ref state) }));
 }
Ejemplo n.º 21
0
        object BuildBody(uint realEMsg, Stream str)
        {
            EMsg eMsg = MsgUtil.GetMsg(realEMsg);

            if (eMsg == EMsg.ClientLogonGameServer)
            {
                eMsg = EMsg.ClientLogon; // temp hack for now
            }
            else if (eMsg == EMsg.ClientGamesPlayedWithDataBlob)
            {
                eMsg = EMsg.ClientGamesPlayed;
            }

            var protomsgType = typeof(CMClient).Assembly.GetTypes().ToList().Find(type =>
            {
                if (type.GetInterfaces().ToList().Find(inter => inter == typeof(IExtensible)) == null)
                {
                    return(false);
                }

                if (type.Name.EndsWith(eMsg.ToString()))
                {
                    return(true);
                }

                return(false);
            });

            if (protomsgType != null)
            {
                return(RuntimeTypeModel.Default.Deserialize(str, null, protomsgType));
            }

            // lets first find the type by checking all EMsgs we have
            var msgType = typeof(CMClient).Assembly.GetTypes().ToList().Find(type =>
            {
                if (type.GetInterfaces().ToList().Find(inter => inter == typeof(ISteamSerializableMessage)) == null)
                {
                    return(false);
                }

                var gcMsg = Activator.CreateInstance(type) as ISteamSerializableMessage;

                return(gcMsg.GetEMsg() == eMsg);
            });

            string eMsgName = eMsg.ToString();

            eMsgName = eMsgName.Replace("Econ", "").Replace("AM", "");

            // check name
            if (msgType == null)
            {
                msgType = GetSteamKitType(string.Format("SteamKit2.Msg{0}", eMsgName));
            }


            if (msgType != null)
            {
                var body = Activator.CreateInstance(msgType) as ISteamSerializableMessage;
                body.Deserialize(str);

                return(body);
            }

            msgType = GetSteamKitType(string.Format("SteamKit2.CMsg{0}", eMsgName));
            if (msgType != null)
            {
                return(Deserialize(msgType, str));
            }

            if (eMsg == EMsg.ClientToGC || eMsg == EMsg.ClientFromGC)
            {
                return(Serializer.Deserialize <CMsgGCClient>(str));
            }

            var gcMsgName = BuildEMsg(realEMsg);
            var gcMsgPossibleTypePrefixes = new[]
            {
                "SteamKit2.GC.Internal.CMsg",
                "SteamKit2.GC.Dota.Internal.CMsg",
                "SteamKit2.GC.CSGO.Internal.CMsg",
                "SteamKit2.GC.TF.Internal.CMsg",
            };

            var typeMsgName = gcMsgName
                              .Replace("GC", string.Empty)
                              .Replace("k_", string.Empty)
                              .Replace("ESOMsg", string.Empty)
                              .TrimStart('_')
                              .Replace("EMsg", string.Empty);


            if (typeMsgName == "Create" || typeMsgName == "Destroy" || typeMsgName == "Update")
            {
                typeMsgName = "SingleObject";
            }
            else if (typeMsgName == "Multiple")
            {
                typeMsgName = "MultipleObjects";
            }

            var possibleTypes = from type in typeof(CMClient).Assembly.GetTypes()
                                from typePrefix in gcMsgPossibleTypePrefixes
                                where type.GetInterfaces().Contains(typeof(IExtensible))
                                where type.FullName.StartsWith(typePrefix) && type.FullName.EndsWith(typeMsgName)
                                select type;

            foreach (var type in possibleTypes)
            {
                var streamPos = str.Position;
                try
                {
                    return(Deserialize(type, str));
                }
                catch (Exception)
                {
                    str.Position = streamPos;
                }
            }

            if (!MsgUtil.IsProtoBuf(realEMsg))
            {
                return(false);
            }

            // try reading it as a protobuf
            using (ProtoReader reader = new ProtoReader(str, null, null))
            {
                var fields = new Dictionary <int, List <object> >();

                while (true)
                {
                    int field = reader.ReadFieldHeader();

                    if (field == 0)
                    {
                        break;
                    }

                    object fieldValue = null;

                    switch (reader.WireType)
                    {
                    case WireType.Variant:
                    case WireType.Fixed32:
                    case WireType.Fixed64:
                    case WireType.SignedVariant:
                    {
                        try
                        {
                            fieldValue = reader.ReadInt64();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read Variant (debugme)";
                        }

                        break;
                    }

                    case WireType.String:
                    {
                        try
                        {
                            fieldValue = reader.ReadString();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read String (debugme)";
                        }

                        break;
                    }

                    default:
                    {
                        fieldValue = string.Format("{0} is not implemented", reader.WireType);
                        break;
                    }
                    }

                    if (!fields.ContainsKey(field))
                    {
                        fields[field] = new List <object>();
                    }

                    fields[field].Add(fieldValue);
                }

                if (fields.Count > 0)
                {
                    return(fields);
                }
            }

            return(null);
        }
Ejemplo n.º 22
0
        private void ReadColumn()
        {
            var    token = ProtoReader.StartSubItem(reader);
            int    field;
            string name          = null;
            var    protoDataType = (ProtoDataType)(-1);

            while ((field = reader.ReadFieldHeader()) != 0)
            {
                switch (field)
                {
                case 1:
                    name = reader.ReadString();
                    break;

                case 2:
                    protoDataType = (ProtoDataType)reader.ReadInt32();
                    break;

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

            switch (protoDataType)
            {
            case ProtoDataType.Int:
                colReaders.Add(() => reader.ReadInt32());
                break;

            case ProtoDataType.Short:
                colReaders.Add(() => reader.ReadInt16());
                break;

            case ProtoDataType.Decimal:
                colReaders.Add(() => BclHelpers.ReadDecimal(reader));
                break;

            case ProtoDataType.String:
                colReaders.Add(() => reader.ReadString());
                break;

            case ProtoDataType.Guid:
                colReaders.Add(() => BclHelpers.ReadGuid(reader));
                break;

            case ProtoDataType.DateTime:
                colReaders.Add(() => BclHelpers.ReadDateTime(reader));
                break;

            case ProtoDataType.Bool:
                colReaders.Add(() => reader.ReadBoolean());
                break;

            case ProtoDataType.Byte:
                colReaders.Add(() => reader.ReadByte());
                break;

            case ProtoDataType.Char:
                colReaders.Add(() => (char)reader.ReadInt16());
                break;

            case ProtoDataType.Double:
                colReaders.Add(() => reader.ReadDouble());
                break;

            case ProtoDataType.Float:
                colReaders.Add(() => reader.ReadSingle());
                break;

            case ProtoDataType.Long:
                colReaders.Add(() => reader.ReadInt64());
                break;

            case ProtoDataType.ByteArray:
                colReaders.Add(() => ProtoReader.AppendBytes(null, reader));
                break;

            case ProtoDataType.CharArray:
                colReaders.Add(() => reader.ReadString().ToCharArray());
                break;

            case ProtoDataType.TimeSpan:
                colReaders.Add(() => BclHelpers.ReadTimeSpan(reader));
                break;

            default:
                throw new NotSupportedException(protoDataType.ToString());
            }

            ProtoReader.EndSubItem(token, reader);
            dataTable.Columns.Add(name, ConvertProtoDataType.ToClrType(protoDataType));
        }
Ejemplo n.º 23
0
        public static Dictionary <int, List <object> > ReadProtobuf(Stream stream)
        {
            // try reading it as a protobuf
            using (var reader = new ProtoReader(stream, null, null))
            {
                var fields = new Dictionary <int, List <object> >();

                while (true)
                {
                    int field = reader.ReadFieldHeader();

                    if (field == 0)
                    {
                        break;
                    }

                    object fieldValue = null;

                    switch (reader.WireType)
                    {
                    case WireType.Variant:
                    case WireType.Fixed32:
                    case WireType.Fixed64:
                    case WireType.SignedVariant:
                    {
                        try
                        {
                            fieldValue = reader.ReadInt64();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read Variant (debugme)";
                        }

                        break;
                    }

                    case WireType.String:
                    {
                        try
                        {
                            fieldValue = reader.ReadString();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read String (debugme)";
                        }

                        break;
                    }

                    default:
                    {
                        fieldValue = string.Format("{0} is not implemented", reader.WireType);
                        break;
                    }
                    }

                    if (!fields.ContainsKey(field))
                    {
                        fields[field] = new List <object>();
                    }

                    fields[field].Add(fieldValue);
                }

                if (fields.Count > 0)
                {
                    return(fields);
                }
            }

            return(null);
        }
        /// <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;
        }
Ejemplo n.º 25
0
        internal bool TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, int tag, Type type, ref object value, bool skipOtherFields, bool asListItem, bool autoCreate, bool insideList)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Type          type2    = null;
            ProtoTypeCode typeCode = Helpers.GetTypeCode(type);
            int           modelKey;
            WireType      wireType = GetWireType(typeCode, format, ref type, out modelKey);
            bool          flag     = false;

            if (wireType == WireType.None)
            {
                type2 = GetListItemType(this, type);
                if (type2 == null && type.IsArray && type.GetArrayRank() == 1 && type != typeof(byte[]))
                {
                    type2 = type.GetElementType();
                }
                if (type2 != null)
                {
                    if (insideList)
                    {
                        throw CreateNestedListsNotSupported();
                    }
                    flag = TryDeserializeList(this, reader, format, tag, type, type2, ref value);
                    if (!flag && autoCreate)
                    {
                        value = CreateListInstance(type, type2);
                    }
                    return(flag);
                }
                ThrowUnexpectedType(type);
            }
            while (!flag || !asListItem)
            {
                int num = reader.ReadFieldHeader();
                if (num <= 0)
                {
                    break;
                }
                if (num != tag)
                {
                    if (skipOtherFields)
                    {
                        reader.SkipField();
                        continue;
                    }
                    throw ProtoReader.AddErrorData(new InvalidOperationException("Expected field " + tag.ToString() + ", but found " + num.ToString()), reader);
                }
                flag = true;
                reader.Hint(wireType);
                if (modelKey >= 0)
                {
                    if ((uint)(wireType - 2) <= 1u)
                    {
                        SubItemToken token = ProtoReader.StartSubItem(reader);
                        value = Deserialize(modelKey, value, reader);
                        ProtoReader.EndSubItem(token, reader);
                    }
                    else
                    {
                        value = Deserialize(modelKey, value, reader);
                    }
                    continue;
                }
                switch (typeCode)
                {
                case ProtoTypeCode.Int16:
                    value = reader.ReadInt16();
                    break;

                case ProtoTypeCode.Int32:
                    value = reader.ReadInt32();
                    break;

                case ProtoTypeCode.Int64:
                    value = reader.ReadInt64();
                    break;

                case ProtoTypeCode.UInt16:
                    value = reader.ReadUInt16();
                    break;

                case ProtoTypeCode.UInt32:
                    value = reader.ReadUInt32();
                    break;

                case ProtoTypeCode.UInt64:
                    value = reader.ReadUInt64();
                    break;

                case ProtoTypeCode.Boolean:
                    value = reader.ReadBoolean();
                    break;

                case ProtoTypeCode.SByte:
                    value = reader.ReadSByte();
                    break;

                case ProtoTypeCode.Byte:
                    value = reader.ReadByte();
                    break;

                case ProtoTypeCode.Char:
                    value = (char)reader.ReadUInt16();
                    break;

                case ProtoTypeCode.Double:
                    value = reader.ReadDouble();
                    break;

                case ProtoTypeCode.Single:
                    value = reader.ReadSingle();
                    break;

                case ProtoTypeCode.DateTime:
                    value = BclHelpers.ReadDateTime(reader);
                    break;

                case ProtoTypeCode.Decimal:
                    value = BclHelpers.ReadDecimal(reader);
                    break;

                case ProtoTypeCode.String:
                    value = reader.ReadString();
                    break;

                case ProtoTypeCode.ByteArray:
                    value = ProtoReader.AppendBytes((byte[])value, reader);
                    break;

                case ProtoTypeCode.TimeSpan:
                    value = BclHelpers.ReadTimeSpan(reader);
                    break;

                case ProtoTypeCode.Guid:
                    value = BclHelpers.ReadGuid(reader);
                    break;

                case ProtoTypeCode.Uri:
                    value = new Uri(reader.ReadString());
                    break;
                }
            }
            if (((!flag && !asListItem) & autoCreate) && type != typeof(string))
            {
                value = Activator.CreateInstance(type);
            }
            return(flag);
        }
Ejemplo n.º 26
0
 public object Read(object value, ProtoReader source)
 {
     return(source.ReadString());
 }
 public object Read(object value, ProtoReader source)
 {
     Helpers.DebugAssert(value == null); // since replaces
     return parse.Invoke(null, new object[] { source.ReadString() });
 }
Ejemplo n.º 28
0
        public static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[]      array  = null;
            Func <object> item   = null;
            Func <object> func2  = null;
            Func <object> func3  = null;
            Func <object> func4  = null;
            Func <object> func5  = null;
            Func <object> func6  = null;
            Func <object> func7  = null;
            Func <object> func8  = null;
            Func <object> func9  = null;
            Func <object> func10 = null;
            Func <object> func11 = null;

            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int num;
                List <Func <object> > list = new List <Func <object> >();
                while ((num = reader.ReadFieldHeader()) != 0)
                {
                    SubItemToken token;
                    string       str;
                    string       str2;
                    MappedType   type;
                    Type         type2;
                    switch (num)
                    {
                    case 1:
                    {
                        table.TableName = reader.ReadString();
                        continue;
                    }

                    case 2:
                        str   = null;
                        str2  = string.Empty;
                        type  = ~MappedType.Boolean;
                        token = ProtoReader.StartSubItem(reader);
                        goto Label_00F1;

                    case 3:
                        if (array != null)
                        {
                            goto Label_0382;
                        }
                        array = new object[table.Columns.Count];
                        goto Label_038C;

                    default:
                        goto Label_03F3;
                    }
Label_009B:
                    switch (num)
                    {
                    case 1:
                        str = reader.ReadString();
                        break;

                    case 2:
                        type = (MappedType)reader.ReadInt32();
                        break;

                    case 3:
                        str2 = reader.ReadString();
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
Label_00F1:
                    if ((num = reader.ReadFieldHeader()) != 0)
                    {
                        goto Label_009B;
                    }
                    switch (type)
                    {
                    case MappedType.Boolean:
                        type2 = typeof(bool);
                        if (item == null)
                        {
                            item = () => reader.ReadBoolean();
                        }
                        list.Add(item);
                        break;

                    case MappedType.Byte:
                        type2 = typeof(byte[]);
                        if (func2 == null)
                        {
                            func2 = () => ReadBytes(reader);
                        }
                        list.Add(func2);
                        break;

                    case MappedType.Double:
                        type2 = typeof(double);
                        if (func3 == null)
                        {
                            func3 = () => reader.ReadDouble();
                        }
                        list.Add(func3);
                        break;

                    case MappedType.Int16:
                        type2 = typeof(short);
                        if (func6 == null)
                        {
                            func6 = () => reader.ReadInt16();
                        }
                        list.Add(func6);
                        break;

                    case MappedType.Int32:
                        type2 = typeof(int);
                        if (func5 == null)
                        {
                            func5 = () => reader.ReadInt32();
                        }
                        list.Add(func5);
                        break;

                    case MappedType.Int64:
                        type2 = typeof(long);
                        if (func4 == null)
                        {
                            func4 = () => reader.ReadInt64();
                        }
                        list.Add(func4);
                        break;

                    case MappedType.String:
                        type2 = typeof(string);
                        if (func8 == null)
                        {
                            func8 = () => reader.ReadString();
                        }
                        list.Add(func8);
                        break;

                    case MappedType.Decimal:
                        type2 = typeof(decimal);
                        if (func7 == null)
                        {
                            func7 = () => BclHelpers.ReadDecimal(reader);
                        }
                        list.Add(func7);
                        break;

                    case MappedType.Guid:
                        type2 = typeof(Guid);
                        if (func9 == null)
                        {
                            func9 = () => BclHelpers.ReadGuid(reader);
                        }
                        list.Add(func9);
                        break;

                    case MappedType.DateTime:
                        type2 = typeof(DateTime);
                        if (func10 == null)
                        {
                            func10 = () => BclHelpers.ReadDateTime(reader);
                        }
                        list.Add(func10);
                        break;

                    case MappedType.TimeSpan:
                        type2 = typeof(TimeSpan);
                        if (func11 == null)
                        {
                            func11 = () => BclHelpers.ReadTimeSpan(reader);
                        }
                        list.Add(func11);
                        break;

                    default:
                        throw new NotSupportedException(type.ToString());
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Columns.Add(str, type2);
                    if (!string.IsNullOrEmpty(str2))
                    {
                        table.Columns[str].Caption = str2;
                    }
                    array = null;
                    continue;
Label_0382:
                    Array.Clear(array, 0, array.Length);
Label_038C:
                    token = ProtoReader.StartSubItem(reader);
                    while ((num = reader.ReadFieldHeader()) != 0)
                    {
                        if (num > array.Length)
                        {
                            reader.SkipField();
                        }
                        else
                        {
                            int index = num - 1;
                            array[index] = list[index]();
                        }
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Rows.Add(array);
                    continue;
Label_03F3:
                    reader.SkipField();
                }
            }
            return(table);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Reads an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
        /// </summary>
        public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
        {
            SubItemToken token = ProtoReader.StartSubItem(source);
            int          fieldNumber;
            int          newObjectKey = -1, newTypeKey = -1, tmp;

            while ((fieldNumber = source.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case FieldExistingObjectKey:
                    tmp   = source.ReadInt32();
                    value = source.NetCache.GetKeyedObject(tmp);
                    break;

                case FieldNewObjectKey:
                    newObjectKey = source.ReadInt32();
                    break;

                case FieldExistingTypeKey:
                    tmp  = source.ReadInt32();
                    type = (Type)source.NetCache.GetKeyedObject(tmp);
                    key  = source.GetTypeKey(ref type);
                    break;

                case FieldNewTypeKey:
                    newTypeKey = source.ReadInt32();
                    break;

                case FieldTypeName:
                    string typeName = source.ReadString();
                    type = source.DeserializeType(typeName);
                    if (type == null)
                    {
                        throw new ProtoException("Unable to resolve type: " + typeName + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
                    }
                    if (type == typeof(string))
                    {
                        key = -1;
                    }
                    else
                    {
                        key = source.GetTypeKey(ref type);
                        if (key < 0)
                        {
                            throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                        }
                    }
                    break;

                case FieldObject:
                    bool isString = type == typeof(string);
                    bool wasNull  = value == null;
                    bool lateSet  = wasNull && (isString || ((options & NetObjectOptions.LateSet) != 0));

                    if (newObjectKey >= 0 && !lateSet)
                    {
                        if (value == null)
                        {
                            source.TrapNextObject(newObjectKey);
                        }
                        else
                        {
                            source.NetCache.SetKeyedObject(newObjectKey, value);
                        }
                        if (newTypeKey >= 0)
                        {
                            source.NetCache.SetKeyedObject(newTypeKey, type);
                        }
                    }
                    object oldValue = value;
                    if (isString)
                    {
                        value = source.ReadString();
                    }
                    else
                    {
                        value = ProtoReader.ReadTypedObject(oldValue, key, source, type);
                    }

                    if (newObjectKey >= 0)
                    {
                        if (wasNull && !lateSet)
                        {     // this both ensures (via exception) that it *was* set, and makes sure we don't shout
                            // about changed references
                            oldValue = source.NetCache.GetKeyedObject(newObjectKey);
                        }
                        if (lateSet)
                        {
                            source.NetCache.SetKeyedObject(newObjectKey, value);
                            if (newTypeKey >= 0)
                            {
                                source.NetCache.SetKeyedObject(newTypeKey, type);
                            }
                        }
                    }
                    if (newObjectKey >= 0 && !lateSet && !ReferenceEquals(oldValue, value))
                    {
                        throw new ProtoException("A reference-tracked object changed reference during deserialization");
                    }
                    if (newObjectKey < 0 && newTypeKey >= 0)
                    {      // have a new type, but not a new object
                        source.NetCache.SetKeyedObject(newTypeKey, type);
                    }
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }
            if (newObjectKey >= 0 && (options & NetObjectOptions.AsReference) == 0)
            {
                throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
            }
            ProtoReader.EndSubItem(token, source);

            return(value);
        }
Ejemplo n.º 30
0
 private static byte[] ReadBytes(ProtoReader reader)
 {
     return(Convert.FromBase64String(reader.ReadString()));
 }
 public object Read(object value, ProtoReader source)
 {
     Helpers.DebugAssert(value == null); // since replaces
     return source.ReadString();
 }
Ejemplo n.º 32
0
        private static void Merge(ProtoReader reader, ref ProtoReader.State state, ref protogen.Order obj)
        {
            SubItemToken tok;
            int          field;

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

                case 2:
                    obj.CustomerID = reader.ReadString(ref state);
                    break;

                case 3:
                    obj.EmployeeID = reader.ReadInt32(ref state);
                    break;

                case 4:
                    obj.OrderDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 5:
                    obj.RequiredDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 6:
                    obj.ShippedDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 7:
                    obj.ShipVia = reader.ReadInt32(ref state);
                    break;

                case 8:
                    obj.Freight = reader.ReadDouble(ref state);
                    break;

                case 9:
                    obj.ShipName = reader.ReadString(ref state);
                    break;

                case 10:
                    obj.ShipAddress = reader.ReadString(ref state);
                    break;

                case 11:
                    obj.ShipCity = reader.ReadString(ref state);
                    break;

                case 12:
                    obj.ShipRegion = reader.ReadString(ref state);
                    break;

                case 13:
                    obj.ShipPostalCode = reader.ReadString(ref state);
                    break;

                case 14:
                    obj.ShipCountry = reader.ReadString(ref state);
                    break;

                case 15:
                    do
                    {
                        protogen.OrderLine _15 = default;
                        tok = ProtoReader.StartSubItem(reader, ref state);
                        Merge(reader, ref state, ref _15);
                        ProtoReader.EndSubItem(tok, reader, ref state);
                        obj.Lines.Add(_15);
                    } while (reader.TryReadFieldHeader(ref state, 1));
                    break;

                default:
                    reader.AppendExtensionData(ref state, obj);
                    break;
                }
            }
        }
Ejemplo n.º 33
0
        object BuildBody(uint realEMsg, Stream str, uint gcAppid = 0)
        {
            EMsg eMsg = MsgUtil.GetMsg(realEMsg);

            if (eMsg == EMsg.ClientLogonGameServer)
            {
                eMsg = EMsg.ClientLogon; // temp hack for now
            }
            else if (eMsg == EMsg.ClientGamesPlayedWithDataBlob)
            {
                eMsg = EMsg.ClientGamesPlayed;
            }

            var protomsgType = GetMessageBodyType(realEMsg);

            if (protomsgType != null)
            {
                return(RuntimeTypeModel.Default.Deserialize(str, null, protomsgType));
            }

            // lets first find the type by checking all EMsgs we have
            var msgType = typeof(CMClient).Assembly.GetTypes().ToList().Find(type =>
            {
                if (type.GetInterfaces().ToList().Find(inter => inter == typeof(ISteamSerializableMessage)) == null)
                {
                    return(false);
                }

                var gcMsg = Activator.CreateInstance(type) as ISteamSerializableMessage;

                return(gcMsg.GetEMsg() == eMsg);
            });

            string eMsgName = eMsg.ToString();

            eMsgName = eMsgName.Replace("Econ", "").Replace("AM", "");

            // check name
            if (msgType == null)
            {
                msgType = GetSteamKitType(string.Format("SteamKit2.Msg{0}", eMsgName));
            }


            if (msgType != null)
            {
                var body = Activator.CreateInstance(msgType) as ISteamSerializableMessage;
                body.Deserialize(str);

                return(body);
            }

            msgType = GetSteamKitType(string.Format("SteamKit2.CMsg{0}", eMsgName));
            if (msgType != null)
            {
                return(Deserialize(msgType, str));
            }

            if (eMsg == EMsg.ClientToGC || eMsg == EMsg.ClientFromGC)
            {
                return(Serializer.Deserialize <CMsgGCClient>(str));
            }

            foreach (var type in GetGCMessageBodyTypeCandidates(realEMsg, gcAppid))
            {
                var streamPos = str.Position;
                try
                {
                    return(Deserialize(type, str));
                }
                catch (Exception)
                {
                    str.Position = streamPos;
                }
            }

            if (!MsgUtil.IsProtoBuf(realEMsg))
            {
                return(null);
            }

            // try reading it as a protobuf
            using (ProtoReader reader = new ProtoReader(str, null, null))
            {
                var fields = new Dictionary <int, List <object> >();

                while (true)
                {
                    int field = reader.ReadFieldHeader();

                    if (field == 0)
                    {
                        break;
                    }

                    object fieldValue = null;

                    switch (reader.WireType)
                    {
                    case WireType.Variant:
                    case WireType.Fixed32:
                    case WireType.Fixed64:
                    case WireType.SignedVariant:
                    {
                        try
                        {
                            fieldValue = reader.ReadInt64();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read Variant (debugme)";
                        }

                        break;
                    }

                    case WireType.String:
                    {
                        try
                        {
                            fieldValue = reader.ReadString();
                        }
                        catch (Exception)
                        {
                            fieldValue = "Unable to read String (debugme)";
                        }

                        break;
                    }

                    default:
                    {
                        fieldValue = string.Format("{0} is not implemented", reader.WireType);
                        break;
                    }
                    }

                    if (!fields.ContainsKey(field))
                    {
                        fields[field] = new List <object>();
                    }

                    fields[field].Add(fieldValue);
                }

                if (fields.Count > 0)
                {
                    return(fields);
                }
            }

            return(null);
        }
Ejemplo n.º 34
0
        static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[] values = null;
            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int field;
                List <Func <object> > colReaders = new List <Func <object> >();
                SubItemToken          token;
                while ((field = reader.ReadFieldHeader()) != 0)
                {
                    switch (field)
                    {
                    case 1:
                        table.TableName = reader.ReadString();
                        break;

                    case 2:
                        string     name       = null;
                        MappedType mappedType = (MappedType)(-1);
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            switch (field)
                            {
                            case 1:
                                name = reader.ReadString();
                                break;

                            case 2:
                                mappedType = (MappedType)reader.ReadInt32();
                                break;

                            default:
                                reader.SkipField();
                                break;
                            }
                        }
                        Type type;
                        switch (mappedType)
                        {
                        case MappedType.Int32:
                            type = typeof(int);
                            colReaders.Add(() => reader.ReadInt32());
                            break;

                        case MappedType.Int16:
                            type = typeof(short);
                            colReaders.Add(() => reader.ReadInt16());
                            break;

                        case MappedType.Decimal:
                            type = typeof(decimal);
                            colReaders.Add(() => BclHelpers.ReadDecimal(reader));
                            break;

                        case MappedType.String:
                            type = typeof(string);
                            colReaders.Add(() => reader.ReadString());
                            break;

                        case MappedType.Guid:
                            type = typeof(Guid);
                            colReaders.Add(() => BclHelpers.ReadGuid(reader));
                            break;

                        case MappedType.DateTime:
                            type = typeof(DateTime);
                            colReaders.Add(() => BclHelpers.ReadDateTime(reader));
                            break;

                        default:
                            throw new NotSupportedException(mappedType.ToString());
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Columns.Add(name, type);
                        values = null;
                        break;

                    case 3:
                        if (values == null)
                        {
                            values = new object[table.Columns.Count];
                        }
                        else
                        {
                            Array.Clear(values, 0, values.Length);
                        }
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            if (field > values.Length)
                            {
                                reader.SkipField();
                            }
                            else
                            {
                                int i = field - 1;
                                values[i] = colReaders[i]();
                            }
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Rows.Add(values);
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
                }
            }
            return(table);
        }
Ejemplo n.º 35
0
        /// <summary>
        /// 解析protobuf数据
        /// </summary>
        /// <param name="reader"></param>
        /// <see cref="https://developers.google.com/protocol-buffers/docs/encoding"/>
        /// <returns>全部成功返回true,部分失败或全失败返回false,这时候可以通过LastError获取失败信息</returns>
        public bool Parse(ProtoReader reader)
        {
            buildIndex();
            lastError.Clear();

            try {
                int          field_id;
                SubItemToken token;
                while ((field_id = reader.ReadFieldHeader()) != 0)
                {
                    WireType pb_type = reader.WireType;

                    FieldDescriptorProto desc;
                    if (false == msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc))
                    {
                        // unknown field skipped
                        reader.SkipField();
                        continue;
                    }

                    // 类型校验
                    try {
                        switch (desc.type)
                        {
                        case FieldDescriptorProto.Type.TYPE_DOUBLE:
                            insertField(desc, reader.ReadDouble());
                            break;

                        case FieldDescriptorProto.Type.TYPE_FLOAT:
                            insertField(desc, reader.ReadSingle());
                            break;

                        case FieldDescriptorProto.Type.TYPE_INT64:
                        case FieldDescriptorProto.Type.TYPE_SINT64:
                        case FieldDescriptorProto.Type.TYPE_SFIXED64:
                            insertField(desc, reader.ReadInt64());
                            break;

                        case FieldDescriptorProto.Type.TYPE_UINT64:
                        case FieldDescriptorProto.Type.TYPE_FIXED64:
                            insertField(desc, reader.ReadUInt64());
                            break;

                        case FieldDescriptorProto.Type.TYPE_INT32:
                        case FieldDescriptorProto.Type.TYPE_SINT32:
                        case FieldDescriptorProto.Type.TYPE_SFIXED32:
                            insertField(desc, reader.ReadInt32());
                            break;

                        case FieldDescriptorProto.Type.TYPE_BOOL:
                            insertField(desc, reader.ReadBoolean());
                            break;

                        case FieldDescriptorProto.Type.TYPE_STRING:
                            insertField(desc, reader.ReadString());
                            break;

                        //case FieldDescriptorProto.Type.TYPE_GROUP: // deprecated
                        //    break;
                        case FieldDescriptorProto.Type.TYPE_MESSAGE:
                            token = ProtoReader.StartSubItem(reader);
                            try {
                                DynamicMessage sub_msg = CreateMessage(desc);
                                if (null == sub_msg)
                                {
                                    lastError.AddLast(string.Format("{0}.{1}.{2} => invalid message path {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, pickMsgName(desc)));
                                    break;
                                }
                                if (false == sub_msg.Parse(reader))
                                {
                                    lastError.AddLast(sub_msg.LastError);
                                }
                                else
                                {
                                    insertField(desc, sub_msg);
                                }
                            } catch (Exception e) {
                                lastError.AddLast(string.Format("{0}.{1}.{2} => {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.Message));
                            }
                            ProtoReader.EndSubItem(token, reader);
                            break;

                        case FieldDescriptorProto.Type.TYPE_BYTES:
                            insertField(desc, ProtoReader.AppendBytes(null, reader));
                            break;

                        case FieldDescriptorProto.Type.TYPE_FIXED32:
                        case FieldDescriptorProto.Type.TYPE_UINT32:
                            insertField(desc, reader.ReadUInt32());
                            break;

                        case FieldDescriptorProto.Type.TYPE_ENUM:
                            insertField(desc, reader.ReadInt32());
                            break;

                        default:
                            // unsupported field
                            lastError.AddLast(string.Format("field type {0} in {1}.{2}.{3} unsupported", desc.type.ToString(), msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name));
                            reader.SkipField();
                            break;
                        }
                    } catch (Exception e) {
                        lastError.AddLast(string.Format("{0}.{1}.{2} {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.ToString()));
                        reader.SkipField();
                    }
                }
            } catch (Exception e) {
                lastError.AddLast(e.Message);
            }

            return(0 == lastError.Count);
        }
Ejemplo n.º 36
0
 public object Read(ProtoReader reader)
 {
     return new Uri(reader.ReadString());
 }
Ejemplo n.º 37
0
        /// <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);
        }