Skip to content

Commit

Permalink
Fix editor needs restart after adding GDExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilderin committed Aug 29, 2024
1 parent fd7239c commit ef6f873
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 65 deletions.
93 changes: 87 additions & 6 deletions core/extension/gdextension_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@

#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<GDExtension> &p_extension) {
GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref<GDExtension> &p_extension, bool p_first_load) {
if (level >= 0) { // Already initialized up to some level.
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;
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;
}
}
// Initialize up to current level.
for (int32_t i = minimum_level; i <= level; i++) {
Expand All @@ -51,10 +55,20 @@ 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<GDExtension> &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--) {
Expand Down Expand Up @@ -89,7 +103,7 @@ GDExtensionManager::LoadStatus GDExtensionManager::load_extension_with_loader(co
return LOAD_STATUS_FAILED;
}

LoadStatus status = _load_extension_internal(extension);
LoadStatus status = _load_extension_internal(extension, true);
if (status != LOAD_STATUS_OK) {
return status;
}
Expand Down Expand Up @@ -135,7 +149,7 @@ GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String
return LOAD_STATUS_FAILED;
}

status = _load_extension_internal(extension);
status = _load_extension_internal(extension, false);
if (status != LOAD_STATUS_OK) {
return status;
}
Expand Down Expand Up @@ -274,6 +288,71 @@ void GDExtensionManager::reload_extensions() {
#endif
}

bool GDExtensionManager::ensure_extensions_loaded(const HashSet<String> &p_extensions) {
Vector<String> extensions_added;
Vector<String> extensions_removed;

for (const String &E : p_extensions) {
if (!is_extension_loaded(E)) {
extensions_added.push_back(E);
}
}

Vector<String> 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.
if (!FileAccess::exists(loaded_extension)) {
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<FileAccess> 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<DirAccess> 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;
}
Expand All @@ -294,6 +373,8 @@ 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;
Expand Down
3 changes: 2 additions & 1 deletion core/extension/gdextension_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GDExtensionManager : public Object {
};

private:
LoadStatus _load_extension_internal(const Ref<GDExtension> &p_extension);
LoadStatus _load_extension_internal(const Ref<GDExtension> &p_extension, bool p_first_load);
LoadStatus _unload_extension_internal(const Ref<GDExtension> &p_extension);

#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -85,6 +85,7 @@ class GDExtensionManager : public Object {

void load_extensions();
void reload_extensions();
bool ensure_extensions_loaded(const HashSet<String> &p_extensions);

GDExtensionManager();
~GDExtensionManager();
Expand Down
16 changes: 16 additions & 0 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ void ClassDB::get_extensions_class_list(List<StringName> *p_classes) {

p_classes->sort_custom<StringName::AlphCompare>();
}

void ClassDB::get_extension_class_list(const Ref<GDExtension> &p_extension, List<StringName> *p_classes) {
OBJTYPE_RLOCK;

for (const KeyValue<StringName, ClassInfo> &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<StringName::AlphCompare>();
}
#endif

void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
Expand Down
1 change: 1 addition & 0 deletions core/object/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class ClassDB {
static void get_class_list(List<StringName> *p_classes);
#ifdef TOOLS_ENABLED
static void get_extensions_class_list(List<StringName> *p_classes);
static void get_extension_class_list(const Ref<GDExtension> &p_extension, List<StringName> *p_classes);
static ObjectGDExtension *get_placeholder_extension(const StringName &p_class);
#endif
static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
Expand Down
14 changes: 14 additions & 0 deletions doc/classes/GDExtensionManager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@
</method>
</methods>
<signals>
<signal name="extension_loaded">
<param index="0" name="extension" type="GDExtension" />
<description>
Emitted after the editor has finished loading a new extension.
[b]Note:[/b] This signal is only emitted in editor builds.
</description>
</signal>
<signal name="extension_unloading">
<param index="0" name="extension" type="GDExtension" />
<description>
Emitted before the editor starts unloading an extension.
[b]Note:[/b] This signal is only emitted in editor builds.
</description>
</signal>
<signal name="extensions_reloaded">
<description>
Emitted after the editor has finished reloading one or more extensions.
Expand Down
69 changes: 15 additions & 54 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,27 @@ void EditorFileSystem::_first_scan_filesystem() {
first_scan_root_dir = memnew(ScannedDirectory);
first_scan_root_dir->full_path = "res://";
HashSet<String> existing_class_names;
HashSet<String> 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);
_first_scan_process_scripts(first_scan_root_dir, existing_class_names, extensions);

// 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.
Expand All @@ -267,9 +276,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<String> &p_existing_class_names) {
void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions) {
for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
_first_scan_process_scripts(scan_sub_dir, p_existing_class_names);
_first_scan_process_scripts(scan_sub_dir, p_existing_class_names, p_extensions);
}

for (const String &scan_file : p_scan_dir->files) {
Expand All @@ -285,6 +294,8 @@ 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);
}
}
}
Expand Down Expand Up @@ -3016,57 +3027,7 @@ bool EditorFileSystem::_scan_extensions() {

_scan_extensions_dir(d, extensions);

//verify against loaded extensions

Vector<String> extensions_added;
Vector<String> extensions_removed;

for (const String &E : extensions) {
if (!GDExtensionManager::get_singleton()->is_extension_loaded(E)) {
extensions_added.push_back(E);
}
}

Vector<String> 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<FileAccess> 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<DirAccess> 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;
return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
}

void EditorFileSystem::_bind_methods() {
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class EditorFileSystem : public Node {

void _scan_filesystem();
void _first_scan_filesystem();
void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet<String> &p_existing_class_names);
void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions);

HashSet<String> late_update_files;

Expand Down
3 changes: 3 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ 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();
}
Expand Down
Loading

0 comments on commit ef6f873

Please sign in to comment.