BasePlugin.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #pragma warning disable SA1402
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Model.Plugins;
  8. using MediaBrowser.Model.Serialization;
  9. using Microsoft.Extensions.DependencyInjection;
  10. namespace MediaBrowser.Common.Plugins
  11. {
  12. /// <summary>
  13. /// Provides a common base class for all plugins.
  14. /// </summary>
  15. public abstract class BasePlugin : IPlugin, IPluginAssembly
  16. {
  17. /// <summary>
  18. /// Gets the name of the plugin.
  19. /// </summary>
  20. /// <value>The name.</value>
  21. public abstract string Name { get; }
  22. /// <summary>
  23. /// Gets the description.
  24. /// </summary>
  25. /// <value>The description.</value>
  26. public virtual string Description => string.Empty;
  27. /// <summary>
  28. /// Gets the unique id.
  29. /// </summary>
  30. /// <value>The unique id.</value>
  31. public virtual Guid Id { get; private set; }
  32. /// <summary>
  33. /// Gets the plugin version.
  34. /// </summary>
  35. /// <value>The version.</value>
  36. public Version Version { get; private set; }
  37. /// <summary>
  38. /// Gets the path to the assembly file.
  39. /// </summary>
  40. /// <value>The assembly file path.</value>
  41. public string AssemblyFilePath { get; private set; }
  42. /// <summary>
  43. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed.
  44. /// </summary>
  45. /// <value>The data folder path.</value>
  46. public string DataFolderPath { get; private set; }
  47. /// <summary>
  48. /// Gets a value indicating whether the plugin can be uninstalled.
  49. /// </summary>
  50. public bool CanUninstall => !Path.GetDirectoryName(AssemblyFilePath)
  51. .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.InvariantCulture);
  52. /// <summary>
  53. /// Gets the plugin info.
  54. /// </summary>
  55. /// <returns>PluginInfo.</returns>
  56. public virtual PluginInfo GetPluginInfo()
  57. {
  58. var info = new PluginInfo
  59. {
  60. Name = Name,
  61. Version = Version.ToString(),
  62. Description = Description,
  63. Id = Id.ToString(),
  64. CanUninstall = CanUninstall
  65. };
  66. return info;
  67. }
  68. /// <summary>
  69. /// Called just before the plugin is uninstalled from the server.
  70. /// </summary>
  71. public virtual void OnUninstalling()
  72. {
  73. }
  74. /// <inheritdoc />
  75. public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
  76. {
  77. AssemblyFilePath = assemblyFilePath;
  78. DataFolderPath = dataFolderPath;
  79. Version = assemblyVersion;
  80. }
  81. /// <inheritdoc />
  82. public void SetId(Guid assemblyId)
  83. {
  84. Id = assemblyId;
  85. }
  86. }
  87. /// <summary>
  88. /// Provides a common base class for all plugins.
  89. /// </summary>
  90. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  91. public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
  92. where TConfigurationType : BasePluginConfiguration
  93. {
  94. /// <summary>
  95. /// The configuration sync lock.
  96. /// </summary>
  97. private readonly object _configurationSyncLock = new object();
  98. /// <summary>
  99. /// The configuration save lock.
  100. /// </summary>
  101. private readonly object _configurationSaveLock = new object();
  102. private Action<string> _directoryCreateFn;
  103. /// <summary>
  104. /// The configuration.
  105. /// </summary>
  106. private TConfigurationType _configuration;
  107. /// <summary>
  108. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  109. /// </summary>
  110. /// <param name="applicationPaths">The application paths.</param>
  111. /// <param name="xmlSerializer">The XML serializer.</param>
  112. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  113. {
  114. ApplicationPaths = applicationPaths;
  115. XmlSerializer = xmlSerializer;
  116. if (this is IPluginAssembly assemblyPlugin)
  117. {
  118. var assembly = GetType().Assembly;
  119. var assemblyName = assembly.GetName();
  120. var assemblyFilePath = assembly.Location;
  121. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
  122. assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
  123. var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
  124. if (idAttributes.Length > 0)
  125. {
  126. var attribute = (GuidAttribute)idAttributes[0];
  127. var assemblyId = new Guid(attribute.Value);
  128. assemblyPlugin.SetId(assemblyId);
  129. }
  130. }
  131. if (this is IHasPluginConfiguration hasPluginConfiguration)
  132. {
  133. hasPluginConfiguration.SetStartupInfo(s => Directory.CreateDirectory(s));
  134. }
  135. }
  136. /// <summary>
  137. /// Gets the application paths.
  138. /// </summary>
  139. /// <value>The application paths.</value>
  140. protected IApplicationPaths ApplicationPaths { get; private set; }
  141. /// <summary>
  142. /// Gets the XML serializer.
  143. /// </summary>
  144. /// <value>The XML serializer.</value>
  145. protected IXmlSerializer XmlSerializer { get; private set; }
  146. /// <summary>
  147. /// Gets the type of configuration this plugin uses.
  148. /// </summary>
  149. /// <value>The type of the configuration.</value>
  150. public Type ConfigurationType => typeof(TConfigurationType);
  151. /// <summary>
  152. /// Gets or sets the event handler that is triggered when this configuration changes.
  153. /// </summary>
  154. public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
  155. /// <summary>
  156. /// Gets the name the assembly file.
  157. /// </summary>
  158. /// <value>The name of the assembly file.</value>
  159. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  160. /// <summary>
  161. /// Gets or sets the plugin configuration.
  162. /// </summary>
  163. /// <value>The configuration.</value>
  164. public TConfigurationType Configuration
  165. {
  166. get
  167. {
  168. // Lazy load
  169. if (_configuration == null)
  170. {
  171. lock (_configurationSyncLock)
  172. {
  173. if (_configuration == null)
  174. {
  175. _configuration = LoadConfiguration();
  176. }
  177. }
  178. }
  179. return _configuration;
  180. }
  181. protected set => _configuration = value;
  182. }
  183. /// <summary>
  184. /// Gets the name of the configuration file. Subclasses should override.
  185. /// </summary>
  186. /// <value>The name of the configuration file.</value>
  187. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  188. /// <summary>
  189. /// Gets the full path to the configuration file.
  190. /// </summary>
  191. /// <value>The configuration file path.</value>
  192. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  193. /// <summary>
  194. /// Gets the plugin configuration.
  195. /// </summary>
  196. /// <value>The configuration.</value>
  197. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  198. /// <inheritdoc />
  199. public void SetStartupInfo(Action<string> directoryCreateFn)
  200. {
  201. // hack alert, until the .net core transition is complete
  202. _directoryCreateFn = directoryCreateFn;
  203. }
  204. private TConfigurationType LoadConfiguration()
  205. {
  206. var path = ConfigurationFilePath;
  207. try
  208. {
  209. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  210. }
  211. catch
  212. {
  213. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  214. }
  215. }
  216. /// <summary>
  217. /// Saves the current configuration to the file system.
  218. /// </summary>
  219. public virtual void SaveConfiguration()
  220. {
  221. lock (_configurationSaveLock)
  222. {
  223. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  224. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  225. }
  226. }
  227. /// <inheritdoc />
  228. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  229. {
  230. if (configuration == null)
  231. {
  232. throw new ArgumentNullException(nameof(configuration));
  233. }
  234. Configuration = (TConfigurationType)configuration;
  235. SaveConfiguration();
  236. ConfigurationChanged.Invoke(this, configuration);
  237. }
  238. /// <inheritdoc />
  239. public override PluginInfo GetPluginInfo()
  240. {
  241. var info = base.GetPluginInfo();
  242. info.ConfigurationFileName = ConfigurationFileName;
  243. return info;
  244. }
  245. }
  246. }