feat: more plugin work and intra-scope communication
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Godot;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OwofGames.GodotLume;
|
||||
|
||||
namespace GodotHostTest.GodotDI;
|
||||
@@ -13,18 +15,33 @@ public partial class ServiceSource : Node
|
||||
[Export] private string _serviceScope = RootScopeQualifiedName;
|
||||
[Export] protected Node?[] InjectedNodes = [];
|
||||
|
||||
internal void ResolveInjectedNodes(IServiceProvider serviceProvider)
|
||||
internal void ResolveInjectedNodes(IServiceProvider serviceProvider, Delegate? onScopeCreated)
|
||||
{
|
||||
var logger = serviceProvider.GetRequiredService<ILogger<ServiceSource>>();
|
||||
|
||||
// if this service source must also act as a scope, create the scope and use its service provider
|
||||
if (_serviceScope != RootScopeQualifiedName)
|
||||
{
|
||||
// create the scoped provider
|
||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||
var scopedProvider = serviceProvider
|
||||
.GetRequiredService<IProvider>()
|
||||
.GetScopedProvider(serviceScope);
|
||||
// convert it also to an IServiceProvider
|
||||
var serviceProviderCreator = serviceProvider.GetRequiredService<Func<IProvider, IServiceProvider>>();
|
||||
var scopedProvider = serviceProvider.GetRequiredService<IProvider>()
|
||||
.GetScopedProvider(ScopeTypesAndNames.GetType(_serviceScope));
|
||||
serviceProvider = serviceProviderCreator(scopedProvider);
|
||||
// serviceProvider = serviceProvider
|
||||
// .CreateScope()
|
||||
// .ServiceProvider;
|
||||
// invoke onScopeCreated, if present
|
||||
// TODO: replace with a source code generation by turning this method private partial with Action<IProvider> argument, and every invocation site calls the necessary provider.Get<...> to build the argument list
|
||||
if (onScopeCreated != null)
|
||||
{
|
||||
var parameters = onScopeCreated.Method.GetParameters()
|
||||
.Select(parameterInfo => scopedProvider.Get(parameterInfo.ParameterType));
|
||||
onScopeCreated.DynamicInvoke(parameters);
|
||||
}
|
||||
}
|
||||
else if (onScopeCreated != null)
|
||||
{
|
||||
logger.LogWarning("onScopeCreated passed during instantiation, but no scoped provider was created.");
|
||||
}
|
||||
|
||||
// resolve the injected nodes against the chosen service provider
|
||||
@@ -32,6 +49,7 @@ public partial class ServiceSource : Node
|
||||
foreach (var node in InjectedNodes)
|
||||
{
|
||||
if (node == null) continue;
|
||||
logger.LogTrace("Injecting {Node}.", node.Name);
|
||||
var nodeType = node.GetType();
|
||||
foreach (var field in nodeType
|
||||
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
||||
|
||||
Reference in New Issue
Block a user