Skip to content

Commit 5aa3933

Browse files
committed
Optimize enumeration of global classes in create dialog and autocomplete
1 parent 742fb47 commit 5aa3933

6 files changed

+61
-66
lines changed

editor/connections_dialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ void ConnectionsDock::update_tree() {
14351435
doc_class_name = String();
14361436
}
14371437

1438-
class_icon = editor_data.get_script_icon(script_base);
1438+
class_icon = editor_data.get_script_icon(script_base->get_path());
14391439
if (class_icon.is_null() && has_theme_icon(native_base, EditorStringName(EditorIcons))) {
14401440
class_icon = get_editor_theme_icon(native_base);
14411441
}

editor/create_dialog.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,9 @@ void CreateDialog::_add_type(const StringName &p_type, TypeCategory p_type_categ
231231
inherits = ClassDB::get_parent_class(p_type);
232232
inherited_type = TypeCategory::CPP_TYPE;
233233
} else {
234-
if (p_type_category == TypeCategory::PATH_TYPE || ScriptServer::is_global_class(p_type)) {
235-
Ref<Script> scr;
236-
if (p_type_category == TypeCategory::PATH_TYPE) {
237-
ERR_FAIL_COND(!ResourceLoader::exists(p_type, "Script"));
238-
scr = ResourceLoader::load(p_type, "Script");
239-
} else {
240-
scr = EditorNode::get_editor_data().script_class_load_script(p_type);
241-
}
234+
if (p_type_category == TypeCategory::PATH_TYPE) {
235+
ERR_FAIL_COND(!ResourceLoader::exists(p_type, "Script"));
236+
Ref<Script> scr = ResourceLoader::load(p_type, "Script");
242237
ERR_FAIL_COND(scr.is_null());
243238

244239
Ref<Script> base = scr->get_base_script();
@@ -260,6 +255,10 @@ void CreateDialog::_add_type(const StringName &p_type, TypeCategory p_type_categ
260255
inherited_type = TypeCategory::PATH_TYPE;
261256
}
262257
}
258+
} else if (ScriptServer::is_global_class(p_type)) {
259+
inherits = ScriptServer::get_global_class_base(p_type);
260+
bool is_native_class = ClassDB::class_exists(inherits);
261+
inherited_type = is_native_class ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE;
263262
} else {
264263
inherits = custom_type_parents[p_type];
265264
if (ClassDB::class_exists(inherits)) {

editor/editor_data.cpp

+41-43
Original file line numberDiff line numberDiff line change
@@ -984,20 +984,6 @@ bool EditorData::script_class_is_parent(const String &p_class, const String &p_i
984984
return true;
985985
}
986986

987-
StringName EditorData::script_class_get_base(const String &p_class) const {
988-
Ref<Script> script = script_class_load_script(p_class);
989-
if (script.is_null()) {
990-
return StringName();
991-
}
992-
993-
Ref<Script> base_script = script->get_base_script();
994-
if (base_script.is_null()) {
995-
return ScriptServer::get_global_class_base(p_class);
996-
}
997-
998-
return script->get_language()->get_global_class_name(base_script->get_path());
999-
}
1000-
1001987
Variant EditorData::script_class_instance(const String &p_class) {
1002988
if (ScriptServer::is_global_class(p_class)) {
1003989
Ref<Script> script = script_class_load_script(p_class);
@@ -1026,22 +1012,25 @@ void EditorData::script_class_set_icon_path(const String &p_class, const String
10261012
_script_class_icon_paths[p_class] = p_icon_path;
10271013
}
10281014

1029-
String EditorData::script_class_get_icon_path(const String &p_class) const {
1030-
if (!ScriptServer::is_global_class(p_class)) {
1031-
return String();
1032-
}
1033-
1015+
String EditorData::script_class_get_icon_path(const String &p_class, bool *r_valid) const {
10341016
String current = p_class;
1035-
String ret = _script_class_icon_paths[current];
1036-
while (ret.is_empty()) {
1037-
current = script_class_get_base(current);
1017+
while (true) {
10381018
if (!ScriptServer::is_global_class(current)) {
1019+
// If the classnames chain has a native class ancestor, we're done with success.
1020+
if (r_valid) {
1021+
*r_valid = ClassDB::class_exists(current);
1022+
}
10391023
return String();
10401024
}
1041-
ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
1025+
HashMap<StringName, String>::ConstIterator E = _script_class_icon_paths.find(current);
1026+
if ((bool)E) {
1027+
if (r_valid) {
1028+
*r_valid = true;
1029+
}
1030+
return E->value;
1031+
}
1032+
current = ScriptServer::get_global_class_base(current);
10421033
}
1043-
1044-
return ret;
10451034
}
10461035

10471036
StringName EditorData::script_class_get_name(const String &p_path) const {
@@ -1126,58 +1115,67 @@ Ref<Texture2D> EditorData::_load_script_icon(const String &p_path) const {
11261115
return nullptr;
11271116
}
11281117

1129-
Ref<Texture2D> EditorData::get_script_icon(const Ref<Script> &p_script) {
1118+
Ref<Texture2D> EditorData::get_script_icon(const String &p_script_path) {
11301119
// Take from the local cache, if available.
1131-
if (_script_icon_cache.has(p_script)) {
1120+
if (_script_icon_cache.has(p_script_path)) {
11321121
// Can be an empty value if we can't resolve any icon for this script.
11331122
// An empty value is still cached to avoid unnecessary attempts at resolving it again.
1134-
return _script_icon_cache[p_script];
1123+
return _script_icon_cache[p_script_path];
1124+
}
1125+
1126+
// Fast path in case the whole hierarchy is made of global classes.
1127+
StringName class_name = script_class_get_name(p_script_path);
1128+
{
1129+
if (class_name != StringName()) {
1130+
bool icon_valid = false;
1131+
String icon_path = script_class_get_icon_path(class_name, &icon_valid);
1132+
if (icon_valid) {
1133+
Ref<Texture2D> icon = _load_script_icon(icon_path);
1134+
_script_icon_cache[p_script_path] = icon;
1135+
return icon;
1136+
}
1137+
}
11351138
}
11361139

1137-
Ref<Script> base_scr = p_script;
1140+
Ref<Script> base_scr = ResourceLoader::load(p_script_path, "Script");
11381141
while (base_scr.is_valid()) {
11391142
// Check for scripted classes.
11401143
String icon_path;
1141-
StringName class_name = script_class_get_name(base_scr->get_path());
1142-
if (base_scr->is_built_in() || class_name == StringName()) {
1144+
StringName base_class_name = script_class_get_name(base_scr->get_path());
1145+
if (base_scr->is_built_in() || base_class_name == StringName()) {
11431146
icon_path = base_scr->get_class_icon_path();
11441147
} else {
1145-
icon_path = script_class_get_icon_path(class_name);
1148+
icon_path = script_class_get_icon_path(base_class_name);
11461149
}
11471150

11481151
Ref<Texture2D> icon = _load_script_icon(icon_path);
11491152
if (icon.is_valid()) {
1150-
_script_icon_cache[p_script] = icon;
1153+
_script_icon_cache[p_script_path] = icon;
11511154
return icon;
11521155
}
11531156

11541157
// Check for legacy custom classes defined by plugins.
11551158
// TODO: Should probably be deprecated in 4.x
11561159
const EditorData::CustomType *ctype = get_custom_type_by_path(base_scr->get_path());
11571160
if (ctype && ctype->icon.is_valid()) {
1158-
_script_icon_cache[p_script] = ctype->icon;
1161+
_script_icon_cache[p_script_path] = ctype->icon;
11591162
return ctype->icon;
11601163
}
11611164

11621165
// Move to the base class.
11631166
base_scr = base_scr->get_base_script();
11641167
}
11651168

1166-
// No custom icon was found in the inheritance chain, so check the base
1167-
// class of the script instead.
1168-
String base_type;
1169-
p_script->get_language()->get_global_class_name(p_script->get_path(), &base_type);
1170-
11711169
// Check if the base type is an extension-defined type.
1172-
Ref<Texture2D> ext_icon = extension_class_get_icon(base_type);
1170+
Ref<Texture2D> ext_icon = extension_class_get_icon(class_name);
11731171
if (ext_icon.is_valid()) {
1174-
_script_icon_cache[p_script] = ext_icon;
1172+
_script_icon_cache[p_script_path] = ext_icon;
11751173
return ext_icon;
11761174
}
11771175

11781176
// If no icon found, cache it as null.
1179-
_script_icon_cache[p_script] = Ref<Texture>();
1180-
return nullptr;
1177+
_script_icon_cache[p_script_path] = Ref<Texture2D>();
1178+
return Ref<Texture2D>();
11811179
}
11821180

11831181
void EditorData::clear_script_icon_cache() {

editor/editor_data.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class EditorData {
146146

147147
HashMap<StringName, String> _script_class_icon_paths;
148148
HashMap<String, StringName> _script_class_file_to_path;
149-
HashMap<Ref<Script>, Ref<Texture>> _script_icon_cache;
149+
HashMap<String, Ref<Texture>> _script_icon_cache;
150150

151151
Ref<Texture2D> _load_script_icon(const String &p_path) const;
152152

@@ -240,23 +240,22 @@ class EditorData {
240240
void notify_scene_saved(const String &p_path);
241241

242242
bool script_class_is_parent(const String &p_class, const String &p_inherits);
243-
StringName script_class_get_base(const String &p_class) const;
244243
Variant script_class_instance(const String &p_class);
245244

246245
Ref<Script> script_class_load_script(const String &p_class) const;
247246

248247
StringName script_class_get_name(const String &p_path) const;
249248
void script_class_set_name(const String &p_path, const StringName &p_class);
250249

251-
String script_class_get_icon_path(const String &p_class) const;
250+
String script_class_get_icon_path(const String &p_class, bool *r_valid = nullptr) const;
252251
void script_class_set_icon_path(const String &p_class, const String &p_icon_path);
253252
void script_class_clear_icon_paths() { _script_class_icon_paths.clear(); }
254253
void script_class_save_icon_paths();
255254
void script_class_load_icon_paths();
256255

257256
Ref<Texture2D> extension_class_get_icon(const String &p_class) const;
258257

259-
Ref<Texture2D> get_script_icon(const Ref<Script> &p_script);
258+
Ref<Texture2D> get_script_icon(const String &p_script_path);
260259
void clear_script_icon_cache();
261260

262261
EditorData();

editor/editor_node.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -4823,22 +4823,21 @@ void EditorNode::_pick_main_scene_custom_action(const String &p_custom_action_na
48234823
}
48244824
}
48254825

4826-
Ref<Texture2D> EditorNode::_get_class_or_script_icon(const String &p_class, const Ref<Script> &p_script, const String &p_fallback, bool p_fallback_script_to_theme) {
4826+
Ref<Texture2D> EditorNode::_get_class_or_script_icon(const String &p_class, const String &p_script_path, const String &p_fallback, bool p_fallback_script_to_theme) {
48274827
ERR_FAIL_COND_V_MSG(p_class.is_empty(), nullptr, "Class name cannot be empty.");
48284828
EditorData &ed = EditorNode::get_editor_data();
48294829

48304830
// Check for a script icon first.
4831-
if (p_script.is_valid()) {
4832-
Ref<Texture2D> script_icon = ed.get_script_icon(p_script);
4831+
if (!p_script_path.is_empty()) {
4832+
Ref<Texture2D> script_icon = ed.get_script_icon(p_script_path);
48334833
if (script_icon.is_valid()) {
48344834
return script_icon;
48354835
}
48364836

48374837
if (p_fallback_script_to_theme) {
48384838
// Look for the native base type in the editor theme. This is relevant for
48394839
// scripts extending other scripts and for built-in classes.
4840-
String script_class_name = p_script->get_language()->get_global_class_name(p_script->get_path());
4841-
String base_type = ScriptServer::get_global_class_native_base(script_class_name);
4840+
String base_type = ScriptServer::get_global_class_native_base(p_class);
48424841
if (theme.is_valid() && theme->has_icon(base_type, EditorStringName(EditorIcons))) {
48434842
return theme->get_icon(base_type, EditorStringName(EditorIcons));
48444843
}
@@ -4894,18 +4893,18 @@ Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String
48944893
scr = p_object;
48954894
}
48964895

4897-
return _get_class_or_script_icon(p_object->get_class(), scr, p_fallback);
4896+
return _get_class_or_script_icon(p_object->get_class(), scr.is_valid() ? scr->get_path() : String(), p_fallback);
48984897
}
48994898

49004899
Ref<Texture2D> EditorNode::get_class_icon(const String &p_class, const String &p_fallback) {
49014900
ERR_FAIL_COND_V_MSG(p_class.is_empty(), nullptr, "Class name cannot be empty.");
49024901

4903-
Ref<Script> scr;
4902+
String script_path;
49044903
if (ScriptServer::is_global_class(p_class)) {
4905-
scr = EditorNode::get_editor_data().script_class_load_script(p_class);
4904+
script_path = ScriptServer::get_global_class_path(p_class);
49064905
}
49074906

4908-
return _get_class_or_script_icon(p_class, scr, p_fallback, true);
4907+
return _get_class_or_script_icon(p_class, script_path, p_fallback, true);
49094908
}
49104909

49114910
bool EditorNode::is_object_of_custom_type(const Object *p_object, const StringName &p_class) {

editor/editor_node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ class EditorNode : public Node {
675675
void _feature_profile_changed();
676676
bool _is_class_editor_disabled_by_feature_profile(const StringName &p_class);
677677

678-
Ref<Texture2D> _get_class_or_script_icon(const String &p_class, const Ref<Script> &p_script, const String &p_fallback = "Object", bool p_fallback_script_to_theme = false);
678+
Ref<Texture2D> _get_class_or_script_icon(const String &p_class, const String &p_script_path, const String &p_fallback = "Object", bool p_fallback_script_to_theme = false);
679679

680680
void _pick_main_scene_custom_action(const String &p_custom_action_name);
681681

0 commit comments

Comments
 (0)