Path: blob/master/core/debugger/debugger_marshalls.cpp
10277 views
/**************************************************************************/1/* debugger_marshalls.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "debugger_marshalls.h"3132#include "core/io/marshalls.h"3334#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()))35#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()))3637Array DebuggerMarshalls::ScriptStackDump::serialize() {38Array arr = { frames.size() * 3 };39for (const ScriptLanguage::StackInfo &frame : frames) {40arr.push_back(frame.file);41arr.push_back(frame.line);42arr.push_back(frame.func);43}44return arr;45}4647bool DebuggerMarshalls::ScriptStackDump::deserialize(const Array &p_arr) {48CHECK_SIZE(p_arr, 1, "ScriptStackDump");49uint32_t size = p_arr[0];50CHECK_SIZE(p_arr, size, "ScriptStackDump");51int idx = 1;52for (uint32_t i = 0; i < size / 3; i++) {53ScriptLanguage::StackInfo sf;54sf.file = p_arr[idx];55sf.line = p_arr[idx + 1];56sf.func = p_arr[idx + 2];57frames.push_back(sf);58idx += 3;59}60CHECK_END(p_arr, idx, "ScriptStackDump");61return true;62}6364Array DebuggerMarshalls::ScriptStackVariable::serialize(int max_size) {65Array arr = { name, type, value.get_type() };6667Variant var = value;68if (value.get_type() == Variant::OBJECT && value.get_validated_object() == nullptr) {69var = Variant();70}7172int len = 0;73Error err = encode_variant(var, nullptr, len, false);74if (err != OK) {75ERR_PRINT("Failed to encode variant.");76}7778if (len > max_size) {79arr.push_back(Variant());80} else {81arr.push_back(var);82}83return arr;84}8586bool DebuggerMarshalls::ScriptStackVariable::deserialize(const Array &p_arr) {87CHECK_SIZE(p_arr, 4, "ScriptStackVariable");88name = p_arr[0];89type = p_arr[1];90var_type = p_arr[2];91value = p_arr[3];92CHECK_END(p_arr, 4, "ScriptStackVariable");93return true;94}9596Array DebuggerMarshalls::OutputError::serialize() {97unsigned int size = callstack.size();98Array arr = {99hr,100min,101sec, msec,102source_file,103source_func,104source_line,105error,106error_descr,107warning,108size * 3109};110const ScriptLanguage::StackInfo *r = callstack.ptr();111for (int i = 0; i < callstack.size(); i++) {112arr.push_back(r[i].file);113arr.push_back(r[i].func);114arr.push_back(r[i].line);115}116return arr;117}118119bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {120CHECK_SIZE(p_arr, 11, "OutputError");121hr = p_arr[0];122min = p_arr[1];123sec = p_arr[2];124msec = p_arr[3];125source_file = p_arr[4];126source_func = p_arr[5];127source_line = p_arr[6];128error = p_arr[7];129error_descr = p_arr[8];130warning = p_arr[9];131unsigned int stack_size = p_arr[10];132CHECK_SIZE(p_arr, stack_size, "OutputError");133int idx = 11;134callstack.resize(stack_size / 3);135ScriptLanguage::StackInfo *w = callstack.ptrw();136for (unsigned int i = 0; i < stack_size / 3; i++) {137w[i].file = p_arr[idx];138w[i].func = p_arr[idx + 1];139w[i].line = p_arr[idx + 2];140idx += 3;141}142CHECK_END(p_arr, idx, "OutputError");143return true;144}145146Array DebuggerMarshalls::serialize_key_shortcut(const Ref<Shortcut> &p_shortcut) {147ERR_FAIL_COND_V(p_shortcut.is_null(), Array());148Array keys;149for (const Ref<InputEvent> ev : p_shortcut->get_events()) {150const Ref<InputEventKey> kev = ev;151ERR_CONTINUE(kev.is_null());152if (kev->get_physical_keycode() != Key::NONE) {153keys.push_back(true);154keys.push_back(kev->get_physical_keycode_with_modifiers());155} else {156keys.push_back(false);157keys.push_back(kev->get_keycode_with_modifiers());158}159}160return keys;161}162163Ref<Shortcut> DebuggerMarshalls::deserialize_key_shortcut(const Array &p_keys) {164Array key_events;165ERR_FAIL_COND_V(p_keys.size() % 2 != 0, Ref<Shortcut>());166for (int i = 0; i < p_keys.size(); i += 2) {167ERR_CONTINUE(p_keys[i].get_type() != Variant::BOOL);168ERR_CONTINUE(p_keys[i + 1].get_type() != Variant::INT);169key_events.push_back(InputEventKey::create_reference((Key)p_keys[i + 1].operator int(), p_keys[i].operator bool()));170}171if (key_events.is_empty()) {172return Ref<Shortcut>();173}174Ref<Shortcut> shortcut;175shortcut.instantiate();176shortcut->set_events(key_events);177return shortcut;178}179180181