refactor: rewrite for packethandler/id map
This commit is contained in:
parent
b7d6865722
commit
24893b861a
1 changed files with 18 additions and 6 deletions
|
@ -95,12 +95,24 @@ public class PacketDistributorService : IHostedService
|
||||||
|
|
||||||
private Dictionary<OperationCode, Type> GetAllPacketHandlersWithId(Assembly assembly)
|
private Dictionary<OperationCode, Type> GetAllPacketHandlersWithId(Assembly assembly)
|
||||||
{
|
{
|
||||||
var packetHandlersWithId = assembly.GetTypes().AsParallel().Where(t =>
|
// ! : We are filtering if types that don't have an instance of the required Attribute
|
||||||
t is { IsClass: true, IsAbstract: false } && t
|
var packetHandlersWithId = assembly.GetTypes().AsParallel()
|
||||||
.GetInterfaces().Any(i =>
|
.Where(t =>
|
||||||
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type => type.GetInterfaces().First(t =>
|
t is { IsClass: true, IsAbstract: false } && Array.Exists(t
|
||||||
t is { IsGenericType: true } && t.GetGenericTypeDefinition() == typeof(IPacketHandler<>))
|
.GetInterfaces(), i =>
|
||||||
.GetGenericArguments().First().GetCustomAttribute<PacketIdAttribute>().Code);
|
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<PacketIdAttribute>()?.Code
|
||||||
|
})
|
||||||
|
.Where(x => x.PacketId is not null)
|
||||||
|
.ToDictionary(
|
||||||
|
x => x.PacketId!.Value, x => x.Type
|
||||||
|
);
|
||||||
|
|
||||||
if (packetHandlersWithId is not { Count: 0 })
|
if (packetHandlersWithId is not { Count: 0 })
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue