PluginManager.cs 25 KB

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