feat: cleanups and API updates
This commit is contained in:
@@ -3,9 +3,9 @@ using GodotHostTest.AetherBind;
|
|||||||
using GodotHostTest.Interfaces;
|
using GodotHostTest.Interfaces;
|
||||||
using OwofGames.AetherBind;
|
using OwofGames.AetherBind;
|
||||||
|
|
||||||
namespace GodotHostTest.Game.Root;
|
namespace GodotHostTest.Game.Game;
|
||||||
|
|
||||||
public partial class Root : Node2D
|
public partial class Game : Node2D
|
||||||
{
|
{
|
||||||
[Inject] private ICurrentLevel _currentLevel = null!;
|
[Inject] private ICurrentLevel _currentLevel = null!;
|
||||||
[Inject] private ILevelDataProvider _levelDataProvider = null!;
|
[Inject] private ILevelDataProvider _levelDataProvider = null!;
|
||||||
@@ -8,7 +8,7 @@ using OwofGames.GodotHost.Observability;
|
|||||||
using OwofGames.GodotLume;
|
using OwofGames.GodotLume;
|
||||||
using RootServiceSource = GodotHostTest.AetherBind.RootServiceSource;
|
using RootServiceSource = GodotHostTest.AetherBind.RootServiceSource;
|
||||||
|
|
||||||
namespace GodotHostTest.Game.Root;
|
namespace GodotHostTest.Game.Game;
|
||||||
|
|
||||||
public partial class GameServiceSource : RootServiceSource
|
public partial class GameServiceSource : RootServiceSource
|
||||||
{
|
{
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
[gd_scene format=3 uid="uid://vsburk37p4u"]
|
[gd_scene format=3 uid="uid://vsburk37p4u"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://bvjvojer872c8" path="res://Game/Root/Root.cs" id="1_iqet6"]
|
[ext_resource type="Script" uid="uid://bvjvojer872c8" path="res://Game/Game/Game.cs" id="1_iqet6"]
|
||||||
[ext_resource type="PackedScene" uid="uid://xfd48ep5qi1k" path="res://Game/Level/level.tscn" id="3_bbjqm"]
|
[ext_resource type="PackedScene" uid="uid://xfd48ep5qi1k" path="res://Game/Level/level.tscn" id="2_ft4gu"]
|
||||||
[ext_resource type="Script" uid="uid://bpes6mnv21f2v" path="res://Game/Root/GameServiceSource.cs" id="3_iqet6"]
|
[ext_resource type="Script" uid="uid://bpes6mnv21f2v" path="res://Game/Game/GameServiceSource.cs" id="3_iqet6"]
|
||||||
[ext_resource type="Resource" uid="uid://c4ym7vrpgk4e6" path="res://Game/levels_data.tres" id="4_t2jh2"]
|
[ext_resource type="Resource" uid="uid://c4ym7vrpgk4e6" path="res://Game/levels_data.tres" id="4_t2jh2"]
|
||||||
[ext_resource type="PackedScene" uid="uid://cvwvwv55n00ce" path="res://Game/Background/background.tscn" id="5_bbjqm"]
|
[ext_resource type="PackedScene" uid="uid://cvwvwv55n00ce" path="res://Game/Background/background.tscn" id="5_bbjqm"]
|
||||||
|
|
||||||
[node name="Root" type="Node2D" unique_id=920849082]
|
[node name="Root" type="Node2D" unique_id=920849082]
|
||||||
script = ExtResource("1_iqet6")
|
script = ExtResource("1_iqet6")
|
||||||
_levelScene = ExtResource("3_bbjqm")
|
_levelScene = ExtResource("2_ft4gu")
|
||||||
|
|
||||||
[node name="Scope" type="Node" parent="." unique_id=597932533 node_paths=PackedStringArray("InjectedNodes")]
|
[node name="Scope" type="Node" parent="." unique_id=597932533 node_paths=PackedStringArray("InjectedNodes")]
|
||||||
script = ExtResource("3_iqet6")
|
script = ExtResource("3_iqet6")
|
||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
[sub_resource type="Resource" id="Resource_bbjqm"]
|
[sub_resource type="Resource" id="Resource_bbjqm"]
|
||||||
script = ExtResource("1_bkfnq")
|
script = ExtResource("1_bkfnq")
|
||||||
TimeBetweenSpawns = 1.0
|
|
||||||
SpawnDuration = 4.0
|
SpawnDuration = 4.0
|
||||||
|
TimeBetweenSpawns = 1.0
|
||||||
metadata/_custom_type_script = "uid://ywe7vkywrdvk"
|
metadata/_custom_type_script = "uid://ywe7vkywrdvk"
|
||||||
|
|
||||||
[resource]
|
[resource]
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace GodotHostTest.AetherBind.Editor;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class GodotScriptPathCache
|
public static class GodotScriptPathCache
|
||||||
{
|
{
|
||||||
private static readonly ConcurrentDictionary<Type, string> TypeToPath = new();
|
// private static readonly ConcurrentDictionary<Type, string> TypeToPath = new();
|
||||||
private static readonly ConcurrentDictionary<string, Type> PathToType = new();
|
private static readonly ConcurrentDictionary<string, Type> PathToType = new();
|
||||||
private static readonly Type GodotObject = typeof(GodotObject);
|
private static readonly Type GodotObject = typeof(GodotObject);
|
||||||
private static readonly Type ScriptPathAttribute = typeof(ScriptPathAttribute);
|
private static readonly Type ScriptPathAttribute = typeof(ScriptPathAttribute);
|
||||||
@@ -32,9 +32,9 @@ public static class GodotScriptPathCache
|
|||||||
private static void Initialize()
|
private static void Initialize()
|
||||||
{
|
{
|
||||||
GD.Print($"Initializing {nameof(GodotScriptPathCache)}");
|
GD.Print($"Initializing {nameof(GodotScriptPathCache)}");
|
||||||
TypeToPath.Clear();
|
// TypeToPath.Clear();
|
||||||
PathToType.Clear();
|
PathToType.Clear();
|
||||||
_assemblyLoadEventHandler = (sender, args) => CacheScriptsInAssembly(args.LoadedAssembly);
|
_assemblyLoadEventHandler = (_, args) => CacheScriptsInAssembly(args.LoadedAssembly);
|
||||||
AppDomain.CurrentDomain.AssemblyLoad += _assemblyLoadEventHandler;
|
AppDomain.CurrentDomain.AssemblyLoad += _assemblyLoadEventHandler;
|
||||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
CacheScriptsInAssembly(assembly);
|
CacheScriptsInAssembly(assembly);
|
||||||
@@ -49,7 +49,7 @@ public static class GodotScriptPathCache
|
|||||||
if (type.GetCustomAttributes(ScriptPathAttribute, false).FirstOrDefault() is ScriptPathAttribute
|
if (type.GetCustomAttributes(ScriptPathAttribute, false).FirstOrDefault() is ScriptPathAttribute
|
||||||
scriptPath)
|
scriptPath)
|
||||||
{
|
{
|
||||||
TypeToPath[type] = scriptPath.Path;
|
// TypeToPath[type] = scriptPath.Path;
|
||||||
PathToType[scriptPath.Path] = type;
|
PathToType[scriptPath.Path] = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,31 +60,31 @@ public static class GodotScriptPathCache
|
|||||||
GD.Print($"Deinitializing {nameof(GodotScriptPathCache)}");
|
GD.Print($"Deinitializing {nameof(GodotScriptPathCache)}");
|
||||||
AppDomain.CurrentDomain.AssemblyLoad -= _assemblyLoadEventHandler;
|
AppDomain.CurrentDomain.AssemblyLoad -= _assemblyLoadEventHandler;
|
||||||
_assemblyLoadEventHandler = null;
|
_assemblyLoadEventHandler = null;
|
||||||
TypeToPath.Clear();
|
// TypeToPath.Clear();
|
||||||
PathToType.Clear();
|
PathToType.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool TryGetScriptPath(Type type, [MaybeNullWhen(false)] out string path)
|
// public static bool TryGetScriptPath(Type type, [MaybeNullWhen(false)] out string path)
|
||||||
{
|
// {
|
||||||
InitializeIfNeeded();
|
// InitializeIfNeeded();
|
||||||
return TypeToPath.TryGetValue(type, out path);
|
// return TypeToPath.TryGetValue(type, out path);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public static Script GetScriptFromType(Type type)
|
// public static Script GetScriptFromType(Type type)
|
||||||
{
|
// {
|
||||||
InitializeIfNeeded();
|
// InitializeIfNeeded();
|
||||||
return TryGetScriptPath(type, out string path)
|
// return TryGetScriptPath(type, out string path)
|
||||||
? GD.Load<Script>(path)
|
// ? GD.Load<Script>(path)
|
||||||
: throw new InvalidOperationException("Script path not found in cache.");
|
// : throw new InvalidOperationException("Script path not found in cache.");
|
||||||
}
|
// }
|
||||||
|
|
||||||
public static Script GetScriptFromType<T>()
|
// public static Script GetScriptFromType<T>()
|
||||||
{
|
// {
|
||||||
InitializeIfNeeded();
|
// InitializeIfNeeded();
|
||||||
return GetScriptFromType(typeof(T));
|
// return GetScriptFromType(typeof(T));
|
||||||
}
|
// }
|
||||||
|
|
||||||
public static bool TryGetTypeFromPath(string path, [MaybeNullWhen(false)] out Type type)
|
private static bool TryGetTypeFromPath(string path, [MaybeNullWhen(false)] out Type type)
|
||||||
{
|
{
|
||||||
InitializeIfNeeded();
|
InitializeIfNeeded();
|
||||||
return PathToType.TryGetValue(path, out type);
|
return PathToType.TryGetValue(path, out type);
|
||||||
|
|||||||
@@ -78,15 +78,20 @@ public partial class Plugin : EditorPlugin
|
|||||||
// mark debounce as completed
|
// mark debounce as completed
|
||||||
_sceneTreeTimer = null;
|
_sceneTreeTimer = null;
|
||||||
|
|
||||||
|
|
||||||
// check that the instance is still valid after debouncing (could have been disposed)
|
// check that the instance is still valid after debouncing (could have been disposed)
|
||||||
if (!IsInstanceValid(sceneRoot)) return;
|
if (!IsInstanceValid(sceneRoot)) return;
|
||||||
|
|
||||||
var sceneNodes = sceneRoot.FindChildren("*").Where(node => node.Owner == sceneRoot).Append(sceneRoot).ToList();
|
// get all nodes belonging to this root
|
||||||
GD.Print($"Updating injection for root {sceneRoot.Name}");
|
var sceneNodes = sceneRoot
|
||||||
|
.FindChildren("*")
|
||||||
|
.Where(node => node.Owner == sceneRoot)
|
||||||
|
.Append(sceneRoot)
|
||||||
|
.ToList();
|
||||||
|
// GD.Print($"Updating injection for root {sceneRoot.Name} ({sceneNodes.Count} nodes)");
|
||||||
|
|
||||||
// look for the service source
|
// look for the service source
|
||||||
var serviceSource = sceneNodes.Where(child => child.IsServiceSource())
|
var serviceSource = sceneNodes
|
||||||
|
.Where(child => child.IsServiceSource())
|
||||||
.Single(out var failureReason);
|
.Single(out var failureReason);
|
||||||
|
|
||||||
switch (failureReason)
|
switch (failureReason)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using Godot;
|
using Godot;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using OwofGames.AetherBind;
|
using OwofGames.AetherBind;
|
||||||
@@ -7,14 +6,31 @@ using OwofGames.GodotLume;
|
|||||||
|
|
||||||
namespace GodotHostTest.AetherBind;
|
namespace GodotHostTest.AetherBind;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A node that provides injected services to other nodes.
|
||||||
|
/// </summary>
|
||||||
[Icon("res://addons/AetherBind/ServiceSource.svg")]
|
[Icon("res://addons/AetherBind/ServiceSource.svg")]
|
||||||
public partial class ServiceSource : Node
|
public partial class ServiceSource : Node
|
||||||
{
|
{
|
||||||
private static readonly string RootScopeQualifiedName = typeof(RootScope).AssemblyQualifiedName!;
|
private static readonly string RootScopeQualifiedName = typeof(RootScope).AssemblyQualifiedName!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of nodes that must be injected. This list will be automatically populated with all nodes in this object's
|
||||||
|
/// editor scene that have at least one [Inject] field or method.
|
||||||
|
/// </summary>
|
||||||
[Export] protected Node?[] InjectedNodes = [];
|
[Export] protected Node?[] InjectedNodes = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Name of the scope for the services built by this service source.
|
||||||
|
/// </summary>
|
||||||
[Export] private string _serviceScope = RootScopeQualifiedName;
|
[Export] private string _serviceScope = RootScopeQualifiedName;
|
||||||
|
|
||||||
internal void ResolveInjectedNodes(IProvider provider, Delegate? onScopeCreated)
|
/// <summary>
|
||||||
|
/// Inject the required services in <see cref="InjectedNodes" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="provider">The service provider.</param>
|
||||||
|
/// <param name="onScopeCreated">A callback method invoked with this scope's provider, if a scope was created.</param>
|
||||||
|
internal void ResolveInjectedNodes(IProvider provider, Action<IProvider>? onScopeCreated)
|
||||||
{
|
{
|
||||||
var logger = provider.Get<ILogger<ServiceSource>>();
|
var logger = provider.Get<ILogger<ServiceSource>>();
|
||||||
|
|
||||||
@@ -24,16 +40,11 @@ public partial class ServiceSource : Node
|
|||||||
// create the scoped provider
|
// create the scoped provider
|
||||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||||
provider = provider.GetScopedProvider(serviceScope);
|
provider = provider.GetScopedProvider(serviceScope);
|
||||||
// invoke onScopeCreated, if present
|
|
||||||
if (onScopeCreated != null)
|
|
||||||
{
|
|
||||||
var parameters = onScopeCreated.Method
|
|
||||||
.GetParameters()
|
|
||||||
.Select(parameterInfo => provider.Get(parameterInfo.ParameterType))
|
|
||||||
.ToArray();
|
|
||||||
onScopeCreated.DynamicInvoke(parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// invoke onScopeCreated, if present
|
||||||
|
onScopeCreated?.Invoke(provider);
|
||||||
|
|
||||||
|
// invoke overridden callback
|
||||||
OnScopeCreated(provider);
|
OnScopeCreated(provider);
|
||||||
}
|
}
|
||||||
else if (onScopeCreated != null)
|
else if (onScopeCreated != null)
|
||||||
@@ -55,6 +66,12 @@ public partial class ServiceSource : Node
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: also add source generator to mark methods in derived classes with [OnScopeCreated] and perform parameter injection
|
// TODO: also add source generator to mark methods in derived classes with [OnScopeCreated] and perform parameter injection
|
||||||
|
/// <summary>
|
||||||
|
/// This method is invoked as soon as the scope for the services is created, but before the services are injected into
|
||||||
|
/// the <see cref="InjectedNodes" />. Override this method in derived classes to perform custom initialization steps
|
||||||
|
/// for the services, like setting <see cref="ValueProvider{T}" />s.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceProvider">Lume's service provider.</param>
|
||||||
protected virtual void OnScopeCreated(IProvider serviceProvider)
|
protected virtual void OnScopeCreated(IProvider serviceProvider)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user