BasePluginOfT.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #nullable disable
  2. #pragma warning disable SA1649 // File name should match first type name
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Model.Plugins;
  8. using MediaBrowser.Model.Serialization;
  9. namespace MediaBrowser.Common.Plugins
  10. {
  11. /// <summary>
  12. /// Provides a common base class for all plugins.
  13. /// </summary>
  14. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  15. public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
  16. where TConfigurationType : BasePluginConfiguration
  17. {
  18. /// <summary>
  19. /// The configuration sync lock.
  20. /// </summary>
  21. private readonly object _configurationSyncLock = new object();
  22. /// <summary>
  23. /// The configuration save lock.
  24. /// </summary>
  25. private readonly object _configurationSaveLock = new object();
  26. /// <summary>
  27. /// The configuration.
  28. /// </summary>
  29. private TConfigurationType _configuration;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  32. /// </summary>
  33. /// <param name="applicationPaths">The application paths.</param>
  34. /// <param name="xmlSerializer">The XML serializer.</param>
  35. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  36. {
  37. ApplicationPaths = applicationPaths;
  38. XmlSerializer = xmlSerializer;
  39. var assembly = GetType().Assembly;
  40. var assemblyName = assembly.GetName();
  41. var assemblyFilePath = assembly.Location;
  42. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
  43. if (Version is not null && !Directory.Exists(dataFolderPath))
  44. {
  45. // Try again with the version number appended to the folder name.
  46. dataFolderPath += "_" + Version;
  47. }
  48. SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
  49. var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
  50. if (idAttributes.Length > 0)
  51. {
  52. var attribute = (GuidAttribute)idAttributes[0];
  53. var assemblyId = new Guid(attribute.Value);
  54. SetId(assemblyId);
  55. }
  56. }
  57. /// <summary>
  58. /// Gets the application paths.
  59. /// </summary>
  60. /// <value>The application paths.</value>
  61. protected IApplicationPaths ApplicationPaths { get; private set; }
  62. /// <summary>
  63. /// Gets the XML serializer.
  64. /// </summary>
  65. /// <value>The XML serializer.</value>
  66. protected IXmlSerializer XmlSerializer { get; private set; }
  67. /// <summary>
  68. /// Gets the type of configuration this plugin uses.
  69. /// </summary>
  70. /// <value>The type of the configuration.</value>
  71. public Type ConfigurationType => typeof(TConfigurationType);
  72. /// <summary>
  73. /// Gets or sets the event handler that is triggered when this configuration changes.
  74. /// </summary>
  75. public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
  76. /// <summary>
  77. /// Gets the name the assembly file.
  78. /// </summary>
  79. /// <value>The name of the assembly file.</value>
  80. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  81. /// <summary>
  82. /// Gets or sets the plugin configuration.
  83. /// </summary>
  84. /// <value>The configuration.</value>
  85. public TConfigurationType Configuration
  86. {
  87. get
  88. {
  89. // Lazy load
  90. if (_configuration is null)
  91. {
  92. lock (_configurationSyncLock)
  93. {
  94. _configuration ??= LoadConfiguration();
  95. }
  96. }
  97. return _configuration;
  98. }
  99. protected set => _configuration = value;
  100. }
  101. /// <summary>
  102. /// Gets the name of the configuration file. Subclasses should override.
  103. /// </summary>
  104. /// <value>The name of the configuration file.</value>
  105. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  106. /// <summary>
  107. /// Gets the full path to the configuration file.
  108. /// </summary>
  109. /// <value>The configuration file path.</value>
  110. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  111. /// <summary>
  112. /// Gets the plugin configuration.
  113. /// </summary>
  114. /// <value>The configuration.</value>
  115. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  116. /// <summary>
  117. /// Saves the current configuration to the file system.
  118. /// </summary>
  119. /// <param name="config">Configuration to save.</param>
  120. public virtual void SaveConfiguration(TConfigurationType config)
  121. {
  122. lock (_configurationSaveLock)
  123. {
  124. var folder = Path.GetDirectoryName(ConfigurationFilePath);
  125. if (!Directory.Exists(folder))
  126. {
  127. Directory.CreateDirectory(folder);
  128. }
  129. XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
  130. }
  131. }
  132. /// <summary>
  133. /// Saves the current configuration to the file system.
  134. /// </summary>
  135. public virtual void SaveConfiguration()
  136. {
  137. SaveConfiguration(Configuration);
  138. }
  139. /// <inheritdoc />
  140. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  141. {
  142. ArgumentNullException.ThrowIfNull(configuration);
  143. Configuration = (TConfigurationType)configuration;
  144. SaveConfiguration(Configuration);
  145. ConfigurationChanged?.Invoke(this, configuration);
  146. }
  147. /// <inheritdoc />
  148. public override PluginInfo GetPluginInfo()
  149. {
  150. var info = base.GetPluginInfo();
  151. info.ConfigurationFileName = ConfigurationFileName;
  152. return info;
  153. }
  154. private TConfigurationType LoadConfiguration()
  155. {
  156. var path = ConfigurationFilePath;
  157. try
  158. {
  159. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  160. }
  161. catch
  162. {
  163. var config = Activator.CreateInstance<TConfigurationType>();
  164. SaveConfiguration(config);
  165. return config;
  166. }
  167. }
  168. }
  169. }