export.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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. static const char* android_perms[]={
  13. "ACCESS_CHECKIN_PROPERTIES",
  14. "ACCESS_COARSE_LOCATION",
  15. "ACCESS_FINE_LOCATION",
  16. "ACCESS_LOCATION_EXTRA_COMMANDS",
  17. "ACCESS_MOCK_LOCATION",
  18. "ACCESS_NETWORK_STATE",
  19. "ACCESS_SURFACE_FLINGER",
  20. "ACCESS_WIFI_STATE",
  21. "ACCOUNT_MANAGER",
  22. "ADD_VOICEMAIL",
  23. "AUTHENTICATE_ACCOUNTS",
  24. "BATTERY_STATS",
  25. "BIND_ACCESSIBILITY_SERVICE",
  26. "BIND_APPWIDGET",
  27. "BIND_DEVICE_ADMIN",
  28. "BIND_INPUT_METHOD",
  29. "BIND_NFC_SERVICE",
  30. "BIND_NOTIFICATION_LISTENER_SERVICE",
  31. "BIND_PRINT_SERVICE",
  32. "BIND_REMOTEVIEWS",
  33. "BIND_TEXT_SERVICE",
  34. "BIND_VPN_SERVICE",
  35. "BIND_WALLPAPER",
  36. "BLUETOOTH",
  37. "BLUETOOTH_ADMIN",
  38. "BLUETOOTH_PRIVILEGED",
  39. "BRICK",
  40. "BROADCAST_PACKAGE_REMOVED",
  41. "BROADCAST_SMS",
  42. "BROADCAST_STICKY",
  43. "BROADCAST_WAP_PUSH",
  44. "CALL_PHONE",
  45. "CALL_PRIVILEGED",
  46. "CAMERA",
  47. "CAPTURE_AUDIO_OUTPUT",
  48. "CAPTURE_SECURE_VIDEO_OUTPUT",
  49. "CAPTURE_VIDEO_OUTPUT",
  50. "CHANGE_COMPONENT_ENABLED_STATE",
  51. "CHANGE_CONFIGURATION",
  52. "CHANGE_NETWORK_STATE",
  53. "CHANGE_WIFI_MULTICAST_STATE",
  54. "CHANGE_WIFI_STATE",
  55. "CLEAR_APP_CACHE",
  56. "CLEAR_APP_USER_DATA",
  57. "CONTROL_LOCATION_UPDATES",
  58. "DELETE_CACHE_FILES",
  59. "DELETE_PACKAGES",
  60. "DEVICE_POWER",
  61. "DIAGNOSTIC",
  62. "DISABLE_KEYGUARD",
  63. "DUMP",
  64. "EXPAND_STATUS_BAR",
  65. "FACTORY_TEST",
  66. "FLASHLIGHT",
  67. "FORCE_BACK",
  68. "GET_ACCOUNTS",
  69. "GET_PACKAGE_SIZE",
  70. "GET_TASKS",
  71. "GET_TOP_ACTIVITY_INFO",
  72. "GLOBAL_SEARCH",
  73. "HARDWARE_TEST",
  74. "INJECT_EVENTS",
  75. "INSTALL_LOCATION_PROVIDER",
  76. "INSTALL_PACKAGES",
  77. "INSTALL_SHORTCUT",
  78. "INTERNAL_SYSTEM_WINDOW",
  79. "INTERNET",
  80. "KILL_BACKGROUND_PROCESSES",
  81. "LOCATION_HARDWARE",
  82. "MANAGE_ACCOUNTS",
  83. "MANAGE_APP_TOKENS",
  84. "MANAGE_DOCUMENTS",
  85. "MASTER_CLEAR",
  86. "MEDIA_CONTENT_CONTROL",
  87. "MODIFY_AUDIO_SETTINGS",
  88. "MODIFY_PHONE_STATE",
  89. "MOUNT_FORMAT_FILESYSTEMS",
  90. "MOUNT_UNMOUNT_FILESYSTEMS",
  91. "NFC",
  92. "PERSISTENT_ACTIVITY",
  93. "PROCESS_OUTGOING_CALLS",
  94. "READ_CALENDAR",
  95. "READ_CALL_LOG",
  96. "READ_CONTACTS",
  97. "READ_EXTERNAL_STORAGE",
  98. "READ_FRAME_BUFFER",
  99. "READ_HISTORY_BOOKMARKS",
  100. "READ_INPUT_STATE",
  101. "READ_LOGS",
  102. "READ_PHONE_STATE",
  103. "READ_PROFILE",
  104. "READ_SMS",
  105. "READ_SOCIAL_STREAM",
  106. "READ_SYNC_SETTINGS",
  107. "READ_SYNC_STATS",
  108. "READ_USER_DICTIONARY",
  109. "REBOOT",
  110. "RECEIVE_BOOT_COMPLETED",
  111. "RECEIVE_MMS",
  112. "RECEIVE_SMS",
  113. "RECEIVE_WAP_PUSH",
  114. "RECORD_AUDIO",
  115. "REORDER_TASKS",
  116. "RESTART_PACKAGES",
  117. "SEND_RESPOND_VIA_MESSAGE",
  118. "SEND_SMS",
  119. "SET_ACTIVITY_WATCHER",
  120. "SET_ALARM",
  121. "SET_ALWAYS_FINISH",
  122. "SET_ANIMATION_SCALE",
  123. "SET_DEBUG_APP",
  124. "SET_ORIENTATION",
  125. "SET_POINTER_SPEED",
  126. "SET_PREFERRED_APPLICATIONS",
  127. "SET_PROCESS_LIMIT",
  128. "SET_TIME",
  129. "SET_TIME_ZONE",
  130. "SET_WALLPAPER",
  131. "SET_WALLPAPER_HINTS",
  132. "SIGNAL_PERSISTENT_PROCESSES",
  133. "STATUS_BAR",
  134. "SUBSCRIBED_FEEDS_READ",
  135. "SUBSCRIBED_FEEDS_WRITE",
  136. "SYSTEM_ALERT_WINDOW",
  137. "TRANSMIT_IR",
  138. "UNINSTALL_SHORTCUT",
  139. "UPDATE_DEVICE_STATS",
  140. "USE_CREDENTIALS",
  141. "USE_SIP",
  142. "VIBRATE",
  143. "WAKE_LOCK",
  144. "WRITE_APN_SETTINGS",
  145. "WRITE_CALENDAR",
  146. "WRITE_CALL_LOG",
  147. "WRITE_CONTACTS",
  148. "WRITE_EXTERNAL_STORAGE",
  149. "WRITE_GSERVICES",
  150. "WRITE_HISTORY_BOOKMARKS",
  151. "WRITE_PROFILE",
  152. "WRITE_SECURE_SETTINGS",
  153. "WRITE_SETTINGS",
  154. "WRITE_SMS",
  155. "WRITE_SOCIAL_STREAM",
  156. "WRITE_SYNC_SETTINGS",
  157. "WRITE_USER_DICTIONARY",
  158. NULL};
  159. class EditorExportPlatformAndroid : public EditorExportPlatform {
  160. OBJ_TYPE( EditorExportPlatformAndroid,EditorExportPlatform );
  161. enum {
  162. MAX_USER_PERMISSIONS=20,
  163. SCREEN_SMALL=0,
  164. SCREEN_NORMAL=1,
  165. SCREEN_LARGE=2,
  166. SCREEN_XLARGE=3,
  167. SCREEN_MAX=4
  168. };
  169. String custom_release_package;
  170. String custom_debug_package;
  171. int version_code;
  172. String version_name;
  173. String package;
  174. String name;
  175. String icon;
  176. String cmdline;
  177. bool _signed;
  178. bool apk_expansion;
  179. bool remove_prev;
  180. bool use_32_fb;
  181. String apk_expansion_salt;
  182. String apk_expansion_pkey;
  183. int orientation;
  184. String release_keystore;
  185. String release_password;
  186. String release_username;
  187. struct APKExportData {
  188. zipFile apk;
  189. EditorProgress *ep;
  190. };
  191. struct Device {
  192. String id;
  193. String name;
  194. String description;
  195. };
  196. Vector<Device> devices;
  197. bool devices_changed;
  198. Mutex *device_lock;
  199. Thread *device_thread;
  200. Ref<ImageTexture> logo;
  201. Set<String> perms;
  202. String user_perms[MAX_USER_PERMISSIONS];
  203. bool screen_support[SCREEN_MAX];
  204. volatile bool quit_request;
  205. static void _device_poll_thread(void *ud);
  206. String get_project_name() const;
  207. void _fix_manifest(Vector<uint8_t>& p_manifest);
  208. void _fix_resources(Vector<uint8_t>& p_manifest);
  209. static Error save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total);
  210. protected:
  211. bool _set(const StringName& p_name, const Variant& p_value);
  212. bool _get(const StringName& p_name,Variant &r_ret) const;
  213. void _get_property_list( List<PropertyInfo> *p_list) const;
  214. public:
  215. virtual String get_name() const { return "Android"; }
  216. virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_ETC1; }
  217. virtual Ref<Texture> get_logo() const { return logo; }
  218. virtual bool poll_devices();
  219. virtual int get_device_count() const;
  220. virtual String get_device_name(int p_device) const;
  221. virtual String get_device_info(int p_device) const;
  222. virtual Error run(int p_device,bool p_dumb=false);
  223. virtual bool requieres_password(bool p_debug) const { return !p_debug; }
  224. virtual String get_binary_extension() const { return "apk"; }
  225. virtual Error export_project(const String& p_path,bool p_debug,bool p_dumb=false);
  226. virtual bool can_export(String *r_error=NULL) const;
  227. EditorExportPlatformAndroid();
  228. ~EditorExportPlatformAndroid();
  229. };
  230. bool EditorExportPlatformAndroid::_set(const StringName& p_name, const Variant& p_value) {
  231. String n=p_name;
  232. if (n=="one_click_deploy/clear_previous_install")
  233. remove_prev=p_value;
  234. else if (n=="custom_package/debug")
  235. custom_debug_package=p_value;
  236. else if (n=="custom_package/release")
  237. custom_release_package=p_value;
  238. else if (n=="version/code")
  239. version_code=p_value;
  240. else if (n=="version/name")
  241. version_name=p_value;
  242. else if (n=="command_line/extra_args")
  243. cmdline=p_value;
  244. else if (n=="package/unique_name")
  245. package=p_value;
  246. else if (n=="package/name")
  247. name=p_value;
  248. else if (n=="package/icon")
  249. icon=p_value;
  250. else if (n=="package/signed")
  251. _signed=p_value;
  252. else if (n=="screen/use_32_bits_view")
  253. use_32_fb=p_value;
  254. else if (n=="screen/orientation")
  255. orientation=p_value;
  256. else if (n=="screen/support_small")
  257. screen_support[SCREEN_SMALL]=p_value;
  258. else if (n=="screen/support_normal")
  259. screen_support[SCREEN_NORMAL]=p_value;
  260. else if (n=="screen/support_large")
  261. screen_support[SCREEN_LARGE]=p_value;
  262. else if (n=="screen/support_xlarge")
  263. screen_support[SCREEN_XLARGE]=p_value;
  264. else if (n=="keystore/release")
  265. release_keystore=p_value;
  266. else if (n=="keystore/release_user")
  267. release_username=p_value;
  268. else if (n=="keystore/release_password")
  269. release_password=p_value;
  270. else if (n=="apk_expansion/enable")
  271. apk_expansion=p_value;
  272. else if (n=="apk_expansion/SALT")
  273. apk_expansion_salt=p_value;
  274. else if (n=="apk_expansion/public_key")
  275. apk_expansion_pkey=p_value;
  276. else if (n.begins_with("permissions/")) {
  277. String what = n.get_slice("/",1).to_upper();
  278. bool state = p_value;
  279. if (state)
  280. perms.insert(what);
  281. else
  282. perms.erase(what);
  283. } else if (n.begins_with("user_permissions/")) {
  284. int which = n.get_slice("/",1).to_int();
  285. ERR_FAIL_INDEX_V(which,MAX_USER_PERMISSIONS,false);
  286. user_perms[which]=p_value;
  287. } else
  288. return false;
  289. return true;
  290. }
  291. bool EditorExportPlatformAndroid::_get(const StringName& p_name,Variant &r_ret) const{
  292. String n=p_name;
  293. if (n=="one_click_deploy/clear_previous_install")
  294. r_ret=remove_prev;
  295. else if (n=="custom_package/debug")
  296. r_ret=custom_debug_package;
  297. else if (n=="custom_package/release")
  298. r_ret=custom_release_package;
  299. else if (n=="version/code")
  300. r_ret=version_code;
  301. else if (n=="version/name")
  302. r_ret=version_name;
  303. else if (n=="command_line/extra_args")
  304. r_ret=cmdline;
  305. else if (n=="package/unique_name")
  306. r_ret=package;
  307. else if (n=="package/name")
  308. r_ret=name;
  309. else if (n=="package/icon")
  310. r_ret=icon;
  311. else if (n=="package/signed")
  312. r_ret=_signed;
  313. else if (n=="screen/use_32_bits_view")
  314. r_ret=use_32_fb;
  315. else if (n=="screen/orientation")
  316. r_ret=orientation;
  317. else if (n=="screen/support_small")
  318. r_ret=screen_support[SCREEN_SMALL];
  319. else if (n=="screen/support_normal")
  320. r_ret=screen_support[SCREEN_NORMAL];
  321. else if (n=="screen/support_large")
  322. r_ret=screen_support[SCREEN_LARGE];
  323. else if (n=="screen/support_xlarge")
  324. r_ret=screen_support[SCREEN_XLARGE];
  325. else if (n=="keystore/release")
  326. r_ret=release_keystore;
  327. else if (n=="keystore/release_user")
  328. r_ret=release_username;
  329. else if (n=="keystore/release_password")
  330. r_ret=release_password;
  331. else if (n=="apk_expansion/enable")
  332. r_ret=apk_expansion;
  333. else if (n=="apk_expansion/SALT")
  334. r_ret=apk_expansion_salt;
  335. else if (n=="apk_expansion/public_key")
  336. r_ret=apk_expansion_pkey;
  337. else if (n.begins_with("permissions/")) {
  338. String what = n.get_slice("/",1).to_upper();
  339. r_ret = perms.has(what);
  340. } else if (n.begins_with("user_permissions/")) {
  341. int which = n.get_slice("/",1).to_int();
  342. ERR_FAIL_INDEX_V(which,MAX_USER_PERMISSIONS,false);
  343. r_ret=user_perms[which];
  344. } else
  345. return false;
  346. return true;
  347. }
  348. void EditorExportPlatformAndroid::_get_property_list( List<PropertyInfo> *p_list) const{
  349. p_list->push_back( PropertyInfo( Variant::BOOL, "one_click_deploy/clear_previous_install"));
  350. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE,"apk"));
  351. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"apk"));
  352. p_list->push_back( PropertyInfo( Variant::STRING, "command_line/extra_args"));
  353. p_list->push_back( PropertyInfo( Variant::INT, "version/code", PROPERTY_HINT_RANGE,"1,65535,1"));
  354. p_list->push_back( PropertyInfo( Variant::STRING, "version/name") );
  355. p_list->push_back( PropertyInfo( Variant::STRING, "package/unique_name") );
  356. p_list->push_back( PropertyInfo( Variant::STRING, "package/name") );
  357. p_list->push_back( PropertyInfo( Variant::STRING, "package/icon",PROPERTY_HINT_FILE,"png") );
  358. p_list->push_back( PropertyInfo( Variant::BOOL, "package/signed") );
  359. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/use_32_bits_view") );
  360. p_list->push_back( PropertyInfo( Variant::INT, "screen/orientation",PROPERTY_HINT_ENUM,"Landscape,Portrait") );
  361. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_small") );
  362. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_normal") );
  363. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_large") );
  364. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_xlarge") );
  365. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release",PROPERTY_HINT_GLOBAL_FILE,"keystore") );
  366. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release_user" ) );
  367. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release_password" ) );
  368. p_list->push_back( PropertyInfo( Variant::BOOL, "apk_expansion/enable" ) );
  369. p_list->push_back( PropertyInfo( Variant::STRING, "apk_expansion/SALT" ) );
  370. p_list->push_back( PropertyInfo( Variant::STRING, "apk_expansion/public_key",PROPERTY_HINT_MULTILINE_TEXT ) );
  371. const char **perms = android_perms;
  372. while(*perms) {
  373. p_list->push_back( PropertyInfo( Variant::BOOL, "permissions/"+String(*perms).to_lower()));
  374. perms++;
  375. }
  376. for(int i=0;i<MAX_USER_PERMISSIONS;i++) {
  377. p_list->push_back( PropertyInfo( Variant::STRING, "user_permissions/"+itos(i)));
  378. }
  379. //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  380. }
  381. static String _parse_string(const uint8_t *p_bytes,bool p_utf8) {
  382. uint32_t offset=0;
  383. uint32_t len = decode_uint16(&p_bytes[offset]);
  384. if (p_utf8) {
  385. //don't know how to read extended utf8, this will have to be for now
  386. len>>=8;
  387. }
  388. offset+=2;
  389. //printf("len %i, unicode: %i\n",len,int(p_utf8));
  390. if (p_utf8) {
  391. Vector<uint8_t> str8;
  392. str8.resize(len+1);
  393. for(int i=0;i<len;i++) {
  394. str8[i]=p_bytes[offset+i];
  395. }
  396. str8[len]=0;
  397. String str;
  398. str.parse_utf8((const char*)str8.ptr());
  399. return str;
  400. } else {
  401. String str;
  402. for(int i=0;i<len;i++) {
  403. CharType c = decode_uint16(&p_bytes[offset+i*2]);
  404. if (c==0)
  405. break;
  406. str += String::chr(c);
  407. }
  408. return str;
  409. }
  410. }
  411. void EditorExportPlatformAndroid::_fix_resources(Vector<uint8_t>& p_manifest) {
  412. const int UTF8_FLAG = 0x00000100;
  413. print_line("*******************GORRRGLE***********************");
  414. uint32_t header = decode_uint32(&p_manifest[0]);
  415. uint32_t filesize = decode_uint32(&p_manifest[4]);
  416. uint32_t string_block_len = decode_uint32(&p_manifest[16]);
  417. uint32_t string_count = decode_uint32(&p_manifest[20]);
  418. uint32_t string_flags = decode_uint32(&p_manifest[28]);
  419. const uint32_t string_table_begins = 40;
  420. Vector<String> string_table;
  421. printf("stirng block len: %i\n",string_block_len);
  422. printf("stirng count: %i\n",string_count);
  423. printf("flags: %x\n",string_flags);
  424. for(int i=0;i<string_count;i++) {
  425. uint32_t offset = decode_uint32(&p_manifest[string_table_begins+i*4]);
  426. offset+=string_table_begins+string_count*4;
  427. String str = _parse_string(&p_manifest[offset],string_flags&UTF8_FLAG);
  428. if (str.begins_with("godot-project-name")) {
  429. if (str=="godot-project-name") {
  430. //project name
  431. str = get_project_name();
  432. } else {
  433. String lang = str.substr(str.find_last("-")+1,str.length()).replace("-","_");
  434. String prop = "application/name_"+lang;
  435. if (Globals::get_singleton()->has(prop)) {
  436. str = Globals::get_singleton()->get(prop);
  437. } else {
  438. str = get_project_name();
  439. }
  440. }
  441. }
  442. string_table.push_back(str);
  443. }
  444. //write a new string table, but use 16 bits
  445. Vector<uint8_t> ret;
  446. ret.resize(string_table_begins+string_table.size()*4);
  447. for(int i=0;i<string_table_begins;i++) {
  448. ret[i]=p_manifest[i];
  449. }
  450. int ofs=0;
  451. for(int i=0;i<string_table.size();i++) {
  452. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  453. ofs+=string_table[i].length()*2+2+2;
  454. }
  455. ret.resize(ret.size()+ofs);
  456. uint8_t *chars=&ret[ret.size()-ofs];
  457. for(int i=0;i<string_table.size();i++) {
  458. String s = string_table[i];
  459. encode_uint16(s.length(),chars);
  460. chars+=2;
  461. for(int j=0;j<s.length();j++) {
  462. encode_uint16(s[j],chars);
  463. chars+=2;
  464. }
  465. encode_uint16(0,chars);
  466. chars+=2;
  467. }
  468. //pad
  469. while(ret.size()%4)
  470. ret.push_back(0);
  471. //change flags to not use utf8
  472. encode_uint32(string_flags&~0x100,&ret[28]);
  473. //change length
  474. encode_uint32(ret.size()-12,&ret[16]);
  475. //append the rest...
  476. int rest_from = 12+string_block_len;
  477. int rest_to = ret.size();
  478. int rest_len = (p_manifest.size() - rest_from);
  479. ret.resize(ret.size() + (p_manifest.size() - rest_from) );
  480. for(int i=0;i<rest_len;i++) {
  481. ret[rest_to+i]=p_manifest[rest_from+i];
  482. }
  483. //finally update the size
  484. encode_uint32(ret.size(),&ret[4]);
  485. p_manifest=ret;
  486. printf("end\n");
  487. }
  488. String EditorExportPlatformAndroid::get_project_name() const {
  489. String aname;
  490. if (this->name!="") {
  491. aname=this->name;
  492. } else {
  493. aname = Globals::get_singleton()->get("application/name");
  494. }
  495. if (aname=="") {
  496. aname=_MKSTR(VERSION_NAME);
  497. }
  498. return aname;
  499. }
  500. void EditorExportPlatformAndroid::_fix_manifest(Vector<uint8_t>& p_manifest) {
  501. const int CHUNK_AXML_FILE = 0x00080003;
  502. const int CHUNK_RESOURCEIDS = 0x00080180;
  503. const int CHUNK_STRINGS = 0x001C0001;
  504. const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  505. const int CHUNK_XML_END_TAG = 0x00100103;
  506. const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  507. const int CHUNK_XML_START_TAG = 0x00100102;
  508. const int CHUNK_XML_TEXT = 0x00100104;
  509. const int UTF8_FLAG = 0x00000100;
  510. Vector<String> string_table;
  511. uint32_t ofs=0;
  512. uint32_t header = decode_uint32(&p_manifest[ofs]);
  513. uint32_t filesize = decode_uint32(&p_manifest[ofs+4]);
  514. ofs+=8;
  515. // print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));
  516. uint32_t string_count;
  517. uint32_t styles_count;
  518. uint32_t string_flags;
  519. uint32_t string_data_offset;
  520. uint32_t styles_offset;
  521. uint32_t string_table_begins;
  522. uint32_t string_table_ends;
  523. Vector<uint8_t> stable_extra;
  524. while(ofs < p_manifest.size()) {
  525. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  526. uint32_t size = decode_uint32(&p_manifest[ofs+4]);
  527. switch(chunk) {
  528. case CHUNK_STRINGS: {
  529. int iofs=ofs+8;
  530. uint32_t string_count=decode_uint32(&p_manifest[iofs]);
  531. uint32_t styles_count=decode_uint32(&p_manifest[iofs+4]);
  532. uint32_t string_flags=decode_uint32(&p_manifest[iofs+8]);
  533. uint32_t string_data_offset=decode_uint32(&p_manifest[iofs+12]);
  534. uint32_t styles_offset=decode_uint32(&p_manifest[iofs+16]);
  535. /*
  536. printf("string count: %i\n",string_count);
  537. printf("flags: %i\n",string_flags);
  538. printf("sdata ofs: %i\n",string_data_offset);
  539. printf("styles ofs: %i\n",styles_offset);
  540. */
  541. uint32_t st_offset=iofs+20;
  542. string_table.resize(string_count);
  543. uint32_t string_end=0;
  544. string_table_begins=st_offset;
  545. for(int i=0;i<string_count;i++) {
  546. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  547. string_at+=st_offset+string_count*4;
  548. ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
  549. ERR_FAIL_COND(string_flags&UTF8_FLAG);
  550. if (string_flags&UTF8_FLAG) {
  551. } else {
  552. uint32_t len = decode_uint16(&p_manifest[string_at]);
  553. Vector<CharType> ucstring;
  554. ucstring.resize(len+1);
  555. for(int j=0;j<len;j++) {
  556. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  557. ucstring[j]=c;
  558. }
  559. string_end=MAX(string_at+2+2*len,string_end);
  560. ucstring[len]=0;
  561. string_table[i]=ucstring.ptr();
  562. }
  563. // print_line("String "+itos(i)+": "+string_table[i]);
  564. }
  565. for(int i=string_end;i<(ofs+size);i++) {
  566. stable_extra.push_back(p_manifest[i]);
  567. }
  568. // printf("stable extra: %i\n",int(stable_extra.size()));
  569. string_table_ends=ofs+size;
  570. // print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));
  571. } break;
  572. case CHUNK_XML_START_TAG: {
  573. int iofs=ofs+8;
  574. uint32_t line=decode_uint32(&p_manifest[iofs]);
  575. uint32_t nspace=decode_uint32(&p_manifest[iofs+8]);
  576. uint32_t name=decode_uint32(&p_manifest[iofs+12]);
  577. uint32_t check=decode_uint32(&p_manifest[iofs+16]);
  578. String tname=string_table[name];
  579. // printf("NSPACE: %i\n",nspace);
  580. //printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
  581. //printf("CHECK: %x\n",check);
  582. uint32_t attrcount=decode_uint32(&p_manifest[iofs+20]);
  583. iofs+=28;
  584. //printf("ATTRCOUNT: %x\n",attrcount);
  585. for(int i=0;i<attrcount;i++) {
  586. uint32_t attr_nspace=decode_uint32(&p_manifest[iofs]);
  587. uint32_t attr_name=decode_uint32(&p_manifest[iofs+4]);
  588. uint32_t attr_value=decode_uint32(&p_manifest[iofs+8]);
  589. uint32_t attr_flags=decode_uint32(&p_manifest[iofs+12]);
  590. uint32_t attr_resid=decode_uint32(&p_manifest[iofs+16]);
  591. String value;
  592. if (attr_value!=0xFFFFFFFF)
  593. value=string_table[attr_value];
  594. else
  595. value="Res #"+itos(attr_resid);
  596. String attrname = string_table[attr_name];
  597. String nspace;
  598. if (attr_nspace!=0xFFFFFFFF)
  599. nspace=string_table[attr_nspace];
  600. else
  601. nspace="";
  602. printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
  603. printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
  604. printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
  605. printf("ATTR %i FLAGS: %x\n",i,attr_flags);
  606. printf("ATTR %i RESID: %x\n",i,attr_resid);
  607. //replace project information
  608. if (tname=="manifest" && attrname=="package") {
  609. print_line("FOUND PACKAGE");
  610. string_table[attr_value]=package;
  611. }
  612. //print_line("tname: "+tname);
  613. //print_line("nspace: "+nspace);
  614. //print_line("attrname: "+attrname);
  615. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionCode") {
  616. print_line("FOUND versioncode");
  617. encode_uint32(version_code,&p_manifest[iofs+16]);
  618. }
  619. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionName") {
  620. print_line("FOUND versionname");
  621. if (attr_value==0xFFFFFFFF) {
  622. WARN_PRINT("Version name in a resource, should be plaintext")
  623. } else
  624. string_table[attr_value]=version_name;
  625. }
  626. if (tname=="activity" && /*nspace=="android" &&*/ attrname=="screenOrientation") {
  627. print_line("FOUND screen orientation");
  628. if (attr_value==0xFFFFFFFF) {
  629. WARN_PRINT("Version name in a resource, should be plaintext")
  630. } else {
  631. string_table[attr_value]=(orientation==0?"landscape":"portrait");
  632. }
  633. }
  634. if (tname=="application" && /*nspace=="android" &&*/ attrname=="label") {
  635. print_line("FOUND application");
  636. if (attr_value==0xFFFFFFFF) {
  637. WARN_PRINT("Application name in a resource, should be plaintext.")
  638. } else {
  639. String aname = get_project_name();
  640. string_table[attr_value]=aname;
  641. }
  642. }
  643. if (tname=="activity" && /*nspace=="android" &&*/ attrname=="label") {
  644. print_line("FOUND activity name");
  645. if (attr_value==0xFFFFFFFF) {
  646. WARN_PRINT("Activity name in a resource, should be plaintext")
  647. } else {
  648. String aname;
  649. if (this->name!="") {
  650. aname=this->name;
  651. } else {
  652. aname = Globals::get_singleton()->get("application/name");
  653. }
  654. if (aname=="") {
  655. aname=_MKSTR(VERSION_NAME);
  656. }
  657. print_line("APP NAME IS..."+aname);
  658. string_table[attr_value]=aname;
  659. }
  660. }
  661. if (tname=="uses-permission" && /*nspace=="android" &&*/ attrname=="name") {
  662. if (value.begins_with("godot.custom")) {
  663. int which = value.get_slice(".",2).to_int();
  664. if (which>=0 && which<MAX_USER_PERMISSIONS && user_perms[which].strip_edges()!="") {
  665. string_table[attr_value]=user_perms[which].strip_edges();
  666. }
  667. } else if (value.begins_with("godot.")) {
  668. String perm = value.get_slice(".",1);
  669. if (perms.has(perm)) {
  670. string_table[attr_value]="android.permission."+perm;
  671. }
  672. }
  673. }
  674. if (tname=="supports-screens" ) {
  675. if (attr_value==0xFFFFFFFF) {
  676. WARN_PRINT("Screen res name in a resource, should be plaintext")
  677. } else if (attrname=="smallScreens") {
  678. print_line("SMALLSCREEN");
  679. string_table[attr_value]=screen_support[SCREEN_SMALL]?"true":"false";
  680. } else if (attrname=="mediumScreens") {
  681. print_line("MEDSCREEN");
  682. string_table[attr_value]=screen_support[SCREEN_NORMAL]?"true":"false";
  683. } else if (attrname=="largeScreens") {
  684. print_line("LARGECREEN");
  685. string_table[attr_value]=screen_support[SCREEN_LARGE]?"true":"false";
  686. } else if (attrname=="xlargeScreens") {
  687. print_line("XLARGECREEN");
  688. string_table[attr_value]=screen_support[SCREEN_XLARGE]?"true":"false";
  689. }
  690. }
  691. iofs+=20;
  692. }
  693. } break;
  694. }
  695. printf("chunk %x: size: %d\n",chunk,size);
  696. ofs+=size;
  697. }
  698. printf("end\n");
  699. //create new andriodmanifest binary
  700. Vector<uint8_t> ret;
  701. ret.resize(string_table_begins+string_table.size()*4);
  702. for(int i=0;i<string_table_begins;i++) {
  703. ret[i]=p_manifest[i];
  704. }
  705. ofs=0;
  706. for(int i=0;i<string_table.size();i++) {
  707. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  708. ofs+=string_table[i].length()*2+2+2;
  709. print_line("ofs: "+itos(i)+": "+itos(ofs));
  710. }
  711. ret.resize(ret.size()+ofs);
  712. uint8_t *chars=&ret[ret.size()-ofs];
  713. for(int i=0;i<string_table.size();i++) {
  714. String s = string_table[i];
  715. print_line("savint string :"+s);
  716. encode_uint16(s.length(),chars);
  717. chars+=2;
  718. for(int j=0;j<s.length();j++) { //include zero?
  719. encode_uint16(s[j],chars);
  720. chars+=2;
  721. }
  722. encode_uint16(0,chars);
  723. chars+=2;
  724. }
  725. ret.resize(ret.size()+stable_extra.size());
  726. while(ret.size()%4)
  727. ret.push_back(0);
  728. for(int i=0;i<stable_extra.size();i++) {
  729. chars[i]=stable_extra[i];
  730. }
  731. uint32_t new_stable_end=ret.size();
  732. uint32_t extra = (p_manifest.size()-string_table_ends);
  733. ret.resize(new_stable_end + extra);
  734. for(int i=0;i<extra;i++)
  735. ret[new_stable_end+i]=p_manifest[string_table_ends+i];
  736. while(ret.size()%4)
  737. ret.push_back(0);
  738. encode_uint32(ret.size(),&ret[4]); //update new file size
  739. encode_uint32(new_stable_end-8,&ret[12]); //update new string table size
  740. print_line("file size: "+itos(ret.size()));
  741. p_manifest=ret;
  742. #if 0
  743. uint32_t header[9];
  744. for(int i=0;i<9;i++) {
  745. header[i]=decode_uint32(&p_manifest[i*4]);
  746. }
  747. print_line("STO: "+itos(header[3]));
  748. uint32_t st_offset=9*4;
  749. //ERR_FAIL_COND(header[3]!=0x24)
  750. uint32_t string_count=header[4];
  751. string_table.resize(string_count);
  752. for(int i=0;i<string_count;i++) {
  753. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  754. string_at+=st_offset+string_count*4;
  755. uint32_t len = decode_uint16(&p_manifest[string_at]);
  756. Vector<CharType> ucstring;
  757. ucstring.resize(len+1);
  758. for(int j=0;j<len;j++) {
  759. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  760. ucstring[j]=c;
  761. }
  762. ucstring[len]=0;
  763. string_table[i]=ucstring.ptr();
  764. }
  765. #endif
  766. }
  767. Error EditorExportPlatformAndroid::save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total) {
  768. APKExportData *ed=(APKExportData*)p_userdata;
  769. String dst_path=p_path;
  770. dst_path=dst_path.replace_first("res://","assets/");
  771. zipOpenNewFileInZip(ed->apk,
  772. dst_path.utf8().get_data(),
  773. NULL,
  774. NULL,
  775. 0,
  776. NULL,
  777. 0,
  778. NULL,
  779. Z_DEFLATED,
  780. Z_DEFAULT_COMPRESSION);
  781. zipWriteInFileInZip(ed->apk,p_data.ptr(),p_data.size());
  782. zipCloseFileInZip(ed->apk);
  783. ed->ep->step("File: "+p_path,3+p_file*100/p_total);
  784. return OK;
  785. }
  786. Error EditorExportPlatformAndroid::export_project(const String& p_path, bool p_debug, bool p_dumb) {
  787. String src_apk;
  788. EditorProgress ep("export","Exporting for Android",104);
  789. String apk_path = EditorSettings::get_singleton()->get_settings_path()+"/templates/";
  790. if (p_debug) {
  791. src_apk=custom_debug_package!=""?custom_debug_package:apk_path+"android_debug.apk";
  792. } else {
  793. src_apk=custom_release_package!=""?custom_release_package:apk_path+"android_release.apk";
  794. }
  795. FileAccess *src_f=NULL;
  796. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  797. ep.step("Creating APK",0);
  798. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  799. if (!pkg) {
  800. EditorNode::add_io_error("Could not find template APK to export:\n"+src_apk);
  801. return ERR_FILE_NOT_FOUND;
  802. }
  803. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  804. int ret = unzGoToFirstFile(pkg);
  805. zlib_filefunc_def io2=io;
  806. FileAccess *dst_f=NULL;
  807. io2.opaque=&dst_f;
  808. zipFile apk=zipOpen2(p_path.utf8().get_data(),APPEND_STATUS_CREATE,NULL,&io2);
  809. while(ret==UNZ_OK) {
  810. //get filename
  811. unz_file_info info;
  812. char fname[16384];
  813. ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
  814. String file=fname;
  815. Vector<uint8_t> data;
  816. data.resize(info.uncompressed_size);
  817. //read
  818. unzOpenCurrentFile(pkg);
  819. unzReadCurrentFile(pkg,data.ptr(),data.size());
  820. unzCloseCurrentFile(pkg);
  821. //write
  822. if (file=="AndroidManifest.xml") {
  823. _fix_manifest(data);
  824. }
  825. if (file=="resources.arsc") {
  826. _fix_resources(data);
  827. }
  828. if (file=="res/drawable/icon.png") {
  829. bool found=false;
  830. if (this->icon!="" && this->icon.ends_with(".png")) {
  831. FileAccess *f = FileAccess::open(this->icon,FileAccess::READ);
  832. if (f) {
  833. data.resize(f->get_len());
  834. f->get_buffer(data.ptr(),data.size());
  835. memdelete(f);
  836. found=true;
  837. }
  838. }
  839. if (!found) {
  840. String appicon = Globals::get_singleton()->get("application/icon");
  841. if (appicon!="" && appicon.ends_with(".png")) {
  842. FileAccess*f = FileAccess::open(appicon,FileAccess::READ);
  843. if (f) {
  844. data.resize(f->get_len());
  845. f->get_buffer(data.ptr(),data.size());
  846. memdelete(f);
  847. }
  848. }
  849. }
  850. }
  851. print_line("ADDING: "+file);
  852. zipOpenNewFileInZip(apk,
  853. file.utf8().get_data(),
  854. NULL,
  855. NULL,
  856. 0,
  857. NULL,
  858. 0,
  859. NULL,
  860. Z_DEFLATED,
  861. Z_DEFAULT_COMPRESSION);
  862. zipWriteInFileInZip(apk,data.ptr(),data.size());
  863. zipCloseFileInZip(apk);
  864. ret = unzGoToNextFile(pkg);
  865. }
  866. ep.step("Adding Files..",1);
  867. Error err=OK;
  868. Vector<String> cl = cmdline.strip_edges().split(" ");
  869. for(int i=0;i<cl.size();i++) {
  870. if (cl[i].strip_edges().length()==0) {
  871. cl.remove(i);
  872. i--;
  873. }
  874. }
  875. if (p_dumb) {
  876. String host = EditorSettings::get_singleton()->get("file_server/host");
  877. int port = EditorSettings::get_singleton()->get("file_server/post");
  878. String passwd = EditorSettings::get_singleton()->get("file_server/password");
  879. cl.push_back("-rfs");
  880. cl.push_back(host+":"+itos(port));
  881. if (passwd!="") {
  882. cl.push_back("-rfs_pass");
  883. cl.push_back(passwd);
  884. }
  885. } else {
  886. //all files
  887. if (apk_expansion) {
  888. String apkfname="main."+itos(version_code)+"."+package+".obb";
  889. String fullpath=p_path.get_base_dir().plus_file(apkfname);
  890. FileAccess *pf = FileAccess::open(fullpath,FileAccess::WRITE);
  891. if (!pf) {
  892. EditorNode::add_io_error("Could not write expansion package file: "+apkfname);
  893. return OK;
  894. }
  895. err = save_pack(pf);
  896. memdelete(pf);
  897. cl.push_back("-use_apk_expansion");
  898. cl.push_back("-apk_expansion_md5");
  899. cl.push_back(FileAccess::get_md5(fullpath));
  900. cl.push_back("-apk_expansion_key");
  901. cl.push_back(apk_expansion_pkey.strip_edges());
  902. } else {
  903. APKExportData ed;
  904. ed.ep=&ep;
  905. ed.apk=apk;
  906. err = export_project_files(save_apk_file,&ed,false);
  907. }
  908. }
  909. if (use_32_fb)
  910. cl.push_back("-use_depth_32");
  911. if (cl.size()) {
  912. //add comandline
  913. Vector<uint8_t> clf;
  914. clf.resize(4);
  915. encode_uint32(cl.size(),&clf[0]);
  916. for(int i=0;i<cl.size();i++) {
  917. CharString txt = cl[i].utf8();
  918. int base = clf.size();
  919. clf.resize(base+4+txt.length());
  920. encode_uint32(txt.length(),&clf[base]);
  921. copymem(&clf[base+4],txt.ptr(),txt.length());
  922. print_line(itos(i)+" param: "+cl[i]);
  923. }
  924. zipOpenNewFileInZip(apk,
  925. "assets/_cl_",
  926. NULL,
  927. NULL,
  928. 0,
  929. NULL,
  930. 0,
  931. NULL,
  932. Z_DEFLATED,
  933. Z_DEFAULT_COMPRESSION);
  934. zipWriteInFileInZip(apk,clf.ptr(),clf.size());
  935. zipCloseFileInZip(apk);
  936. }
  937. zipClose(apk,NULL);
  938. unzClose(pkg);
  939. if (err) {
  940. return err;
  941. }
  942. if (_signed) {
  943. String jarsigner=EditorSettings::get_singleton()->get("android/jarsigner");
  944. if (!FileAccess::exists(jarsigner)) {
  945. EditorNode::add_io_error("'jarsigner' could not be found.\nPlease supply a path in the editor settings.\nResulting apk is unsigned.");
  946. return OK;
  947. }
  948. String keystore;
  949. String password;
  950. String user;
  951. if (p_debug) {
  952. keystore=EditorSettings::get_singleton()->get("android/debug_keystore");
  953. password=EditorSettings::get_singleton()->get("android/debug_keystore_pass");
  954. user=EditorSettings::get_singleton()->get("android/debug_keystore_user");
  955. ep.step("Signing Debug APK..",103);
  956. } else {
  957. keystore=release_keystore;
  958. password=release_password;
  959. user=release_username;
  960. ep.step("Signing Release APK..",103);
  961. }
  962. if (!FileAccess::exists(keystore)) {
  963. EditorNode::add_io_error("Could not find keystore, unable to export.");
  964. return ERR_FILE_CANT_OPEN;
  965. }
  966. List<String> args;
  967. args.push_back("-digestalg");
  968. args.push_back("SHA1");
  969. args.push_back("-sigalg");
  970. args.push_back("MD5withRSA");
  971. String tsa_url=EditorSettings::get_singleton()->get("android/timestamping_authority_url");
  972. if (tsa_url != "") {
  973. args.push_back("-tsa");
  974. args.push_back(tsa_url);
  975. }
  976. args.push_back("-verbose");
  977. args.push_back("-keystore");
  978. args.push_back(keystore);
  979. args.push_back("-storepass");
  980. args.push_back(password);
  981. args.push_back(p_path);
  982. args.push_back(user);
  983. int retval;
  984. int err = OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  985. if (retval) {
  986. EditorNode::add_io_error("'jarsigner' returned with error #"+itos(retval));
  987. return ERR_CANT_CREATE;
  988. }
  989. ep.step("Verifying APK..",104);
  990. args.clear();
  991. args.push_back("-verify");
  992. args.push_back(p_path);
  993. args.push_back("-verbose");
  994. err = OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  995. if (retval) {
  996. EditorNode::add_io_error("'jarsigner' verificaiton of APK failed. Make sure to use jarsigner from Java 6.");
  997. return ERR_CANT_CREATE;
  998. }
  999. }
  1000. return OK;
  1001. }
  1002. bool EditorExportPlatformAndroid::poll_devices() {
  1003. bool dc=devices_changed;
  1004. devices_changed=false;
  1005. return dc;
  1006. }
  1007. int EditorExportPlatformAndroid::get_device_count() const {
  1008. device_lock->lock();
  1009. int dc=devices.size();
  1010. device_lock->unlock();
  1011. return dc;
  1012. }
  1013. String EditorExportPlatformAndroid::get_device_name(int p_device) const {
  1014. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  1015. device_lock->lock();
  1016. String s=devices[p_device].name;
  1017. device_lock->unlock();
  1018. return s;
  1019. }
  1020. String EditorExportPlatformAndroid::get_device_info(int p_device) const {
  1021. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  1022. device_lock->lock();
  1023. String s=devices[p_device].description;
  1024. device_lock->unlock();
  1025. return s;
  1026. }
  1027. void EditorExportPlatformAndroid::_device_poll_thread(void *ud) {
  1028. EditorExportPlatformAndroid *ea=(EditorExportPlatformAndroid *)ud;
  1029. while(!ea->quit_request) {
  1030. String adb=EditorSettings::get_singleton()->get("android/adb");
  1031. if (!FileAccess::exists(adb)) {
  1032. OS::get_singleton()->delay_usec(3000000);
  1033. continue; //adb not configured
  1034. }
  1035. String devices;
  1036. List<String> args;
  1037. args.push_back("devices");
  1038. int ec;
  1039. Error err = OS::get_singleton()->execute(adb,args,true,NULL,&devices,&ec);
  1040. Vector<String> ds = devices.split("\n");
  1041. Vector<String> ldevices;
  1042. for(int i=1;i<ds.size();i++) {
  1043. String d = ds[i];
  1044. int dpos = d.find("device");
  1045. if (dpos==-1)
  1046. continue;
  1047. d=d.substr(0,dpos).strip_edges();
  1048. // print_line("found devuce: "+d);
  1049. ldevices.push_back(d);
  1050. }
  1051. ea->device_lock->lock();
  1052. bool different=false;
  1053. if (devices.size()!=ldevices.size()) {
  1054. different=true;
  1055. } else {
  1056. for(int i=0;i<ea->devices.size();i++) {
  1057. if (ea->devices[i].id!=ldevices[i]) {
  1058. different=true;
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. if (different) {
  1064. Vector<Device> ndevices;
  1065. for(int i=0;i<ldevices.size();i++) {
  1066. Device d;
  1067. d.id=ldevices[i];
  1068. for(int j=0;j<ea->devices.size();j++) {
  1069. if (ea->devices[j].id==ldevices[i]) {
  1070. d.description=ea->devices[j].description;
  1071. d.name=ea->devices[j].name;
  1072. }
  1073. }
  1074. if (d.description=="") {
  1075. //in the oven, request!
  1076. args.clear();
  1077. args.push_back("-s");
  1078. args.push_back(d.id);
  1079. args.push_back("shell");
  1080. args.push_back("cat");
  1081. args.push_back("/system/build.prop");
  1082. int ec;
  1083. String dp;
  1084. Error err = OS::get_singleton()->execute(adb,args,true,NULL,&dp,&ec);
  1085. print_line("RV: "+itos(ec));
  1086. Vector<String> props = dp.split("\n");
  1087. String vendor;
  1088. String device;
  1089. d.description+"Device ID: "+d.id+"\n";
  1090. for(int j=0;j<props.size();j++) {
  1091. String p = props[j];
  1092. if (p.begins_with("ro.product.model=")) {
  1093. device=p.get_slice("=",1).strip_edges();
  1094. } else if (p.begins_with("ro.product.brand=")) {
  1095. vendor=p.get_slice("=",1).strip_edges().capitalize();
  1096. } else if (p.begins_with("ro.build.display.id=")) {
  1097. d.description+="Build: "+p.get_slice("=",1).strip_edges()+"\n";
  1098. } else if (p.begins_with("ro.build.version.release=")) {
  1099. d.description+="Release: "+p.get_slice("=",1).strip_edges()+"\n";
  1100. } else if (p.begins_with("ro.product.cpu.abi=")) {
  1101. d.description+="CPU: "+p.get_slice("=",1).strip_edges()+"\n";
  1102. } else if (p.begins_with("ro.product.manufacturer=")) {
  1103. d.description+="Manufacturer: "+p.get_slice("=",1).strip_edges()+"\n";
  1104. } else if (p.begins_with("ro.board.platform=")) {
  1105. d.description+="Chipset: "+p.get_slice("=",1).strip_edges()+"\n";
  1106. } else if (p.begins_with("ro.opengles.version=")) {
  1107. uint32_t opengl = p.get_slice("=",1).to_int();
  1108. d.description+="OpenGL: "+itos(opengl>>16)+"."+itos((opengl>>8)&0xFF)+"."+itos((opengl)&0xFF)+"\n";
  1109. }
  1110. }
  1111. d.name=vendor+" "+device;
  1112. // print_line("name: "+d.name);
  1113. // print_line("description: "+d.description);
  1114. }
  1115. ndevices.push_back(d);
  1116. }
  1117. ea->devices=ndevices;
  1118. ea->devices_changed=true;
  1119. }
  1120. ea->device_lock->unlock();
  1121. OS::get_singleton()->delay_usec(3000000);
  1122. }
  1123. }
  1124. Error EditorExportPlatformAndroid::run(int p_device, bool p_dumb) {
  1125. ERR_FAIL_INDEX_V(p_device,devices.size(),ERR_INVALID_PARAMETER);
  1126. device_lock->lock();
  1127. EditorProgress ep("run","Running on "+devices[p_device].name,3);
  1128. String adb=EditorSettings::get_singleton()->get("android/adb");
  1129. if (adb=="") {
  1130. EditorNode::add_io_error("ADB executable not configured in settings, can't run.");
  1131. device_lock->unlock();
  1132. return ERR_UNCONFIGURED;
  1133. }
  1134. //export_temp
  1135. ep.step("Exporting APK",0);
  1136. String export_to=EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmpexport.apk";
  1137. Error err = export_project(export_to,true,p_dumb);
  1138. if (err) {
  1139. device_lock->unlock();
  1140. return err;
  1141. }
  1142. List<String> args;
  1143. int rv;
  1144. if (remove_prev) {
  1145. ep.step("Uninstalling..",1);
  1146. print_line("Uninstalling previous version: "+devices[p_device].name);
  1147. args.push_back("-s");
  1148. args.push_back(devices[p_device].id);
  1149. args.push_back("uninstall");
  1150. args.push_back(package);
  1151. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1152. #if 0
  1153. if (err || rv!=0) {
  1154. EditorNode::add_io_error("Could not install to device.");
  1155. device_lock->unlock();
  1156. return ERR_CANT_CREATE;
  1157. }
  1158. #endif
  1159. }
  1160. print_line("Installing into device (please wait..): "+devices[p_device].name);
  1161. ep.step("Installing to Device (please wait..)..",2);
  1162. args.clear();
  1163. args.push_back("-s");
  1164. args.push_back(devices[p_device].id);
  1165. args.push_back("install");
  1166. args.push_back(export_to);
  1167. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1168. if (err || rv!=0) {
  1169. EditorNode::add_io_error("Could not install to device.");
  1170. device_lock->unlock();
  1171. return ERR_CANT_CREATE;
  1172. }
  1173. ep.step("Running on Device..",3);
  1174. args.clear();
  1175. args.push_back("-s");
  1176. args.push_back(devices[p_device].id);
  1177. args.push_back("shell");
  1178. args.push_back("am");
  1179. args.push_back("start");
  1180. args.push_back("-a");
  1181. args.push_back("android.intent.action.MAIN");
  1182. args.push_back("-n");
  1183. args.push_back(package+"/com.android.godot.Godot");
  1184. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1185. if (err || rv!=0) {
  1186. EditorNode::add_io_error("Could not execute ondevice.");
  1187. device_lock->unlock();
  1188. return ERR_CANT_CREATE;
  1189. }
  1190. device_lock->unlock();
  1191. return OK;
  1192. }
  1193. EditorExportPlatformAndroid::EditorExportPlatformAndroid() {
  1194. version_code=1;
  1195. version_name="1.0";
  1196. package="com.android.noname";
  1197. name="";
  1198. _signed=true;
  1199. apk_expansion=false;
  1200. device_lock = Mutex::create();
  1201. quit_request=false;
  1202. orientation=0;
  1203. remove_prev=true;
  1204. use_32_fb=false;
  1205. device_thread=Thread::create(_device_poll_thread,this);
  1206. devices_changed=true;
  1207. Image img( _android_logo );
  1208. logo = Ref<ImageTexture>( memnew( ImageTexture ));
  1209. logo->create_from_image(img);
  1210. for(int i=0;i<4;i++)
  1211. screen_support[i]=true;
  1212. }
  1213. bool EditorExportPlatformAndroid::can_export(String *r_error) const {
  1214. bool valid=true;
  1215. String adb=EditorSettings::get_singleton()->get("android/adb");
  1216. String err;
  1217. if (!FileAccess::exists(adb)) {
  1218. valid=false;
  1219. err+="ADB executable not configured in editor settings.\n";
  1220. }
  1221. String js = EditorSettings::get_singleton()->get("android/jarsigner");
  1222. if (!FileAccess::exists(js)) {
  1223. valid=false;
  1224. err+="OpenJDK 6 jarsigner not configured in editor settings.\n";
  1225. }
  1226. String dk = EditorSettings::get_singleton()->get("android/debug_keystore");
  1227. if (!FileAccess::exists(dk)) {
  1228. valid=false;
  1229. err+="Debug Keystore not configured in editor settings.\n";
  1230. }
  1231. String exe_path = EditorSettings::get_singleton()->get_settings_path()+"/templates/";
  1232. if (!FileAccess::exists(exe_path+"android_debug.apk") || !FileAccess::exists(exe_path+"android_release.apk")) {
  1233. valid=false;
  1234. err+="No export templates found.\nDownload and install export templates.\n";
  1235. }
  1236. if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
  1237. valid=false;
  1238. err+="Custom debug package not found.\n";
  1239. }
  1240. if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
  1241. valid=false;
  1242. err+="Custom release package not found.\n";
  1243. }
  1244. if (apk_expansion) {
  1245. //if (apk_expansion_salt=="") {
  1246. // valid=false;
  1247. // err+="Invalid SALT for apk expansion.\n";
  1248. //}
  1249. if (apk_expansion_pkey=="") {
  1250. valid=false;
  1251. err+="Invalid public key for apk expansion.\n";
  1252. }
  1253. }
  1254. if (r_error)
  1255. *r_error=err;
  1256. return valid;
  1257. }
  1258. EditorExportPlatformAndroid::~EditorExportPlatformAndroid() {
  1259. quit_request=true;
  1260. Thread::wait_to_finish(device_thread);
  1261. }
  1262. void register_android_exporter() {
  1263. String exe_ext=OS::get_singleton()->get_name()=="Windows"?"exe":"";
  1264. EDITOR_DEF("android/adb","");
  1265. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/adb",PROPERTY_HINT_GLOBAL_FILE,exe_ext));
  1266. EDITOR_DEF("android/jarsigner","");
  1267. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/jarsigner",PROPERTY_HINT_GLOBAL_FILE,exe_ext));
  1268. EDITOR_DEF("android/debug_keystore","");
  1269. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/debug_keystore",PROPERTY_HINT_GLOBAL_FILE,"keystore"));
  1270. EDITOR_DEF("android/debug_keystore_user","androiddebugkey");
  1271. EDITOR_DEF("android/debug_keystore_pass","android");
  1272. //EDITOR_DEF("android/release_keystore","");
  1273. //EDITOR_DEF("android/release_username","");
  1274. //EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"android/release_keystore",PROPERTY_HINT_GLOBAL_FILE,"*.keystore"));
  1275. EDITOR_DEF("android/timestamping_authority_url","");
  1276. Ref<EditorExportPlatformAndroid> exporter = Ref<EditorExportPlatformAndroid>( memnew(EditorExportPlatformAndroid) );
  1277. EditorImportExport::get_singleton()->add_export_platform(exporter);
  1278. }