Exemplo n.º 1
0
 /// <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); // 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);
     }
 }
Exemplo n.º 2
0
        private static PacketDefinition Deserialize(string packetContent, PacketDefinition deserializedPacket, KeyValuePair <Tuple <Type, string>,
                                                                                                                             Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation, bool includesKeepAliveIdentity)
        {
            MatchCollection matches = Regex.Matches(packetContent, @"([^\040]+[\.][^\040]+[\040]?)+((?=\040)|$)|([^\040]+)((?=\040)|$)");

            if (matches.Count <= 0)
            {
                return(deserializedPacket);
            }

            foreach (KeyValuePair <PacketIndexAttribute, PropertyInfo> 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;

                    if (packetBasePropertyInfo.Value.PropertyType == typeof(string) && string.IsNullOrEmpty(currentValue))
                    {
                        throw new NullReferenceException();
                    }

                    // set the value & convert currentValue
                    packetBasePropertyInfo.Value.SetValue(deserializedPacket,
                                                          DeserializeValue(packetBasePropertyInfo.Value.PropertyType, currentValue, packetBasePropertyInfo.Key, matches, includesKeepAliveIdentity));
                }
                else
                {
                    break;
                }
            }

            return(deserializedPacket);
        }
Exemplo n.º 3
0
 private static void SetDeserializationInformations(PacketDefinition packetDefinition, string packetContent, string packetHeader)
 {
     packetDefinition.OriginalContent = packetContent;
     packetDefinition.OriginalHeader  = packetHeader;
 }