WutheringWaves/GameServer/Extensions/ServiceCollectionExtensions.cs

35 lines
1,020 B
C#
Raw Permalink Normal View History

2024-02-07 21:41:39 +00:00
using System.Reflection;
2024-02-09 22:15:05 +00:00
using GameServer.Controllers;
2024-02-14 21:22:21 +00:00
using GameServer.Controllers.Attributes;
2024-02-07 21:41:39 +00:00
using Microsoft.Extensions.DependencyInjection;
namespace GameServer.Extensions;
internal static class ServiceCollectionExtensions
{
2024-02-09 22:15:05 +00:00
public static IServiceCollection AddControllers(this IServiceCollection services)
2024-02-07 21:41:39 +00:00
{
2024-02-09 22:15:05 +00:00
IEnumerable<Type> controllerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsAssignableTo(typeof(Controller)) && !t.IsAbstract);
2024-02-07 21:41:39 +00:00
2024-02-09 22:15:05 +00:00
foreach (Type type in controllerTypes)
2024-02-07 21:41:39 +00:00
{
services.AddScoped(type);
}
return services;
}
2024-02-14 21:22:21 +00:00
public static IServiceCollection AddCommands(this IServiceCollection services)
{
IEnumerable<Type> handlerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.GetCustomAttribute<ChatCommandCategoryAttribute>() != null);
foreach (Type type in handlerTypes)
{
services.AddScoped(type);
}
return services;
}
2024-02-07 21:41:39 +00:00
}