BasePluginOfT.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (this is IPluginAssembly assemblyPlugin)
  39. {
  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 (!Directory.Exists(dataFolderPath) && Version != null)
  45. {
  46. // Try again with the version number appended to the folder name.
  47. dataFolderPath = dataFolderPath + "_" + Version.ToString();
  48. }
  49. assemblyPlugin.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. assemblyPlugin.SetId(assemblyId);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Gets the application paths.
  61. /// </summary>
  62. /// <value>The application paths.</value>
  63. protected IApplicationPaths ApplicationPaths { get; private set; }
  64. /// <summary>
  65. /// Gets the XML serializer.
  66. /// </summary>
  67. /// <value>The XML serializer.</value>
  68. protected IXmlSerializer XmlSerializer { get; private set; }
  69. /// <summary>
  70. /// Gets the type of configuration this plugin uses.
  71. /// </summary>
  72. /// <value>The type of the configuration.</value>
  73. public Type ConfigurationType => typeof(TConfigurationType);
  74. /// <summary>
  75. /// Gets or sets the event handler that is triggered when this configuration changes.
  76. /// </summary>
  77. public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
  78. /// <summary>
  79. /// Gets the name the assembly file.
  80. /// </summary>
  81. /// <value>The name of the assembly file.</value>
  82. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  83. /// <summary>
  84. /// Gets or sets the plugin configuration.
  85. /// </summary>
  86. /// <value>The configuration.</value>
  87. public TConfigurationType Configuration
  88. {
  89. get
  90. {
  91. // Lazy load
  92. if (_configuration == null)
  93. {
  94. lock (_configurationSyncLock)
  95. {
  96. if (_configuration == null)
  97. {
  98. _configuration = LoadConfiguration();
  99. }
  100. }
  101. }
  102. return _configuration;
  103. }
  104. protected set => _configuration = value;
  105. }
  106. /// <summary>
  107. /// Gets the name of the configuration file. Subclasses should override.
  108. /// </summary>
  109. /// <value>The name of the configuration file.</value>
  110. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  111. /// <summary>
  112. /// Gets the full path to the configuration file.
  113. /// </summary>
  114. /// <value>The configuration file path.</value>
  115. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  116. /// <summary>
  117. /// Gets the plugin configuration.
  118. /// </summary>
  119. /// <value>The configuration.</value>
  120. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  121. /// <summary>
  122. /// Saves the current configuration to the file system.
  123. /// </summary>
  124. /// <param name="config">Configuration to save.</param>
  125. public virtual void SaveConfiguration(TConfigurationType config)
  126. {
  127. lock (_configurationSaveLock)
  128. {
  129. var folder = Path.GetDirectoryName(ConfigurationFilePath);
  130. if (!Directory.Exists(folder))
  131. {
  132. Directory.CreateDirectory(folder);
  133. }
  134. XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
  135. }
  136. }
  137. /// <summary>
  138. /// Saves the current configuration to the file system.
  139. /// </summary>
  140. public virtual void SaveConfiguration()
  141. {
  142. SaveConfiguration(Configuration);
  143. }
  144. /// <inheritdoc />
  145. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  146. {
  147. if (configuration == null)
  148. {
  149. throw new ArgumentNullException(nameof(configuration));
  150. }
  151. Configuration = (TConfigurationType)configuration;
  152. SaveConfiguration(Configuration);
  153. ConfigurationChanged?.Invoke(this, configuration);
  154. }
  155. /// <inheritdoc />
  156. public override PluginInfo GetPluginInfo()
  157. {
  158. var info = base.GetPluginInfo();
  159. info.ConfigurationFileName = ConfigurationFileName;
  160. return info;
  161. }
  162. private TConfigurationType LoadConfiguration()
  163. {
  164. var path = ConfigurationFilePath;
  165. try
  166. {
  167. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  168. }
  169. catch
  170. {
  171. var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  172. SaveConfiguration(config);
  173. return config;
  174. }
  175. }
  176. }
  177. }