export.cpp 46 KB

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