| 1 | /**************************************************************************/ |
| 2 | /* jsonrpc.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 "jsonrpc.h" |
| 32 | |
| 33 | #include "core/io/json.h" |
| 34 | |
| 35 | JSONRPC::JSONRPC() { |
| 36 | } |
| 37 | |
| 38 | JSONRPC::~JSONRPC() { |
| 39 | } |
| 40 | |
| 41 | void JSONRPC::_bind_methods() { |
| 42 | ClassDB::bind_method(D_METHOD("set_scope" , "scope" , "target" ), &JSONRPC::set_scope); |
| 43 | ClassDB::bind_method(D_METHOD("process_action" , "action" , "recurse" ), &JSONRPC::process_action, DEFVAL(false)); |
| 44 | ClassDB::bind_method(D_METHOD("process_string" , "action" ), &JSONRPC::process_string); |
| 45 | |
| 46 | ClassDB::bind_method(D_METHOD("make_request" , "method" , "params" , "id" ), &JSONRPC::make_request); |
| 47 | ClassDB::bind_method(D_METHOD("make_response" , "result" , "id" ), &JSONRPC::make_response); |
| 48 | ClassDB::bind_method(D_METHOD("make_notification" , "method" , "params" ), &JSONRPC::make_notification); |
| 49 | ClassDB::bind_method(D_METHOD("make_response_error" , "code" , "message" , "id" ), &JSONRPC::make_response_error, DEFVAL(Variant())); |
| 50 | |
| 51 | BIND_ENUM_CONSTANT(PARSE_ERROR); |
| 52 | BIND_ENUM_CONSTANT(INVALID_REQUEST); |
| 53 | BIND_ENUM_CONSTANT(METHOD_NOT_FOUND); |
| 54 | BIND_ENUM_CONSTANT(INVALID_PARAMS); |
| 55 | BIND_ENUM_CONSTANT(INTERNAL_ERROR); |
| 56 | } |
| 57 | |
| 58 | Dictionary JSONRPC::make_response_error(int p_code, const String &p_message, const Variant &p_id) const { |
| 59 | Dictionary dict; |
| 60 | dict["jsonrpc" ] = "2.0" ; |
| 61 | |
| 62 | Dictionary err; |
| 63 | err["code" ] = p_code; |
| 64 | err["message" ] = p_message; |
| 65 | |
| 66 | dict["error" ] = err; |
| 67 | dict["id" ] = p_id; |
| 68 | |
| 69 | return dict; |
| 70 | } |
| 71 | |
| 72 | Dictionary JSONRPC::make_response(const Variant &p_value, const Variant &p_id) { |
| 73 | Dictionary dict; |
| 74 | dict["jsonrpc" ] = "2.0" ; |
| 75 | dict["id" ] = p_id; |
| 76 | dict["result" ] = p_value; |
| 77 | return dict; |
| 78 | } |
| 79 | |
| 80 | Dictionary JSONRPC::make_notification(const String &p_method, const Variant &p_params) { |
| 81 | Dictionary dict; |
| 82 | dict["jsonrpc" ] = "2.0" ; |
| 83 | dict["method" ] = p_method; |
| 84 | dict["params" ] = p_params; |
| 85 | return dict; |
| 86 | } |
| 87 | |
| 88 | Dictionary JSONRPC::make_request(const String &p_method, const Variant &p_params, const Variant &p_id) { |
| 89 | Dictionary dict; |
| 90 | dict["jsonrpc" ] = "2.0" ; |
| 91 | dict["method" ] = p_method; |
| 92 | dict["params" ] = p_params; |
| 93 | dict["id" ] = p_id; |
| 94 | return dict; |
| 95 | } |
| 96 | |
| 97 | Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elements) { |
| 98 | Variant ret; |
| 99 | if (p_action.get_type() == Variant::DICTIONARY) { |
| 100 | Dictionary dict = p_action; |
| 101 | String method = dict.get("method" , "" ); |
| 102 | if (method.begins_with("$/" )) { |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | Array args; |
| 107 | if (dict.has("params" )) { |
| 108 | Variant params = dict.get("params" , Variant()); |
| 109 | if (params.get_type() == Variant::ARRAY) { |
| 110 | args = params; |
| 111 | } else { |
| 112 | args.push_back(params); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Object *object = this; |
| 117 | if (method_scopes.has(method.get_base_dir())) { |
| 118 | object = method_scopes[method.get_base_dir()]; |
| 119 | method = method.get_file(); |
| 120 | } |
| 121 | |
| 122 | Variant id; |
| 123 | if (dict.has("id" )) { |
| 124 | id = dict["id" ]; |
| 125 | } |
| 126 | |
| 127 | if (object == nullptr || !object->has_method(method)) { |
| 128 | ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id); |
| 129 | } else { |
| 130 | Variant call_ret = object->callv(method, args); |
| 131 | if (id.get_type() != Variant::NIL) { |
| 132 | ret = make_response(call_ret, id); |
| 133 | } |
| 134 | } |
| 135 | } else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) { |
| 136 | Array arr = p_action; |
| 137 | int size = arr.size(); |
| 138 | if (size) { |
| 139 | Array arr_ret; |
| 140 | for (int i = 0; i < size; i++) { |
| 141 | const Variant &var = arr.get(i); |
| 142 | arr_ret.push_back(process_action(var)); |
| 143 | } |
| 144 | ret = arr_ret; |
| 145 | } else { |
| 146 | ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request" ); |
| 147 | } |
| 148 | } else { |
| 149 | ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request" ); |
| 150 | } |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | String JSONRPC::process_string(const String &p_input) { |
| 155 | if (p_input.is_empty()) { |
| 156 | return String(); |
| 157 | } |
| 158 | |
| 159 | Variant ret; |
| 160 | JSON json; |
| 161 | if (json.parse(p_input) == OK) { |
| 162 | ret = process_action(json.get_data(), true); |
| 163 | } else { |
| 164 | ret = make_response_error(JSONRPC::PARSE_ERROR, "Parse error" ); |
| 165 | } |
| 166 | |
| 167 | if (ret.get_type() == Variant::NIL) { |
| 168 | return "" ; |
| 169 | } |
| 170 | return ret.to_json_string(); |
| 171 | } |
| 172 | |
| 173 | void JSONRPC::set_scope(const String &p_scope, Object *p_obj) { |
| 174 | method_scopes[p_scope] = p_obj; |
| 175 | } |
| 176 | |