2
0

BasePlugin.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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).Equals(
  49. Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  50. StringComparison.InvariantCulture);
  51. /// <summary>
  52. /// Gets the plugin info.
  53. /// </summary>
  54. /// <returns>PluginInfo.</returns>
  55. public virtual PluginInfo GetPluginInfo()
  56. {
  57. var info = new PluginInfo
  58. {
  59. Name = Name,
  60. Version = Version.ToString(),
  61. Description = Description,
  62. Id = Id.ToString(),
  63. CanUninstall = CanUninstall
  64. };
  65. return info;
  66. }
  67. /// <summary>
  68. /// Called just before the plugin is uninstalled from the server.
  69. /// </summary>
  70. public virtual void OnUninstalling()
  71. {
  72. }
  73. /// <inheritdoc />
  74. public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
  75. {
  76. AssemblyFilePath = assemblyFilePath;
  77. DataFolderPath = dataFolderPath;
  78. Version = assemblyVersion;
  79. }
  80. /// <inheritdoc />
  81. public void SetId(Guid assemblyId)
  82. {
  83. Id = assemblyId;
  84. }
  85. }
  86. /// <summary>
  87. /// Provides a common base class for all plugins.
  88. /// </summary>
  89. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  90. public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
  91. where TConfigurationType : BasePluginConfiguration
  92. {
  93. /// <summary>
  94. /// The configuration sync lock.
  95. /// </summary>
  96. private readonly object _configurationSyncLock = new object();
  97. /// <summary>
  98. /// The configuration save lock.
  99. /// </summary>
  100. private readonly object _configurationSaveLock = new object();
  101. private Action<string> _directoryCreateFn;
  102. /// <summary>
  103. /// The configuration.
  104. /// </summary>
  105. private TConfigurationType _configuration;
  106. /// <summary>
  107. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  108. /// </summary>
  109. /// <param name="applicationPaths">The application paths.</param>
  110. /// <param name="xmlSerializer">The XML serializer.</param>
  111. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  112. {
  113. ApplicationPaths = applicationPaths;
  114. XmlSerializer = xmlSerializer;
  115. }
  116. /// <summary>
  117. /// Gets the application paths.
  118. /// </summary>
  119. /// <value>The application paths.</value>
  120. protected IApplicationPaths ApplicationPaths { get; private set; }
  121. /// <summary>
  122. /// Gets the XML serializer.
  123. /// </summary>
  124. /// <value>The XML serializer.</value>
  125. protected IXmlSerializer XmlSerializer { get; private set; }
  126. /// <summary>
  127. /// Gets the type of configuration this plugin uses.
  128. /// </summary>
  129. /// <value>The type of the configuration.</value>
  130. public Type ConfigurationType => typeof(TConfigurationType);
  131. /// <summary>
  132. /// Gets the name the assembly file.
  133. /// </summary>
  134. /// <value>The name of the assembly file.</value>
  135. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  136. /// <summary>
  137. /// Gets or sets the plugin configuration.
  138. /// </summary>
  139. /// <value>The configuration.</value>
  140. public TConfigurationType Configuration
  141. {
  142. get
  143. {
  144. // Lazy load
  145. if (_configuration == null)
  146. {
  147. lock (_configurationSyncLock)
  148. {
  149. if (_configuration == null)
  150. {
  151. _configuration = LoadConfiguration();
  152. }
  153. }
  154. }
  155. return _configuration;
  156. }
  157. protected set => _configuration = value;
  158. }
  159. /// <summary>
  160. /// Gets the name of the configuration file. Subclasses should override.
  161. /// </summary>
  162. /// <value>The name of the configuration file.</value>
  163. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  164. /// <summary>
  165. /// Gets the full path to the configuration file.
  166. /// </summary>
  167. /// <value>The configuration file path.</value>
  168. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  169. /// <summary>
  170. /// Gets the plugin configuration.
  171. /// </summary>
  172. /// <value>The configuration.</value>
  173. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  174. /// <inheritdoc />
  175. public void SetStartupInfo(Action<string> directoryCreateFn)
  176. {
  177. // hack alert, until the .net core transition is complete
  178. _directoryCreateFn = directoryCreateFn;
  179. }
  180. private TConfigurationType LoadConfiguration()
  181. {
  182. var path = ConfigurationFilePath;
  183. try
  184. {
  185. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  186. }
  187. catch
  188. {
  189. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  190. }
  191. }
  192. /// <summary>
  193. /// Saves the current configuration to the file system.
  194. /// </summary>
  195. public virtual void SaveConfiguration()
  196. {
  197. lock (_configurationSaveLock)
  198. {
  199. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  200. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  201. }
  202. }
  203. /// <inheritdoc />
  204. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  205. {
  206. if (configuration == null)
  207. {
  208. throw new ArgumentNullException(nameof(configuration));
  209. }
  210. Configuration = (TConfigurationType)configuration;
  211. SaveConfiguration();
  212. }
  213. /// <inheritdoc />
  214. public override PluginInfo GetPluginInfo()
  215. {
  216. var info = base.GetPluginInfo();
  217. info.ConfigurationFileName = ConfigurationFileName;
  218. return info;
  219. }
  220. }
  221. }