BasePluginOfT.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. private Action<string> _directoryCreateFn;
  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. if (this is IPluginAssembly assemblyPlugin)
  40. {
  41. var assembly = GetType().Assembly;
  42. var assemblyName = assembly.GetName();
  43. var assemblyFilePath = assembly.Location;
  44. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
  45. if (!Directory.Exists(dataFolderPath) && Version != null)
  46. {
  47. // Try again with the version number appended to the folder name.
  48. dataFolderPath = dataFolderPath + "_" + Version.ToString();
  49. }
  50. assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
  51. var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
  52. if (idAttributes.Length > 0)
  53. {
  54. var attribute = (GuidAttribute)idAttributes[0];
  55. var assemblyId = new Guid(attribute.Value);
  56. assemblyPlugin.SetId(assemblyId);
  57. }
  58. }
  59. if (this is IHasPluginConfiguration hasPluginConfiguration)
  60. {
  61. hasPluginConfiguration.SetStartupInfo(s => Directory.CreateDirectory(s));
  62. }
  63. }
  64. /// <summary>
  65. /// Gets the application paths.
  66. /// </summary>
  67. /// <value>The application paths.</value>
  68. protected IApplicationPaths ApplicationPaths { get; private set; }
  69. /// <summary>
  70. /// Gets the XML serializer.
  71. /// </summary>
  72. /// <value>The XML serializer.</value>
  73. protected IXmlSerializer XmlSerializer { get; private set; }
  74. /// <summary>
  75. /// Gets the type of configuration this plugin uses.
  76. /// </summary>
  77. /// <value>The type of the configuration.</value>
  78. public Type ConfigurationType => typeof(TConfigurationType);
  79. /// <summary>
  80. /// Gets or sets the event handler that is triggered when this configuration changes.
  81. /// </summary>
  82. public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
  83. /// <summary>
  84. /// Gets the name the assembly file.
  85. /// </summary>
  86. /// <value>The name of the assembly file.</value>
  87. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  88. /// <summary>
  89. /// Gets or sets the plugin configuration.
  90. /// </summary>
  91. /// <value>The configuration.</value>
  92. public TConfigurationType Configuration
  93. {
  94. get
  95. {
  96. // Lazy load
  97. if (_configuration == null)
  98. {
  99. lock (_configurationSyncLock)
  100. {
  101. if (_configuration == null)
  102. {
  103. _configuration = LoadConfiguration();
  104. }
  105. }
  106. }
  107. return _configuration;
  108. }
  109. protected set => _configuration = value;
  110. }
  111. /// <summary>
  112. /// Gets the name of the configuration file. Subclasses should override.
  113. /// </summary>
  114. /// <value>The name of the configuration file.</value>
  115. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  116. /// <summary>
  117. /// Gets the full path to the configuration file.
  118. /// </summary>
  119. /// <value>The configuration file path.</value>
  120. public string ConfigurationFilePath
  121. {
  122. get
  123. {
  124. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(AssemblyFilePath));
  125. if (!Directory.Exists(dataFolderPath) && Version != null)
  126. {
  127. // Try again with the version number appended to the folder name.
  128. return dataFolderPath + "_" + Version.ToString();
  129. }
  130. return dataFolderPath;
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the plugin configuration.
  135. /// </summary>
  136. /// <value>The configuration.</value>
  137. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  138. /// <inheritdoc />
  139. public void SetStartupInfo(Action<string> directoryCreateFn)
  140. {
  141. // hack alert, until the .net core transition is complete
  142. _directoryCreateFn = directoryCreateFn;
  143. }
  144. /// <summary>
  145. /// Saves the current configuration to the file system.
  146. /// </summary>
  147. /// <param name="config">Configuration to save.</param>
  148. public virtual void SaveConfiguration(TConfigurationType config)
  149. {
  150. lock (_configurationSaveLock)
  151. {
  152. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  153. XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
  154. }
  155. }
  156. /// <summary>
  157. /// Saves the current configuration to the file system.
  158. /// </summary>
  159. public virtual void SaveConfiguration()
  160. {
  161. SaveConfiguration(Configuration);
  162. }
  163. /// <inheritdoc />
  164. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  165. {
  166. if (configuration == null)
  167. {
  168. throw new ArgumentNullException(nameof(configuration));
  169. }
  170. Configuration = (TConfigurationType)configuration;
  171. SaveConfiguration(Configuration);
  172. ConfigurationChanged?.Invoke(this, configuration);
  173. }
  174. /// <inheritdoc />
  175. public override PluginInfo GetPluginInfo()
  176. {
  177. var info = base.GetPluginInfo();
  178. info.ConfigurationFileName = ConfigurationFileName;
  179. return info;
  180. }
  181. private TConfigurationType LoadConfiguration()
  182. {
  183. var path = ConfigurationFilePath;
  184. try
  185. {
  186. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  187. }
  188. catch
  189. {
  190. var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  191. SaveConfiguration(config);
  192. return config;
  193. }
  194. }
  195. }
  196. }