BasePluginOfT.cs 6.7 KB

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