export.cpp 13 KB

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