2
0

BasePlugin.cs 10 KB

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