mirror of
				https://github.com/jellyfin/jellyfin.git
				synced 2025-11-03 19:17:24 -05:00 
			
		
		
		
	Merge pull request #4253 from BaronGreenback/fordiscussion
DI in plugins
This commit is contained in:
		
						commit
						331c7f8481
					
				@ -133,6 +133,33 @@ 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 = Activator.CreateInstance<T>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (_configurationFactories == null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                _configurationFactories = new[] { factory };
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                var oldLen = _configurationFactories.Length;
 | 
				
			||||||
 | 
					                var arr = new IConfigurationFactory[oldLen + 1];
 | 
				
			||||||
 | 
					                _configurationFactories.CopyTo(arr, 0);
 | 
				
			||||||
 | 
					                arr[oldLen] = factory;
 | 
				
			||||||
 | 
					                _configurationFactories = arr;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            _configurationStores = _configurationFactories
 | 
				
			||||||
 | 
					                .SelectMany(i => i.GetConfigurations())
 | 
				
			||||||
 | 
					                .ToArray();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// Adds parts.
 | 
					        /// Adds parts.
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
 | 
				
			|||||||
@ -126,7 +126,6 @@ namespace Emby.Server.Implementations
 | 
				
			|||||||
        private IMediaEncoder _mediaEncoder;
 | 
					        private IMediaEncoder _mediaEncoder;
 | 
				
			||||||
        private ISessionManager _sessionManager;
 | 
					        private ISessionManager _sessionManager;
 | 
				
			||||||
        private IHttpClientFactory _httpClientFactory;
 | 
					        private IHttpClientFactory _httpClientFactory;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        private string[] _urlPrefixes;
 | 
					        private string[] _urlPrefixes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
@ -497,24 +496,11 @@ namespace Emby.Server.Implementations
 | 
				
			|||||||
                HttpsPort = ServerConfiguration.DefaultHttpsPort;
 | 
					                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();
 | 
					            DiscoverTypes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            RegisterServices();
 | 
					            RegisterServices();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            RegisterPluginServices();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
@ -779,10 +765,24 @@ namespace Emby.Server.Implementations
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            ConfigurationManager.AddParts(GetExports<IConfigurationFactory>());
 | 
					            ConfigurationManager.AddParts(GetExports<IConfigurationFactory>());
 | 
				
			||||||
            _plugins = GetExports<IPlugin>()
 | 
					            _plugins = GetExports<IPlugin>()
 | 
				
			||||||
                        .Select(LoadPlugin)
 | 
					 | 
				
			||||||
                        .Where(i => i != null)
 | 
					                        .Where(i => i != null)
 | 
				
			||||||
                        .ToArray();
 | 
					                        .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();
 | 
					            _urlPrefixes = GetUrlPrefixes().ToArray();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Resolve<ILibraryManager>().AddParts(
 | 
					            Resolve<ILibraryManager>().AddParts(
 | 
				
			||||||
@ -812,21 +812,6 @@ namespace Emby.Server.Implementations
 | 
				
			|||||||
            Resolve<IIsoManager>().AddParts(GetExports<IIsoMounter>());
 | 
					            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>
 | 
					        /// <summary>
 | 
				
			||||||
        /// Discovers the types.
 | 
					        /// Discovers the types.
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
@ -837,6 +822,22 @@ namespace Emby.Server.Implementations
 | 
				
			|||||||
            _allConcreteTypes = GetTypes(GetComposablePartAssemblies()).ToArray();
 | 
					            _allConcreteTypes = GetTypes(GetComposablePartAssemblies()).ToArray();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void RegisterPluginServices()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            foreach (var pluginServiceRegistrator in GetExportTypes<IPluginServiceRegistrator>())
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                try
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var instance = (IPluginServiceRegistrator)Activator.CreateInstance(pluginServiceRegistrator);
 | 
				
			||||||
 | 
					                    instance.RegisterServices(ServiceCollection);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                catch (Exception ex)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    Logger.LogError(ex, "Error registering plugin services from {Assembly}.", pluginServiceRegistrator.Assembly);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies)
 | 
					        private IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            foreach (var ass in assemblies)
 | 
					            foreach (var ass in assemblies)
 | 
				
			||||||
 | 
				
			|||||||
@ -46,6 +46,13 @@ namespace MediaBrowser.Common.Configuration
 | 
				
			|||||||
        /// <param name="newConfiguration">The new configuration.</param>
 | 
					        /// <param name="newConfiguration">The new configuration.</param>
 | 
				
			||||||
        void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration);
 | 
					        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>
 | 
					        /// <summary>
 | 
				
			||||||
        /// Gets the configuration.
 | 
					        /// Gets the configuration.
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
 | 
				
			|||||||
@ -83,16 +83,6 @@ namespace MediaBrowser.Common.Plugins
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <inheritdoc />
 | 
					 | 
				
			||||||
        public virtual void RegisterServices(IServiceCollection serviceCollection)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /// <inheritdoc />
 | 
					 | 
				
			||||||
        public virtual void UnregisterServices(IServiceCollection serviceCollection)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /// <inheritdoc />
 | 
					        /// <inheritdoc />
 | 
				
			||||||
        public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
 | 
					        public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@ -185,6 +175,11 @@ namespace MediaBrowser.Common.Plugins
 | 
				
			|||||||
        /// <value>The type of the configuration.</value>
 | 
					        /// <value>The type of the configuration.</value>
 | 
				
			||||||
        public Type ConfigurationType => typeof(TConfigurationType);
 | 
					        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>
 | 
					        /// <summary>
 | 
				
			||||||
        /// Gets the name the assembly file.
 | 
					        /// Gets the name the assembly file.
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
@ -280,6 +275,8 @@ namespace MediaBrowser.Common.Plugins
 | 
				
			|||||||
            Configuration = (TConfigurationType)configuration;
 | 
					            Configuration = (TConfigurationType)configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            SaveConfiguration();
 | 
					            SaveConfiguration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            ConfigurationChanged.Invoke(this, configuration);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <inheritdoc />
 | 
					        /// <inheritdoc />
 | 
				
			||||||
 | 
				
			|||||||
@ -62,18 +62,6 @@ namespace MediaBrowser.Common.Plugins
 | 
				
			|||||||
        /// Called when just before the plugin is uninstalled from the server.
 | 
					        /// Called when just before the plugin is uninstalled from the server.
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
        void OnUninstalling();
 | 
					        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
 | 
					    public interface IHasPluginConfiguration
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					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.
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <remarks>
 | 
				
			||||||
 | 
					        /// This interface is only used for service registration and requires a parameterless constructor.
 | 
				
			||||||
 | 
					        /// </remarks>
 | 
				
			||||||
 | 
					        /// <param name="serviceCollection">The service collection.</param>
 | 
				
			||||||
 | 
					        void RegisterServices(IServiceCollection serviceCollection);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user