mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Re-added medusa system, but untested
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
using DryIoc;
|
||||
using LinqToDB.Extensions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NadekoBot.Modules.Music;
|
||||
using NadekoBot.Modules.Music.Resolvers;
|
||||
using NadekoBot.Modules.Music.Services;
|
||||
using Ninject.Extensions.Conventions.Syntax;
|
||||
using StackExchange.Redis;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using Ninject.Infrastructure.Language;
|
||||
|
||||
namespace NadekoBot.Extensions;
|
||||
|
||||
@@ -32,63 +31,32 @@ public static class ServiceCollectionExtensions
|
||||
return svcs;
|
||||
}
|
||||
|
||||
public static IContainer AddConfigServices(this IContainer kernel, Assembly a)
|
||||
public static IContainer AddConfigServices(this IContainer svcs, Assembly a)
|
||||
{
|
||||
// kernel.RegisterMany([typeof(ConfigServiceBase<>)]);
|
||||
|
||||
foreach (var type in a.GetTypes()
|
||||
.Where(x => !x.IsAbstract && x.IsAssignableToGenericType(typeof(ConfigServiceBase<>))))
|
||||
{
|
||||
kernel.RegisterMany([type],
|
||||
svcs.RegisterMany([type],
|
||||
getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
|
||||
getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
|
||||
}
|
||||
|
||||
//
|
||||
// kernel.Bind(x =>
|
||||
// {
|
||||
// var configs = x.From(a)
|
||||
// .SelectAllClasses()
|
||||
// .Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
|
||||
//
|
||||
// configs.BindToSelfWithInterfaces()
|
||||
// .Configure(c => c.InSingletonScope());
|
||||
// });
|
||||
|
||||
return kernel;
|
||||
|
||||
return svcs;
|
||||
}
|
||||
|
||||
public static IContainer AddConfigMigrators(this IContainer kernel, Assembly a)
|
||||
=> kernel.AddSealedSubclassesOf(typeof(IConfigMigrator), a);
|
||||
|
||||
public static IContainer AddMusic(this IContainer kernel)
|
||||
public static IContainer AddMusic(this IContainer svcs)
|
||||
{
|
||||
kernel.RegisterMany<MusicService>(Reuse.Singleton);
|
||||
svcs.RegisterMany<MusicService>(Reuse.Singleton);
|
||||
|
||||
kernel.AddSingleton<ITrackResolveProvider, TrackResolveProvider>();
|
||||
kernel.AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>();
|
||||
kernel.AddSingleton<ILocalTrackResolver, LocalTrackResolver>();
|
||||
kernel.AddSingleton<IRadioResolver, RadioResolver>();
|
||||
kernel.AddSingleton<ITrackCacher, TrackCacher>();
|
||||
svcs.AddSingleton<ITrackResolveProvider, TrackResolveProvider>();
|
||||
svcs.AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>();
|
||||
svcs.AddSingleton<ILocalTrackResolver, LocalTrackResolver>();
|
||||
svcs.AddSingleton<IRadioResolver, RadioResolver>();
|
||||
svcs.AddSingleton<ITrackCacher, TrackCacher>();
|
||||
|
||||
return kernel;
|
||||
}
|
||||
|
||||
public static IContainer AddSealedSubclassesOf(this IContainer cont, Type baseType, Assembly a)
|
||||
{
|
||||
var classes = a.GetExportedTypes()
|
||||
.Where(x => x.IsClass && !x.IsAbstract && x.IsPublic)
|
||||
.Where(x => x.IsNested && baseType.IsAssignableFrom(x));
|
||||
|
||||
foreach (var c in classes)
|
||||
{
|
||||
cont.RegisterMany([c], Reuse.Singleton);
|
||||
// var inters = c.GetInterfaces();
|
||||
|
||||
// cont.RegisterMany(inters, c);
|
||||
}
|
||||
|
||||
return cont;
|
||||
return svcs;
|
||||
}
|
||||
|
||||
public static IContainer AddCache(this IContainer cont, IBotCredentials creds)
|
||||
@@ -110,33 +78,31 @@ public static class ServiceCollectionExtensions
|
||||
.AddBotStringsServices(creds.BotCache);
|
||||
}
|
||||
|
||||
public static IContainer AddHttpClients(this IContainer kernel)
|
||||
public static IContainer AddHttpClients(this IContainer svcs)
|
||||
{
|
||||
IServiceCollection svcs = new ServiceCollection();
|
||||
svcs.AddHttpClient();
|
||||
svcs.AddHttpClient("memelist")
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
});
|
||||
IServiceCollection proxySvcs = new ServiceCollection();
|
||||
proxySvcs.AddHttpClient();
|
||||
proxySvcs.AddHttpClient("memelist")
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
});
|
||||
|
||||
svcs.AddHttpClient("google:search")
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||
});
|
||||
proxySvcs.AddHttpClient("google:search")
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||
});
|
||||
|
||||
var prov = svcs.BuildServiceProvider();
|
||||
kernel.RegisterDelegate<IHttpClientFactory>(_ => prov.GetRequiredService<IHttpClientFactory>());
|
||||
kernel.RegisterDelegate<HttpClient>(_ => prov.GetRequiredService<HttpClient>());
|
||||
var prov = proxySvcs.BuildServiceProvider();
|
||||
|
||||
svcs.RegisterDelegate<IHttpClientFactory>(_ => prov.GetRequiredService<IHttpClientFactory>());
|
||||
svcs.RegisterDelegate<HttpClient>(_ => prov.GetRequiredService<HttpClient>());
|
||||
|
||||
return kernel;
|
||||
return svcs;
|
||||
}
|
||||
|
||||
public static IConfigureSyntax BindToSelfWithInterfaces(this IJoinExcludeIncludeBindSyntax matcher)
|
||||
=> matcher.BindSelection((type, types) => types.Append(type));
|
||||
|
||||
public static IContainer AddLifetimeServices(this IContainer kernel, Assembly a)
|
||||
public static IContainer AddLifetimeServices(this IContainer svcs, Assembly a)
|
||||
{
|
||||
Type[] types =
|
||||
[
|
||||
@@ -149,27 +115,17 @@ public static class ServiceCollectionExtensions
|
||||
];
|
||||
|
||||
foreach (var svc in a.GetTypes()
|
||||
.Where(type => type.IsClass && types.Any(t => type.IsAssignableTo(t)) && !type.HasAttribute<DIIgnoreAttribute>()))
|
||||
.Where(type => type.IsClass && types.Any(t => type.IsAssignableTo(t)) && !type.HasAttribute<DIIgnoreAttribute>()
|
||||
#if GLOBAL_NADEKO
|
||||
&& !type.HasAttribute<NoPublicBotAttribute>()
|
||||
#endif
|
||||
))
|
||||
{
|
||||
kernel.RegisterMany([svc],
|
||||
svcs.RegisterMany([svc],
|
||||
getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
|
||||
getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
|
||||
}
|
||||
//
|
||||
// kernel.RegisterMany(
|
||||
// [a],
|
||||
// #if GLOBAL_NADEKO
|
||||
// && !c.HasAttribute<NoPublicBotAttribute>()
|
||||
// #endif
|
||||
// ),
|
||||
// reuse:
|
||||
// Reuse.Singleton
|
||||
// );
|
||||
|
||||
|
||||
// todo maybe self is missing
|
||||
// todo maybe attribute doesn't work
|
||||
|
||||
return kernel;
|
||||
return svcs;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user