public virtual void InitializePacketIdMaps(IEnumerable <FieldInfo> fields)
        {
            var mappingAttributeList = fields
                                       .SelectWhere(
                f => f.GetCustomAttribute <PacketIdMappingAttribute>(),
                (f, a) => a != null,
                (f, a) => new PacketIdMappingInfo(f, a !))
                                       .ToList();

            for (int stateIndex = 0; stateIndex < PacketIdMaps.Length; stateIndex++)
            {
                PacketIdMaps[stateIndex]       = new Dictionary <int, PacketIdDefinition>();
                PacketTypeToIdMaps[stateIndex] = new Dictionary <Type, PacketIdDefinition>();

                var state = (ProtocolState)stateIndex;
                foreach (var mappingInfo in mappingAttributeList.Where(x => x.Attribute.State == state))
                {
                    foreach (var typeEntry in RegisteredPacketTypes)
                    {
                        var packetStructAttrib = typeEntry.Value.Attribute;
                        var enumValue          = mappingInfo.Field.GetRawConstantValue();
                        if (packetStructAttrib.PacketId.Equals(enumValue))
                        {
                            var mapRawId   = mappingInfo.Attribute.RawId;
                            var mapId      = EnumConverter.ToEnum <TPacketId>(packetStructAttrib.PacketId);
                            var definition = new PacketIdDefinition(typeEntry.Key, mapRawId, mapId);

                            PacketIdMaps[stateIndex].Add(definition.RawId, definition);
                            PacketTypeToIdMaps[stateIndex].Add(definition.Type, definition);
                        }
                    }
                }
            }
        }
        public bool TryGetPacketIdDefinition(
            ProtocolState state, Type packetType, out PacketIdDefinition definition)
        {
            var map = GetTypeToPacketIdMap(state);

            return(map.TryGetValue(packetType, out definition));
        }
        public bool TryGetPacketIdDefinition(
            ProtocolState state, int rawId, out PacketIdDefinition definition)
        {
            var map = GetPacketIdMap(state);

            return(map.TryGetValue(rawId, out definition));
        }
 public bool TryGetPacketIdDefinition(TPacketId id, out PacketIdDefinition definition)
 {
     for (int i = 0; i < PacketIdMaps.Length; i++)
     {
         var map = GetPacketIdMap((ProtocolState)i);
         foreach (var value in map.Values)
         {
             if (EqualityComparer <TPacketId> .Default.Equals(value.Id, id))
             {
                 definition = value;
                 return(true);
             }
         }
     }
     definition = default;
     return(false);
 }