| | | 1 | | using System.Reflection; |
| | | 2 | | using TeleFlow.Annotations; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 5 | | |
| | | 6 | | internal static class TelegramHandlerAssemblyScanner |
| | | 7 | | { |
| | | 8 | | public static TelegramGeneratedHandlersAttribute[] GetGeneratedRegistrars(Assembly assembly) |
| | | 9 | | { |
| | 39 | 10 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 11 | | |
| | 39 | 12 | | return assembly |
| | 39 | 13 | | .GetCustomAttributes<TelegramGeneratedHandlersAttribute>() |
| | 0 | 14 | | .OrderBy(static attribute => attribute.RegistrarType.FullName, StringComparer.Ordinal) |
| | 39 | 15 | | .ToArray(); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public static Type[] GetHandlerTypes(Assembly assembly) |
| | | 19 | | { |
| | 14 | 20 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 21 | | |
| | 14 | 22 | | return GetAssemblyTypes(assembly) |
| | 2810 | 23 | | .Where(static type => type is { IsAbstract: false, IsInterface: false }) |
| | 14 | 24 | | .Where(HasTelegramHandlerMethods) |
| | 434 | 25 | | .OrderBy(static type => type.FullName, StringComparer.Ordinal) |
| | 14 | 26 | | .ToArray(); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | private static Type[] GetAssemblyTypes(Assembly assembly) |
| | | 30 | | { |
| | | 31 | | try |
| | | 32 | | { |
| | 14 | 33 | | return assembly.GetTypes(); |
| | | 34 | | } |
| | 1 | 35 | | catch (ReflectionTypeLoadException exception) |
| | | 36 | | { |
| | 1 | 37 | | var loaderMessages = exception.LoaderExceptions |
| | 1 | 38 | | .Where(static loaderException => loaderException is not null) |
| | 1 | 39 | | .Select(static loaderException => loaderException!.Message) |
| | 1 | 40 | | .Where(static message => !string.IsNullOrWhiteSpace(message)) |
| | 1 | 41 | | .Distinct(StringComparer.Ordinal) |
| | 1 | 42 | | .Take(5) |
| | 1 | 43 | | .ToArray(); |
| | 1 | 44 | | var details = loaderMessages.Length == 0 |
| | 1 | 45 | | ? string.Empty |
| | 1 | 46 | | : $" Loader exceptions: {string.Join(" | ", loaderMessages)}"; |
| | | 47 | | |
| | 1 | 48 | | throw new InvalidOperationException( |
| | 1 | 49 | | $"Could not load Telegram handler types from assembly '{assembly.FullName}'.{details}", |
| | 1 | 50 | | exception); |
| | | 51 | | } |
| | 13 | 52 | | } |
| | | 53 | | |
| | | 54 | | private static bool HasTelegramHandlerMethods(Type handlerType) |
| | | 55 | | { |
| | 2783 | 56 | | if (IsClassBasedHandlerType(handlerType) && |
| | 2783 | 57 | | HasDeclaredHandleAsync(handlerType) && |
| | 2783 | 58 | | HasRouteAttributes(handlerType)) |
| | | 59 | | { |
| | 14 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | |
| | 2769 | 63 | | return handlerType |
| | 2769 | 64 | | .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) |
| | 6117 | 65 | | .Any(static method => !method.IsSpecialName && |
| | 6117 | 66 | | (HasRouteAttributes(method) || HasErrorAttributes(method))); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static bool IsClassBasedHandlerType(Type handlerType) |
| | | 70 | | { |
| | 2783 | 71 | | return typeof(MessageHandler).IsAssignableFrom(handlerType) || |
| | 2783 | 72 | | typeof(CallbackHandler).IsAssignableFrom(handlerType) || |
| | 2783 | 73 | | typeof(ChatMemberUpdateHandler).IsAssignableFrom(handlerType); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private static bool HasDeclaredHandleAsync(Type handlerType) |
| | | 77 | | { |
| | 16 | 78 | | return handlerType |
| | 16 | 79 | | .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) |
| | 30 | 80 | | .Any(static method => !method.IsSpecialName && |
| | 30 | 81 | | string.Equals(method.Name, "HandleAsync", StringComparison.Ordinal)); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | private static bool HasRouteAttributes(MemberInfo member) |
| | | 85 | | { |
| | 2898 | 86 | | return member.GetCustomAttributes<CommandAttribute>(inherit: true).Any() || |
| | 2898 | 87 | | member.GetCustomAttributes<MessageAttribute>(inherit: true).Any() || |
| | 2898 | 88 | | member.GetCustomAttributes<TextAttribute>(inherit: true).Any() || |
| | 2898 | 89 | | member.GetCustomAttributes<TextTemplateAttribute>(inherit: true).Any() || |
| | 2898 | 90 | | member.GetCustomAttributes<CommandTemplateAttribute>(inherit: true).Any() || |
| | 2898 | 91 | | member.GetCustomAttributes<TextRegexAttribute>(inherit: true).Any() || |
| | 2898 | 92 | | member.GetCustomAttributes<CommandRegexAttribute>(inherit: true).Any() || |
| | 2898 | 93 | | member.GetCustomAttributes<CallbackAttribute>(inherit: true).Any() || |
| | 2898 | 94 | | HasGenericCallbackAttribute(member) || |
| | 2898 | 95 | | member.GetCustomAttributes<ChatMemberUpdatedAttribute>(inherit: true).Any() || |
| | 2898 | 96 | | member.GetCustomAttributes<MyChatMemberUpdatedAttribute>(inherit: true).Any(); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static bool HasErrorAttributes(MemberInfo member) |
| | | 100 | | { |
| | 2496 | 101 | | return member.GetCustomAttributes<ErrorAttribute>(inherit: true).Any(); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static bool HasGenericCallbackAttribute(MemberInfo member) |
| | | 105 | | { |
| | 2537 | 106 | | return member |
| | 2537 | 107 | | .GetCustomAttributes(inherit: true) |
| | 3879 | 108 | | .Select(static attribute => attribute.GetType()) |
| | 2537 | 109 | | .Any(static type => |
| | 6416 | 110 | | type.IsGenericType && |
| | 6416 | 111 | | type.GetGenericTypeDefinition() == typeof(CallbackAttribute<>)); |
| | | 112 | | } |
| | | 113 | | } |