export.cpp 13 KB

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