BasePlugin.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Common.Json;
  4. using MediaBrowser.Model.Plugins;
  5. namespace MediaBrowser.Common.Plugins
  6. {
  7. /// <summary>
  8. /// Provides a BasePlugin with generics, allowing for strongly typed configuration access.
  9. /// </summary>
  10. public abstract class BaseGenericPlugin<TConfigurationType> : BasePlugin
  11. where TConfigurationType : BasePluginConfiguration, new()
  12. {
  13. public new TConfigurationType Configuration
  14. {
  15. get
  16. {
  17. return base.Configuration as TConfigurationType;
  18. }
  19. set
  20. {
  21. base.Configuration = value;
  22. }
  23. }
  24. public override void ReloadConfiguration()
  25. {
  26. if (!File.Exists(ConfigurationPath))
  27. {
  28. Configuration = new TConfigurationType();
  29. }
  30. else
  31. {
  32. Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
  33. Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Provides a common base class for all plugins
  39. /// </summary>
  40. public abstract class BasePlugin
  41. {
  42. public abstract string Name { get; }
  43. public string Path { get; set; }
  44. public Version Version { get; set; }
  45. public BasePluginConfiguration Configuration { get; protected set; }
  46. protected string ConfigurationPath
  47. {
  48. get
  49. {
  50. return System.IO.Path.Combine(Path, "config.js");
  51. }
  52. }
  53. public bool Enabled
  54. {
  55. get
  56. {
  57. return Configuration.Enabled;
  58. }
  59. }
  60. public DateTime ConfigurationDateLastModified
  61. {
  62. get
  63. {
  64. return Configuration.DateLastModified;
  65. }
  66. }
  67. /// <summary>
  68. /// Returns true or false indicating if the plugin should be downloaded and run within the UI.
  69. /// </summary>
  70. public virtual bool DownloadToUI
  71. {
  72. get
  73. {
  74. return false;
  75. }
  76. }
  77. public abstract void ReloadConfiguration();
  78. public virtual void InitInServer()
  79. {
  80. }
  81. public virtual void InitInUI()
  82. {
  83. }
  84. }
  85. }