2
0

BasePluginOfT.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma warning disable SA1649 // File name should match first type name
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Model.Plugins;
  7. using MediaBrowser.Model.Serialization;
  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> : BasePlugin, IHasPluginConfiguration
  15. where TConfigurationType : BasePluginConfiguration
  16. {
  17. /// <summary>
  18. /// The configuration sync lock.
  19. /// </summary>
  20. private readonly object _configurationSyncLock = new object();
  21. /// <summary>
  22. /// The configuration save lock.
  23. /// </summary>
  24. private readonly object _configurationSaveLock = new object();
  25. /// <summary>
  26. /// The configuration.
  27. /// </summary>
  28. private TConfigurationType _configuration;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
  31. /// </summary>
  32. /// <param name="applicationPaths">The application paths.</param>
  33. /// <param name="xmlSerializer">The XML serializer.</param>
  34. protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
  35. {
  36. ApplicationPaths = applicationPaths;
  37. XmlSerializer = xmlSerializer;
  38. var assembly = GetType().Assembly;
  39. var assemblyName = assembly.GetName();
  40. var assemblyFilePath = assembly.Location;
  41. var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
  42. if (!Directory.Exists(dataFolderPath) && Version != null)
  43. {
  44. // Try again with the version number appended to the folder name.
  45. dataFolderPath = dataFolderPath + "_" + Version.ToString();
  46. }
  47. SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
  48. var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
  49. if (idAttributes.Length > 0)
  50. {
  51. var attribute = (GuidAttribute)idAttributes[0];
  52. var assemblyId = new Guid(attribute.Value);
  53. SetId(assemblyId);
  54. }
  55. }
  56. /// <summary>
  57. /// Gets the application paths.
  58. /// </summary>
  59. /// <value>The application paths.</value>
  60. protected IApplicationPaths ApplicationPaths { get; private set; }
  61. /// <summary>
  62. /// Gets the XML serializer.
  63. /// </summary>
  64. /// <value>The XML serializer.</value>
  65. protected IXmlSerializer XmlSerializer { get; private set; }
  66. /// <summary>
  67. /// Gets the type of configuration this plugin uses.
  68. /// </summary>
  69. /// <value>The type of the configuration.</value>
  70. public Type ConfigurationType => typeof(TConfigurationType);
  71. /// <summary>
  72. /// Gets or sets the event handler that is triggered when this configuration changes.
  73. /// </summary>
  74. public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
  75. /// <summary>
  76. /// Gets the name the assembly file.
  77. /// </summary>
  78. /// <value>The name of the assembly file.</value>
  79. protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
  80. /// <summary>
  81. /// Gets or sets the plugin configuration.
  82. /// </summary>
  83. /// <value>The configuration.</value>
  84. public TConfigurationType Configuration
  85. {
  86. get
  87. {
  88. // Lazy load
  89. if (_configuration == null)
  90. {
  91. lock (_configurationSyncLock)
  92. {
  93. if (_configuration == null)
  94. {
  95. _configuration = LoadConfiguration();
  96. }
  97. }
  98. }
  99. return _configuration;
  100. }
  101. protected set => _configuration = value;
  102. }
  103. /// <summary>
  104. /// Gets the name of the configuration file. Subclasses should override.
  105. /// </summary>
  106. /// <value>The name of the configuration file.</value>
  107. public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
  108. /// <summary>
  109. /// Gets the full path to the configuration file.
  110. /// </summary>
  111. /// <value>The configuration file path.</value>
  112. public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
  113. /// <summary>
  114. /// Gets the plugin configuration.
  115. /// </summary>
  116. /// <value>The configuration.</value>
  117. BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
  118. /// <summary>
  119. /// Saves the current configuration to the file system.
  120. /// </summary>
  121. /// <param name="config">Configuration to save.</param>
  122. public virtual void SaveConfiguration(TConfigurationType config)
  123. {
  124. lock (_configurationSaveLock)
  125. {
  126. var folder = Path.GetDirectoryName(ConfigurationFilePath);
  127. if (!Directory.Exists(folder))
  128. {
  129. Directory.CreateDirectory(folder);
  130. }
  131. XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
  132. }
  133. }
  134. /// <summary>
  135. /// Saves the current configuration to the file system.
  136. /// </summary>
  137. public virtual void SaveConfiguration()
  138. {
  139. SaveConfiguration(Configuration);
  140. }
  141. /// <inheritdoc />
  142. public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
  143. {
  144. if (configuration == null)
  145. {
  146. throw new ArgumentNullException(nameof(configuration));
  147. }
  148. Configuration = (TConfigurationType)configuration;
  149. SaveConfiguration(Configuration);
  150. ConfigurationChanged?.Invoke(this, configuration);
  151. }
  152. /// <inheritdoc />
  153. public override PluginInfo GetPluginInfo()
  154. {
  155. var info = base.GetPluginInfo();
  156. info.ConfigurationFileName = ConfigurationFileName;
  157. return info;
  158. }
  159. private TConfigurationType LoadConfiguration()
  160. {
  161. var path = ConfigurationFilePath;
  162. try
  163. {
  164. return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
  165. }
  166. catch
  167. {
  168. var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
  169. SaveConfiguration(config);
  170. return config;
  171. }
  172. }
  173. }
  174. }