static public int ReadString(IntPtr l) { try { ProtoBuf.ProtoReader self = (ProtoBuf.ProtoReader)checkSelf(l); var ret = self.ReadString(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
/// <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> /// 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; }
/// <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 }
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; }
/// <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); }
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> /// 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); }
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); } }
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; }
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)); }
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; }
/// <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; }
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; }
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; }