From 09043785158ac65e76bcedc1406b858b93855476 Mon Sep 17 00:00:00 2001 From: kobewi Date: Wed, 1 May 2024 00:26:42 +0200 Subject: [PATCH] Remove code duplication for adding global script class --- editor/editor_file_system.cpp | 54 +++++++++++++++++++---------------- editor/editor_file_system.h | 1 + editor/filesystem_dock.cpp | 29 +------------------ 3 files changed, 31 insertions(+), 53 deletions(-) diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 14190a43a500..b6f44fb72c7b 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -1578,31 +1578,7 @@ void EditorFileSystem::_update_script_classes() { update_script_mutex.lock(); for (const String &path : update_script_paths) { - ScriptServer::remove_global_class_by_path(path); // First remove, just in case it changed - - int index = -1; - EditorFileSystemDirectory *efd = find_file(path, &index); - - if (!efd || index < 0) { - // The file was removed - continue; - } - - if (!efd->files[index]->script_class_name.is_empty()) { - String lang; - for (int j = 0; j < ScriptServer::get_language_count(); j++) { - if (ScriptServer::get_language(j)->handles_global_class_type(efd->files[index]->type)) { - lang = ScriptServer::get_language(j)->get_name(); - } - } - if (lang.is_empty()) { - continue; // No lang found that can handle this global class - } - - ScriptServer::add_global_class(efd->files[index]->script_class_name, efd->files[index]->script_class_extends, lang, path); - EditorNode::get_editor_data().script_class_set_icon_path(efd->files[index]->script_class_name, efd->files[index]->script_class_icon_path); - EditorNode::get_editor_data().script_class_set_name(path, efd->files[index]->script_class_name); - } + EditorFileSystem::get_singleton()->register_global_class_script(path, path); } // Parse documentation second, as it requires the class names to be correct and registered @@ -1844,6 +1820,34 @@ HashSet EditorFileSystem::get_valid_extensions() const { return valid_extensions; } +void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) { + ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed + + int index = -1; + EditorFileSystemDirectory *efd = find_file(p_search_path, &index); + + if (!efd || index < 0) { + // The file was removed + return; + } + + if (!efd->files[index]->script_class_name.is_empty()) { + String lang; + for (int j = 0; j < ScriptServer::get_language_count(); j++) { + if (ScriptServer::get_language(j)->handles_global_class_type(efd->files[index]->type)) { + lang = ScriptServer::get_language(j)->get_name(); + } + } + if (lang.is_empty()) { + return; // No lang found that can handle this global class + } + + ScriptServer::add_global_class(efd->files[index]->script_class_name, efd->files[index]->script_class_extends, lang, p_target_path); + EditorNode::get_editor_data().script_class_set_icon_path(efd->files[index]->script_class_name, efd->files[index]->script_class_icon_path); + EditorNode::get_editor_data().script_class_set_name(p_target_path, efd->files[index]->script_class_name); + } +} + Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector &p_files) { String importer_name; diff --git a/editor/editor_file_system.h b/editor/editor_file_system.h index 782d3eee3843..f77eac1a008c 100644 --- a/editor/editor_file_system.h +++ b/editor/editor_file_system.h @@ -310,6 +310,7 @@ public: void scan_changes(); void update_file(const String &p_file); HashSet get_valid_extensions() const; + void register_global_class_script(const String &p_search_path, const String &p_target_path); EditorFileSystemDirectory *get_filesystem_path(const String &p_path); String get_file_type(const String &p_file) const; diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index 3f96d934a85a..ac4991755b70 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -1568,34 +1568,7 @@ void FileSystemDock::_update_resource_paths_after_move(const HashMapset_id(I->value, new_path); } - - ScriptServer::remove_global_class_by_path(old_path); - - int index = -1; - EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(old_path, &index); - - if (!efd || index < 0) { - // The file was removed. - continue; - } - - // Update paths for global classes. - if (!efd->get_file_script_class_name(index).is_empty()) { - String lang; - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - if (ScriptServer::get_language(i)->handles_global_class_type(efd->get_file_type(index))) { - lang = ScriptServer::get_language(i)->get_name(); - break; - } - } - if (lang.is_empty()) { - continue; // No language found that can handle this global class. - } - - ScriptServer::add_global_class(efd->get_file_script_class_name(index), efd->get_file_script_class_extends(index), lang, new_path); - EditorNode::get_editor_data().script_class_set_icon_path(efd->get_file_script_class_name(index), efd->get_file_script_class_icon_path(index)); - EditorNode::get_editor_data().script_class_set_name(new_path, efd->get_file_script_class_name(index)); - } + EditorFileSystem::get_singleton()->register_global_class_script(old_path, new_path); } // Rename all resources loaded, be it subresources or actual resources.