export.cpp 43 KB

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