private static PacketDefinition Deserialize(string packetContent, PacketDefinition deserializedPacket, KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation, bool includesKeepAliveIdentity) { MatchCollection matches = Regex.Matches(packetContent, @"([^\s]+[\.][^\s]+[\s]?)+((?=\s)|$)|([^\s]+)((?=\s)|$)"); if (matches.Count > 0) { foreach (var packetBasePropertyInfo in serializationInformation.Value) { int currentIndex = packetBasePropertyInfo.Key.Index + (includesKeepAliveIdentity ? 2 : 1); // adding 2 because we need to skip incrementing number and packet header if (currentIndex < matches.Count) { if (packetBasePropertyInfo.Key.SerializeToEnd) { // get the value to the end and stop deserialization string valueToEnd = packetContent.Substring(matches[currentIndex].Index, packetContent.Length - matches[currentIndex].Index); packetBasePropertyInfo.Value.SetValue(deserializedPacket, DeserializeValue(packetBasePropertyInfo.Value.PropertyType, valueToEnd, packetBasePropertyInfo.Key, matches, includesKeepAliveIdentity)); break; } string currentValue = matches[currentIndex].Value; // set the value & convert currentValue packetBasePropertyInfo.Value.SetValue(deserializedPacket, DeserializeValue(packetBasePropertyInfo.Value.PropertyType, currentValue, packetBasePropertyInfo.Key, matches, includesKeepAliveIdentity)); } else { break; } } } return(deserializedPacket); }
private static PacketDefinition SetDeserializationInformations(PacketDefinition packetDefinition, string packetContent, string header) { packetDefinition.OriginalContent = packetContent; packetDefinition.OriginalHeader = header; return(packetDefinition); }
/// <summary> /// Deserializes a string into a PacketDefinition /// </summary> /// <param name="packetContent">The content to deseralize</param> /// <param name="packetType">The type of the packet to deserialize to</param> /// <param name="includesKeepAliveIdentity"> /// Include the keep alive identity or exclude it /// </param> /// <returns>The deserialized packet.</returns> public static PacketDefinition Deserialize(string packetContent, Type packetType, bool includesKeepAliveIdentity = false) { try { KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation = GetSerializationInformation(packetType); PacketDefinition deserializedPacket = (PacketDefinition)Activator.CreateInstance(packetType); SetDeserializationInformations(deserializedPacket, packetContent, serializationInformation.Key.Item2); return(Deserialize(packetContent, deserializedPacket, serializationInformation, includesKeepAliveIdentity)); } catch (Exception ex) { Logger.Warn($"The serialized packet has the wrong format. Packet: {packetContent}", ex); return(null); } }
/// <summary> /// Deserializes a string into a PacketDefinition /// </summary> /// <param name="packetContent">The content to deseralize</param> /// <param name="packetType">The type of the packet to deserialize to</param> /// <param name="includesKeepAliveIdentity"> /// Include the keep alive identity or exclude it /// </param> /// <returns>The deserialized packet.</returns> public static PacketDefinition Deserialize(string packetContent, Type packetType, bool includesKeepAliveIdentity = false) { try { var serializationInformation = GetSerializationInformation(packetType); PacketDefinition deserializedPacket = (PacketDefinition)Activator.CreateInstance(packetType); // reflection is bad, improve? SetDeserializationInformations(deserializedPacket, packetContent, serializationInformation.Key.Item2); deserializedPacket = Deserialize(packetContent, deserializedPacket, serializationInformation, includesKeepAliveIdentity); return(deserializedPacket); } catch (Exception) { Logger.Log.Warn($"The serialized packet has the wrong format. Packet: {packetContent}"); return(null); } }