export.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. #include "version.h"
  2. #include "export.h"
  3. #include "tools/editor/editor_settings.h"
  4. #include "tools/editor/editor_import_export.h"
  5. #include "tools/editor/editor_node.h"
  6. #include "io/zip_io.h"
  7. #include "io/marshalls.h"
  8. #include "globals.h"
  9. #include "os/file_access.h"
  10. #include "os/os.h"
  11. #include "platform/android/logo.h"
  12. class EditorExportPlatformAndroid : public EditorExportPlatform {
  13. OBJ_TYPE( EditorExportPlatformAndroid,EditorExportPlatform );
  14. String custom_release_package;
  15. String custom_debug_package;
  16. int version_code;
  17. String version_name;
  18. String package;
  19. String name;
  20. String icon;
  21. bool _signed;
  22. int orientation;
  23. String release_keystore;
  24. String release_username;
  25. struct APKExportData {
  26. zipFile apk;
  27. EditorProgress *ep;
  28. };
  29. struct Device {
  30. String id;
  31. String name;
  32. String description;
  33. };
  34. Vector<Device> devices;
  35. bool devices_changed;
  36. Mutex *device_lock;
  37. Thread *device_thread;
  38. Ref<ImageTexture> logo;
  39. volatile bool quit_request;
  40. static void _device_poll_thread(void *ud);
  41. String get_project_name() const;
  42. void _fix_manifest(Vector<uint8_t>& p_manifest);
  43. void _fix_resources(Vector<uint8_t>& p_manifest);
  44. static Error save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total);
  45. protected:
  46. bool _set(const StringName& p_name, const Variant& p_value);
  47. bool _get(const StringName& p_name,Variant &r_ret) const;
  48. void _get_property_list( List<PropertyInfo> *p_list) const;
  49. public:
  50. virtual String get_name() const { return "Android"; }
  51. virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_ETC1; }
  52. virtual Ref<Texture> get_logo() const { return logo; }
  53. virtual bool poll_devices();
  54. virtual int get_device_count() const;
  55. virtual String get_device_name(int p_device) const;
  56. virtual String get_device_info(int p_device) const;
  57. virtual Error run(int p_device);
  58. virtual bool requieres_password(bool p_debug) const { return !p_debug; }
  59. virtual String get_binary_extension() const { return "apk"; }
  60. virtual Error export_project(const String& p_path,bool p_debug,const String& p_password="");
  61. virtual bool can_export(String *r_error=NULL) const;
  62. EditorExportPlatformAndroid();
  63. ~EditorExportPlatformAndroid();
  64. };
  65. bool EditorExportPlatformAndroid::_set(const StringName& p_name, const Variant& p_value) {
  66. String n=p_name;
  67. if (n=="version/code")
  68. version_code=p_value;
  69. else if (n=="version/name")
  70. version_name=p_value;
  71. else if (n=="package/unique_name")
  72. package=p_value;
  73. else if (n=="package/name")
  74. name=p_value;
  75. else if (n=="package/icon")
  76. icon=p_value;
  77. else if (n=="package/signed")
  78. _signed=p_value;
  79. else if (n=="screen/orientation")
  80. orientation=p_value;
  81. else if (n=="keystore/release")
  82. release_keystore=p_value;
  83. else if (n=="keystore/release_user")
  84. release_username=p_value;
  85. else
  86. return false;
  87. return true;
  88. }
  89. bool EditorExportPlatformAndroid::_get(const StringName& p_name,Variant &r_ret) const{
  90. String n=p_name;
  91. if (n=="version/code")
  92. r_ret=version_code;
  93. else if (n=="version/name")
  94. r_ret=version_name;
  95. else if (n=="package/unique_name")
  96. r_ret=package;
  97. else if (n=="package/name")
  98. r_ret=name;
  99. else if (n=="package/icon")
  100. r_ret=icon;
  101. else if (n=="package/signed")
  102. r_ret=_signed;
  103. else if (n=="screen/orientation")
  104. r_ret=orientation;
  105. else if (n=="keystore/release")
  106. r_ret=release_keystore;
  107. else if (n=="keystore/release_user")
  108. r_ret=release_username;
  109. else
  110. return false;
  111. return true;
  112. }
  113. void EditorExportPlatformAndroid::_get_property_list( List<PropertyInfo> *p_list) const{
  114. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_FILE,"apk"));
  115. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_FILE,"apk"));
  116. p_list->push_back( PropertyInfo( Variant::INT, "version/code", PROPERTY_HINT_RANGE,"1,65535,1"));
  117. p_list->push_back( PropertyInfo( Variant::STRING, "version/name") );
  118. p_list->push_back( PropertyInfo( Variant::STRING, "package/unique_name") );
  119. p_list->push_back( PropertyInfo( Variant::STRING, "package/name") );
  120. p_list->push_back( PropertyInfo( Variant::STRING, "package/icon",PROPERTY_HINT_FILE,"png") );
  121. p_list->push_back( PropertyInfo( Variant::BOOL, "package/signed") );
  122. p_list->push_back( PropertyInfo( Variant::INT, "screen/orientation",PROPERTY_HINT_ENUM,"Landscape,Portrait") );
  123. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release",PROPERTY_HINT_FILE,"keystore") );
  124. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release_user" ) );
  125. //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  126. }
  127. static String _parse_string(const uint8_t *p_bytes,bool p_utf8) {
  128. uint32_t offset=0;
  129. uint32_t len = decode_uint16(&p_bytes[offset]);
  130. if (p_utf8) {
  131. //don't know how to read extended utf8, this will have to be for now
  132. len>>=8;
  133. }
  134. offset+=2;
  135. printf("len %i, unicode: %i\n",len,int(p_utf8));
  136. if (p_utf8) {
  137. Vector<uint8_t> str8;
  138. str8.resize(len+1);
  139. for(int i=0;i<len;i++) {
  140. str8[i]=p_bytes[offset+i];
  141. }
  142. str8[len]=0;
  143. String str;
  144. str.parse_utf8((const char*)str8.ptr());
  145. return str;
  146. } else {
  147. String str;
  148. for(int i=0;i<len;i++) {
  149. CharType c = decode_uint16(&p_bytes[offset+i*2]);
  150. if (c==0)
  151. break;
  152. str += String::chr(c);
  153. }
  154. return str;
  155. }
  156. }
  157. void EditorExportPlatformAndroid::_fix_resources(Vector<uint8_t>& p_manifest) {
  158. const int UTF8_FLAG = 0x00000100;
  159. print_line("*******************GORRRGLE***********************");
  160. uint32_t header = decode_uint32(&p_manifest[0]);
  161. uint32_t filesize = decode_uint32(&p_manifest[4]);
  162. uint32_t string_block_len = decode_uint32(&p_manifest[16]);
  163. uint32_t string_count = decode_uint32(&p_manifest[20]);
  164. uint32_t string_flags = decode_uint32(&p_manifest[28]);
  165. const uint32_t string_table_begins = 40;
  166. Vector<String> string_table;
  167. printf("stirng block len: %i\n",string_block_len);
  168. printf("stirng count: %i\n",string_count);
  169. printf("flags: %x\n",string_flags);
  170. for(int i=0;i<string_count;i++) {
  171. uint32_t offset = decode_uint32(&p_manifest[string_table_begins+i*4]);
  172. offset+=string_table_begins+string_count*4;
  173. String str = _parse_string(&p_manifest[offset],string_flags&UTF8_FLAG);
  174. if (str.begins_with("godot-project-name")) {
  175. if (str=="godot-project-name") {
  176. //project name
  177. str = get_project_name();
  178. } else {
  179. String lang = str.substr(str.find_last("-")+1,str.length()).replace("-","_");
  180. String prop = "application/name_"+lang;
  181. if (Globals::get_singleton()->has(prop)) {
  182. str = Globals::get_singleton()->get(prop);
  183. } else {
  184. str = get_project_name();
  185. }
  186. }
  187. }
  188. string_table.push_back(str);
  189. }
  190. //write a new string table, but use 16 bits
  191. Vector<uint8_t> ret;
  192. ret.resize(string_table_begins+string_table.size()*4);
  193. for(int i=0;i<string_table_begins;i++) {
  194. ret[i]=p_manifest[i];
  195. }
  196. int ofs=0;
  197. for(int i=0;i<string_table.size();i++) {
  198. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  199. ofs+=string_table[i].length()*2+2+2;
  200. }
  201. ret.resize(ret.size()+ofs);
  202. uint8_t *chars=&ret[ret.size()-ofs];
  203. for(int i=0;i<string_table.size();i++) {
  204. String s = string_table[i];
  205. encode_uint16(s.length(),chars);
  206. chars+=2;
  207. for(int j=0;j<s.length();j++) {
  208. encode_uint16(s[j],chars);
  209. chars+=2;
  210. }
  211. encode_uint16(0,chars);
  212. chars+=2;
  213. }
  214. //pad
  215. while(ret.size()%4)
  216. ret.push_back(0);
  217. //change flags to not use utf8
  218. encode_uint32(string_flags&~0x100,&ret[28]);
  219. //change length
  220. encode_uint32(ret.size()-12,&ret[16]);
  221. //append the rest...
  222. int rest_from = 12+string_block_len;
  223. int rest_to = ret.size();
  224. int rest_len = (p_manifest.size() - rest_from);
  225. ret.resize(ret.size() + (p_manifest.size() - rest_from) );
  226. for(int i=0;i<rest_len;i++) {
  227. ret[rest_to+i]=p_manifest[rest_from+i];
  228. }
  229. //finally update the size
  230. encode_uint32(ret.size(),&ret[4]);
  231. p_manifest=ret;
  232. printf("end\n");
  233. }
  234. String EditorExportPlatformAndroid::get_project_name() const {
  235. String aname;
  236. if (this->name!="") {
  237. aname=this->name;
  238. } else {
  239. aname = Globals::get_singleton()->get("application/name");
  240. }
  241. if (aname=="") {
  242. aname=_MKSTR(VERSION_NAME);
  243. }
  244. return aname;
  245. }
  246. void EditorExportPlatformAndroid::_fix_manifest(Vector<uint8_t>& p_manifest) {
  247. const int CHUNK_AXML_FILE = 0x00080003;
  248. const int CHUNK_RESOURCEIDS = 0x00080180;
  249. const int CHUNK_STRINGS = 0x001C0001;
  250. const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  251. const int CHUNK_XML_END_TAG = 0x00100103;
  252. const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  253. const int CHUNK_XML_START_TAG = 0x00100102;
  254. const int CHUNK_XML_TEXT = 0x00100104;
  255. const int UTF8_FLAG = 0x00000100;
  256. Vector<String> string_table;
  257. uint32_t ofs=0;
  258. uint32_t header = decode_uint32(&p_manifest[ofs]);
  259. uint32_t filesize = decode_uint32(&p_manifest[ofs+4]);
  260. ofs+=8;
  261. // print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));
  262. uint32_t string_count;
  263. uint32_t styles_count;
  264. uint32_t string_flags;
  265. uint32_t string_data_offset;
  266. uint32_t styles_offset;
  267. uint32_t string_table_begins;
  268. uint32_t string_table_ends;
  269. Vector<uint8_t> stable_extra;
  270. while(ofs < p_manifest.size()) {
  271. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  272. uint32_t size = decode_uint32(&p_manifest[ofs+4]);
  273. switch(chunk) {
  274. case CHUNK_STRINGS: {
  275. int iofs=ofs+8;
  276. uint32_t string_count=decode_uint32(&p_manifest[iofs]);
  277. uint32_t styles_count=decode_uint32(&p_manifest[iofs+4]);
  278. uint32_t string_flags=decode_uint32(&p_manifest[iofs+8]);
  279. uint32_t string_data_offset=decode_uint32(&p_manifest[iofs+12]);
  280. uint32_t styles_offset=decode_uint32(&p_manifest[iofs+16]);
  281. /*
  282. printf("string count: %i\n",string_count);
  283. printf("flags: %i\n",string_flags);
  284. printf("sdata ofs: %i\n",string_data_offset);
  285. printf("styles ofs: %i\n",styles_offset);
  286. */
  287. uint32_t st_offset=iofs+20;
  288. string_table.resize(string_count);
  289. uint32_t string_end=0;
  290. string_table_begins=st_offset;
  291. for(int i=0;i<string_count;i++) {
  292. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  293. string_at+=st_offset+string_count*4;
  294. ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
  295. ERR_FAIL_COND(string_flags&UTF8_FLAG);
  296. if (string_flags&UTF8_FLAG) {
  297. } else {
  298. uint32_t len = decode_uint16(&p_manifest[string_at]);
  299. Vector<CharType> ucstring;
  300. ucstring.resize(len+1);
  301. for(int j=0;j<len;j++) {
  302. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  303. ucstring[j]=c;
  304. }
  305. string_end=MAX(string_at+2+2*len,string_end);
  306. ucstring[len]=0;
  307. string_table[i]=ucstring.ptr();
  308. }
  309. // print_line("String "+itos(i)+": "+string_table[i]);
  310. }
  311. for(int i=string_end;i<(ofs+size);i++) {
  312. stable_extra.push_back(p_manifest[i]);
  313. }
  314. // printf("stable extra: %i\n",int(stable_extra.size()));
  315. string_table_ends=ofs+size;
  316. // print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));
  317. } break;
  318. case CHUNK_XML_START_TAG: {
  319. int iofs=ofs+8;
  320. uint32_t line=decode_uint32(&p_manifest[iofs]);
  321. uint32_t nspace=decode_uint32(&p_manifest[iofs+8]);
  322. uint32_t name=decode_uint32(&p_manifest[iofs+12]);
  323. uint32_t check=decode_uint32(&p_manifest[iofs+16]);
  324. String tname=string_table[name];
  325. // printf("NSPACE: %i\n",nspace);
  326. //printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
  327. //printf("CHECK: %x\n",check);
  328. uint32_t attrcount=decode_uint32(&p_manifest[iofs+20]);
  329. iofs+=28;
  330. //printf("ATTRCOUNT: %x\n",attrcount);
  331. for(int i=0;i<attrcount;i++) {
  332. uint32_t attr_nspace=decode_uint32(&p_manifest[iofs]);
  333. uint32_t attr_name=decode_uint32(&p_manifest[iofs+4]);
  334. uint32_t attr_value=decode_uint32(&p_manifest[iofs+8]);
  335. uint32_t attr_flags=decode_uint32(&p_manifest[iofs+12]);
  336. uint32_t attr_resid=decode_uint32(&p_manifest[iofs+16]);
  337. String value;
  338. if (attr_value!=0xFFFFFFFF)
  339. value=string_table[attr_value];
  340. else
  341. value="Res #"+itos(attr_resid);
  342. String attrname = string_table[attr_name];
  343. String nspace;
  344. if (attr_nspace!=0xFFFFFFFF)
  345. nspace=string_table[attr_nspace];
  346. else
  347. nspace="";
  348. printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
  349. printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
  350. printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
  351. printf("ATTR %i FLAGS: %x\n",i,attr_flags);
  352. printf("ATTR %i RESID: %x\n",i,attr_resid);
  353. //replace project information
  354. if (tname=="manifest" && attrname=="package") {
  355. print_line("FOUND PACKAGE");
  356. string_table[attr_value]=package;
  357. }
  358. //print_line("tname: "+tname);
  359. //print_line("nspace: "+nspace);
  360. //print_line("attrname: "+attrname);
  361. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionCode") {
  362. print_line("FOUND versioncode");
  363. encode_uint32(version_code,&p_manifest[iofs+16]);
  364. }
  365. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionName") {
  366. print_line("FOUND versionname");
  367. if (attr_value==0xFFFFFFFF) {
  368. WARN_PRINT("Version name in a resource, should be plaintext")
  369. } else
  370. string_table[attr_value]=version_name;
  371. }
  372. if (tname=="activity" && /*nspace=="android" &&*/ attrname=="screenOrientation") {
  373. print_line("FOUND screen orientation");
  374. if (attr_value==0xFFFFFFFF) {
  375. WARN_PRINT("Version name in a resource, should be plaintext")
  376. } else {
  377. string_table[attr_value]=(orientation==0?"landscape":"portrait");
  378. }
  379. }
  380. if (tname=="application" && /*nspace=="android" &&*/ attrname=="label") {
  381. print_line("FOUND application");
  382. if (attr_value==0xFFFFFFFF) {
  383. WARN_PRINT("Application name in a resource, should be plaintext.")
  384. } else {
  385. String aname = get_project_name();
  386. string_table[attr_value]=aname;
  387. }
  388. }
  389. if (tname=="activity" && /*nspace=="android" &&*/ attrname=="label") {
  390. print_line("FOUND activity name");
  391. if (attr_value==0xFFFFFFFF) {
  392. WARN_PRINT("Activity name in a resource, should be plaintext")
  393. } else {
  394. String aname;
  395. if (this->name!="") {
  396. aname=this->name;
  397. } else {
  398. aname = Globals::get_singleton()->get("application/name");
  399. }
  400. if (aname=="") {
  401. aname=_MKSTR(VERSION_NAME);
  402. }
  403. print_line("APP NAME IS..."+aname);
  404. string_table[attr_value]=aname;
  405. }
  406. }
  407. iofs+=20;
  408. }
  409. } break;
  410. }
  411. printf("chunk %x: size: %d\n",chunk,size);
  412. ofs+=size;
  413. }
  414. printf("end\n");
  415. //create new andriodmanifest binary
  416. Vector<uint8_t> ret;
  417. ret.resize(string_table_begins+string_table.size()*4);
  418. for(int i=0;i<string_table_begins;i++) {
  419. ret[i]=p_manifest[i];
  420. }
  421. ofs=0;
  422. for(int i=0;i<string_table.size();i++) {
  423. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  424. ofs+=string_table[i].length()*2+2+2;
  425. print_line("ofs: "+itos(i)+": "+itos(ofs));
  426. }
  427. ret.resize(ret.size()+ofs);
  428. uint8_t *chars=&ret[ret.size()-ofs];
  429. for(int i=0;i<string_table.size();i++) {
  430. String s = string_table[i];
  431. print_line("savint string :"+s);
  432. encode_uint16(s.length(),chars);
  433. chars+=2;
  434. for(int j=0;j<s.length();j++) { //include zero?
  435. encode_uint16(s[j],chars);
  436. chars+=2;
  437. }
  438. encode_uint16(0,chars);
  439. chars+=2;
  440. }
  441. ret.resize(ret.size()+stable_extra.size());
  442. while(ret.size()%4)
  443. ret.push_back(0);
  444. for(int i=0;i<stable_extra.size();i++) {
  445. chars[i]=stable_extra[i];
  446. }
  447. uint32_t new_stable_end=ret.size();
  448. uint32_t extra = (p_manifest.size()-string_table_ends);
  449. ret.resize(new_stable_end + extra);
  450. for(int i=0;i<extra;i++)
  451. ret[new_stable_end+i]=p_manifest[string_table_ends+i];
  452. while(ret.size()%4)
  453. ret.push_back(0);
  454. encode_uint32(ret.size(),&ret[4]); //update new file size
  455. encode_uint32(new_stable_end-8,&ret[12]); //update new string table size
  456. print_line("file size: "+itos(ret.size()));
  457. p_manifest=ret;
  458. #if 0
  459. uint32_t header[9];
  460. for(int i=0;i<9;i++) {
  461. header[i]=decode_uint32(&p_manifest[i*4]);
  462. }
  463. print_line("STO: "+itos(header[3]));
  464. uint32_t st_offset=9*4;
  465. //ERR_FAIL_COND(header[3]!=0x24)
  466. uint32_t string_count=header[4];
  467. string_table.resize(string_count);
  468. for(int i=0;i<string_count;i++) {
  469. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  470. string_at+=st_offset+string_count*4;
  471. uint32_t len = decode_uint16(&p_manifest[string_at]);
  472. Vector<CharType> ucstring;
  473. ucstring.resize(len+1);
  474. for(int j=0;j<len;j++) {
  475. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  476. ucstring[j]=c;
  477. }
  478. ucstring[len]=0;
  479. string_table[i]=ucstring.ptr();
  480. }
  481. #endif
  482. }
  483. Error EditorExportPlatformAndroid::save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total) {
  484. APKExportData *ed=(APKExportData*)p_userdata;
  485. String dst_path=p_path;
  486. dst_path=dst_path.replace_first("res://","assets/");
  487. zipOpenNewFileInZip(ed->apk,
  488. dst_path.utf8().get_data(),
  489. NULL,
  490. NULL,
  491. 0,
  492. NULL,
  493. 0,
  494. NULL,
  495. Z_DEFLATED,
  496. Z_DEFAULT_COMPRESSION);
  497. zipWriteInFileInZip(ed->apk,p_data.ptr(),p_data.size());
  498. zipCloseFileInZip(ed->apk);
  499. ed->ep->step("File: "+p_path,3+p_file*100/p_total);
  500. return OK;
  501. }
  502. Error EditorExportPlatformAndroid::export_project(const String& p_path,bool p_debug,const String& p_password) {
  503. String src_apk;
  504. EditorProgress ep("export","Exporting for Android",104);
  505. String apk_path = EditorSettings::get_singleton()->get_settings_path()+"/templates/";
  506. if (p_debug) {
  507. src_apk=custom_debug_package!=""?custom_debug_package:apk_path+"android_debug.apk";
  508. } else {
  509. src_apk=custom_release_package!=""?custom_release_package:apk_path+"android_release.apk";
  510. }
  511. FileAccess *src_f=NULL;
  512. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  513. ep.step("Creating APK",0);
  514. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  515. if (!pkg) {
  516. EditorNode::add_io_error("Could not find template APK to export:\n"+src_apk);
  517. return ERR_FILE_NOT_FOUND;
  518. }
  519. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  520. int ret = unzGoToFirstFile(pkg);
  521. zlib_filefunc_def io2=io;
  522. FileAccess *dst_f=NULL;
  523. io2.opaque=&dst_f;
  524. zipFile apk=zipOpen2(p_path.utf8().get_data(),APPEND_STATUS_CREATE,NULL,&io2);
  525. while(ret==UNZ_OK) {
  526. //get filename
  527. unz_file_info info;
  528. char fname[16384];
  529. ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
  530. String file=fname;
  531. Vector<uint8_t> data;
  532. data.resize(info.uncompressed_size);
  533. //read
  534. unzOpenCurrentFile(pkg);
  535. unzReadCurrentFile(pkg,data.ptr(),data.size());
  536. unzCloseCurrentFile(pkg);
  537. //write
  538. if (file=="AndroidManifest.xml") {
  539. _fix_manifest(data);
  540. }
  541. if (file=="resources.arsc") {
  542. _fix_resources(data);
  543. }
  544. if (file=="res/drawable/icon.png") {
  545. bool found=false;
  546. if (this->icon!="" && this->icon.ends_with(".png")) {
  547. FileAccess *f = FileAccess::open(this->icon,FileAccess::READ);
  548. if (f) {
  549. data.resize(f->get_len());
  550. f->get_buffer(data.ptr(),data.size());
  551. memdelete(f);
  552. found=true;
  553. }
  554. }
  555. if (!found) {
  556. String appicon = Globals::get_singleton()->get("application/icon");
  557. if (appicon!="" && appicon.ends_with(".png")) {
  558. FileAccess*f = FileAccess::open(appicon,FileAccess::READ);
  559. if (f) {
  560. data.resize(f->get_len());
  561. f->get_buffer(data.ptr(),data.size());
  562. memdelete(f);
  563. }
  564. }
  565. }
  566. }
  567. print_line("ADDING: "+file);
  568. zipOpenNewFileInZip(apk,
  569. file.utf8().get_data(),
  570. NULL,
  571. NULL,
  572. 0,
  573. NULL,
  574. 0,
  575. NULL,
  576. Z_DEFLATED,
  577. Z_DEFAULT_COMPRESSION);
  578. zipWriteInFileInZip(apk,data.ptr(),data.size());
  579. zipCloseFileInZip(apk);
  580. ret = unzGoToNextFile(pkg);
  581. }
  582. ep.step("Adding Files..",1);
  583. APKExportData ed;
  584. ed.ep=&ep;
  585. ed.apk=apk;
  586. Error err = export_project_files(save_apk_file,&ed,false);
  587. zipClose(apk,NULL);
  588. unzClose(pkg);
  589. if (err) {
  590. return err;
  591. }
  592. if (_signed) {
  593. String jarsigner=EditorSettings::get_singleton()->get("android/jarsigner");
  594. if (!FileAccess::exists(jarsigner)) {
  595. EditorNode::add_io_error("'jarsigner' could not be found.\nPlease supply a path in the editor settings.\nResulting apk is unsigned.");
  596. return OK;
  597. }
  598. String keystore;
  599. String password;
  600. String user;
  601. if (p_debug) {
  602. keystore=EditorSettings::get_singleton()->get("android/debug_keystore");
  603. password=EditorSettings::get_singleton()->get("android/debug_keystore_pass");
  604. user=EditorSettings::get_singleton()->get("android/debug_keystore_user");
  605. ep.step("Signing Debug APK..",103);
  606. } else {
  607. keystore=release_keystore;
  608. password=p_password;
  609. user=release_username;
  610. ep.step("Signing Release APK..",103);
  611. }
  612. if (!FileAccess::exists(keystore)) {
  613. EditorNode::add_io_error("Could not find keytore, unable to export.");
  614. return ERR_FILE_CANT_OPEN;
  615. }
  616. List<String> args;
  617. args.push_back("-digestalg");
  618. args.push_back("SHA1");
  619. args.push_back("-sigalg");
  620. args.push_back("MD5withRSA");
  621. args.push_back("-verbose");
  622. args.push_back("-keystore");
  623. args.push_back(keystore);
  624. args.push_back("-storepass");
  625. args.push_back(password);
  626. args.push_back(p_path);
  627. args.push_back(user);
  628. int retval;
  629. int err = OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  630. if (retval) {
  631. EditorNode::add_io_error("'jarsigner' returned with error #"+itos(retval));
  632. return ERR_CANT_CREATE;
  633. }
  634. ep.step("Verifying APK..",104);
  635. args.clear();
  636. args.push_back("-verify");
  637. args.push_back(p_path);
  638. args.push_back("-verbose");
  639. err = OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  640. if (retval) {
  641. EditorNode::add_io_error("'jarsigner' verificaiton of APK failed. Make sure to use jarsigner from Java 6.");
  642. return ERR_CANT_CREATE;
  643. }
  644. }
  645. return OK;
  646. }
  647. bool EditorExportPlatformAndroid::poll_devices() {
  648. bool dc=devices_changed;
  649. devices_changed=false;
  650. return dc;
  651. }
  652. int EditorExportPlatformAndroid::get_device_count() const {
  653. device_lock->lock();
  654. int dc=devices.size();
  655. device_lock->unlock();
  656. return dc;
  657. }
  658. String EditorExportPlatformAndroid::get_device_name(int p_device) const {
  659. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  660. device_lock->lock();
  661. String s=devices[p_device].name;
  662. device_lock->unlock();
  663. return s;
  664. }
  665. String EditorExportPlatformAndroid::get_device_info(int p_device) const {
  666. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  667. device_lock->lock();
  668. String s=devices[p_device].description;
  669. device_lock->unlock();
  670. return s;
  671. }
  672. void EditorExportPlatformAndroid::_device_poll_thread(void *ud) {
  673. EditorExportPlatformAndroid *ea=(EditorExportPlatformAndroid *)ud;
  674. while(!ea->quit_request) {
  675. String adb=EditorSettings::get_singleton()->get("android/adb");
  676. if (!FileAccess::exists(adb)) {
  677. OS::get_singleton()->delay_usec(3000000);
  678. continue; //adb not configured
  679. }
  680. String devices;
  681. List<String> args;
  682. args.push_back("devices");
  683. int ec;
  684. Error err = OS::get_singleton()->execute(adb,args,true,NULL,&devices,&ec);
  685. Vector<String> ds = devices.split("\n");
  686. Vector<String> ldevices;
  687. for(int i=1;i<ds.size();i++) {
  688. String d = ds[i];
  689. int dpos = d.find("device");
  690. if (dpos==-1)
  691. continue;
  692. d=d.substr(0,dpos).strip_edges();
  693. // print_line("found devuce: "+d);
  694. ldevices.push_back(d);
  695. }
  696. ea->device_lock->lock();
  697. bool different=false;
  698. if (devices.size()!=ldevices.size()) {
  699. different=true;
  700. } else {
  701. for(int i=0;i<ea->devices.size();i++) {
  702. if (ea->devices[i].id!=ldevices[i]) {
  703. different=true;
  704. break;
  705. }
  706. }
  707. }
  708. if (different) {
  709. Vector<Device> ndevices;
  710. for(int i=0;i<ldevices.size();i++) {
  711. Device d;
  712. d.id=ldevices[i];
  713. for(int j=0;j<ea->devices.size();j++) {
  714. if (ea->devices[j].id==ldevices[i]) {
  715. d.description=ea->devices[j].description;
  716. d.name=ea->devices[j].name;
  717. }
  718. }
  719. if (d.description=="") {
  720. //in the oven, request!
  721. args.clear();
  722. args.push_back("-s");
  723. args.push_back(d.id);
  724. args.push_back("shell");
  725. args.push_back("cat");
  726. args.push_back("/system/build.prop");
  727. int ec;
  728. String dp;
  729. Error err = OS::get_singleton()->execute(adb,args,true,NULL,&dp,&ec);
  730. print_line("RV: "+itos(ec));
  731. Vector<String> props = dp.split("\n");
  732. String vendor;
  733. String device;
  734. d.description+"Device ID: "+d.id+"\n";
  735. for(int j=0;j<props.size();j++) {
  736. String p = props[j];
  737. if (p.begins_with("ro.product.model=")) {
  738. device=p.get_slice("=",1).strip_edges();
  739. } else if (p.begins_with("ro.product.brand=")) {
  740. vendor=p.get_slice("=",1).strip_edges().capitalize();
  741. } else if (p.begins_with("ro.build.display.id=")) {
  742. d.description+="Build: "+p.get_slice("=",1).strip_edges()+"\n";
  743. } else if (p.begins_with("ro.build.version.release=")) {
  744. d.description+="Release: "+p.get_slice("=",1).strip_edges()+"\n";
  745. } else if (p.begins_with("ro.product.cpu.abi=")) {
  746. d.description+="CPU: "+p.get_slice("=",1).strip_edges()+"\n";
  747. } else if (p.begins_with("ro.product.manufacturer=")) {
  748. d.description+="Manufacturer: "+p.get_slice("=",1).strip_edges()+"\n";
  749. } else if (p.begins_with("ro.board.platform=")) {
  750. d.description+="Chipset: "+p.get_slice("=",1).strip_edges()+"\n";
  751. } else if (p.begins_with("ro.opengles.version=")) {
  752. uint32_t opengl = p.get_slice("=",1).to_int();
  753. d.description+="OpenGL: "+itos(opengl>>16)+"."+itos((opengl>>8)&0xFF)+"."+itos((opengl)&0xFF)+"\n";
  754. }
  755. }
  756. d.name=vendor+" "+device;
  757. // print_line("name: "+d.name);
  758. // print_line("description: "+d.description);
  759. }
  760. ndevices.push_back(d);
  761. }
  762. ea->devices=ndevices;
  763. ea->devices_changed=true;
  764. }
  765. ea->device_lock->unlock();
  766. OS::get_singleton()->delay_usec(3000000);
  767. }
  768. }
  769. Error EditorExportPlatformAndroid::run(int p_device) {
  770. ERR_FAIL_INDEX_V(p_device,devices.size(),ERR_INVALID_PARAMETER);
  771. device_lock->lock();
  772. EditorProgress ep("run","Running on "+devices[p_device].name,3);
  773. String adb=EditorSettings::get_singleton()->get("android/adb");
  774. if (adb=="") {
  775. EditorNode::add_io_error("ADB executable not configured in settings, can't run.");
  776. device_lock->unlock();
  777. return ERR_UNCONFIGURED;
  778. }
  779. //export_temp
  780. ep.step("Exporting APK",0);
  781. String export_to=EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmpexport.apk";
  782. Error err = export_project(export_to,true);
  783. if (err) {
  784. device_lock->unlock();
  785. return err;
  786. }
  787. ep.step("Uninstalling..",1);
  788. print_line("Uninstalling previous version: "+devices[p_device].name);
  789. List<String> args;
  790. args.push_back("-s");
  791. args.push_back(devices[p_device].id);
  792. args.push_back("uninstall");
  793. args.push_back(package);
  794. int rv;
  795. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  796. #if 0
  797. if (err || rv!=0) {
  798. EditorNode::add_io_error("Could not install to device.");
  799. device_lock->unlock();
  800. return ERR_CANT_CREATE;
  801. }
  802. #endif
  803. print_line("Installing into device (please wait..): "+devices[p_device].name);
  804. ep.step("Installing to Device (please wait..)..",2);
  805. args.clear();
  806. args.push_back("-s");
  807. args.push_back(devices[p_device].id);
  808. args.push_back("install");
  809. args.push_back(export_to);
  810. rv;
  811. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  812. if (err || rv!=0) {
  813. EditorNode::add_io_error("Could not install to device.");
  814. device_lock->unlock();
  815. return ERR_CANT_CREATE;
  816. }
  817. ep.step("Running on Device..",3);
  818. args.clear();
  819. args.push_back("-s");
  820. args.push_back(devices[p_device].id);
  821. args.push_back("shell");
  822. args.push_back("am");
  823. args.push_back("start");
  824. args.push_back("-a");
  825. args.push_back("android.intent.action.MAIN");
  826. args.push_back("-n");
  827. args.push_back(package+"/com.android.godot.Godot");
  828. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  829. if (err || rv!=0) {
  830. EditorNode::add_io_error("Could not execute ondevice.");
  831. device_lock->unlock();
  832. return ERR_CANT_CREATE;
  833. }
  834. device_lock->unlock();
  835. return OK;
  836. }
  837. EditorExportPlatformAndroid::EditorExportPlatformAndroid() {
  838. version_code=1;
  839. version_name="1.0";
  840. package="com.android.noname";
  841. name="";
  842. _signed=true;
  843. device_lock = Mutex::create();
  844. quit_request=false;
  845. orientation=0;
  846. device_thread=Thread::create(_device_poll_thread,this);
  847. devices_changed=true;
  848. Image img( _android_logo );
  849. logo = Ref<ImageTexture>( memnew( ImageTexture ));
  850. logo->create_from_image(img);
  851. }
  852. bool EditorExportPlatformAndroid::can_export(String *r_error) const {
  853. bool valid=true;
  854. String adb=EditorSettings::get_singleton()->get("android/adb");
  855. String err;
  856. if (!FileAccess::exists(adb)) {
  857. valid=false;
  858. err+="ADB executable not configured in editor settings.\n";
  859. }
  860. String js = EditorSettings::get_singleton()->get("android/jarsigner");
  861. if (!FileAccess::exists(js)) {
  862. valid=false;
  863. err+="OpenJDK 6 jarsigner not configured in editor settings.\n";
  864. }
  865. String dk = EditorSettings::get_singleton()->get("android/debug_keystore");
  866. if (!FileAccess::exists(dk)) {
  867. valid=false;
  868. err+="Debug Keystore not configured in editor settings.\n";
  869. }
  870. String exe_path = EditorSettings::get_singleton()->get_settings_path()+"/templates/";
  871. if (!FileAccess::exists(exe_path+"android_debug.apk") || !FileAccess::exists(exe_path+"android_release.apk")) {
  872. valid=false;
  873. err+="No export templates found.\nDownload and install export templates.\n";
  874. }
  875. if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
  876. valid=false;
  877. err+="Custom debug package not found.\n";
  878. }
  879. if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
  880. valid=false;
  881. err+="Custom release package not found.\n";
  882. }
  883. if (r_error)
  884. *r_error=err;
  885. return valid;
  886. }
  887. EditorExportPlatformAndroid::~EditorExportPlatformAndroid() {
  888. quit_request=true;
  889. Thread::wait_to_finish(device_thread);
  890. }
  891. void register_android_exporter() {
  892. String exe_ext=OS::get_singleton()->get_name()=="Windows"?"exe":"";
  893. EDITOR_DEF("android/adb","");
  894. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/adb",PROPERTY_HINT_GLOBAL_FILE,exe_ext));
  895. EDITOR_DEF("android/jarsigner","");
  896. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/jarsigner",PROPERTY_HINT_GLOBAL_FILE,exe_ext));
  897. EDITOR_DEF("android/debug_keystore","");
  898. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/debug_keystore",PROPERTY_HINT_GLOBAL_FILE,"keystore"));
  899. EDITOR_DEF("android/debug_keystore_user","androiddebugkey");
  900. EDITOR_DEF("android/debug_keystore_pass","android");
  901. //EDITOR_DEF("android/release_keystore","");
  902. //EDITOR_DEF("android/release_username","");
  903. //EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/release_keystore",PROPERTY_HINT_GLOBAL_FILE,"*.keystore"));
  904. Ref<EditorExportPlatformAndroid> exporter = Ref<EditorExportPlatformAndroid>( memnew(EditorExportPlatformAndroid) );
  905. EditorImportExport::get_singleton()->add_export_platform(exporter);
  906. }