< Summary - Jellyfin

Information
Class: MediaBrowser.Common.Plugins.BasePlugin<T>
Assembly: MediaBrowser.Common
File(s): /srv/git/jellyfin/MediaBrowser.Common/Plugins/BasePluginOfT.cs
Line coverage
73%
Covered lines: 38
Uncovered lines: 14
Coverable lines: 52
Total lines: 202
Line coverage: 73%
Branch coverage
56%
Covered branches: 9
Total branches: 16
Branch coverage: 56.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/srv/git/jellyfin/MediaBrowser.Common/Plugins/BasePluginOfT.cs

#LineLine coverage
 1#nullable disable
 2#pragma warning disable SA1649 // File name should match first type name
 3
 4using System;
 5using System.IO;
 6using System.Runtime.InteropServices;
 7using MediaBrowser.Common.Configuration;
 8using MediaBrowser.Model.Plugins;
 9using MediaBrowser.Model.Serialization;
 10
 11namespace MediaBrowser.Common.Plugins
 12{
 13    /// <summary>
 14    /// Provides a common base class for all plugins.
 15    /// </summary>
 16    /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
 17    public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
 18        where TConfigurationType : BasePluginConfiguration
 19    {
 20        /// <summary>
 21        /// The configuration sync lock.
 22        /// </summary>
 15423        private readonly object _configurationSyncLock = new object();
 24
 25        /// <summary>
 26        /// The configuration save lock.
 27        /// </summary>
 15428        private readonly object _configurationSaveLock = new object();
 29
 30        /// <summary>
 31        /// The configuration.
 32        /// </summary>
 33        private TConfigurationType _configuration;
 34
 35        /// <summary>
 36        /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
 37        /// </summary>
 38        /// <param name="applicationPaths">The application paths.</param>
 39        /// <param name="xmlSerializer">The XML serializer.</param>
 15440        protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
 41        {
 15442            ApplicationPaths = applicationPaths;
 15443            XmlSerializer = xmlSerializer;
 44
 45            var assembly = GetType().Assembly;
 15446            var assemblyName = assembly.GetName();
 15447            var assemblyFilePath = assembly.Location;
 48
 15449            var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFil
 15450            if (Version is not null && !Directory.Exists(dataFolderPath))
 51            {
 52                // Try again with the version number appended to the folder name.
 053                dataFolderPath += "_" + Version;
 54            }
 55
 15456            SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
 57
 15458            var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
 15459            if (idAttributes.Length > 0)
 60            {
 061                var attribute = (GuidAttribute)idAttributes[0];
 062                var assemblyId = new Guid(attribute.Value);
 63
 064                SetId(assemblyId);
 65            }
 15466        }
 67
 68        /// <summary>
 69        /// Gets the application paths.
 70        /// </summary>
 71        /// <value>The application paths.</value>
 72        protected IApplicationPaths ApplicationPaths { get; private set; }
 73
 74        /// <summary>
 75        /// Gets the XML serializer.
 76        /// </summary>
 77        /// <value>The XML serializer.</value>
 78        protected IXmlSerializer XmlSerializer { get; private set; }
 79
 80        /// <summary>
 81        /// Gets the type of configuration this plugin uses.
 82        /// </summary>
 83        /// <value>The type of the configuration.</value>
 084        public Type ConfigurationType => typeof(TConfigurationType);
 85
 86        /// <summary>
 87        /// Gets or sets the event handler that is triggered when this configuration changes.
 88        /// </summary>
 89        public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
 90
 91        /// <summary>
 92        /// Gets the name the assembly file.
 93        /// </summary>
 94        /// <value>The name of the assembly file.</value>
 295        protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
 96
 97        /// <summary>
 98        /// Gets or sets the plugin configuration.
 99        /// </summary>
 100        /// <value>The configuration.</value>
 101        public TConfigurationType Configuration
 102        {
 103            get
 104            {
 105                // Lazy load
 110106                if (_configuration is null)
 107                {
 44108                    lock (_configurationSyncLock)
 109                    {
 44110                        _configuration ??= LoadConfiguration();
 44111                    }
 112                }
 113
 110114                return _configuration;
 115            }
 116
 0117            protected set => _configuration = value;
 118        }
 119
 120        /// <summary>
 121        /// Gets the name of the configuration file. Subclasses should override.
 122        /// </summary>
 123        /// <value>The name of the configuration file.</value>
 2124        public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
 125
 126        /// <summary>
 127        /// Gets the full path to the configuration file.
 128        /// </summary>
 129        /// <value>The configuration file path.</value>
 132130        public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFile
 131
 132        /// <summary>
 133        /// Gets the plugin configuration.
 134        /// </summary>
 135        /// <value>The configuration.</value>
 0136        BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
 137
 138        /// <summary>
 139        /// Saves the current configuration to the file system.
 140        /// </summary>
 141        /// <param name="config">Configuration to save.</param>
 142        public virtual void SaveConfiguration(TConfigurationType config)
 143        {
 44144            lock (_configurationSaveLock)
 145            {
 44146                var folder = Path.GetDirectoryName(ConfigurationFilePath);
 44147                if (!Directory.Exists(folder))
 148                {
 22149                    Directory.CreateDirectory(folder);
 150                }
 151
 44152                XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
 44153            }
 44154        }
 155
 156        /// <summary>
 157        /// Saves the current configuration to the file system.
 158        /// </summary>
 159        public virtual void SaveConfiguration()
 160        {
 0161            SaveConfiguration(Configuration);
 0162        }
 163
 164        /// <inheritdoc />
 165        public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
 166        {
 0167            ArgumentNullException.ThrowIfNull(configuration);
 168
 0169            Configuration = (TConfigurationType)configuration;
 170
 0171            SaveConfiguration(Configuration);
 172
 0173            ConfigurationChanged?.Invoke(this, configuration);
 0174        }
 175
 176        /// <inheritdoc />
 177        public override PluginInfo GetPluginInfo()
 178        {
 7179            var info = base.GetPluginInfo();
 180
 7181            info.ConfigurationFileName = ConfigurationFileName;
 182
 7183            return info;
 184        }
 185
 186        private TConfigurationType LoadConfiguration()
 187        {
 44188            var path = ConfigurationFilePath;
 189
 190            try
 191            {
 44192                return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
 193            }
 44194            catch
 195            {
 44196                var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
 44197                SaveConfiguration(config);
 44198                return config;
 199            }
 44200        }
 201    }
 202}