BasePlugin.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Common.Json;
  4. using MediaBrowser.Model.Plugins;
  5. using MediaBrowser.Common.Kernel;
  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. protected 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. /// <summary>
  36. /// Gets or sets the plugin's current context
  37. /// </summary>
  38. public KernelContext Context { get; set; }
  39. /// <summary>
  40. /// Gets the name of the plugin
  41. /// </summary>
  42. public abstract string Name { get; }
  43. /// <summary>
  44. /// Gets the type of configuration this plugin uses
  45. /// </summary>
  46. protected abstract Type ConfigurationType { get; }
  47. /// <summary>
  48. /// Gets or sets the path to the plugin's folder
  49. /// </summary>
  50. public string Path { get; set; }
  51. /// <summary>
  52. /// Gets or sets the plugin version
  53. /// </summary>
  54. public Version Version { get; set; }
  55. /// <summary>
  56. /// Gets or sets the current plugin configuration
  57. /// </summary>
  58. public BasePluginConfiguration Configuration { get; protected set; }
  59. protected string ConfigurationPath
  60. {
  61. get
  62. {
  63. return System.IO.Path.Combine(Path, "config.js");
  64. }
  65. }
  66. public bool Enabled
  67. {
  68. get
  69. {
  70. return Configuration.Enabled;
  71. }
  72. }
  73. public DateTime ConfigurationDateLastModified
  74. {
  75. get
  76. {
  77. return Configuration.DateLastModified;
  78. }
  79. }
  80. /// <summary>
  81. /// Returns true or false indicating if the plugin should be downloaded and run within the UI.
  82. /// </summary>
  83. public virtual bool DownloadToUI
  84. {
  85. get
  86. {
  87. return false;
  88. }
  89. }
  90. public void ReloadConfiguration()
  91. {
  92. if (!File.Exists(ConfigurationPath))
  93. {
  94. Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
  95. }
  96. else
  97. {
  98. Configuration = JsonSerializer.DeserializeFromFile(ConfigurationType, ConfigurationPath) as BasePluginConfiguration;
  99. Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
  100. }
  101. }
  102. /// <summary>
  103. /// Starts the plugin.
  104. /// </summary>
  105. public virtual void Init()
  106. {
  107. }
  108. /// <summary>
  109. /// Disposes the plugins. Undos all actions performed during Init.
  110. /// </summary>
  111. public virtual void Dispose()
  112. {
  113. }
  114. }
  115. }