BasePlugin.cs 6.9 KB

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