editor_debugger_inspector.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include "editor_debugger_inspector.h"
  31. #include "core/debugger/debugger_marshalls.h"
  32. #include "core/io/marshalls.h"
  33. #include "editor/docks/inspector_dock.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "scene/debugger/scene_debugger.h"
  37. bool EditorDebuggerRemoteObjects::_set(const StringName &p_name, const Variant &p_value) {
  38. return _set_impl(p_name, p_value, "");
  39. }
  40. bool EditorDebuggerRemoteObjects::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
  41. String name = p_name;
  42. if (!prop_values.has(name) || String(name).begins_with("Constants/")) {
  43. return false;
  44. }
  45. // Change it back to the real name when fetching.
  46. if (name == "Script") {
  47. name = "script";
  48. } else if (name.begins_with("Metadata/")) {
  49. name = name.replace_first("Metadata/", "metadata/");
  50. }
  51. Dictionary &values = prop_values[p_name];
  52. Dictionary old_values = values.duplicate();
  53. for (const uint64_t key : values.keys()) {
  54. values.set(key, p_value);
  55. }
  56. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  57. const int size = remote_object_ids.size();
  58. ur->create_action(size == 1 ? vformat(TTR("Set %s"), name) : vformat(TTR("Set %s on %d objects"), name, size), UndoRedo::MERGE_ENDS);
  59. ur->add_do_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, values, p_field);
  60. ur->add_undo_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, old_values, p_field);
  61. ur->commit_action();
  62. return true;
  63. }
  64. bool EditorDebuggerRemoteObjects::_get(const StringName &p_name, Variant &r_ret) const {
  65. String name = p_name;
  66. if (!prop_values.has(name)) {
  67. return false;
  68. }
  69. // Change it back to the real name when fetching.
  70. if (name == "Script") {
  71. name = "script";
  72. } else if (name.begins_with("Metadata/")) {
  73. name = name.replace_first("Metadata/", "metadata/");
  74. }
  75. r_ret = prop_values[p_name][remote_object_ids[0]];
  76. return true;
  77. }
  78. void EditorDebuggerRemoteObjects::_get_property_list(List<PropertyInfo> *p_list) const {
  79. p_list->clear(); // Sorry, don't want any categories.
  80. for (const PropertyInfo &prop : prop_list) {
  81. p_list->push_back(prop);
  82. }
  83. }
  84. void EditorDebuggerRemoteObjects::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  85. _set_impl(p_property, p_value, p_field);
  86. }
  87. String EditorDebuggerRemoteObjects::get_title() {
  88. if (!remote_object_ids.is_empty() && ObjectID(remote_object_ids[0].operator uint64_t()).is_valid()) {
  89. const int size = remote_object_ids.size();
  90. return size == 1 ? vformat(TTR("Remote %s: %d"), type_name, remote_object_ids[0]) : vformat(TTR("Remote %s (%d Selected)"), type_name, size);
  91. }
  92. return "<null>";
  93. }
  94. Variant EditorDebuggerRemoteObjects::get_variant(const StringName &p_name) {
  95. Variant var;
  96. _get(p_name, var);
  97. return var;
  98. }
  99. void EditorDebuggerRemoteObjects::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObjects::get_title);
  101. ClassDB::bind_method("_hide_script_from_inspector", &EditorDebuggerRemoteObjects::_hide_script_from_inspector);
  102. ClassDB::bind_method("_hide_metadata_from_inspector", &EditorDebuggerRemoteObjects::_hide_metadata_from_inspector);
  103. ADD_SIGNAL(MethodInfo("values_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "uint64_t:Variant"), PropertyInfo(Variant::STRING, "field")));
  104. }
  105. /// EditorDebuggerInspector
  106. EditorDebuggerInspector::EditorDebuggerInspector() {
  107. variables = memnew(EditorDebuggerRemoteObjects);
  108. }
  109. EditorDebuggerInspector::~EditorDebuggerInspector() {
  110. clear_cache();
  111. memdelete(variables);
  112. }
  113. void EditorDebuggerInspector::_bind_methods() {
  114. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
  115. ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::ARRAY, "ids"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value"), PropertyInfo(Variant::STRING, "field")));
  116. ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  117. }
  118. void EditorDebuggerInspector::_notification(int p_what) {
  119. switch (p_what) {
  120. case NOTIFICATION_POSTINITIALIZE: {
  121. connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
  122. } break;
  123. case NOTIFICATION_ENTER_TREE: {
  124. variables->remote_object_ids.append(0);
  125. edit(variables);
  126. } break;
  127. }
  128. }
  129. void EditorDebuggerInspector::_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field) {
  130. emit_signal(SNAME("objects_edited"), p_prop, p_values, p_field);
  131. }
  132. void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
  133. emit_signal(SNAME("object_selected"), p_object);
  134. }
  135. EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr) {
  136. ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);
  137. TypedArray<uint64_t> ids;
  138. LocalVector<SceneDebuggerObject> objects;
  139. for (const Array arr : p_arr) {
  140. SceneDebuggerObject obj;
  141. obj.deserialize(arr);
  142. if (obj.id.is_valid()) {
  143. ids.push_back((uint64_t)obj.id);
  144. objects.push_back(obj);
  145. }
  146. }
  147. ERR_FAIL_COND_V(ids.is_empty(), nullptr);
  148. // Sorting is necessary, as selected nodes in the remote tree are ordered by index.
  149. ids.sort();
  150. EditorDebuggerRemoteObjects *remote_objects = nullptr;
  151. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  152. if (robjs->remote_object_ids == ids) {
  153. remote_objects = robjs;
  154. break;
  155. }
  156. }
  157. if (!remote_objects) {
  158. remote_objects = memnew(EditorDebuggerRemoteObjects);
  159. remote_objects->remote_object_ids = ids;
  160. remote_objects->remote_object_ids.make_read_only();
  161. remote_objects->connect("values_edited", callable_mp(this, &EditorDebuggerInspector::_objects_edited));
  162. remote_objects_list.push_back(remote_objects);
  163. }
  164. StringName class_name = objects[0].class_name;
  165. if (class_name != SNAME("Object")) {
  166. // Search for the common class between all selected objects.
  167. bool check_type_again = true;
  168. while (check_type_again) {
  169. check_type_again = false;
  170. if (class_name == SNAME("Object") || class_name == StringName()) {
  171. // All objects inherit from Object, so no need to continue checking.
  172. class_name = SNAME("Object");
  173. break;
  174. }
  175. // Check that all objects inherit from type_name.
  176. for (const SceneDebuggerObject &obj : objects) {
  177. if (obj.class_name == class_name || ClassDB::is_parent_class(obj.class_name, class_name)) {
  178. continue; // class_name is the same or a parent of the object's class.
  179. }
  180. // class_name is not a parent of the node's class, so check again with the parent class.
  181. class_name = ClassDB::get_parent_class(class_name);
  182. check_type_again = true;
  183. break;
  184. }
  185. }
  186. }
  187. remote_objects->type_name = class_name;
  188. // Search for properties that are present in all selected objects.
  189. struct UsageData {
  190. int qty = 0;
  191. SceneDebuggerObject::SceneDebuggerProperty prop;
  192. TypedDictionary<uint64_t, Variant> values;
  193. };
  194. HashMap<String, UsageData> usage;
  195. int nc = 0;
  196. for (const SceneDebuggerObject &obj : objects) {
  197. for (const SceneDebuggerObject::SceneDebuggerProperty &prop : obj.properties) {
  198. PropertyInfo pinfo = prop.first;
  199. // Rename those variables, so they don't conflict with the ones from the resource itself.
  200. if (pinfo.name == "script") {
  201. pinfo.name = "Script";
  202. } else if (pinfo.name.begins_with("metadata/")) {
  203. pinfo.name = pinfo.name.replace_first("metadata/", "Metadata/");
  204. }
  205. if (!usage.has(pinfo.name)) {
  206. UsageData usage_dt;
  207. usage_dt.prop = prop;
  208. usage_dt.prop.first.name = pinfo.name;
  209. usage_dt.values[obj.id] = prop.second;
  210. usage[pinfo.name] = usage_dt;
  211. }
  212. // Make sure only properties with the same exact PropertyInfo data will appear.
  213. if (usage[pinfo.name].prop.first == pinfo) {
  214. usage[pinfo.name].qty++;
  215. usage[pinfo.name].values[obj.id] = prop.second;
  216. }
  217. }
  218. nc++;
  219. }
  220. for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {
  221. HashMap<String, UsageData>::Iterator next = E;
  222. ++next;
  223. UsageData usage_dt = E->value;
  224. if (nc != usage_dt.qty) {
  225. // Doesn't appear on all of them, remove it.
  226. usage.erase(E->key);
  227. }
  228. E = next;
  229. }
  230. int old_prop_size = remote_objects->prop_list.size();
  231. remote_objects->prop_list.clear();
  232. int new_props_added = 0;
  233. HashSet<String> changed;
  234. for (KeyValue<String, UsageData> &KV : usage) {
  235. const PropertyInfo &pinfo = KV.value.prop.first;
  236. Variant var = KV.value.values[remote_objects->remote_object_ids[0]];
  237. if (pinfo.type == Variant::OBJECT && var.is_string()) {
  238. String path = var;
  239. if (path.contains("::")) {
  240. // Built-in resource.
  241. String base_path = path.get_slice("::", 0);
  242. Ref<Resource> dependency = ResourceLoader::load(base_path);
  243. if (dependency.is_valid()) {
  244. remote_dependencies.insert(dependency);
  245. }
  246. }
  247. var = ResourceLoader::load(path);
  248. KV.value.values[remote_objects->remote_object_ids[0]] = var;
  249. }
  250. // Always add the property, since props may have been added or removed.
  251. remote_objects->prop_list.push_back(pinfo);
  252. if (!remote_objects->prop_values.has(pinfo.name)) {
  253. new_props_added++;
  254. } else if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, remote_objects->prop_values[pinfo.name], var))) {
  255. changed.insert(pinfo.name);
  256. }
  257. remote_objects->prop_values[pinfo.name] = KV.value.values;
  258. }
  259. if (old_prop_size == remote_objects->prop_list.size() && new_props_added == 0) {
  260. // Only some may have changed, if so, then update those, if they exist.
  261. for (const String &E : changed) {
  262. emit_signal(SNAME("object_property_updated"), remote_objects->get_instance_id(), E);
  263. }
  264. } else {
  265. // Full update, because props were added or removed.
  266. remote_objects->update();
  267. }
  268. return remote_objects;
  269. }
  270. void EditorDebuggerInspector::clear_remote_inspector() {
  271. if (remote_objects_list.is_empty()) {
  272. return;
  273. }
  274. const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
  275. // Check if the inspector holds remote items, and take it out if so.
  276. if (Object::cast_to<EditorDebuggerRemoteObjects>(obj)) {
  277. EditorNode::get_singleton()->push_item(nullptr);
  278. }
  279. }
  280. void EditorDebuggerInspector::clear_cache() {
  281. clear_remote_inspector();
  282. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  283. memdelete(robjs);
  284. }
  285. remote_objects_list.clear();
  286. remote_dependencies.clear();
  287. }
  288. void EditorDebuggerInspector::invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids) {
  289. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  290. if (robjs->remote_object_ids == p_ids) {
  291. const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
  292. if (obj == robjs) {
  293. EditorNode::get_singleton()->push_item(nullptr);
  294. }
  295. remote_objects_list.erase(robjs);
  296. memdelete(robjs);
  297. break;
  298. }
  299. }
  300. }
  301. void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {
  302. DebuggerMarshalls::ScriptStackVariable var;
  303. var.deserialize(p_array);
  304. String n = var.name;
  305. Variant v = var.value;
  306. PropertyHint h = PROPERTY_HINT_NONE;
  307. String hs;
  308. if (var.var_type == Variant::OBJECT && v) {
  309. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  310. h = PROPERTY_HINT_OBJECT_ID;
  311. hs = "Object";
  312. }
  313. String type;
  314. switch (var.type) {
  315. case 0:
  316. type = "Locals/";
  317. break;
  318. case 1:
  319. type = "Members/";
  320. break;
  321. case 2:
  322. type = "Globals/";
  323. break;
  324. case 3:
  325. type = "Evaluated/";
  326. break;
  327. default:
  328. type = "Unknown/";
  329. }
  330. PropertyInfo pinfo;
  331. pinfo.name = type + n;
  332. pinfo.type = v.get_type();
  333. pinfo.hint = h;
  334. pinfo.hint_string = hs;
  335. if ((p_offset == -1) || variables->prop_list.is_empty()) {
  336. variables->prop_list.push_back(pinfo);
  337. } else {
  338. List<PropertyInfo>::Element *current = variables->prop_list.front();
  339. for (int i = 0; i < p_offset; i++) {
  340. current = current->next();
  341. }
  342. variables->prop_list.insert_before(current, pinfo);
  343. }
  344. variables->prop_values[type + n][0] = v;
  345. variables->update();
  346. edit(variables);
  347. }
  348. void EditorDebuggerInspector::clear_stack_variables() {
  349. variables->clear();
  350. variables->update();
  351. }
  352. String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
  353. for (KeyValue<StringName, TypedDictionary<uint64_t, Variant>> &E : variables->prop_values) {
  354. String v = E.key.operator String();
  355. if (v.get_slicec('/', 1) == p_var) {
  356. return variables->get_variant(v);
  357. }
  358. }
  359. return String();
  360. }