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