From 63ddbd707faacde0c797259c9902f5f6c6119da0 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 5 Feb 2024 16:31:54 +0100 Subject: [PATCH] chore: minor adjustments --- Rai.PacketMediator/PacketDistributor.cs | 44 ++++++++++++------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Rai.PacketMediator/PacketDistributor.cs b/Rai.PacketMediator/PacketDistributor.cs index 6987afc..5ed9c63 100644 --- a/Rai.PacketMediator/PacketDistributor.cs +++ b/Rai.PacketMediator/PacketDistributor.cs @@ -76,14 +76,14 @@ public class PacketDistributor where TPacketIdEnum : En private Dictionary GetAllPackets() { - // ! : item.Attribute cannot be null due to previous Where check var packetsWithId = this._sourcesContainingPackets.SelectMany(a => a.GetTypes() - .Where(type => type is { IsInterface: false, IsAbstract: false } && - type.GetInterfaces().Contains(typeof(IIncomingPacket))) + .Where(type => type is { IsInterface: false, IsAbstract: false } && + type.GetInterfaces().Contains(typeof(IIncomingPacket)) + && type.GetCustomAttributes>().Any() + )) .Select(type => new { Type = type, Attribute = type.GetCustomAttribute>() }) - .Where(item => item.Attribute is not null) - .ToDictionary(item => item.Attribute!.Code, item => item.Type)).ToDictionary(); + .ToDictionary(item => item.Attribute!.Code, item => item.Type); return packetsWithId; } @@ -91,23 +91,22 @@ public class PacketDistributor where TPacketIdEnum : En private Dictionary GetAllPacketHandlersWithId() { var packetHandlersWithId = this._sourcesContainingPackets.SelectMany(assembly => assembly.GetTypes() - .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().First(t => t.GetCustomAttributes>().Any()) - .GetCustomAttributes>().First().Code - }) - .ToDictionary( - x => x.PacketId, x => x.Type - )).ToDictionary(); + .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().First(t => + t.GetCustomAttributes>().Any()) + .GetCustomAttributes>().First().Code + })) + .ToDictionary(x => x.PacketId, x => x.Type); return packetHandlersWithId; } @@ -139,7 +138,6 @@ public class PacketDistributor where TPacketIdEnum : En var packet = func(packetData); - // ! I don't see how it's possibly null here. await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session, cancellationToken)!; } }