diff --git a/src/YumeChan.Core/CommandHandler.cs b/src/YumeChan.Core/CommandHandler.cs index 62ed1ae..11a9a7c 100644 --- a/src/YumeChan.Core/CommandHandler.cs +++ b/src/YumeChan.Core/CommandHandler.cs @@ -112,8 +112,8 @@ public async Task UninstallCommandsAsync() public async Task RegisterCommandsAsync() { - _logger.LogInformation("Using PluginBase v{Version}.", typeof(IPlugin).Assembly.GetCustomAttribute()?.InformationalVersion); - _logger.LogInformation("Current Plugins directory: {PluginsDirectory}", _pluginsLoader.PluginsLoadDirectory); + _logger.LogInformation("Using PluginBase v{version}.", typeof(IPlugin).Assembly.GetCustomAttribute()?.InformationalVersion); + _logger.LogInformation("Current Plugins directory: {pluginsDirectory}", _pluginsLoader.PluginsLoadDirectory); await _pluginsFetcher.FetchPluginsAsync(); _pluginsLoader.ScanDirectoryForPluginFiles(); @@ -124,7 +124,9 @@ public async Task RegisterCommandsAsync() _container.Populate(handler.ConfigureServices(new ServiceCollection())); } - _pluginsLoader.LoadPluginManifests(); + Dictionary pluginManifestTypes = _pluginsLoader.GetPluginManifestTypes(); + _container.RegisterMany(pluginManifestTypes.Values, serviceTypeCondition: type => type.IsAssignableTo(typeof(IPlugin))); + _pluginsLoader.LoadPluginManifests(pluginManifestTypes); // Add YumeCore internal commands _pluginsLoader.ImportPlugin(new Modules.InternalPlugin()); diff --git a/src/YumeChan.Core/Services/Plugins/PluginsLoader.cs b/src/YumeChan.Core/Services/Plugins/PluginsLoader.cs index 2aa06f7..6dbd9e8 100644 --- a/src/YumeChan.Core/Services/Plugins/PluginsLoader.cs +++ b/src/YumeChan.Core/Services/Plugins/PluginsLoader.cs @@ -127,35 +127,33 @@ internal void ImportPlugin(IPlugin plugin, Assembly? assembly = null) // Also try add the assembly to the list of plugin assemblies. _pluginAssemblies.TryAdd(plugin.AssemblyName, assembly ?? plugin.GetType().Assembly); } - - internal IEnumerable LoadPluginManifests() + + internal Dictionary GetPluginManifestTypes() => new( + from a in _loadAssemblies + from t in a.ExportedTypes + where t.ImplementsInterface(typeof(IPlugin)) + select new KeyValuePair(a, t) + ); + + internal IEnumerable LoadPluginManifests(IEnumerable>? pluginTypes = null) { PluginManifestsInternal.Clear(); + + pluginTypes ??= GetPluginManifestTypes(); - foreach (Assembly a in _loadAssemblies) + foreach (var (a, t) in pluginTypes) { try { - // Try to load the plugin manifests from the assemblies, log error in console if unsuccessful. - foreach (Type t in a.ExportedTypes.Where(static x => x.ImplementsInterface(typeof(IPlugin)))) - { - try - { - // Moment of truth... - IPlugin plugin = InstantiateManifest(t)!; - - // It works! Import it. - ImportPlugin(plugin, a); - } - catch (Exception e) - { - YumeCore.Instance.Logger.LogError(e,"Failed to instantiate plugin {pluginName}.", t.Name); - } - } + // Moment of truth... + IPlugin plugin = InstantiateManifest(t)!; + + // It works! Import it. + ImportPlugin(plugin, a); } catch (Exception e) { - YumeCore.Instance.Logger.LogError(e,"Failed to load plugin manifests from assembly {assemblyFullName}.", a.FullName); + YumeCore.Instance.Logger.LogError(e,"Failed to instantiate plugin {pluginName}.", t.Name); } }