diff --git a/addons/GodotDI/Editor/Plugin.cs b/addons/GodotDI/Editor/Plugin.cs index 5541c5a..0414db1 100644 --- a/addons/GodotDI/Editor/Plugin.cs +++ b/addons/GodotDI/Editor/Plugin.cs @@ -1,5 +1,4 @@ #if TOOLS -using System; using System.Linq; using System.Reflection; using Godot; @@ -21,8 +20,6 @@ public partial class Plugin : EditorPlugin private ScopeInspectorPlugin? _plugin; private SceneTreeTimer? _sceneTreeTimer; - private readonly object _sceneTreeTimerLock = new(); - private bool _treeChangesRegistered; public override void _EnterTree() @@ -56,7 +53,7 @@ public partial class Plugin : EditorPlugin OnSceneChanged(root); } - private async void OnSceneChanged(Node? sceneRoot) + private void OnSceneChanged(Node? sceneRoot) { // register to tree changes, in case the registration wasn't possible before RegisterTreeChanges(); @@ -65,25 +62,14 @@ public partial class Plugin : EditorPlugin if (sceneRoot == null) return; // apply debounce - var editedSceneRoot = EditorInterface.Singleton.GetEditedSceneRoot(); - - if (!editedSceneRoot.IsInsideTree()) await ToSignal(editedSceneRoot, Node.SignalName.TreeEntered); - - var tree = editedSceneRoot.GetTree(); - if (tree == null) - throw new InvalidOperationException("editedSceneRoot.GetTree() was null even after TreeEntered"); - - lock (_sceneTreeTimerLock) + if (_sceneTreeTimer != null) { - if (_sceneTreeTimer != null) - { - _sceneTreeTimer.SetTimeLeft(DebounceDelay); - return; - } - - _sceneTreeTimer = tree.CreateTimer(DebounceDelay); - _sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot); + _sceneTreeTimer.SetTimeLeft(DebounceDelay); + return; } + + _sceneTreeTimer = GetTree().CreateTimer(DebounceDelay); + _sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot); } private void ActualOnSceneChanged(Node sceneRoot) @@ -91,12 +77,13 @@ public partial class Plugin : EditorPlugin // mark debounce as completed _sceneTreeTimer = null; - var sceneNodes = sceneRoot.FindChildren("*"); + var sceneNodes = sceneRoot.FindChildren("*").Where(node => node.Owner == sceneRoot).Append(sceneRoot).ToList(); GD.Print($"Updating injection for root {sceneRoot.Name}"); // look for the service source var serviceSource = sceneNodes.Where(child => child.IsServiceSource()) .Single(out var failureReason); + switch (failureReason) { case EnumerableExtensions.SingleFailureReason.LessThanOne: @@ -107,7 +94,7 @@ public partial class Plugin : EditorPlugin default: { // find all nodes that need injection - var nodesNeedingInjection = sceneNodes.Append(sceneRoot).Where(node => + var nodesNeedingInjection = sceneNodes.Where(node => { var type = node.GetCSharpScriptType(); return type != null && ( @@ -126,7 +113,7 @@ public partial class Plugin : EditorPlugin { newArray ??= new Array(injectedNodes); newArray.Add(node); - GD.Print($"Added node {node.Name} to injection."); + GD.Print($"Added node {node.Name} to scope {serviceSource.GetPath()}."); } // remove all nodes that are no longer needing injection @@ -137,7 +124,7 @@ public partial class Plugin : EditorPlugin if (node == null) continue; newArray ??= new Array(injectedNodes); newArray.Remove(node); - GD.Print($"Removed node {node?.Name} from injection."); + GD.Print($"Removed node {node.Name} to scope {serviceSource.GetPath()}."); } // apply changes to the scene if necessary