BasePlugin.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, Guid assemblyId)
  52. {
  53. AssemblyFilePath = assemblyFilePath;
  54. AssemblyFileName = assemblyFileName;
  55. Version = assemblyVersion;
  56. Id = assemblyId;
  57. }
  58. private Func<string, DateTime> _dateModifiedFn;
  59. private Action<string> _directoryCreateFn;
  60. public void SetStartupInfo(bool isFirstRun, Func<string, DateTime> dateModifiedFn, Action<string> directoryCreateFn)
  61. {
  62. IsFirstRun = isFirstRun;
  63. // hack alert, until the .net core transition is complete
  64. _dateModifiedFn = dateModifiedFn;
  65. _directoryCreateFn = directoryCreateFn;
  66. }
  67. /// <summary>
  68. /// Gets the unique id.
  69. /// </summary>
  70. /// <value>The unique id.</value>
  71. public Guid Id { get; private set; }
  72. /// <summary>
  73. /// Gets the plugin version
  74. /// </summary>
  75. /// <value>The version.</value>
  76. public Version Version { get; private set; }
  77. /// <summary>
  78. /// Gets the name the assembly file
  79. /// </summary>
  80. /// <value>The name of the assembly file.</value>
  81. protected string AssemblyFileName { get; private set; }
  82. /// <summary>
  83. /// Gets the last date modified of the configuration
  84. /// </summary>
  85. /// <value>The configuration date last modified.</value>
  86. public DateTime ConfigurationDateLastModified
  87. {
  88. get
  89. {
  90. // Ensure it's been lazy loaded
  91. var config = Configuration;
  92. return _dateModifiedFn(ConfigurationFilePath);
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the path to the assembly file
  97. /// </summary>
  98. /// <value>The assembly file path.</value>
  99. public string AssemblyFilePath { get; private set; }
  100. /// <summary>
  101. /// The _configuration sync lock
  102. /// </summary>
  103. private readonly object _configurationSyncLock = new object();
  104. /// <summary>
  105. /// The _configuration
  106. /// </summary>
  107. private TConfigurationType _configuration;
  108. /// <summary>
  109. /// Gets the plugin's configuration
  110. /// </summary>
  111. /// <value>The configuration.</value>
  112. public TConfigurationType Configuration
  113. {
  114. get
  115. {
  116. // Lazy load
  117. if (_configuration == null)
  118. {
  119. lock (_configurationSyncLock)
  120. {
  121. if (_configuration == null)
  122. {
  123. _configuration = LoadConfiguration();
  124. }
  125. }
  126. }
  127. return _configuration;
  128. }
  129. protected set
  130. {
  131. _configuration = value;
  132. }
  133. }
  134. private TConfigurationType LoadConfiguration()
  135. {
  136. var path = ConfigurationFilePath;
  137. try
  138. {
  139. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  140. }
  141. catch
  142. {
  143. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  144. }
  145. }
  146. /// <summary>
  147. /// Gets the name of the configuration file. Subclasses should override
  148. /// </summary>
  149. /// <value>The name of the configuration file.</value>
  150. public virtual string ConfigurationFileName
  151. {
  152. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  153. }
  154. /// <summary>
  155. /// Gets the full path to the configuration file
  156. /// </summary>
  157. /// <value>The configuration file path.</value>
  158. public string ConfigurationFilePath
  159. {
  160. get
  161. {
  162. return Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  167. /// </summary>
  168. /// <value>The data folder path.</value>
  169. public string DataFolderPath
  170. {
  171. get
  172. {
  173. // Give the folder name the same name as the config file name
  174. // We can always make this configurable if/when needed
  175. return Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  176. }
  177. }
  178. /// <summary>
  179. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  180. /// </summary>
  181. /// <param name="applicationPaths">The application paths.</param>
  182. /// <param name="xmlSerializer">The XML serializer.</param>
  183. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  184. {
  185. ApplicationPaths = applicationPaths;
  186. XmlSerializer = xmlSerializer;
  187. }
  188. /// <summary>
  189. /// The _save lock
  190. /// </summary>
  191. private readonly object _configurationSaveLock = new object();
  192. /// <summary>
  193. /// Saves the current configuration to the file system
  194. /// </summary>
  195. public virtual void SaveConfiguration()
  196. {
  197. lock (_configurationSaveLock)
  198. {
  199. _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
  200. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  201. }
  202. }
  203. /// <summary>
  204. /// Completely overwrites the current configuration with a new copy
  205. /// Returns true or false indicating success or failure
  206. /// </summary>
  207. /// <param name="configuration">The configuration.</param>
  208. /// <exception cref="System.ArgumentNullException">configuration</exception>
  209. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  210. {
  211. if (configuration == null)
  212. {
  213. throw new ArgumentNullException("configuration");
  214. }
  215. Configuration = (TConfigurationType)configuration;
  216. SaveConfiguration();
  217. }
  218. /// <summary>
  219. /// Gets the plugin info.
  220. /// </summary>
  221. /// <returns>PluginInfo.</returns>
  222. public PluginInfo GetPluginInfo()
  223. {
  224. var info = new PluginInfo
  225. {
  226. Name = Name,
  227. Version = Version.ToString(),
  228. AssemblyFileName = AssemblyFileName,
  229. ConfigurationDateLastModified = ConfigurationDateLastModified,
  230. Description = Description,
  231. Id = Id.ToString(),
  232. ConfigurationFileName = ConfigurationFileName
  233. };
  234. return info;
  235. }
  236. /// <summary>
  237. /// Called when just before the plugin is uninstalled from the server.
  238. /// </summary>
  239. public virtual void OnUninstalling()
  240. {
  241. }
  242. /// <summary>
  243. /// Gets the plugin's configuration
  244. /// </summary>
  245. /// <value>The configuration.</value>
  246. BasePluginConfiguration IPlugin.Configuration
  247. {
  248. get { return Configuration; }
  249. }
  250. }
  251. public interface IPluginAssembly
  252. {
  253. void SetAttributes(string assemblyFilePath, string assemblyFileName, Version assemblyVersion, Guid assemblyId);
  254. }
  255. }