BasePlugin.cs 7.8 KB

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