using MoonlightServers.Daemon.Models.Cache; using MoonlightServers.Daemon.ServerSystem.Docker; using MoonlightServers.Daemon.ServerSystem.FileSystems; using MoonlightServers.Daemon.ServerSystem.Handlers; using MoonlightServers.Daemon.ServerSystem.Implementations; using MoonlightServers.Daemon.ServerSystem.Interfaces; using MoonlightServers.Daemon.ServerSystem.Models; namespace MoonlightServers.Daemon.ServerSystem; public class ServerFactory { private readonly IServiceProvider ServiceProvider; public ServerFactory(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; } public async Task CreateAsync(ServerConfiguration configuration) { var scope = ServiceProvider.CreateAsyncScope(); var loggerFactory = scope.ServiceProvider.GetRequiredService(); var logger = loggerFactory.CreateLogger($"Servers.Instance.{configuration.Id}.{nameof(Server)}"); var context = scope.ServiceProvider.GetRequiredService(); context.Identifier = configuration.Id; context.Configuration = configuration; context.ServiceScope = scope; context.Logger = logger; // Define all required components IConsole console; IFileSystem runtimeFs; IFileSystem installFs; IInstallation installation; IOnlineDetector onlineDetector; IReporter reporter; IRestorer restorer; IRuntime runtime; IStatistics statistics; // Resolve the components console = ActivatorUtilities.CreateInstance(scope.ServiceProvider); reporter = ActivatorUtilities.CreateInstance(scope.ServiceProvider); runtimeFs = ActivatorUtilities.CreateInstance(scope.ServiceProvider); installFs = ActivatorUtilities.CreateInstance(scope.ServiceProvider); installation = ActivatorUtilities.CreateInstance(scope.ServiceProvider); onlineDetector = ActivatorUtilities.CreateInstance(scope.ServiceProvider); restorer = ActivatorUtilities.CreateInstance(scope.ServiceProvider); runtime = ActivatorUtilities.CreateInstance(scope.ServiceProvider); statistics = ActivatorUtilities.CreateInstance(scope.ServiceProvider); // Resolve handlers var handlers = new List(); handlers.Add(ActivatorUtilities.CreateInstance(scope.ServiceProvider)); handlers.Add(ActivatorUtilities.CreateInstance(scope.ServiceProvider)); // TODO: Add a plugin hook for dynamically resolving components and checking if any is unset // Resolve server from di var server = new Server( logger, context, // Now all components console, runtimeFs, installFs, installation, onlineDetector, reporter, restorer, runtime, statistics, // And now all the handlers handlers.ToArray() ); context.Server = server; return server; } }