From 24893b861adec551fbc2aedee5ec609169823979 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 6 Nov 2023 10:16:42 +0100 Subject: [PATCH] refactor: rewrite for packethandler/id map --- Server/Services/PacketDistributorService.cs | 24 +++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Server/Services/PacketDistributorService.cs b/Server/Services/PacketDistributorService.cs index 24d0e7d..107fbce 100644 --- a/Server/Services/PacketDistributorService.cs +++ b/Server/Services/PacketDistributorService.cs @@ -95,12 +95,24 @@ public class PacketDistributorService : IHostedService private Dictionary GetAllPacketHandlersWithId(Assembly assembly) { - var packetHandlersWithId = assembly.GetTypes().AsParallel().Where(t => - t is { IsClass: true, IsAbstract: false } && t - .GetInterfaces().Any(i => - i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type => type.GetInterfaces().First(t => - t is { IsGenericType: true } && t.GetGenericTypeDefinition() == typeof(IPacketHandler<>)) - .GetGenericArguments().First().GetCustomAttribute().Code); + // ! : We are filtering if types that don't have an instance of the required Attribute + var packetHandlersWithId = assembly.GetTypes().AsParallel() + .Where(t => + t is { IsClass: true, IsAbstract: false } && Array.Exists(t + .GetInterfaces(), i => + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))) + .Select(type => new + { + Type = type, + PacketId = type + .GetInterfaces().First(t1 => + t1 is { IsGenericType: true } && t1.GetGenericTypeDefinition() == typeof(IPacketHandler<>)) + .GetGenericArguments()[0].GetCustomAttribute()?.Code + }) + .Where(x => x.PacketId is not null) + .ToDictionary( + x => x.PacketId!.Value, x => x.Type + ); if (packetHandlersWithId is not { Count: 0 }) {