export.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*************************************************************************/
  2. /* export.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 "core/io/tcp_server.h"
  31. #include "core/io/zip_io.h"
  32. #include "editor/editor_export.h"
  33. #include "editor/editor_node.h"
  34. #include "main/splash.gen.h"
  35. #include "platform/javascript/logo.gen.h"
  36. #include "platform/javascript/run_icon.gen.h"
  37. #define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
  38. #define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
  39. class EditorHTTPServer : public Reference {
  40. private:
  41. Ref<TCP_Server> server;
  42. Ref<StreamPeerTCP> connection;
  43. uint64_t time;
  44. uint8_t req_buf[4096];
  45. int req_pos;
  46. void _clear_client() {
  47. connection = Ref<StreamPeerTCP>();
  48. memset(req_buf, 0, sizeof(req_buf));
  49. time = 0;
  50. req_pos = 0;
  51. }
  52. public:
  53. EditorHTTPServer() {
  54. server.instance();
  55. stop();
  56. }
  57. void stop() {
  58. server->stop();
  59. _clear_client();
  60. }
  61. Error listen(int p_port, IP_Address p_address) {
  62. return server->listen(p_port, p_address);
  63. }
  64. bool is_listening() const {
  65. return server->is_listening();
  66. }
  67. void _send_response() {
  68. Vector<String> psa = String((char *)req_buf).split("\r\n");
  69. int len = psa.size();
  70. ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
  71. Vector<String> req = psa[0].split(" ", false);
  72. ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
  73. // Wrong protocol
  74. ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
  75. String filepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
  76. const String basereq = "/tmp_js_export";
  77. String ctype = "";
  78. if (req[1] == basereq + ".html") {
  79. filepath += ".html";
  80. ctype = "text/html";
  81. } else if (req[1] == basereq + ".js") {
  82. filepath += ".js";
  83. ctype = "application/javascript";
  84. } else if (req[1] == basereq + ".worker.js") {
  85. filepath += ".worker.js";
  86. ctype = "application/javascript";
  87. } else if (req[1] == basereq + ".pck") {
  88. filepath += ".pck";
  89. ctype = "application/octet-stream";
  90. } else if (req[1] == basereq + ".png" || req[1] == "/favicon.png") {
  91. // Also allow serving the generated favicon for a smoother loading experience.
  92. if (req[1] == "/favicon.png") {
  93. filepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("favicon.png");
  94. } else {
  95. filepath += ".png";
  96. }
  97. ctype = "image/png";
  98. } else if (req[1] == basereq + ".wasm") {
  99. filepath += ".wasm";
  100. ctype = "application/wasm";
  101. } else {
  102. String s = "HTTP/1.1 404 Not Found\r\n";
  103. s += "Connection: Close\r\n";
  104. s += "\r\n";
  105. CharString cs = s.utf8();
  106. connection->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  107. return;
  108. }
  109. FileAccess *f = FileAccess::open(filepath, FileAccess::READ);
  110. ERR_FAIL_COND(!f);
  111. String s = "HTTP/1.1 200 OK\r\n";
  112. s += "Connection: Close\r\n";
  113. s += "Content-Type: " + ctype + "\r\n";
  114. s += "\r\n";
  115. CharString cs = s.utf8();
  116. Error err = connection->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  117. if (err != OK) {
  118. memdelete(f);
  119. ERR_FAIL();
  120. }
  121. while (true) {
  122. uint8_t bytes[4096];
  123. int read = f->get_buffer(bytes, 4096);
  124. if (read < 1) {
  125. break;
  126. }
  127. err = connection->put_data(bytes, read);
  128. if (err != OK) {
  129. memdelete(f);
  130. ERR_FAIL();
  131. }
  132. }
  133. memdelete(f);
  134. }
  135. void poll() {
  136. if (!server->is_listening())
  137. return;
  138. if (connection.is_null()) {
  139. if (!server->is_connection_available())
  140. return;
  141. connection = server->take_connection();
  142. time = OS::get_singleton()->get_ticks_usec();
  143. }
  144. if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
  145. _clear_client();
  146. return;
  147. }
  148. if (connection->get_status() != StreamPeerTCP::STATUS_CONNECTED)
  149. return;
  150. while (true) {
  151. char *r = (char *)req_buf;
  152. int l = req_pos - 1;
  153. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  154. _send_response();
  155. _clear_client();
  156. return;
  157. }
  158. int read = 0;
  159. ERR_FAIL_COND(req_pos >= 4096);
  160. Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
  161. if (err != OK) {
  162. // Got an error
  163. _clear_client();
  164. return;
  165. } else if (read != 1) {
  166. // Busy, wait next poll
  167. return;
  168. }
  169. req_pos += read;
  170. }
  171. }
  172. };
  173. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  174. GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform);
  175. Ref<ImageTexture> logo;
  176. Ref<ImageTexture> run_icon;
  177. Ref<ImageTexture> stop_icon;
  178. int menu_options;
  179. void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
  180. private:
  181. Ref<EditorHTTPServer> server;
  182. bool server_quit;
  183. Mutex *server_lock;
  184. Thread *server_thread;
  185. static void _server_thread_poll(void *data);
  186. public:
  187. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  188. virtual void get_export_options(List<ExportOption> *r_options);
  189. virtual String get_name() const;
  190. virtual String get_os_name() const;
  191. virtual Ref<Texture> get_logo() const;
  192. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  193. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const;
  194. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  195. virtual bool poll_export();
  196. virtual int get_options_count() const;
  197. virtual String get_option_label(int p_index) const { return p_index ? TTR("Stop HTTP Server") : TTR("Run in Browser"); }
  198. virtual String get_option_tooltip(int p_index) const { return p_index ? TTR("Stop HTTP Server") : TTR("Run exported HTML in the system's default browser."); }
  199. virtual Ref<ImageTexture> get_option_icon(int p_index) const;
  200. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags);
  201. virtual Ref<Texture> get_run_icon() const;
  202. virtual void get_platform_features(List<String> *r_features) {
  203. r_features->push_back("web");
  204. r_features->push_back(get_os_name());
  205. }
  206. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  207. }
  208. EditorExportPlatformJavaScript();
  209. ~EditorExportPlatformJavaScript();
  210. };
  211. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {
  212. String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
  213. String str_export;
  214. Vector<String> lines = str_template.split("\n");
  215. for (int i = 0; i < lines.size(); i++) {
  216. String current_line = lines[i];
  217. current_line = current_line.replace("$GODOT_BASENAME", p_name);
  218. current_line = current_line.replace("$GODOT_PROJECT_NAME", ProjectSettings::get_singleton()->get_setting("application/config/name"));
  219. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
  220. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  221. str_export += current_line + "\n";
  222. }
  223. CharString cs = str_export.utf8();
  224. p_html.resize(cs.length());
  225. for (int i = 0; i < cs.length(); i++) {
  226. p_html.write[i] = cs[i];
  227. }
  228. }
  229. void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  230. if (p_preset->get("vram_texture_compression/for_desktop")) {
  231. r_features->push_back("s3tc");
  232. }
  233. if (p_preset->get("vram_texture_compression/for_mobile")) {
  234. String driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name");
  235. if (driver == "GLES2") {
  236. r_features->push_back("etc");
  237. } else if (driver == "GLES3") {
  238. r_features->push_back("etc2");
  239. if (ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2")) {
  240. r_features->push_back("etc");
  241. }
  242. }
  243. }
  244. }
  245. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  246. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
  247. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer
  248. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/custom_html_shell", PROPERTY_HINT_FILE, "*.html"), ""));
  249. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  250. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  251. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  252. }
  253. String EditorExportPlatformJavaScript::get_name() const {
  254. return "HTML5";
  255. }
  256. String EditorExportPlatformJavaScript::get_os_name() const {
  257. return "HTML5";
  258. }
  259. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  260. return logo;
  261. }
  262. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  263. String err;
  264. bool valid = false;
  265. // Look for export templates (first official, and if defined custom templates).
  266. bool dvalid = exists_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG, &err);
  267. bool rvalid = exists_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE, &err);
  268. if (p_preset->get("custom_template/debug") != "") {
  269. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  270. if (!dvalid) {
  271. err += TTR("Custom debug template not found.") + "\n";
  272. }
  273. }
  274. if (p_preset->get("custom_template/release") != "") {
  275. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  276. if (!rvalid) {
  277. err += TTR("Custom release template not found.") + "\n";
  278. }
  279. }
  280. valid = dvalid || rvalid;
  281. r_missing_templates = !valid;
  282. // Validate the rest of the configuration.
  283. if (p_preset->get("vram_texture_compression/for_mobile")) {
  284. String etc_error = test_etc2();
  285. if (etc_error != String()) {
  286. valid = false;
  287. err += etc_error;
  288. }
  289. }
  290. if (!err.empty())
  291. r_error = err;
  292. return valid;
  293. }
  294. List<String> EditorExportPlatformJavaScript::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  295. List<String> list;
  296. list.push_back("html");
  297. return list;
  298. }
  299. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  300. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  301. String custom_debug = p_preset->get("custom_template/debug");
  302. String custom_release = p_preset->get("custom_template/release");
  303. String custom_html = p_preset->get("html/custom_html_shell");
  304. String template_path = p_debug ? custom_debug : custom_release;
  305. template_path = template_path.strip_edges();
  306. if (template_path == String()) {
  307. if (p_debug)
  308. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
  309. else
  310. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
  311. }
  312. if (!DirAccess::exists(p_path.get_base_dir())) {
  313. return ERR_FILE_BAD_PATH;
  314. }
  315. if (template_path != String() && !FileAccess::exists(template_path)) {
  316. EditorNode::get_singleton()->show_warning(TTR("Template file not found:") + "\n" + template_path);
  317. return ERR_FILE_NOT_FOUND;
  318. }
  319. String pck_path = p_path.get_basename() + ".pck";
  320. Error error = save_pack(p_preset, pck_path);
  321. if (error != OK) {
  322. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + pck_path);
  323. return error;
  324. }
  325. FileAccess *src_f = NULL;
  326. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  327. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  328. if (!pkg) {
  329. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:") + "\n" + template_path);
  330. return ERR_FILE_NOT_FOUND;
  331. }
  332. if (unzGoToFirstFile(pkg) != UNZ_OK) {
  333. EditorNode::get_singleton()->show_warning(TTR("Invalid export template:") + "\n" + template_path);
  334. unzClose(pkg);
  335. return ERR_FILE_CORRUPT;
  336. }
  337. do {
  338. //get filename
  339. unz_file_info info;
  340. char fname[16384];
  341. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  342. String file = fname;
  343. Vector<uint8_t> data;
  344. data.resize(info.uncompressed_size);
  345. //read
  346. unzOpenCurrentFile(pkg);
  347. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  348. unzCloseCurrentFile(pkg);
  349. //write
  350. if (file == "godot.html") {
  351. if (!custom_html.empty()) {
  352. continue;
  353. }
  354. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  355. file = p_path.get_file();
  356. } else if (file == "godot.js") {
  357. file = p_path.get_file().get_basename() + ".js";
  358. } else if (file == "godot.worker.js") {
  359. file = p_path.get_file().get_basename() + ".worker.js";
  360. } else if (file == "godot.wasm") {
  361. file = p_path.get_file().get_basename() + ".wasm";
  362. }
  363. String dst = p_path.get_base_dir().plus_file(file);
  364. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  365. if (!f) {
  366. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + dst);
  367. unzClose(pkg);
  368. return ERR_FILE_CANT_WRITE;
  369. }
  370. f->store_buffer(data.ptr(), data.size());
  371. memdelete(f);
  372. } while (unzGoToNextFile(pkg) == UNZ_OK);
  373. unzClose(pkg);
  374. if (!custom_html.empty()) {
  375. FileAccess *f = FileAccess::open(custom_html, FileAccess::READ);
  376. if (!f) {
  377. EditorNode::get_singleton()->show_warning(TTR("Could not read custom HTML shell:") + "\n" + custom_html);
  378. return ERR_FILE_CANT_READ;
  379. }
  380. Vector<uint8_t> buf;
  381. buf.resize(f->get_len());
  382. f->get_buffer(buf.ptrw(), buf.size());
  383. memdelete(f);
  384. _fix_html(buf, p_preset, p_path.get_file().get_basename(), p_debug);
  385. f = FileAccess::open(p_path, FileAccess::WRITE);
  386. if (!f) {
  387. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + p_path);
  388. return ERR_FILE_CANT_WRITE;
  389. }
  390. f->store_buffer(buf.ptr(), buf.size());
  391. memdelete(f);
  392. }
  393. Ref<Image> splash;
  394. const String splash_path = String(GLOBAL_GET("application/boot_splash/image")).strip_edges();
  395. if (!splash_path.empty()) {
  396. splash.instance();
  397. const Error err = splash->load(splash_path);
  398. if (err) {
  399. EditorNode::get_singleton()->show_warning(TTR("Could not read boot splash image file:") + "\n" + splash_path + "\n" + TTR("Using default boot splash image."));
  400. splash.unref();
  401. }
  402. }
  403. if (splash.is_null()) {
  404. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  405. }
  406. const String splash_png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
  407. if (splash->save_png(splash_png_path) != OK) {
  408. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + splash_png_path);
  409. return ERR_FILE_CANT_WRITE;
  410. }
  411. // Save a favicon that can be accessed without waiting for the project to finish loading.
  412. // This way, the favicon can be displayed immediately when loading the page.
  413. Ref<Image> favicon;
  414. const String favicon_path = String(GLOBAL_GET("application/config/icon")).strip_edges();
  415. if (!favicon_path.empty()) {
  416. favicon.instance();
  417. const Error err = favicon->load(favicon_path);
  418. if (err) {
  419. favicon.unref();
  420. }
  421. }
  422. if (favicon.is_valid()) {
  423. const String favicon_png_path = p_path.get_base_dir().plus_file("favicon.png");
  424. if (favicon->save_png(favicon_png_path) != OK) {
  425. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + favicon_png_path);
  426. return ERR_FILE_CANT_WRITE;
  427. }
  428. }
  429. return OK;
  430. }
  431. bool EditorExportPlatformJavaScript::poll_export() {
  432. Ref<EditorExportPreset> preset;
  433. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  434. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  435. if (ep->is_runnable() && ep->get_platform() == this) {
  436. preset = ep;
  437. break;
  438. }
  439. }
  440. int prev = menu_options;
  441. menu_options = preset.is_valid();
  442. if (server->is_listening()) {
  443. if (menu_options == 0) {
  444. server_lock->lock();
  445. server->stop();
  446. server_lock->unlock();
  447. } else {
  448. menu_options += 1;
  449. }
  450. }
  451. return menu_options != prev;
  452. }
  453. Ref<ImageTexture> EditorExportPlatformJavaScript::get_option_icon(int p_index) const {
  454. return p_index == 1 ? stop_icon : EditorExportPlatform::get_option_icon(p_index);
  455. }
  456. int EditorExportPlatformJavaScript::get_options_count() const {
  457. return menu_options;
  458. }
  459. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags) {
  460. if (p_option == 1) {
  461. server_lock->lock();
  462. server->stop();
  463. server_lock->unlock();
  464. return OK;
  465. }
  466. const String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
  467. Error err = export_project(p_preset, true, basepath + ".html", p_debug_flags);
  468. if (err != OK) {
  469. // Export generates several files, clean them up on failure.
  470. DirAccess::remove_file_or_error(basepath + ".html");
  471. DirAccess::remove_file_or_error(basepath + ".js");
  472. DirAccess::remove_file_or_error(basepath + ".worker.js");
  473. DirAccess::remove_file_or_error(basepath + ".pck");
  474. DirAccess::remove_file_or_error(basepath + ".png");
  475. DirAccess::remove_file_or_error(basepath + ".wasm");
  476. DirAccess::remove_file_or_error(EditorSettings::get_singleton()->get_cache_dir().plus_file("favicon.png"));
  477. return err;
  478. }
  479. const uint16_t bind_port = EDITOR_GET("export/web/http_port");
  480. // Resolve host if needed.
  481. const String bind_host = EDITOR_GET("export/web/http_host");
  482. IP_Address bind_ip;
  483. if (bind_host.is_valid_ip_address()) {
  484. bind_ip = bind_host;
  485. } else {
  486. bind_ip = IP::get_singleton()->resolve_hostname(bind_host);
  487. }
  488. ERR_FAIL_COND_V_MSG(!bind_ip.is_valid(), ERR_INVALID_PARAMETER, "Invalid editor setting 'export/web/http_host': '" + bind_host + "'. Try using '127.0.0.1'.");
  489. // Restart server.
  490. server_lock->lock();
  491. server->stop();
  492. err = server->listen(bind_port, bind_ip);
  493. server_lock->unlock();
  494. ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to start HTTP server.");
  495. OS::get_singleton()->shell_open(String("http://" + bind_host + ":" + itos(bind_port) + "/tmp_js_export.html"));
  496. // FIXME: Find out how to clean up export files after running the successfully
  497. // exported game. Might not be trivial.
  498. return OK;
  499. }
  500. Ref<Texture> EditorExportPlatformJavaScript::get_run_icon() const {
  501. return run_icon;
  502. }
  503. void EditorExportPlatformJavaScript::_server_thread_poll(void *data) {
  504. EditorExportPlatformJavaScript *ej = (EditorExportPlatformJavaScript *)data;
  505. while (!ej->server_quit) {
  506. OS::get_singleton()->delay_usec(1000);
  507. ej->server_lock->lock();
  508. ej->server->poll();
  509. ej->server_lock->unlock();
  510. }
  511. }
  512. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  513. server.instance();
  514. server_quit = false;
  515. server_lock = Mutex::create();
  516. server_thread = Thread::create(_server_thread_poll, this);
  517. Ref<Image> img = memnew(Image(_javascript_logo));
  518. logo.instance();
  519. logo->create_from_image(img);
  520. img = Ref<Image>(memnew(Image(_javascript_run_icon)));
  521. run_icon.instance();
  522. run_icon->create_from_image(img);
  523. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  524. if (theme.is_valid())
  525. stop_icon = theme->get_icon("Stop", "EditorIcons");
  526. else
  527. stop_icon.instance();
  528. menu_options = 0;
  529. }
  530. EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
  531. server->stop();
  532. server_quit = true;
  533. Thread::wait_to_finish(server_thread);
  534. memdelete(server_lock);
  535. memdelete(server_thread);
  536. }
  537. void register_javascript_exporter() {
  538. EDITOR_DEF("export/web/http_host", "localhost");
  539. EDITOR_DEF("export/web/http_port", 8060);
  540. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "export/web/http_port", PROPERTY_HINT_RANGE, "1,65535,1"));
  541. Ref<EditorExportPlatformJavaScript> platform;
  542. platform.instance();
  543. EditorExport::get_singleton()->add_export_platform(platform);
  544. }