From 5b880eadd917d5af4c6f84bea3b1640d63a65722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 22 Jan 2022 18:07:06 +0100 Subject: [PATCH] feat(packets)!: let the user add the default packet converters --- .../PacketTypesRepositoryExtensions.cs | 42 +++++++++++++++++++ .../Extensions/ServiceCollectionExtensions.cs | 19 +-------- .../Packets/IPacketTypesRepository.cs | 13 ++++++ .../Packets/PacketTypesRepository.cs | 28 +++++++++++-- 4 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 Packets/NosSmooth.PacketSerializer/Extensions/PacketTypesRepositoryExtensions.cs diff --git a/Packets/NosSmooth.PacketSerializer/Extensions/PacketTypesRepositoryExtensions.cs b/Packets/NosSmooth.PacketSerializer/Extensions/PacketTypesRepositoryExtensions.cs new file mode 100644 index 0000000..3782ce2 --- /dev/null +++ b/Packets/NosSmooth.PacketSerializer/Extensions/PacketTypesRepositoryExtensions.cs @@ -0,0 +1,42 @@ +// +// PacketTypesRepositoryExtensions.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Linq; +using System.Reflection; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Packets.Extensions; + +/// +/// Extension methods for . +/// +public static class PacketTypesRepositoryExtensions +{ + /// + /// Add packets from the given assembly. + /// + /// The packet types repository. + /// The assembly to add packets from. + /// A result that may or may not have succeeded. + public static Result AddPacketTypes(this IPacketTypesRepository packetTypesRepository, Assembly assembly) + { + var packetTypes = assembly + .GetExportedTypes() + .Where(x => x != typeof(UnresolvedPacket) && !x.IsAbstract && typeof(IPacket).IsAssignableFrom(x)); + return packetTypesRepository.AddPacketTypes(packetTypes); + } + + /// + /// Adds the default NosSmooth packets. + /// + /// The packet types repository. + /// A result tht may or may not have succeeded. + public static Result AddDefaultPackets(this IPacketTypesRepository packetTypesRepository) + { + return packetTypesRepository.AddPacketTypes(typeof(IPacket).Assembly); + } +} \ No newline at end of file diff --git a/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs b/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs index c4e2baf..e9dcea3 100644 --- a/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs +++ b/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs @@ -38,24 +38,7 @@ public static class ServiceCollectionExtensions .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(p => - { - var repository = new PacketTypesRepository(p.GetRequiredService()); - var packetTypes = typeof(IPacket).Assembly - .GetExportedTypes() - .Where(x => x != typeof(UnresolvedPacket) && !x.IsAbstract && typeof(IPacket).IsAssignableFrom(x)); - foreach (var packetType in packetTypes) - { - var result = repository.AddPacketType(packetType); - if (!result.IsSuccess) - { - // TODO: figure out how to handle this. - throw new Exception(packetType + ": " + result.Error.Message); - } - } - - return repository; - }) + .AddSingleton() .AddGeneratedSerializers(typeof(IPacket).Assembly) .AddBasicConverters(); } diff --git a/Packets/NosSmooth.PacketSerializer/Packets/IPacketTypesRepository.cs b/Packets/NosSmooth.PacketSerializer/Packets/IPacketTypesRepository.cs index 89923c1..4760252 100644 --- a/Packets/NosSmooth.PacketSerializer/Packets/IPacketTypesRepository.cs +++ b/Packets/NosSmooth.PacketSerializer/Packets/IPacketTypesRepository.cs @@ -25,6 +25,19 @@ public interface IPacketTypesRepository /// A result that may or may not have succeeded. public Result AddPacketType(Type type); + /// + /// Add all of the given packet types. + /// + /// + /// If there is an error, it will continue to add the rest of the types + /// and then return an aggregate error containing all the errors. + /// + /// The application can still run, but without the errorful packets. + /// + /// The types add. + /// A result that may or may not have succeeded. + public Result AddPacketTypes(IEnumerable packetTypes); + /// /// Gets the type of a packet that corresponds to the given header. /// diff --git a/Packets/NosSmooth.PacketSerializer/Packets/PacketTypesRepository.cs b/Packets/NosSmooth.PacketSerializer/Packets/PacketTypesRepository.cs index b6d8024..fd091ef 100644 --- a/Packets/NosSmooth.PacketSerializer/Packets/PacketTypesRepository.cs +++ b/Packets/NosSmooth.PacketSerializer/Packets/PacketTypesRepository.cs @@ -46,13 +46,13 @@ public class PacketTypesRepository : IPacketTypesRepository { if (!typeof(IPacket).IsAssignableFrom(type)) { - return new ArgumentInvalidError(nameof(type), "The type has to be assignable to IPacket."); + return new ArgumentInvalidError(nameof(type), $"The type has to be assignable to IPacket. {type.FullName} isn't."); } var header = type.GetCustomAttribute(); if (header is null) { - return new ArgumentInvalidError(nameof(type), "Every packet has to specify the header."); + return new ArgumentInvalidError(nameof(type), $"Every packet has to specify the header. {type.FullName} didn't."); } var converterResult = _stringConverterRepository.GetTypeConverter(type); @@ -91,6 +91,27 @@ public class PacketTypesRepository : IPacketTypesRepository return Result.FromSuccess(); } + /// + public Result AddPacketTypes(IEnumerable packetTypes) + { + var errorResults = new List(); + foreach (var packetType in packetTypes) + { + var result = AddPacketType(packetType); + if (!result.IsSuccess) + { + errorResults.Add(result); + } + } + + return errorResults.Count switch + { + 0 => Result.FromSuccess(), + 1 => (Result)errorResults[0], + _ => new AggregateError(errorResults) + }; + } + /// /// Gets the type of a packet that corresponds to the given header. /// @@ -133,7 +154,8 @@ public class PacketTypesRepository : IPacketTypesRepository /// /// The type of the packet. /// Info that stores the packet's info. Or an error, if not found. - public Result FindPacketInfo() where TPacket : IPacket + public Result FindPacketInfo() + where TPacket : IPacket => FindPacketInfo(typeof(TPacket)); /// -- 2.49.0