BasePlugin.cs 10 KB

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