BasePlugin.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 virtual void RegisterServices(IServiceCollection serviceCollection)
  76. {
  77. }
  78. /// <inheritdoc />
  79. public virtual void UnregisterServices(IServiceCollection serviceCollection)
  80. {
  81. }
  82. /// <inheritdoc />
  83. public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
  84. {
  85. AssemblyFilePath = assemblyFilePath;
  86. DataFolderPath = dataFolderPath;
  87. Version = assemblyVersion;
  88. }
  89. /// <inheritdoc />
  90. public void SetId(Guid assemblyId)
  91. {
  92. Id = assemblyId;
  93. }
  94. }
  95. /// <summary>
  96. /// Provides a common base class for all plugins.
  97. /// </summary>
  98. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  99. public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
  100. where TConfigurationType : BasePluginConfiguration
  101. {
  102. /// <summary>
  103. /// The configuration sync lock.
  104. /// </summary>
  105. private readonly object _configurationSyncLock = new object();
  106. /// <summary>
  107. /// The configuration save lock.
  108. /// </summary>
  109. private readonly object _configurationSaveLock = new object();
  110. private Action<string> _directoryCreateFn;
  111. /// <summary>
  112. /// The configuration.
  113. /// </summary>
  114. private TConfigurationType _configuration;
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  117. /// </summary>
  118. /// <param name="applicationPaths">The application paths.</param>
  119. /// <param name="xmlSerializer">The XML serializer.</param>
  120. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  121. {
  122. ApplicationPaths = applicationPaths;
  123. XmlSerializer = xmlSerializer;
  124. if (this is IPluginAssembly assemblyPlugin)
  125. {
  126. var assembly = GetType().Assembly;
  127. var assemblyName = assembly.GetName();
  128. var assemblyFilePath = assembly.Location;
  129. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
  130. assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
  131. var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
  132. if (idAttributes.Length > 0)
  133. {
  134. var attribute = (GuidAttribute)idAttributes[0];
  135. var assemblyId = new Guid(attribute.Value);
  136. assemblyPlugin.SetId(assemblyId);
  137. }
  138. }
  139. if (this is IHasPluginConfiguration hasPluginConfiguration)
  140. {
  141. hasPluginConfiguration.SetStartupInfo(s => Directory.CreateDirectory(s));
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the application paths.
  146. /// </summary>
  147. /// <value>The application paths.</value>
  148. protected IApplicationPaths ApplicationPaths { get; private set; }
  149. /// <summary>
  150. /// Gets the XML serializer.
  151. /// </summary>
  152. /// <value>The XML serializer.</value>
  153. protected IXmlSerializer XmlSerializer { get; private set; }
  154. /// <summary>
  155. /// Gets the type of configuration this plugin uses.
  156. /// </summary>
  157. /// <value>The type of the configuration.</value>
  158. public Type ConfigurationType => typeof(TConfigurationType);
  159. /// <summary>
  160. /// Gets the name the assembly file.
  161. /// </summary>
  162. /// <value>The name of the assembly file.</value>
  163. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  164. /// <summary>
  165. /// Gets or sets the plugin configuration.
  166. /// </summary>
  167. /// <value>The configuration.</value>
  168. public TConfigurationType Configuration
  169. {
  170. get
  171. {
  172. // Lazy load
  173. if (_configuration == null)
  174. {
  175. lock (_configurationSyncLock)
  176. {
  177. if (_configuration == null)
  178. {
  179. _configuration = LoadConfiguration();
  180. }
  181. }
  182. }
  183. return _configuration;
  184. }
  185. protected set => _configuration = value;
  186. }
  187. /// <summary>
  188. /// Gets the name of the configuration file. Subclasses should override.
  189. /// </summary>
  190. /// <value>The name of the configuration file.</value>
  191. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  192. /// <summary>
  193. /// Gets the full path to the configuration file.
  194. /// </summary>
  195. /// <value>The configuration file path.</value>
  196. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  197. /// <summary>
  198. /// Gets the plugin configuration.
  199. /// </summary>
  200. /// <value>The configuration.</value>
  201. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  202. /// <inheritdoc />
  203. public void SetStartupInfo(Action<string> directoryCreateFn)
  204. {
  205. // hack alert, until the .net core transition is complete
  206. _directoryCreateFn = directoryCreateFn;
  207. }
  208. private TConfigurationType LoadConfiguration()
  209. {
  210. var path = ConfigurationFilePath;
  211. try
  212. {
  213. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  214. }
  215. catch
  216. {
  217. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  218. }
  219. }
  220. /// <summary>
  221. /// Saves the current configuration to the file system.
  222. /// </summary>
  223. public virtual void SaveConfiguration()
  224. {
  225. lock (_configurationSaveLock)
  226. {
  227. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  228. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  229. }
  230. }
  231. /// <inheritdoc />
  232. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  233. {
  234. if (configuration == null)
  235. {
  236. throw new ArgumentNullException(nameof(configuration));
  237. }
  238. Configuration = (TConfigurationType)configuration;
  239. SaveConfiguration();
  240. }
  241. /// <inheritdoc />
  242. public override PluginInfo GetPluginInfo()
  243. {
  244. var info = base.GetPluginInfo();
  245. info.ConfigurationFileName = ConfigurationFileName;
  246. return info;
  247. }
  248. }
  249. }