From 72d9ebb3c4b583020cd33b2059008a4cf2870162 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Sun, 6 Oct 2024 15:07:33 -0700 Subject: [PATCH] Revert "Merge pull request #93972 from Hilderin/fix-editor-needs-restart-after-adding-gdextensions" This reverts commit f0ee0bdd376c7d32adb83754e4ae5899dba37f91, reversing changes made to b310e5e064ab44d594e1aca148e3ca41af3fb15a. --- core/extension/gdextension_manager.cpp | 94 ++------------------------ core/extension/gdextension_manager.h | 3 +- core/object/class_db.cpp | 16 ----- core/object/class_db.h | 1 - doc/classes/GDExtensionManager.xml | 14 ---- editor/editor_file_system.cpp | 69 +++++++++++++++---- editor/editor_file_system.h | 2 +- editor/editor_node.cpp | 3 - modules/gdscript/gdscript.cpp | 53 +-------------- modules/gdscript/gdscript.h | 7 -- 10 files changed, 65 insertions(+), 197 deletions(-) diff --git a/core/extension/gdextension_manager.cpp b/core/extension/gdextension_manager.cpp index fff938858fd3..eeae6b19961b 100644 --- a/core/extension/gdextension_manager.cpp +++ b/core/extension/gdextension_manager.cpp @@ -32,18 +32,14 @@ #include "core/extension/gdextension_compat_hashes.h" #include "core/extension/gdextension_library_loader.h" -#include "core/io/dir_access.h" #include "core/io/file_access.h" #include "core/object/script_language.h" -GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref &p_extension, bool p_first_load) { +GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref &p_extension) { if (level >= 0) { // Already initialized up to some level. - int32_t minimum_level = 0; - if (!p_first_load) { - minimum_level = p_extension->get_minimum_library_initialization_level(); - if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) { - return LOAD_STATUS_NEEDS_RESTART; - } + int32_t minimum_level = p_extension->get_minimum_library_initialization_level(); + if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) { + return LOAD_STATUS_NEEDS_RESTART; } // Initialize up to current level. for (int32_t i = minimum_level; i <= level; i++) { @@ -55,20 +51,10 @@ GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(cons gdextension_class_icon_paths[kv.key] = kv.value; } -#ifdef TOOLS_ENABLED - // Signals that a new extension is loaded so GDScript can register new class names. - emit_signal("extension_loaded", p_extension); -#endif - return LOAD_STATUS_OK; } GDExtensionManager::LoadStatus GDExtensionManager::_unload_extension_internal(const Ref &p_extension) { -#ifdef TOOLS_ENABLED - // Signals that a new extension is unloading so GDScript can unregister class names. - emit_signal("extension_unloading", p_extension); -#endif - if (level >= 0) { // Already initialized up to some level. // Deinitialize down from current level. for (int32_t i = level; i >= GDExtension::INITIALIZATION_LEVEL_CORE; i--) { @@ -103,7 +89,7 @@ GDExtensionManager::LoadStatus GDExtensionManager::load_extension_with_loader(co return LOAD_STATUS_FAILED; } - LoadStatus status = _load_extension_internal(extension, true); + LoadStatus status = _load_extension_internal(extension); if (status != LOAD_STATUS_OK) { return status; } @@ -149,7 +135,7 @@ GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String return LOAD_STATUS_FAILED; } - status = _load_extension_internal(extension, false); + status = _load_extension_internal(extension); if (status != LOAD_STATUS_OK) { return status; } @@ -288,72 +274,6 @@ void GDExtensionManager::reload_extensions() { #endif } -bool GDExtensionManager::ensure_extensions_loaded(const HashSet &p_extensions) { - Vector extensions_added; - Vector extensions_removed; - - for (const String &E : p_extensions) { - if (!is_extension_loaded(E)) { - extensions_added.push_back(E); - } - } - - Vector loaded_extensions = get_loaded_extensions(); - for (const String &loaded_extension : loaded_extensions) { - if (!p_extensions.has(loaded_extension)) { - // The extension may not have a .gdextension file. - const Ref extension = GDExtensionManager::get_singleton()->get_extension(loaded_extension); - if (!extension->get_loader()->library_exists()) { - extensions_removed.push_back(loaded_extension); - } - } - } - - String extension_list_config_file = GDExtension::get_extension_list_config_file(); - if (p_extensions.size()) { - if (extensions_added.size() || extensions_removed.size()) { - // Extensions were added or removed. - Ref f = FileAccess::open(extension_list_config_file, FileAccess::WRITE); - for (const String &E : p_extensions) { - f->store_line(E); - } - } - } else { - if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { - // Extensions were removed. - Ref da = DirAccess::create(DirAccess::ACCESS_RESOURCES); - da->remove(extension_list_config_file); - } - } - - bool needs_restart = false; - for (const String &extension : extensions_added) { - GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->load_extension(extension); - if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) { - needs_restart = true; - } - } - - for (const String &extension : extensions_removed) { - GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->unload_extension(extension); - if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) { - needs_restart = true; - } - } - -#ifdef TOOLS_ENABLED - if (extensions_added.size() || extensions_removed.size()) { - // Emitting extensions_reloaded so EditorNode can reload Inspector and regenerate documentation. - emit_signal("extensions_reloaded"); - - // Reload all scripts to clear out old references. - callable_mp_static(&GDExtensionManager::_reload_all_scripts).call_deferred(); - } -#endif - - return needs_restart; -} - GDExtensionManager *GDExtensionManager::get_singleton() { return singleton; } @@ -374,8 +294,6 @@ void GDExtensionManager::_bind_methods() { BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART); ADD_SIGNAL(MethodInfo("extensions_reloaded")); - ADD_SIGNAL(MethodInfo("extension_loaded", PropertyInfo(Variant::OBJECT, "extension", PROPERTY_HINT_RESOURCE_TYPE, "GDExtension"))); - ADD_SIGNAL(MethodInfo("extension_unloading", PropertyInfo(Variant::OBJECT, "extension", PROPERTY_HINT_RESOURCE_TYPE, "GDExtension"))); } GDExtensionManager *GDExtensionManager::singleton = nullptr; diff --git a/core/extension/gdextension_manager.h b/core/extension/gdextension_manager.h index 39a600474cda..b48818960483 100644 --- a/core/extension/gdextension_manager.h +++ b/core/extension/gdextension_manager.h @@ -54,7 +54,7 @@ class GDExtensionManager : public Object { }; private: - LoadStatus _load_extension_internal(const Ref &p_extension, bool p_first_load); + LoadStatus _load_extension_internal(const Ref &p_extension); LoadStatus _unload_extension_internal(const Ref &p_extension); #ifdef TOOLS_ENABLED @@ -85,7 +85,6 @@ class GDExtensionManager : public Object { void load_extensions(); void reload_extensions(); - bool ensure_extensions_loaded(const HashSet &p_extensions); GDExtensionManager(); ~GDExtensionManager(); diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 9826d73a9dda..109c0bb16a40 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -267,22 +267,6 @@ void ClassDB::get_extensions_class_list(List *p_classes) { p_classes->sort_custom(); } - -void ClassDB::get_extension_class_list(const Ref &p_extension, List *p_classes) { - OBJTYPE_RLOCK; - - for (const KeyValue &E : classes) { - if (E.value.api != API_EXTENSION && E.value.api != API_EDITOR_EXTENSION) { - continue; - } - if (!E.value.gdextension || E.value.gdextension->library != p_extension.ptr()) { - continue; - } - p_classes->push_back(E.key); - } - - p_classes->sort_custom(); -} #endif void ClassDB::get_inheriters_from_class(const StringName &p_class, List *p_classes) { diff --git a/core/object/class_db.h b/core/object/class_db.h index 81100d7586b8..3fcc8c2c0027 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -289,7 +289,6 @@ class ClassDB { static void get_class_list(List *p_classes); #ifdef TOOLS_ENABLED static void get_extensions_class_list(List *p_classes); - static void get_extension_class_list(const Ref &p_extension, List *p_classes); static ObjectGDExtension *get_placeholder_extension(const StringName &p_class); #endif static void get_inheriters_from_class(const StringName &p_class, List *p_classes); diff --git a/doc/classes/GDExtensionManager.xml b/doc/classes/GDExtensionManager.xml index 97d2d08752bc..211bc023c0e3 100644 --- a/doc/classes/GDExtensionManager.xml +++ b/doc/classes/GDExtensionManager.xml @@ -56,20 +56,6 @@ - - - - Emitted after the editor has finished loading a new extension. - [b]Note:[/b] This signal is only emitted in editor builds. - - - - - - Emitted before the editor starts unloading an extension. - [b]Note:[/b] This signal is only emitted in editor builds. - - Emitted after the editor has finished reloading one or more extensions. diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 2d1a91412008..6d9c5a3e1933 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -243,27 +243,18 @@ void EditorFileSystem::_first_scan_filesystem() { first_scan_root_dir = memnew(ScannedDirectory); first_scan_root_dir->full_path = "res://"; HashSet existing_class_names; - HashSet extensions; ep.step(TTR("Scanning file structure..."), 0, true); nb_files_total = _scan_new_dir(first_scan_root_dir, d); // This loads the global class names from the scripts and ensures that even if the // global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer. - // At the same time, to prevent looping multiple times in all files, it looks for extensions. ep.step(TTR("Loading global class names..."), 1, true); - _first_scan_process_scripts(first_scan_root_dir, existing_class_names, extensions); + _first_scan_process_scripts(first_scan_root_dir, existing_class_names); // Removing invalid global class to prevent having invalid paths in ScriptServer. _remove_invalid_global_class_names(existing_class_names); - // Processing extensions to add new extensions or remove invalid ones. - // Important to do it in the first scan so custom types, new class names, custom importers, etc... - // from extensions are ready to go before plugins, autoloads and resources validation/importation. - // At this point, a restart of the editor should not be needed so we don't use the return value. - ep.step(TTR("Verifying GDExtensions..."), 2, true); - GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions); - // Now that all the global class names should be loaded, create autoloads and plugins. // This is done after loading the global class names because autoloads and plugins can use // global class names. @@ -276,9 +267,9 @@ void EditorFileSystem::_first_scan_filesystem() { ep.step(TTR("Starting file scan..."), 5, true); } -void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet &p_existing_class_names, HashSet &p_extensions) { +void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet &p_existing_class_names) { for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) { - _first_scan_process_scripts(scan_sub_dir, p_existing_class_names, p_extensions); + _first_scan_process_scripts(scan_sub_dir, p_existing_class_names); } for (const String &scan_file : p_scan_dir->files) { @@ -309,8 +300,6 @@ void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_sca if (!script_class_name.is_empty()) { p_existing_class_names.insert(script_class_name); } - } else if (type == SNAME("GDExtension")) { - p_extensions.insert(path); } } } @@ -3300,7 +3289,57 @@ bool EditorFileSystem::_scan_extensions() { _scan_extensions_dir(d, extensions); - return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions); + //verify against loaded extensions + + Vector extensions_added; + Vector extensions_removed; + + for (const String &E : extensions) { + if (!GDExtensionManager::get_singleton()->is_extension_loaded(E)) { + extensions_added.push_back(E); + } + } + + Vector loaded_extensions = GDExtensionManager::get_singleton()->get_loaded_extensions(); + for (int i = 0; i < loaded_extensions.size(); i++) { + if (!extensions.has(loaded_extensions[i])) { + // The extension may not have a .gdextension file. + if (!FileAccess::exists(loaded_extensions[i])) { + extensions_removed.push_back(loaded_extensions[i]); + } + } + } + + String extension_list_config_file = GDExtension::get_extension_list_config_file(); + if (extensions.size()) { + if (extensions_added.size() || extensions_removed.size()) { //extensions were added or removed + Ref f = FileAccess::open(extension_list_config_file, FileAccess::WRITE); + for (const String &E : extensions) { + f->store_line(E); + } + } + } else { + if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { //extensions were removed + Ref da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + da->remove(extension_list_config_file); + } + } + + bool needs_restart = false; + for (int i = 0; i < extensions_added.size(); i++) { + GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->load_extension(extensions_added[i]); + if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) { + needs_restart = true; + } + } + for (int i = 0; i < extensions_removed.size(); i++) { + GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->unload_extension(extensions_removed[i]); + if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) { + needs_restart = true; + } + } + + return needs_restart; } void EditorFileSystem::_bind_methods() { diff --git a/editor/editor_file_system.h b/editor/editor_file_system.h index 7aa0137f4ed2..65fa642041fc 100644 --- a/editor/editor_file_system.h +++ b/editor/editor_file_system.h @@ -191,7 +191,7 @@ class EditorFileSystem : public Node { void _scan_filesystem(); void _first_scan_filesystem(); - void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet &p_existing_class_names, HashSet &p_extensions); + void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet &p_existing_class_names); HashSet late_update_files; diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index d88fb134f162..632cbab04b64 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -485,9 +485,6 @@ void EditorNode::_gdextensions_reloaded() { // In case the developer is inspecting an object that will be changed by the reload. InspectorDock::get_inspector_singleton()->update_tree(); - // Reload script editor to revalidate GDScript if classes are added or removed. - ScriptEditor::get_singleton()->reload_scripts(true); - // Regenerate documentation. EditorHelp::generate_doc(); } diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 7b9aa70686a7..2917fb4a9f7a 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -57,7 +57,6 @@ #include "scene/scene_string_names.h" #ifdef TOOLS_ENABLED -#include "core/extension/gdextension_manager.h" #include "editor/editor_paths.h" #endif @@ -2184,26 +2183,9 @@ void GDScriptLanguage::_add_global(const StringName &p_name, const Variant &p_va global_array.write[globals[p_name]] = p_value; return; } - - if (global_array_empty_indexes.size()) { - int index = global_array_empty_indexes[global_array_empty_indexes.size() - 1]; - globals[p_name] = index; - global_array.write[index] = p_value; - global_array_empty_indexes.resize(global_array_empty_indexes.size() - 1); - } else { - globals[p_name] = global_array.size(); - global_array.push_back(p_value); - _global_array = global_array.ptrw(); - } -} - -void GDScriptLanguage::_remove_global(const StringName &p_name) { - if (!globals.has(p_name)) { - return; - } - global_array_empty_indexes.push_back(globals[p_name]); - global_array.write[globals[p_name]] = Variant::NIL; - globals.erase(p_name); + globals[p_name] = global_array.size(); + global_array.push_back(p_value); + _global_array = global_array.ptrw(); } void GDScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) { @@ -2261,40 +2243,11 @@ void GDScriptLanguage::init() { _add_global(E.name, E.ptr); } -#ifdef TOOLS_ENABLED - if (Engine::get_singleton()->is_editor_hint()) { - GDExtensionManager::get_singleton()->connect("extension_loaded", callable_mp(this, &GDScriptLanguage::_extension_loaded)); - GDExtensionManager::get_singleton()->connect("extension_unloading", callable_mp(this, &GDScriptLanguage::_extension_unloading)); - } -#endif - #ifdef TESTS_ENABLED GDScriptTests::GDScriptTestRunner::handle_cmdline(); #endif } -#ifdef TOOLS_ENABLED -void GDScriptLanguage::_extension_loaded(const Ref &p_extension) { - List class_list; - ClassDB::get_extension_class_list(p_extension, &class_list); - for (const StringName &n : class_list) { - if (globals.has(n)) { - continue; - } - Ref nc = memnew(GDScriptNativeClass(n)); - _add_global(n, nc); - } -} - -void GDScriptLanguage::_extension_unloading(const Ref &p_extension) { - List class_list; - ClassDB::get_extension_class_list(p_extension, &class_list); - for (const StringName &n : class_list) { - _remove_global(n); - } -} -#endif - String GDScriptLanguage::get_type() const { return "GDScript"; } diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 9bb39aac0f7e..4ff57d1c2e3b 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -417,7 +417,6 @@ class GDScriptLanguage : public ScriptLanguage { Vector global_array; HashMap globals; HashMap named_globals; - Vector global_array_empty_indexes; struct CallLevel { Variant *stack = nullptr; @@ -449,7 +448,6 @@ class GDScriptLanguage : public ScriptLanguage { int _debug_max_call_stack = 0; void _add_global(const StringName &p_name, const Variant &p_value); - void _remove_global(const StringName &p_name); friend class GDScriptInstance; @@ -469,11 +467,6 @@ class GDScriptLanguage : public ScriptLanguage { HashMap orphan_subclasses; -#ifdef TOOLS_ENABLED - void _extension_loaded(const Ref &p_extension); - void _extension_unloading(const Ref &p_extension); -#endif - public: int calls;