BasePlugin.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Model.Plugins;
  3. using MediaBrowser.Model.Serialization;
  4. using System;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. namespace MediaBrowser.Common.Plugins
  10. {
  11. /// <summary>
  12. /// Provides a common base class for all plugins
  13. /// </summary>
  14. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  15. public abstract class BasePlugin<TConfigurationType> : IPlugin
  16. where TConfigurationType : BasePluginConfiguration
  17. {
  18. /// <summary>
  19. /// Gets the kernel.
  20. /// </summary>
  21. /// <value>The kernel.</value>
  22. protected IKernel Kernel { get; private set; }
  23. /// <summary>
  24. /// Gets the XML serializer.
  25. /// </summary>
  26. /// <value>The XML serializer.</value>
  27. protected IXmlSerializer XmlSerializer { get; private set; }
  28. /// <summary>
  29. /// Gets or sets the plugin's current context
  30. /// </summary>
  31. /// <value>The context.</value>
  32. protected KernelContext Context { get { return Kernel.KernelContext; } }
  33. /// <summary>
  34. /// Gets the name of the plugin
  35. /// </summary>
  36. /// <value>The name.</value>
  37. public abstract string Name { get; }
  38. /// <summary>
  39. /// Gets the description.
  40. /// </summary>
  41. /// <value>The description.</value>
  42. public virtual string Description
  43. {
  44. get { return string.Empty; }
  45. }
  46. /// <summary>
  47. /// Gets a value indicating whether this instance is a core plugin.
  48. /// </summary>
  49. /// <value><c>true</c> if this instance is a core plugin; otherwise, <c>false</c>.</value>
  50. public virtual bool IsCorePlugin
  51. {
  52. get
  53. {
  54. return false;
  55. }
  56. }
  57. /// <summary>
  58. /// Gets a value indicating whether this instance is first run.
  59. /// </summary>
  60. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  61. public bool IsFirstRun { get; private set; }
  62. /// <summary>
  63. /// Gets the type of configuration this plugin uses
  64. /// </summary>
  65. /// <value>The type of the configuration.</value>
  66. public Type ConfigurationType
  67. {
  68. get { return typeof(TConfigurationType); }
  69. }
  70. /// <summary>
  71. /// The _assembly name
  72. /// </summary>
  73. private AssemblyName _assemblyName;
  74. /// <summary>
  75. /// Gets the name of the assembly.
  76. /// </summary>
  77. /// <value>The name of the assembly.</value>
  78. protected AssemblyName AssemblyName
  79. {
  80. get
  81. {
  82. return _assemblyName ?? (_assemblyName = GetType().Assembly.GetName());
  83. }
  84. }
  85. /// <summary>
  86. /// The _unique id
  87. /// </summary>
  88. private Guid? _uniqueId;
  89. /// <summary>
  90. /// Gets the unique id.
  91. /// </summary>
  92. /// <value>The unique id.</value>
  93. public Guid Id
  94. {
  95. get
  96. {
  97. if (!_uniqueId.HasValue)
  98. {
  99. _uniqueId = Marshal.GetTypeLibGuidForAssembly(GetType().Assembly);
  100. }
  101. return _uniqueId.Value;
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the plugin version
  106. /// </summary>
  107. /// <value>The version.</value>
  108. public Version Version
  109. {
  110. get
  111. {
  112. return AssemblyName.Version;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets the name the assembly file
  117. /// </summary>
  118. /// <value>The name of the assembly file.</value>
  119. public string AssemblyFileName
  120. {
  121. get
  122. {
  123. return AssemblyName.Name + ".dll";
  124. }
  125. }
  126. /// <summary>
  127. /// Gets the last date modified of the configuration
  128. /// </summary>
  129. /// <value>The configuration date last modified.</value>
  130. public DateTime ConfigurationDateLastModified
  131. {
  132. get
  133. {
  134. // Ensure it's been lazy loaded
  135. var config = Configuration;
  136. return File.GetLastWriteTimeUtc(ConfigurationFilePath);
  137. }
  138. }
  139. /// <summary>
  140. /// Gets the last date modified of the plugin
  141. /// </summary>
  142. /// <value>The assembly date last modified.</value>
  143. public DateTime AssemblyDateLastModified
  144. {
  145. get
  146. {
  147. return File.GetLastWriteTimeUtc(AssemblyFilePath);
  148. }
  149. }
  150. /// <summary>
  151. /// Gets the path to the assembly file
  152. /// </summary>
  153. /// <value>The assembly file path.</value>
  154. public string AssemblyFilePath
  155. {
  156. get
  157. {
  158. return Path.Combine(Kernel.ApplicationPaths.PluginsPath, AssemblyFileName);
  159. }
  160. }
  161. /// <summary>
  162. /// The _configuration sync lock
  163. /// </summary>
  164. private object _configurationSyncLock = new object();
  165. /// <summary>
  166. /// The _configuration initialized
  167. /// </summary>
  168. private bool _configurationInitialized;
  169. /// <summary>
  170. /// The _configuration
  171. /// </summary>
  172. private TConfigurationType _configuration;
  173. /// <summary>
  174. /// Gets the plugin's configuration
  175. /// </summary>
  176. /// <value>The configuration.</value>
  177. public TConfigurationType Configuration
  178. {
  179. get
  180. {
  181. // Lazy load
  182. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => Kernel.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath) as TConfigurationType);
  183. return _configuration;
  184. }
  185. protected set
  186. {
  187. _configuration = value;
  188. if (value == null)
  189. {
  190. _configurationInitialized = false;
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Gets the name of the configuration file. Subclasses should override
  196. /// </summary>
  197. /// <value>The name of the configuration file.</value>
  198. public virtual string ConfigurationFileName
  199. {
  200. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  201. }
  202. /// <summary>
  203. /// Gets the full path to the configuration file
  204. /// </summary>
  205. /// <value>The configuration file path.</value>
  206. public string ConfigurationFilePath
  207. {
  208. get
  209. {
  210. return Path.Combine(Kernel.ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  211. }
  212. }
  213. /// <summary>
  214. /// The _data folder path
  215. /// </summary>
  216. private string _dataFolderPath;
  217. /// <summary>
  218. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  219. /// </summary>
  220. /// <value>The data folder path.</value>
  221. public string DataFolderPath
  222. {
  223. get
  224. {
  225. if (_dataFolderPath == null)
  226. {
  227. // Give the folder name the same name as the config file name
  228. // We can always make this configurable if/when needed
  229. _dataFolderPath = Path.Combine(Kernel.ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  230. if (!Directory.Exists(_dataFolderPath))
  231. {
  232. Directory.CreateDirectory(_dataFolderPath);
  233. }
  234. }
  235. return _dataFolderPath;
  236. }
  237. }
  238. /// <summary>
  239. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  240. /// </summary>
  241. /// <param name="kernel">The kernel.</param>
  242. /// <param name="xmlSerializer">The XML serializer.</param>
  243. protected BasePlugin(IKernel kernel, IXmlSerializer xmlSerializer)
  244. {
  245. Kernel = kernel;
  246. XmlSerializer = xmlSerializer;
  247. }
  248. /// <summary>
  249. /// The _save lock
  250. /// </summary>
  251. private readonly object _configurationSaveLock = new object();
  252. /// <summary>
  253. /// Saves the current configuration to the file system
  254. /// </summary>
  255. /// <exception cref="System.InvalidOperationException">Cannot call Plugin.SaveConfiguration from the UI.</exception>
  256. public virtual void SaveConfiguration()
  257. {
  258. if (Kernel.KernelContext != KernelContext.Server)
  259. {
  260. throw new InvalidOperationException("Cannot call Plugin.SaveConfiguration from the UI.");
  261. }
  262. lock (_configurationSaveLock)
  263. {
  264. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  265. }
  266. }
  267. /// <summary>
  268. /// Completely overwrites the current configuration with a new copy
  269. /// Returns true or false indicating success or failure
  270. /// </summary>
  271. /// <param name="configuration">The configuration.</param>
  272. /// <exception cref="System.ArgumentNullException">configuration</exception>
  273. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  274. {
  275. if (configuration == null)
  276. {
  277. throw new ArgumentNullException("configuration");
  278. }
  279. Configuration = (TConfigurationType)configuration;
  280. SaveConfiguration();
  281. }
  282. /// <summary>
  283. /// Gets the plugin info.
  284. /// </summary>
  285. /// <returns>PluginInfo.</returns>
  286. public PluginInfo GetPluginInfo()
  287. {
  288. var info = new PluginInfo
  289. {
  290. Name = Name,
  291. DownloadToUI = this is IUIPlugin,
  292. Version = Version.ToString(),
  293. AssemblyFileName = AssemblyFileName,
  294. ConfigurationDateLastModified = ConfigurationDateLastModified,
  295. Description = Description,
  296. IsCorePlugin = IsCorePlugin,
  297. Id = Id,
  298. EnableAutoUpdate = Configuration.EnableAutoUpdate,
  299. UpdateClass = Configuration.UpdateClass,
  300. ConfigurationFileName = ConfigurationFileName
  301. };
  302. var uiPlugin = this as IUIPlugin;
  303. if (uiPlugin != null)
  304. {
  305. info.MinimumRequiredUIVersion = uiPlugin.MinimumRequiredUIVersion.ToString();
  306. }
  307. return info;
  308. }
  309. /// <summary>
  310. /// Called when just before the plugin is uninstalled from the server.
  311. /// </summary>
  312. public virtual void OnUninstalling()
  313. {
  314. }
  315. /// <summary>
  316. /// Gets the plugin's configuration
  317. /// </summary>
  318. /// <value>The configuration.</value>
  319. BasePluginConfiguration IPlugin.Configuration
  320. {
  321. get { return Configuration; }
  322. }
  323. }
  324. }