BasePlugin.cs 8.1 KB

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