export.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. PackMode pack_mode;
  50. bool show_run;
  51. int max_memory;
  52. int version_code;
  53. Ref<ImageTexture> logo;
  54. protected:
  55. bool _set(const StringName& p_name, const Variant& p_value);
  56. bool _get(const StringName& p_name,Variant &r_ret) const;
  57. void _get_property_list( List<PropertyInfo> *p_list) const;
  58. public:
  59. virtual String get_name() const { return "HTML5"; }
  60. virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_BC; }
  61. virtual Ref<Texture> get_logo() const { return logo; }
  62. virtual bool poll_devices() { return show_run?true:false;}
  63. virtual int get_device_count() const { return show_run?1:0; };
  64. virtual String get_device_name(int p_device) const { return "Run in Browser"; }
  65. virtual String get_device_info(int p_device) const { return "Run exported HTML in the system's default browser."; }
  66. virtual Error run(int p_device,int p_flags=0);
  67. virtual bool requieres_password(bool p_debug) const { return false; }
  68. virtual String get_binary_extension() const { return "html"; }
  69. virtual Error export_project(const String& p_path,bool p_debug,int p_flags=0);
  70. virtual bool can_export(String *r_error=NULL) const;
  71. EditorExportPlatformJavaScript();
  72. ~EditorExportPlatformJavaScript();
  73. };
  74. bool EditorExportPlatformJavaScript::_set(const StringName& p_name, const Variant& p_value) {
  75. String n=p_name;
  76. if (n=="custom_package/debug")
  77. custom_debug_package=p_value;
  78. else if (n=="custom_package/release")
  79. custom_release_package=p_value;
  80. else if (n=="browser/enable_run")
  81. show_run=p_value;
  82. else if (n=="options/memory_size")
  83. max_memory=p_value;
  84. else
  85. return false;
  86. return true;
  87. }
  88. bool EditorExportPlatformJavaScript::_get(const StringName& p_name,Variant &r_ret) const{
  89. String n=p_name;
  90. if (n=="custom_package/debug")
  91. r_ret=custom_debug_package;
  92. else if (n=="custom_package/release")
  93. r_ret=custom_release_package;
  94. else if (n=="browser/enable_run")
  95. r_ret=show_run;
  96. else if (n=="options/memory_size")
  97. r_ret=max_memory;
  98. else
  99. return false;
  100. return true;
  101. }
  102. void EditorExportPlatformJavaScript::_get_property_list( List<PropertyInfo> *p_list) const{
  103. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE,"zip"));
  104. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"zip"));
  105. p_list->push_back( PropertyInfo( Variant::INT, "options/memory_size",PROPERTY_HINT_ENUM,"32mb,64mb,128mb,256mb,512mb,1024mb"));
  106. p_list->push_back( PropertyInfo( Variant::BOOL, "browser/enable_run"));
  107. //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  108. }
  109. static void _fix_html(Vector<uint8_t>& html,const String& name,int max_memory) {
  110. String str;
  111. String strnew;
  112. str.parse_utf8((const char*)html.ptr(),html.size());
  113. Vector<String> lines=str.split("\n");
  114. for(int i=0;i<lines.size();i++) {
  115. if (lines[i].find("$GODOTTMEM")!=-1) {
  116. strnew+=lines[i].replace("$GODOTTMEM",itos(max_memory*1024*1024))+"\n";
  117. } else if (lines[i].find("$GODOTFS")!=-1) {
  118. strnew+=lines[i].replace("$GODOTFS",name+"fs.js")+"\n";
  119. } else if (lines[i].find("$GODOTMEM")!=-1) {
  120. strnew+=lines[i].replace("$GODOTMEM",name+".mem")+"\n";
  121. } else if (lines[i].find("$GODOTJS")!=-1) {
  122. strnew+=lines[i].replace("$GODOTJS",name+".js")+"\n";
  123. } else {
  124. strnew+=lines[i]+"\n";
  125. }
  126. }
  127. CharString cs = strnew.utf8();
  128. html.resize(cs.size());
  129. for(int i=9;i<cs.size();i++) {
  130. html[i]=cs[i];
  131. }
  132. }
  133. static void _fix_files(Vector<uint8_t>& html,uint64_t p_data_size) {
  134. String str;
  135. String strnew;
  136. str.parse_utf8((const char*)html.ptr(),html.size());
  137. Vector<String> lines=str.split("\n");
  138. for(int i=0;i<lines.size();i++) {
  139. if (lines[i].find("$DPLEN")!=-1) {
  140. strnew+=lines[i].replace("$DPLEN",itos(p_data_size));
  141. } else {
  142. strnew+=lines[i]+"\n";
  143. }
  144. }
  145. CharString cs = strnew.utf8();
  146. html.resize(cs.length());
  147. for(int i=9;i<cs.length();i++) {
  148. html[i]=cs[i];
  149. }
  150. }
  151. struct JSExportData {
  152. EditorProgress *ep;
  153. FileAccess *f;
  154. };
  155. Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool p_debug, int p_flags) {
  156. String src_template;
  157. EditorProgress ep("export","Exporting for javascript",104);
  158. if (p_debug)
  159. src_template=custom_debug_package;
  160. else
  161. src_template=custom_release_package;
  162. if (src_template=="") {
  163. String err;
  164. if (p_debug) {
  165. src_template=find_export_template("javascript_debug.zip", &err);
  166. } else {
  167. src_template=find_export_template("javascript_release.zip", &err);
  168. }
  169. if (src_template=="") {
  170. EditorNode::add_io_error(err);
  171. return ERR_FILE_NOT_FOUND;
  172. }
  173. }
  174. FileAccess *src_f=NULL;
  175. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  176. ep.step("Exporting to HTML5",0);
  177. ep.step("Finding Files..",1);
  178. FileAccess *f=FileAccess::open(p_path.get_base_dir()+"/data.pck",FileAccess::WRITE);
  179. if (!f) {
  180. EditorNode::add_io_error("Could not create file for writing:\n"+p_path.basename()+"_files.js");
  181. return ERR_FILE_CANT_WRITE;
  182. }
  183. Error err = save_pack(f);
  184. size_t len = f->get_len();
  185. memdelete(f);
  186. if (err)
  187. return err;
  188. unzFile pkg = unzOpen2(src_template.utf8().get_data(), &io);
  189. if (!pkg) {
  190. EditorNode::add_io_error("Could not find template HTML5 to export:\n"+src_template);
  191. return ERR_FILE_NOT_FOUND;
  192. }
  193. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  194. int ret = unzGoToFirstFile(pkg);
  195. while(ret==UNZ_OK) {
  196. //get filename
  197. unz_file_info info;
  198. char fname[16384];
  199. ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
  200. String file=fname;
  201. Vector<uint8_t> data;
  202. data.resize(info.uncompressed_size);
  203. //read
  204. unzOpenCurrentFile(pkg);
  205. unzReadCurrentFile(pkg,data.ptr(),data.size());
  206. unzCloseCurrentFile(pkg);
  207. //write
  208. if (file=="godot.html") {
  209. _fix_html(data,p_path.get_file().basename(),1<<(max_memory+5));
  210. file=p_path.get_file();
  211. }
  212. if (file=="godotfs.js") {
  213. _fix_files(data,len);
  214. file=p_path.get_file().basename()+"fs.js";
  215. }
  216. if (file=="godot.js") {
  217. //_fix_godot(data);
  218. file=p_path.get_file().basename()+".js";
  219. }
  220. if (file=="godot.mem") {
  221. //_fix_godot(data);
  222. file=p_path.get_file().basename()+".mem";
  223. }
  224. String dst = p_path.get_base_dir().plus_file(file);
  225. FileAccess *f=FileAccess::open(dst,FileAccess::WRITE);
  226. if (!f) {
  227. EditorNode::add_io_error("Could not create file for writing:\n"+dst);
  228. unzClose(pkg);
  229. return ERR_FILE_CANT_WRITE;
  230. }
  231. f->store_buffer(data.ptr(),data.size());
  232. memdelete(f);
  233. ret = unzGoToNextFile(pkg);
  234. }
  235. return OK;
  236. }
  237. Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
  238. String path = EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmp_export.html";
  239. Error err = export_project(path,true,p_flags);
  240. if (err)
  241. return err;
  242. OS::get_singleton()->shell_open(path);
  243. return OK;
  244. }
  245. EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
  246. show_run=false;
  247. Image img( _javascript_logo );
  248. logo = Ref<ImageTexture>( memnew( ImageTexture ));
  249. logo->create_from_image(img);
  250. max_memory=3;
  251. pack_mode=PACK_SINGLE_FILE;
  252. }
  253. bool EditorExportPlatformJavaScript::can_export(String *r_error) const {
  254. bool valid=true;
  255. String err;
  256. if (!exists_export_template("javascript_debug.zip") || !exists_export_template("javascript_release.zip")) {
  257. valid=false;
  258. err+="No export templates found.\nDownload and install export templates.\n";
  259. }
  260. if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
  261. valid=false;
  262. err+="Custom debug package not found.\n";
  263. }
  264. if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
  265. valid=false;
  266. err+="Custom release package not found.\n";
  267. }
  268. if (r_error)
  269. *r_error=err;
  270. return valid;
  271. }
  272. EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
  273. }
  274. void register_javascript_exporter() {
  275. Ref<EditorExportPlatformJavaScript> exporter = Ref<EditorExportPlatformJavaScript>( memnew(EditorExportPlatformJavaScript) );
  276. EditorImportExport::get_singleton()->add_export_platform(exporter);
  277. }