using DryIoc; using LinqToDB.Extensions; using Microsoft.Extensions.DependencyInjection; using NadekoBot.Modules.Music; using NadekoBot.Modules.Music.Resolvers; using NadekoBot.Modules.Music.Services; using StackExchange.Redis; using System.Net; using System.Reflection; using NadekoBot.Common.ModuleBehaviors; namespace NadekoBot.Extensions; public static class ServiceCollectionExtensions { public static IContainer AddBotStringsServices(this IContainer svcs, BotCacheImplemenation botCache) { if (botCache == BotCacheImplemenation.Memory) { svcs.AddSingleton(); svcs.AddSingleton(); } else { svcs.AddSingleton(); svcs.AddSingleton(); } svcs.AddSingleton(); return svcs; } public static IContainer AddConfigServices(this IContainer svcs, Assembly a) { foreach (var type in a.GetTypes() .Where(x => !x.IsAbstract && x.IsAssignableToGenericType(typeof(ConfigServiceBase<>)))) { svcs.RegisterMany([type], getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType), getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton)); } return svcs; } public static IContainer AddMusic(this IContainer svcs) { svcs.RegisterMany(Reuse.Singleton); svcs.AddSingleton(); svcs.AddSingleton(); svcs.AddSingleton(); svcs.AddSingleton(); svcs.AddSingleton(); return svcs; } public static IContainer AddCache(this IContainer cont, IBotCredentials creds) { if (creds.BotCache == BotCacheImplemenation.Redis) { var conf = ConfigurationOptions.Parse(creds.RedisOptions); cont.AddSingleton(ConnectionMultiplexer.Connect(conf)); cont.AddSingleton(); cont.AddSingleton(); } else { cont.AddSingleton(); cont.AddSingleton(); } return cont .AddBotStringsServices(creds.BotCache); } public static IContainer AddHttpClients(this IContainer svcs) { IServiceCollection proxySvcs = new ServiceCollection(); proxySvcs.AddHttpClient(); proxySvcs.AddHttpClient("memelist") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false }); proxySvcs.AddHttpClient("google:search") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }); var prov = proxySvcs.BuildServiceProvider(); svcs.RegisterDelegate(_ => prov.GetRequiredService()); svcs.RegisterDelegate(_ => prov.GetRequiredService()); return svcs; } public static IContainer AddLifetimeServices(this IContainer svcs, Assembly a) { Type[] types = [ typeof(IExecOnMessage), typeof(IExecPreCommand), typeof(IExecPostCommand), typeof(IExecNoCommand), typeof(IInputTransformer), typeof(INService) ]; foreach (var svc in a.GetTypes() .Where(type => type.IsClass && types.Any(t => type.IsAssignableTo(t)) && !type.HasAttribute() #if GLOBAL_NADEKO && !type.HasAttribute() #endif )) { svcs.RegisterMany([svc], getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType), getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton)); } return svcs; } }