BasePlugin.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.Serialization;
  3. using MediaBrowser.Model.Plugins;
  4. using System;
  5. using System.IO;
  6. namespace MediaBrowser.Common.Plugins
  7. {
  8. /// <summary>
  9. /// Provides a BasePlugin with generics, allowing for strongly typed configuration access.
  10. /// </summary>
  11. public abstract class BaseGenericPlugin<TConfigurationType> : BasePlugin
  12. where TConfigurationType : BasePluginConfiguration, new()
  13. {
  14. public new TConfigurationType Configuration
  15. {
  16. get
  17. {
  18. return base.Configuration as TConfigurationType;
  19. }
  20. set
  21. {
  22. base.Configuration = value;
  23. }
  24. }
  25. public override Type ConfigurationType
  26. {
  27. get { return typeof(TConfigurationType); }
  28. }
  29. }
  30. /// <summary>
  31. /// Provides a common base class for all plugins
  32. /// </summary>
  33. public abstract class BasePlugin : IDisposable
  34. {
  35. private IKernel Kernel { get; set; }
  36. /// <summary>
  37. /// Gets or sets the plugin's current context
  38. /// </summary>
  39. protected KernelContext Context { get { return Kernel.KernelContext; } }
  40. /// <summary>
  41. /// Gets the name of the plugin
  42. /// </summary>
  43. public abstract string Name { get; }
  44. /// <summary>
  45. /// Gets the type of configuration this plugin uses
  46. /// </summary>
  47. public abstract Type ConfigurationType { get; }
  48. /// <summary>
  49. /// Gets the plugin version
  50. /// </summary>
  51. public Version Version
  52. {
  53. get
  54. {
  55. return GetType().Assembly.GetName().Version;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the name the assembly file
  60. /// </summary>
  61. public string AssemblyFileName
  62. {
  63. get
  64. {
  65. return GetType().Assembly.GetName().Name + ".dll";
  66. }
  67. }
  68. private DateTime? _configurationDateLastModified;
  69. public DateTime ConfigurationDateLastModified
  70. {
  71. get
  72. {
  73. if (_configurationDateLastModified == null)
  74. {
  75. if (File.Exists(ConfigurationFilePath))
  76. {
  77. _configurationDateLastModified = File.GetLastWriteTimeUtc(ConfigurationFilePath);
  78. }
  79. }
  80. return _configurationDateLastModified ?? DateTime.MinValue;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the path to the assembly file
  85. /// </summary>
  86. public string AssemblyFilePath
  87. {
  88. get
  89. {
  90. return Path.Combine(Kernel.ApplicationPaths.PluginsPath, AssemblyFileName);
  91. }
  92. }
  93. /// <summary>
  94. /// Gets or sets the current plugin configuration
  95. /// </summary>
  96. public BasePluginConfiguration Configuration { get; protected set; }
  97. /// <summary>
  98. /// Gets the name of the configuration file. Subclasses should override
  99. /// </summary>
  100. public virtual string ConfigurationFileName
  101. {
  102. get
  103. {
  104. return Name.Replace(" ", string.Empty) + ".xml";
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the full path to the configuration file
  109. /// </summary>
  110. public string ConfigurationFilePath
  111. {
  112. get
  113. {
  114. return Path.Combine(Kernel.ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  115. }
  116. }
  117. private string _dataFolderPath;
  118. /// <summary>
  119. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  120. /// </summary>
  121. public string DataFolderPath
  122. {
  123. get
  124. {
  125. if (_dataFolderPath == null)
  126. {
  127. // Give the folder name the same name as the config file name
  128. // We can always make this configurable if/when needed
  129. _dataFolderPath = Path.Combine(Kernel.ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  130. if (!Directory.Exists(_dataFolderPath))
  131. {
  132. Directory.CreateDirectory(_dataFolderPath);
  133. }
  134. }
  135. return _dataFolderPath;
  136. }
  137. }
  138. public bool Enabled
  139. {
  140. get
  141. {
  142. return Configuration.Enabled;
  143. }
  144. }
  145. /// <summary>
  146. /// Returns true or false indicating if the plugin should be downloaded and run within the Ui.
  147. /// </summary>
  148. public virtual bool DownloadToUi
  149. {
  150. get
  151. {
  152. return false;
  153. }
  154. }
  155. public void Initialize(IKernel kernel)
  156. {
  157. Initialize(kernel, true);
  158. }
  159. /// <summary>
  160. /// Starts the plugin.
  161. /// </summary>
  162. public void Initialize(IKernel kernel, bool loadFeatures)
  163. {
  164. Kernel = kernel;
  165. if (loadFeatures)
  166. {
  167. ReloadConfiguration();
  168. if (Enabled)
  169. {
  170. if (kernel.KernelContext == KernelContext.Server)
  171. {
  172. InitializeOnServer();
  173. }
  174. else if (kernel.KernelContext == KernelContext.Ui)
  175. {
  176. InitializeInUi();
  177. }
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Starts the plugin on the server
  183. /// </summary>
  184. protected virtual void InitializeOnServer()
  185. {
  186. }
  187. /// <summary>
  188. /// Starts the plugin in the Ui
  189. /// </summary>
  190. protected virtual void InitializeInUi()
  191. {
  192. }
  193. /// <summary>
  194. /// Disposes the plugins. Undos all actions performed during Init.
  195. /// </summary>
  196. public void Dispose()
  197. {
  198. if (Context == KernelContext.Server)
  199. {
  200. DisposeOnServer();
  201. }
  202. else if (Context == KernelContext.Ui)
  203. {
  204. InitializeInUi();
  205. }
  206. }
  207. /// <summary>
  208. /// Disposes the plugin on the server
  209. /// </summary>
  210. protected virtual void DisposeOnServer()
  211. {
  212. }
  213. /// <summary>
  214. /// Disposes the plugin in the Ui
  215. /// </summary>
  216. protected virtual void DisposeInUi()
  217. {
  218. }
  219. public void ReloadConfiguration()
  220. {
  221. if (!File.Exists(ConfigurationFilePath))
  222. {
  223. Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
  224. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  225. }
  226. else
  227. {
  228. Configuration = XmlSerializer.DeserializeFromFile(ConfigurationType, ConfigurationFilePath) as BasePluginConfiguration;
  229. }
  230. // Reset this so it will be loaded again next time it's accessed
  231. _configurationDateLastModified = null;
  232. }
  233. }
  234. }