2
0

gdscript_language_protocol.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* gdscript_language_protocol.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gdscript_language_protocol.h"
  31. #include "core/io/json.h"
  32. #include "core/os/copymem.h"
  33. #include "core/project_settings.h"
  34. #include "editor/editor_node.h"
  35. GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL;
  36. void GDScriptLanguageProtocol::on_data_received(int p_id) {
  37. lastest_client_id = p_id;
  38. Ref<WebSocketPeer> peer = server->get_peer(p_id);
  39. PoolByteArray data;
  40. if (OK == peer->get_packet_buffer(data)) {
  41. String message;
  42. message.parse_utf8((const char *)data.read().ptr(), data.size());
  43. if (message.begins_with("Content-Length:")) return;
  44. String output = process_message(message);
  45. if (!output.empty()) {
  46. CharString charstr = output.utf8();
  47. peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  48. }
  49. }
  50. }
  51. void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) {
  52. clients.set(p_id, server->get_peer(p_id));
  53. }
  54. void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) {
  55. clients.erase(p_id);
  56. }
  57. String GDScriptLanguageProtocol::process_message(const String &p_text) {
  58. String ret = process_string(p_text);
  59. if (ret.empty()) {
  60. return ret;
  61. } else {
  62. return format_output(ret);
  63. }
  64. }
  65. String GDScriptLanguageProtocol::format_output(const String &p_text) {
  66. String header = "Content-Length: ";
  67. CharString charstr = p_text.utf8();
  68. size_t len = charstr.length();
  69. header += itos(len);
  70. header += "\r\n\r\n";
  71. return header + p_text;
  72. }
  73. void GDScriptLanguageProtocol::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
  75. ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
  76. ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received);
  77. ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
  78. ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
  79. ClassDB::bind_method(D_METHOD("notify_all_clients", "p_method", "p_params"), &GDScriptLanguageProtocol::notify_all_clients, DEFVAL(Variant()));
  80. ClassDB::bind_method(D_METHOD("notify_client", "p_method", "p_params", "p_client"), &GDScriptLanguageProtocol::notify_client, DEFVAL(Variant()), DEFVAL(-1));
  81. ClassDB::bind_method(D_METHOD("is_smart_resolve_enabled"), &GDScriptLanguageProtocol::is_smart_resolve_enabled);
  82. ClassDB::bind_method(D_METHOD("get_text_document"), &GDScriptLanguageProtocol::get_text_document);
  83. ClassDB::bind_method(D_METHOD("get_workspace"), &GDScriptLanguageProtocol::get_workspace);
  84. ClassDB::bind_method(D_METHOD("is_initialized"), &GDScriptLanguageProtocol::is_initialized);
  85. }
  86. Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
  87. lsp::InitializeResult ret;
  88. String root_uri = p_params["rootUri"];
  89. String root = p_params["rootPath"];
  90. bool is_same_workspace;
  91. #ifndef WINDOWS_ENABLED
  92. is_same_workspace = root.to_lower() == workspace->root.to_lower();
  93. #else
  94. is_same_workspace = root.replace("\\", "/").to_lower() == workspace->root.to_lower();
  95. #endif
  96. if (root_uri.length() && is_same_workspace) {
  97. workspace->root_uri = root_uri;
  98. } else {
  99. workspace->root_uri = "file://" + workspace->root;
  100. Dictionary params;
  101. params["path"] = workspace->root;
  102. Dictionary request = make_notification("gdscrip_client/changeWorkspace", params);
  103. if (Ref<WebSocketPeer> *peer = clients.getptr(lastest_client_id)) {
  104. String msg = JSON::print(request);
  105. msg = format_output(msg);
  106. CharString charstr = msg.utf8();
  107. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  108. }
  109. }
  110. if (!_initialized) {
  111. workspace->initialize();
  112. text_document->initialize();
  113. _initialized = true;
  114. }
  115. return ret.to_json();
  116. }
  117. void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
  118. lsp::GodotCapabilities capabilities;
  119. DocData *doc = EditorHelp::get_doc_data();
  120. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  121. lsp::GodotNativeClassInfo gdclass;
  122. gdclass.name = E->get().name;
  123. gdclass.class_doc = &(E->get());
  124. if (ClassDB::ClassInfo *ptr = ClassDB::classes.getptr(StringName(E->get().name))) {
  125. gdclass.class_info = ptr;
  126. }
  127. capabilities.native_classes.push_back(gdclass);
  128. }
  129. notify_client("gdscript/capabilities", capabilities.to_json());
  130. }
  131. void GDScriptLanguageProtocol::poll() {
  132. server->poll();
  133. }
  134. Error GDScriptLanguageProtocol::start(int p_port, const IP_Address &p_bind_ip) {
  135. if (server == NULL) {
  136. server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer"));
  137. ERR_FAIL_COND_V(!server, FAILED);
  138. server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough
  139. server->connect("data_received", this, "on_data_received");
  140. server->connect("client_connected", this, "on_client_connected");
  141. server->connect("client_disconnected", this, "on_client_disconnected");
  142. }
  143. server->set_bind_ip(p_bind_ip);
  144. return server->listen(p_port);
  145. }
  146. void GDScriptLanguageProtocol::stop() {
  147. const int *ptr = clients.next(NULL);
  148. while (ptr) {
  149. clients.get(*ptr)->close();
  150. ptr = clients.next(ptr);
  151. }
  152. server->stop();
  153. clients.clear();
  154. }
  155. void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) {
  156. Dictionary message = make_notification(p_method, p_params);
  157. String msg = JSON::print(message);
  158. msg = format_output(msg);
  159. CharString charstr = msg.utf8();
  160. const int *p_id = clients.next(NULL);
  161. while (p_id != NULL) {
  162. Ref<WebSocketPeer> peer = clients.get(*p_id);
  163. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  164. p_id = clients.next(p_id);
  165. }
  166. }
  167. void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) {
  168. if (p_client == -1) {
  169. p_client = lastest_client_id;
  170. }
  171. Ref<WebSocketPeer> *peer = clients.getptr(p_client);
  172. ERR_FAIL_COND(peer == NULL);
  173. Dictionary message = make_notification(p_method, p_params);
  174. String msg = JSON::print(message);
  175. msg = format_output(msg);
  176. CharString charstr = msg.utf8();
  177. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  178. }
  179. bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const {
  180. return bool(_EDITOR_GET("network/language_server/enable_smart_resolve"));
  181. }
  182. bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const {
  183. return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor"));
  184. }
  185. GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
  186. server = NULL;
  187. singleton = this;
  188. _initialized = false;
  189. workspace.instance();
  190. text_document.instance();
  191. set_scope("textDocument", text_document.ptr());
  192. set_scope("completionItem", text_document.ptr());
  193. set_scope("workspace", workspace.ptr());
  194. workspace->root = ProjectSettings::get_singleton()->get_resource_path();
  195. }
  196. GDScriptLanguageProtocol::~GDScriptLanguageProtocol() {
  197. memdelete(server);
  198. server = NULL;
  199. }