Esempio n. 1
0
        private static PacketDefinition Deserialize(string packetContent, PacketDefinition deserializedPacket,
                                                    KeyValuePair <Tuple <Type, string>,
                                                                  Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation,
                                                    bool includesKeepAliveIdentity)
        {
            var matches = Regex.Matches(packetContent,
                                        @"([^\040]+[\.][^\040]+[\040]?)+((?=\040)|$)|([^\040]+)((?=\040)|$)");

            if (matches.Count > 0)
            {
                foreach (var packetBasePropertyInfo in serializationInformation.Value)
                {
                    var currentIndex =
                        packetBasePropertyInfo.Key.Index
                        + (includesKeepAliveIdentity ? 2
                            : 1); // adding 2 because we need to skip incrementing number and packet header

                    if (currentIndex < matches.Count + (includesKeepAliveIdentity ? 1 : 0))
                    {
                        if (packetBasePropertyInfo.Key.SerializeToEnd)
                        {
                            // get the value to the end and stop deserialization
                            var index = matches.Count > currentIndex ? matches[currentIndex].Index
                                : packetContent.Length;
                            var valueToEnd =
                                packetContent.Substring(index, packetContent.Length - index);
                            packetBasePropertyInfo.Value.SetValue(deserializedPacket,
                                                                  DeserializeValue(packetBasePropertyInfo.Value.PropertyType, valueToEnd,
                                                                                   packetBasePropertyInfo.Key,
                                                                                   packetBasePropertyInfo.Value.GetCustomAttributes <ValidationAttribute>(), matches,
                                                                                   includesKeepAliveIdentity));
                            break;
                        }

                        var currentValue = matches[currentIndex].Value;

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

            return(deserializedPacket);
        }
Esempio n. 2
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 = packetType.CreateInstance <PacketDefinition>();
         SetDeserializationInformations(deserializedPacket, packetContent, serializationInformation.Key.Item2);
         return(Deserialize(packetContent, deserializedPacket, serializationInformation, includesKeepAliveIdentity));
     }
     catch (Exception e)
     {
         Logger.Logger.Log.Warn($"The serialized packet has the wrong format. Packet: {packetContent}", e);
         return(null);
     }
 }
Esempio n. 3
0
 private static void SetDeserializationInformations(PacketDefinition packetDefinition,
                                                    string packetContent, string packetHeader)
 {
     packetDefinition.OriginalContent = packetContent;
     packetDefinition.OriginalHeader  = packetHeader;
 }
Esempio n. 4
0
        private static PacketDefinition Deserialize(string packetContent, PacketDefinition deserializedPacket,
                                                    KeyValuePair <Tuple <Type, string>,
                                                                  Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation,
                                                    bool includesKeepAliveIdentity)
        {
            var matches = Regex.Matches(packetContent,
                                        @"([^\040]+[\.][^\040]+[\040]?)+((?=\040)|$)|([^\040]+)((?=\040)|$)");

            if (matches.Count > 0)
            {
                int?realIndex = null;
                foreach (var packetBasePropertyInfo in serializationInformation.Value)
                {
                    var currentIndex =
                        packetBasePropertyInfo.Key.Index
                        + (includesKeepAliveIdentity ? 2
                            : 1); // adding 2 because we need to skip incrementing number and packet header

                    if (currentIndex < matches.Count + (includesKeepAliveIdentity ? 1 : 0))
                    {
                        if (packetBasePropertyInfo.Key.Length == -1)
                        {
                            packetBasePropertyInfo.Key.Length = sbyte.Parse(matches[currentIndex - 1].Value);
                        }

                        if (packetBasePropertyInfo.Key.Length > 0 && typeof(ICollection).IsAssignableFrom(packetBasePropertyInfo.Value.PropertyType))
                        {
                            packetBasePropertyInfo.Key.SpecialSeparator = " ";
                            var propertiesAttributes = packetBasePropertyInfo.Value.PropertyType.GetGenericArguments()[0]
                                                       .GetProperties().Select(s => s.GetCustomAttribute <PacketIndexAttribute>());
                            var length = (propertiesAttributes.Select(s => s?.Index ?? 0).Max() + 1);

                            var listType            = typeof(List <>);
                            var constructedListType = listType.MakeGenericType(packetBasePropertyInfo.Value.PropertyType.GetGenericArguments()[0]);
                            var list = (IList)Activator.CreateInstance(constructedListType);
                            for (var i = 0; i < packetBasePropertyInfo.Key.Length; i++)
                            {
                                list.Add(DeserializeValue(packetBasePropertyInfo.Value.PropertyType.GetGenericArguments()[0], string.Join(".", matches.Skip(currentIndex).Take(length)),
                                                          packetBasePropertyInfo.Key,
                                                          packetBasePropertyInfo.Value.GetCustomAttributes <ValidationAttribute>(), matches,
                                                          false));
                                currentIndex += length;
                            }

                            packetBasePropertyInfo.Value.SetValue(deserializedPacket, list);
                            realIndex = currentIndex;
                            continue;
                        }

                        if (packetBasePropertyInfo.Key.SerializeToEnd)
                        {
                            // get the value to the end and stop deserialization
                            var index = matches.Count > (realIndex ?? currentIndex) ? matches[realIndex ?? currentIndex].Index
                                : packetContent.Length;
                            var valueToEnd =
                                packetContent.Substring(index, packetContent.Length - index);
                            packetBasePropertyInfo.Value.SetValue(deserializedPacket,
                                                                  DeserializeValue(packetBasePropertyInfo.Value.PropertyType, valueToEnd,
                                                                                   packetBasePropertyInfo.Key,
                                                                                   packetBasePropertyInfo.Value.GetCustomAttributes <ValidationAttribute>(), matches,
                                                                                   includesKeepAliveIdentity));
                            break;
                        }

                        var currentValue = (currentIndex >= matches.Count) ? null : matches[currentIndex].Value;

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

            return(deserializedPacket);
        }