private IValueReference GetCurrentScene()

in debugger/debugger-worker/src/Evaluation/MonoGodotAdditionalValuesProvider.cs [99:163]


        private IValueReference<TValue> GetCurrentScene(IStackFrame frame)
        {
            return myLogger.CatchEvaluatorException<TValue, IValueReference<TValue>>(() =>
                {
                    var engineType = myValueServices.GetReifiedType(frame, "Godot.Engine, GodotSharp");
                    if (engineType == null)
                    {
                        myLogger.Warn("Unable to get typeof(Engine). Not a Godot project?");
                        return null;
                    }

                    var getMainLoop = engineType.MetadataType.GetMethods()
                        .FirstOrDefault(m => m.IsStatic && m.Parameters.Length == 0 && m.Name == "GetMainLoop");
                    if (getMainLoop == null)
                    {
                        myLogger.Warn("Unable to find Engine.GetMainLoop method");
                        return null;
                    }

                    // GetMainLoop can throw a exception if we call it from the wrong location
                    var mainLoop = engineType.CallStaticMethod(frame, mySession.EvaluationOptions, getMainLoop);
                    if (mainLoop == null)
                    {
                        myLogger.Warn("Unexpected response: Engine.GetMainLoop() == null");
                        return null;
                    }
                    
                    var sceneTreeType = myValueServices.GetReifiedType(frame, "Godot.SceneTree, GodotSharp");
                    if (sceneTreeType == null)
                    {
                        myLogger.Warn("Unable to get typeof(SceneTree).");
                        return null;
                    }
                    
                    var nodeType = myValueServices.GetReifiedType(frame, "Godot.Node, GodotSharp");
                    if (nodeType == null)
                    {
                        myLogger.Warn("Unable to get typeof(Node).");
                        return null;
                    }
                    
                    var mainLoopReference = new SimpleValueReference<TValue>(mainLoop, sceneTreeType.MetadataType,
                        "MainLoop", ValueOriginKind.Property,
                        ValueFlags.None | ValueFlags.IsReadOnly | ValueFlags.IsDefaultTypePresentation, frame,
                        myValueServices.RoleFactory);

                    if (!(mainLoopReference.GetPrimaryRole(mySession.EvaluationOptions) is IObjectValueRole<TValue> role))
                    {
                        myLogger.Warn("Unable to get role from 'MainLoop' reference");
                        return null;
                    }

                    var currentSceneReference = role.GetInstancePropertyReference(new[] { "CurrentScene" });
                    if (currentSceneReference == null)
                    {
                        myLogger.Warn("Unexpected response: CurrentScene == null");
                        return null;
                    }

                    return new SimpleValueReference<TValue>(currentSceneReference.GetValue(mySession.EvaluationOptions),
                        nodeType.MetadataType, "CurrentScene", ValueOriginKind.Property,
                        ValueFlags.None | ValueFlags.IsReadOnly | ValueFlags.IsDefaultTypePresentation, frame,
                        myValueServices.RoleFactory);
                }, exception => { });
        }