diff --git a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs index fa4b3080c9..8503a358e4 100644 --- a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs +++ b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs @@ -134,6 +134,35 @@ namespace Emby.Server.Implementations.AppBase } } + /// + /// Manually pre-loads a factory so that it is available pre system initialisation. + /// + /// Class to register. + public virtual void RegisterConfiguration() + { + if (!typeof(IConfigurationFactory).IsAssignableFrom(typeof(T))) + { + throw new ArgumentException("Parameter does not implement IConfigurationFactory"); + } + + IConfigurationFactory factory = (IConfigurationFactory)Activator.CreateInstance(typeof(T)); + + if (_configurationFactories == null) + { + _configurationFactories = new IConfigurationFactory[] { factory }; + } + else + { + var list = _configurationFactories.ToList(); + list.Add(factory); + _configurationFactories = list.ToArray(); + } + + _configurationStores = _configurationFactories + .SelectMany(i => i.GetConfigurations()) + .ToArray(); + } + /// /// Adds parts. /// @@ -269,7 +298,7 @@ namespace Emby.Server.Implementations.AppBase } /// - public object GetConfiguration(string key, Type objectType = null) + public object GetConfiguration(string key) { return _configurations.GetOrAdd(key, k => { @@ -278,12 +307,12 @@ namespace Emby.Server.Implementations.AppBase var configurationInfo = _configurationStores .FirstOrDefault(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); - if (configurationInfo == null && objectType == null) + if (configurationInfo == null) { throw new ResourceNotFoundException("Configuration with key " + key + " not found."); } - var configurationType = configurationInfo?.ConfigurationType ?? objectType; + var configurationType = configurationInfo.ConfigurationType; lock (_configurationSyncLock) { diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index ecd26c0d84..768f7a9c8d 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -276,9 +276,11 @@ namespace Emby.Server.Implementations _fileSystemManager = fileSystem; ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager); + MigrateNetworkConfiguration(); + // Have to pre-register the NetworkConfigurationFactory. + ConfigurationManager.RegisterConfiguration(); NetManager = new NetworkManager((IServerConfigurationManager)ConfigurationManager, LoggerFactory.CreateLogger()); - NetManager.UpdateSettings(GetNetworkConfiguration()); Logger = LoggerFactory.CreateLogger(); @@ -304,7 +306,7 @@ namespace Emby.Server.Implementations ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString; } - private NetworkConfiguration GetNetworkConfiguration() + private void MigrateNetworkConfiguration() { string path = Path.Combine(ConfigurationManager.CommonApplicationPaths.ConfigurationDirectoryPath, "network.xml"); if (!File.Exists(path)) @@ -312,11 +314,8 @@ namespace Emby.Server.Implementations var networkSettings = new NetworkConfiguration(); ClassMigrationHelper.CopyProperties(ServerConfigurationManager.Configuration, networkSettings); _xmlSerializer.SerializeToFile(networkSettings, path); - - return networkSettings; + Logger.LogDebug("Successfully migrated network settings."); } - - return (NetworkConfiguration)ConfigurationManager.GetConfiguration("network", typeof(NetworkConfiguration)); } public string ExpandVirtualPath(string path) diff --git a/Jellyfin.Api/Controllers/ConfigurationController.cs b/Jellyfin.Api/Controllers/ConfigurationController.cs index 09d72e8b10..53f94cf375 100644 --- a/Jellyfin.Api/Controllers/ConfigurationController.cs +++ b/Jellyfin.Api/Controllers/ConfigurationController.cs @@ -51,10 +51,6 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetConfiguration() { - // TODO: Temp workaround until the web can be changed. - var net = _configurationManager.GetNetworkConfiguration(); - ClassMigrationHelper.CopyProperties(net, _configurationManager.Configuration); - return _configurationManager.Configuration; } @@ -70,12 +66,6 @@ namespace Jellyfin.Api.Controllers public ActionResult UpdateConfiguration([FromBody, Required] ServerConfiguration configuration) { _configurationManager.ReplaceConfiguration(configuration); - - // TODO: Temp workaround until the web can be changed. - var network = _configurationManager.GetNetworkConfiguration(); - ClassMigrationHelper.CopyProperties(configuration, network); - _configurationManager.SaveConfiguration("Network", network); - return NoContent(); } diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 26614c85e0..6d6b7ebc4e 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -110,10 +110,12 @@ namespace Jellyfin.Networking.Manager _publishedServerUrls = new Dictionary(); _eventFireLock = new object(); + UpdateSettings(_configurationManager.GetNetworkConfiguration()); + NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged; - _configurationManager.ConfigurationUpdated += ConfigurationUpdated; + _configurationManager.NamedConfigurationUpdated += ConfigurationUpdated; } #pragma warning restore CS8618 // Non-nullable field is uninitialized. @@ -600,7 +602,7 @@ namespace Jellyfin.Networking.Manager { if (disposing) { - _configurationManager.ConfigurationUpdated -= ConfigurationUpdated; + _configurationManager.NamedConfigurationUpdated -= ConfigurationUpdated; NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged; NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged; } @@ -609,9 +611,12 @@ namespace Jellyfin.Networking.Manager } } - private void ConfigurationUpdated(object? sender, EventArgs args) + private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt) { - UpdateSettings(_configurationManager.GetNetworkConfiguration()); + if (evt.Key.Equals("network", StringComparison.Ordinal)) + { + UpdateSettings(evt.NewConfiguration); + } } /// diff --git a/MediaBrowser.Common/Configuration/IConfigurationManager.cs b/MediaBrowser.Common/Configuration/IConfigurationManager.cs index 7bcd4d8ed3..17520f8a37 100644 --- a/MediaBrowser.Common/Configuration/IConfigurationManager.cs +++ b/MediaBrowser.Common/Configuration/IConfigurationManager.cs @@ -47,12 +47,12 @@ namespace MediaBrowser.Common.Configuration void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration); /// - /// Gets the configuration. + /// Manually pre-loads a factory so that it is available pre system initialisation. /// - /// The key. - /// Optional parameter containing the key object to create, if it hasn't been registered. - /// System.Object. - object GetConfiguration(string key, Type objectType = null); + /// Class to register. + void RegisterConfiguration(); + + object GetConfiguration(string key); /// /// Gets the type of the configuration.