BasePlugin.cs 8.1 KB

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