BasePlugin.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. namespace MediaBrowser.Common.Plugins
  9. {
  10. /// <summary>
  11. /// Provides a common base class for all plugins
  12. /// </summary>
  13. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  14. public abstract class BasePlugin<TConfigurationType> : IPlugin
  15. where TConfigurationType : BasePluginConfiguration
  16. {
  17. /// <summary>
  18. /// Gets the application paths.
  19. /// </summary>
  20. /// <value>The application paths.</value>
  21. protected IApplicationPaths ApplicationPaths { get; private set; }
  22. /// <summary>
  23. /// Gets the XML serializer.
  24. /// </summary>
  25. /// <value>The XML serializer.</value>
  26. protected IXmlSerializer XmlSerializer { get; private set; }
  27. /// <summary>
  28. /// Gets the name of the plugin
  29. /// </summary>
  30. /// <value>The name.</value>
  31. public abstract string Name { get; }
  32. /// <summary>
  33. /// Gets a value indicating whether this instance is first run.
  34. /// </summary>
  35. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  36. public bool IsFirstRun { get; private set; }
  37. /// <summary>
  38. /// Gets the description.
  39. /// </summary>
  40. /// <value>The description.</value>
  41. public virtual string Description
  42. {
  43. get { return string.Empty; }
  44. }
  45. /// <summary>
  46. /// Gets the type of configuration this plugin uses
  47. /// </summary>
  48. /// <value>The type of the configuration.</value>
  49. public Type ConfigurationType
  50. {
  51. get { return typeof(TConfigurationType); }
  52. }
  53. /// <summary>
  54. /// The _assembly name
  55. /// </summary>
  56. private AssemblyName _assemblyName;
  57. /// <summary>
  58. /// Gets the name of the assembly.
  59. /// </summary>
  60. /// <value>The name of the assembly.</value>
  61. protected AssemblyName AssemblyName
  62. {
  63. get
  64. {
  65. return _assemblyName ?? (_assemblyName = GetType().Assembly.GetName());
  66. }
  67. }
  68. /// <summary>
  69. /// The _unique id
  70. /// </summary>
  71. private Guid? _uniqueId;
  72. /// <summary>
  73. /// Gets the unique id.
  74. /// </summary>
  75. /// <value>The unique id.</value>
  76. public Guid Id
  77. {
  78. get
  79. {
  80. if (!_uniqueId.HasValue)
  81. {
  82. var attribute = (GuidAttribute)GetType().Assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
  83. _uniqueId = new Guid(attribute.Value);
  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 readonly object _configurationSyncLock = new object();
  149. /// <summary>
  150. /// The _configuration
  151. /// </summary>
  152. private TConfigurationType _configuration;
  153. /// <summary>
  154. /// Gets the plugin's configuration
  155. /// </summary>
  156. /// <value>The configuration.</value>
  157. public TConfigurationType Configuration
  158. {
  159. get
  160. {
  161. // Lazy load
  162. if (_configuration == null)
  163. {
  164. lock (_configurationSyncLock)
  165. {
  166. if (_configuration == null)
  167. {
  168. _configuration = LoadConfiguration();
  169. }
  170. }
  171. }
  172. return _configuration;
  173. }
  174. protected set
  175. {
  176. _configuration = value;
  177. }
  178. }
  179. private TConfigurationType LoadConfiguration()
  180. {
  181. var path = ConfigurationFilePath;
  182. try
  183. {
  184. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  185. }
  186. catch (DirectoryNotFoundException)
  187. {
  188. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  189. }
  190. catch (FileNotFoundException)
  191. {
  192. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  193. }
  194. catch (Exception ex)
  195. {
  196. return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  197. }
  198. }
  199. /// <summary>
  200. /// Gets the name of the configuration file. Subclasses should override
  201. /// </summary>
  202. /// <value>The name of the configuration file.</value>
  203. public virtual string ConfigurationFileName
  204. {
  205. get { return Path.ChangeExtension(AssemblyFileName, ".xml"); }
  206. }
  207. /// <summary>
  208. /// Gets the full path to the configuration file
  209. /// </summary>
  210. /// <value>The configuration file path.</value>
  211. public string ConfigurationFilePath
  212. {
  213. get
  214. {
  215. return Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  216. }
  217. }
  218. /// <summary>
  219. /// The _data folder path
  220. /// </summary>
  221. private string _dataFolderPath;
  222. /// <summary>
  223. /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
  224. /// </summary>
  225. /// <value>The data folder path.</value>
  226. public string DataFolderPath
  227. {
  228. get
  229. {
  230. if (_dataFolderPath == null)
  231. {
  232. // Give the folder name the same name as the config file name
  233. // We can always make this configurable if/when needed
  234. _dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
  235. Directory.CreateDirectory(_dataFolderPath);
  236. }
  237. return _dataFolderPath;
  238. }
  239. }
  240. /// <summary>
  241. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  242. /// </summary>
  243. /// <param name="applicationPaths">The application paths.</param>
  244. /// <param name="xmlSerializer">The XML serializer.</param>
  245. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  246. {
  247. ApplicationPaths = applicationPaths;
  248. XmlSerializer = xmlSerializer;
  249. IsFirstRun = !File.Exists(ConfigurationFilePath);
  250. }
  251. /// <summary>
  252. /// The _save lock
  253. /// </summary>
  254. private readonly object _configurationSaveLock = new object();
  255. /// <summary>
  256. /// Saves the current configuration to the file system
  257. /// </summary>
  258. public virtual void SaveConfiguration()
  259. {
  260. lock (_configurationSaveLock)
  261. {
  262. Directory.CreateDirectory(Path.GetDirectoryName(ConfigurationFilePath));
  263. XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
  264. }
  265. }
  266. /// <summary>
  267. /// Completely overwrites the current configuration with a new copy
  268. /// Returns true or false indicating success or failure
  269. /// </summary>
  270. /// <param name="configuration">The configuration.</param>
  271. /// <exception cref="System.ArgumentNullException">configuration</exception>
  272. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  273. {
  274. if (configuration == null)
  275. {
  276. throw new ArgumentNullException("configuration");
  277. }
  278. Configuration = (TConfigurationType)configuration;
  279. SaveConfiguration();
  280. }
  281. /// <summary>
  282. /// Gets the plugin info.
  283. /// </summary>
  284. /// <returns>PluginInfo.</returns>
  285. public PluginInfo GetPluginInfo()
  286. {
  287. var info = new PluginInfo
  288. {
  289. Name = Name,
  290. Version = Version.ToString(),
  291. AssemblyFileName = AssemblyFileName,
  292. ConfigurationDateLastModified = ConfigurationDateLastModified,
  293. Description = Description,
  294. Id = Id.ToString(),
  295. ConfigurationFileName = ConfigurationFileName
  296. };
  297. return info;
  298. }
  299. /// <summary>
  300. /// Called when just before the plugin is uninstalled from the server.
  301. /// </summary>
  302. public virtual void OnUninstalling()
  303. {
  304. }
  305. /// <summary>
  306. /// Gets the plugin's configuration
  307. /// </summary>
  308. /// <value>The configuration.</value>
  309. BasePluginConfiguration IPlugin.Configuration
  310. {
  311. get { return Configuration; }
  312. }
  313. }
  314. }