export.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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. #include "platform/javascript/run_icon.gen.h"
  35. #define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
  36. #define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
  37. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  38. GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform)
  39. Ref<ImageTexture> logo;
  40. Ref<ImageTexture> run_icon;
  41. bool runnable_when_last_polled;
  42. void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
  43. public:
  44. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  45. virtual void get_export_options(List<ExportOption> *r_options);
  46. virtual String get_name() const;
  47. virtual String get_os_name() const;
  48. virtual Ref<Texture> get_logo() const;
  49. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  50. virtual String get_binary_extension() const;
  51. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  52. virtual bool poll_devices();
  53. virtual int get_device_count() const;
  54. virtual String get_device_name(int p_device) const { return TTR("Run in Browser"); }
  55. virtual String get_device_info(int p_device) const { return TTR("Run exported HTML in the system's default browser."); }
  56. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags);
  57. virtual Ref<Texture> get_run_icon() const;
  58. virtual void get_platform_features(List<String> *r_features) {
  59. r_features->push_back("web");
  60. r_features->push_back("JavaScript");
  61. }
  62. EditorExportPlatformJavaScript();
  63. };
  64. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {
  65. String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
  66. String str_export;
  67. Vector<String> lines = str_template.split("\n");
  68. for (int i = 0; i < lines.size(); i++) {
  69. String current_line = lines[i];
  70. current_line = current_line.replace("$GODOT_BASENAME", p_name);
  71. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
  72. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  73. str_export += current_line + "\n";
  74. }
  75. CharString cs = str_export.utf8();
  76. p_html.resize(cs.length());
  77. for (int i = 0; i < cs.length(); i++) {
  78. p_html[i] = cs[i];
  79. }
  80. }
  81. void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  82. if (p_preset->get("texture_format/s3tc")) {
  83. r_features->push_back("s3tc");
  84. }
  85. if (p_preset->get("texture_format/etc")) {
  86. r_features->push_back("etc");
  87. }
  88. if (p_preset->get("texture_format/etc2")) {
  89. r_features->push_back("etc2");
  90. }
  91. }
  92. void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), false));
  94. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), true));
  95. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  96. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
  97. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  99. }
  100. String EditorExportPlatformJavaScript::get_name() const {
  101. return "HTML5";
  102. }
  103. String EditorExportPlatformJavaScript::get_os_name() const {
  104. return "JavaScript";
  105. }
  106. Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {
  107. return logo;
  108. }
  109. bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  110. r_missing_templates = false;
  111. if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) == String())
  112. r_missing_templates = true;
  113. else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) == String())
  114. r_missing_templates = true;
  115. return !r_missing_templates;
  116. }
  117. String EditorExportPlatformJavaScript::get_binary_extension() const {
  118. return "html";
  119. }
  120. Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  121. String custom_debug = p_preset->get("custom_template/debug");
  122. String custom_release = p_preset->get("custom_template/release");
  123. String template_path = p_debug ? custom_debug : custom_release;
  124. template_path = template_path.strip_edges();
  125. if (template_path == String()) {
  126. if (p_debug)
  127. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
  128. else
  129. template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
  130. }
  131. if (template_path != String() && !FileAccess::exists(template_path)) {
  132. EditorNode::get_singleton()->show_warning(TTR("Template file not found:\n") + template_path);
  133. return ERR_FILE_NOT_FOUND;
  134. }
  135. String pck_path = p_path.get_basename() + ".pck";
  136. Error error = save_pack(p_preset, pck_path);
  137. if (error != OK) {
  138. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + pck_path);
  139. return error;
  140. }
  141. FileAccess *f = FileAccess::open(pck_path, FileAccess::READ);
  142. if (!f) {
  143. EditorNode::get_singleton()->show_warning(TTR("Could not read file:\n") + pck_path);
  144. return ERR_FILE_CANT_READ;
  145. }
  146. size_t pack_size = f->get_len();
  147. memdelete(f);
  148. FileAccess *src_f = NULL;
  149. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  150. unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);
  151. if (!pkg) {
  152. EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:\n") + template_path);
  153. return ERR_FILE_NOT_FOUND;
  154. }
  155. int ret = unzGoToFirstFile(pkg);
  156. while (ret == UNZ_OK) {
  157. //get filename
  158. unz_file_info info;
  159. char fname[16384];
  160. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  161. String file = fname;
  162. Vector<uint8_t> data;
  163. data.resize(info.uncompressed_size);
  164. //read
  165. unzOpenCurrentFile(pkg);
  166. unzReadCurrentFile(pkg, data.ptr(), data.size());
  167. unzCloseCurrentFile(pkg);
  168. //write
  169. if (file == "godot.html") {
  170. _fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
  171. file = p_path.get_file();
  172. } else if (file == "godot.js") {
  173. file = p_path.get_file().get_basename() + ".js";
  174. } else if (file == "godot.wasm") {
  175. file = p_path.get_file().get_basename() + ".wasm";
  176. }
  177. String dst = p_path.get_base_dir().plus_file(file);
  178. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  179. if (!f) {
  180. EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + dst);
  181. unzClose(pkg);
  182. return ERR_FILE_CANT_WRITE;
  183. }
  184. f->store_buffer(data.ptr(), data.size());
  185. memdelete(f);
  186. ret = unzGoToNextFile(pkg);
  187. }
  188. return OK;
  189. }
  190. bool EditorExportPlatformJavaScript::poll_devices() {
  191. Ref<EditorExportPreset> preset;
  192. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  193. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  194. if (ep->is_runnable() && ep->get_platform() == this) {
  195. preset = ep;
  196. break;
  197. }
  198. }
  199. bool prev = runnable_when_last_polled;
  200. runnable_when_last_polled = preset.is_valid();
  201. return runnable_when_last_polled != prev;
  202. }
  203. int EditorExportPlatformJavaScript::get_device_count() const {
  204. return runnable_when_last_polled;
  205. }
  206. Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  207. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
  208. Error err = export_project(p_preset, true, path, p_debug_flags);
  209. if (err) {
  210. return err;
  211. }
  212. OS::get_singleton()->shell_open(path);
  213. return OK;
  214. }
  215. Ref<Texture> EditorExportPlatformJavaScript::get_run_icon() const {
  216. return run_icon;
  217. }
  218. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  219. Ref<Image> img = memnew(Image(_javascript_logo));
  220. logo.instance();
  221. logo->create_from_image(img);
  222. img = Ref<Image>(memnew(Image(_javascript_run_icon)));
  223. run_icon.instance();
  224. run_icon->create_from_image(img);
  225. runnable_when_last_polled = false;
  226. }
  227. void register_javascript_exporter() {
  228. Ref<EditorExportPlatformJavaScript> platform;
  229. platform.instance();
  230. EditorExport::get_singleton()->add_export_platform(platform);
  231. }