export.cpp 22 KB

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