69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using OwofGames.GodotHost;
|
|
using OwofGames.GodotLume;
|
|
using OwofGames.GodotLume.Microsoft.DependencyInjection;
|
|
|
|
namespace GodotHostTest.AetherBind;
|
|
|
|
/// <summary>
|
|
/// The base class for a root scope. Implement this class and its abstract methods to kick off the DI system.
|
|
/// </summary>
|
|
public abstract partial class RootServiceSource : ServiceSource
|
|
{
|
|
private Host? _host;
|
|
|
|
private ILogger<RootServiceSource>? _logger;
|
|
|
|
private Host Host
|
|
{
|
|
get => _host ??
|
|
throw new InvalidOperationException(
|
|
"Some method of RootScope has been invoked before it entered the tree");
|
|
set => _host = value;
|
|
}
|
|
|
|
private ILogger<RootServiceSource> Logger => _logger ??= Host.GetLogger<RootServiceSource>();
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
// create the host
|
|
Host = new HostBuilder()
|
|
.SetServiceCollectionBuilder(InnerConfigure)
|
|
.SetServiceProviderFactory(new LumeServiceProviderFactory(), Configure)
|
|
.Build();
|
|
|
|
// host startup
|
|
Host.Startup();
|
|
|
|
// resolve nodes
|
|
ResolveInjectedNodes(Host.ServiceProvider.GetRequiredService<IProvider>(), null);
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
// async shutdown
|
|
Host.Shutdown();
|
|
}
|
|
|
|
private void InnerConfigure(IServiceCollection serviceCollection)
|
|
{
|
|
serviceCollection.AddTransient<ISceneInstantiator, SceneInstantiator>();
|
|
ConfigureServices(serviceCollection);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure the service collection by adding the registrations your game needs.
|
|
/// </summary>
|
|
/// <param name="builder">The builder to enrich with your services.</param>
|
|
protected abstract void Configure(Builder builder);
|
|
|
|
/// <summary>
|
|
/// Configure the service collection by adding the registrations your game needs.
|
|
/// </summary>
|
|
/// <param name="serviceCollection">The service collection to enrich.</param>
|
|
protected virtual void ConfigureServices(IServiceCollection serviceCollection)
|
|
{
|
|
}
|
|
} |