export.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/editor_node.h"
  31. #include "editor_export.h"
  32. #include "io/zip_io.h"
  33. #include "platform/javascript/logo.gen.h"
  34. #define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
  35. #define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
  36. #define EXPORT_TEMPLATE_ASMJS_RELEASE "javascript_release.zip"
  37. #define EXPORT_TEMPLATE_ASMJS_DEBUG "javascript_debug.zip"
  38. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  39. GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform)
  40. Ref<ImageTexture> logo;
  41. void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
  42. void _fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size);
  43. public:
  44. enum Target {
  45. TARGET_WEBASSEMBLY,
  46. TARGET_ASMJS
  47. };
  48. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  49. virtual void get_export_options(List<ExportOption> *r_options);
  50. virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;
  51. virtual String get_name() const;
  52. virtual Ref<Texture> get_logo() const;
  53. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  54. virtual String get_binary_extension() const;
  55. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  56. virtual int get_device_count() const { return 1; }
  57. virtual String get_device_name(int p_device) const { return TTR("Run in Browser"); }
  58. virtual String get_device_info(int p_device) const { return TTR("Run exported HTML in the system's default browser."); }
  59. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags);
  60. EditorExportPlatformJavaScript();
  61. };
  62. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {
  63. String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
  64. String str_export;
  65. Vector<String> lines = str_template.split("\n");
  66. int memory_mb;
  67. if (p_preset->get("options/target").operator int() != TARGET_ASMJS)
  68. // WebAssembly allows memory growth, so start with a reasonable default
  69. memory_mb = 1 << 4;
  70. else
  71. memory_mb = 1 << (p_preset->get("options/memory_size").operator int() + 5);
  72. for (int i = 0; i < lines.size(); i++) {
  73. String current_line = lines[i];
  74. current_line = current_line.replace("$GODOT_TMEM", itos(memory_mb * 1024 * 1024));
  75. current_line = current_line.replace("$GODOT_BASE", p_name);
  76. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
  77. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  78. str_export += current_line + "\n";
  79. }
  80. CharString cs = str_export.utf8();
  81. p_html.resize(cs.length());
  82. for (int i = 0; i < cs.length(); i++) {
  83. p_html[i] = cs[i];
  84. }
  85. }
  86. void EditorExportPlatformJavaScript::_fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size) {
  87. String str_template = String::utf8(reinterpret_cast<const char *>(p_js.ptr()), p_js.size());
  88. String str_export;
  89. Vector<String> lines = str_template.split("\n");
  90. for (int i = 0; i < lines.size(); i++) {
  91. if (lines[i].find("$GODOT_PACK_NAME") != -1) {
  92. str_export += lines[i].replace("$GODOT_PACK_NAME", p_pack_name);
  93. } else if (lines[i].find("$GODOT_PACK_SIZE") != -1) {
  94. str_export += lines[i].replace("$GODOT_PACK_SIZE", itos(p_pack_size));
  95. } else {
  96. str_export += lines[i] + "\n";
  97. }
  98. }
  99. CharString cs = str_export.utf8();
  100. p_js.resize(cs.length());
  101. for (int i = 0; i < cs.length(); i++) {
  102. p_js[i] = cs[i];
  103. }
  104. }
  105. void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  106. if (p_preset->get("texture_format/s3tc")) {
  107. r_features->push_back("s3tc");
  108. }
  109. if (p_preset->get("texture_format/etc")) {
  110. r_features->push_back("etc");
  111. }
  112. if (p_preset->get("texture_format/etc2")) {
  113. r_features->push_back("etc2");
  114. }
  115. }
  116. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  117. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "options/target", PROPERTY_HINT_ENUM, "WebAssembly,asm.js"), TARGET_WEBASSEMBLY));
  118. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "options/memory_size", PROPERTY_HINT_ENUM, "32 MB,64 MB,128 MB,256 MB,512 MB,1 GB"), 3));
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), false));
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), true));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  125. }
  126. bool EditorExportPlatformJavaScript::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  127. if (p_option == "options/memory_size") {
  128. return p_options["options/target"].operator int() == TARGET_ASMJS;
  129. }
  130. return true;
  131. }
  132. String EditorExportPlatformJavaScript::get_name() const {
  133. return "HTML5";
  134. }
  135. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  136. return logo;
  137. }
  138. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  139. r_missing_templates = false;
  140. if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
  141. if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) == String())
  142. r_missing_templates = true;
  143. else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) == String())
  144. r_missing_templates = true;
  145. } else {
  146. if (find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE) == String())
  147. r_missing_templates = true;
  148. else if (find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG) == String())
  149. r_missing_templates = true;
  150. }
  151. return !r_missing_templates;
  152. }
  153. String EditorExportPlatformJavaScript::get_binary_extension() const {
  154. return "html";
  155. }
  156. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  157. String custom_debug = p_preset->get("custom_template/debug");
  158. String custom_release = p_preset->get("custom_template/release");
  159. String template_path = p_debug ? custom_debug : custom_release;
  160. template_path = template_path.strip_edges();
  161. if (template_path == String()) {
  162. if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
  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. } else {
  168. if (p_debug)
  169. template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG);
  170. else
  171. template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE);
  172. }
  173. }
  174. if (template_path != String() && !FileAccess::exists(template_path)) {
  175. EditorNode::get_singleton()->show_warning(TTR("Template file not found:\n") + template_path);
  176. return ERR_FILE_NOT_FOUND;
  177. }
  178. String pck_path = p_path.get_basename() + ".pck";
  179. Error error = save_pack(p_preset, pck_path);
  180. if (error != OK) {
  181. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + pck_path);
  182. return error;
  183. }
  184. FileAccess *f = FileAccess::open(pck_path, FileAccess::READ);
  185. if (!f) {
  186. EditorNode::get_singleton()->show_warning(TTR("Could not read file:\n") + pck_path);
  187. return ERR_FILE_CANT_READ;
  188. }
  189. size_t pack_size = f->get_len();
  190. memdelete(f);
  191. FileAccess *src_f = NULL;
  192. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  193. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  194. if (!pkg) {
  195. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:\n") + template_path);
  196. return ERR_FILE_NOT_FOUND;
  197. }
  198. int ret = unzGoToFirstFile(pkg);
  199. while (ret == UNZ_OK) {
  200. //get filename
  201. unz_file_info info;
  202. char fname[16384];
  203. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  204. String file = fname;
  205. Vector<uint8_t> data;
  206. data.resize(info.uncompressed_size);
  207. //read
  208. unzOpenCurrentFile(pkg);
  209. unzReadCurrentFile(pkg, data.ptr(), data.size());
  210. unzCloseCurrentFile(pkg);
  211. //write
  212. if (file == "godot.html") {
  213. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  214. file = p_path.get_file();
  215. } else if (file == "godotfs.js") {
  216. _fix_fsloader_js(data, pck_path.get_file(), pack_size);
  217. file = p_path.get_file().get_basename() + "fs.js";
  218. } else if (file == "godot.js") {
  219. file = p_path.get_file().get_basename() + ".js";
  220. } else if (file == "godot.wasm") {
  221. file = p_path.get_file().get_basename() + ".wasm";
  222. } else if (file == "godot.asm.js") {
  223. file = p_path.get_file().get_basename() + ".asm.js";
  224. } else if (file == "godot.mem") {
  225. file = p_path.get_file().get_basename() + ".mem";
  226. }
  227. String dst = p_path.get_base_dir().plus_file(file);
  228. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  229. if (!f) {
  230. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + dst);
  231. unzClose(pkg);
  232. return ERR_FILE_CANT_WRITE;
  233. }
  234. f->store_buffer(data.ptr(), data.size());
  235. memdelete(f);
  236. ret = unzGoToNextFile(pkg);
  237. }
  238. return OK;
  239. }
  240. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  241. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
  242. Error err = export_project(p_preset, true, path, p_debug_flags);
  243. if (err) {
  244. return err;
  245. }
  246. OS::get_singleton()->shell_open(path);
  247. return OK;
  248. }
  249. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  250. Ref<Image> img = memnew(Image(_javascript_logo));
  251. logo.instance();
  252. logo->create_from_image(img);
  253. }
  254. void register_javascript_exporter() {
  255. Ref<EditorExportPlatformJavaScript> platform;
  256. platform.instance();
  257. EditorExport::get_singleton()->add_export_platform(platform);
  258. }