BasePluginOfT.cs 6.9 KB

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