feat: some application of source code generator
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using OwofGames.GodotLume;
|
||||
|
||||
namespace GodotHostTest.AetherBind;
|
||||
|
||||
@@ -9,7 +10,7 @@ public interface ISceneInstantiator
|
||||
/// Instantiate a packed scene, just like <see cref="PackedScene.Instantiate"/>, but also looks for a scope between the top level children of the instantiated scene and triggers the dependency injection mechanism when found.
|
||||
/// </summary>
|
||||
/// <param name="packedScene">The packed scene to instantiate.</param>
|
||||
/// <param name="onScopeCreated">An optional callback invoked once the scope has been created, but before instantiating the scene.</param>
|
||||
/// <param name="onScopeCreated">An optional callback invoked once the scope has been created, but before instantiating the scene, with the provider as argument.</param>
|
||||
/// <returns>The created node, with its dependencies satisfied.</returns>
|
||||
Node Instantiate(PackedScene packedScene, Delegate? onScopeCreated = null);
|
||||
Node Instantiate(PackedScene packedScene, Action<IProvider>? onScopeCreated = null);
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace GodotHostTest.AetherBind;
|
||||
internal class SceneInstantiator(IProvider provider) : ISceneInstantiator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Node Instantiate(PackedScene packedScene, Delegate? onScopeCreated = null)
|
||||
public Node Instantiate(PackedScene packedScene, Action<IProvider>? onScopeCreated = null)
|
||||
{
|
||||
var node = packedScene.Instantiate();
|
||||
var scope = node.GetChildren().OfType<ServiceSource>().SingleOrDefault();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using OwofGames.GodotLume;
|
||||
|
||||
namespace GodotHostTest.AetherBind;
|
||||
|
||||
@@ -13,11 +14,11 @@ public static class SceneInstantiatorExtensions
|
||||
/// <param name="packedScene">The packed scene to instantiate.</param>
|
||||
/// <param name="onScopeCreated">
|
||||
/// An optional callback invoked once the scope has been created, but before instantiating the
|
||||
/// scene.
|
||||
/// scene, with the provider as argument.
|
||||
/// </param>
|
||||
/// <returns>The created node, with its dependencies satisfied.</returns>
|
||||
private static T Instantiate<T>(this ISceneInstantiator sceneInstantiator, PackedScene packedScene,
|
||||
Delegate? onScopeCreated = null) where T : Node
|
||||
Action<IProvider>? onScopeCreated = null) where T : Node
|
||||
{
|
||||
return (T)sceneInstantiator.Instantiate(packedScene, onScopeCreated);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ public partial class ServiceSource : Node
|
||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||
provider = provider.GetScopedProvider(serviceScope);
|
||||
// invoke onScopeCreated, if present
|
||||
// TODO: replace with a source code generation by turning this method private 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
|
||||
@@ -52,33 +51,6 @@ public partial class ServiceSource : Node
|
||||
{
|
||||
injectable.Inject(provider);
|
||||
}
|
||||
|
||||
// var nodeType = node.GetType();
|
||||
// foreach (var field in nodeType
|
||||
// .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
||||
// {
|
||||
// if (field.GetCustomAttribute<InjectAttribute>() == null) continue;
|
||||
// var fieldType = field.FieldType;
|
||||
// var service = provider.Get(fieldType);
|
||||
// field.SetValue(node, service);
|
||||
// }
|
||||
//
|
||||
// foreach (var method in nodeType.GetMethods(BindingFlags.Instance | BindingFlags.Public |
|
||||
// BindingFlags.NonPublic))
|
||||
// {
|
||||
// if (method.GetCustomAttribute<InjectAttribute>() == null) continue;
|
||||
// var parameters = method.GetParameters();
|
||||
// var values = new object?[parameters.Length];
|
||||
// var i = 0;
|
||||
// foreach (var parameter in parameters)
|
||||
// {
|
||||
// var parameterType = parameter.ParameterType;
|
||||
// var service = provider.Get(parameterType);
|
||||
// values[i++] = service;
|
||||
// }
|
||||
//
|
||||
// method.Invoke(node, values);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user