BasePlugin.cs 7.1 KB

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