jsonrpc.cpp 6.1 KB

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