BasePlugin.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #pragma warning disable SA1402
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Model.Plugins;
  7. using MediaBrowser.Model.Serialization;
  8. namespace MediaBrowser.Common.Plugins
  9. {
  10. /// <summary>
  11. /// Provides a common base class for all plugins.
  12. /// </summary>
  13. public abstract class BasePlugin : IPlugin, IPluginAssembly
  14. {
  15. /// <summary>
  16. /// Gets the name of the plugin.
  17. /// </summary>
  18. /// <value>The name.</value>
  19. public abstract string Name { get; }
  20. /// <summary>
  21. /// Gets the description.
  22. /// </summary>
  23. /// <value>The description.</value>
  24. public virtual string Description => string.Empty;
  25. /// <summary>
  26. /// Gets the unique id.
  27. /// </summary>
  28. /// <value>The unique id.</value>
  29. public virtual Guid Id { get; private set; }
  30. /// <summary>
  31. /// Gets the plugin version.
  32. /// </summary>
  33. /// <value>The version.</value>
  34. public Version Version { get; private set; }
  35. /// <summary>
  36. /// Gets the path to the assembly file.
  37. /// </summary>
  38. /// <value>The assembly file path.</value>
  39. public string AssemblyFilePath { get; private set; }
  40. /// <summary>
  41. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed.
  42. /// </summary>
  43. /// <value>The data folder path.</value>
  44. public string DataFolderPath { get; private set; }
  45. /// <summary>
  46. /// Gets a value indicating whether the plugin can be uninstalled.
  47. /// </summary>
  48. public bool CanUninstall => !Path.GetDirectoryName(AssemblyFilePath)
  49. .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.InvariantCulture);
  50. /// <summary>
  51. /// Gets the plugin info.
  52. /// </summary>
  53. /// <returns>PluginInfo.</returns>
  54. public virtual PluginInfo GetPluginInfo()
  55. {
  56. var info = new PluginInfo
  57. {
  58. Name = Name,
  59. Version = Version.ToString(),
  60. Description = Description,
  61. Id = Id.ToString(),
  62. CanUninstall = CanUninstall
  63. };
  64. return info;
  65. }
  66. /// <summary>
  67. /// Called just before the plugin is uninstalled from the server.
  68. /// </summary>
  69. public virtual void OnUninstalling()
  70. {
  71. }
  72. /// <inheritdoc />
  73. public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
  74. {
  75. AssemblyFilePath = assemblyFilePath;
  76. DataFolderPath = dataFolderPath;
  77. Version = assemblyVersion;
  78. }
  79. /// <inheritdoc />
  80. public void SetId(Guid assemblyId)
  81. {
  82. Id = assemblyId;
  83. }
  84. }
  85. /// <summary>
  86. /// Provides a common base class for all plugins.
  87. /// </summary>
  88. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  89. public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
  90. where TConfigurationType : BasePluginConfiguration
  91. {
  92. /// <summary>
  93. /// The configuration sync lock.
  94. /// </summary>
  95. private readonly object _configurationSyncLock = new object();
  96. /// <summary>
  97. /// The configuration save lock.
  98. /// </summary>
  99. private readonly object _configurationSaveLock = new object();
  100. private Action<string> _directoryCreateFn;
  101. /// <summary>
  102. /// The configuration.
  103. /// </summary>
  104. private TConfigurationType _configuration;
  105. /// <summary>
  106. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  107. /// </summary>
  108. /// <param name="applicationPaths">The application paths.</param>
  109. /// <param name="xmlSerializer">The XML serializer.</param>
  110. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  111. {
  112. ApplicationPaths = applicationPaths;
  113. XmlSerializer = xmlSerializer;
  114. }
  115. /// <summary>
  116. /// Gets the application paths.
  117. /// </summary>
  118. /// <value>The application paths.</value>
  119. protected IApplicationPaths ApplicationPaths { get; private set; }
  120. /// <summary>
  121. /// Gets the XML serializer.
  122. /// </summary>
  123. /// <value>The XML serializer.</value>
  124. protected IXmlSerializer XmlSerializer { get; private set; }
  125. /// <summary>
  126. /// Gets the type of configuration this plugin uses.
  127. /// </summary>
  128. /// <value>The type of the configuration.</value>
  129. public Type ConfigurationType => typeof(TConfigurationType);
  130. /// <summary>
  131. /// Gets the name the assembly file.
  132. /// </summary>
  133. /// <value>The name of the assembly file.</value>
  134. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  135. /// <summary>
  136. /// Gets or sets the plugin configuration.
  137. /// </summary>
  138. /// <value>The configuration.</value>
  139. public TConfigurationType Configuration
  140. {
  141. get
  142. {
  143. // Lazy load
  144. if (_configuration == null)
  145. {
  146. lock (_configurationSyncLock)
  147. {
  148. if (_configuration == null)
  149. {
  150. _configuration = LoadConfiguration();
  151. }
  152. }
  153. }
  154. return _configuration;
  155. }
  156. protected set => _configuration = value;
  157. }
  158. /// <summary>
  159. /// Gets the name of the configuration file. Subclasses should override.
  160. /// </summary>
  161. /// <value>The name of the configuration file.</value>
  162. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  163. /// <summary>
  164. /// Gets the full path to the configuration file.
  165. /// </summary>
  166. /// <value>The configuration file path.</value>
  167. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  168. /// <summary>
  169. /// Gets the plugin configuration.
  170. /// </summary>
  171. /// <value>The configuration.</value>
  172. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  173. /// <inheritdoc />
  174. public void SetStartupInfo(Action<string> directoryCreateFn)
  175. {
  176. // hack alert, until the .net core transition is complete
  177. _directoryCreateFn = directoryCreateFn;
  178. }
  179. private TConfigurationType LoadConfiguration()
  180. {
  181. var path = ConfigurationFilePath;
  182. try
  183. {
  184. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  185. }
  186. catch
  187. {
  188. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  189. }
  190. }
  191. /// <summary>
  192. /// Saves the current configuration to the file system.
  193. /// </summary>
  194. public virtual void SaveConfiguration()
  195. {
  196. lock (_configurationSaveLock)
  197. {
  198. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  199. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  200. }
  201. }
  202. /// <inheritdoc />
  203. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  204. {
  205. if (configuration == null)
  206. {
  207. throw new ArgumentNullException(nameof(configuration));
  208. }
  209. Configuration = (TConfigurationType)configuration;
  210. SaveConfiguration();
  211. }
  212. /// <inheritdoc />
  213. public override PluginInfo GetPluginInfo()
  214. {
  215. var info = base.GetPluginInfo();
  216. info.ConfigurationFileName = ConfigurationFileName;
  217. return info;
  218. }
  219. }
  220. }