BasePlugin.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Model.Plugins;
  4. using MediaBrowser.Model.Serialization;
  5. using System;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. namespace MediaBrowser.Common.Plugins
  11. {
  12. /// <summary>
  13. /// Provides a common base class for all plugins
  14. /// </summary>
  15. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  16. public abstract class BasePlugin<TConfigurationType> : IDisposable, IPlugin
  17. where TConfigurationType : BasePluginConfiguration
  18. {
  19. /// <summary>
  20. /// Gets the kernel.
  21. /// </summary>
  22. /// <value>The kernel.</value>
  23. protected IKernel Kernel { get; private set; }
  24. /// <summary>
  25. /// Gets or sets the plugin's current context
  26. /// </summary>
  27. /// <value>The context.</value>
  28. protected KernelContext Context { get { return Kernel.KernelContext; } }
  29. /// <summary>
  30. /// Gets the name of the plugin
  31. /// </summary>
  32. /// <value>The name.</value>
  33. public abstract string Name { get; }
  34. /// <summary>
  35. /// Gets the description.
  36. /// </summary>
  37. /// <value>The description.</value>
  38. public virtual string Description
  39. {
  40. get { return string.Empty; }
  41. }
  42. /// <summary>
  43. /// Gets a value indicating whether this instance is a core plugin.
  44. /// </summary>
  45. /// <value><c>true</c> if this instance is a core plugin; otherwise, <c>false</c>.</value>
  46. public virtual bool IsCorePlugin
  47. {
  48. get
  49. {
  50. return false;
  51. }
  52. }
  53. /// <summary>
  54. /// Gets the type of configuration this plugin uses
  55. /// </summary>
  56. /// <value>The type of the configuration.</value>
  57. public Type ConfigurationType
  58. {
  59. get { return typeof(TConfigurationType); }
  60. }
  61. /// <summary>
  62. /// The _assembly name
  63. /// </summary>
  64. private AssemblyName _assemblyName;
  65. /// <summary>
  66. /// Gets the name of the assembly.
  67. /// </summary>
  68. /// <value>The name of the assembly.</value>
  69. protected AssemblyName AssemblyName
  70. {
  71. get
  72. {
  73. return _assemblyName ?? (_assemblyName = GetType().Assembly.GetName());
  74. }
  75. }
  76. /// <summary>
  77. /// The _unique id
  78. /// </summary>
  79. private Guid? _uniqueId;
  80. /// <summary>
  81. /// Gets the unique id.
  82. /// </summary>
  83. /// <value>The unique id.</value>
  84. public Guid Id
  85. {
  86. get
  87. {
  88. if (!_uniqueId.HasValue)
  89. {
  90. _uniqueId = Marshal.GetTypeLibGuidForAssembly(GetType().Assembly);
  91. }
  92. return _uniqueId.Value;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the plugin version
  97. /// </summary>
  98. /// <value>The version.</value>
  99. public Version Version
  100. {
  101. get
  102. {
  103. return AssemblyName.Version;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets the name the assembly file
  108. /// </summary>
  109. /// <value>The name of the assembly file.</value>
  110. public string AssemblyFileName
  111. {
  112. get
  113. {
  114. return AssemblyName.Name + ".dll";
  115. }
  116. }
  117. /// <summary>
  118. /// Gets the last date modified of the configuration
  119. /// </summary>
  120. /// <value>The configuration date last modified.</value>
  121. public DateTime ConfigurationDateLastModified
  122. {
  123. get
  124. {
  125. // Ensure it's been lazy loaded
  126. var config = Configuration;
  127. return File.GetLastWriteTimeUtc(ConfigurationFilePath);
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the last date modified of the plugin
  132. /// </summary>
  133. /// <value>The assembly date last modified.</value>
  134. public DateTime AssemblyDateLastModified
  135. {
  136. get
  137. {
  138. return File.GetLastWriteTimeUtc(AssemblyFilePath);
  139. }
  140. }
  141. /// <summary>
  142. /// Gets the path to the assembly file
  143. /// </summary>
  144. /// <value>The assembly file path.</value>
  145. public string AssemblyFilePath
  146. {
  147. get
  148. {
  149. return Path.Combine(Kernel.ApplicationPaths.PluginsPath, AssemblyFileName);
  150. }
  151. }
  152. /// <summary>
  153. /// The _configuration sync lock
  154. /// </summary>
  155. private object _configurationSyncLock = new object();
  156. /// <summary>
  157. /// The _configuration initialized
  158. /// </summary>
  159. private bool _configurationInitialized;
  160. /// <summary>
  161. /// The _configuration
  162. /// </summary>
  163. private TConfigurationType _configuration;
  164. /// <summary>
  165. /// Gets the plugin's configuration
  166. /// </summary>
  167. /// <value>The configuration.</value>
  168. public TConfigurationType Configuration
  169. {
  170. get
  171. {
  172. // Lazy load
  173. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => Kernel.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath) as TConfigurationType);
  174. return _configuration;
  175. }
  176. protected set
  177. {
  178. _configuration = value;
  179. if (value == null)
  180. {
  181. _configurationInitialized = false;
  182. }
  183. }
  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
  190. {
  191. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  192. }
  193. /// <summary>
  194. /// Gets the full path to the configuration file
  195. /// </summary>
  196. /// <value>The configuration file path.</value>
  197. public string ConfigurationFilePath
  198. {
  199. get
  200. {
  201. return Path.Combine(Kernel.ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  202. }
  203. }
  204. /// <summary>
  205. /// The _data folder path
  206. /// </summary>
  207. private string _dataFolderPath;
  208. /// <summary>
  209. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  210. /// </summary>
  211. /// <value>The data folder path.</value>
  212. public string DataFolderPath
  213. {
  214. get
  215. {
  216. if (_dataFolderPath == null)
  217. {
  218. // Give the folder name the same name as the config file name
  219. // We can always make this configurable if/when needed
  220. _dataFolderPath = Path.Combine(Kernel.ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  221. if (!Directory.Exists(_dataFolderPath))
  222. {
  223. Directory.CreateDirectory(_dataFolderPath);
  224. }
  225. }
  226. return _dataFolderPath;
  227. }
  228. }
  229. /// <summary>
  230. /// Gets the logger.
  231. /// </summary>
  232. /// <value>The logger.</value>
  233. public ILogger Logger { get; private set; }
  234. /// <summary>
  235. /// Gets the XML serializer.
  236. /// </summary>
  237. /// <value>The XML serializer.</value>
  238. protected IXmlSerializer XmlSerializer { get; private set; }
  239. /// <summary>
  240. /// Starts the plugin.
  241. /// </summary>
  242. /// <param name="kernel">The kernel.</param>
  243. /// <param name="xmlSerializer">The XML serializer.</param>
  244. /// <param name="logger">The logger.</param>
  245. /// <exception cref="System.ArgumentNullException">kernel</exception>
  246. public void Initialize(IKernel kernel, IXmlSerializer xmlSerializer, ILogger logger)
  247. {
  248. if (kernel == null)
  249. {
  250. throw new ArgumentNullException("kernel");
  251. }
  252. if (xmlSerializer == null)
  253. {
  254. throw new ArgumentNullException("xmlSerializer");
  255. }
  256. if (logger == null)
  257. {
  258. throw new ArgumentNullException("logger");
  259. }
  260. XmlSerializer = xmlSerializer;
  261. Logger = logger;
  262. Kernel = kernel;
  263. if (kernel.KernelContext == KernelContext.Server)
  264. {
  265. InitializeOnServer(!File.Exists(ConfigurationFilePath));
  266. }
  267. }
  268. /// <summary>
  269. /// Starts the plugin on the server
  270. /// </summary>
  271. /// <param name="isFirstRun">if set to <c>true</c> [is first run].</param>
  272. protected virtual void InitializeOnServer(bool isFirstRun)
  273. {
  274. }
  275. /// <summary>
  276. /// Disposes the plugins. Undos all actions performed during Init.
  277. /// </summary>
  278. public void Dispose()
  279. {
  280. Dispose(true);
  281. GC.SuppressFinalize(this);
  282. }
  283. /// <summary>
  284. /// Releases unmanaged and - optionally - managed resources.
  285. /// </summary>
  286. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  287. protected void Dispose(bool dispose)
  288. {
  289. if (Kernel.KernelContext == KernelContext.Server)
  290. {
  291. DisposeOnServer(dispose);
  292. }
  293. }
  294. /// <summary>
  295. /// Releases unmanaged and - optionally - managed resources.
  296. /// </summary>
  297. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  298. protected virtual void DisposeOnServer(bool dispose)
  299. {
  300. }
  301. /// <summary>
  302. /// The _save lock
  303. /// </summary>
  304. private readonly object _configurationSaveLock = new object();
  305. /// <summary>
  306. /// Saves the current configuration to the file system
  307. /// </summary>
  308. /// <exception cref="System.InvalidOperationException">Cannot call Plugin.SaveConfiguration from the UI.</exception>
  309. public virtual void SaveConfiguration()
  310. {
  311. if (Kernel.KernelContext != KernelContext.Server)
  312. {
  313. throw new InvalidOperationException("Cannot call Plugin.SaveConfiguration from the UI.");
  314. }
  315. Logger.Info("Saving configuration");
  316. lock (_configurationSaveLock)
  317. {
  318. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  319. }
  320. }
  321. /// <summary>
  322. /// Completely overwrites the current configuration with a new copy
  323. /// Returns true or false indicating success or failure
  324. /// </summary>
  325. /// <param name="configuration">The configuration.</param>
  326. /// <exception cref="System.ArgumentNullException">configuration</exception>
  327. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  328. {
  329. if (configuration == null)
  330. {
  331. throw new ArgumentNullException("configuration");
  332. }
  333. Configuration = (TConfigurationType)configuration;
  334. SaveConfiguration();
  335. }
  336. /// <summary>
  337. /// Gets the plugin info.
  338. /// </summary>
  339. /// <returns>PluginInfo.</returns>
  340. public PluginInfo GetPluginInfo()
  341. {
  342. var info = new PluginInfo
  343. {
  344. Name = Name,
  345. DownloadToUI = this is IUIPlugin,
  346. Version = Version.ToString(),
  347. AssemblyFileName = AssemblyFileName,
  348. ConfigurationDateLastModified = ConfigurationDateLastModified,
  349. Description = Description,
  350. IsCorePlugin = IsCorePlugin,
  351. Id = Id,
  352. EnableAutoUpdate = Configuration.EnableAutoUpdate,
  353. UpdateClass = Configuration.UpdateClass,
  354. ConfigurationFileName = ConfigurationFileName
  355. };
  356. var uiPlugin = this as IUIPlugin;
  357. if (uiPlugin != null)
  358. {
  359. info.MinimumRequiredUIVersion = uiPlugin.MinimumRequiredUIVersion.ToString();
  360. }
  361. return info;
  362. }
  363. /// <summary>
  364. /// Called when just before the plugin is uninstalled from the server.
  365. /// </summary>
  366. public virtual void OnUninstalling()
  367. {
  368. }
  369. /// <summary>
  370. /// Gets the plugin's configuration
  371. /// </summary>
  372. /// <value>The configuration.</value>
  373. BasePluginConfiguration IPlugin.Configuration
  374. {
  375. get { return Configuration; }
  376. }
  377. }
  378. }