2
0

scene_debugger_object.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**************************************************************************/
  2. /* scene_debugger_object.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. #ifdef DEBUG_ENABLED
  31. #include "scene_debugger_object.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/object/script_language.h"
  34. SceneDebuggerObject::SceneDebuggerObject(Object *p_obj) {
  35. if (!p_obj) {
  36. return;
  37. }
  38. id = p_obj->get_instance_id();
  39. class_name = p_obj->get_class();
  40. if (ScriptInstance *si = p_obj->get_script_instance()) {
  41. // Read script instance constants and variables.
  42. if (!si->get_script().is_null()) {
  43. Script *s = si->get_script().ptr();
  44. _parse_script_properties(s, si);
  45. }
  46. }
  47. if (Node *node = Object::cast_to<Node>(p_obj)) {
  48. // For debugging multiplayer.
  49. {
  50. PropertyInfo pi(Variant::INT, String("Node/multiplayer_authority"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY);
  51. properties.push_back(SceneDebuggerProperty(pi, node->get_multiplayer_authority()));
  52. }
  53. // Add specialized NodePath info (if inside tree).
  54. if (node->is_inside_tree()) {
  55. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  56. properties.push_back(SceneDebuggerProperty(pi, node->get_path()));
  57. } else { // Can't ask for path if a node is not in tree.
  58. PropertyInfo pi(Variant::STRING, String("Node/path"));
  59. properties.push_back(SceneDebuggerProperty(pi, "[Orphan]"));
  60. }
  61. } else if (Script *s = Object::cast_to<Script>(p_obj)) {
  62. // Add script constants (no instance).
  63. _parse_script_properties(s, nullptr);
  64. }
  65. // Add base object properties.
  66. List<PropertyInfo> pinfo;
  67. p_obj->get_property_list(&pinfo, true);
  68. for (const PropertyInfo &E : pinfo) {
  69. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  70. properties.push_back(SceneDebuggerProperty(E, p_obj->get(E.name)));
  71. }
  72. }
  73. }
  74. void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) {
  75. typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap;
  76. typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap;
  77. ScriptMemberMap members;
  78. if (p_instance) {
  79. members[p_script] = HashSet<StringName>();
  80. p_script->get_members(&(members[p_script]));
  81. }
  82. ScriptConstantsMap constants;
  83. constants[p_script] = HashMap<StringName, Variant>();
  84. p_script->get_constants(&(constants[p_script]));
  85. Ref<Script> base = p_script->get_base_script();
  86. while (base.is_valid()) {
  87. if (p_instance) {
  88. members[base.ptr()] = HashSet<StringName>();
  89. base->get_members(&(members[base.ptr()]));
  90. }
  91. constants[base.ptr()] = HashMap<StringName, Variant>();
  92. base->get_constants(&(constants[base.ptr()]));
  93. base = base->get_base_script();
  94. }
  95. HashSet<String> exported_members;
  96. if (p_instance) {
  97. List<PropertyInfo> pinfo;
  98. p_instance->get_property_list(&pinfo);
  99. for (const PropertyInfo &E : pinfo) {
  100. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  101. exported_members.insert(E.name);
  102. }
  103. }
  104. }
  105. // Members
  106. for (KeyValue<const Script *, HashSet<StringName>> sm : members) {
  107. for (const StringName &E : sm.value) {
  108. if (exported_members.has(E)) {
  109. continue; // Exported variables already show up in the inspector.
  110. }
  111. if (String(E).begins_with("@")) {
  112. continue; // Skip groups.
  113. }
  114. Variant m;
  115. if (p_instance->get(E, m)) {
  116. String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/";
  117. PropertyInfo pi(m.get_type(), "Members/" + script_path + E);
  118. properties.push_back(SceneDebuggerProperty(pi, m));
  119. }
  120. }
  121. }
  122. // Constants
  123. for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) {
  124. for (const KeyValue<StringName, Variant> &E : sc.value) {
  125. String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/";
  126. if (E.value.get_type() == Variant::OBJECT) {
  127. Variant inst_id = ((Object *)E.value)->get_instance_id();
  128. PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  129. properties.push_back(SceneDebuggerProperty(pi, inst_id));
  130. } else {
  131. PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key);
  132. properties.push_back(SceneDebuggerProperty(pi, E.value));
  133. }
  134. }
  135. }
  136. }
  137. void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) {
  138. Array send_props;
  139. for (SceneDebuggerProperty &property : properties) {
  140. const PropertyInfo &pi = property.first;
  141. Variant &var = property.second;
  142. Ref<Resource> res = var;
  143. Array prop = { pi.name, pi.type };
  144. PropertyHint hint = pi.hint;
  145. String hint_string = pi.hint_string;
  146. if (res.is_valid() && !res->get_path().is_empty()) {
  147. var = res->get_path();
  148. } else { //only send information that can be sent..
  149. int len = 0; //test how big is this to encode
  150. encode_variant(var, nullptr, len);
  151. if (len > p_max_size) { //limit to max size
  152. hint = PROPERTY_HINT_OBJECT_TOO_BIG;
  153. hint_string = "";
  154. var = Variant();
  155. }
  156. }
  157. prop.push_back(hint);
  158. prop.push_back(hint_string);
  159. prop.push_back(pi.usage);
  160. prop.push_back(var);
  161. send_props.push_back(prop);
  162. }
  163. r_arr.push_back(uint64_t(id));
  164. r_arr.push_back(class_name);
  165. r_arr.push_back(send_props);
  166. }
  167. #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type)
  168. void SceneDebuggerObject::deserialize(const Array &p_arr) {
  169. ERR_FAIL_COND(p_arr.size() < 3);
  170. CHECK_TYPE(p_arr[0], INT);
  171. CHECK_TYPE(p_arr[1], STRING);
  172. CHECK_TYPE(p_arr[2], ARRAY);
  173. deserialize(uint64_t(p_arr[0]), p_arr[1], p_arr[2]);
  174. }
  175. void SceneDebuggerObject::deserialize(uint64_t p_id, const String &p_class_name, const Array &p_props) {
  176. id = p_id;
  177. class_name = p_class_name;
  178. for (int i = 0; i < p_props.size(); i++) {
  179. CHECK_TYPE(p_props[i], ARRAY);
  180. Array prop = p_props[i];
  181. ERR_FAIL_COND(prop.size() != 6);
  182. CHECK_TYPE(prop[0], STRING);
  183. CHECK_TYPE(prop[1], INT);
  184. CHECK_TYPE(prop[2], INT);
  185. CHECK_TYPE(prop[3], STRING);
  186. CHECK_TYPE(prop[4], INT);
  187. PropertyInfo pinfo;
  188. pinfo.name = prop[0];
  189. pinfo.type = Variant::Type(int(prop[1]));
  190. pinfo.hint = PropertyHint(int(prop[2]));
  191. pinfo.hint_string = prop[3];
  192. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  193. Variant var = prop[5];
  194. if (pinfo.type == Variant::OBJECT) {
  195. if (var.is_zero()) {
  196. var = Ref<Resource>();
  197. } else if (var.get_type() == Variant::OBJECT) {
  198. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  199. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  200. pinfo.type = var.get_type();
  201. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  202. pinfo.hint_string = "Object";
  203. }
  204. }
  205. }
  206. properties.push_back(SceneDebuggerProperty(pinfo, var));
  207. }
  208. }
  209. SceneDebuggerTree::SceneDebuggerTree(Node *p_root) {
  210. // Flatten tree into list, depth first, use stack to avoid recursion.
  211. List<Node *> stack;
  212. stack.push_back(p_root);
  213. bool is_root = true;
  214. const StringName &is_visible_sn = SNAME("is_visible");
  215. const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree");
  216. while (stack.size()) {
  217. Node *n = stack.front()->get();
  218. stack.pop_front();
  219. int count = n->get_child_count();
  220. for (int i = 0; i < count; i++) {
  221. stack.push_front(n->get_child(count - i - 1));
  222. }
  223. int view_flags = 0;
  224. if (is_root) {
  225. // Prevent root window visibility from being changed.
  226. is_root = false;
  227. } else if (n->has_method(is_visible_sn)) {
  228. const Variant visible = n->call(is_visible_sn);
  229. if (visible.get_type() == Variant::BOOL) {
  230. view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD;
  231. view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE;
  232. }
  233. if (n->has_method(is_visible_in_tree_sn)) {
  234. const Variant visible_in_tree = n->call(is_visible_in_tree_sn);
  235. if (visible_in_tree.get_type() == Variant::BOOL) {
  236. view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE;
  237. }
  238. }
  239. }
  240. String class_name;
  241. ScriptInstance *script_instance = n->get_script_instance();
  242. if (script_instance) {
  243. Ref<Script> script = script_instance->get_script();
  244. if (script.is_valid()) {
  245. class_name = script->get_global_name();
  246. if (class_name.is_empty()) {
  247. // If there is no class_name in this script we just take the script path.
  248. class_name = script->get_path();
  249. }
  250. }
  251. }
  252. nodes.push_back(RemoteNode(count, n->get_name(), class_name.is_empty() ? n->get_class() : class_name, n->get_instance_id(), n->get_scene_file_path(), view_flags));
  253. }
  254. }
  255. void SceneDebuggerTree::serialize(Array &p_arr) {
  256. for (const RemoteNode &n : nodes) {
  257. p_arr.push_back(n.child_count);
  258. p_arr.push_back(n.name);
  259. p_arr.push_back(n.type_name);
  260. p_arr.push_back(n.id);
  261. p_arr.push_back(n.scene_file_path);
  262. p_arr.push_back(n.view_flags);
  263. }
  264. }
  265. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  266. int idx = 0;
  267. while (p_arr.size() > idx) {
  268. ERR_FAIL_COND(p_arr.size() < 6);
  269. CHECK_TYPE(p_arr[idx], INT); // child_count.
  270. CHECK_TYPE(p_arr[idx + 1], STRING); // name.
  271. CHECK_TYPE(p_arr[idx + 2], STRING); // type_name.
  272. CHECK_TYPE(p_arr[idx + 3], INT); // id.
  273. CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path.
  274. CHECK_TYPE(p_arr[idx + 5], INT); // view_flags.
  275. nodes.push_back(RemoteNode(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5]));
  276. idx += 6;
  277. }
  278. }
  279. #undef CHECK_TYPE
  280. #endif // DEBUG_ENABLED