feat: initial import

This commit is contained in:
redglow
2026-07-11 10:54:02 +02:00
commit e560a743fb
55 changed files with 993 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
using Godot;
namespace GodotHostTest.GodotDI;
public interface ISceneInstantiator
{
/// <summary>
/// Instantiate a packed scene, just like <see cref="PackedScene.Instantiate"/>, but also looks for a scope between the top level children and triggers the dependency injection mechanism when found.
/// </summary>
/// <param name="packedScene">The packed scene to instantiate.</param>
/// <returns>The created node, with its dependencies satisfied.</returns>
Node Instantiate(PackedScene packedScene);
/// <summary>
/// Instantiate a packed scene, just like <see cref="PackedScene.Instantiate{T}"/>, but also looks for a scope between the top level children and triggers the dependency injection mechanism when found.
/// </summary>
/// <param name="packedScene">The packed scene to instantiate.</param>
/// <returns>The created node, with its dependencies satisfied.</returns>
T Instantiate<T>(PackedScene packedScene) where T : Node;
}
+1
View File
@@ -0,0 +1 @@
uid://bv7aut87rpm5u
+6
View File
@@ -0,0 +1,6 @@
using System;
namespace GodotHostTest.GodotDI;
[AttributeUsage(AttributeTargets.Field)]
public class InjectAttribute: Attribute;
+1
View File
@@ -0,0 +1 @@
uid://mshffj1jlgi7
+77
View File
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using Godot;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OwofGames.GodotHost;
namespace GodotHostTest.GodotDI;
/// <summary>
/// The base class for a root scope. Implement this class and its abstract methods to kick off the DI system.
/// </summary>
public abstract partial class RootScope : Scope, ISceneInstantiator
{
private Host? _host;
private Host Host
{
get => _host ??
throw new InvalidOperationException(
"Some method of RootScope has been invoked before it entered the tree");
set => _host = value;
}
private ILogger<RootScope>? _logger;
private ILogger<RootScope> Logger => _logger ??= Host.GetLogger<RootScope>();
public override void _EnterTree()
{
// create the host
Host = new HostBuilder()
.SetServiceCollectionBuilder(InnerConfigure)
.Build();
// resolve nodes
ResolveInjectedNodes(Host.ServiceProvider);
}
private void InnerConfigure(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<ISceneInstantiator>(this);
Configure(serviceCollection);
}
/// <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)
{
var node = packedScene.Instantiate();
var scope = node.GetChildren().OfType<Scope>().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);
}
}
+1
View File
@@ -0,0 +1 @@
uid://fr0c6wwras8c
+29
View File
@@ -0,0 +1,29 @@
using System;
using System.Reflection;
using Godot;
using Microsoft.Extensions.DependencyInjection;
namespace GodotHostTest.GodotDI;
public partial class Scope : Node
{
[Export] protected Node?[] InjectedNodes = [];
internal void ResolveInjectedNodes(IServiceProvider serviceProvider)
{
// resolve the injected nodes
// TODO: move to source generator, make it the only way for injection
foreach (var node in InjectedNodes)
{
if (node == null) continue;
foreach (var field in node.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (field.GetCustomAttribute<InjectAttribute>() == null) continue;
var fieldType = field.FieldType;
var service = serviceProvider.GetRequiredService(fieldType);
field.SetValue(node, service);
}
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://qjonl8stia46