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