/// <summary> /// Parses a Guid from a protobuf stream /// </summary> public static Guid ReadGuid(ProtoReader source) { ulong low = 0, high = 0; int fieldNumber; SubItemToken token = ProtoReader.StartSubItem(source); while ((fieldNumber = source.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldGuidLow: low = source.ReadUInt64(); break; case FieldGuidHigh: high = source.ReadUInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); if (low == 0 && high == 0) { return(Guid.Empty); } uint a = (uint)(low >> 32), b = (uint)low, c = (uint)(high >> 32), d = (uint)high; return(new Guid((int)b, (short)a, (short)(a >> 16), (byte)d, (byte)(d >> 8), (byte)(d >> 16), (byte)(d >> 24), (byte)c, (byte)(c >> 8), (byte)(c >> 16), (byte)(c >> 24))); }
public static decimal ReadDecimal(ProtoReader reader) { ulong num = (ulong)0; uint num1 = 0; uint num2 = 0; SubItemToken subItemToken = ProtoReader.StartSubItem(reader); while (true) { int num3 = reader.ReadFieldHeader(); int num4 = num3; if (num3 <= 0) { break; } switch (num4) { case 1: { num = reader.ReadUInt64(); continue; } case 2: { num1 = reader.ReadUInt32(); continue; } case 3: { num2 = reader.ReadUInt32(); continue; } default: { reader.SkipField(); continue; } } } ProtoReader.EndSubItem(subItemToken, reader); if (num == (long)0 && num1 == 0) { return(new decimal(0)); } int num5 = (int)(num & (ulong)-1); int num6 = (int)(num >> 32 & (ulong)-1); int num7 = (int)num1; bool flag = (num2 & 1) == 1; byte num8 = (byte)((num2 & 510) >> 1); return(new decimal(num5, num6, num7, flag, num8)); }
/// <summary> /// Parses a decimal from a protobuf stream /// </summary> public static decimal ReadDecimal(ProtoReader reader) { ulong low = 0; uint high = 0; uint signScale = 0; int fieldNumber; SubItemToken token = ProtoReader.StartSubItem(reader); while ((fieldNumber = reader.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldDecimalLow: low = reader.ReadUInt64(); break; case FieldDecimalHigh: high = reader.ReadUInt32(); break; case FieldDecimalSignScale: signScale = reader.ReadUInt32(); break; default: reader.SkipField(); break; } } ProtoReader.EndSubItem(token, reader); if (low == 0 && high == 0) { return(decimal.Zero); } int lo = (int)(low & 0xFFFFFFFFL), mid = (int)((low >> 32) & 0xFFFFFFFFL), hi = (int)high; bool isNeg = (signScale & 0x0001) == 0x0001; byte scale = (byte)((signScale & 0x01FE) >> 1); return(new decimal(lo, mid, hi, isNeg, scale)); }
static public int ReadFieldHeader(IntPtr l) { try { ProtoBuf.ProtoReader self = (ProtoBuf.ProtoReader)checkSelf(l); var ret = self.ReadFieldHeader(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public void TestRandomDataWithReader() { var input = File.ReadAllBytes("protobuf-net.dll"); var stream = new MemoryStream(input); stream.Seek(0, SeekOrigin.Begin); Assert.Greater(3, 0); // I always double-check the param order Assert.Greater(stream.Length, 0); using (var reader = new ProtoReader(stream, null, null)) { while (reader.ReadFieldHeader() > 0) { reader.SkipField(); } } }
public static Guid ReadGuid(ProtoReader source) { ulong num = (ulong)0; ulong num1 = (ulong)0; SubItemToken subItemToken = ProtoReader.StartSubItem(source); while (true) { int num2 = source.ReadFieldHeader(); int num3 = num2; if (num2 <= 0) { break; } switch (num3) { case 1: { num = source.ReadUInt64(); continue; } case 2: { num1 = source.ReadUInt64(); continue; } default: { source.SkipField(); continue; } } } ProtoReader.EndSubItem(subItemToken, source); if (num == (long)0 && num1 == (long)0) { return(Guid.Empty); } uint num4 = (uint)(num >> 32); uint num5 = (uint)num; uint num6 = (uint)(num1 >> 32); uint num7 = (uint)num1; return(new Guid((int)num5, (short)num4, (short)(num4 >> 16), (byte)num7, (byte)(num7 >> 8), (byte)(num7 >> 16), (byte)(num7 >> 24), (byte)num6, (byte)(num6 >> 8), (byte)(num6 >> 16), (byte)(num6 >> 24))); }
public static decimal ReadDecimal(ProtoReader reader) { ulong num = 0uL; uint num2 = 0u; uint num3 = 0u; SubItemToken token = ProtoReader.StartSubItem(reader); int num4; while ((num4 = reader.ReadFieldHeader()) > 0) { switch (num4) { case 1: num = reader.ReadUInt64(); break; case 2: num2 = reader.ReadUInt32(); break; case 3: num3 = reader.ReadUInt32(); break; default: reader.SkipField(); break; } } ProtoReader.EndSubItem(token, reader); if (num == 0L && num2 == 0) { return(0m); } int lo = (int)(num & uint.MaxValue); int mid = (int)((num >> 32) & uint.MaxValue); int hi = (int)num2; bool isNegative = (num3 & 1) == 1; byte scale = (byte)((num3 & 0x1FE) >> 1); return(new decimal(lo, mid, hi, isNegative, scale)); }
public static decimal ReadDecimal(ProtoReader reader) { ulong num = 0uL; uint num2 = 0u; uint num3 = 0u; SubItemToken token = ProtoReader.StartSubItem(reader); int num4; while ((num4 = reader.ReadFieldHeader()) > 0) { switch (num4) { case 1: num = reader.ReadUInt64(); break; case 2: num2 = reader.ReadUInt32(); break; case 3: num3 = reader.ReadUInt32(); break; default: reader.SkipField(); break; } } ProtoReader.EndSubItem(token, reader); if (num == 0uL && num2 == 0u) { return(0m); } int num5 = (int)(num & (ulong)-1); int num6 = (int)(num >> 32 & (ulong)-1); int num7 = (int)num2; bool flag = (num3 & 1u) == 1u; byte b = (byte)((num3 & 510u) >> 1); return(new decimal(num5, num6, num7, flag, b)); }
public static Guid ReadGuid(ProtoReader source) { ulong num = 0uL; ulong num2 = 0uL; SubItemToken token = ProtoReader.StartSubItem(source); int num3; while ((num3 = source.ReadFieldHeader()) > 0) { int num4 = num3; if (num4 != 1) { if (num4 != 2) { source.SkipField(); } else { num2 = source.ReadUInt64(); } } else { num = source.ReadUInt64(); } } ProtoReader.EndSubItem(token, source); if (num == 0uL && num2 == 0uL) { return(Guid.Empty); } uint num5 = (uint)(num >> 32); uint num6 = (uint)num; uint num7 = (uint)(num2 >> 32); uint num8 = (uint)num2; return(new Guid((int)num6, (short)num5, (short)(num5 >> 16), (byte)num8, (byte)(num8 >> 8), (byte)(num8 >> 16), (byte)(num8 >> 24), (byte)num7, (byte)(num7 >> 8), (byte)(num7 >> 16), (byte)(num7 >> 24))); }
public static decimal ReadDecimal(ProtoReader reader) { ulong num = 0uL; uint num2 = 0u; uint num3 = 0u; SubItemToken token = ProtoReader.StartSubItem(reader); int num4; while ((num4 = reader.ReadFieldHeader()) > 0) { switch (num4) { case 1: num = reader.ReadUInt64(); break; case 2: num2 = reader.ReadUInt32(); break; case 3: num3 = reader.ReadUInt32(); break; default: reader.SkipField(); break; } } ProtoReader.EndSubItem(token, reader); if (num == 0uL && num2 == 0u) { return 0m; } int lo = (int)(num & 0xFFFFFFFFL); int mid = (int)(num >> 32 & 0xFFFFFFFFL); int hi = (int)num2; bool isNegative = (num3 & 1u) == 1u; byte scale = (byte)((num3 & 510u) >> 1); return new decimal(lo, mid, hi, isNegative, scale); }
public static Guid ReadGuid(ProtoReader source) { ulong num = 0uL; ulong num2 = 0uL; SubItemToken token = ProtoReader.StartSubItem(source); int num3; while ((num3 = source.ReadFieldHeader()) > 0) { switch (num3) { case 1: num = source.ReadUInt64(); break; case 2: num2 = source.ReadUInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); if (num == 0L && num2 == 0L) { return(Guid.Empty); } uint num4 = (uint)(num >> 32); int a = (int)num; uint num5 = (uint)(num2 >> 32); uint num6 = (uint)num2; return(new Guid(a, (short)num4, (short)(num4 >> 16), (byte)num6, (byte)(num6 >> 8), (byte)(num6 >> 16), (byte)(num6 >> 24), (byte)num5, (byte)(num5 >> 8), (byte)(num5 >> 16), (byte)(num5 >> 24))); }
/// <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: type = source.DeserializeType(source.ReadString()); key = source.GetTypeKey(ref type); break; case FieldObject: bool isString = type == typeof(string); bool lateSet = value == null && isString; if (value == null && !lateSet) { value = ((options & NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : Activator.CreateInstance(type); } if (newObjectKey >= 0 && !lateSet) { 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 && lateSet) { source.NetCache.SetKeyedObject(newObjectKey, value); if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type); } if (!lateSet && !ReferenceEquals(oldValue, value)) { throw new ProtoException("A reference-tracked object changed reference during deserialization"); } break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); return value; }
/// <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) { 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: type = source.DeserializeType(source.ReadString()); key = source.GetTypeKey(ref type); break; case FieldObject: bool isString = type == typeof(string); bool lateSet = value == null && isString; if (value == null && !lateSet) { value = Activator.CreateInstance(type); // TODO wcf-style inits } if (newObjectKey >= 0 && !lateSet) { 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 && lateSet) { source.NetCache.SetKeyedObject(newObjectKey, value); if (newTypeKey >= 0) { source.NetCache.SetKeyedObject(newTypeKey, type); } } if (!lateSet && !ReferenceEquals(oldValue, value)) { throw new ProtoException("A reference-tracked object changed reference during deserialization"); } break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); return(value); }
private static long ReadTimeSpanTicks(ProtoReader source) { WireType wireType = source.WireType; if (wireType != WireType.Fixed64) { if ((uint)(wireType - 2) <= 1u) { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { switch (num2) { case 2: timeSpanScale = (TimeSpanScale)source.ReadInt32(); break; case 1: source.Assert(WireType.SignedVariant); num = source.ReadInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (timeSpanScale) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000); case TimeSpanScale.Seconds: return(num * 10000000); case TimeSpanScale.Milliseconds: return(num * 10000); case TimeSpanScale.Ticks: return(num); case TimeSpanScale.MinMax: switch (num) { case 1L: return(9223372036854775807L); case -1L: return(-9223372036854775808L); default: throw new ProtoException("Unknown min/max value: " + num.ToString()); } default: throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } } wireType = source.WireType; throw new ProtoException("Unexpected wire-type: " + wireType.ToString()); } return(source.ReadInt64()); }
// Token: 0x06000039 RID: 57 RVA: 0x0000A274 File Offset: 0x00008474 private static long ReadTimeSpanTicks(ProtoReader source) { WireType wireType = source.WireType; if (wireType == WireType.Fixed64) { return(source.ReadInt64()); } if (wireType - WireType.String > 1) { throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { if (num2 != 1) { if (num2 == 2) { timeSpanScale = (TimeSpanScale)source.ReadInt32(); } else { source.SkipField(); } } else { source.Assert(WireType.SignedVariant); num = source.ReadInt64(); } } ProtoReader.EndSubItem(token, source); switch (timeSpanScale) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000L); case TimeSpanScale.Seconds: return(num * 10000000L); case TimeSpanScale.Milliseconds: return(num * 10000L); case TimeSpanScale.Ticks: return(num); default: if (timeSpanScale != TimeSpanScale.MinMax) { throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } if (num == -1L) { return(long.MinValue); } if (num == 1L) { return(long.MaxValue); } throw new ProtoException("Unknown min/max value: " + num.ToString()); } }
/// <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 static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options) { SubItemToken token = ProtoReader.StartSubItem(source); int num = -1; int num2 = -1; int num3; while ((num3 = source.ReadFieldHeader()) > 0) { switch (num3) { case 1: { int key2 = source.ReadInt32(); value = source.NetCache.GetKeyedObject(key2); break; } case 2: num = source.ReadInt32(); break; case 3: { int key2 = source.ReadInt32(); type = (Type)source.NetCache.GetKeyedObject(key2); key = source.GetTypeKey(ref type); break; } case 4: num2 = source.ReadInt32(); break; case 8: { string text = source.ReadString(); type = source.DeserializeType(text); if (type == null) { throw new ProtoException("Unable to resolve type: " + text + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)"); } if (type == typeof(string)) { key = -1; break; } key = source.GetTypeKey(ref type); if (key >= 0) { break; } throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name); } case 10: { bool flag = type == typeof(string); bool flag2 = value == null; bool flag3 = flag2 && (flag || (options & NetObjectOptions.LateSet) != 0); if (num >= 0 && !flag3) { if (value == null) { source.TrapNextObject(num); } else { source.NetCache.SetKeyedObject(num, value); } if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } object obj = value; value = ((!flag) ? ProtoReader.ReadTypedObject(obj, key, source, type) : source.ReadString()); if (num >= 0) { if (flag2 && !flag3) { obj = source.NetCache.GetKeyedObject(num); } if (flag3) { source.NetCache.SetKeyedObject(num, value); if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } } if (num >= 0 && !flag3 && obj != value) { throw new ProtoException("A reference-tracked object changed reference during deserialization"); } if (num < 0 && num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } break; } default: source.SkipField(); break; } } if (num >= 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); }
/// <summary> /// The read. /// </summary> /// <param name="mxMessage"> /// The mx message. /// </param> /// <param name="protoReader"> /// The proto reader. /// </param> /// <returns> /// The <see cref="MxMessage"/>. /// </returns> private static MxMessage Read(MxMessage mxMessage, ProtoReader protoReader) { int num; while ((num = protoReader.ReadFieldHeader()) > 0) { if (num != 1) { if (num != 2) { if (num != 3) { if (num != 4) { if (num != 6) { if (mxMessage == null) { var expr_170 = new MxMessage(); ProtoReader.NoteObject(expr_170, protoReader); mxMessage = expr_170; } protoReader.SkipField(); } else { if (mxMessage == null) { var expr_D9 = new MxMessage(); ProtoReader.NoteObject(expr_D9, protoReader); mxMessage = expr_D9; } MxPayload[] payloads = mxMessage.Payloads; var list = new List<MxPayload>(); int num2 = protoReader.FieldNumber; do { List<MxPayload> arg_111_0 = list; MxPayload arg_104_0 = null; SubItemToken token = ProtoReader.StartSubItem(protoReader); MxPayload arg_111_1 = Read(arg_104_0, protoReader); ProtoReader.EndSubItem(token, protoReader); arg_111_0.Add(arg_111_1); } while (protoReader.TryReadFieldHeader(num2)); MxPayload[] expr_124 = payloads; var array = new MxPayload[(num2 = (expr_124 != null) ? expr_124.Length : 0) + list.Count]; if (num2 != 0) { payloads.CopyTo(array, 0); } list.CopyTo(array, num2); array = array; if (array != null) { mxMessage.Payloads = array; } } } else { if (mxMessage == null) { var expr_A9 = new MxMessage(); ProtoReader.NoteObject(expr_A9, protoReader); mxMessage = expr_A9; } uint num3 = protoReader.ReadUInt32(); mxMessage.AckBitfield = num3; } } else { if (mxMessage == null) { var expr_79 = new MxMessage(); ProtoReader.NoteObject(expr_79, protoReader); mxMessage = expr_79; } uint num3 = protoReader.ReadUInt32(); mxMessage.Ack = num3; } } else { if (mxMessage == null) { var expr_49 = new MxMessage(); ProtoReader.NoteObject(expr_49, protoReader); mxMessage = expr_49; } uint num3 = protoReader.ReadUInt32(); mxMessage.Sequence = num3; } } else { if (mxMessage == null) { var expr_19 = new MxMessage(); ProtoReader.NoteObject(expr_19, protoReader); mxMessage = expr_19; } uint num3 = protoReader.ReadUInt32(); mxMessage.ProtocolID = num3; } } if (mxMessage == null) { var expr_198 = new MxMessage(); ProtoReader.NoteObject(expr_198, protoReader); mxMessage = expr_198; } return mxMessage; }
/// <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; }
private static long ReadTimeSpanTicks(ProtoReader source) { switch (source.WireType) { case WireType.Fixed64: return(source.ReadInt64()); case WireType.String: case WireType.StartGroup: { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { int num3 = num2; if (num3 != 1) { if (num3 != 2) { source.SkipField(); } else { timeSpanScale = (TimeSpanScale)source.ReadInt32(); } } else { source.Assert(WireType.SignedVariant); num = source.ReadInt64(); } } ProtoReader.EndSubItem(token, source); TimeSpanScale timeSpanScale2 = timeSpanScale; switch (timeSpanScale2) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000L); case TimeSpanScale.Seconds: return(num * 10000000L); case TimeSpanScale.Milliseconds: return(num * 10000L); case TimeSpanScale.Ticks: return(num); default: { if (timeSpanScale2 != TimeSpanScale.MinMax) { throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } long num4 = num; if (num4 >= -1L && num4 <= 1L) { switch ((int)(num4 - -1L)) { case 0: return(-9223372036854775808L); case 2: return(9223372036854775807L); } } throw new ProtoException("Unknown min/max value: " + num.ToString()); } } break; } default: throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } }
/// <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)"); } 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 lateSet = value == null && isString; if (value == null && !lateSet) { try { value = ((options & NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : Activator.CreateInstance(type); } catch (Exception ex) { throw new ProtoException("Unable to create type " + (type == null ? "<null>" : type.FullName) + ": " + ex.Message, ex); } } if (newObjectKey >= 0 && !lateSet) { 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 && 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); }
private static long ReadTimeSpanTicks(ProtoReader source) { TimeSpanScale timeSpanScale; switch (source.WireType) { case WireType.Fixed64: { return(source.ReadInt64()); } case WireType.String: case WireType.StartGroup: { SubItemToken subItemToken = ProtoReader.StartSubItem(source); timeSpanScale = TimeSpanScale.Days; long num = (long)0; while (true) { int num1 = source.ReadFieldHeader(); int num2 = num1; if (num1 <= 0) { break; } switch (num2) { case 1: { source.Assert(WireType.SignedVariant); num = source.ReadInt64(); continue; } case 2: { timeSpanScale = (TimeSpanScale)source.ReadInt32(); continue; } default: { source.SkipField(); continue; } } } ProtoReader.EndSubItem(subItemToken, source); TimeSpanScale timeSpanScale1 = timeSpanScale; switch (timeSpanScale1) { case TimeSpanScale.Days: { return(num * 864000000000L); } case TimeSpanScale.Hours: { return(num * 36000000000L); } case TimeSpanScale.Minutes: { return(num * (long)600000000); } case TimeSpanScale.Seconds: { return(num * (long)10000000); } case TimeSpanScale.Milliseconds: { return(num * (long)10000); } case TimeSpanScale.Ticks: { return(num); } default: { if (timeSpanScale1 == TimeSpanScale.MinMax) { break; } else { throw new ProtoException(string.Concat("Unknown timescale: ", timeSpanScale.ToString())); } } } long num3 = num; if (num3 <= (long)1 && num3 >= (long)-1) { switch ((int)(num3 - (long)-1)) { case 0: { return(-9223372036854775808L); } case 2: { return(9223372036854775807L); } } } throw new ProtoException(string.Concat("Unknown min/max value: ", num.ToString())); } } throw new ProtoException(string.Concat("Unexpected wire-type: ", source.WireType.ToString())); throw new ProtoException(string.Concat("Unknown timescale: ", timeSpanScale.ToString())); }
public static object ReadNetObject(object value, ProtoReader source, int key, Type type, BclHelpers.NetObjectOptions options) { int num; bool flag; SubItemToken subItemToken = ProtoReader.StartSubItem(source); int num1 = -1; int num2 = -1; do { Label0: int num3 = source.ReadFieldHeader(); int num4 = num3; if (num3 <= 0) { if (num1 >= 0 && (byte)(options & BclHelpers.NetObjectOptions.AsReference) == 0) { throw new ProtoException("Object key in input stream, but reference-tracking was not expected"); } ProtoReader.EndSubItem(subItemToken, source); return(value); } switch (num4) { case 1: { num = source.ReadInt32(); value = source.NetCache.GetKeyedObject(num); goto Label0; } case 2: { num1 = source.ReadInt32(); goto Label0; } case 3: { num = source.ReadInt32(); type = (Type)source.NetCache.GetKeyedObject(num); key = source.GetTypeKey(ref type); goto Label0; } case 4: { num2 = source.ReadInt32(); goto Label0; } case 5: case 6: case 7: case 9: { source.SkipField(); goto Label0; } case 8: { string str = source.ReadString(); type = source.DeserializeType(str); if (type == null) { throw new ProtoException(string.Concat("Unable to resolve type: ", str, " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)")); } if (type != typeof(string)) { key = source.GetTypeKey(ref type); continue; } else { key = -1; goto Label0; } } case 10: { bool flag1 = type == typeof(string); bool flag2 = value == null; if (!flag2) { flag = false; } else { flag = (flag1 ? true : (byte)(options & BclHelpers.NetObjectOptions.LateSet) != 0); } bool flag3 = flag; if (num1 >= 0 && !flag3) { if (value != null) { source.NetCache.SetKeyedObject(num1, value); } else { source.TrapNextObject(num1); } if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } object keyedObject = value; if (!flag1) { value = ProtoReader.ReadTypedObject(keyedObject, key, source, type); } else { value = source.ReadString(); } if (num1 >= 0) { if (flag2 && !flag3) { keyedObject = source.NetCache.GetKeyedObject(num1); } if (flag3) { source.NetCache.SetKeyedObject(num1, value); if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } } if (num1 >= 0 && !flag3 && !object.ReferenceEquals(keyedObject, value)) { throw new ProtoException("A reference-tracked object changed reference during deserialization"); } if (num1 >= 0 || num2 < 0) { goto Label0; } source.NetCache.SetKeyedObject(num2, type); goto Label0; } default: { goto case 9; } } }while (key >= 0); throw new InvalidOperationException(string.Concat("Dynamic type is not a contract-type: ", type.Name)); }
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; }
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 ); } // 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; }
// Token: 0x06003269 RID: 12905 RVA: 0x001266EC File Offset: 0x00124AEC private static long ReadTimeSpanTicks(ProtoReader source, out DateTimeKind kind) { kind = DateTimeKind.Unspecified; switch (source.WireType) { case WireType.Fixed64: return(source.ReadInt64()); case WireType.String: case WireType.StartGroup: { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { switch (num2) { case 1: source.Assert(WireType.SignedVariant); num = source.ReadInt64(); break; case 2: timeSpanScale = (TimeSpanScale)source.ReadInt32(); break; case 3: kind = (DateTimeKind)source.ReadInt32(); switch (kind) { case DateTimeKind.Unspecified: case DateTimeKind.Utc: case DateTimeKind.Local: break; default: throw new ProtoException("Invalid date/time kind: " + kind.ToString()); } break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (timeSpanScale) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000L); case TimeSpanScale.Seconds: return(num * 10000000L); case TimeSpanScale.Milliseconds: return(num * 10000L); case TimeSpanScale.Ticks: return(num); default: if (timeSpanScale != TimeSpanScale.MinMax) { throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } if (num == 1L) { return(long.MaxValue); } if (num != -1L) { throw new ProtoException("Unknown min/max value: " + num.ToString()); } return(long.MinValue); } } default: throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } }
private static long ReadTimeSpanTicks(ProtoReader source) { switch (source.WireType) { case WireType.String: case WireType.StartGroup: { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { switch (num2) { case 2: timeSpanScale = (TimeSpanScale)source.ReadInt32(); break; case 1: source.Assert(WireType.SignedVariant); num = source.ReadInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (timeSpanScale) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000); case TimeSpanScale.Seconds: return(num * 10000000); case TimeSpanScale.Milliseconds: return(num * 10000); case TimeSpanScale.Ticks: return(num); case TimeSpanScale.MinMax: switch (num) { case 1L: return(long.MaxValue); case -1L: return(long.MinValue); default: throw new ProtoException("Unknown min/max value: " + num); } default: throw new ProtoException("Unknown timescale: " + timeSpanScale); } } case WireType.Fixed64: return(source.ReadInt64()); default: throw new ProtoException("Unexpected wire-type: " + source.WireType); } }
private static long ReadTimeSpanTicks(ProtoReader source) { switch (source.WireType) { case WireType.String: case WireType.StartGroup: SubItemToken token = ProtoReader.StartSubItem(source); int fieldNumber; TimeSpanScale scale = TimeSpanScale.Days; long value = 0; while ((fieldNumber = source.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldTimeSpanScale: scale = (TimeSpanScale)source.ReadInt32(); break; case FieldTimeSpanValue: source.Assert(WireType.SignedVariant); value = source.ReadInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (scale) { case TimeSpanScale.Days: return value * TimeSpan.TicksPerDay; case TimeSpanScale.Hours: return value * TimeSpan.TicksPerHour; case TimeSpanScale.Minutes: return value * TimeSpan.TicksPerMinute; case TimeSpanScale.Seconds: return value * TimeSpan.TicksPerSecond; case TimeSpanScale.Milliseconds: return value * TimeSpan.TicksPerMillisecond; case TimeSpanScale.Ticks: return value; case TimeSpanScale.MinMax: switch (value) { case 1: return long.MaxValue; case -1: return long.MinValue; default: throw new ProtoException("Unknown min/max value: " + value.ToString()); } default: throw new ProtoException("Unknown timescale: " + scale.ToString()); } case WireType.Fixed64: return source.ReadInt64(); default: throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } }
/// <summary> /// Parses a Guid from a protobuf stream /// </summary> public static Guid ReadGuid(ProtoReader source) { ulong low = 0, high = 0; int fieldNumber; SubItemToken token = ProtoReader.StartSubItem(source); while ((fieldNumber = source.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldGuidLow: low = source.ReadUInt64(); break; case FieldGuidHigh: high = source.ReadUInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); if(low == 0 && high == 0) return Guid.Empty; uint a = (uint)(low >> 32), b = (uint)low, c = (uint)(high >> 32), d= (uint)high; return new Guid((int)b, (short)a, (short)(a >> 16), (byte)d, (byte)(d >> 8), (byte)(d >> 16), (byte)(d >> 24), (byte)c, (byte)(c >> 8), (byte)(c >> 16), (byte)(c >> 24)); }
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> /// 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) { #if FEAT_IKVM throw new NotSupportedException(); #else 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); #endif }
/// <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; }
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(), StringComparison.OrdinalIgnoreCase)) 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 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; }
void ReadSInstance(ProtoReader reader, SInstance sInstance, CLS_Environment environment) { List<CLS_Content.Value> values; List<string> keywords; GetSortMembers(sInstance, out values, out keywords); int fieldNumber = 0; while ((fieldNumber = reader.ReadFieldHeader()) > 0) { Type memberT = values[fieldNumber - 1].type; CLS_Content.Value memberV = values[fieldNumber - 1]; string sClassName = keywords[fieldNumber - 1]; if (memberT == null) { memberT = typeof(SInstance); sClassName = ((SType)memberV.type).Name; } Type itemType = GetItemType(memberT); if (itemType != null) { sClassName = sInstance.type.members[sClassName].type.keyword; // 数组判断 if (memberT.IsArray) { string itemClass = sClassName.Substring(0, sClassName.Length - 2); // 从 xxx[] 中提取xxx BasicList list = new BasicList(); do { list.Add(ReadField(reader, itemType, itemClass, environment)); } while (reader.TryReadFieldHeader(fieldNumber)); Array result = Array.CreateInstance(itemType, list.Count); list.CopyTo(result, 0); memberV.value = result; } // 列表判断 else { string itemClass = sClassName.Substring(5, sClassName.Length - 6); // 从 List<xxx> 中提取xxx ICLS_Type iType = environment.GetTypeByKeywordQuiet(sClassName); CLS_Content content = CLS_Content.NewContent(environment); memberV.value = iType.function.New(content, m_emptyParams).value; CLS_Content.PoolContent(content); IList list = (IList)memberV.value; do { list.Add(ReadField(reader, itemType, itemClass, environment)); } while (reader.TryReadFieldHeader(fieldNumber)); } } else { memberV.value = ReadField(reader, memberT, sClassName, environment); } } }
private static long ReadTimeSpanTicks(ProtoReader source, out DateTimeKind kind) { kind = DateTimeKind.Unspecified; switch (source.WireType) { case WireType.String: case WireType.StartGroup: SubItemToken token = ProtoReader.StartSubItem(source); int fieldNumber; TimeSpanScale scale = TimeSpanScale.Days; long value = 0; while ((fieldNumber = source.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldTimeSpanScale: scale = (TimeSpanScale)source.ReadInt32(); break; case FieldTimeSpanValue: source.Assert(WireType.SignedVariant); value = source.ReadInt64(); break; case FieldTimeSpanKind: kind = (DateTimeKind)source.ReadInt32(); switch (kind) { case DateTimeKind.Unspecified: case DateTimeKind.Utc: case DateTimeKind.Local: break; // fine default: throw new ProtoException("Invalid date/time kind: " + kind.ToString()); } break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (scale) { case TimeSpanScale.Days: return(value * TimeSpan.TicksPerDay); case TimeSpanScale.Hours: return(value * TimeSpan.TicksPerHour); case TimeSpanScale.Minutes: return(value * TimeSpan.TicksPerMinute); case TimeSpanScale.Seconds: return(value * TimeSpan.TicksPerSecond); case TimeSpanScale.Milliseconds: return(value * TimeSpan.TicksPerMillisecond); case TimeSpanScale.Ticks: return(value); case TimeSpanScale.MinMax: switch (value) { case 1: return(long.MaxValue); case -1: return(long.MinValue); default: throw new ProtoException("Unknown min/max value: " + value.ToString()); } default: throw new ProtoException("Unknown timescale: " + scale.ToString()); } case WireType.Fixed64: return(source.ReadInt64()); default: throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } }
public static object ReadNetObject(object value, ProtoReader source, int key, Type type, BclHelpers.NetObjectOptions options) { SubItemToken token = ProtoReader.StartSubItem(source); int num = -1; int num2 = -1; int num3; while ((num3 = source.ReadFieldHeader()) > 0) { switch (num3) { case 1: { int key2 = source.ReadInt32(); value = source.NetCache.GetKeyedObject(key2); continue; } case 2: num = source.ReadInt32(); continue; case 3: { int key2 = source.ReadInt32(); type = (Type)source.NetCache.GetKeyedObject(key2); key = source.GetTypeKey(ref type); continue; } case 4: num2 = source.ReadInt32(); continue; case 8: { string text = source.ReadString(); type = source.DeserializeType(text); if (type == null) { throw new ProtoException("Unable to resolve type: " + text + " (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); } } continue; } case 10: { bool flag = type == typeof(string); bool flag2 = value == null; bool flag3 = flag2 && (flag || (byte)(options & BclHelpers.NetObjectOptions.LateSet) != 0); if (num >= 0 && !flag3) { if (value == null) { source.TrapNextObject(num); } else { source.NetCache.SetKeyedObject(num, value); } if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } object obj = value; if (flag) { value = source.ReadString(); } else { value = ProtoReader.ReadTypedObject(obj, key, source, type); } if (num >= 0) { if (flag2 && !flag3) { obj = source.NetCache.GetKeyedObject(num); } if (flag3) { source.NetCache.SetKeyedObject(num, value); if (num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } } } if (num >= 0 && !flag3 && !object.ReferenceEquals(obj, value)) { throw new ProtoException("A reference-tracked object changed reference during deserialization"); } if (num < 0 && num2 >= 0) { source.NetCache.SetKeyedObject(num2, type); } continue; } } source.SkipField(); } if (num >= 0 && (byte)(options & BclHelpers.NetObjectOptions.AsReference) == 0) { throw new ProtoException("Object key in input stream, but reference-tracking was not expected"); } ProtoReader.EndSubItem(token, source); return value; }
/// <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)"); } 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 lateSet = value == null && isString; if (value == null && !lateSet) { try { value = ((options & NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : (type.IsArray || type.IsInterface ? null : Activator.CreateInstance(type, true)); } catch (Exception ex) { throw new ProtoException("Unable to create type " + (type == null ? "<null>" : type.FullName) + ": " + ex.Message, ex); } } if (newObjectKey >= 0 && !lateSet) { 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 && lateSet) { source.NetCache.SetKeyedObject(newObjectKey, value); if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type); } if (newObjectKey >= 0 && !lateSet && !ReferenceEquals(oldValue, value) && type.FullName.StartsWith("System.Collections.Generic.Dictionary`2")) { 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; }
private static long ReadTimeSpanTicks(ProtoReader source) { switch (source.WireType) { case WireType.Fixed64: return source.ReadInt64(); case WireType.String: case WireType.StartGroup: { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { int num3 = num2; if (num3 != 1) { if (num3 != 2) { source.SkipField(); } else { timeSpanScale = (TimeSpanScale)source.ReadInt32(); } } else { source.Assert(WireType.SignedVariant); num = source.ReadInt64(); } } ProtoReader.EndSubItem(token, source); TimeSpanScale timeSpanScale2 = timeSpanScale; switch (timeSpanScale2) { case TimeSpanScale.Days: return num * 864000000000L; case TimeSpanScale.Hours: return num * 36000000000L; case TimeSpanScale.Minutes: return num * 600000000L; case TimeSpanScale.Seconds: return num * 10000000L; case TimeSpanScale.Milliseconds: return num * 10000L; case TimeSpanScale.Ticks: return num; default: { if (timeSpanScale2 != TimeSpanScale.MinMax) { throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } long num4 = num; if (num4 >= -1L && num4 <= 1L) { switch ((int)(num4 - -1L)) { case 0: return -9223372036854775808L; case 2: return 9223372036854775807L; } } throw new ProtoException("Unknown min/max value: " + num.ToString()); } } break; } default: throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString()); } }
/// <summary> /// Parses a decimal from a protobuf stream /// </summary> public static decimal ReadDecimal(ProtoReader reader) { ulong low = 0; uint high = 0; uint signScale = 0; int fieldNumber; SubItemToken token = ProtoReader.StartSubItem(reader); while ((fieldNumber = reader.ReadFieldHeader()) > 0) { switch (fieldNumber) { case FieldDecimalLow: low = reader.ReadUInt64(); break; case FieldDecimalHigh: high = reader.ReadUInt32(); break; case FieldDecimalSignScale: signScale = reader.ReadUInt32(); break; default: reader.SkipField(); break; } } ProtoReader.EndSubItem(token, reader); if (low == 0 && high == 0) return decimal.Zero; int lo = (int)(low & 0xFFFFFFFFL), mid = (int)((low >> 32) & 0xFFFFFFFFL), hi = (int)high; bool isNeg = (signScale & 0x0001) == 0x0001; byte scale = (byte)((signScale & 0x01FE) >> 1); return new decimal(lo, mid, hi, isNeg, scale); }
public static Guid ReadGuid(ProtoReader source) { ulong num = 0uL; ulong num2 = 0uL; SubItemToken token = ProtoReader.StartSubItem(source); int num3; while ((num3 = source.ReadFieldHeader()) > 0) { int num4 = num3; if (num4 != 1) { if (num4 != 2) { source.SkipField(); } else { num2 = source.ReadUInt64(); } } else { num = source.ReadUInt64(); } } ProtoReader.EndSubItem(token, source); if (num == 0uL && num2 == 0uL) { return Guid.Empty; } uint num5 = (uint)(num >> 32); uint a = (uint)num; uint num6 = (uint)(num2 >> 32); uint num7 = (uint)num2; return new Guid((int)a, (short)num5, (short)(num5 >> 16), (byte)num7, (byte)(num7 >> 8), (byte)(num7 >> 16), (byte)(num7 >> 24), (byte)num6, (byte)(num6 >> 8), (byte)(num6 >> 16), (byte)(num6 >> 24)); }
/// <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) { #if FEAT_IKVM throw new NotSupportedException(); #else 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; #endif }
public bool TryReadType(ArraySegment<byte> data, out Type type) { using (var stream = new MemoryStream(data.Array, data.Offset, data.Count)) { var reader = new ProtoReader(stream, null, null); int typeId = reader.ReadFieldHeader(); return _messageIdToType.TryGetValue(typeId, out type); } }