export.cpp 20 KB

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