export.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "editor/editor_node.h"
  30. #include "editor_export.h"
  31. #include "io/zip_io.h"
  32. #include "platform/javascript/logo.h"
  33. #define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
  34. #define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
  35. #define EXPORT_TEMPLATE_ASMJS_RELEASE "javascript_release.zip"
  36. #define EXPORT_TEMPLATE_ASMJS_DEBUG "javascript_debug.zip"
  37. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  38. GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform)
  39. Ref<ImageTexture> logo;
  40. void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
  41. void _fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size);
  42. public:
  43. enum Target {
  44. TARGET_WEBASSEMBLY,
  45. TARGET_ASMJS
  46. };
  47. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  48. virtual void get_export_options(List<ExportOption> *r_options);
  49. virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;
  50. virtual String get_name() const;
  51. virtual Ref<Texture> get_logo() const;
  52. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  53. virtual String get_binary_extension() const;
  54. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  55. virtual int get_device_count() const { return 1; }
  56. virtual String get_device_name(int p_device) const { return TTR("Run in Browser"); }
  57. virtual String get_device_info(int p_device) const { return TTR("Run exported HTML in the system's default browser."); }
  58. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags);
  59. EditorExportPlatformJavaScript();
  60. };
  61. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {
  62. String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
  63. String str_export;
  64. Vector<String> lines = str_template.split("\n");
  65. int memory_mb;
  66. if (p_preset->get("options/target").operator int() != TARGET_ASMJS)
  67. // WebAssembly allows memory growth, so start with a reasonable default
  68. memory_mb = 1 << 4;
  69. else
  70. memory_mb = 1 << (p_preset->get("options/memory_size").operator int() + 5);
  71. for (int i = 0; i < lines.size(); i++) {
  72. String current_line = lines[i];
  73. current_line = current_line.replace("$GODOT_TMEM", itos(memory_mb * 1024 * 1024));
  74. current_line = current_line.replace("$GODOT_BASE", p_name);
  75. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
  76. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  77. str_export += current_line + "\n";
  78. }
  79. CharString cs = str_export.utf8();
  80. p_html.resize(cs.length());
  81. for (int i = 0; i < cs.length(); i++) {
  82. p_html[i] = cs[i];
  83. }
  84. }
  85. void EditorExportPlatformJavaScript::_fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size) {
  86. String str_template = String::utf8(reinterpret_cast<const char *>(p_js.ptr()), p_js.size());
  87. String str_export;
  88. Vector<String> lines = str_template.split("\n");
  89. for (int i = 0; i < lines.size(); i++) {
  90. if (lines[i].find("$GODOT_PACK_NAME") != -1) {
  91. str_export += lines[i].replace("$GODOT_PACK_NAME", p_pack_name);
  92. } else if (lines[i].find("$GODOT_PACK_SIZE") != -1) {
  93. str_export += lines[i].replace("$GODOT_PACK_SIZE", itos(p_pack_size));
  94. } else {
  95. str_export += lines[i] + "\n";
  96. }
  97. }
  98. CharString cs = str_export.utf8();
  99. p_js.resize(cs.length());
  100. for (int i = 0; i < cs.length(); i++) {
  101. p_js[i] = cs[i];
  102. }
  103. }
  104. void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  105. if (p_preset->get("texture_format/s3tc")) {
  106. r_features->push_back("s3tc");
  107. }
  108. if (p_preset->get("texture_format/etc")) {
  109. r_features->push_back("etc");
  110. }
  111. if (p_preset->get("texture_format/etc2")) {
  112. r_features->push_back("etc2");
  113. }
  114. }
  115. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "options/target", PROPERTY_HINT_ENUM, "WebAssembly,asm.js"), TARGET_WEBASSEMBLY));
  117. 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));
  118. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), false));
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), true));
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  124. }
  125. bool EditorExportPlatformJavaScript::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  126. if (p_option == "options/memory_size") {
  127. return p_options["options/target"].operator int() == TARGET_ASMJS;
  128. }
  129. return true;
  130. }
  131. String EditorExportPlatformJavaScript::get_name() const {
  132. return "HTML5";
  133. }
  134. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  135. return logo;
  136. }
  137. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  138. r_missing_templates = false;
  139. if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
  140. if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) == String())
  141. r_missing_templates = true;
  142. else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) == String())
  143. r_missing_templates = true;
  144. } else {
  145. if (find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE) == String())
  146. r_missing_templates = true;
  147. else if (find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG) == String())
  148. r_missing_templates = true;
  149. }
  150. return !r_missing_templates;
  151. }
  152. String EditorExportPlatformJavaScript::get_binary_extension() const {
  153. return "html";
  154. }
  155. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  156. String custom_debug = p_preset->get("custom_template/debug");
  157. String custom_release = p_preset->get("custom_template/release");
  158. String template_path = p_debug ? custom_debug : custom_release;
  159. template_path = template_path.strip_edges();
  160. if (template_path == String()) {
  161. if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
  162. if (p_debug)
  163. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
  164. else
  165. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
  166. } else {
  167. if (p_debug)
  168. template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG);
  169. else
  170. template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE);
  171. }
  172. }
  173. if (template_path != String() && !FileAccess::exists(template_path)) {
  174. EditorNode::get_singleton()->show_warning(TTR("Template file not found:\n") + template_path);
  175. return ERR_FILE_NOT_FOUND;
  176. }
  177. String pck_path = p_path.get_basename() + ".pck";
  178. Error error = save_pack(p_preset, pck_path);
  179. if (error != OK) {
  180. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + pck_path);
  181. return error;
  182. }
  183. FileAccess *f = FileAccess::open(pck_path, FileAccess::READ);
  184. if (!f) {
  185. EditorNode::get_singleton()->show_warning(TTR("Could not read file:\n") + pck_path);
  186. return ERR_FILE_CANT_READ;
  187. }
  188. size_t pack_size = f->get_len();
  189. memdelete(f);
  190. FileAccess *src_f = NULL;
  191. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  192. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  193. if (!pkg) {
  194. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:\n") + template_path);
  195. return ERR_FILE_NOT_FOUND;
  196. }
  197. int ret = unzGoToFirstFile(pkg);
  198. while (ret == UNZ_OK) {
  199. //get filename
  200. unz_file_info info;
  201. char fname[16384];
  202. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  203. String file = fname;
  204. Vector<uint8_t> data;
  205. data.resize(info.uncompressed_size);
  206. //read
  207. unzOpenCurrentFile(pkg);
  208. unzReadCurrentFile(pkg, data.ptr(), data.size());
  209. unzCloseCurrentFile(pkg);
  210. //write
  211. if (file == "godot.html") {
  212. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  213. file = p_path.get_file();
  214. } else if (file == "godotfs.js") {
  215. _fix_fsloader_js(data, pck_path.get_file(), pack_size);
  216. file = p_path.get_file().get_basename() + "fs.js";
  217. } else if (file == "godot.js") {
  218. file = p_path.get_file().get_basename() + ".js";
  219. } else if (file == "godot.wasm") {
  220. file = p_path.get_file().get_basename() + ".wasm";
  221. } else if (file == "godot.asm.js") {
  222. file = p_path.get_file().get_basename() + ".asm.js";
  223. } else if (file == "godot.mem") {
  224. file = p_path.get_file().get_basename() + ".mem";
  225. }
  226. String dst = p_path.get_base_dir().plus_file(file);
  227. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  228. if (!f) {
  229. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + dst);
  230. unzClose(pkg);
  231. return ERR_FILE_CANT_WRITE;
  232. }
  233. f->store_buffer(data.ptr(), data.size());
  234. memdelete(f);
  235. ret = unzGoToNextFile(pkg);
  236. }
  237. return OK;
  238. }
  239. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  240. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
  241. Error err = export_project(p_preset, true, path, p_debug_flags);
  242. if (err) {
  243. return err;
  244. }
  245. OS::get_singleton()->shell_open(path);
  246. return OK;
  247. }
  248. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  249. Image img(_javascript_logo);
  250. logo.instance();
  251. logo->create_from_image(img);
  252. }
  253. void register_javascript_exporter() {
  254. Ref<EditorExportPlatformJavaScript> platform;
  255. platform.instance();
  256. EditorExport::get_singleton()->add_export_platform(platform);
  257. }