editor_file_server.cpp 11 KB

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