export.cpp 40 KB

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