feat: some application of source code generator

This commit is contained in:
redglow
2026-07-19 19:18:01 +02:00
parent aae9c083d9
commit 036e4af431
6 changed files with 14 additions and 34 deletions
+1
View File
@@ -0,0 +1 @@
uid://c4pr1lc7ikb4h
+6 -1
View File
@@ -1,7 +1,9 @@
using System;
using Godot;
using GodotHostTest.AetherBind;
using GodotHostTest.Interfaces;
using OwofGames.AetherBind;
using OwofGames.GodotLume;
namespace GodotHostTest.Game.Root;
@@ -17,6 +19,9 @@ public partial class Root : Node2D
{
var levelData = _levelDataProvider.GetLevelData(_currentLevel.LevelNumber);
AddChild(_sceneInstantiator.Instantiate(_levelScene,
(IValueSetter<ILevel> levelSetter) => { levelSetter.Set(levelData); }));
OnInstantiate(levelValueSetter => levelValueSetter.Set(levelData))));
}
[ServiceMethod]
private partial Action<IProvider> OnInstantiate(Action<IValueSetter<ILevel>> a);
}
+3 -2
View File
@@ -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);
}
+1 -1
View File
@@ -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);
}
-28
View File
@@ -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);
// }
}
}