BasePlugin.cs 10 KB

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