BasePlugin.cs 7.0 KB

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