export.cpp 13 KB

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