export.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/zip_io.h"
  31. #include "editor/editor_export.h"
  32. #include "editor/editor_node.h"
  33. #include "main/splash.gen.h"
  34. #include "platform/javascript/logo.gen.h"
  35. #include "platform/javascript/run_icon.gen.h"
  36. #define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
  37. #define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
  38. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  39. GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform);
  40. Ref<ImageTexture> logo;
  41. Ref<ImageTexture> run_icon;
  42. bool runnable_when_last_polled;
  43. void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
  44. public:
  45. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  46. virtual void get_export_options(List<ExportOption> *r_options);
  47. virtual String get_name() const;
  48. virtual String get_os_name() const;
  49. virtual Ref<Texture> get_logo() const;
  50. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  51. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const;
  52. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  53. virtual bool poll_export();
  54. virtual int get_options_count() const;
  55. virtual String get_options_name(int p_index) const { return p_index ? TTR("Stop HTTP Server") : TTR("Run in Browser"); }
  56. 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."); }
  57. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags);
  58. virtual Ref<Texture> get_run_icon() const;
  59. virtual void get_platform_features(List<String> *r_features) {
  60. r_features->push_back("web");
  61. r_features->push_back(get_os_name());
  62. }
  63. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  64. }
  65. EditorExportPlatformJavaScript();
  66. };
  67. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {
  68. String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
  69. String str_export;
  70. Vector<String> lines = str_template.split("\n");
  71. for (int i = 0; i < lines.size(); i++) {
  72. String current_line = lines[i];
  73. current_line = current_line.replace("$GODOT_BASENAME", p_name);
  74. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
  75. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  76. str_export += current_line + "\n";
  77. }
  78. CharString cs = str_export.utf8();
  79. p_html.resize(cs.length());
  80. for (int i = 0; i < cs.length(); i++) {
  81. p_html.write[i] = cs[i];
  82. }
  83. }
  84. void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  85. if (p_preset->get("vram_texture_compression/for_desktop")) {
  86. r_features->push_back("s3tc");
  87. }
  88. if (p_preset->get("vram_texture_compression/for_mobile")) {
  89. String driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name");
  90. if (driver == "GLES2") {
  91. r_features->push_back("etc");
  92. } else if (driver == "GLES3") {
  93. r_features->push_back("etc2");
  94. if (ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2")) {
  95. r_features->push_back("etc");
  96. }
  97. }
  98. }
  99. }
  100. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/custom_html_shell", PROPERTY_HINT_FILE, "*.html"), ""));
  104. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  105. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  106. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  107. }
  108. String EditorExportPlatformJavaScript::get_name() const {
  109. return "HTML5";
  110. }
  111. String EditorExportPlatformJavaScript::get_os_name() const {
  112. return "HTML5";
  113. }
  114. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  115. return logo;
  116. }
  117. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  118. bool valid = false;
  119. String err;
  120. if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) != "")
  121. valid = true;
  122. else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) != "")
  123. valid = true;
  124. if (p_preset->get("custom_template/debug") != "") {
  125. if (FileAccess::exists(p_preset->get("custom_template/debug"))) {
  126. valid = true;
  127. } else {
  128. err += TTR("Custom debug template not found.") + "\n";
  129. }
  130. }
  131. if (p_preset->get("custom_template/release") != "") {
  132. if (FileAccess::exists(p_preset->get("custom_template/release"))) {
  133. valid = true;
  134. } else {
  135. err += TTR("Custom release template not found.") + "\n";
  136. }
  137. }
  138. r_missing_templates = !valid;
  139. if (p_preset->get("vram_texture_compression/for_mobile")) {
  140. String etc_error = test_etc2();
  141. if (etc_error != String()) {
  142. valid = false;
  143. err += etc_error;
  144. }
  145. }
  146. if (!err.empty())
  147. r_error = err;
  148. return valid;
  149. }
  150. List<String> EditorExportPlatformJavaScript::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  151. List<String> list;
  152. list.push_back("html");
  153. return list;
  154. }
  155. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  156. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  157. String custom_debug = p_preset->get("custom_template/debug");
  158. String custom_release = p_preset->get("custom_template/release");
  159. String custom_html = p_preset->get("html/custom_html_shell");
  160. String template_path = p_debug ? custom_debug : custom_release;
  161. template_path = template_path.strip_edges();
  162. if (template_path == String()) {
  163. if (p_debug)
  164. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
  165. else
  166. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
  167. }
  168. if (!DirAccess::exists(p_path.get_base_dir())) {
  169. return ERR_FILE_BAD_PATH;
  170. }
  171. if (template_path != String() && !FileAccess::exists(template_path)) {
  172. EditorNode::get_singleton()->show_warning(TTR("Template file not found:") + "\n" + template_path);
  173. return ERR_FILE_NOT_FOUND;
  174. }
  175. String pck_path = p_path.get_basename() + ".pck";
  176. Error error = save_pack(p_preset, pck_path);
  177. if (error != OK) {
  178. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + pck_path);
  179. return error;
  180. }
  181. FileAccess *src_f = NULL;
  182. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  183. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  184. if (!pkg) {
  185. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:") + "\n" + template_path);
  186. return ERR_FILE_NOT_FOUND;
  187. }
  188. if (unzGoToFirstFile(pkg) != UNZ_OK) {
  189. EditorNode::get_singleton()->show_warning(TTR("Invalid export template:") + "\n" + template_path);
  190. unzClose(pkg);
  191. return ERR_FILE_CORRUPT;
  192. }
  193. do {
  194. //get filename
  195. unz_file_info info;
  196. char fname[16384];
  197. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  198. String file = fname;
  199. Vector<uint8_t> data;
  200. data.resize(info.uncompressed_size);
  201. //read
  202. unzOpenCurrentFile(pkg);
  203. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  204. unzCloseCurrentFile(pkg);
  205. //write
  206. if (file == "godot.html") {
  207. if (!custom_html.empty()) {
  208. continue;
  209. }
  210. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  211. file = p_path.get_file();
  212. } else if (file == "godot.js") {
  213. file = p_path.get_file().get_basename() + ".js";
  214. } else if (file == "godot.wasm") {
  215. file = p_path.get_file().get_basename() + ".wasm";
  216. }
  217. String dst = p_path.get_base_dir().plus_file(file);
  218. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  219. if (!f) {
  220. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + dst);
  221. unzClose(pkg);
  222. return ERR_FILE_CANT_WRITE;
  223. }
  224. f->store_buffer(data.ptr(), data.size());
  225. memdelete(f);
  226. } while (unzGoToNextFile(pkg) == UNZ_OK);
  227. unzClose(pkg);
  228. if (!custom_html.empty()) {
  229. FileAccess *f = FileAccess::open(custom_html, FileAccess::READ);
  230. if (!f) {
  231. EditorNode::get_singleton()->show_warning(TTR("Could not read custom HTML shell:") + "\n" + custom_html);
  232. return ERR_FILE_CANT_READ;
  233. }
  234. Vector<uint8_t> buf;
  235. buf.resize(f->get_len());
  236. f->get_buffer(buf.ptrw(), buf.size());
  237. memdelete(f);
  238. _fix_html(buf, p_preset, p_path.get_file().get_basename(), p_debug);
  239. f = FileAccess::open(p_path, FileAccess::WRITE);
  240. if (!f) {
  241. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + p_path);
  242. return ERR_FILE_CANT_WRITE;
  243. }
  244. f->store_buffer(buf.ptr(), buf.size());
  245. memdelete(f);
  246. }
  247. Ref<Image> splash;
  248. String splash_path = GLOBAL_GET("application/boot_splash/image");
  249. splash_path = splash_path.strip_edges();
  250. if (!splash_path.empty()) {
  251. splash.instance();
  252. Error err = splash->load(splash_path);
  253. if (err) {
  254. EditorNode::get_singleton()->show_warning(TTR("Could not read boot splash image file:") + "\n" + splash_path + "\n" + TTR("Using default boot splash image."));
  255. splash.unref();
  256. }
  257. }
  258. if (splash.is_null()) {
  259. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  260. }
  261. String png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
  262. if (splash->save_png(png_path) != OK) {
  263. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + png_path);
  264. return ERR_FILE_CANT_WRITE;
  265. }
  266. return OK;
  267. }
  268. bool EditorExportPlatformJavaScript::poll_export() {
  269. Ref<EditorExportPreset> preset;
  270. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  271. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  272. if (ep->is_runnable() && ep->get_platform() == this) {
  273. preset = ep;
  274. break;
  275. }
  276. }
  277. bool prev = runnable_when_last_polled;
  278. runnable_when_last_polled = preset.is_valid();
  279. return runnable_when_last_polled != prev;
  280. }
  281. int EditorExportPlatformJavaScript::get_options_count() const {
  282. return runnable_when_last_polled;
  283. }
  284. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags) {
  285. String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
  286. String path = basepath + ".html";
  287. Error err = export_project(p_preset, true, path, p_debug_flags);
  288. if (err != OK) {
  289. // Export generates several files, clean them up on failure.
  290. DirAccess::remove_file_or_error(basepath + ".html");
  291. DirAccess::remove_file_or_error(basepath + ".js");
  292. DirAccess::remove_file_or_error(basepath + ".pck");
  293. DirAccess::remove_file_or_error(basepath + ".png");
  294. DirAccess::remove_file_or_error(basepath + ".wasm");
  295. return err;
  296. }
  297. OS::get_singleton()->shell_open(String("file://") + path);
  298. // FIXME: Find out how to clean up export files after running the successfully
  299. // exported game. Might not be trivial.
  300. return OK;
  301. }
  302. Ref<Texture> EditorExportPlatformJavaScript::get_run_icon() const {
  303. return run_icon;
  304. }
  305. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  306. Ref<Image> img = memnew(Image(_javascript_logo));
  307. logo.instance();
  308. logo->create_from_image(img);
  309. img = Ref<Image>(memnew(Image(_javascript_run_icon)));
  310. run_icon.instance();
  311. run_icon->create_from_image(img);
  312. runnable_when_last_polled = false;
  313. }
  314. void register_javascript_exporter() {
  315. Ref<EditorExportPlatformJavaScript> platform;
  316. platform.instance();
  317. EditorExport::get_singleton()->add_export_platform(platform);
  318. }