* add: .prunecancel to cancel active prunes

* change: .qs improved with thumbnails
* change: .prune now reports progress
* dev: DryIoc replacing Ninject
This commit is contained in:
Kwoth
2024-05-04 06:33:45 +00:00
parent 7637de8fed
commit ea0b51d474
22 changed files with 418 additions and 235 deletions

View File

@@ -1,9 +1,8 @@
using DryIoc;
using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Modules.Music;
using NadekoBot.Modules.Music.Resolvers;
using NadekoBot.Modules.Music.Services;
using Ninject;
using Ninject.Extensions.Conventions;
using Ninject.Extensions.Conventions.Syntax;
using StackExchange.Redis;
using System.Net;
@@ -15,92 +14,103 @@ namespace NadekoBot.Extensions;
public static class ServiceCollectionExtensions
{
public static IKernel AddBotStringsServices(this IKernel kernel, BotCacheImplemenation botCache)
public static IContainer AddBotStringsServices(this IContainer svcs, BotCacheImplemenation botCache)
{
if (botCache == BotCacheImplemenation.Memory)
{
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope();
kernel.Bind<IBotStringsProvider>().To<MemoryBotStringsProvider>().InSingletonScope();
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
svcs.AddSingleton<IBotStringsProvider, MemoryBotStringsProvider>();
}
else
{
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope();
kernel.Bind<IBotStringsProvider>().To<RedisBotStringsProvider>().InSingletonScope();
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
svcs.AddSingleton<IBotStringsProvider, RedisBotStringsProvider>();
}
return kernel;
svcs.AddSingleton<IBotStrings, BotStrings>();
return svcs;
}
public static IKernel AddConfigServices(this IKernel kernel, Assembly a)
public static IContainer AddConfigServices(this IContainer kernel, Assembly a)
{
kernel.Bind(x =>
// kernel.RegisterMany([typeof(ConfigServiceBase<>)]);
foreach (var type in a.GetTypes()
.Where(x => !x.IsAbstract && x.IsAssignableToGenericType(typeof(ConfigServiceBase<>))))
{
var configs = x.From(a)
.SelectAllClasses()
.Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
kernel.RegisterMany([type],
getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
}
configs.BindToSelfWithInterfaces()
.Configure(c => c.InSingletonScope());
});
//
// kernel.Bind(x =>
// {
// var configs = x.From(a)
// .SelectAllClasses()
// .Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
//
// configs.BindToSelfWithInterfaces()
// .Configure(c => c.InSingletonScope());
// });
return kernel;
}
public static IKernel AddConfigMigrators(this IKernel kernel, Assembly a)
public static IContainer AddConfigMigrators(this IContainer kernel, Assembly a)
=> kernel.AddSealedSubclassesOf(typeof(IConfigMigrator), a);
public static IKernel AddMusic(this IKernel kernel)
public static IContainer AddMusic(this IContainer kernel)
{
kernel.Bind<IMusicService, IPlaceholderProvider>()
.To<MusicService>()
.InSingletonScope();
kernel.RegisterMany<MusicService>(Reuse.Singleton);
kernel.Bind<ITrackResolveProvider>().To<TrackResolveProvider>().InSingletonScope();
kernel.Bind<IYoutubeResolver>().To<YtdlYoutubeResolver>().InSingletonScope();
kernel.Bind<ILocalTrackResolver>().To<LocalTrackResolver>().InSingletonScope();
kernel.Bind<IRadioResolver>().To<RadioResolver>().InSingletonScope();
kernel.Bind<ITrackCacher>().To<TrackCacher>().InSingletonScope();
// kernel.Bind<YtLoader>().ToSelf().InSingletonScope();
kernel.AddSingleton<ITrackResolveProvider, TrackResolveProvider>();
kernel.AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>();
kernel.AddSingleton<ILocalTrackResolver, LocalTrackResolver>();
kernel.AddSingleton<IRadioResolver, RadioResolver>();
kernel.AddSingleton<ITrackCacher, TrackCacher>();
return kernel;
}
public static IKernel AddSealedSubclassesOf(this IKernel kernel, Type baseType, Assembly a)
public static IContainer AddSealedSubclassesOf(this IContainer cont, Type baseType, Assembly a)
{
kernel.Bind(x =>
var classes = a.GetExportedTypes()
.Where(x => x.IsClass && !x.IsAbstract && x.IsPublic)
.Where(x => x.IsNested && baseType.IsAssignableFrom(x));
foreach (var c in classes)
{
var classes = x.From(a)
.SelectAllClasses()
.Where(c => c.IsPublic && c.IsNested && baseType.IsAssignableFrom(baseType));
cont.RegisterMany([c], Reuse.Singleton);
// var inters = c.GetInterfaces();
classes.BindToSelfWithInterfaces().Configure(x => x.InSingletonScope());
});
// cont.RegisterMany(inters, c);
}
return kernel;
return cont;
}
public static IKernel AddCache(this IKernel kernel, IBotCredentials creds)
public static IContainer AddCache(this IContainer cont, IBotCredentials creds)
{
if (creds.BotCache == BotCacheImplemenation.Redis)
{
var conf = ConfigurationOptions.Parse(creds.RedisOptions);
kernel.Bind<ConnectionMultiplexer>().ToConstant(ConnectionMultiplexer.Connect(conf)).InSingletonScope();
kernel.Bind<IBotCache>().To<RedisBotCache>().InSingletonScope();
kernel.Bind<IPubSub>().To<RedisPubSub>().InSingletonScope();
cont.AddSingleton<ConnectionMultiplexer>(ConnectionMultiplexer.Connect(conf));
cont.AddSingleton<IBotCache, RedisBotCache>();
cont.AddSingleton<IPubSub, RedisPubSub>();
}
else
{
kernel.Bind<IBotCache>().To<MemoryBotCache>().InSingletonScope();
kernel.Bind<IPubSub>().To<EventPubSub>().InSingletonScope();
cont.AddSingleton<IBotCache, MemoryBotCache>();
cont.AddSingleton<IPubSub, EventPubSub>();
}
return kernel
return cont
.AddBotStringsServices(creds.BotCache);
}
public static IKernel AddHttpClients(this IKernel kernel)
public static IContainer AddHttpClients(this IContainer kernel)
{
IServiceCollection svcs = new ServiceCollection();
svcs.AddHttpClient();
@@ -117,8 +127,8 @@ public static class ServiceCollectionExtensions
});
var prov = svcs.BuildServiceProvider();
kernel.Bind<IHttpClientFactory>().ToMethod(_ => prov.GetRequiredService<IHttpClientFactory>());
kernel.Bind<HttpClient>().ToMethod(_ => prov.GetRequiredService<HttpClient>());
kernel.RegisterDelegate<IHttpClientFactory>(_ => prov.GetRequiredService<IHttpClientFactory>());
kernel.RegisterDelegate<HttpClient>(_ => prov.GetRequiredService<HttpClient>());
return kernel;
}
@@ -126,26 +136,39 @@ public static class ServiceCollectionExtensions
public static IConfigureSyntax BindToSelfWithInterfaces(this IJoinExcludeIncludeBindSyntax matcher)
=> matcher.BindSelection((type, types) => types.Append(type));
public static IKernel AddLifetimeServices(this IKernel kernel, Assembly a)
public static IContainer AddLifetimeServices(this IContainer kernel, Assembly a)
{
kernel.Bind(scan =>
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<DIIgnoreAttribute>()))
{
scan.From(a)
.SelectAllClasses()
.Where(c => (c.IsAssignableTo(typeof(INService))
|| c.IsAssignableTo(typeof(IExecOnMessage))
|| c.IsAssignableTo(typeof(IInputTransformer))
|| c.IsAssignableTo(typeof(IExecPreCommand))
|| c.IsAssignableTo(typeof(IExecPostCommand))
|| c.IsAssignableTo(typeof(IExecNoCommand)))
&& !c.HasAttribute<DIIgnoreAttribute>()
#if GLOBAL_NADEKO
&& !c.HasAttribute<NoPublicBotAttribute>()
#endif
)
.BindToSelfWithInterfaces()
.Configure(c => c.InSingletonScope());
});
kernel.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;
}