BasePlugin.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Model.Plugins;
  3. using MediaBrowser.Model.Serialization;
  4. using System;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. namespace MediaBrowser.Common.Plugins
  10. {
  11. /// <summary>
  12. /// Provides a common base class for all plugins
  13. /// </summary>
  14. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  15. public abstract class BasePlugin<TConfigurationType> : IPlugin
  16. where TConfigurationType : BasePluginConfiguration
  17. {
  18. /// <summary>
  19. /// Gets the application paths.
  20. /// </summary>
  21. /// <value>The application paths.</value>
  22. protected IApplicationPaths ApplicationPaths { get; private set; }
  23. /// <summary>
  24. /// Gets the XML serializer.
  25. /// </summary>
  26. /// <value>The XML serializer.</value>
  27. protected IXmlSerializer XmlSerializer { get; private set; }
  28. /// <summary>
  29. /// Gets the name of the plugin
  30. /// </summary>
  31. /// <value>The name.</value>
  32. public abstract string Name { get; }
  33. /// <summary>
  34. /// Gets the description.
  35. /// </summary>
  36. /// <value>The description.</value>
  37. public virtual string Description
  38. {
  39. get { return string.Empty; }
  40. }
  41. /// <summary>
  42. /// Gets a value indicating whether this instance is a core plugin.
  43. /// </summary>
  44. /// <value><c>true</c> if this instance is a core plugin; otherwise, <c>false</c>.</value>
  45. public virtual bool IsCorePlugin
  46. {
  47. get
  48. {
  49. return false;
  50. }
  51. }
  52. /// <summary>
  53. /// Gets a value indicating whether this instance is first run.
  54. /// </summary>
  55. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  56. public bool IsFirstRun { get; private set; }
  57. /// <summary>
  58. /// Gets the type of configuration this plugin uses
  59. /// </summary>
  60. /// <value>The type of the configuration.</value>
  61. public Type ConfigurationType
  62. {
  63. get { return typeof(TConfigurationType); }
  64. }
  65. /// <summary>
  66. /// The _assembly name
  67. /// </summary>
  68. private AssemblyName _assemblyName;
  69. /// <summary>
  70. /// Gets the name of the assembly.
  71. /// </summary>
  72. /// <value>The name of the assembly.</value>
  73. protected AssemblyName AssemblyName
  74. {
  75. get
  76. {
  77. return _assemblyName ?? (_assemblyName = GetType().Assembly.GetName());
  78. }
  79. }
  80. /// <summary>
  81. /// The _unique id
  82. /// </summary>
  83. private Guid? _uniqueId;
  84. /// <summary>
  85. /// Gets the unique id.
  86. /// </summary>
  87. /// <value>The unique id.</value>
  88. public Guid Id
  89. {
  90. get
  91. {
  92. if (!_uniqueId.HasValue)
  93. {
  94. _uniqueId = Marshal.GetTypeLibGuidForAssembly(GetType().Assembly);
  95. }
  96. return _uniqueId.Value;
  97. }
  98. }
  99. /// <summary>
  100. /// Gets the plugin version
  101. /// </summary>
  102. /// <value>The version.</value>
  103. public Version Version
  104. {
  105. get
  106. {
  107. return AssemblyName.Version;
  108. }
  109. }
  110. /// <summary>
  111. /// Gets the name the assembly file
  112. /// </summary>
  113. /// <value>The name of the assembly file.</value>
  114. public string AssemblyFileName
  115. {
  116. get
  117. {
  118. return AssemblyName.Name + ".dll";
  119. }
  120. }
  121. /// <summary>
  122. /// Gets the last date modified of the configuration
  123. /// </summary>
  124. /// <value>The configuration date last modified.</value>
  125. public DateTime ConfigurationDateLastModified
  126. {
  127. get
  128. {
  129. // Ensure it's been lazy loaded
  130. var config = Configuration;
  131. return File.GetLastWriteTimeUtc(ConfigurationFilePath);
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the last date modified of the plugin
  136. /// </summary>
  137. /// <value>The assembly date last modified.</value>
  138. public DateTime AssemblyDateLastModified
  139. {
  140. get
  141. {
  142. return File.GetLastWriteTimeUtc(AssemblyFilePath);
  143. }
  144. }
  145. /// <summary>
  146. /// Gets the path to the assembly file
  147. /// </summary>
  148. /// <value>The assembly file path.</value>
  149. public string AssemblyFilePath
  150. {
  151. get
  152. {
  153. return Path.Combine(ApplicationPaths.PluginsPath, AssemblyFileName);
  154. }
  155. }
  156. /// <summary>
  157. /// The _configuration sync lock
  158. /// </summary>
  159. private object _configurationSyncLock = new object();
  160. /// <summary>
  161. /// The _configuration initialized
  162. /// </summary>
  163. private bool _configurationInitialized;
  164. /// <summary>
  165. /// The _configuration
  166. /// </summary>
  167. private TConfigurationType _configuration;
  168. /// <summary>
  169. /// Gets the plugin's configuration
  170. /// </summary>
  171. /// <value>The configuration.</value>
  172. public TConfigurationType Configuration
  173. {
  174. get
  175. {
  176. // Lazy load
  177. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => ConfigurationHelper.GetXmlConfiguration(ConfigurationType, ConfigurationFilePath, XmlSerializer) as TConfigurationType);
  178. return _configuration;
  179. }
  180. protected set
  181. {
  182. _configuration = value;
  183. if (value == null)
  184. {
  185. _configurationInitialized = false;
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// Gets the name of the configuration file. Subclasses should override
  191. /// </summary>
  192. /// <value>The name of the configuration file.</value>
  193. public virtual string ConfigurationFileName
  194. {
  195. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  196. }
  197. /// <summary>
  198. /// Gets the full path to the configuration file
  199. /// </summary>
  200. /// <value>The configuration file path.</value>
  201. public string ConfigurationFilePath
  202. {
  203. get
  204. {
  205. return Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  206. }
  207. }
  208. /// <summary>
  209. /// The _data folder path
  210. /// </summary>
  211. private string _dataFolderPath;
  212. /// <summary>
  213. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  214. /// </summary>
  215. /// <value>The data folder path.</value>
  216. public string DataFolderPath
  217. {
  218. get
  219. {
  220. if (_dataFolderPath == null)
  221. {
  222. // Give the folder name the same name as the config file name
  223. // We can always make this configurable if/when needed
  224. _dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  225. if (!Directory.Exists(_dataFolderPath))
  226. {
  227. Directory.CreateDirectory(_dataFolderPath);
  228. }
  229. }
  230. return _dataFolderPath;
  231. }
  232. }
  233. /// <summary>
  234. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  235. /// </summary>
  236. /// <param name="applicationPaths">The application paths.</param>
  237. /// <param name="xmlSerializer">The XML serializer.</param>
  238. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  239. {
  240. ApplicationPaths = applicationPaths;
  241. XmlSerializer = xmlSerializer;
  242. }
  243. /// <summary>
  244. /// The _save lock
  245. /// </summary>
  246. private readonly object _configurationSaveLock = new object();
  247. /// <summary>
  248. /// Saves the current configuration to the file system
  249. /// </summary>
  250. /// <exception cref="System.InvalidOperationException">Cannot call Plugin.SaveConfiguration from the UI.</exception>
  251. public virtual void SaveConfiguration()
  252. {
  253. lock (_configurationSaveLock)
  254. {
  255. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  256. }
  257. }
  258. /// <summary>
  259. /// Completely overwrites the current configuration with a new copy
  260. /// Returns true or false indicating success or failure
  261. /// </summary>
  262. /// <param name="configuration">The configuration.</param>
  263. /// <exception cref="System.ArgumentNullException">configuration</exception>
  264. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  265. {
  266. if (configuration == null)
  267. {
  268. throw new ArgumentNullException("configuration");
  269. }
  270. Configuration = (TConfigurationType)configuration;
  271. SaveConfiguration();
  272. }
  273. /// <summary>
  274. /// Gets the plugin info.
  275. /// </summary>
  276. /// <returns>PluginInfo.</returns>
  277. public PluginInfo GetPluginInfo()
  278. {
  279. var info = new PluginInfo
  280. {
  281. Name = Name,
  282. DownloadToUI = this is IUIPlugin,
  283. Version = Version.ToString(),
  284. AssemblyFileName = AssemblyFileName,
  285. ConfigurationDateLastModified = ConfigurationDateLastModified,
  286. Description = Description,
  287. IsCorePlugin = IsCorePlugin,
  288. Id = Id,
  289. EnableAutoUpdate = Configuration.EnableAutoUpdate,
  290. UpdateClass = Configuration.UpdateClass,
  291. ConfigurationFileName = ConfigurationFileName
  292. };
  293. var uiPlugin = this as IUIPlugin;
  294. if (uiPlugin != null)
  295. {
  296. info.MinimumRequiredUIVersion = uiPlugin.MinimumRequiredUIVersion.ToString();
  297. }
  298. return info;
  299. }
  300. /// <summary>
  301. /// Called when just before the plugin is uninstalled from the server.
  302. /// </summary>
  303. public virtual void OnUninstalling()
  304. {
  305. }
  306. /// <summary>
  307. /// Gets the plugin's configuration
  308. /// </summary>
  309. /// <value>The configuration.</value>
  310. BasePluginConfiguration IPlugin.Configuration
  311. {
  312. get { return Configuration; }
  313. }
  314. }
  315. }