debugger_marshalls.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* debugger_marshalls.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 "debugger_marshalls.h"
  31. #include "core/io/marshalls.h"
  32. #define CHECK_SIZE(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() < (uint32_t)(expected), false, String("Malformed ") + what + " message from script debugger, message too short. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  33. #define CHECK_END(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() > (uint32_t)expected, false, String("Malformed ") + what + " message from script debugger, message too long. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  34. Array DebuggerMarshalls::ScriptStackDump::serialize() {
  35. Array arr = { frames.size() * 3 };
  36. for (const ScriptLanguage::StackInfo &frame : frames) {
  37. arr.push_back(frame.file);
  38. arr.push_back(frame.line);
  39. arr.push_back(frame.func);
  40. }
  41. return arr;
  42. }
  43. bool DebuggerMarshalls::ScriptStackDump::deserialize(const Array &p_arr) {
  44. CHECK_SIZE(p_arr, 1, "ScriptStackDump");
  45. uint32_t size = p_arr[0];
  46. CHECK_SIZE(p_arr, size, "ScriptStackDump");
  47. int idx = 1;
  48. for (uint32_t i = 0; i < size / 3; i++) {
  49. ScriptLanguage::StackInfo sf;
  50. sf.file = p_arr[idx];
  51. sf.line = p_arr[idx + 1];
  52. sf.func = p_arr[idx + 2];
  53. frames.push_back(sf);
  54. idx += 3;
  55. }
  56. CHECK_END(p_arr, idx, "ScriptStackDump");
  57. return true;
  58. }
  59. Array DebuggerMarshalls::ScriptStackVariable::serialize(int max_size) {
  60. Array arr = { name, type, value.get_type() };
  61. Variant var = value;
  62. if (value.get_type() == Variant::OBJECT && value.get_validated_object() == nullptr) {
  63. var = Variant();
  64. }
  65. int len = 0;
  66. Error err = encode_variant(var, nullptr, len, false);
  67. if (err != OK) {
  68. ERR_PRINT("Failed to encode variant.");
  69. }
  70. if (len > max_size) {
  71. arr.push_back(Variant());
  72. } else {
  73. arr.push_back(var);
  74. }
  75. return arr;
  76. }
  77. bool DebuggerMarshalls::ScriptStackVariable::deserialize(const Array &p_arr) {
  78. CHECK_SIZE(p_arr, 4, "ScriptStackVariable");
  79. name = p_arr[0];
  80. type = p_arr[1];
  81. var_type = p_arr[2];
  82. value = p_arr[3];
  83. CHECK_END(p_arr, 4, "ScriptStackVariable");
  84. return true;
  85. }
  86. Array DebuggerMarshalls::OutputError::serialize() {
  87. unsigned int size = callstack.size();
  88. Array arr = {
  89. hr,
  90. min,
  91. sec, msec,
  92. source_file,
  93. source_func,
  94. source_line,
  95. error,
  96. error_descr,
  97. warning,
  98. size * 3
  99. };
  100. const ScriptLanguage::StackInfo *r = callstack.ptr();
  101. for (int i = 0; i < callstack.size(); i++) {
  102. arr.push_back(r[i].file);
  103. arr.push_back(r[i].func);
  104. arr.push_back(r[i].line);
  105. }
  106. return arr;
  107. }
  108. bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {
  109. CHECK_SIZE(p_arr, 11, "OutputError");
  110. hr = p_arr[0];
  111. min = p_arr[1];
  112. sec = p_arr[2];
  113. msec = p_arr[3];
  114. source_file = p_arr[4];
  115. source_func = p_arr[5];
  116. source_line = p_arr[6];
  117. error = p_arr[7];
  118. error_descr = p_arr[8];
  119. warning = p_arr[9];
  120. unsigned int stack_size = p_arr[10];
  121. CHECK_SIZE(p_arr, stack_size, "OutputError");
  122. int idx = 11;
  123. callstack.resize(stack_size / 3);
  124. ScriptLanguage::StackInfo *w = callstack.ptrw();
  125. for (unsigned int i = 0; i < stack_size / 3; i++) {
  126. w[i].file = p_arr[idx];
  127. w[i].func = p_arr[idx + 1];
  128. w[i].line = p_arr[idx + 2];
  129. idx += 3;
  130. }
  131. CHECK_END(p_arr, idx, "OutputError");
  132. return true;
  133. }
  134. Array DebuggerMarshalls::serialize_key_shortcut(const Ref<Shortcut> &p_shortcut) {
  135. ERR_FAIL_COND_V(p_shortcut.is_null(), Array());
  136. Array keys;
  137. for (const Ref<InputEvent> ev : p_shortcut->get_events()) {
  138. const Ref<InputEventKey> kev = ev;
  139. ERR_CONTINUE(kev.is_null());
  140. if (kev->get_physical_keycode() != Key::NONE) {
  141. keys.push_back(true);
  142. keys.push_back(kev->get_physical_keycode_with_modifiers());
  143. } else {
  144. keys.push_back(false);
  145. keys.push_back(kev->get_keycode_with_modifiers());
  146. }
  147. }
  148. return keys;
  149. }
  150. Ref<Shortcut> DebuggerMarshalls::deserialize_key_shortcut(const Array &p_keys) {
  151. Array key_events;
  152. ERR_FAIL_COND_V(p_keys.size() % 2 != 0, Ref<Shortcut>());
  153. for (int i = 0; i < p_keys.size(); i += 2) {
  154. ERR_CONTINUE(p_keys[i].get_type() != Variant::BOOL);
  155. ERR_CONTINUE(p_keys[i + 1].get_type() != Variant::INT);
  156. key_events.push_back(InputEventKey::create_reference((Key)p_keys[i + 1].operator int(), p_keys[i].operator bool()));
  157. }
  158. if (key_events.is_empty()) {
  159. return Ref<Shortcut>();
  160. }
  161. Ref<Shortcut> shortcut;
  162. shortcut.instantiate();
  163. shortcut->set_events(key_events);
  164. return shortcut;
  165. }