| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Discovers and caches source-generated compact callback data codecs from payload |
| | | 8 | | /// assemblies so keyboard packing and callback routing can share the generated path. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class GeneratedCallbackDataCodecRegistry |
| | | 11 | | { |
| | 1 | 12 | | private static readonly ConcurrentDictionary<Assembly, Lazy<AssemblyCodecLookup>> AssemblyCache = new(); |
| | | 13 | | |
| | | 14 | | public static bool TryGet(Type payloadType, out GeneratedCallbackDataCodec codec) |
| | | 15 | | { |
| | 91 | 16 | | ArgumentNullException.ThrowIfNull(payloadType); |
| | | 17 | | |
| | 91 | 18 | | var lookup = AssemblyCache.GetOrAdd( |
| | 91 | 19 | | payloadType.Assembly, |
| | 93 | 20 | | static assembly => new Lazy<AssemblyCodecLookup>( |
| | 2 | 21 | | () => CreateAssemblyLookup(assembly), |
| | 93 | 22 | | LazyThreadSafetyMode.ExecutionAndPublication)).Value; |
| | | 23 | | |
| | 91 | 24 | | return lookup.TryGet(payloadType, out codec); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private static AssemblyCodecLookup CreateAssemblyLookup(Assembly assembly) |
| | | 28 | | { |
| | 2 | 29 | | var attributes = assembly |
| | 2 | 30 | | .GetCustomAttributes<TelegramGeneratedCallbackDataCodecsAttribute>() |
| | 0 | 31 | | .OrderBy(static attribute => attribute.RegistrarType.FullName, StringComparer.Ordinal) |
| | 2 | 32 | | .ToArray(); |
| | | 33 | | |
| | 2 | 34 | | if (attributes.Length == 0) |
| | | 35 | | { |
| | 1 | 36 | | return AssemblyCodecLookup.Empty; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | var registry = new Registry(); |
| | | 40 | | |
| | 4 | 41 | | foreach (var attribute in attributes) |
| | | 42 | | { |
| | 1 | 43 | | var registrar = CreateRegistrar(attribute.RegistrarType); |
| | | 44 | | |
| | 1 | 45 | | registrar.RegisterCallbackDataCodecs(registry); |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | return registry.ToLookup(); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private static ITelegramGeneratedCallbackDataCodecRegistrar CreateRegistrar(Type registrarType) |
| | | 52 | | { |
| | 1 | 53 | | if (!typeof(ITelegramGeneratedCallbackDataCodecRegistrar).IsAssignableFrom(registrarType)) |
| | | 54 | | { |
| | 0 | 55 | | throw new InvalidOperationException( |
| | 0 | 56 | | $"Generated Telegram callback data codec registrar '{registrarType.FullName}' must implement {nameof(ITe |
| | | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | if (Activator.CreateInstance(registrarType) is not ITelegramGeneratedCallbackDataCodecRegistrar registrar) |
| | | 60 | | { |
| | 0 | 61 | | throw new InvalidOperationException( |
| | 0 | 62 | | $"Generated Telegram callback data codec registrar '{registrarType.FullName}' could not be created."); |
| | | 63 | | } |
| | | 64 | | |
| | 1 | 65 | | return registrar; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private sealed class Registry : ITelegramGeneratedCallbackDataCodecRegistry |
| | | 69 | | { |
| | 1 | 70 | | private readonly List<GeneratedCallbackDataCodec> _codecs = []; |
| | | 71 | | |
| | | 72 | | public void RegisterCallbackDataCodec(TelegramGeneratedCallbackDataCodecDescriptor descriptor) |
| | | 73 | | { |
| | 1 | 74 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | | 75 | | |
| | 1 | 76 | | _codecs.Add(new GeneratedCallbackDataCodec(descriptor)); |
| | 1 | 77 | | } |
| | | 78 | | |
| | | 79 | | public AssemblyCodecLookup ToLookup() |
| | | 80 | | { |
| | 1 | 81 | | return AssemblyCodecLookup.Create(_codecs); |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private sealed class AssemblyCodecLookup |
| | | 86 | | { |
| | 1 | 87 | | public static readonly AssemblyCodecLookup Empty = new( |
| | 1 | 88 | | new Dictionary<Type, GeneratedCallbackDataCodec>()); |
| | | 89 | | |
| | | 90 | | private readonly IReadOnlyDictionary<Type, GeneratedCallbackDataCodec> _codecs; |
| | | 91 | | |
| | | 92 | | private AssemblyCodecLookup(IReadOnlyDictionary<Type, GeneratedCallbackDataCodec> codecs) |
| | | 93 | | { |
| | 2 | 94 | | _codecs = codecs; |
| | 2 | 95 | | } |
| | | 96 | | |
| | | 97 | | public static AssemblyCodecLookup Create(IReadOnlyList<GeneratedCallbackDataCodec> codecs) |
| | | 98 | | { |
| | 1 | 99 | | var duplicateType = codecs |
| | 1 | 100 | | .GroupBy(static codec => codec.PayloadType) |
| | 2 | 101 | | .FirstOrDefault(static group => group.Count() > 1); |
| | | 102 | | |
| | 1 | 103 | | if (duplicateType is not null) |
| | | 104 | | { |
| | 0 | 105 | | throw new InvalidOperationException( |
| | 0 | 106 | | $"Generated Telegram callback data payload type '{duplicateType.Key.FullName}' was registered more t |
| | | 107 | | } |
| | | 108 | | |
| | 1 | 109 | | var duplicatePrefix = codecs |
| | 1 | 110 | | .GroupBy(static codec => codec.Prefix, StringComparer.Ordinal) |
| | 2 | 111 | | .FirstOrDefault(static group => group.Count() > 1); |
| | | 112 | | |
| | 1 | 113 | | if (duplicatePrefix is not null) |
| | | 114 | | { |
| | 0 | 115 | | var payloadTypes = string.Join( |
| | 0 | 116 | | ", ", |
| | 0 | 117 | | duplicatePrefix.Select(static codec => codec.PayloadType.FullName)); |
| | | 118 | | |
| | 0 | 119 | | throw new InvalidOperationException( |
| | 0 | 120 | | $"Generated Telegram callback data prefix '{duplicatePrefix.Key}' is used by multiple payload types: |
| | | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | return new AssemblyCodecLookup(codecs.ToDictionary(static codec => codec.PayloadType)); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | public bool TryGet(Type payloadType, out GeneratedCallbackDataCodec codec) |
| | | 127 | | { |
| | 91 | 128 | | return _codecs.TryGetValue(payloadType, out codec!); |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Runtime wrapper around generated callback data delegates that validates payload |
| | | 135 | | /// compatibility and Telegram callback data size at framework boundaries. |
| | | 136 | | /// </summary> |
| | | 137 | | internal sealed class GeneratedCallbackDataCodec |
| | | 138 | | { |
| | | 139 | | private readonly TelegramGeneratedCallbackDataCodecDescriptor _descriptor; |
| | | 140 | | |
| | | 141 | | public GeneratedCallbackDataCodec(TelegramGeneratedCallbackDataCodecDescriptor descriptor) |
| | | 142 | | { |
| | | 143 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | | 144 | | |
| | | 145 | | _descriptor = descriptor; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public Type PayloadType => _descriptor.PayloadType; |
| | | 149 | | |
| | | 150 | | public string Prefix => _descriptor.Prefix; |
| | | 151 | | |
| | | 152 | | public string Pack(object payload) |
| | | 153 | | { |
| | | 154 | | ArgumentNullException.ThrowIfNull(payload); |
| | | 155 | | |
| | | 156 | | if (!PayloadType.IsInstanceOfType(payload)) |
| | | 157 | | { |
| | | 158 | | throw new InvalidOperationException( |
| | | 159 | | $"Callback data payload type '{payload.GetType().FullName}' is not compatible with generated codec for ' |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | var serializedPayload = _descriptor.Packer(payload); |
| | | 163 | | CallbackDataCodec.ValidateCallbackData(serializedPayload, "Serialized Telegram callback data"); |
| | | 164 | | return serializedPayload; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public bool MatchesSerializedPayload(string serializedPayload) |
| | | 168 | | { |
| | | 169 | | ArgumentNullException.ThrowIfNull(serializedPayload); |
| | | 170 | | |
| | | 171 | | return _descriptor.Matcher(serializedPayload); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | public object Unpack(string serializedPayload) |
| | | 175 | | { |
| | | 176 | | ArgumentNullException.ThrowIfNull(serializedPayload); |
| | | 177 | | |
| | | 178 | | return _descriptor.Unpacker(serializedPayload); |
| | | 179 | | } |
| | | 180 | | } |