gdscript_language_protocol.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL;
  35. void GDScriptLanguageProtocol::on_data_received(int p_id) {
  36. lastest_client_id = p_id;
  37. Ref<WebSocketPeer> peer = server->get_peer(p_id);
  38. PoolByteArray data;
  39. if (OK == peer->get_packet_buffer(data)) {
  40. String message;
  41. message.parse_utf8((const char *)data.read().ptr(), data.size());
  42. if (message.begins_with("Content-Length:")) return;
  43. String output = process_message(message);
  44. if (!output.empty()) {
  45. CharString charstr = output.utf8();
  46. peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  47. }
  48. }
  49. }
  50. void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) {
  51. clients.set(p_id, server->get_peer(p_id));
  52. }
  53. void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) {
  54. clients.erase(p_id);
  55. }
  56. String GDScriptLanguageProtocol::process_message(const String &p_text) {
  57. String ret = process_string(p_text);
  58. if (ret.empty()) {
  59. return ret;
  60. } else {
  61. return format_output(ret);
  62. }
  63. }
  64. String GDScriptLanguageProtocol::format_output(const String &p_text) {
  65. String header = "Content-Length: ";
  66. CharString charstr = p_text.utf8();
  67. size_t len = charstr.length();
  68. header += itos(len);
  69. header += "\r\n\r\n";
  70. return header + p_text;
  71. }
  72. void GDScriptLanguageProtocol::_bind_methods() {
  73. ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
  74. ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
  75. ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received);
  76. ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
  77. ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
  78. }
  79. Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
  80. lsp::InitializeResult ret;
  81. return ret.to_json();
  82. }
  83. void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
  84. Dictionary params;
  85. params["type"] = 3;
  86. params["message"] = "GDScript Language Server initialized!";
  87. Dictionary test_message = make_notification("window/showMessage", params);
  88. if (Ref<WebSocketPeer> *peer = clients.getptr(lastest_client_id)) {
  89. String msg = JSON::print(test_message);
  90. msg = format_output(msg);
  91. CharString charstr = msg.utf8();
  92. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  93. }
  94. }
  95. void GDScriptLanguageProtocol::poll() {
  96. server->poll();
  97. }
  98. Error GDScriptLanguageProtocol::start(int p_port) {
  99. if (server == NULL) {
  100. server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer"));
  101. server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough
  102. server->connect("data_received", this, "on_data_received");
  103. server->connect("client_connected", this, "on_client_connected");
  104. server->connect("client_disconnected", this, "on_client_disconnected");
  105. }
  106. return server->listen(p_port);
  107. }
  108. void GDScriptLanguageProtocol::stop() {
  109. server->stop();
  110. }
  111. void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) {
  112. Dictionary message = make_notification(p_method, p_params);
  113. String msg = JSON::print(message);
  114. msg = format_output(msg);
  115. CharString charstr = msg.utf8();
  116. const int *p_id = clients.next(NULL);
  117. while (p_id != NULL) {
  118. Ref<WebSocketPeer> peer = clients.get(*p_id);
  119. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  120. p_id = clients.next(p_id);
  121. }
  122. }
  123. void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) {
  124. if (p_client == -1) {
  125. p_client = lastest_client_id;
  126. }
  127. Ref<WebSocketPeer> *peer = clients.getptr(p_client);
  128. ERR_FAIL_COND(peer == NULL);
  129. Dictionary message = make_notification(p_method, p_params);
  130. String msg = JSON::print(message);
  131. msg = format_output(msg);
  132. CharString charstr = msg.utf8();
  133. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  134. }
  135. GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
  136. server = NULL;
  137. singleton = this;
  138. set_scope("textDocument", &text_document);
  139. set_scope("workspace", &workspace);
  140. workspace.root = ProjectSettings::get_singleton()->get_resource_path();
  141. }
  142. GDScriptLanguageProtocol::~GDScriptLanguageProtocol() {
  143. memdelete(server);
  144. server = NULL;
  145. }