BasePlugin.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 = null;
  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 = null;
  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. InitializeInternal();
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Starts the plugin.
  170. /// </summary>
  171. protected virtual void InitializeInternal()
  172. {
  173. }
  174. /// <summary>
  175. /// Disposes the plugins. Undos all actions performed during Init.
  176. /// </summary>
  177. public virtual void Dispose()
  178. {
  179. }
  180. public void ReloadConfiguration()
  181. {
  182. if (!File.Exists(ConfigurationFilePath))
  183. {
  184. Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
  185. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  186. }
  187. else
  188. {
  189. Configuration = XmlSerializer.DeserializeFromFile(ConfigurationType, ConfigurationFilePath) as BasePluginConfiguration;
  190. }
  191. // Reset this so it will be loaded again next time it's accessed
  192. _ConfigurationDateLastModified = null;
  193. }
  194. }
  195. }