PluginManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Text.Json;
  9. using MediaBrowser.Common;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Common.Json;
  12. using MediaBrowser.Common.Plugins;
  13. using MediaBrowser.Model.Configuration;
  14. using MediaBrowser.Model.Plugins;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Microsoft.Extensions.Logging;
  17. namespace Emby.Server.Implementations.Plugins
  18. {
  19. /// <summary>
  20. /// Defines the <see cref="PluginManager" />.
  21. /// </summary>
  22. public class PluginManager : IPluginManager
  23. {
  24. private readonly string _pluginsPath;
  25. private readonly Version _appVersion;
  26. private readonly JsonSerializerOptions _jsonOptions;
  27. private readonly ILogger<PluginManager> _logger;
  28. private readonly IApplicationHost _appHost;
  29. private readonly ServerConfiguration _config;
  30. private readonly IList<LocalPlugin> _plugins;
  31. private readonly Version _nextVersion;
  32. private readonly Version _minimumVersion;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="PluginManager"/> class.
  35. /// </summary>
  36. /// <param name="logger">The <see cref="ILogger"/>.</param>
  37. /// <param name="appHost">The <see cref="IApplicationHost"/>.</param>
  38. /// <param name="config">The <see cref="ServerConfiguration"/>.</param>
  39. /// <param name="pluginsPath">The plugin path.</param>
  40. /// <param name="imagesPath">The image cache path.</param>
  41. /// <param name="appVersion">The application version.</param>
  42. public PluginManager(
  43. ILogger<PluginManager> logger,
  44. IApplicationHost appHost,
  45. ServerConfiguration config,
  46. string pluginsPath,
  47. string imagesPath,
  48. Version appVersion)
  49. {
  50. _logger = _logger ?? throw new ArgumentNullException(nameof(logger));
  51. _pluginsPath = pluginsPath;
  52. _appVersion = appVersion ?? throw new ArgumentNullException(nameof(appVersion));
  53. _jsonOptions = JsonDefaults.GetOptions();
  54. _config = config;
  55. _appHost = appHost;
  56. _nextVersion = new Version(_appVersion.Major, _appVersion.Minor + 2, _appVersion.Build, _appVersion.Revision);
  57. _minimumVersion = new Version(0, 0, 0, 1);
  58. _plugins = Directory.Exists(_pluginsPath) ? DiscoverPlugins().ToList() : new List<LocalPlugin>();
  59. }
  60. /// <summary>
  61. /// Gets the Plugins.
  62. /// </summary>
  63. public IList<LocalPlugin> Plugins => _plugins;
  64. /// <summary>
  65. /// Returns all the assemblies.
  66. /// </summary>
  67. /// <returns>An IEnumerable{Assembly}.</returns>
  68. public IEnumerable<Assembly> LoadAssemblies()
  69. {
  70. // Attempt to remove any deleted plugins and change any successors to be active.
  71. for (int a = _plugins.Count - 1; a >= 0; a--)
  72. {
  73. var plugin = _plugins[a];
  74. if (plugin.Manifest.Status == PluginStatus.Deleted && DeletePlugin(plugin))
  75. {
  76. UpdateSuccessors(plugin);
  77. }
  78. }
  79. // Now load the assemblies..
  80. foreach (var plugin in _plugins)
  81. {
  82. CheckIfStillSuperceded(plugin);
  83. if (plugin.IsEnabledAndSupported == false)
  84. {
  85. _logger.LogInformation("Skipping disabled plugin {Version} of {Name} ", plugin.Version, plugin.Name);
  86. continue;
  87. }
  88. foreach (var file in plugin.DllFiles)
  89. {
  90. Assembly assembly;
  91. try
  92. {
  93. assembly = Assembly.LoadFrom(file);
  94. }
  95. catch (FileLoadException ex)
  96. {
  97. _logger.LogError(ex, "Failed to load assembly {Path}. Disabling plugin.", file);
  98. ChangePluginState(plugin, PluginStatus.Malfunctioned);
  99. continue;
  100. }
  101. _logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file);
  102. yield return assembly;
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// Creates all the plugin instances.
  108. /// </summary>
  109. public void CreatePlugins()
  110. {
  111. var createdPlugins = _appHost.GetExports<IPlugin>(CreatePluginInstance)
  112. .Where(i => i != null)
  113. .ToArray();
  114. }
  115. /// <summary>
  116. /// Registers the plugin's services with the DI.
  117. /// Note: DI is not yet instantiated yet.
  118. /// </summary>
  119. /// <param name="serviceCollection">A <see cref="ServiceCollection"/> instance.</param>
  120. public void RegisterServices(IServiceCollection serviceCollection)
  121. {
  122. foreach (var pluginServiceRegistrator in _appHost.GetExportTypes<IPluginServiceRegistrator>())
  123. {
  124. var plugin = GetPluginByType(pluginServiceRegistrator.Assembly.GetType());
  125. if (plugin == null)
  126. {
  127. _logger.LogError("Unable to find plugin in assembly {Assembly}", pluginServiceRegistrator.Assembly.FullName);
  128. continue;
  129. }
  130. CheckIfStillSuperceded(plugin);
  131. if (!plugin.IsEnabledAndSupported)
  132. {
  133. continue;
  134. }
  135. try
  136. {
  137. var instance = (IPluginServiceRegistrator?)Activator.CreateInstance(pluginServiceRegistrator);
  138. instance?.RegisterServices(serviceCollection);
  139. }
  140. #pragma warning disable CA1031 // Do not catch general exception types
  141. catch (Exception ex)
  142. #pragma warning restore CA1031 // Do not catch general exception types
  143. {
  144. _logger.LogError(ex, "Error registering plugin services from {Assembly}.", pluginServiceRegistrator.Assembly.FullName);
  145. if (ChangePluginState(plugin, PluginStatus.Malfunctioned))
  146. {
  147. _logger.LogInformation("Disabling plugin {Path}", plugin.Path);
  148. }
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Imports a plugin manifest from <paramref name="folder"/>.
  154. /// </summary>
  155. /// <param name="folder">Folder of the plugin.</param>
  156. public void ImportPluginFrom(string folder)
  157. {
  158. if (string.IsNullOrEmpty(folder))
  159. {
  160. throw new ArgumentNullException(nameof(folder));
  161. }
  162. // Load the plugin.
  163. var plugin = LoadManifest(folder);
  164. // Make sure we haven't already loaded this.
  165. if (plugin == null || _plugins.Any(p => p.Manifest.Equals(plugin.Manifest)))
  166. {
  167. return;
  168. }
  169. _plugins.Add(plugin);
  170. EnablePlugin(plugin);
  171. }
  172. /// <summary>
  173. /// Removes the plugin reference '<paramref name="plugin"/>.
  174. /// </summary>
  175. /// <param name="plugin">The plugin.</param>
  176. /// <returns>Outcome of the operation.</returns>
  177. public bool RemovePlugin(LocalPlugin plugin)
  178. {
  179. if (plugin == null)
  180. {
  181. throw new ArgumentNullException(nameof(plugin));
  182. }
  183. if (DeletePlugin(plugin))
  184. {
  185. return true;
  186. }
  187. _logger.LogWarning("Unable to delete {Path}, so marking as deleteOnStartup.", plugin.Path);
  188. // Unable to delete, so disable.
  189. return ChangePluginState(plugin, PluginStatus.Deleted);
  190. }
  191. /// <summary>
  192. /// Attempts to find the plugin with and id of <paramref name="id"/>.
  193. /// </summary>
  194. /// <param name="id">The <see cref="Guid"/> of plugin.</param>
  195. /// <param name="version">Optional <see cref="Version"/> of the plugin to locate.</param>
  196. /// <returns>A <see cref="LocalPlugin"/> if located, or null if not.</returns>
  197. public LocalPlugin? GetPlugin(Guid id, Version? version = null)
  198. {
  199. LocalPlugin? plugin;
  200. if (version == null)
  201. {
  202. // If no version is given, return the current instance.
  203. var plugins = _plugins.Where(p => p.Id.Equals(id));
  204. plugin = plugins.FirstOrDefault(p => p.Instance != null);
  205. if (plugin == null)
  206. {
  207. plugin = plugins.OrderByDescending(p => p.Version).FirstOrDefault();
  208. }
  209. }
  210. else
  211. {
  212. // Match id and version number.
  213. plugin = _plugins.FirstOrDefault(p => p.Id.Equals(id) && p.Version.Equals(version));
  214. }
  215. return plugin;
  216. }
  217. /// <summary>
  218. /// Enables the plugin, disabling all other versions.
  219. /// </summary>
  220. /// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
  221. public void EnablePlugin(LocalPlugin plugin)
  222. {
  223. if (plugin == null)
  224. {
  225. throw new ArgumentNullException(nameof(plugin));
  226. }
  227. if (ChangePluginState(plugin, PluginStatus.Active))
  228. {
  229. UpdateSuccessors(plugin);
  230. }
  231. }
  232. /// <summary>
  233. /// Disable the plugin.
  234. /// </summary>
  235. /// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
  236. public void DisablePlugin(LocalPlugin plugin)
  237. {
  238. if (plugin == null)
  239. {
  240. throw new ArgumentNullException(nameof(plugin));
  241. }
  242. // Update the manifest on disk
  243. if (ChangePluginState(plugin, PluginStatus.Disabled))
  244. {
  245. UpdateSuccessors(plugin);
  246. }
  247. }
  248. /// <summary>
  249. /// Disable the plugin.
  250. /// </summary>
  251. /// <param name="assembly">The <see cref="Assembly"/> of the plug to disable.</param>
  252. public void FailPlugin(Assembly assembly)
  253. {
  254. // Only save if disabled.
  255. if (assembly == null)
  256. {
  257. throw new ArgumentNullException(nameof(assembly));
  258. }
  259. var plugin = _plugins.Where(p => p.DllFiles.Contains(assembly.Location)).FirstOrDefault();
  260. if (plugin == null)
  261. {
  262. // A plugin's assembly didn't cause this issue, so ignore it.
  263. return;
  264. }
  265. ChangePluginState(plugin, PluginStatus.Malfunctioned);
  266. }
  267. /// <summary>
  268. /// Saves the manifest back to disk.
  269. /// </summary>
  270. /// <param name="manifest">The <see cref="PluginManifest"/> to save.</param>
  271. /// <param name="path">The path where to save the manifest.</param>
  272. /// <returns>True if successful.</returns>
  273. public bool SaveManifest(PluginManifest manifest, string path)
  274. {
  275. if (manifest == null)
  276. {
  277. return false;
  278. }
  279. try
  280. {
  281. var data = JsonSerializer.Serialize(manifest, _jsonOptions);
  282. File.WriteAllText(Path.Combine(path, "meta.json"), data, Encoding.UTF8);
  283. return true;
  284. }
  285. #pragma warning disable CA1031 // Do not catch general exception types
  286. catch (Exception e)
  287. #pragma warning restore CA1031 // Do not catch general exception types
  288. {
  289. _logger.LogWarning(e, "Unable to save plugin manifest. {Path}", path);
  290. return false;
  291. }
  292. }
  293. /// <summary>
  294. /// Changes a plugin's load status.
  295. /// </summary>
  296. /// <param name="plugin">The <see cref="LocalPlugin"/> instance.</param>
  297. /// <param name="state">The <see cref="PluginStatus"/> of the plugin.</param>
  298. /// <returns>Success of the task.</returns>
  299. private bool ChangePluginState(LocalPlugin plugin, PluginStatus state)
  300. {
  301. if (plugin.Manifest.Status == state || string.IsNullOrEmpty(plugin.Path))
  302. {
  303. // No need to save as the state hasn't changed.
  304. return true;
  305. }
  306. plugin.Manifest.Status = state;
  307. return SaveManifest(plugin.Manifest, plugin.Path);
  308. }
  309. /// <summary>
  310. /// Finds the plugin record using the type.
  311. /// </summary>
  312. /// <param name="type">The <see cref="Type"/> being sought.</param>
  313. /// <returns>The matching record, or null if not found.</returns>
  314. private LocalPlugin? GetPluginByType(Type type)
  315. {
  316. // Find which plugin it is by the path.
  317. return _plugins.FirstOrDefault(p => string.Equals(p.Path, Path.GetDirectoryName(type.Assembly.Location), StringComparison.Ordinal));
  318. }
  319. /// <summary>
  320. /// Creates the instance safe.
  321. /// </summary>
  322. /// <param name="type">The type.</param>
  323. /// <returns>System.Object.</returns>
  324. private IPlugin? CreatePluginInstance(Type type)
  325. {
  326. // Find the record for this plugin.
  327. var plugin = GetPluginByType(type);
  328. if (plugin?.Manifest.Status < PluginStatus.Active)
  329. {
  330. return null;
  331. }
  332. try
  333. {
  334. _logger.LogDebug("Creating instance of {Type}", type);
  335. var instance = (IPlugin)ActivatorUtilities.CreateInstance(_appHost.ServiceProvider, type);
  336. if (plugin == null)
  337. {
  338. // Create a dummy record for the providers.
  339. // TODO: remove this code, if all provided have been released as separate plugins.
  340. plugin = new LocalPlugin(
  341. instance.AssemblyFilePath,
  342. true,
  343. new PluginManifest
  344. {
  345. Guid = instance.Id,
  346. Status = PluginStatus.Active,
  347. Name = instance.Name,
  348. Version = instance.Version.ToString()
  349. })
  350. {
  351. Instance = instance
  352. };
  353. _plugins.Add(plugin);
  354. plugin.Manifest.Status = PluginStatus.Active;
  355. }
  356. else
  357. {
  358. plugin.Instance = instance;
  359. var manifest = plugin.Manifest;
  360. var pluginStr = plugin.Instance.Version.ToString();
  361. bool changed = false;
  362. if (string.Equals(manifest.Version, pluginStr, StringComparison.Ordinal))
  363. {
  364. // If a plugin without a manifest failed to load due to an external issue (eg config),
  365. // this updates the manifest to the actual plugin values.
  366. manifest.Version = pluginStr;
  367. manifest.Name = plugin.Instance.Name;
  368. manifest.Description = plugin.Instance.Description;
  369. changed = true;
  370. }
  371. changed = changed || manifest.Status != PluginStatus.Active;
  372. manifest.Status = PluginStatus.Active;
  373. if (changed)
  374. {
  375. SaveManifest(manifest, plugin.Path);
  376. }
  377. }
  378. _logger.LogInformation("Loaded plugin: {PluginName} {PluginVersion}", plugin.Name, plugin.Version);
  379. return instance;
  380. }
  381. #pragma warning disable CA1031 // Do not catch general exception types
  382. catch (Exception ex)
  383. #pragma warning restore CA1031 // Do not catch general exception types
  384. {
  385. _logger.LogError(ex, "Error creating {Type}", type.FullName);
  386. if (plugin != null)
  387. {
  388. if (ChangePluginState(plugin, PluginStatus.Malfunctioned))
  389. {
  390. _logger.LogInformation("Plugin {Path} has been disabled.", plugin.Path);
  391. return null;
  392. }
  393. }
  394. _logger.LogDebug("Unable to auto-disable.");
  395. return null;
  396. }
  397. }
  398. private void CheckIfStillSuperceded(LocalPlugin plugin)
  399. {
  400. if (plugin.Manifest.Status != PluginStatus.Superceded)
  401. {
  402. return;
  403. }
  404. var predecessor = _plugins.OrderByDescending(p => p.Version)
  405. .FirstOrDefault(p => p.Id.Equals(plugin.Id) && p.IsEnabledAndSupported && p.Version != plugin.Version);
  406. if (predecessor != null)
  407. {
  408. return;
  409. }
  410. plugin.Manifest.Status = PluginStatus.Active;
  411. }
  412. /// <summary>
  413. /// Attempts to delete a plugin.
  414. /// </summary>
  415. /// <param name="plugin">A <see cref="LocalPlugin"/> instance to delete.</param>
  416. /// <returns>True if successful.</returns>
  417. private bool DeletePlugin(LocalPlugin plugin)
  418. {
  419. // Attempt a cleanup of old folders.
  420. try
  421. {
  422. Directory.Delete(plugin.Path, true);
  423. _logger.LogDebug("Deleted {Path}", plugin.Path);
  424. _plugins.Remove(plugin);
  425. }
  426. #pragma warning disable CA1031 // Do not catch general exception types
  427. catch
  428. #pragma warning restore CA1031 // Do not catch general exception types
  429. {
  430. return false;
  431. }
  432. return _plugins.Remove(plugin);
  433. }
  434. private LocalPlugin? LoadManifest(string dir)
  435. {
  436. try
  437. {
  438. Version? version;
  439. PluginManifest? manifest = null;
  440. var metafile = Path.Combine(dir, "meta.json");
  441. if (File.Exists(metafile))
  442. {
  443. try
  444. {
  445. var data = File.ReadAllText(metafile, Encoding.UTF8);
  446. manifest = JsonSerializer.Deserialize<PluginManifest>(data, _jsonOptions);
  447. }
  448. #pragma warning disable CA1031 // Do not catch general exception types
  449. catch (Exception ex)
  450. #pragma warning restore CA1031 // Do not catch general exception types
  451. {
  452. _logger.LogError(ex, "Error deserializing {Path}.", dir);
  453. }
  454. }
  455. if (manifest != null)
  456. {
  457. if (!Version.TryParse(manifest.TargetAbi, out var targetAbi))
  458. {
  459. targetAbi = _minimumVersion;
  460. }
  461. if (!Version.TryParse(manifest.MaxAbi, out var maxAbi))
  462. {
  463. maxAbi = _appVersion;
  464. }
  465. if (!Version.TryParse(manifest.Version, out version))
  466. {
  467. manifest.Version = _minimumVersion.ToString();
  468. }
  469. return new LocalPlugin(dir, _appVersion >= targetAbi && _appVersion <= maxAbi, manifest);
  470. }
  471. // No metafile, so lets see if the folder is versioned.
  472. // TODO: Phase this support out in future versions.
  473. metafile = dir.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)[^1];
  474. int versionIndex = dir.LastIndexOf('_');
  475. if (versionIndex != -1)
  476. {
  477. // Get the version number from the filename if possible.
  478. metafile = Path.GetFileName(dir[..versionIndex]) ?? dir[..versionIndex];
  479. version = Version.TryParse(dir.AsSpan()[(versionIndex + 1)..], out Version? parsedVersion) ? parsedVersion : _appVersion;
  480. }
  481. else
  482. {
  483. // Un-versioned folder - Add it under the path name and version it suitable for this instance.
  484. version = _appVersion;
  485. }
  486. // Auto-create a plugin manifest, so we can disable it, if it fails to load.
  487. manifest = new PluginManifest
  488. {
  489. Status = PluginStatus.Restart,
  490. Name = metafile,
  491. AutoUpdate = false,
  492. Guid = metafile.GetMD5(),
  493. TargetAbi = _appVersion.ToString(),
  494. Version = version.ToString()
  495. };
  496. return new LocalPlugin(dir, true, manifest);
  497. }
  498. #pragma warning disable CA1031 // Do not catch general exception types
  499. catch (Exception ex)
  500. #pragma warning restore CA1031 // Do not catch general exception types
  501. {
  502. _logger.LogError(ex, "Something went wrong!");
  503. return null;
  504. }
  505. }
  506. /// <summary>
  507. /// Gets the list of local plugins.
  508. /// </summary>
  509. /// <returns>Enumerable of local plugins.</returns>
  510. private IEnumerable<LocalPlugin> DiscoverPlugins()
  511. {
  512. var versions = new List<LocalPlugin>();
  513. if (!Directory.Exists(_pluginsPath))
  514. {
  515. // Plugin path doesn't exist, don't try to enumerate sub-folders.
  516. return Enumerable.Empty<LocalPlugin>();
  517. }
  518. var directories = Directory.EnumerateDirectories(_pluginsPath, "*.*", SearchOption.TopDirectoryOnly);
  519. LocalPlugin? entry;
  520. foreach (var dir in directories)
  521. {
  522. entry = LoadManifest(dir);
  523. if (entry != null)
  524. {
  525. versions.Add(entry);
  526. }
  527. }
  528. string lastName = string.Empty;
  529. versions.Sort(LocalPlugin.Compare);
  530. // Traverse backwards through the list.
  531. // The first item will be the latest version.
  532. for (int x = versions.Count - 1; x >= 0; x--)
  533. {
  534. entry = versions[x];
  535. if (!string.Equals(lastName, entry.Name, StringComparison.OrdinalIgnoreCase))
  536. {
  537. entry.DllFiles.AddRange(Directory.EnumerateFiles(entry.Path, "*.dll", SearchOption.AllDirectories));
  538. if (entry.IsEnabledAndSupported)
  539. {
  540. lastName = entry.Name;
  541. continue;
  542. }
  543. }
  544. if (string.IsNullOrEmpty(lastName))
  545. {
  546. continue;
  547. }
  548. var manifest = entry.Manifest;
  549. var cleaned = false;
  550. var path = entry.Path;
  551. if (_config.RemoveOldPlugins)
  552. {
  553. // Attempt a cleanup of old folders.
  554. try
  555. {
  556. _logger.LogDebug("Deleting {Path}", path);
  557. Directory.Delete(path, true);
  558. cleaned = true;
  559. }
  560. #pragma warning disable CA1031 // Do not catch general exception types
  561. catch (Exception e)
  562. #pragma warning restore CA1031 // Do not catch general exception types
  563. {
  564. _logger.LogWarning(e, "Unable to delete {Path}", path);
  565. }
  566. if (cleaned)
  567. {
  568. versions.RemoveAt(x);
  569. }
  570. else
  571. {
  572. if (manifest == null)
  573. {
  574. _logger.LogWarning("Unable to disable plugin {Path}", entry.Path);
  575. continue;
  576. }
  577. if (manifest.Status != PluginStatus.Deleted)
  578. {
  579. manifest.Status = PluginStatus.Deleted;
  580. SaveManifest(manifest, entry.Path);
  581. }
  582. }
  583. }
  584. }
  585. // Only want plugin folders which have files.
  586. return versions.Where(p => p.DllFiles.Count != 0);
  587. }
  588. /// <summary>
  589. /// Changes the status of the other versions of the plugin to "Superceded".
  590. /// </summary>
  591. /// <param name="plugin">The <see cref="LocalPlugin"/> that's master.</param>
  592. private void UpdateSuccessors(LocalPlugin plugin)
  593. {
  594. // This value is memory only - so that the web will show restart required.
  595. plugin.Manifest.Status = PluginStatus.Restart;
  596. // Detect whether there is another version of this plugin that needs disabling.
  597. var predecessor = _plugins.OrderByDescending(p => p.Version)
  598. .FirstOrDefault(
  599. p => p.Id.Equals(plugin.Id)
  600. && p.IsEnabledAndSupported
  601. && p.Version != plugin.Version);
  602. if (predecessor == null)
  603. {
  604. return;
  605. }
  606. if (predecessor.Manifest.Status == PluginStatus.Active && !ChangePluginState(predecessor, PluginStatus.Superceded))
  607. {
  608. _logger.LogError("Unable to disable version {Version} of {Name}", predecessor.Version, predecessor.Name);
  609. }
  610. }
  611. }
  612. }