From 094af845a0b4068d7bc123b46937e24e7f2dca04 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Fri, 13 Dec 2024 18:46:10 +0100 Subject: [PATCH] Added app startup for the frontend --- Moonlight.Client/Interfaces/IAppStartup.cs | 9 +++ Moonlight.Client/Startup.cs | 71 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Moonlight.Client/Interfaces/IAppStartup.cs diff --git a/Moonlight.Client/Interfaces/IAppStartup.cs b/Moonlight.Client/Interfaces/IAppStartup.cs new file mode 100644 index 00000000..b8fcb443 --- /dev/null +++ b/Moonlight.Client/Interfaces/IAppStartup.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace Moonlight.Client.Interfaces; + +public interface IAppStartup +{ + public Task BuildApp(WebAssemblyHostBuilder builder); + public Task ConfigureApp(WebAssemblyHost app); +} \ No newline at end of file diff --git a/Moonlight.Client/Startup.cs b/Moonlight.Client/Startup.cs index 13a403b1..dcc2f12c 100644 --- a/Moonlight.Client/Startup.cs +++ b/Moonlight.Client/Startup.cs @@ -36,6 +36,8 @@ public class Startup // Plugin Loading private PluginLoaderService PluginLoaderService; private ApplicationAssemblyService ApplicationAssemblyService; + + private IAppStartup[] PluginAppStartups; public async Task Run(string[] args, Assembly[]? assemblies = null) { @@ -53,15 +55,18 @@ public class Startup await CreateWebAssemblyHostBuilder(); await LoadPlugins(); + await InitializePlugins(); await RegisterLogging(); await RegisterBase(); await RegisterOAuth2(); await RegisterFormComponents(); await RegisterInterfaces(); + await HookPluginBuild(); await BuildWebAssemblyHost(); + await HookPluginConfigure(); await LoadAssets(); await WebAssemblyHost.RunAsync(); @@ -185,6 +190,72 @@ public class Startup WebAssemblyHostBuilder.Services.AddSingleton(ApplicationAssemblyService); } + private Task InitializePlugins() + { + var initialisationServiceCollection = new ServiceCollection(); + + initialisationServiceCollection.AddLogging(builder => { builder.AddProviders(LoggerProviders); }); + + // Configure plugin loading by using the interface service + initialisationServiceCollection.AddInterfaces(configuration => + { + // We use moonlight itself as a plugin assembly + configuration.AddAssembly(typeof(Startup).Assembly); + + configuration.AddAssemblies(PluginLoaderService.PluginAssemblies); + + configuration.AddInterface(); + }); + + var initialisationServiceProvider = initialisationServiceCollection.BuildServiceProvider(); + + PluginAppStartups = initialisationServiceProvider.GetRequiredService(); + + return Task.CompletedTask; + } + + #region Hooks + + private async Task HookPluginBuild() + { + foreach (var pluginAppStartup in PluginAppStartups) + { + try + { + await pluginAppStartup.BuildApp(WebAssemblyHostBuilder); + } + catch (Exception e) + { + Logger.LogError( + "An error occured while processing 'BuildApp' for '{name}': {e}", + pluginAppStartup.GetType().FullName, + e + ); + } + } + } + + private async Task HookPluginConfigure() + { + foreach (var pluginAppStartup in PluginAppStartups) + { + try + { + await pluginAppStartup.ConfigureApp(WebAssemblyHost); + } + catch (Exception e) + { + Logger.LogError( + "An error occured while processing 'ConfigureApp' for '{name}': {e}", + pluginAppStartup.GetType().FullName, + e + ); + } + } + } + + #endregion + #endregion #region Logging