Enable nullable for more files (#9310)

pull/9315/head
Bond-009 2 years ago committed by GitHub
parent 36b7157589
commit 92f6e19a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -33,15 +31,10 @@ namespace Emby.Server.Implementations.AppBase
private ConfigurationStore[] _configurationStores = Array.Empty<ConfigurationStore>(); private ConfigurationStore[] _configurationStores = Array.Empty<ConfigurationStore>();
private IConfigurationFactory[] _configurationFactories = Array.Empty<IConfigurationFactory>(); private IConfigurationFactory[] _configurationFactories = Array.Empty<IConfigurationFactory>();
/// <summary>
/// The _configuration loaded.
/// </summary>
private bool _configurationLoaded;
/// <summary> /// <summary>
/// The _configuration. /// The _configuration.
/// </summary> /// </summary>
private BaseApplicationConfiguration _configuration; private BaseApplicationConfiguration? _configuration;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class. /// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class.
@ -63,17 +56,17 @@ namespace Emby.Server.Implementations.AppBase
/// <summary> /// <summary>
/// Occurs when [configuration updated]. /// Occurs when [configuration updated].
/// </summary> /// </summary>
public event EventHandler<EventArgs> ConfigurationUpdated; public event EventHandler<EventArgs>? ConfigurationUpdated;
/// <summary> /// <summary>
/// Occurs when [configuration updating]. /// Occurs when [configuration updating].
/// </summary> /// </summary>
public event EventHandler<ConfigurationUpdateEventArgs> NamedConfigurationUpdating; public event EventHandler<ConfigurationUpdateEventArgs>? NamedConfigurationUpdating;
/// <summary> /// <summary>
/// Occurs when [named configuration updated]. /// Occurs when [named configuration updated].
/// </summary> /// </summary>
public event EventHandler<ConfigurationUpdateEventArgs> NamedConfigurationUpdated; public event EventHandler<ConfigurationUpdateEventArgs>? NamedConfigurationUpdated;
/// <summary> /// <summary>
/// Gets the type of the configuration. /// Gets the type of the configuration.
@ -107,31 +100,25 @@ namespace Emby.Server.Implementations.AppBase
{ {
get get
{ {
if (_configurationLoaded) if (_configuration is not null)
{ {
return _configuration; return _configuration;
} }
lock (_configurationSyncLock) lock (_configurationSyncLock)
{ {
if (_configurationLoaded) if (_configuration is not null)
{ {
return _configuration; return _configuration;
} }
_configuration = (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer); return _configuration = (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer);
_configurationLoaded = true;
return _configuration;
} }
} }
protected set protected set
{ {
_configuration = value; _configuration = value;
_configurationLoaded = value is not null;
} }
} }
@ -183,7 +170,7 @@ namespace Emby.Server.Implementations.AppBase
Logger.LogInformation("Saving system configuration"); Logger.LogInformation("Saving system configuration");
var path = CommonApplicationPaths.SystemConfigurationFilePath; var path = CommonApplicationPaths.SystemConfigurationFilePath;
Directory.CreateDirectory(Path.GetDirectoryName(path)); Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be a root directory."));
lock (_configurationSyncLock) lock (_configurationSyncLock)
{ {
@ -323,25 +310,20 @@ namespace Emby.Server.Implementations.AppBase
private object LoadConfiguration(string path, Type configurationType) private object LoadConfiguration(string path, Type configurationType)
{ {
if (!File.Exists(path))
{
return Activator.CreateInstance(configurationType);
}
try try
{
if (File.Exists(path))
{ {
return XmlSerializer.DeserializeFromFile(configurationType, path); return XmlSerializer.DeserializeFromFile(configurationType, path);
} }
catch (IOException)
{
return Activator.CreateInstance(configurationType);
} }
catch (Exception ex) catch (Exception ex) when (ex is not IOException)
{ {
Logger.LogError(ex, "Error loading configuration file: {Path}", path); Logger.LogError(ex, "Error loading configuration file: {Path}", path);
return Activator.CreateInstance(configurationType);
} }
return Activator.CreateInstance(configurationType)
?? throw new InvalidOperationException("Configuration type can't be Nullable<T>.");
} }
/// <inheritdoc /> /// <inheritdoc />
@ -367,7 +349,7 @@ namespace Emby.Server.Implementations.AppBase
_configurations.AddOrUpdate(key, configuration, (_, _) => configuration); _configurations.AddOrUpdate(key, configuration, (_, _) => configuration);
var path = GetConfigurationFile(key); var path = GetConfigurationFile(key);
Directory.CreateDirectory(Path.GetDirectoryName(path)); Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be a root directory."));
lock (_configurationSyncLock) lock (_configurationSyncLock)
{ {

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@ -36,7 +34,7 @@ namespace Emby.Server.Implementations.Configuration
/// <summary> /// <summary>
/// Configuration updating event. /// Configuration updating event.
/// </summary> /// </summary>
public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating; public event EventHandler<GenericEventArgs<ServerConfiguration>>? ConfigurationUpdating;
/// <summary> /// <summary>
/// Gets the type of the configuration. /// Gets the type of the configuration.

@ -1,5 +1,3 @@
#nullable disable
#pragma warning disable CS1591 #pragma warning disable CS1591
using System.Collections.Generic; using System.Collections.Generic;

@ -1,5 +1,3 @@
#nullable disable
#pragma warning disable CS1591 #pragma warning disable CS1591
using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Configuration;

@ -1,5 +1,3 @@
#nullable disable
#pragma warning disable CS1591 #pragma warning disable CS1591
using System.Collections.Generic; using System.Collections.Generic;

Loading…
Cancel
Save