PluginManager.cs 25 KB

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