Debian Codegraph @

editor/debugger/editor_debugger_inspector.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_debugger_inspector.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "editor_debugger_inspector.h"
32
33 #include "core/debugger/debugger_marshalls.h"
34 #include "core/io/marshalls.h"
35 #include "editor/docks/inspector_dock.h"
36 #include "editor/editor_node.h"
37 #include "editor/editor_undo_redo_manager.h"
38 #include "scene/debugger/scene_debugger.h"
39
40 bool EditorDebuggerRemoteObjects::_set(const StringName &p_name, const Variant &p_value) {
41 return _set_impl(p_name, p_value, "");
42 }
43
44 bool EditorDebuggerRemoteObjects::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
45 String name = p_name;
46 if (!prop_values.has(name) || String(name).begins_with("Constants/")) {
47 return false;
48 }
49
50 // Change it back to the real name when fetching.
51 if (name == "Script") {
52 name = "script";
53 } else if (name.begins_with("Metadata/")) {
54 name = name.replace_first("Metadata/", "metadata/");
55 }
56
57 Dictionary &values = prop_values[p_name];
58 Dictionary old_values = values.duplicate();
59 for (const uint64_t key : values.keys()) {
60 values.set(key, p_value);
61 }
62
63 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
64 const int size = remote_object_ids.size();
65 ur->create_action(size == 1 ? vformat(TTR("Set %s"), name) : vformat(TTR("Set %s on %d objects"), name, size), UndoRedo::MERGE_ENDS);
66
67 ur->add_do_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, values, p_field);
68 ur->add_undo_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, old_values, p_field);
69 ur->commit_action();
70
71 return true;
72 }
73
74 bool EditorDebuggerRemoteObjects::_get(const StringName &p_name, Variant &r_ret) const {
75 String name = p_name;
76 if (!prop_values.has(name)) {
77 return false;
78 }
79
80 // Change it back to the real name when fetching.
81 if (name == "Script") {
82 name = "script";
83 } else if (name.begins_with("Metadata/")) {
84 name = name.replace_first("Metadata/", "metadata/");
85 }
86
87 r_ret = prop_values[p_name][remote_object_ids[0]];
88 return true;
89 }
90
91 void EditorDebuggerRemoteObjects::_get_property_list(List<PropertyInfo> *p_list) const {
92 p_list->clear(); // Sorry, don't want any categories.
93 for (const PropertyInfo &prop : prop_list) {
94 p_list->push_back(prop);
95 }
96 }
97
98 void EditorDebuggerRemoteObjects::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
99 // Ignore the field with arrays and dictionaries, as they are passed whole when edited.
100 Variant::Type type = p_value.get_type();
101 if (type == Variant::ARRAY || type == Variant::DICTIONARY) {
102 _set_impl(p_property, p_value, "");
103 } else {
104 _set_impl(p_property, p_value, p_field);
105 }
106 }
107
108 String EditorDebuggerRemoteObjects::get_title() {
109 if (!remote_object_ids.is_empty() && ObjectID(remote_object_ids[0].operator uint64_t()).is_valid()) {
110 const int size = remote_object_ids.size();
111 return size == 1 ? vformat(TTR("Remote %s: %d"), type_name, remote_object_ids[0]) : vformat(TTR("Remote %s (%d Selected)"), type_name, size);
112 }
113
114 return "<null>";
115 }
116
117 Variant EditorDebuggerRemoteObjects::get_variant(const StringName &p_name) {
118 Variant var;
119 _get(p_name, var);
120 return var;
121 }
122
123 void EditorDebuggerRemoteObjects::_bind_methods() {
124 ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObjects::get_title);
125 ClassDB::bind_method("_hide_script_from_inspector", &EditorDebuggerRemoteObjects::_hide_script_from_inspector);
126 ClassDB::bind_method("_hide_metadata_from_inspector", &EditorDebuggerRemoteObjects::_hide_metadata_from_inspector);
127
128 ADD_SIGNAL(MethodInfo("values_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "uint64_t:Variant"), PropertyInfo(Variant::STRING, "field")));
129 }
130
131 /// EditorDebuggerInspector
132
133 EditorDebuggerInspector::EditorDebuggerInspector() {
134 variables = memnew(EditorDebuggerRemoteObjects);
135 }
136
137 EditorDebuggerInspector::~EditorDebuggerInspector() {
138 clear_cache();
139 memdelete(variables);
140 }
141
142 void EditorDebuggerInspector::_bind_methods() {
143 ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
144 ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::ARRAY, "ids"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value"), PropertyInfo(Variant::STRING, "field")));
145 ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
146 }
147
148 void EditorDebuggerInspector::_notification(int p_what) {
149 switch (p_what) {
150 case NOTIFICATION_POSTINITIALIZE: {
151 connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
152 } break;
153
154 case NOTIFICATION_ENTER_TREE: {
155 variables->remote_object_ids.append(0);
156 edit(variables);
157 } break;
158 }
159 }
160
161 void EditorDebuggerInspector::_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field) {
162 emit_signal(SNAME("objects_edited"), p_prop, p_values, p_field);
163 }
164
165 void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
166 emit_signal(SNAME("object_selected"), p_object);
167 }
168
169 EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr) {
170 ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);
171
172 TypedArray<uint64_t> ids;
173 LocalVector<SceneDebuggerObject> objects;
174 for (const Array arr : p_arr) {
175 SceneDebuggerObject obj;
176 obj.deserialize(arr);
177 if (obj.id.is_valid()) {
178 ids.push_back((uint64_t)obj.id);
179 objects.push_back(obj);
180 }
181 }
182 ERR_FAIL_COND_V(ids.is_empty(), nullptr);
183
184 // Sorting is necessary, as selected nodes in the remote tree are ordered by index.
185 ids.sort();
186
187 EditorDebuggerRemoteObjects *remote_objects = nullptr;
188 for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
189 if (robjs->remote_object_ids == ids) {
190 remote_objects = robjs;
191 break;
192 }
193 }
194
195 if (!remote_objects) {
196 remote_objects = memnew(EditorDebuggerRemoteObjects);
197 remote_objects->remote_object_ids = ids;
198 remote_objects->remote_object_ids.make_read_only();
199 remote_objects->connect("values_edited", callable_mp(this, &EditorDebuggerInspector::_objects_edited));
200 remote_objects_list.push_back(remote_objects);
201 }
202
203 StringName class_name = objects[0].class_name;
204 if (class_name != SNAME("Object")) {
205 // Search for the common class between all selected objects.
206 bool check_type_again = true;
207 while (check_type_again) {
208 check_type_again = false;
209
210 if (class_name == SNAME("Object") || class_name == StringName()) {
211 // All objects inherit from Object, so no need to continue checking.
212 class_name = SNAME("Object");
213 break;
214 }
215
216 // Check that all objects inherit from type_name.
217 for (const SceneDebuggerObject &obj : objects) {
218 if (obj.class_name == class_name || ClassDB::is_parent_class(obj.class_name, class_name)) {
219 continue; // class_name is the same or a parent of the object's class.
220 }
221
222 // class_name is not a parent of the node's class, so check again with the parent class.
223 class_name = ClassDB::get_parent_class(class_name);
224 check_type_again = true;
225 break;
226 }
227 }
228 }
229 remote_objects->type_name = class_name;
230
231 // Search for properties that are present in all selected objects.
232 struct UsageData {
233 int qty = 0;
234 SceneDebuggerObject::SceneDebuggerProperty prop;
235 TypedDictionary<uint64_t, Variant> values;
236 };
237 HashMap<String, UsageData> usage;
238 int nc = 0;
239 for (const SceneDebuggerObject &obj : objects) {
240 for (const SceneDebuggerObject::SceneDebuggerProperty &prop : obj.properties) {
241 PropertyInfo pinfo = prop.first;
242 // Rename those variables, so they don't conflict with the ones from the resource itself.
243 if (pinfo.name == "script") {
244 pinfo.name = "Script";
245 } else if (pinfo.name.begins_with("metadata/")) {
246 pinfo.name = pinfo.name.replace_first("metadata/", "Metadata/");
247 }
248
249 if (!usage.has(pinfo.name)) {
250 UsageData usage_dt;
251 usage_dt.prop = prop;
252 usage_dt.prop.first.name = pinfo.name;
253 usage_dt.values[obj.id] = prop.second;
254 usage[pinfo.name] = usage_dt;
255 }
256
257 // Make sure only properties with the same exact PropertyInfo data will appear.
258 if (usage[pinfo.name].prop.first == pinfo) {
259 usage[pinfo.name].qty++;
260 usage[pinfo.name].values[obj.id] = prop.second;
261 }
262 }
263
264 nc++;
265 }
266 for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {
267 HashMap<String, UsageData>::Iterator next = E;
268 ++next;
269
270 UsageData usage_dt = E->value;
271 if (nc != usage_dt.qty) {
272 // Doesn't appear on all of them, remove it.
273 usage.erase(E->key);
274 }
275
276 E = next;
277 }
278
279 int old_prop_size = remote_objects->prop_list.size();
280
281 remote_objects->prop_list.clear();
282 int new_props_added = 0;
283 HashSet<String> changed;
284 for (KeyValue<String, UsageData> &KV : usage) {
285 const PropertyInfo &pinfo = KV.value.prop.first;
286 Variant var = KV.value.values[remote_objects->remote_object_ids[0]];
287
288 if (pinfo.type == Variant::OBJECT && var.is_string()) {
289 String path = var;
290 if (path.contains("::")) {
291 // Built-in resource.
292 String base_path = path.get_slice("::", 0);
293 Ref<Resource> dependency = ResourceLoader::load(base_path);
294 if (dependency.is_valid()) {
295 remote_dependencies.insert(dependency);
296 }
297 }
298 var = ResourceLoader::load(path);
299 KV.value.values[remote_objects->remote_object_ids[0]] = var;
300 }
301
302 // Always add the property, since props may have been added or removed.
303 remote_objects->prop_list.push_back(pinfo);
304
305 if (!remote_objects->prop_values.has(pinfo.name)) {
306 new_props_added++;
307 } else if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, remote_objects->prop_values[pinfo.name], var))) {
308 changed.insert(pinfo.name);
309 }
310
311 remote_objects->prop_values[pinfo.name] = KV.value.values;
312 }
313
314 if (old_prop_size == remote_objects->prop_list.size() && new_props_added == 0) {
315 // Only some may have changed, if so, then update those, if they exist.
316 for (const String &E : changed) {
317 emit_signal(SNAME("object_property_updated"), remote_objects->get_instance_id(), E);
318 }
319 } else {
320 // Full update, because props were added or removed.
321 remote_objects->update();
322 }
323
324 return remote_objects;
325 }
326
327 void EditorDebuggerInspector::clear_remote_inspector() {
328 if (remote_objects_list.is_empty()) {
329 return;
330 }
331
332 const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
333 // Check if the inspector holds remote items, and take it out if so.
334 if (Object::cast_to<EditorDebuggerRemoteObjects>(obj)) {
335 EditorNode::get_singleton()->push_item(nullptr);
336 }
337 }
338
339 void EditorDebuggerInspector::clear_cache() {
340 clear_remote_inspector();
341
342 for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
343 memdelete(robjs);
344 }
345 remote_objects_list.clear();
346
347 remote_dependencies.clear();
348 }
349
350 void EditorDebuggerInspector::invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids) {
351 for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
352 if (robjs->remote_object_ids == p_ids) {
353 const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
354 if (obj == robjs) {
355 EditorNode::get_singleton()->push_item(nullptr);
356 }
357
358 remote_objects_list.erase(robjs);
359 memdelete(robjs);
360 break;
361 }
362 }
363 }
364
365 void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {
366 DebuggerMarshalls::ScriptStackVariable var;
367 var.deserialize(p_array);
368 String n = var.name;
369 Variant v = var.value;
370
371 PropertyHint h = PROPERTY_HINT_NONE;
372 String hs;
373
374 if (var.var_type == Variant::OBJECT && v) {
375 v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
376 h = PROPERTY_HINT_OBJECT_ID;
377 hs = "Object";
378 }
379 String type;
380 switch (var.type) {
381 case 0:
382 type = "Locals/";
383 break;
384 case 1:
385 if (n.begins_with("@")) {
386 return; // Skip groups.
387 }
388
389 type = "Members/";
390 break;
391 case 2:
392 type = "Globals/";
393 break;
394 case 3:
395 type = "Evaluated/";
396 break;
397 default:
398 type = "Unknown/";
399 }
400
401 PropertyInfo pinfo;
402 // Encode special characters to avoid issues with expressions in Evaluator.
403 // Dots are skipped by uri_encode(), but uri_decode() process them correctly when replaced with "%2E".
404 pinfo.name = type + n.uri_encode().replace(".", "%2E");
405 pinfo.type = v.get_type();
406 pinfo.hint = h;
407 pinfo.hint_string = hs;
408
409 if ((p_offset == -1) || variables->prop_list.is_empty()) {
410 variables->prop_list.push_back(pinfo);
411 } else {
412 List<PropertyInfo>::Element *current = variables->prop_list.front();
413 for (int i = 0; i < p_offset; i++) {
414 current = current->next();
415 }
416 variables->prop_list.insert_before(current, pinfo);
417 }
418 variables->prop_values[pinfo.name][0] = v;
419 variables->update();
420 edit(variables);
421 }
422
423 void EditorDebuggerInspector::clear_stack_variables() {
424 variables->clear();
425 variables->update();
426 }
427
428 String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
429 for (KeyValue<StringName, TypedDictionary<uint64_t, Variant>> &E : variables->prop_values) {
430 String v = E.key.operator String();
431 if (v.get_slicec('/', 1) == p_var) {
432 return variables->get_variant(v);
433 }
434 }
435 return String();
436 }