Fix system plugins resource loading

This commit is contained in:
Kukks 2021-07-08 12:53:34 +02:00
parent 4bb3d60b6c
commit 060fc46e4f
3 changed files with 21 additions and 15 deletions

View File

@ -20,6 +20,7 @@ namespace BTCPayServer.Plugins.Test.Services
await using var context = _testPluginDbContextFactory.CreateContext();
await context.TestPluginRecords.AddAsync(new TestPluginData() {Timestamp = DateTimeOffset.UtcNow});
await context.SaveChangesAsync();
}

View File

@ -10,7 +10,7 @@
<div class="row">
<h2>Persisted Data</h2>
<p>The following is data persisted to the configured database but in an isolated DbContext. Every time you start BTCPayw with this plugin enabled, a timestamp is logged.</p>
<ul class="list-group">>
<ul class="list-group">
@foreach (var item in Model.Data)
{
<li class="list-group-item">@item.Id at @item.Timestamp.ToString("F")</li>

View File

@ -25,6 +25,7 @@ namespace BTCPayServer.Plugins
private static readonly List<PluginLoader> _plugins = new List<PluginLoader>();
private static ILogger _logger;
private static List<(PluginLoader, Assembly, IFileProvider)> loadedPlugins;
public static bool IsExceptionByPlugin(Exception exception)
{
return _pluginAssemblies.Any(assembly => assembly?.FullName?.Contains(exception.Source!, StringComparison.OrdinalIgnoreCase) is true);
@ -43,16 +44,24 @@ namespace BTCPayServer.Plugins
_logger.LogInformation($"Loading plugins from {pluginsFolder}");
Directory.CreateDirectory(pluginsFolder);
ExecuteCommands(pluginsFolder);
List<(PluginLoader, Assembly, IFileProvider)> loadedPlugins =
new List<(PluginLoader, Assembly, IFileProvider)>();
var systemExtensions = GetDefaultLoadedPluginAssemblies();
plugins.AddRange(systemExtensions.SelectMany(assembly =>
GetAllPluginTypesFromAssembly(assembly).Select(GetPluginInstanceFromType)));
foreach (IBTCPayServerPlugin btcPayServerExtension in plugins)
{
btcPayServerExtension.SystemPlugin = true;
}
loadedPlugins = new List<(PluginLoader, Assembly, IFileProvider)>();
var systemPlugins = GetDefaultLoadedPluginAssemblies();
foreach (Assembly systemExtension in systemPlugins)
{
var detectedPlugins = GetAllPluginTypesFromAssembly(systemExtension).Select(GetPluginInstanceFromType);
if (!detectedPlugins.Any())
{
continue;
}
foreach (var btcPayServerPlugin in detectedPlugins)
{
btcPayServerPlugin.SystemPlugin = true;
loadedPlugins.Add((null,systemExtension, CreateEmbeddedFileProviderForAssembly(systemExtension)));
}
plugins.AddRange(detectedPlugins);
}
var orderFilePath = Path.Combine(pluginsFolder, "order");
var availableDirs = Directory.GetDirectories(pluginsFolder);
@ -77,8 +86,6 @@ namespace BTCPayServer.Plugins
var disabledPlugins = GetDisabledPlugins(pluginsFolder);
foreach (var dir in orderedDirs)
{
var pluginName = Path.GetFileName(dir);
@ -154,9 +161,7 @@ namespace BTCPayServer.Plugins
var webHostEnvironment = applicationBuilder.ApplicationServices.GetService<IWebHostEnvironment>();
List<IFileProvider> providers = new List<IFileProvider>() {webHostEnvironment.WebRootFileProvider};
providers.AddRange(
_pluginAssemblies
.Select(CreateEmbeddedFileProviderForAssembly));
providers.AddRange(loadedPlugins.Select(tuple => tuple.Item3));
webHostEnvironment.WebRootFileProvider = new CompositeFileProvider(providers);
}