export.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "export.h"
  31. #include "editor/editor_import_export.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "globals.h"
  35. #include "io/marshalls.h"
  36. #include "io/zip_io.h"
  37. #include "os/file_access.h"
  38. #include "os/os.h"
  39. #include "platform/javascript/logo.gen.h"
  40. #include "string.h"
  41. #include "version.h"
  42. class EditorExportPlatformJavaScript : public EditorExportPlatform {
  43. OBJ_TYPE(EditorExportPlatformJavaScript, EditorExportPlatform);
  44. String custom_release_package;
  45. String custom_debug_package;
  46. enum PackMode {
  47. PACK_SINGLE_FILE,
  48. PACK_MULTIPLE_FILES
  49. };
  50. void _fix_html(Vector<uint8_t> &p_html, const String &p_name, bool p_debug);
  51. PackMode pack_mode;
  52. bool show_run;
  53. int max_memory;
  54. int version_code;
  55. String html_title;
  56. String html_head_include;
  57. String html_font_family;
  58. String html_style_include;
  59. bool html_controls_enabled;
  60. Ref<ImageTexture> logo;
  61. protected:
  62. bool _set(const StringName &p_name, const Variant &p_value);
  63. bool _get(const StringName &p_name, Variant &r_ret) const;
  64. void _get_property_list(List<PropertyInfo> *p_list) const;
  65. public:
  66. virtual String get_name() const { return "HTML5"; }
  67. virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_BC; }
  68. virtual Ref<Texture> get_logo() const { return logo; }
  69. virtual bool poll_devices() { return show_run ? true : false; }
  70. virtual int get_device_count() const { return show_run ? 1 : 0; };
  71. virtual String get_device_name(int p_device) const { return "Run in Browser"; }
  72. virtual String get_device_info(int p_device) const { return "Run exported HTML in the system's default browser."; }
  73. virtual Error run(int p_device, int p_flags = 0);
  74. virtual bool requires_password(bool p_debug) const { return false; }
  75. virtual String get_binary_extension() const { return "html"; }
  76. virtual Error export_project(const String &p_path, bool p_debug, int p_flags = 0);
  77. virtual bool can_export(String *r_error = NULL) const;
  78. EditorExportPlatformJavaScript();
  79. ~EditorExportPlatformJavaScript();
  80. };
  81. bool EditorExportPlatformJavaScript::_set(const StringName &p_name, const Variant &p_value) {
  82. String n = p_name;
  83. if (n == "custom_package/debug")
  84. custom_debug_package = p_value;
  85. else if (n == "custom_package/release")
  86. custom_release_package = p_value;
  87. else if (n == "browser/enable_run")
  88. show_run = p_value;
  89. else if (n == "options/memory_size")
  90. max_memory = p_value;
  91. else if (n == "html/title")
  92. html_title = p_value;
  93. else if (n == "html/head_include")
  94. html_head_include = p_value;
  95. else if (n == "html/font_family")
  96. html_font_family = p_value;
  97. else if (n == "html/style_include")
  98. html_style_include = p_value;
  99. else if (n == "html/controls_enabled")
  100. html_controls_enabled = p_value;
  101. else
  102. return false;
  103. return true;
  104. }
  105. bool EditorExportPlatformJavaScript::_get(const StringName &p_name, Variant &r_ret) const {
  106. String n = p_name;
  107. if (n == "custom_package/debug")
  108. r_ret = custom_debug_package;
  109. else if (n == "custom_package/release")
  110. r_ret = custom_release_package;
  111. else if (n == "browser/enable_run")
  112. r_ret = show_run;
  113. else if (n == "options/memory_size")
  114. r_ret = max_memory;
  115. else if (n == "html/title")
  116. r_ret = html_title;
  117. else if (n == "html/head_include")
  118. r_ret = html_head_include;
  119. else if (n == "html/font_family")
  120. r_ret = html_font_family;
  121. else if (n == "html/style_include")
  122. r_ret = html_style_include;
  123. else if (n == "html/controls_enabled")
  124. r_ret = html_controls_enabled;
  125. else
  126. return false;
  127. return true;
  128. }
  129. void EditorExportPlatformJavaScript::_get_property_list(List<PropertyInfo> *p_list) const {
  130. p_list->push_back(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"));
  131. p_list->push_back(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "zip"));
  132. p_list->push_back(PropertyInfo(Variant::INT, "options/memory_size", PROPERTY_HINT_ENUM, "32mb,64mb,128mb,256mb,512mb,1024mb"));
  133. p_list->push_back(PropertyInfo(Variant::BOOL, "browser/enable_run"));
  134. p_list->push_back(PropertyInfo(Variant::STRING, "html/title"));
  135. p_list->push_back(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT));
  136. p_list->push_back(PropertyInfo(Variant::STRING, "html/font_family"));
  137. p_list->push_back(PropertyInfo(Variant::STRING, "html/style_include", PROPERTY_HINT_MULTILINE_TEXT));
  138. p_list->push_back(PropertyInfo(Variant::BOOL, "html/controls_enabled"));
  139. //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  140. }
  141. void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const String &p_name, bool p_debug) {
  142. String str;
  143. String strnew;
  144. str.parse_utf8((const char *)p_html.ptr(), p_html.size());
  145. Vector<String> lines = str.split("\n");
  146. for (int i = 0; i < lines.size(); i++) {
  147. String current_line = lines[i];
  148. current_line = current_line.replace("$GODOT_TMEM", itos((1 << (max_memory + 5)) * 1024 * 1024));
  149. current_line = current_line.replace("$GODOT_FS", p_name + "fs.js");
  150. current_line = current_line.replace("$GODOT_MEM", p_name + ".mem");
  151. current_line = current_line.replace("$GODOT_JS", p_name + ".js");
  152. current_line = current_line.replace("$GODOT_ASM", p_name + ".asm.js");
  153. current_line = current_line.replace("$GODOT_CANVAS_WIDTH", Globals::get_singleton()->get("display/width"));
  154. current_line = current_line.replace("$GODOT_CANVAS_HEIGHT", Globals::get_singleton()->get("display/height"));
  155. current_line = current_line.replace("$GODOT_HEAD_TITLE", !html_title.empty() ? html_title : (String)Globals::get_singleton()->get("application/name"));
  156. current_line = current_line.replace("$GODOT_HEAD_INCLUDE", html_head_include);
  157. current_line = current_line.replace("$GODOT_STYLE_FONT_FAMILY", html_font_family);
  158. current_line = current_line.replace("$GODOT_STYLE_INCLUDE", html_style_include);
  159. current_line = current_line.replace("$GODOT_CONTROLS_ENABLED", html_controls_enabled ? "true" : "false");
  160. current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
  161. strnew += current_line + "\n";
  162. }
  163. CharString cs = strnew.utf8();
  164. p_html.resize(cs.length());
  165. for (int i = 9; i < cs.length(); i++) {
  166. p_html[i] = cs[i];
  167. }
  168. }
  169. static void _fix_files(Vector<uint8_t> &html, uint64_t p_data_size) {
  170. String str;
  171. String strnew;
  172. str.parse_utf8((const char *)html.ptr(), html.size());
  173. Vector<String> lines = str.split("\n");
  174. for (int i = 0; i < lines.size(); i++) {
  175. if (lines[i].find("$DPLEN") != -1) {
  176. strnew += lines[i].replace("$DPLEN", itos(p_data_size));
  177. } else {
  178. strnew += lines[i] + "\n";
  179. }
  180. }
  181. CharString cs = strnew.utf8();
  182. html.resize(cs.length());
  183. for (int i = 9; i < cs.length(); i++) {
  184. html[i] = cs[i];
  185. }
  186. }
  187. struct JSExportData {
  188. EditorProgress *ep;
  189. FileAccess *f;
  190. };
  191. Error EditorExportPlatformJavaScript::export_project(const String &p_path, bool p_debug, int p_flags) {
  192. String src_template;
  193. EditorProgress ep("export", "Exporting for javascript", 104);
  194. if (p_debug)
  195. src_template = custom_debug_package;
  196. else
  197. src_template = custom_release_package;
  198. if (src_template == "") {
  199. String err;
  200. if (p_debug) {
  201. src_template = find_export_template("javascript_debug.zip", &err);
  202. } else {
  203. src_template = find_export_template("javascript_release.zip", &err);
  204. }
  205. if (src_template == "") {
  206. EditorNode::add_io_error(err);
  207. return ERR_FILE_NOT_FOUND;
  208. }
  209. }
  210. FileAccess *src_f = NULL;
  211. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  212. ep.step("Exporting to HTML5", 0);
  213. ep.step("Finding Files..", 1);
  214. FileAccess *f = FileAccess::open(p_path.get_base_dir() + "/data.pck", FileAccess::WRITE);
  215. if (!f) {
  216. EditorNode::add_io_error("Could not create file for writing:\n" + p_path.basename() + "_files.js");
  217. return ERR_FILE_CANT_WRITE;
  218. }
  219. Error err = save_pack(f);
  220. size_t len = f->get_len();
  221. memdelete(f);
  222. if (err)
  223. return err;
  224. unzFile pkg = unzOpen2(src_template.utf8().get_data(), &io);
  225. if (!pkg) {
  226. EditorNode::add_io_error("Could not find template HTML5 to export:\n" + src_template);
  227. return ERR_FILE_NOT_FOUND;
  228. }
  229. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  230. int ret = unzGoToFirstFile(pkg);
  231. while (ret == UNZ_OK) {
  232. //get filename
  233. unz_file_info info;
  234. char fname[16384];
  235. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  236. String file = fname;
  237. Vector<uint8_t> data;
  238. data.resize(info.uncompressed_size);
  239. //read
  240. unzOpenCurrentFile(pkg);
  241. unzReadCurrentFile(pkg, data.ptr(), data.size());
  242. unzCloseCurrentFile(pkg);
  243. //write
  244. if (file == "godot.html") {
  245. _fix_html(data, p_path.get_file().basename(), p_debug);
  246. file = p_path.get_file();
  247. }
  248. if (file == "godotfs.js") {
  249. _fix_files(data, len);
  250. file = p_path.get_file().basename() + "fs.js";
  251. }
  252. if (file == "godot.js") {
  253. //_fix_godot(data);
  254. file = p_path.get_file().basename() + ".js";
  255. }
  256. if (file == "godot.asm.js") {
  257. file = p_path.get_file().basename() + ".asm.js";
  258. }
  259. if (file == "godot.mem") {
  260. //_fix_godot(data);
  261. file = p_path.get_file().basename() + ".mem";
  262. }
  263. String dst = p_path.get_base_dir().plus_file(file);
  264. FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
  265. if (!f) {
  266. EditorNode::add_io_error("Could not create file for writing:\n" + dst);
  267. unzClose(pkg);
  268. return ERR_FILE_CANT_WRITE;
  269. }
  270. f->store_buffer(data.ptr(), data.size());
  271. memdelete(f);
  272. ret = unzGoToNextFile(pkg);
  273. }
  274. return OK;
  275. }
  276. Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
  277. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
  278. Error err = export_project(path, true, p_flags);
  279. if (err)
  280. return err;
  281. OS::get_singleton()->shell_open(path);
  282. return OK;
  283. }
  284. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  285. show_run = false;
  286. Image img(_javascript_logo);
  287. logo = Ref<ImageTexture>(memnew(ImageTexture));
  288. logo->create_from_image(img);
  289. max_memory = 3;
  290. html_title = "";
  291. html_font_family = "arial,sans-serif";
  292. html_controls_enabled = true;
  293. pack_mode = PACK_SINGLE_FILE;
  294. }
  295. bool EditorExportPlatformJavaScript::can_export(String *r_error) const {
  296. bool valid = true;
  297. String err;
  298. if (!exists_export_template("javascript_debug.zip") || !exists_export_template("javascript_release.zip")) {
  299. valid = false;
  300. err += "No export templates found.\nDownload and install export templates.\n";
  301. }
  302. if (custom_debug_package != "" && !FileAccess::exists(custom_debug_package)) {
  303. valid = false;
  304. err += "Custom debug package not found.\n";
  305. }
  306. if (custom_release_package != "" && !FileAccess::exists(custom_release_package)) {
  307. valid = false;
  308. err += "Custom release package not found.\n";
  309. }
  310. if (r_error)
  311. *r_error = err;
  312. return valid;
  313. }
  314. EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
  315. }
  316. void register_javascript_exporter() {
  317. Ref<EditorExportPlatformJavaScript> exporter = Ref<EditorExportPlatformJavaScript>(memnew(EditorExportPlatformJavaScript));
  318. EditorImportExport::get_singleton()->add_export_platform(exporter);
  319. }