BasePlugin.cs 9.6 KB

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