chore: minor adjustments

This commit is contained in:
Timothy Schenk 2024-02-05 16:31:54 +01:00
parent 0c2accd72c
commit 63ddbd707f
Signed by: rainote
SSH key fingerprint: SHA256:pnkNSDwpAnaip00xaZlVFHKKsS7T8UtOomMzvs0yITE

View file

@ -76,14 +76,14 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
private Dictionary<TPacketIdEnum, Type> GetAllPackets() private Dictionary<TPacketIdEnum, Type> GetAllPackets()
{ {
// ! : item.Attribute cannot be null due to previous Where check
var packetsWithId = this._sourcesContainingPackets.SelectMany(a => a.GetTypes() var packetsWithId = this._sourcesContainingPackets.SelectMany(a => a.GetTypes()
.Where(type => type is { IsInterface: false, IsAbstract: false } && .Where(type => type is { IsInterface: false, IsAbstract: false } &&
type.GetInterfaces().Contains(typeof(IIncomingPacket))) type.GetInterfaces().Contains(typeof(IIncomingPacket))
&& type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
))
.Select(type => .Select(type =>
new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() }) new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() })
.Where(item => item.Attribute is not null) .ToDictionary(item => item.Attribute!.Code, item => item.Type);
.ToDictionary(item => item.Attribute!.Code, item => item.Type)).ToDictionary();
return packetsWithId; return packetsWithId;
} }
@ -91,23 +91,22 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
private Dictionary<TPacketIdEnum, Type> GetAllPacketHandlersWithId() private Dictionary<TPacketIdEnum, Type> GetAllPacketHandlersWithId()
{ {
var packetHandlersWithId = this._sourcesContainingPackets.SelectMany(assembly => assembly.GetTypes() var packetHandlersWithId = this._sourcesContainingPackets.SelectMany(assembly => assembly.GetTypes()
.Where(t => .Where(t =>
t is { IsClass: true, IsAbstract: false } && Array.Exists(t t is { IsClass: true, IsAbstract: false } && Array.Exists(t
.GetInterfaces(), i => .GetInterfaces(), i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>))) i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>)))
.Select(type => new .Select(type => new
{ {
Type = type, Type = type,
PacketId = type PacketId = type
.GetInterfaces().First(t1 => .GetInterfaces().First(t1 =>
t1 is { IsGenericType: true } && t1 is { IsGenericType: true } &&
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>)) t1.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>))
.GetGenericArguments().First(t => t.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()) .GetGenericArguments().First(t =>
.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().First().Code t.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any())
}) .GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().First().Code
.ToDictionary( }))
x => x.PacketId, x => x.Type .ToDictionary(x => x.PacketId, x => x.Type);
)).ToDictionary();
return packetHandlersWithId; return packetHandlersWithId;
} }
@ -139,7 +138,6 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
var packet = func(packetData); var packet = func(packetData);
// ! I don't see how it's possibly null here.
await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session, cancellationToken)!; await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session, cancellationToken)!;
} }
} }