Beispiel #1
0
        protected static bool InvalidValue <T>(IClient client, PacketField <T> field, T expected, bool disconnect = true)
        {
            Contract.Requires(client != null);

            LogError("{0} was expected to be {1} (was {2})".Interpolate(field.Name, expected, field.Value), client, disconnect);
            return(false);
        }
Beispiel #2
0
        protected static bool InvalidValueRange <T>(IClient client, PacketField <T> field, T expectedLower, T expectedUpper,
                                                    bool disconnect = true)
        {
            Contract.Requires(client != null);

            LogError("{0} was expected to be between {1} and {2} (was {3})".Interpolate(field.Name, expectedLower, expectedUpper, field.Value),
                     client, disconnect);
            return(false);
        }
Beispiel #3
0
        private void SetValue(PacketField value, PropertyInfo prop, object instance)
        {
            if (value.Fields != null)
            {
                var rank     = 1;
                var arr      = (GetDynamicObject(value, rank) as object[]).ToArray();
                var newArray = CreateArray(arr, value.Type);

                prop.SetValue(instance, newArray);
                return;
            }

            prop.SetValue(instance, GetValue(value.Value, value.Type));
        }
Beispiel #4
0
        private object GetDynamicObject(PacketField field, int maxRank, int rank = 0)
        {
            if (field.Fields == null || maxRank == rank)
            {
                return(GetValue(field.Value, field.Type));
            }

            var list = new List <object>();

            foreach (var f in field.Fields)
            {
                list.Add(GetDynamicObject(f, maxRank, rank + 1));
            }

            return(list.ToArray());
        }
Beispiel #5
0
    public static Packet GetPacketFromType(Type type)
    {
        // Skip compiler generated bullshit
        if (Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute)) ||
            type.FullName.IndexOfAny(new char[] { '<', '>', '+' }) != -1)
        {
            return(null);
        }

        var typeAttributes = type.GetCustomAttributes(false).ToDictionary(a => a.GetType().Name, a => a);

        if (!Attribute.IsDefined(type, typeof(DescriptionAttribute)))
        {
            System.Console.WriteLine($"SKIPPING TYPE {type.FullName} DUE TO MISSING DESCRIPTION!");
            return(null);
        }
        if (!Attribute.IsDefined(type, typeof(PacketAttribute)))
        {
            System.Console.WriteLine($"SKIPPING TYPE {type.FullName} DUE TO MISSING PACKET ATTRIBUTE!");
            return(null);
        }
        var typeDescription  = ((DescriptionAttribute)typeAttributes["DescriptionAttribute"]).Description;
        var packetDescriptor = ((PacketAttribute)typeAttributes["PacketAttribute"]);

        var packet = new Packet()
        {
            Id           = packetDescriptor.packetId,
            Name         = type.FullName,
            InternalName = packetDescriptor.internalName,
            Type         = packetDescriptor.direction,
        };

        // TODO: Load answerPacket name, etc..
        // If the answerPacketIds has been provided.
        if (packetDescriptor.answerPacketIds != null)
        {
            foreach (var answerPacketId in packetDescriptor.answerPacketIds)
            {
                packet.AnswerPacket.Add(answerPacketId);
            }
        }

        foreach (var field in type.GetFields())
        {
            var fieldAttributes = field.GetCustomAttributes(false).ToDictionary(a => a.GetType().Name, a => a);
            var packetField     = new PacketField();

            if (Attribute.IsDefined(field, typeof(DescriptionAttribute)))
            {
                packetField.Description = ((DescriptionAttribute)fieldAttributes["DescriptionAttribute"]).Description;
            }

            packetField.Name = field.Name;
            packetField.Type = TypeLookup[field.FieldType.FullName];
            packetField.Size = SizeLookup[field.FieldType.FullName];

            if (field.FieldType.IsArray)
            {
                if (Attribute.IsDefined(field, typeof(LengthAttribute)))
                {
                    var arrayLength = ((LengthAttribute)fieldAttributes["LengthAttribute"]).Length;
                    if (arrayLength.HasValue)
                    {
                        packetField.Size *= ((LengthAttribute)fieldAttributes["LengthAttribute"]).Length;
                    }
                    else
                    {
                        packet.hasUnknownField = true;
                        packetField.Size       = null;
                    }
                }

                if (field.FieldType == typeof(char[]) && Attribute.IsDefined(field, typeof(UnicodeAttribute)))
                {
                    packetField.Type = UnicodeLookup[packetField.Type];
                }
            }

            packet.Fields.Add(packetField);
            if (packetField.Size.HasValue)
            {
                packet.Size += packetField.Size.Value;
            }
        }

        var fileDir = @"C:\Users\GigaToni\Documents\Visual Studio 2015\Projects\MittronMadness\PacketCaptures\";

        if (packet.Type == Packet.Direction.ToServer)
        {
            fileDir += "incoming\\";
        }
        else
        {
            fileDir += "outgoing\\";
        }

        if (File.Exists(fileDir + packet.Name + ".bin"))
        {
            packet.sample = File.ReadAllBytes(fileDir + packet.Name + ".bin");
        }
        else if (File.Exists(fileDir + packet.Id + ".bin"))
        {
            packet.sample = File.ReadAllBytes(fileDir + packet.Id + ".bin");
        }
        else
        {
            System.Console.WriteLine("WARNING: NO BINARY HEXDUMP AVAILABLE FOR " + packet.Name + " RESORTING TO OLD TYPE METHOD");
            var hexDumpMethod = type.GetMethod("HexDump");
            packet.sample = new byte[0];
            if (hexDumpMethod != null)
            {
                packet.sample = (byte[])hexDumpMethod.Invoke(Activator.CreateInstance(type), new object[] { });
            }
        }

        // Pretty dirty fix for the incoming bin files not having ids..
        if (packet.Type == Packet.Direction.ToServer)
        {
            var incomingBytesOld = new byte[packet.sample.Length + 2];
            var packetIdBytes    = BitConverter.GetBytes(packet.Id);
            Array.Copy(packet.sample, 0, incomingBytesOld, 2, packet.sample.Length);
            Array.Copy(packetIdBytes, 0, incomingBytesOld, 0, 2);
            packet.sample = incomingBytesOld;
        }

        return(packet);
    }