diff --git a/Iceshrimp.Backend/Core/Helpers/AssemblyHelpers.cs b/Iceshrimp.Backend/Core/Helpers/AssemblyHelpers.cs index a9774754..135ed12e 100644 --- a/Iceshrimp.Backend/Core/Helpers/AssemblyHelpers.cs +++ b/Iceshrimp.Backend/Core/Helpers/AssemblyHelpers.cs @@ -19,18 +19,4 @@ public static class AssemblyHelpers return assembly.GetManifestResourceStream(resourceName) ?? throw new Exception($"Failed to get embedded resource {resourceName} from assembly."); } - - public static IEnumerable GetTypesWithAttribute(Type attribute, Assembly? assembly = null) - { - assembly ??= Assembly.GetExecutingAssembly(); - return assembly.GetTypes().Where(type => Attribute.IsDefined(type, attribute)); - } - - public static IEnumerable GetImplementationsOfInterface(Type @interface, Assembly? assembly = null) - { - assembly ??= Assembly.GetExecutingAssembly(); - return assembly.GetTypes() - .Where(type => type is { IsAbstract: false, IsClass: true } && - type.GetInterfaces().Contains(@interface)); - } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Helpers/PluginLoader.cs b/Iceshrimp.Backend/Core/Helpers/PluginLoader.cs index 477236c0..c3b7a05a 100644 --- a/Iceshrimp.Backend/Core/Helpers/PluginLoader.cs +++ b/Iceshrimp.Backend/Core/Helpers/PluginLoader.cs @@ -1,11 +1,10 @@ using System.Reflection; +using Iceshrimp.AssemblyUtils; using Iceshrimp.Backend.Core.Extensions; -using Weikio.PluginFramework.Abstractions; -using Weikio.PluginFramework.Catalogs; namespace Iceshrimp.Backend.Core.Helpers; -using LoadedPlugin = (Plugin descriptor, IPlugin instance); +using LoadedPlugin = (Assembly assembly, IPlugin instance); public abstract class PluginLoader { @@ -20,24 +19,18 @@ public abstract class PluginLoader private static List _loaded = []; private static IEnumerable Plugins => _loaded.Select(p => p.instance); - public static IEnumerable Assemblies => _loaded.Select(p => p.descriptor.Assembly); + public static IEnumerable Assemblies => _loaded.Select(p => p.assembly); public static async Task LoadPlugins() { if (!Directory.Exists(DllPath)) return; var dlls = Directory.EnumerateFiles(DllPath, "*.dll").ToList(); - var catalogs = dlls - .Select(p => new AssemblyPluginCatalog(p, type => type.Implements())) - .Cast() - .ToArray(); - - var combined = new CompositePluginCatalog(catalogs); - await combined.Initialize(); - _loaded = combined.GetPlugins() - .Select(p => (descriptor: p, instance: Activator.CreateInstance(p) as IPlugin)) - .Where(p => p.instance is not null) - .Cast() - .ToList(); + _loaded = dlls.Select(Path.GetFullPath) + .Select(AssemblyLoader.LoadAssemblyFromPath) + .Select(p => (assembly: p, impls: AssemblyLoader.GetImplementationsOfInterface(p))) + .SelectMany(p => p.impls.Select(impl => (p.assembly, Activator.CreateInstance(impl) as IPlugin))) + .Cast() + .ToList(); await Plugins.Select(i => i.Initialize()).AwaitAllNoConcurrencyAsync(); } @@ -62,7 +55,7 @@ public abstract class PluginLoader } var plugins = _loaded.Select(plugin => $"{plugin.instance.Name} v{plugin.instance.Version} " + - $"({Path.GetFileName(plugin.descriptor.Assembly.Location)})"); + $"({Path.GetFileName(plugin.assembly.Location)})"); logger.LogInformation("Loaded {count} plugins from {dllPath}: \n* {files}", _loaded.Count, DllPath, string.Join("\n* ", plugins)); } diff --git a/Iceshrimp.Backend/Core/Services/CronService.cs b/Iceshrimp.Backend/Core/Services/CronService.cs index ad975ce8..9d445bbe 100644 --- a/Iceshrimp.Backend/Core/Services/CronService.cs +++ b/Iceshrimp.Backend/Core/Services/CronService.cs @@ -1,4 +1,5 @@ using System.Reflection; +using Iceshrimp.AssemblyUtils; using Iceshrimp.Backend.Core.Helpers; namespace Iceshrimp.Backend.Core.Services; @@ -9,7 +10,7 @@ public class CronService(IServiceScopeFactory serviceScopeFactory) : BackgroundS { var tasks = PluginLoader .Assemblies.Prepend(Assembly.GetExecutingAssembly()) - .SelectMany(assembly => AssemblyHelpers.GetImplementationsOfInterface(typeof(ICronTask), assembly)) + .SelectMany(AssemblyLoader.GetImplementationsOfInterface) .Select(p => Activator.CreateInstance(p) as ICronTask) .Where(p => p != null) .Cast(); diff --git a/Iceshrimp.Backend/Iceshrimp.Backend.csproj b/Iceshrimp.Backend/Iceshrimp.Backend.csproj index fde8122d..36519c4f 100644 --- a/Iceshrimp.Backend/Iceshrimp.Backend.csproj +++ b/Iceshrimp.Backend/Iceshrimp.Backend.csproj @@ -43,7 +43,7 @@ - +