// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.GameServer.Host; using System.Collections.Generic; using System.Threading; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MUnique.OpenMU.Dapr.Common; using MUnique.OpenMU.Interfaces; using MUnique.OpenMU.PlugIns; using MUnique.OpenMU.Web.Map; /// /// A wrapper which takes a and wraps it as , /// so that additional initialization can be done before actually starting it. /// The actual server start is deferred to which is called after the web application /// has started (i.e. the HTTP API is already available), breaking the circular startup dependency with the Dapr sidecar. /// TODO: listen to configuration changes/database reinit. /// See also: ServerContainerBase. /// public class GameServerHostedServiceWrapper : IHostedLifecycleService { private readonly IServiceProvider _serviceProvider; private IGameServer? _gameServer; /// /// Initializes a new instance of the class. /// /// The service provider. public GameServerHostedServiceWrapper(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider; } /// public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask; /// public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; /// public async Task StartedAsync(CancellationToken cancellationToken) { await this._serviceProvider.WaitForDatabaseInitializationAsync(cancellationToken).ConfigureAwait(false); if (this._serviceProvider.GetService>() is { } plugInCollection) { if (plugInCollection is not List plugInConfigurations) { throw new InvalidOperationException($"The registered {nameof(ICollection)} must be a {nameof(List)} to be able to load plugin configurations."); } await this._serviceProvider.TryLoadPlugInConfigurationsAsync(plugInConfigurations).ConfigureAwait(false); } this._gameServer = this._serviceProvider.GetRequiredService(); var initializer = this._serviceProvider.GetRequiredService(); await initializer.InitializeAsync().ConfigureAwait(false); await ((ObservableGameServerAdapter)this._serviceProvider.GetRequiredService()) .InitializeAsync().ConfigureAwait(false); await this._gameServer.StartAsync(cancellationToken).ConfigureAwait(false); } /// public Task StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; /// public Task StopAsync(CancellationToken cancellationToken) { return this._gameServer?.StopAsync(cancellationToken) ?? Task.CompletedTask; } /// public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask; }