public static bool RegisterPacketType(Type type) { if (!type.IsSealed) { return(false); } if (!typeof(Packet).IsAssignableFrom(type)) { return(false); } object[] attrs = type.GetCustomAttributes(typeof(PacketTypeAttribute), false); if (attrs == null || attrs.Length == 0) { return(false); } PacketTypeAttribute attr = (PacketTypeAttribute)attrs[0]; int id = attr.Id; if (type == GetPacketType(id)) { return(true); } Logger.Debug("network", "Register packet type={0} id={1}.", type, id); types.Add(id, new TypeInfo() { type = type, attr = attr }); return(true); }
public static bool GetPacketTypeInfo(Type type, out int id, out int size) { object[] attrs = type.GetCustomAttributes(typeof(PacketTypeAttribute), false); if (attrs == null || attrs.Length == 0) { id = size = 0; return(false); } PacketTypeAttribute attr = (PacketTypeAttribute)attrs[0]; id = attr.Id; size = attr.Size; return(id > 0); }