From 499c970127152e470458e2550a1e06ad9d66acf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 21 Dec 2021 22:43:47 +0100 Subject: [PATCH] feat: add nostale game service collection extension --- .../Extensions/ServiceCollectionExtensions.cs | 123 +++++++++++------- 1 file changed, 79 insertions(+), 44 deletions(-) diff --git a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs index e2466c7..6c8f02e 100644 --- a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs +++ b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs @@ -1,44 +1,79 @@ - - /// - /// Adds the given game event responder. - /// - /// The service collection. - /// The responder to add. - /// The collection. - public static IServiceCollection AddGameResponder(this IServiceCollection serviceCollection) - where TGameResponder : IGameResponder - { - return serviceCollection.AddGameResponder(typeof(TGameResponder)); - } - - /// - /// Adds the given game event responder. - /// - /// The service collection. - /// The type of the event responder. - /// The collection. - public static IServiceCollection AddGameResponder(this IServiceCollection serviceCollection, Type gameResponder) - { - if (!gameResponder.GetInterfaces().Any( - i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IGameResponder<>) - )) - { - throw new ArgumentException( - $"{nameof(gameResponder)} should implement IGameResponder.", - nameof(gameResponder)); - } - - var handlerTypeInterfaces = gameResponder.GetInterfaces(); - var handlerInterfaces = handlerTypeInterfaces.Where - ( - r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(IGameResponder<>) - ); - - foreach (var handlerInterface in handlerInterfaces) - { - serviceCollection.AddScoped(handlerInterface, gameResponder); - } - - return serviceCollection; - } -} \ No newline at end of file +// +// ServiceCollectionExtensions.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 Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using NosSmooth.Core.Commands; +using NosSmooth.Core.Extensions; +using NosSmooth.Game.Events.Handlers; + +namespace NosSmooth.Game.Extensions; + +/// +/// Contains extension methods for . +/// +public static class ServiceCollectionExtensions +{ + /// + /// Adds handling of nostale packets, registering singleton and dispatching of game events. + /// + /// The service collection. + /// The collection. + public static IServiceCollection AddNostaleGame(this IServiceCollection serviceCollection) + { + serviceCollection + .AddNostaleCore() + .AddMemoryCache() + .TryAddSingleton(); + serviceCollection.TryAddSingleton(); + + // TODO: add events + return serviceCollection; + } + + /// + /// Adds the given game event responder. + /// + /// The service collection. + /// The responder to add. + /// The collection. + public static IServiceCollection AddGameResponder(this IServiceCollection serviceCollection) + where TGameResponder : IGameResponder + { + return serviceCollection.AddGameResponder(typeof(TGameResponder)); + } + + /// + /// Adds the given game event responder. + /// + /// The service collection. + /// The type of the event responder. + /// The collection. + public static IServiceCollection AddGameResponder(this IServiceCollection serviceCollection, Type gameResponder) + { + if (!gameResponder.GetInterfaces().Any( + i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IGameResponder<>) + )) + { + throw new ArgumentException( + $"{nameof(gameResponder)} should implement IGameResponder.", + nameof(gameResponder)); + } + + var handlerTypeInterfaces = gameResponder.GetInterfaces(); + var handlerInterfaces = handlerTypeInterfaces.Where + ( + r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(IGameResponder<>) + ); + + foreach (var handlerInterface in handlerInterfaces) + { + serviceCollection.AddScoped(handlerInterface, gameResponder); + } + + return serviceCollection; + } +} -- 2.48.1