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-2016 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. OBJ_TYPE( 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_FS",p_name+"fs.js");
  149. current_line = current_line.replace("$GODOT_MEM",p_name+".mem");
  150. current_line = current_line.replace("$GODOT_JS",p_name+".js");
  151. current_line = current_line.replace("$GODOT_ASM",p_name+".asm.js");
  152. current_line = current_line.replace("$GODOT_CANVAS_WIDTH",Globals::get_singleton()->get("display/width"));
  153. current_line = current_line.replace("$GODOT_CANVAS_HEIGHT",Globals::get_singleton()->get("display/height"));
  154. current_line = current_line.replace("$GODOT_HEAD_TITLE",!html_title.empty()?html_title:(String) Globals::get_singleton()->get("application/name"));
  155. current_line = current_line.replace("$GODOT_HEAD_INCLUDE",html_head_include);
  156. current_line = current_line.replace("$GODOT_STYLE_FONT_FAMILY",html_font_family);
  157. current_line = current_line.replace("$GODOT_STYLE_INCLUDE",html_style_include);
  158. current_line = current_line.replace("$GODOT_CONTROLS_ENABLED",html_controls_enabled?"true":"false");
  159. current_line = current_line.replace("$GODOT_DEBUG_ENABLED",p_debug?"true":"false");
  160. strnew += current_line+"\n";
  161. }
  162. CharString cs = strnew.utf8();
  163. p_html.resize(cs.length());
  164. for(int i=9;i<cs.length();i++) {
  165. p_html[i]=cs[i];
  166. }
  167. }
  168. static void _fix_files(Vector<uint8_t>& html,uint64_t p_data_size) {
  169. String str;
  170. String strnew;
  171. str.parse_utf8((const char*)html.ptr(),html.size());
  172. Vector<String> lines=str.split("\n");
  173. for(int i=0;i<lines.size();i++) {
  174. if (lines[i].find("$DPLEN")!=-1) {
  175. strnew+=lines[i].replace("$DPLEN",itos(p_data_size));
  176. } else {
  177. strnew+=lines[i]+"\n";
  178. }
  179. }
  180. CharString cs = strnew.utf8();
  181. html.resize(cs.length());
  182. for(int i=9;i<cs.length();i++) {
  183. html[i]=cs[i];
  184. }
  185. }
  186. struct JSExportData {
  187. EditorProgress *ep;
  188. FileAccess *f;
  189. };
  190. Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool p_debug, int p_flags) {
  191. String src_template;
  192. EditorProgress ep("export","Exporting for javascript",104);
  193. if (p_debug)
  194. src_template=custom_debug_package;
  195. else
  196. src_template=custom_release_package;
  197. if (src_template=="") {
  198. String err;
  199. if (p_debug) {
  200. src_template=find_export_template("javascript_debug.zip", &err);
  201. } else {
  202. src_template=find_export_template("javascript_release.zip", &err);
  203. }
  204. if (src_template=="") {
  205. EditorNode::add_io_error(err);
  206. return ERR_FILE_NOT_FOUND;
  207. }
  208. }
  209. FileAccess *src_f=NULL;
  210. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  211. ep.step("Exporting to HTML5",0);
  212. ep.step("Finding Files..",1);
  213. FileAccess *f=FileAccess::open(p_path.get_base_dir()+"/data.pck",FileAccess::WRITE);
  214. if (!f) {
  215. EditorNode::add_io_error("Could not create file for writing:\n"+p_path.basename()+"_files.js");
  216. return ERR_FILE_CANT_WRITE;
  217. }
  218. Error err = save_pack(f);
  219. size_t len = f->get_len();
  220. memdelete(f);
  221. if (err)
  222. return err;
  223. unzFile pkg = unzOpen2(src_template.utf8().get_data(), &io);
  224. if (!pkg) {
  225. EditorNode::add_io_error("Could not find template HTML5 to export:\n"+src_template);
  226. return ERR_FILE_NOT_FOUND;
  227. }
  228. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  229. int ret = unzGoToFirstFile(pkg);
  230. while(ret==UNZ_OK) {
  231. //get filename
  232. unz_file_info info;
  233. char fname[16384];
  234. ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
  235. String file=fname;
  236. Vector<uint8_t> data;
  237. data.resize(info.uncompressed_size);
  238. //read
  239. unzOpenCurrentFile(pkg);
  240. unzReadCurrentFile(pkg,data.ptr(),data.size());
  241. unzCloseCurrentFile(pkg);
  242. //write
  243. if (file=="godot.html") {
  244. _fix_html(data,p_path.get_file().basename(), p_debug);
  245. file=p_path.get_file();
  246. }
  247. if (file=="godotfs.js") {
  248. _fix_files(data,len);
  249. file=p_path.get_file().basename()+"fs.js";
  250. }
  251. if (file=="godot.js") {
  252. //_fix_godot(data);
  253. file=p_path.get_file().basename()+".js";
  254. }
  255. if (file=="godot.asm.js") {
  256. file=p_path.get_file().basename()+".asm.js";
  257. }
  258. if (file=="godot.mem") {
  259. //_fix_godot(data);
  260. file=p_path.get_file().basename()+".mem";
  261. }
  262. String dst = p_path.get_base_dir().plus_file(file);
  263. FileAccess *f=FileAccess::open(dst,FileAccess::WRITE);
  264. if (!f) {
  265. EditorNode::add_io_error("Could not create file for writing:\n"+dst);
  266. unzClose(pkg);
  267. return ERR_FILE_CANT_WRITE;
  268. }
  269. f->store_buffer(data.ptr(),data.size());
  270. memdelete(f);
  271. ret = unzGoToNextFile(pkg);
  272. }
  273. return OK;
  274. }
  275. Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
  276. String path = EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmp_export.html";
  277. Error err = export_project(path,true,p_flags);
  278. if (err)
  279. return err;
  280. OS::get_singleton()->shell_open(path);
  281. return OK;
  282. }
  283. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  284. show_run=false;
  285. Image img( _javascript_logo );
  286. logo = Ref<ImageTexture>( memnew( ImageTexture ));
  287. logo->create_from_image(img);
  288. max_memory=3;
  289. html_title="";
  290. html_font_family="arial,sans-serif";
  291. html_controls_enabled=true;
  292. pack_mode=PACK_SINGLE_FILE;
  293. }
  294. bool EditorExportPlatformJavaScript::can_export(String *r_error) const {
  295. bool valid=true;
  296. String err;
  297. if (!exists_export_template("javascript_debug.zip") || !exists_export_template("javascript_release.zip")) {
  298. valid=false;
  299. err+="No export templates found.\nDownload and install export templates.\n";
  300. }
  301. if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
  302. valid=false;
  303. err+="Custom debug package not found.\n";
  304. }
  305. if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
  306. valid=false;
  307. err+="Custom release package not found.\n";
  308. }
  309. if (r_error)
  310. *r_error=err;
  311. return valid;
  312. }
  313. EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
  314. }
  315. void register_javascript_exporter() {
  316. Ref<EditorExportPlatformJavaScript> exporter = Ref<EditorExportPlatformJavaScript>( memnew(EditorExportPlatformJavaScript) );
  317. EditorImportExport::get_singleton()->add_export_platform(exporter);
  318. }