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()
{
// ! : 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<PacketIdAttribute<TPacketIdEnum>>().Any()
))
.Select(type =>
new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() })
.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<TPacketIdEnum, TSession> where TPacketIdEnum : En
private Dictionary<TPacketIdEnum, Type> 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<TSession>)))
.Select(type => new
{
Type = type,
PacketId = type
.GetInterfaces().First(t1 =>
t1 is { IsGenericType: true } &&
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>))
.GetGenericArguments().First(t => t.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any())
.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().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<TSession>)))
.Select(type => new
{
Type = type,
PacketId = type
.GetInterfaces().First(t1 =>
t1 is { IsGenericType: true } &&
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<TSession>))
.GetGenericArguments().First(t =>
t.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any())
.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().First().Code
}))
.ToDictionary(x => x.PacketId, x => x.Type);
return packetHandlersWithId;
}
@ -139,7 +138,6 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
var packet = func(packetData);
// ! I don't see how it's possibly null here.
await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session, cancellationToken)!;
}
}