diff options
| -rw-r--r-- | Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs | 25 | ||||
| -rw-r--r-- | Emby.Server.Implementations/ApplicationHost.cs | 65 | ||||
| -rw-r--r-- | MediaBrowser.Common/Configuration/IConfigurationManager.cs | 7 | ||||
| -rw-r--r-- | MediaBrowser.Common/Plugins/BasePlugin.cs | 20 | ||||
| -rw-r--r-- | MediaBrowser.Common/Plugins/IPlugin.cs | 12 | ||||
| -rw-r--r-- | MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs | 17 |
6 files changed, 91 insertions, 55 deletions
diff --git a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs index 4ab0a2a3f..a5a361d24 100644 --- a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs +++ b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs @@ -134,6 +134,31 @@ namespace Emby.Server.Implementations.AppBase } /// <summary> + /// Manually pre-loads a factory so that it is available pre system initialisation. + /// </summary> + /// <typeparam name="T">Class to register.</typeparam> + public virtual void RegisterConfiguration<T>() + where T : IConfigurationFactory + { + IConfigurationFactory factory = (IConfigurationFactory)Activator.CreateInstance(typeof(T)); + + if (_configurationFactories == null) + { + _configurationFactories = new[] { factory }; + } + else + { + var list = _configurationFactories.ToList(); + list.Add(factory); + _configurationFactories = list.ToArray(); + } + + _configurationStores = _configurationFactories + .SelectMany(i => i.GetConfigurations()) + .ToArray(); + } + + /// <summary> /// Adds parts. /// </summary> /// <param name="factories">The configuration factories.</param> diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 31ca73829..69f8521d0 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -128,7 +128,6 @@ namespace Emby.Server.Implementations private IMediaEncoder _mediaEncoder; private ISessionManager _sessionManager; private IHttpClientFactory _httpClientFactory; - private string[] _urlPrefixes; /// <summary> @@ -499,24 +498,11 @@ namespace Emby.Server.Implementations HttpsPort = ServerConfiguration.DefaultHttpsPort; } - if (Plugins != null) - { - var pluginBuilder = new StringBuilder(); - - foreach (var plugin in Plugins) - { - pluginBuilder.Append(plugin.Name) - .Append(' ') - .Append(plugin.Version) - .AppendLine(); - } - - Logger.LogInformation("Plugins: {Plugins}", pluginBuilder.ToString()); - } - DiscoverTypes(); RegisterServices(); + + RegisterPluginServices(); } /// <summary> @@ -781,10 +767,24 @@ namespace Emby.Server.Implementations ConfigurationManager.AddParts(GetExports<IConfigurationFactory>()); _plugins = GetExports<IPlugin>() - .Select(LoadPlugin) .Where(i => i != null) .ToArray(); + if (Plugins != null) + { + var pluginBuilder = new StringBuilder(); + + foreach (var plugin in Plugins) + { + pluginBuilder.Append(plugin.Name) + .Append(' ') + .Append(plugin.Version) + .AppendLine(); + } + + Logger.LogInformation("Plugins: {Plugins}", pluginBuilder.ToString()); + } + _urlPrefixes = GetUrlPrefixes().ToArray(); Resolve<ILibraryManager>().AddParts( @@ -814,21 +814,6 @@ namespace Emby.Server.Implementations Resolve<IIsoManager>().AddParts(GetExports<IIsoMounter>()); } - private IPlugin LoadPlugin(IPlugin plugin) - { - try - { - plugin.RegisterServices(ServiceCollection); - } - catch (Exception ex) - { - Logger.LogError(ex, "Error loading plugin {PluginName}", plugin.GetType().FullName); - return null; - } - - return plugin; - } - /// <summary> /// Discovers the types. /// </summary> @@ -839,6 +824,22 @@ namespace Emby.Server.Implementations _allConcreteTypes = GetTypes(GetComposablePartAssemblies()).ToArray(); } + private void RegisterPluginServices() + { + foreach (var pluginServiceRegistrar in GetExportTypes<IPluginServiceRegistrator>()) + { + try + { + var instance = (IPluginServiceRegistrator)Activator.CreateInstance(pluginServiceRegistrar); + instance.RegisterServices(ServiceCollection); + } + catch (Exception ex) + { + Logger.LogError(ex, "Error registering plugin services from {Assembly}.", pluginServiceRegistrar.Assembly); + } + } + } + private IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies) { foreach (var ass in assemblies) diff --git a/MediaBrowser.Common/Configuration/IConfigurationManager.cs b/MediaBrowser.Common/Configuration/IConfigurationManager.cs index fe726090d..fc63d9350 100644 --- a/MediaBrowser.Common/Configuration/IConfigurationManager.cs +++ b/MediaBrowser.Common/Configuration/IConfigurationManager.cs @@ -47,6 +47,13 @@ namespace MediaBrowser.Common.Configuration void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration); /// <summary> + /// Manually pre-loads a factory so that it is available pre system initialisation. + /// </summary> + /// <typeparam name="T">Class to register.</typeparam> + void RegisterConfiguration<T>() + where T : IConfigurationFactory; + + /// <summary> /// Gets the configuration. /// </summary> /// <param name="key">The key.</param> diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs index 8545fd5dc..11445d79d 100644 --- a/MediaBrowser.Common/Plugins/BasePlugin.cs +++ b/MediaBrowser.Common/Plugins/BasePlugin.cs @@ -56,7 +56,8 @@ namespace MediaBrowser.Common.Plugins /// Gets a value indicating whether the plugin can be uninstalled. /// </summary> public bool CanUninstall => !Path.GetDirectoryName(AssemblyFilePath) - .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.InvariantCulture); + .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.InvariantCulture) && + !typeof(IPluginServiceRegistrator).IsAssignableFrom(this.GetType()); /// <summary> /// Gets the plugin info. @@ -84,16 +85,6 @@ namespace MediaBrowser.Common.Plugins } /// <inheritdoc /> - public virtual void RegisterServices(IServiceCollection serviceCollection) - { - } - - /// <inheritdoc /> - public virtual void UnregisterServices(IServiceCollection serviceCollection) - { - } - - /// <inheritdoc /> public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion) { AssemblyFilePath = assemblyFilePath; @@ -186,6 +177,11 @@ namespace MediaBrowser.Common.Plugins public Type ConfigurationType => typeof(TConfigurationType); /// <summary> + /// Gets or sets the event handler that is triggered when this configuration changes. + /// </summary> + public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; } + + /// <summary> /// Gets the name the assembly file. /// </summary> /// <value>The name of the assembly file.</value> @@ -280,6 +276,8 @@ namespace MediaBrowser.Common.Plugins Configuration = (TConfigurationType)configuration; SaveConfiguration(); + + ConfigurationChanged.Invoke(this, configuration); } /// <inheritdoc /> diff --git a/MediaBrowser.Common/Plugins/IPlugin.cs b/MediaBrowser.Common/Plugins/IPlugin.cs index 1844eb124..d583a5887 100644 --- a/MediaBrowser.Common/Plugins/IPlugin.cs +++ b/MediaBrowser.Common/Plugins/IPlugin.cs @@ -62,18 +62,6 @@ namespace MediaBrowser.Common.Plugins /// Called when just before the plugin is uninstalled from the server. /// </summary> void OnUninstalling(); - - /// <summary> - /// Registers the plugin's services to the service collection. - /// </summary> - /// <param name="serviceCollection">The service collection.</param> - void RegisterServices(IServiceCollection serviceCollection); - - /// <summary> - /// Unregisters the plugin's services from the service collection. - /// </summary> - /// <param name="serviceCollection">The service collection.</param> - void UnregisterServices(IServiceCollection serviceCollection); } public interface IHasPluginConfiguration diff --git a/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs b/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs new file mode 100644 index 000000000..cb6c27f86 --- /dev/null +++ b/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs @@ -0,0 +1,17 @@ +namespace MediaBrowser.Common.Plugins +{ + using Microsoft.Extensions.DependencyInjection; + + /// <summary> + /// Defines the <see cref="IPluginServiceRegistrator" />. + /// </summary> + public interface IPluginServiceRegistrator + { + /// <summary> + /// Registers the plugin's services with the service collection. + /// This object is created prior to the plugin creation, so access to other classes is limited. + /// </summary> + /// <param name="serviceCollection">The service collection.</param> + void RegisterServices(IServiceCollection serviceCollection); + } +} |
