public object Read(object value, ProtoReader source)
        {
            var result = ProtoReader.AppendBytes(_overwriteList ? null : (byte[])value, source);

            if (_overwriteList || value == null)
            {
                ProtoReader.NoteObject(result, source);
            }
            return(result);
        }
 public object Read(object value, ProtoReader source)
 {
     byte[] numArray;
     if (this.overwriteList)
     {
         numArray = null;
     }
     else
     {
         numArray = (byte[])value;
     }
     return(ProtoReader.AppendBytes(numArray, source));
 }
Exemple #3
0
            public void ShouldSerializeByteArrayValue()
            {
                // Arrange
                var value      = new[] { (byte)42, (byte)42 };
                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, ProtoReader.AppendBytes(null, reader));
            }
        public void ShouldSerializeByteArrayValue()
        {
            // Arrange
            var value      = new[] { (byte)42, (byte)42 };
            var dataReader = this.CreateDataReader(value);

            // Act
            this.writer.Serialize(this.stream, dataReader);

            // Assert
            this.stream.Position = 0;

            this.ReadUntilFieldValue();

            Assert.Equal(value, ProtoReader.AppendBytes(null, this.reader));
        }
        /// <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);
        }
        /// <summary>
        /// The read.
        /// </summary>
        /// <param name="platformData">
        /// The platform data.
        /// </param>
        /// <param name="protoReader">
        /// The proto reader.
        /// </param>
        /// <returns>
        /// The <see cref="PlatformData"/>.
        /// </returns>
        private static PlatformData Read(PlatformData platformData, ProtoReader protoReader)
        {
            int num;

            while ((num = protoReader.ReadFieldHeader()) > 0)
            {
                if (num != 1)
                {
                    if (num != 2)
                    {
                        if (platformData == null)
                        {
                            var expr_164 = new PlatformData();
                            ProtoReader.NoteObject(expr_164, protoReader);
                            platformData = expr_164;
                        }

                        protoReader.SkipField();
                    }
                    else
                    {
                        if (platformData == null)
                        {
                            var expr_134 = new PlatformData();
                            ProtoReader.NoteObject(expr_134, protoReader);
                            platformData = expr_134;
                        }

                        byte[] array = ProtoReader.AppendBytes(platformData.Data, protoReader);
                        if (array != null)
                        {
                            platformData.Data = array;
                        }
                    }
                }
                else
                {
                    if (platformData == null)
                    {
                        var expr_19 = new PlatformData();
                        ProtoReader.NoteObject(expr_19, protoReader);
                        platformData = expr_19;
                    }

                    int num2           = protoReader.ReadInt32();
                    var targetPlatform = TargetPlatform.Windows;
                    if (num2 != 0)
                    {
                        if (num2 != 1)
                        {
                            if (num2 != 2)
                            {
                                if (num2 != 3)
                                {
                                    if (num2 != 4)
                                    {
                                        if (num2 != 5)
                                        {
                                            if (num2 != 6)
                                            {
                                                if (num2 != 7)
                                                {
                                                    if (num2 != 8)
                                                    {
                                                        if (num2 != 9)
                                                        {
                                                            if (num2 != 10)
                                                            {
                                                                if (num2 != 11)
                                                                {
                                                                    if (num2 != 12)
                                                                    {
                                                                        protoReader.ThrowEnumException(
                                                                            typeof(TargetPlatform),
                                                                            num2);
                                                                    }
                                                                    else
                                                                    {
                                                                        targetPlatform = TargetPlatform.RaspberryPi;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    targetPlatform = TargetPlatform.WindowsPhone8;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                targetPlatform = TargetPlatform.PlayStationMobile;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            targetPlatform = TargetPlatform.Ouya;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        targetPlatform = TargetPlatform.NativeClient;
                                                    }
                                                }
                                                else
                                                {
                                                    targetPlatform = TargetPlatform.WindowsStoreApp;
                                                }
                                            }
                                            else
                                            {
                                                targetPlatform = TargetPlatform.MacOSX;
                                            }
                                        }
                                        else
                                        {
                                            targetPlatform = TargetPlatform.Linux;
                                        }
                                    }
                                    else
                                    {
                                        targetPlatform = TargetPlatform.Android;
                                    }
                                }
                                else
                                {
                                    targetPlatform = TargetPlatform.iOS;
                                }
                            }
                            else
                            {
                                targetPlatform = TargetPlatform.WindowsPhone;
                            }
                        }
                        else
                        {
                            targetPlatform = TargetPlatform.Xbox360;
                        }
                    }
                    else
                    {
                        targetPlatform = TargetPlatform.Windows;
                    }

                    platformData.Platform = targetPlatform;
                }
            }

            if (platformData == null)
            {
                var expr_18C = new PlatformData();
                ProtoReader.NoteObject(expr_18C, protoReader);
                platformData = expr_18C;
            }

            return(platformData);
        }
 public object Read(object value, ProtoReader source)
 {
     return(ProtoReader.AppendBytes((byte[])value, source));
 }
        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));
        }
Exemple #9
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);
        }
 object ReadField(ProtoReader reader, Type memberT, string sClassName, CLS_Environment environment)
 {
     if (memberT == typeof(int))
     {
         return(reader.ReadInt32());
     }
     else if (memberT == typeof(uint))
     {
         return(reader.ReadUInt32());
     }
     else if (memberT == typeof(bool))
     {
         return(reader.ReadBoolean());
     }
     else if (memberT == typeof(byte))
     {
         return(reader.ReadByte());
     }
     else if (memberT == typeof(sbyte))
     {
         return(reader.ReadSByte());
     }
     else if (memberT == typeof(float))
     {
         return(reader.ReadSingle());
     }
     else if (memberT == typeof(double))
     {
         return(reader.ReadDouble());
     }
     else if (memberT == typeof(short))
     {
         return(reader.ReadInt16());
     }
     else if (memberT == typeof(ushort))
     {
         return(reader.ReadUInt16());
     }
     else if (memberT == typeof(long))
     {
         return(reader.ReadInt64());
     }
     else if (memberT == typeof(ulong))
     {
         return(reader.ReadUInt64());
     }
     else if (memberT == typeof(string))
     {
         return(reader.ReadString());
     }
     else if (memberT == typeof(byte[]))
     {
         return(ProtoReader.AppendBytes(null, reader));
     }
     else if (memberT == typeof(SInstance))
     {
         SubItemToken   st     = ProtoReader.StartSubItem(reader);
         CLS_Type_Class sClass = environment.GetTypeByKeywordQuiet(sClassName) as CLS_Type_Class;
         if (!sClass.compiled)
         {
             RuntimeCompilerClass(sClassName);
         }
         CLS_Content       content = CLS_Content.NewContent(environment);
         CLS_Content.Value retVal  = sClass.function.New(content, m_emptyParams);
         CLS_Content.PoolContent(content);
         SInstance sInstance = (SInstance)retVal.value;
         ReadSInstance(reader, sInstance, environment);
         ProtoReader.EndSubItem(st, reader);
         return(sInstance);
     }
     else
     {
         throw new NotImplementedException("未实现类型: " + memberT);
     }
 }
Exemple #11
0
 public object Read(object value, ProtoReader source)
 {
     return(ProtoReader.AppendBytes(overwriteList ? null : (byte[])value, source));
 }
Exemple #12
0
        public static TreeNodeStruct QuickDecodeProto(byte[] bytesIn, string RootKey, TreeNodeStruct NodeStruct)
        {
            var NodeList = new List <TreeNode>();

            if (!string.IsNullOrEmpty(RootKey))
            {
                NodeList.Add(NodeStruct.parentNode);
                var        NodeCollection = NodeStruct.parentNode.Nodes;
                TreeNode[] Nodes          = NodeCollection.Find(RootKey, true);
                if (Nodes.Count() > 0)
                {
                    NodeStruct.parentNode = Nodes[0];
                }
            }

            ProtoReader reader = null;

            using (var ms = new MemoryStream(bytesIn))
            {
                reader = ProtoReader.Create(ms, null, null);
            }
            long start = reader.Position;
            int  field = reader.ReadFieldHeader();

            try
            {
                while (field > 0)
                {
                    long payloadStart = reader.Position;
                    switch (reader.WireType)
                    {
                    case WireType.Varint:
                    {
                        var val = reader.ReadInt64();
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (Varint) " + "Value=" + val.ToString() + " Hex=" + BitConverter.ToString(BitConverter.GetBytes(val).Reverse().ToArray()).Replace("-", " ").Replace("00 00 00", "").Trim().TrimStart('0');
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    case WireType.Fixed32:
                    {
                        var val = reader.ReadInt32();
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (Fixed32) " + "Value=" + val.ToString() + " Hex=" + BitConverter.ToString(BitConverter.GetBytes(val).Reverse().ToArray()).Replace("-", " ").Replace("00 00 00", "").Trim().TrimStart('0');
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    case WireType.Fixed64:
                    {
                        var val = reader.ReadInt64();
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (Fixed32) " + "Value=" + val.ToString() + " Hex=" + BitConverter.ToString(BitConverter.GetBytes(val).Reverse().ToArray()).Replace("-", " ").Replace("00 00 00", "").Trim().TrimStart('0');
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    case WireType.String:
                    {
                        var payloadBytes = ProtoReader.AppendBytes(null, reader);
                        using (var subReader = ReadProto(payloadBytes))
                        {
                            if (subReader != null)
                            {
                                var RandKey = (new Random()).Next().ToString();
                                var key     = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (String-SubProto) " + "Length=" + payloadBytes.Length.ToString();
                                NodeStruct.parentNode.Nodes.Add(key + RandKey, key);
                                QuickDecodeProto(payloadBytes, key + RandKey, NodeStruct);
                            }
                            else
                            {
                                var str = Encoding.UTF8.GetString(payloadBytes).Replace("\0", "").Replace("\n", "").Replace("\r\n", "").Replace("\r", "").Replace("\b", "").Replace("\f", "").Replace("\v", "");
                                var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (String) " + "Length=" + payloadBytes.Length.ToString() + " UTF8 =" + ((payloadBytes.Length == 0) ? "\"\"" : str);
                                NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                            }
                        }
                        break;
                    }

                    case WireType.None:
                    {
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (None) " + "Value=0";
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    case WireType.StartGroup:
                    {
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (StartGroup) " + "Value=StartGroup";
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    case WireType.EndGroup:
                    {
                        var key = "Field #" + reader.FieldNumber.ToString() + " [" + bytesIn[start].ToString("x2").ToUpper() + "] (EndGroup) " + "Value=EndGroup";
                        NodeStruct.parentNode.Nodes.Add(new TreeNode(key));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    start = reader.Position;
                    field = reader.ReadFieldHeader();
                }
            }
            catch (Exception ex)
            {
                bytesIn = bytesIn.Skip(1).ToArray();
                QuickDecodeProto(bytesIn, "", NodeStruct);
            }
            return(NodeStruct);
        }
 public object Read(ProtoReader source, ref ProtoReader.State state, object value)
 {
     return(ProtoReader.AppendBytes(overwriteList ? null : (byte[])value, source, ref state));
 }
        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);
        }
Exemple #15
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");
            }
            ProtoTypeCode typecode = Helpers.GetTypeCode(type);
            int           modelKey;
            WireType      wiretype = this.GetWireType(typecode, format, ref type, out modelKey);
            bool          found    = false;

            if (wiretype == WireType.None)
            {
                Type itemType = TypeModel.GetListItemType(this, type);
                if (itemType == null && type.IsArray && type.GetArrayRank() == 1 && type != typeof(byte[]))
                {
                    itemType = type.GetElementType();
                }
                if (itemType != null)
                {
                    if (insideList)
                    {
                        throw TypeModel.CreateNestedListsNotSupported();
                    }
                    found = this.TryDeserializeList(this, reader, format, tag, type, itemType, ref value);
                    if (!found && autoCreate)
                    {
                        value = TypeModel.CreateListInstance(type, itemType);
                    }
                    return(found);
                }
                else
                {
                    TypeModel.ThrowUnexpectedType(type);
                }
            }
            while (!found || !asListItem)
            {
                int fieldNumber = reader.ReadFieldHeader();
                if (fieldNumber <= 0)
                {
                    break;
                }
                if (fieldNumber != tag)
                {
                    if (!skipOtherFields)
                    {
                        throw ProtoReader.AddErrorData(new InvalidOperationException("Expected field " + tag.ToString() + ", but found " + fieldNumber.ToString()), reader);
                    }
                    reader.SkipField();
                }
                else
                {
                    found = true;
                    reader.Hint(wiretype);
                    if (modelKey >= 0)
                    {
                        switch (wiretype)
                        {
                        case WireType.String:
                        case WireType.StartGroup:
                        {
                            SubItemToken token = ProtoReader.StartSubItem(reader);
                            value = this.Deserialize(modelKey, value, reader);
                            ProtoReader.EndSubItem(token, reader);
                            break;
                        }

                        default:
                            value = this.Deserialize(modelKey, value, reader);
                            break;
                        }
                    }
                    else
                    {
                        ProtoTypeCode protoTypeCode = typecode;
                        switch (protoTypeCode)
                        {
                        case ProtoTypeCode.Boolean:
                            value = reader.ReadBoolean();
                            break;

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

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

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

                        case ProtoTypeCode.Int16:
                            value = reader.ReadInt16();
                            break;

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

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

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

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

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

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

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

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

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

                        case (ProtoTypeCode)17:
                            break;

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

                        default:
                            switch (protoTypeCode)
                            {
                            case ProtoTypeCode.TimeSpan:
                                value = BclHelpers.ReadTimeSpan(reader);
                                break;

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

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

                            case ProtoTypeCode.Uri:
                                value = new Uri(reader.ReadString());
                                break;
                            }
                            break;
                        }
                    }
                }
            }
            if (!found && !asListItem && autoCreate && type != typeof(string))
            {
                value = Activator.CreateInstance(type);
            }
            return(found);
        }
Exemple #16
0
 // Token: 0x0600351D RID: 13597 RVA: 0x001336BA File Offset: 0x00131ABA
 public object Read(object value, ProtoReader source)
 {
     return(ProtoReader.AppendBytes((!overwriteList) ? ((byte[])value) : null, source));
 }
        private static void ReadRecordValues(ProtoReaderContext context)
        {
            while (context.ReadFieldHeader() != NoneFieldHeader)
            {
                // Backwards compatibility or unnecessary?
                if (context.CurrentFieldHeader > context.Buffers.Length)
                {
                    context.Reader.SkipField();

                    continue;
                }

                var columnIndex = context.CurrentFieldHeader - 1;

                switch (context.Columns[columnIndex].ProtoDataType)
                {
                case ProtoDataType.String:
                    context.Buffers[columnIndex].String = context.Reader.ReadString();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.DateTime:
                    context.Buffers[columnIndex].DateTime = BclHelpers.ReadDateTime(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;

                case ProtoDataType.Int:
                    context.Buffers[columnIndex].Int    = context.Reader.ReadInt32();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Long:
                    context.Buffers[columnIndex].Long   = context.Reader.ReadInt64();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Short:
                    context.Buffers[columnIndex].Short  = context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Bool:
                    context.Buffers[columnIndex].Bool   = context.Reader.ReadBoolean();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Byte:
                    context.Buffers[columnIndex].Byte   = context.Reader.ReadByte();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Float:
                    context.Buffers[columnIndex].Float  = context.Reader.ReadSingle();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Double:
                    context.Buffers[columnIndex].Double = context.Reader.ReadDouble();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Guid:
                    context.Buffers[columnIndex].Guid   = BclHelpers.ReadGuid(context.Reader);
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Char:
                    context.Buffers[columnIndex].Char   = (char)context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Decimal:
                    context.Buffers[columnIndex].Decimal = BclHelpers.ReadDecimal(context.Reader);
                    context.Buffers[columnIndex].IsNull  = false;
                    break;

                case ProtoDataType.ByteArray:
                    context.Buffers[columnIndex].ByteArray = ProtoReader.AppendBytes(null, context.Reader);
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.CharArray:
                    context.Buffers[columnIndex].CharArray = context.Reader.ReadString().ToCharArray();
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.TimeSpan:
                    context.Buffers[columnIndex].TimeSpan = BclHelpers.ReadTimeSpan(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;
                }
            }
        }
Exemple #18
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);
        }
Exemple #19
0
 public object Read(object value, ProtoReader source)
 {
     return(ProtoReader.AppendBytes(overwriteList ? null : (byte[])value, source, model.bufferPoolDelegate));
 }
        private void ReadRecordValues()
        {
            int fieldHeader;

            while ((fieldHeader = this.protoReader.ReadFieldHeader()) != FieldHeaders.None)
            {
                var columnIndex = fieldHeader - 1;

                switch (this.columns[columnIndex].ProtoBufDataType)
                {
                case ProtoBufDataType.Bool:
                    this.buffers[columnIndex].Bool   = this.protoReader.ReadBoolean();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Byte:
                    this.buffers[columnIndex].Byte   = this.protoReader.ReadByte();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.ByteArray:
                    this.buffers[columnIndex].ByteArray = ProtoReader.AppendBytes(null, this.protoReader);
                    this.buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoBufDataType.Char:
                    this.buffers[columnIndex].Char   = (char)this.protoReader.ReadInt16();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.CharArray:
                    this.buffers[columnIndex].CharArray = this.protoReader.ReadString().ToCharArray();
                    this.buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoBufDataType.DateTime:
                    this.buffers[columnIndex].DateTime = BclHelpers.ReadDateTime(this.protoReader);
                    this.buffers[columnIndex].IsNull   = false;
                    break;

                case ProtoBufDataType.Decimal:
                    this.buffers[columnIndex].Decimal = BclHelpers.ReadDecimal(this.protoReader);
                    this.buffers[columnIndex].IsNull  = false;
                    break;

                case ProtoBufDataType.Double:
                    this.buffers[columnIndex].Double = this.protoReader.ReadDouble();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Float:
                    this.buffers[columnIndex].Float  = this.protoReader.ReadSingle();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Guid:
                    this.buffers[columnIndex].Guid   = BclHelpers.ReadGuid(this.protoReader);
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Int:
                    this.buffers[columnIndex].Int    = this.protoReader.ReadInt32();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Long:
                    this.buffers[columnIndex].Long   = this.protoReader.ReadInt64();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Short:
                    this.buffers[columnIndex].Short  = this.protoReader.ReadInt16();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.String:
                    this.buffers[columnIndex].String = this.protoReader.ReadString();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.TimeSpan:
                    this.buffers[columnIndex].TimeSpan = BclHelpers.ReadTimeSpan(this.protoReader);
                    this.buffers[columnIndex].IsNull   = false;
                    break;
                }
            }
        }