BasePlugin.cs 8.4 KB

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