2
0

BasePlugin.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.Serialization;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Plugins;
  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, () => XmlSerializer.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath, Logger) 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. /// Returns true or false indicating if the plugin should be downloaded and run within the Ui.
  231. /// </summary>
  232. /// <value><c>true</c> if [download to UI]; otherwise, <c>false</c>.</value>
  233. public virtual bool DownloadToUi
  234. {
  235. get
  236. {
  237. return false;
  238. }
  239. }
  240. /// <summary>
  241. /// Gets the logger.
  242. /// </summary>
  243. /// <value>The logger.</value>
  244. public ILogger Logger { get; private set; }
  245. /// <summary>
  246. /// Starts the plugin.
  247. /// </summary>
  248. /// <param name="kernel">The kernel.</param>
  249. /// <param name="logger">The logger.</param>
  250. /// <exception cref="System.ArgumentNullException">kernel</exception>
  251. public void Initialize(IKernel kernel, ILogger logger)
  252. {
  253. if (kernel == null)
  254. {
  255. throw new ArgumentNullException("kernel");
  256. }
  257. if (logger == null)
  258. {
  259. throw new ArgumentNullException("logger");
  260. }
  261. Logger = logger;
  262. Kernel = kernel;
  263. if (kernel.KernelContext == KernelContext.Server)
  264. {
  265. InitializeOnServer(!File.Exists(ConfigurationFilePath));
  266. }
  267. else if (kernel.KernelContext == KernelContext.Ui)
  268. {
  269. InitializeInUi();
  270. }
  271. }
  272. /// <summary>
  273. /// Starts the plugin on the server
  274. /// </summary>
  275. /// <param name="isFirstRun">if set to <c>true</c> [is first run].</param>
  276. protected virtual void InitializeOnServer(bool isFirstRun)
  277. {
  278. }
  279. /// <summary>
  280. /// Starts the plugin in the Ui
  281. /// </summary>
  282. protected virtual void InitializeInUi()
  283. {
  284. }
  285. /// <summary>
  286. /// Disposes the plugins. Undos all actions performed during Init.
  287. /// </summary>
  288. public void Dispose()
  289. {
  290. Dispose(true);
  291. GC.SuppressFinalize(this);
  292. }
  293. /// <summary>
  294. /// Releases unmanaged and - optionally - managed resources.
  295. /// </summary>
  296. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  297. protected void Dispose(bool dispose)
  298. {
  299. if (Kernel.KernelContext == KernelContext.Server)
  300. {
  301. DisposeOnServer(dispose);
  302. }
  303. else if (Kernel.KernelContext == KernelContext.Ui)
  304. {
  305. DisposeInUI(dispose);
  306. }
  307. }
  308. /// <summary>
  309. /// Releases unmanaged and - optionally - managed resources.
  310. /// </summary>
  311. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  312. protected virtual void DisposeOnServer(bool dispose)
  313. {
  314. }
  315. /// <summary>
  316. /// Releases unmanaged and - optionally - managed resources.
  317. /// </summary>
  318. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  319. protected virtual void DisposeInUI(bool dispose)
  320. {
  321. }
  322. /// <summary>
  323. /// The _save lock
  324. /// </summary>
  325. private readonly object _configurationSaveLock = new object();
  326. /// <summary>
  327. /// Saves the current configuration to the file system
  328. /// </summary>
  329. /// <exception cref="System.InvalidOperationException">Cannot call Plugin.SaveConfiguration from the UI.</exception>
  330. public virtual void SaveConfiguration()
  331. {
  332. if (Kernel.KernelContext != KernelContext.Server)
  333. {
  334. throw new InvalidOperationException("Cannot call Plugin.SaveConfiguration from the UI.");
  335. }
  336. Logger.Info("Saving configuration");
  337. lock (_configurationSaveLock)
  338. {
  339. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  340. }
  341. // Notify connected UI's
  342. Kernel.TcpManager.SendWebSocketMessage("PluginConfigurationUpdated-" + Name, Configuration);
  343. }
  344. /// <summary>
  345. /// Completely overwrites the current configuration with a new copy
  346. /// Returns true or false indicating success or failure
  347. /// </summary>
  348. /// <param name="configuration">The configuration.</param>
  349. /// <exception cref="System.ArgumentNullException">configuration</exception>
  350. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  351. {
  352. if (configuration == null)
  353. {
  354. throw new ArgumentNullException("configuration");
  355. }
  356. Configuration = (TConfigurationType)configuration;
  357. SaveConfiguration();
  358. }
  359. /// <summary>
  360. /// Gets the plugin info.
  361. /// </summary>
  362. /// <returns>PluginInfo.</returns>
  363. public PluginInfo GetPluginInfo()
  364. {
  365. var info = new PluginInfo
  366. {
  367. Name = Name,
  368. DownloadToUI = DownloadToUi,
  369. Version = Version.ToString(),
  370. AssemblyFileName = AssemblyFileName,
  371. ConfigurationDateLastModified = ConfigurationDateLastModified,
  372. Description = Description,
  373. IsCorePlugin = IsCorePlugin,
  374. Id = Id,
  375. EnableAutoUpdate = Configuration.EnableAutoUpdate,
  376. UpdateClass = Configuration.UpdateClass,
  377. ConfigurationFileName = ConfigurationFileName
  378. };
  379. var uiPlugin = this as IUIPlugin;
  380. if (uiPlugin != null)
  381. {
  382. info.MinimumRequiredUIVersion = uiPlugin.MinimumRequiredUIVersion.ToString();
  383. }
  384. return info;
  385. }
  386. /// <summary>
  387. /// Called when just before the plugin is uninstalled from the server.
  388. /// </summary>
  389. public virtual void OnUninstalling()
  390. {
  391. }
  392. /// <summary>
  393. /// Gets the plugin's configuration
  394. /// </summary>
  395. /// <value>The configuration.</value>
  396. BasePluginConfiguration IPlugin.Configuration
  397. {
  398. get { return Configuration; }
  399. }
  400. }
  401. }