From b83c64faacd317bd375b90b5f24cfd62cf72d96c Mon Sep 17 00:00:00 2001 From: kobewi Date: Sat, 29 Jun 2024 00:40:34 +0200 Subject: [PATCH] Speed up scene group scanning for text scenes --- editor/editor_file_system.cpp | 8 +---- editor/editor_file_system.h | 1 - scene/resources/packed_scene.cpp | 50 ++++++++++++++++++++++++++++++++ scene/resources/packed_scene.h | 1 + 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index f0dc850af02..3dc3c660ce5 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -1891,7 +1891,7 @@ void EditorFileSystem::_update_scene_groups() { continue; } - const HashSet scene_groups = _get_scene_groups(path); + const HashSet scene_groups = PackedScene::get_scene_groups(path); if (!scene_groups.is_empty()) { ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups); } @@ -1935,12 +1935,6 @@ void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet } } -HashSet EditorFileSystem::_get_scene_groups(const String &p_path) { - Ref packed_scene = ResourceLoader::load(p_path); - ERR_FAIL_COND_V(packed_scene.is_null(), HashSet()); - return packed_scene->get_state()->get_all_groups(); -} - void EditorFileSystem::update_file(const String &p_file) { ERR_FAIL_COND(p_file.is_empty()); update_files({ p_file }); diff --git a/editor/editor_file_system.h b/editor/editor_file_system.h index b0c6f0de510..aee40ed23de 100644 --- a/editor/editor_file_system.h +++ b/editor/editor_file_system.h @@ -290,7 +290,6 @@ class EditorFileSystem : public Node { void _queue_update_scene_groups(const String &p_path); void _update_scene_groups(); void _update_pending_scene_groups(); - HashSet _get_scene_groups(const String &p_path); void _get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet &r_list); String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const; diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp index b1742bd5a30..900629f5f80 100644 --- a/scene/resources/packed_scene.cpp +++ b/scene/resources/packed_scene.cpp @@ -2124,6 +2124,56 @@ void PackedScene::recreate_state() { #endif } +#ifdef TOOLS_ENABLED +HashSet PackedScene::get_scene_groups(const String &p_path) { + { + Ref packed_scene = ResourceCache::get_ref(p_path); + if (packed_scene.is_valid()) { + return packed_scene->get_state()->get_all_groups(); + } + } + + if (p_path.get_extension() == "tscn") { + Ref scene_file = FileAccess::open(p_path, FileAccess::READ); + ERR_FAIL_COND_V(scene_file.is_null(), HashSet()); + + HashSet ret; + while (!scene_file->eof_reached()) { + const String line = scene_file->get_line(); + if (!line.begins_with("[node")) { + continue; + } + + int i = line.find("groups=["); + if (i == -1) { + continue; + } + + int j = line.find_char(']', i); + while (i < j) { + i = line.find_char('"', i); + if (i == -1) { + break; + } + + int k = line.find_char('"', i + 1); + if (k == -1) { + break; + } + + ret.insert(line.substr(i + 1, k - i - 1)); + i = k + 1; + } + } + return ret; + } else { + Ref packed_scene = ResourceLoader::load(p_path); + ERR_FAIL_COND_V(packed_scene.is_null(), HashSet()); + return packed_scene->get_state()->get_all_groups(); + } +} +#endif + Ref PackedScene::get_state() const { return state; } diff --git a/scene/resources/packed_scene.h b/scene/resources/packed_scene.h index c46a4dd5fe8..e26b9f7b908 100644 --- a/scene/resources/packed_scene.h +++ b/scene/resources/packed_scene.h @@ -270,6 +270,7 @@ public: state->set_last_modified_time(p_time); } + static HashSet get_scene_groups(const String &p_path); #endif Ref get_state() const;