BasePlugin.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Common.Kernel;
  4. using MediaBrowser.Common.Serialization;
  5. using MediaBrowser.Model.Plugins;
  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 { get { return Name + ".xml"; } }
  101. /// <summary>
  102. /// Gets the full path to the configuration file
  103. /// </summary>
  104. public string ConfigurationFilePath
  105. {
  106. get
  107. {
  108. return Path.Combine(Kernel.ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  109. }
  110. }
  111. private string _dataFolderPath;
  112. /// <summary>
  113. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  114. /// </summary>
  115. public string DataFolderPath
  116. {
  117. get
  118. {
  119. if (_dataFolderPath == null)
  120. {
  121. // Give the folder name the same name as the config file name
  122. // We can always make this configurable if/when needed
  123. _dataFolderPath = Path.Combine(Kernel.ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  124. if (!Directory.Exists(_dataFolderPath))
  125. {
  126. Directory.CreateDirectory(_dataFolderPath);
  127. }
  128. }
  129. return _dataFolderPath;
  130. }
  131. }
  132. public bool Enabled
  133. {
  134. get
  135. {
  136. return Configuration.Enabled;
  137. }
  138. }
  139. /// <summary>
  140. /// Returns true or false indicating if the plugin should be downloaded and run within the Ui.
  141. /// </summary>
  142. public virtual bool DownloadToUi
  143. {
  144. get
  145. {
  146. return false;
  147. }
  148. }
  149. public void Initialize(IKernel kernel)
  150. {
  151. Initialize(kernel, true);
  152. }
  153. /// <summary>
  154. /// Starts the plugin.
  155. /// </summary>
  156. public void Initialize(IKernel kernel, bool loadFeatures)
  157. {
  158. Kernel = kernel;
  159. if (loadFeatures)
  160. {
  161. ReloadConfiguration();
  162. if (Enabled)
  163. {
  164. if (kernel.KernelContext == KernelContext.Server)
  165. {
  166. InitializeOnServer();
  167. }
  168. else if (kernel.KernelContext == KernelContext.Ui)
  169. {
  170. InitializeInUi();
  171. }
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Starts the plugin on the server
  177. /// </summary>
  178. protected virtual void InitializeOnServer()
  179. {
  180. }
  181. /// <summary>
  182. /// Starts the plugin in the Ui
  183. /// </summary>
  184. protected virtual void InitializeInUi()
  185. {
  186. }
  187. /// <summary>
  188. /// Disposes the plugins. Undos all actions performed during Init.
  189. /// </summary>
  190. public void Dispose()
  191. {
  192. if (Context == KernelContext.Server)
  193. {
  194. DisposeOnServer();
  195. }
  196. else if (Context == KernelContext.Ui)
  197. {
  198. InitializeInUi();
  199. }
  200. }
  201. /// <summary>
  202. /// Disposes the plugin on the server
  203. /// </summary>
  204. protected virtual void DisposeOnServer()
  205. {
  206. }
  207. /// <summary>
  208. /// Disposes the plugin in the Ui
  209. /// </summary>
  210. protected virtual void DisposeInUi()
  211. {
  212. }
  213. public void ReloadConfiguration()
  214. {
  215. if (!File.Exists(ConfigurationFilePath))
  216. {
  217. Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
  218. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  219. }
  220. else
  221. {
  222. Configuration = XmlSerializer.DeserializeFromFile(ConfigurationType, ConfigurationFilePath) as BasePluginConfiguration;
  223. }
  224. // Reset this so it will be loaded again next time it's accessed
  225. _configurationDateLastModified = null;
  226. }
  227. }
  228. }