private void RegisterPackets(IEnumerable <Type> types) { _registry = new Dictionary <ServerPacketId, ServerPacketInfo>(); List <PacketRegistrationException> RegistrationFailures = new List <PacketRegistrationException>(); foreach (Type t in types) { ServerPacketAttribute attr = (ServerPacketAttribute)t.GetCustomAttributes(typeof(ServerPacketAttribute), false).FirstOrDefault(); if (attr == null) { RegistrationFailures.Add(new InvalidPacketException(t, string.Format("Server Packet {0} must be adorned with ServerPacketAttribute to be registered.", t), null)); } else if (ServerPacket.IsValidForClientVersion(attr, Version)) { try { Register(t, attr.PacketID, attr.isFixedLength, attr.FixedLength); } catch (PacketRegistrationException ex) { RegistrationFailures.Add(ex); } } } if (RegistrationFailures.Count > 0) { throw new PacketRegistrationExceptions(RegistrationFailures); } }
private static ServerPacketAttribute FindServerPacketAttributeOnType <T>() where T : ServerPacket { ServerPacketAttribute attr; if (PacketInfoCache.TryGetValue(typeof(T), out attr)) { return(attr); } ServerPacketAttribute idattr = (ServerPacketAttribute)typeof(T).GetCustomAttributes(typeof(ServerPacketAttribute), false).FirstOrDefault(); if (idattr == null) { throw new InvalidOperationException(string.Format("PacketID is not defined on {0}", typeof(T))); } PacketInfoCache[typeof(T)] = idattr; return(idattr); }
internal static bool IsValidForClientVersion(ServerPacketAttribute attr, ClientVersion version) { return(version >= attr.StartVersion && ((attr.EndVersion == ClientVersion.vMAX && version == attr.EndVersion) || attr.StartVersion == attr.EndVersion || version < attr.EndVersion)); }
public static bool IsValidForClientVersion <T>(ClientVersion version) where T : ServerPacket { ServerPacketAttribute attr = FindServerPacketAttributeOnType <T>(); return(IsValidForClientVersion(attr, version)); }