feat: some application of source code generator
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
uid://c4pr1lc7ikb4h
|
||||||
+6
-1
@@ -1,7 +1,9 @@
|
|||||||
|
using System;
|
||||||
using Godot;
|
using Godot;
|
||||||
using GodotHostTest.AetherBind;
|
using GodotHostTest.AetherBind;
|
||||||
using GodotHostTest.Interfaces;
|
using GodotHostTest.Interfaces;
|
||||||
using OwofGames.AetherBind;
|
using OwofGames.AetherBind;
|
||||||
|
using OwofGames.GodotLume;
|
||||||
|
|
||||||
namespace GodotHostTest.Game.Root;
|
namespace GodotHostTest.Game.Root;
|
||||||
|
|
||||||
@@ -17,6 +19,9 @@ public partial class Root : Node2D
|
|||||||
{
|
{
|
||||||
var levelData = _levelDataProvider.GetLevelData(_currentLevel.LevelNumber);
|
var levelData = _levelDataProvider.GetLevelData(_currentLevel.LevelNumber);
|
||||||
AddChild(_sceneInstantiator.Instantiate(_levelScene,
|
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);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
using OwofGames.GodotLume;
|
||||||
|
|
||||||
namespace GodotHostTest.AetherBind;
|
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.
|
/// 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>
|
/// </summary>
|
||||||
/// <param name="packedScene">The packed scene to instantiate.</param>
|
/// <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>
|
/// <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
|
internal class SceneInstantiator(IProvider provider) : ISceneInstantiator
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Node Instantiate(PackedScene packedScene, Delegate? onScopeCreated = null)
|
public Node Instantiate(PackedScene packedScene, Action<IProvider>? onScopeCreated = null)
|
||||||
{
|
{
|
||||||
var node = packedScene.Instantiate();
|
var node = packedScene.Instantiate();
|
||||||
var scope = node.GetChildren().OfType<ServiceSource>().SingleOrDefault();
|
var scope = node.GetChildren().OfType<ServiceSource>().SingleOrDefault();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
using OwofGames.GodotLume;
|
||||||
|
|
||||||
namespace GodotHostTest.AetherBind;
|
namespace GodotHostTest.AetherBind;
|
||||||
|
|
||||||
@@ -13,11 +14,11 @@ public static class SceneInstantiatorExtensions
|
|||||||
/// <param name="packedScene">The packed scene to instantiate.</param>
|
/// <param name="packedScene">The packed scene to instantiate.</param>
|
||||||
/// <param name="onScopeCreated">
|
/// <param name="onScopeCreated">
|
||||||
/// An optional callback invoked once the scope has been created, but before instantiating the
|
/// An optional callback invoked once the scope has been created, but before instantiating the
|
||||||
/// scene.
|
/// scene, with the provider as argument.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>The created node, with its dependencies satisfied.</returns>
|
/// <returns>The created node, with its dependencies satisfied.</returns>
|
||||||
private static T Instantiate<T>(this ISceneInstantiator sceneInstantiator, PackedScene packedScene,
|
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);
|
return (T)sceneInstantiator.Instantiate(packedScene, onScopeCreated);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ public partial class ServiceSource : Node
|
|||||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||||
provider = provider.GetScopedProvider(serviceScope);
|
provider = provider.GetScopedProvider(serviceScope);
|
||||||
// invoke onScopeCreated, if present
|
// 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)
|
if (onScopeCreated != null)
|
||||||
{
|
{
|
||||||
var parameters = onScopeCreated.Method
|
var parameters = onScopeCreated.Method
|
||||||
@@ -52,33 +51,6 @@ public partial class ServiceSource : Node
|
|||||||
{
|
{
|
||||||
injectable.Inject(provider);
|
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