feat: more plugin work and intra-scope communication
This commit is contained in:
@@ -4,6 +4,7 @@ using Godot;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OwofGames.GodotHost;
|
||||
using OwofGames.GodotLume;
|
||||
using OwofGames.GodotLume.Microsoft.DependencyInjection;
|
||||
|
||||
namespace GodotHostTest.GodotDI;
|
||||
@@ -15,6 +16,8 @@ public abstract partial class RootServiceSource : ServiceSource, ISceneInstantia
|
||||
{
|
||||
private Host? _host;
|
||||
|
||||
private ILogger<RootServiceSource>? _logger;
|
||||
|
||||
private Host Host
|
||||
{
|
||||
get => _host ??
|
||||
@@ -23,57 +26,59 @@ public abstract partial class RootServiceSource : ServiceSource, ISceneInstantia
|
||||
set => _host = value;
|
||||
}
|
||||
|
||||
private ILogger<RootServiceSource>? _logger;
|
||||
|
||||
private ILogger<RootServiceSource> Logger => _logger ??= Host.GetLogger<RootServiceSource>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public Node Instantiate(PackedScene packedScene, Delegate? onScopeCreated = null)
|
||||
{
|
||||
var node = packedScene.Instantiate();
|
||||
var scope = node.GetChildren().OfType<ServiceSource>().SingleOrDefault();
|
||||
if (scope == null)
|
||||
Logger.LogDebug(
|
||||
"Instantiated the scene {PackedSceneName} through ISceneInstantiator.Instantiate, but no scope found.",
|
||||
packedScene.GetName());
|
||||
else
|
||||
scope.ResolveInjectedNodes(Host.ServiceProvider, onScopeCreated);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public T Instantiate<T>(PackedScene packedScene, Delegate? onScopeCreated = null)
|
||||
where T : Node
|
||||
{
|
||||
return (T)Instantiate(packedScene, onScopeCreated);
|
||||
}
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
// create the host
|
||||
Host = new HostBuilder()
|
||||
.SetServiceCollectionBuilder(InnerConfigure)
|
||||
.SetServiceProviderFactory(new LumeServiceProviderFactory())
|
||||
.SetServiceProviderFactory(new LumeServiceProviderFactory(), Configure)
|
||||
.Build();
|
||||
|
||||
// resolve nodes
|
||||
ResolveInjectedNodes(Host.ServiceProvider);
|
||||
ResolveInjectedNodes(Host.ServiceProvider, null);
|
||||
}
|
||||
|
||||
private void InnerConfigure(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<ISceneInstantiator>(this);
|
||||
Configure(serviceCollection);
|
||||
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 abstract void Configure(IServiceCollection serviceCollection);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Node Instantiate(PackedScene packedScene)
|
||||
protected virtual void ConfigureServices(IServiceCollection serviceCollection)
|
||||
{
|
||||
var node = packedScene.Instantiate();
|
||||
var scope = node.GetChildren().OfType<ServiceSource>().SingleOrDefault();
|
||||
if (scope == null)
|
||||
{
|
||||
Logger.LogDebug(
|
||||
"Instantiated the scene {PackedSceneName} through ISceneInstantiator.Instantiate, but no scope found.",
|
||||
packedScene.GetName());
|
||||
}
|
||||
else
|
||||
{
|
||||
scope.ResolveInjectedNodes(Host.ServiceProvider);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public T Instantiate<T>(PackedScene packedScene)
|
||||
where T : Node
|
||||
{
|
||||
return (T)Instantiate(packedScene);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user