export.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_node.h"
  32. #include "editor_export.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_devices();
  54. virtual int get_device_count() const;
  55. virtual String get_device_name(int p_device) const { return TTR("Run in Browser"); }
  56. virtual String get_device_info(int p_device) const { return TTR("Run exported HTML in the system's default browser."); }
  57. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, 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. }
  95. }
  96. }
  97. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/custom_html_shell", PROPERTY_HINT_FILE, "*.html"), ""));
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  104. }
  105. String EditorExportPlatformJavaScript::get_name() const {
  106. return "HTML5";
  107. }
  108. String EditorExportPlatformJavaScript::get_os_name() const {
  109. return "HTML5";
  110. }
  111. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  112. return logo;
  113. }
  114. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  115. bool valid = false;
  116. String err;
  117. if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) != "")
  118. valid = true;
  119. else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) != "")
  120. valid = true;
  121. if (p_preset->get("custom_template/debug") != "") {
  122. if (FileAccess::exists(p_preset->get("custom_template/debug"))) {
  123. valid = true;
  124. } else {
  125. err += TTR("Custom debug template not found.") + "\n";
  126. }
  127. }
  128. if (p_preset->get("custom_template/release") != "") {
  129. if (FileAccess::exists(p_preset->get("custom_template/release"))) {
  130. valid = true;
  131. } else {
  132. err += TTR("Custom release template not found.") + "\n";
  133. }
  134. }
  135. r_missing_templates = !valid;
  136. if (p_preset->get("vram_texture_compression/for_mobile")) {
  137. String etc_error = test_etc2();
  138. if (etc_error != String()) {
  139. valid = false;
  140. err += etc_error;
  141. }
  142. }
  143. if (!err.empty())
  144. r_error = err;
  145. return valid;
  146. }
  147. List<String> EditorExportPlatformJavaScript::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  148. List<String> list;
  149. list.push_back("html");
  150. return list;
  151. }
  152. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  153. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  154. String custom_debug = p_preset->get("custom_template/debug");
  155. String custom_release = p_preset->get("custom_template/release");
  156. String custom_html = p_preset->get("html/custom_html_shell");
  157. String template_path = p_debug ? custom_debug : custom_release;
  158. template_path = template_path.strip_edges();
  159. if (template_path == String()) {
  160. if (p_debug)
  161. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
  162. else
  163. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
  164. }
  165. if (template_path != String() && !FileAccess::exists(template_path)) {
  166. EditorNode::get_singleton()->show_warning(TTR("Template file not found:") + "\n" + template_path);
  167. return ERR_FILE_NOT_FOUND;
  168. }
  169. String pck_path = p_path.get_basename() + ".pck";
  170. Error error = save_pack(p_preset, pck_path);
  171. if (error != OK) {
  172. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + pck_path);
  173. return error;
  174. }
  175. FileAccess *src_f = NULL;
  176. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  177. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  178. if (!pkg) {
  179. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:") + "\n" + template_path);
  180. return ERR_FILE_NOT_FOUND;
  181. }
  182. if (unzGoToFirstFile(pkg) != UNZ_OK) {
  183. EditorNode::get_singleton()->show_warning(TTR("Invalid export template:") + "\n" + template_path);
  184. unzClose(pkg);
  185. return ERR_FILE_CORRUPT;
  186. }
  187. do {
  188. //get filename
  189. unz_file_info info;
  190. char fname[16384];
  191. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  192. String file = fname;
  193. Vector<uint8_t> data;
  194. data.resize(info.uncompressed_size);
  195. //read
  196. unzOpenCurrentFile(pkg);
  197. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  198. unzCloseCurrentFile(pkg);
  199. //write
  200. if (file == "godot.html") {
  201. if (!custom_html.empty()) {
  202. continue;
  203. }
  204. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  205. file = p_path.get_file();
  206. } else if (file == "godot.js") {
  207. file = p_path.get_file().get_basename() + ".js";
  208. } else if (file == "godot.wasm") {
  209. file = p_path.get_file().get_basename() + ".wasm";
  210. }
  211. String dst = p_path.get_base_dir().plus_file(file);
  212. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  213. if (!f) {
  214. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + dst);
  215. unzClose(pkg);
  216. return ERR_FILE_CANT_WRITE;
  217. }
  218. f->store_buffer(data.ptr(), data.size());
  219. memdelete(f);
  220. } while (unzGoToNextFile(pkg) == UNZ_OK);
  221. unzClose(pkg);
  222. if (!custom_html.empty()) {
  223. FileAccess *f = FileAccess::open(custom_html, FileAccess::READ);
  224. if (!f) {
  225. EditorNode::get_singleton()->show_warning(TTR("Could not read custom HTML shell:") + "\n" + custom_html);
  226. return ERR_FILE_CANT_READ;
  227. }
  228. Vector<uint8_t> buf;
  229. buf.resize(f->get_len());
  230. f->get_buffer(buf.ptrw(), buf.size());
  231. memdelete(f);
  232. _fix_html(buf, p_preset, p_path.get_file().get_basename(), p_debug);
  233. f = FileAccess::open(p_path, FileAccess::WRITE);
  234. if (!f) {
  235. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + p_path);
  236. return ERR_FILE_CANT_WRITE;
  237. }
  238. f->store_buffer(buf.ptr(), buf.size());
  239. memdelete(f);
  240. }
  241. Ref<Image> splash;
  242. String splash_path = GLOBAL_GET("application/boot_splash/image");
  243. splash_path = splash_path.strip_edges();
  244. if (!splash_path.empty()) {
  245. splash.instance();
  246. Error err = splash->load(splash_path);
  247. if (err) {
  248. EditorNode::get_singleton()->show_warning(TTR("Could not read boot splash image file:") + "\n" + splash_path + "\n" + TTR("Using default boot splash image."));
  249. splash.unref();
  250. }
  251. }
  252. if (splash.is_null()) {
  253. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  254. }
  255. String png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
  256. if (splash->save_png(png_path) != OK) {
  257. EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + png_path);
  258. return ERR_FILE_CANT_WRITE;
  259. }
  260. return OK;
  261. }
  262. bool EditorExportPlatformJavaScript::poll_devices() {
  263. Ref<EditorExportPreset> preset;
  264. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  265. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  266. if (ep->is_runnable() && ep->get_platform() == this) {
  267. preset = ep;
  268. break;
  269. }
  270. }
  271. bool prev = runnable_when_last_polled;
  272. runnable_when_last_polled = preset.is_valid();
  273. return runnable_when_last_polled != prev;
  274. }
  275. int EditorExportPlatformJavaScript::get_device_count() const {
  276. return runnable_when_last_polled;
  277. }
  278. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  279. String path = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_export.html");
  280. Error err = export_project(p_preset, true, path, p_debug_flags);
  281. if (err) {
  282. return err;
  283. }
  284. OS::get_singleton()->shell_open(String("file://") + path);
  285. return OK;
  286. }
  287. Ref<Texture> EditorExportPlatformJavaScript::get_run_icon() const {
  288. return run_icon;
  289. }
  290. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  291. Ref<Image> img = memnew(Image(_javascript_logo));
  292. logo.instance();
  293. logo->create_from_image(img);
  294. img = Ref<Image>(memnew(Image(_javascript_run_icon)));
  295. run_icon.instance();
  296. run_icon->create_from_image(img);
  297. runnable_when_last_polled = false;
  298. }
  299. void register_javascript_exporter() {
  300. Ref<EditorExportPlatformJavaScript> platform;
  301. platform.instance();
  302. EditorExport::get_singleton()->add_export_platform(platform);
  303. }