From 9c919ea28504ae67226a5c8df94227e9da8899b7 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 26 Jan 2024 13:03:32 +0100 Subject: [PATCH] Generate script resource preview without parsing --- core/object/script_language.cpp | 12 ++++++++ core/object/script_language.h | 1 + editor/plugins/editor_preview_plugins.cpp | 37 +++++++++++++++++------ editor/plugins/editor_preview_plugins.h | 5 +++ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 0b2d5e41cfc..3b9b1f90947 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -168,6 +168,18 @@ ScriptLanguage *ScriptServer::get_language(int p_idx) { return _languages[p_idx]; } +ScriptLanguage *ScriptServer::get_language_for_extension(const String &p_extension) { + MutexLock lock(languages_mutex); + + for (int i = 0; i < _language_count; i++) { + if (_languages[i] && _languages[i]->get_extension() == p_extension) { + return _languages[i]; + } + } + + return nullptr; +} + Error ScriptServer::register_language(ScriptLanguage *p_language) { MutexLock lock(languages_mutex); ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER); diff --git a/core/object/script_language.h b/core/object/script_language.h index bb714d5bc35..294231a3e7d 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -75,6 +75,7 @@ public: static bool is_scripting_enabled(); _FORCE_INLINE_ static int get_language_count() { return _language_count; } static ScriptLanguage *get_language(int p_idx); + static ScriptLanguage *get_language_for_extension(const String &p_extension); static Error register_language(ScriptLanguage *p_language); static Error unregister_language(const ScriptLanguage *p_language); diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 90bd117543b..0019922f9c1 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -464,6 +464,17 @@ bool EditorScriptPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Script"); } +Ref EditorScriptPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const { + Error err; + String code = FileAccess::get_file_as_string(p_path, &err); + if (err != OK) { + return Ref(); + } + + ScriptLanguage *lang = ScriptServer::get_language_for_extension(p_path.get_extension()); + return _generate_from_source_code(lang, code, p_size, p_metadata); +} + Ref EditorScriptPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size, Dictionary &p_metadata) const { Ref