editor_file_server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**************************************************************************/
  2. /* editor_file_server.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 "editor_file_server.h"
  31. #include "../editor_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/export/editor_export_platform.h"
  34. #define FILESYSTEM_PROTOCOL_VERSION 1
  35. #define PASSWORD_LENGTH 32
  36. #define MAX_FILE_BUFFER_SIZE 100 * 1024 * 1024 // 100mb max file buffer size (description of files to update, compressed).
  37. static void _add_file(String f, const uint64_t &p_modified_time, HashMap<String, uint64_t> &files_to_send, HashMap<String, uint64_t> &cached_files) {
  38. f = f.replace_first("res://", ""); // remove res://
  39. const uint64_t *cached_mt = cached_files.getptr(f);
  40. if (cached_mt && *cached_mt == p_modified_time) {
  41. // File is good, skip it.
  42. cached_files.erase(f); // Erase to mark this file as existing. Remaining files not added to files_to_send will be considered erased here, so they need to be erased in the client too.
  43. return;
  44. }
  45. files_to_send.insert(f, p_modified_time);
  46. }
  47. void EditorFileServer::_scan_files_changed(EditorFileSystemDirectory *efd, const Vector<String> &p_tags, HashMap<String, uint64_t> &files_to_send, HashMap<String, uint64_t> &cached_files) {
  48. for (int i = 0; i < efd->get_file_count(); i++) {
  49. String f = efd->get_file_path(i);
  50. if (FileAccess::exists(f + ".import")) {
  51. // is imported, determine what to do
  52. // Todo the modified times of remapped files should most likely be kept in EditorFileSystem to speed this up in the future.
  53. Ref<ConfigFile> cf;
  54. cf.instantiate();
  55. Error err = cf->load(f + ".import");
  56. ERR_CONTINUE(err != OK);
  57. {
  58. uint64_t mt = FileAccess::get_modified_time(f + ".import");
  59. _add_file(f + ".import", mt, files_to_send, cached_files);
  60. }
  61. if (!cf->has_section("remap")) {
  62. continue;
  63. }
  64. Vector<String> remaps = cf->get_section_keys("remap");
  65. for (const String &remap : remaps) {
  66. if (remap == "path") {
  67. String remapped_path = cf->get_value("remap", remap);
  68. uint64_t mt = FileAccess::get_modified_time(remapped_path);
  69. _add_file(remapped_path, mt, files_to_send, cached_files);
  70. } else if (remap.begins_with("path.")) {
  71. String feature = remap.get_slicec('.', 1);
  72. if (p_tags.has(feature)) {
  73. String remapped_path = cf->get_value("remap", remap);
  74. uint64_t mt = FileAccess::get_modified_time(remapped_path);
  75. _add_file(remapped_path, mt, files_to_send, cached_files);
  76. }
  77. }
  78. }
  79. } else {
  80. uint64_t mt = efd->get_file_modified_time(i);
  81. _add_file(f, mt, files_to_send, cached_files);
  82. }
  83. }
  84. for (int i = 0; i < efd->get_subdir_count(); i++) {
  85. _scan_files_changed(efd->get_subdir(i), p_tags, files_to_send, cached_files);
  86. }
  87. }
  88. static void _add_custom_file(const String &f, HashMap<String, uint64_t> &files_to_send, HashMap<String, uint64_t> &cached_files) {
  89. if (!FileAccess::exists(f)) {
  90. return;
  91. }
  92. _add_file(f, FileAccess::get_modified_time(f), files_to_send, cached_files);
  93. }
  94. void EditorFileServer::poll() {
  95. if (!active) {
  96. return;
  97. }
  98. if (!server->is_connection_available()) {
  99. return;
  100. }
  101. Ref<StreamPeerTCP> tcp_peer = server->take_connection();
  102. ERR_FAIL_COND(tcp_peer.is_null());
  103. // Got a connection!
  104. EditorProgress pr("updating_remote_file_system", TTR("Updating assets on target device:"), 105);
  105. pr.step(TTR("Syncing headers"), 0, true);
  106. print_verbose("EFS: Connecting taken!");
  107. char header[4];
  108. Error err = tcp_peer->get_data((uint8_t *)&header, 4);
  109. ERR_FAIL_COND(err != OK);
  110. ERR_FAIL_COND(header[0] != 'G');
  111. ERR_FAIL_COND(header[1] != 'R');
  112. ERR_FAIL_COND(header[2] != 'F');
  113. ERR_FAIL_COND(header[3] != 'S');
  114. uint32_t protocol_version = tcp_peer->get_u32();
  115. ERR_FAIL_COND(protocol_version != FILESYSTEM_PROTOCOL_VERSION);
  116. char cpassword[PASSWORD_LENGTH + 1];
  117. err = tcp_peer->get_data((uint8_t *)cpassword, PASSWORD_LENGTH);
  118. cpassword[PASSWORD_LENGTH] = 0;
  119. ERR_FAIL_COND(err != OK);
  120. print_verbose("EFS: Got password: " + String(cpassword));
  121. ERR_FAIL_COND_MSG(password != cpassword, "Client disconnected because password mismatch.");
  122. uint32_t tag_count = tcp_peer->get_u32();
  123. print_verbose("EFS: Getting tags: " + itos(tag_count));
  124. ERR_FAIL_COND(tcp_peer->get_status() != StreamPeerTCP::STATUS_CONNECTED);
  125. Vector<String> tags;
  126. for (uint32_t i = 0; i < tag_count; i++) {
  127. String tag = tcp_peer->get_utf8_string();
  128. print_verbose("EFS: tag #" + itos(i) + ": " + tag);
  129. ERR_FAIL_COND(tcp_peer->get_status() != StreamPeerTCP::STATUS_CONNECTED);
  130. tags.push_back(tag);
  131. }
  132. uint32_t file_buffer_decompressed_size = tcp_peer->get_32();
  133. HashMap<String, uint64_t> cached_files;
  134. if (file_buffer_decompressed_size > 0) {
  135. pr.step(TTR("Getting remote file system"), 1, true);
  136. // Got files cached by client.
  137. uint32_t file_buffer_size = tcp_peer->get_32();
  138. print_verbose("EFS: Getting file buffer: compressed - " + String::humanize_size(file_buffer_size) + " decompressed: " + String::humanize_size(file_buffer_decompressed_size));
  139. ERR_FAIL_COND(tcp_peer->get_status() != StreamPeerTCP::STATUS_CONNECTED);
  140. ERR_FAIL_COND(file_buffer_size > MAX_FILE_BUFFER_SIZE);
  141. LocalVector<uint8_t> file_buffer;
  142. file_buffer.resize(file_buffer_size);
  143. LocalVector<uint8_t> file_buffer_decompressed;
  144. file_buffer_decompressed.resize(file_buffer_decompressed_size);
  145. err = tcp_peer->get_data(file_buffer.ptr(), file_buffer_size);
  146. pr.step(TTR("Decompressing remote file system"), 2, true);
  147. ERR_FAIL_COND(err != OK);
  148. // Decompress the text with all the files
  149. Compression::decompress(file_buffer_decompressed.ptr(), file_buffer_decompressed.size(), file_buffer.ptr(), file_buffer.size(), Compression::MODE_ZSTD);
  150. String files_text = String::utf8((const char *)file_buffer_decompressed.ptr(), file_buffer_decompressed.size());
  151. Vector<String> files = files_text.split("\n");
  152. print_verbose("EFS: Total cached files received: " + itos(files.size()));
  153. for (int i = 0; i < files.size(); i++) {
  154. if (files[i].get_slice_count("::") != 2) {
  155. continue;
  156. }
  157. String file = files[i].get_slice("::", 0);
  158. uint64_t modified_time = files[i].get_slice("::", 1).to_int();
  159. cached_files.insert(file, modified_time);
  160. }
  161. } else {
  162. // Client does not have any files stored.
  163. }
  164. pr.step(TTR("Scanning for local changes"), 3, true);
  165. print_verbose("EFS: Scanning changes:");
  166. HashMap<String, uint64_t> files_to_send;
  167. // Scan files to send.
  168. _scan_files_changed(EditorFileSystem::get_singleton()->get_filesystem(), tags, files_to_send, cached_files);
  169. // Add forced export files
  170. Vector<String> forced_export = EditorExportPlatform::get_forced_export_files(Ref<EditorExportPreset>());
  171. for (int i = 0; i < forced_export.size(); i++) {
  172. _add_custom_file(forced_export[i], files_to_send, cached_files);
  173. }
  174. _add_custom_file("res://project.godot", files_to_send, cached_files);
  175. // Check which files were removed and also add them
  176. for (KeyValue<String, uint64_t> K : cached_files) {
  177. if (!files_to_send.has(K.key)) {
  178. files_to_send.insert(K.key, 0); //0 means removed
  179. }
  180. }
  181. tcp_peer->put_32(files_to_send.size());
  182. print_verbose("EFS: Sending list of changed files.");
  183. pr.step(TTR("Sending list of changed files:"), 4, true);
  184. // Send list of changed files first, to ensure that if connecting breaks, the client is not found in a broken state.
  185. for (KeyValue<String, uint64_t> K : files_to_send) {
  186. tcp_peer->put_utf8_string(K.key);
  187. tcp_peer->put_64(K.value);
  188. }
  189. print_verbose("EFS: Sending " + itos(files_to_send.size()) + " files.");
  190. int idx = 0;
  191. for (KeyValue<String, uint64_t> K : files_to_send) {
  192. pr.step(TTR("Sending file:") + " " + K.key.get_file(), 5 + idx * 100 / files_to_send.size(), false);
  193. idx++;
  194. if (K.value == 0 || !FileAccess::exists("res://" + K.key)) { // File was removed
  195. continue;
  196. }
  197. Vector<uint8_t> array = FileAccess::_get_file_as_bytes("res://" + K.key);
  198. tcp_peer->put_64(array.size());
  199. tcp_peer->put_data(array.ptr(), array.size());
  200. ERR_FAIL_COND(tcp_peer->get_status() != StreamPeerTCP::STATUS_CONNECTED);
  201. }
  202. tcp_peer->put_data((const uint8_t *)"GEND", 4); // End marker.
  203. print_verbose("EFS: Done.");
  204. }
  205. void EditorFileServer::start() {
  206. if (active) {
  207. stop();
  208. }
  209. port = EDITOR_GET("filesystem/file_server/port");
  210. password = EDITOR_GET("filesystem/file_server/password");
  211. Error err = server->listen(port);
  212. ERR_FAIL_COND_MSG(err != OK, "EditorFileServer: Unable to listen on port " + itos(port));
  213. active = true;
  214. }
  215. bool EditorFileServer::is_active() const {
  216. return active;
  217. }
  218. void EditorFileServer::stop() {
  219. if (active) {
  220. server->stop();
  221. active = false;
  222. }
  223. }
  224. EditorFileServer::EditorFileServer() {
  225. server.instantiate();
  226. }
  227. EditorFileServer::~EditorFileServer() {
  228. stop();
  229. }