Simplified plugin service and loading
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
using System.Runtime.Loader;
|
||||
using MoonCore.Plugins;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.Client.Implementations;
|
||||
|
||||
public class RemotePluginSource : IPluginSource
|
||||
{
|
||||
private readonly FrontendConfiguration Configuration;
|
||||
private readonly ILogger<RemotePluginSource> Logger;
|
||||
private readonly HttpClient HttpClient;
|
||||
|
||||
public RemotePluginSource(
|
||||
FrontendConfiguration configuration,
|
||||
ILogger<RemotePluginSource> logger,
|
||||
HttpClient httpClient
|
||||
)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
HttpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task Load(AssemblyLoadContext loadContext, List<string> entrypoints)
|
||||
{
|
||||
entrypoints.AddRange(Configuration.Plugins.Entrypoints);
|
||||
|
||||
foreach (var assembly in Configuration.Plugins.Assemblies)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileStream = await HttpClient.GetStreamAsync($"plugins/{assembly}");
|
||||
loadContext.LoadFromStream(fileStream);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to load plugin assembly '{assembly}': {e}", assembly, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,5 @@ namespace Moonlight.Client.Services;
|
||||
|
||||
public class ApplicationAssemblyService
|
||||
{
|
||||
public Assembly[] AdditionalAssemblies { get; set; }
|
||||
public Assembly[] PluginAssemblies { get; set; }
|
||||
public Assembly[] NavigationAssemblies => PluginAssemblies.Concat(AdditionalAssemblies).ToArray();
|
||||
public List<Assembly> Assemblies { get; set; } = new();
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
@@ -34,20 +35,15 @@ public class Startup
|
||||
private WebAssemblyHost WebAssemblyHost;
|
||||
|
||||
// Plugin Loading
|
||||
private PluginLoaderService PluginLoaderService;
|
||||
private ApplicationAssemblyService ApplicationAssemblyService;
|
||||
private AssemblyLoadContext PluginLoadContext;
|
||||
private Assembly[] AdditionalAssemblies;
|
||||
|
||||
private IPluginStartup[] PluginStartups;
|
||||
|
||||
public async Task Run(string[] args, Assembly[]? assemblies = null)
|
||||
public async Task Run(string[] args, Assembly[]? additionalAssemblies = null)
|
||||
{
|
||||
Args = args;
|
||||
|
||||
// Setup assembly storage
|
||||
ApplicationAssemblyService = new()
|
||||
{
|
||||
AdditionalAssemblies = assemblies ?? []
|
||||
};
|
||||
AdditionalAssemblies = additionalAssemblies ?? [];
|
||||
|
||||
await PrintVersion();
|
||||
await SetupLogging();
|
||||
@@ -174,11 +170,6 @@ public class Startup
|
||||
|
||||
private async Task LoadPlugins()
|
||||
{
|
||||
// Initialize api server plugin loader
|
||||
PluginLoaderService = new PluginLoaderService(
|
||||
LoggerFactory.CreateLogger<PluginLoaderService>()
|
||||
);
|
||||
|
||||
// Create everything required to stream plugins
|
||||
using var clientForStreaming = new HttpClient();
|
||||
|
||||
@@ -187,19 +178,21 @@ public class Startup
|
||||
: WebAssemblyHostBuilder.HostEnvironment.BaseAddress
|
||||
);
|
||||
|
||||
PluginLoaderService.AddSource(new RemotePluginSource(
|
||||
Configuration,
|
||||
LoggerFactory.CreateLogger<RemotePluginSource>(),
|
||||
clientForStreaming
|
||||
));
|
||||
PluginLoadContext = new AssemblyLoadContext(null);
|
||||
|
||||
// Perform assembly loading
|
||||
await PluginLoaderService.Load();
|
||||
foreach (var assembly in Configuration.Assemblies)
|
||||
{
|
||||
var assemblyStream = await clientForStreaming.GetStreamAsync($"plugins/{assembly}");
|
||||
PluginLoadContext.LoadFromStream(assemblyStream);
|
||||
}
|
||||
|
||||
// Add application assembly service
|
||||
var appAssemblyService = new ApplicationAssemblyService();
|
||||
|
||||
appAssemblyService.Assemblies.AddRange(AdditionalAssemblies);
|
||||
appAssemblyService.Assemblies.AddRange(PluginLoadContext.Assemblies);
|
||||
|
||||
// Add plugin loader service to di for the Router/App.razor
|
||||
ApplicationAssemblyService.PluginAssemblies = PluginLoaderService.PluginAssemblies;
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddSingleton(ApplicationAssemblyService);
|
||||
WebAssemblyHostBuilder.Services.AddSingleton(appAssemblyService);
|
||||
}
|
||||
|
||||
private Task InitializePlugins()
|
||||
@@ -224,8 +217,8 @@ public class Startup
|
||||
var assembliesToScan = new List<Assembly>();
|
||||
|
||||
assembliesToScan.Add(typeof(Startup).Assembly);
|
||||
assembliesToScan.AddRange(PluginLoaderService.PluginAssemblies);
|
||||
assembliesToScan.AddRange(ApplicationAssemblyService.AdditionalAssemblies);
|
||||
assembliesToScan.AddRange(AdditionalAssemblies);
|
||||
assembliesToScan.AddRange(PluginLoadContext.Assemblies);
|
||||
|
||||
foreach (var pluginAssembly in assembliesToScan)
|
||||
{
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
|
||||
<ApplicationRouter DefaultLayout="@typeof(MainLayout)"
|
||||
AppAssembly="@typeof(App).Assembly"
|
||||
AdditionalAssemblies="ApplicationAssemblyService.NavigationAssemblies" />
|
||||
AdditionalAssemblies="ApplicationAssemblyService.Assemblies" />
|
||||
@@ -71,7 +71,7 @@
|
||||
<div class="hidden lg:fixed lg:inset-y-0 lg:z-50 lg:flex lg:w-72 lg:flex-col">
|
||||
<div class="flex grow flex-col gap-y-5 overflow-y-auto bg-gray-800/60 px-6 pb-4">
|
||||
<div class="flex h-16 shrink-0 items-center">
|
||||
<img class="h-8 w-auto" src="https://gamecp.masuowo.xyz/api/core/asset/Core/svg/logo.svg"
|
||||
<img class="h-8 w-auto" src="/svg/logo.svg"
|
||||
alt="Your Company">
|
||||
</div>
|
||||
<nav class="flex flex-1 flex-col">
|
||||
|
||||
Reference in New Issue
Block a user