BasePlugin.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /// <summary>
  9. /// Provides a common base class for all plugins
  10. /// </summary>
  11. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  12. public abstract class BasePlugin<TConfigurationType> : IPlugin, IPluginAssembly
  13. where TConfigurationType : BasePluginConfiguration
  14. {
  15. /// <summary>
  16. /// Gets the application paths.
  17. /// </summary>
  18. /// <value>The application paths.</value>
  19. protected IApplicationPaths ApplicationPaths { get; private set; }
  20. /// <summary>
  21. /// Gets the XML serializer.
  22. /// </summary>
  23. /// <value>The XML serializer.</value>
  24. protected IXmlSerializer XmlSerializer { get; private set; }
  25. /// <summary>
  26. /// Gets the name of the plugin
  27. /// </summary>
  28. /// <value>The name.</value>
  29. public abstract string Name { get; }
  30. /// <summary>
  31. /// Gets a value indicating whether this instance is first run.
  32. /// </summary>
  33. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  34. public bool IsFirstRun { get; private set; }
  35. /// <summary>
  36. /// Gets the description.
  37. /// </summary>
  38. /// <value>The description.</value>
  39. public virtual string Description
  40. {
  41. get { return string.Empty; }
  42. }
  43. /// <summary>
  44. /// Gets the type of configuration this plugin uses
  45. /// </summary>
  46. /// <value>The type of the configuration.</value>
  47. public Type ConfigurationType
  48. {
  49. get { return typeof(TConfigurationType); }
  50. }
  51. public void SetAttributes(string assemblyFilePath, string assemblyFileName, Version assemblyVersion)
  52. {
  53. AssemblyFilePath = assemblyFilePath;
  54. AssemblyFileName = assemblyFileName;
  55. Version = assemblyVersion;
  56. }
  57. public void SetId(Guid assemblyId)
  58. {
  59. Id = assemblyId;
  60. }
  61. private Func<string, DateTime> _dateModifiedFn;
  62. private Action<string> _directoryCreateFn;
  63. public void SetStartupInfo(bool isFirstRun, Func<string, DateTime> dateModifiedFn, Action<string> directoryCreateFn)
  64. {
  65. IsFirstRun = isFirstRun;
  66. // hack alert, until the .net core transition is complete
  67. _dateModifiedFn = dateModifiedFn;
  68. _directoryCreateFn = directoryCreateFn;
  69. }
  70. /// <summary>
  71. /// Gets the unique id.
  72. /// </summary>
  73. /// <value>The unique id.</value>
  74. public virtual Guid Id { get; private set; }
  75. /// <summary>
  76. /// Gets the plugin version
  77. /// </summary>
  78. /// <value>The version.</value>
  79. public Version Version { get; private set; }
  80. /// <summary>
  81. /// Gets the name the assembly file
  82. /// </summary>
  83. /// <value>The name of the assembly file.</value>
  84. protected string AssemblyFileName { get; private set; }
  85. /// <summary>
  86. /// Gets the last date modified of the configuration
  87. /// </summary>
  88. /// <value>The configuration date last modified.</value>
  89. public DateTime ConfigurationDateLastModified
  90. {
  91. get
  92. {
  93. // Ensure it's been lazy loaded
  94. var config = Configuration;
  95. return _dateModifiedFn(ConfigurationFilePath);
  96. }
  97. }
  98. /// <summary>
  99. /// Gets the path to the assembly file
  100. /// </summary>
  101. /// <value>The assembly file path.</value>
  102. public string AssemblyFilePath { get; private set; }
  103. /// <summary>
  104. /// The _configuration sync lock
  105. /// </summary>
  106. private readonly object _configurationSyncLock = new object();
  107. /// <summary>
  108. /// The _configuration
  109. /// </summary>
  110. private TConfigurationType _configuration;
  111. /// <summary>
  112. /// Gets the plugin's configuration
  113. /// </summary>
  114. /// <value>The configuration.</value>
  115. public TConfigurationType Configuration
  116. {
  117. get
  118. {
  119. // Lazy load
  120. if (_configuration == null)
  121. {
  122. lock (_configurationSyncLock)
  123. {
  124. if (_configuration == null)
  125. {
  126. _configuration = LoadConfiguration();
  127. }
  128. }
  129. }
  130. return _configuration;
  131. }
  132. protected set
  133. {
  134. _configuration = value;
  135. }
  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
  154. {
  155. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  156. }
  157. /// <summary>
  158. /// Gets the full path to the configuration file
  159. /// </summary>
  160. /// <value>The configuration file path.</value>
  161. public string ConfigurationFilePath
  162. {
  163. get
  164. {
  165. return Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  166. }
  167. }
  168. /// <summary>
  169. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  170. /// </summary>
  171. /// <value>The data folder path.</value>
  172. public string DataFolderPath
  173. {
  174. get
  175. {
  176. // Give the folder name the same name as the config file name
  177. // We can always make this configurable if/when needed
  178. return Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  179. }
  180. }
  181. /// <summary>
  182. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  183. /// </summary>
  184. /// <param name="applicationPaths">The application paths.</param>
  185. /// <param name="xmlSerializer">The XML serializer.</param>
  186. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  187. {
  188. ApplicationPaths = applicationPaths;
  189. XmlSerializer = xmlSerializer;
  190. }
  191. /// <summary>
  192. /// The _save lock
  193. /// </summary>
  194. private readonly object _configurationSaveLock = new object();
  195. /// <summary>
  196. /// Saves the current configuration to the file system
  197. /// </summary>
  198. public virtual void SaveConfiguration()
  199. {
  200. lock (_configurationSaveLock)
  201. {
  202. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  203. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  204. }
  205. }
  206. /// <summary>
  207. /// Completely overwrites the current configuration with a new copy
  208. /// Returns true or false indicating success or failure
  209. /// </summary>
  210. /// <param name="configuration">The configuration.</param>
  211. /// <exception cref="System.ArgumentNullException">configuration</exception>
  212. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  213. {
  214. if (configuration == null)
  215. {
  216. throw new ArgumentNullException("configuration");
  217. }
  218. Configuration = (TConfigurationType)configuration;
  219. SaveConfiguration();
  220. }
  221. /// <summary>
  222. /// Gets the plugin info.
  223. /// </summary>
  224. /// <returns>PluginInfo.</returns>
  225. public PluginInfo GetPluginInfo()
  226. {
  227. var info = new PluginInfo
  228. {
  229. Name = Name,
  230. Version = Version.ToString(),
  231. AssemblyFileName = AssemblyFileName,
  232. ConfigurationDateLastModified = ConfigurationDateLastModified,
  233. Description = Description,
  234. Id = Id.ToString(),
  235. ConfigurationFileName = ConfigurationFileName
  236. };
  237. return info;
  238. }
  239. /// <summary>
  240. /// Called when just before the plugin is uninstalled from the server.
  241. /// </summary>
  242. public virtual void OnUninstalling()
  243. {
  244. }
  245. /// <summary>
  246. /// Gets the plugin's configuration
  247. /// </summary>
  248. /// <value>The configuration.</value>
  249. BasePluginConfiguration IPlugin.Configuration
  250. {
  251. get { return Configuration; }
  252. }
  253. }
  254. public interface IPluginAssembly
  255. {
  256. void SetAttributes(string assemblyFilePath, string assemblyFileName, Version assemblyVersion);
  257. void SetId(Guid assemblyId);
  258. }
  259. }