export_plugin.cpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  1. /**************************************************************************/
  2. /* export_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "export_plugin.h"
  31. #include "logo_svg.gen.h"
  32. #include "run_icon_svg.gen.h"
  33. #include "core/io/json.h"
  34. #include "core/io/plist.h"
  35. #include "core/string/translation.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_paths.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/export/editor_export.h"
  40. #include "editor/import/resource_importer_texture_settings.h"
  41. #include "editor/plugins/script_editor_plugin.h"
  42. #include "editor/themes/editor_scale.h"
  43. #include "modules/modules_enabled.gen.h" // For mono and svg.
  44. #ifdef MODULE_SVG_ENABLED
  45. #include "modules/svg/image_loader_svg.h"
  46. #endif
  47. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  48. // Vulkan and OpenGL ES 3.0 both mandate ETC2 support.
  49. r_features->push_back("etc2");
  50. r_features->push_back("astc");
  51. Vector<String> architectures = _get_preset_architectures(p_preset);
  52. for (int i = 0; i < architectures.size(); ++i) {
  53. r_features->push_back(architectures[i]);
  54. }
  55. }
  56. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() const {
  57. Vector<ExportArchitecture> archs;
  58. archs.push_back(ExportArchitecture("arm64", true));
  59. return archs;
  60. }
  61. struct IconInfo {
  62. const char *preset_key;
  63. const char *idiom;
  64. const char *export_name;
  65. const char *actual_size_side;
  66. const char *scale;
  67. const char *unscaled_size;
  68. const bool force_opaque;
  69. };
  70. static const IconInfo icon_infos[] = {
  71. // Home screen on iPhone
  72. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "2x", "60x60", false },
  73. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "3x", "40x40", false },
  74. { PNAME("icons/iphone_180x180"), "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  75. // Home screen on iPad
  76. { PNAME("icons/ipad_76x76"), "ipad", "Icon-76.png", "76", "1x", "76x76", false },
  77. { PNAME("icons/ipad_152x152"), "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  78. { PNAME("icons/ipad_167x167"), "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  79. // App Store
  80. { PNAME("icons/app_store_1024x1024"), "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", true },
  81. // Spotlight
  82. { PNAME("icons/spotlight_40x40"), "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  83. { PNAME("icons/spotlight_80x80"), "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  84. { PNAME("icons/spotlight_80x80"), "ipad", "Icon-80.png", "80", "2x", "40x40", false },
  85. // Settings
  86. { PNAME("icons/settings_58x58"), "iphone", "Icon-58.png", "58", "2x", "29x29", false },
  87. { PNAME("icons/settings_58x58"), "ipad", "Icon-58.png", "58", "2x", "29x29", false },
  88. { PNAME("icons/settings_87x87"), "iphone", "Icon-87.png", "87", "3x", "29x29", false },
  89. // Notification
  90. { PNAME("icons/notification_40x40"), "iphone", "Icon-40.png", "40", "2x", "20x20", false },
  91. { PNAME("icons/notification_40x40"), "ipad", "Icon-40.png", "40", "2x", "20x20", false },
  92. { PNAME("icons/notification_60x60"), "iphone", "Icon-60.png", "60", "3x", "20x20", false }
  93. };
  94. String EditorExportPlatformIOS::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
  95. if (p_preset) {
  96. if (p_name == "application/app_store_team_id") {
  97. String team_id = p_preset->get("application/app_store_team_id");
  98. if (team_id.is_empty()) {
  99. return TTR("App Store Team ID not specified.") + "\n";
  100. }
  101. } else if (p_name == "application/bundle_identifier") {
  102. String identifier = p_preset->get("application/bundle_identifier");
  103. String pn_err;
  104. if (!is_package_name_valid(identifier, &pn_err)) {
  105. return TTR("Invalid Identifier:") + " " + pn_err;
  106. }
  107. }
  108. }
  109. return String();
  110. }
  111. void EditorExportPlatformIOS::_notification(int p_what) {
  112. #ifdef MACOS_ENABLED
  113. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  114. if (EditorExport::get_singleton()) {
  115. EditorExport::get_singleton()->connect_presets_runnable_updated(callable_mp(this, &EditorExportPlatformIOS::_update_preset_status));
  116. }
  117. }
  118. #endif
  119. }
  120. bool EditorExportPlatformIOS::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
  121. return true;
  122. }
  123. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  126. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  127. for (int i = 0; i < architectures.size(); ++i) {
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), architectures[i].name)), architectures[i].is_default));
  129. }
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), "", false, true));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  132. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), ""));
  133. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  134. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), "", false, true));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  141. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  142. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), "12.0"));
  143. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/additional_plist_content", PROPERTY_HINT_MULTILINE_TEXT), ""));
  144. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));
  145. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/export_project_only"), false));
  146. Vector<PluginConfigIOS> found_plugins = get_plugins();
  147. for (int i = 0; i < found_plugins.size(); i++) {
  148. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), found_plugins[i].name)), false));
  149. }
  150. HashSet<String> plist_keys;
  151. for (int i = 0; i < found_plugins.size(); i++) {
  152. // Editable plugin plist values
  153. PluginConfigIOS plugin = found_plugins[i];
  154. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  155. switch (E.value.type) {
  156. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  157. String preset_name = "plugins_plist/" + E.key;
  158. if (!plist_keys.has(preset_name)) {
  159. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, preset_name), E.value.value));
  160. plist_keys.insert(preset_name);
  161. }
  162. } break;
  163. default:
  164. continue;
  165. }
  166. }
  167. }
  168. plugins_changed.clear();
  169. plugins = found_plugins;
  170. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  171. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  172. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_gaming_tier"), false));
  173. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_a12"), false));
  174. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  175. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  176. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/camera_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the camera"), ""));
  177. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/camera_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  178. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/microphone_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the microphone"), ""));
  179. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/microphone_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  180. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/photolibrary_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need access to the photo library"), ""));
  181. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/photolibrary_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  182. HashSet<String> used_names;
  183. for (uint64_t i = 0; i < sizeof(icon_infos) / sizeof(icon_infos[0]); ++i) {
  184. if (!used_names.has(icon_infos[i].preset_key)) {
  185. used_names.insert(icon_infos[i].preset_key);
  186. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, icon_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  187. }
  188. }
  189. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "storyboard/image_scale_mode", PROPERTY_HINT_ENUM, "Same as Logo,Center,Scale to Fit,Scale to Fill,Scale"), 0));
  190. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  191. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  192. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  193. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  194. }
  195. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  196. static const String export_method_string[] = {
  197. "app-store",
  198. "development",
  199. "ad-hoc",
  200. "enterprise"
  201. };
  202. static const String storyboard_image_scale_mode[] = {
  203. "center",
  204. "scaleAspectFit",
  205. "scaleAspectFill",
  206. "scaleToFill"
  207. };
  208. String dbg_sign_id = p_preset->get("application/code_sign_identity_debug").operator String().is_empty() ? "iPhone Developer" : p_preset->get("application/code_sign_identity_debug");
  209. String rel_sign_id = p_preset->get("application/code_sign_identity_release").operator String().is_empty() ? "iPhone Distribution" : p_preset->get("application/code_sign_identity_release");
  210. bool dbg_manual = !p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG).operator String().is_empty() || (dbg_sign_id != "iPhone Developer" && dbg_sign_id != "iPhone Distribution");
  211. bool rel_manual = !p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE).operator String().is_empty() || (rel_sign_id != "iPhone Developer" && rel_sign_id != "iPhone Distribution");
  212. String str;
  213. String strnew;
  214. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  215. Vector<String> lines = str.split("\n");
  216. for (int i = 0; i < lines.size(); i++) {
  217. if (lines[i].find("$binary") != -1) {
  218. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  219. } else if (lines[i].find("$modules_buildfile") != -1) {
  220. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  221. } else if (lines[i].find("$modules_fileref") != -1) {
  222. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  223. } else if (lines[i].find("$modules_buildphase") != -1) {
  224. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  225. } else if (lines[i].find("$modules_buildgrp") != -1) {
  226. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  227. } else if (lines[i].find("$name") != -1) {
  228. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  229. } else if (lines[i].find("$bundle_identifier") != -1) {
  230. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  231. } else if (lines[i].find("$short_version") != -1) {
  232. strnew += lines[i].replace("$short_version", p_preset->get_version("application/short_version")) + "\n";
  233. } else if (lines[i].find("$version") != -1) {
  234. strnew += lines[i].replace("$version", p_preset->get_version("application/version")) + "\n";
  235. } else if (lines[i].find("$min_version") != -1) {
  236. strnew += lines[i].replace("$min_version", p_preset->get("application/min_ios_version")) + "\n";
  237. } else if (lines[i].find("$signature") != -1) {
  238. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  239. } else if (lines[i].find("$team_id") != -1) {
  240. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  241. } else if (lines[i].find("$default_build_config") != -1) {
  242. strnew += lines[i].replace("$default_build_config", p_debug ? "Debug" : "Release") + "\n";
  243. } else if (lines[i].find("$export_method") != -1) {
  244. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  245. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  246. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  247. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE)) + "\n";
  248. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  249. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG)) + "\n";
  250. } else if (lines[i].find("$code_sign_style_debug") != -1) {
  251. if (dbg_manual) {
  252. strnew += lines[i].replace("$code_sign_style_debug", "Manual") + "\n";
  253. } else {
  254. strnew += lines[i].replace("$code_sign_style_debug", "Automatic") + "\n";
  255. }
  256. } else if (lines[i].find("$code_sign_style_release") != -1) {
  257. if (rel_manual) {
  258. strnew += lines[i].replace("$code_sign_style_release", "Manual") + "\n";
  259. } else {
  260. strnew += lines[i].replace("$code_sign_style_release", "Automatic") + "\n";
  261. }
  262. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  263. String uuid = p_debug ? p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG) : p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE);
  264. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  265. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  266. strnew += lines[i].replace("$code_sign_identity_debug", dbg_sign_id) + "\n";
  267. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  268. strnew += lines[i].replace("$code_sign_identity_release", rel_sign_id) + "\n";
  269. } else if (lines[i].find("$additional_plist_content") != -1) {
  270. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  271. } else if (lines[i].find("$godot_archs") != -1) {
  272. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  273. } else if (lines[i].find("$linker_flags") != -1) {
  274. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  275. } else if (lines[i].find("$targeted_device_family") != -1) {
  276. String xcode_value;
  277. switch ((int)p_preset->get("application/targeted_device_family")) {
  278. case 0: // iPhone
  279. xcode_value = "1";
  280. break;
  281. case 1: // iPad
  282. xcode_value = "2";
  283. break;
  284. case 2: // iPhone & iPad
  285. xcode_value = "1,2";
  286. break;
  287. }
  288. strnew += lines[i].replace("$targeted_device_family", xcode_value) + "\n";
  289. } else if (lines[i].find("$cpp_code") != -1) {
  290. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  291. } else if (lines[i].find("$docs_in_place") != -1) {
  292. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  293. } else if (lines[i].find("$docs_sharing") != -1) {
  294. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  295. } else if (lines[i].find("$entitlements_push_notifications") != -1) {
  296. bool is_on = p_preset->get("capabilities/push_notifications");
  297. strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n";
  298. } else if (lines[i].find("$required_device_capabilities") != -1) {
  299. String capabilities;
  300. // I've removed armv7 as we can run on 64bit only devices
  301. // Note that capabilities listed here are requirements for the app to be installed.
  302. // They don't enable anything.
  303. Vector<String> capabilities_list = p_config.capabilities;
  304. if ((bool)p_preset->get("capabilities/access_wifi") && !capabilities_list.has("wifi")) {
  305. capabilities_list.push_back("wifi");
  306. }
  307. if ((bool)p_preset->get("capabilities/performance_gaming_tier") && !capabilities_list.has("iphone-performance-gaming-tier")) {
  308. capabilities_list.push_back("iphone-performance-gaming-tier");
  309. }
  310. if ((bool)p_preset->get("capabilities/performance_a12") && !capabilities_list.has("iphone-ipad-minimum-performance-a12")) {
  311. capabilities_list.push_back("iphone-ipad-minimum-performance-a12");
  312. }
  313. for (int idx = 0; idx < capabilities_list.size(); idx++) {
  314. capabilities += "<string>" + capabilities_list[idx] + "</string>\n";
  315. }
  316. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  317. } else if (lines[i].find("$interface_orientations") != -1) {
  318. String orientations;
  319. const DisplayServer::ScreenOrientation screen_orientation =
  320. DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")));
  321. switch (screen_orientation) {
  322. case DisplayServer::SCREEN_LANDSCAPE:
  323. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  324. break;
  325. case DisplayServer::SCREEN_PORTRAIT:
  326. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  327. break;
  328. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  329. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  330. break;
  331. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  332. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  333. break;
  334. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  335. // Allow both landscape orientations depending on sensor direction.
  336. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  337. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  338. break;
  339. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  340. // Allow both portrait orientations depending on sensor direction.
  341. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  342. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  343. break;
  344. case DisplayServer::SCREEN_SENSOR:
  345. // Allow all screen orientations depending on sensor direction.
  346. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  347. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  348. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  349. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  350. break;
  351. }
  352. strnew += lines[i].replace("$interface_orientations", orientations);
  353. } else if (lines[i].find("$camera_usage_description") != -1) {
  354. String description = p_preset->get("privacy/camera_usage_description");
  355. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  356. } else if (lines[i].find("$microphone_usage_description") != -1) {
  357. String description = p_preset->get("privacy/microphone_usage_description");
  358. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  359. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  360. String description = p_preset->get("privacy/photolibrary_usage_description");
  361. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  362. } else if (lines[i].find("$plist_launch_screen_name") != -1) {
  363. String value = "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>";
  364. strnew += lines[i].replace("$plist_launch_screen_name", value) + "\n";
  365. } else if (lines[i].find("$pbx_launch_screen_file_reference") != -1) {
  366. String value = "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };";
  367. strnew += lines[i].replace("$pbx_launch_screen_file_reference", value) + "\n";
  368. } else if (lines[i].find("$pbx_launch_screen_copy_files") != -1) {
  369. String value = "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */,";
  370. strnew += lines[i].replace("$pbx_launch_screen_copy_files", value) + "\n";
  371. } else if (lines[i].find("$pbx_launch_screen_build_phase") != -1) {
  372. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */,";
  373. strnew += lines[i].replace("$pbx_launch_screen_build_phase", value) + "\n";
  374. } else if (lines[i].find("$pbx_launch_screen_build_reference") != -1) {
  375. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };";
  376. strnew += lines[i].replace("$pbx_launch_screen_build_reference", value) + "\n";
  377. #ifndef DISABLE_DEPRECATED
  378. } else if (lines[i].find("$pbx_launch_image_usage_setting") != -1) {
  379. strnew += lines[i].replace("$pbx_launch_image_usage_setting", "") + "\n";
  380. #endif
  381. } else if (lines[i].find("$launch_screen_image_mode") != -1) {
  382. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  383. String value;
  384. switch (image_scale_mode) {
  385. case 0: {
  386. String logo_path = GLOBAL_GET("application/boot_splash/image");
  387. bool is_on = GLOBAL_GET("application/boot_splash/fullsize");
  388. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  389. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  390. } break;
  391. default: {
  392. value = storyboard_image_scale_mode[image_scale_mode - 1];
  393. }
  394. }
  395. strnew += lines[i].replace("$launch_screen_image_mode", value) + "\n";
  396. } else if (lines[i].find("$launch_screen_background_color") != -1) {
  397. bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
  398. Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : GLOBAL_GET("application/boot_splash/bg_color");
  399. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  400. Dictionary value_dictionary;
  401. value_dictionary["red"] = color.r;
  402. value_dictionary["green"] = color.g;
  403. value_dictionary["blue"] = color.b;
  404. value_dictionary["alpha"] = color.a;
  405. String value = value_format.format(value_dictionary, "$_");
  406. strnew += lines[i].replace("$launch_screen_background_color", value) + "\n";
  407. } else if (lines[i].find("$pbx_locale_file_reference") != -1) {
  408. String locale_files;
  409. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  410. if (translations.size() > 0) {
  411. HashSet<String> languages;
  412. for (const String &E : translations) {
  413. Ref<Translation> tr = ResourceLoader::load(E);
  414. if (tr.is_valid() && tr->get_locale() != "en") {
  415. languages.insert(tr->get_locale());
  416. }
  417. }
  418. int index = 0;
  419. for (const String &lang : languages) {
  420. locale_files += "D0BCFE4518AEBDA2004A" + itos(index).pad_zeros(4) + " /* " + lang + " */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = " + lang + "; path = " + lang + ".lproj/InfoPlist.strings; sourceTree = \"<group>\"; };\n";
  421. index++;
  422. }
  423. }
  424. strnew += lines[i].replace("$pbx_locale_file_reference", locale_files);
  425. } else if (lines[i].find("$pbx_locale_build_reference") != -1) {
  426. String locale_files;
  427. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  428. if (translations.size() > 0) {
  429. HashSet<String> languages;
  430. for (const String &E : translations) {
  431. Ref<Translation> tr = ResourceLoader::load(E);
  432. if (tr.is_valid() && tr->get_locale() != "en") {
  433. languages.insert(tr->get_locale());
  434. }
  435. }
  436. int index = 0;
  437. for (const String &lang : languages) {
  438. locale_files += "D0BCFE4518AEBDA2004A" + itos(index).pad_zeros(4) + " /* " + lang + " */,\n";
  439. index++;
  440. }
  441. }
  442. strnew += lines[i].replace("$pbx_locale_build_reference", locale_files);
  443. } else if (lines[i].find("$swift_runtime_migration") != -1) {
  444. String value = !p_config.use_swift_runtime ? "" : "LastSwiftMigration = 1250;";
  445. strnew += lines[i].replace("$swift_runtime_migration", value) + "\n";
  446. } else if (lines[i].find("$swift_runtime_build_settings") != -1) {
  447. String value = !p_config.use_swift_runtime ? "" : R"(
  448. CLANG_ENABLE_MODULES = YES;
  449. SWIFT_OBJC_BRIDGING_HEADER = "$binary/dummy.h";
  450. SWIFT_VERSION = 5.0;
  451. )";
  452. value = value.replace("$binary", p_config.binary_name);
  453. strnew += lines[i].replace("$swift_runtime_build_settings", value) + "\n";
  454. } else if (lines[i].find("$swift_runtime_fileref") != -1) {
  455. String value = !p_config.use_swift_runtime ? "" : R"(
  456. 90B4C2AA2680BC560039117A /* dummy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dummy.h"; sourceTree = "<group>"; };
  457. 90B4C2B52680C7E90039117A /* dummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "dummy.swift"; sourceTree = "<group>"; };
  458. )";
  459. strnew += lines[i].replace("$swift_runtime_fileref", value) + "\n";
  460. } else if (lines[i].find("$swift_runtime_binary_files") != -1) {
  461. String value = !p_config.use_swift_runtime ? "" : R"(
  462. 90B4C2AA2680BC560039117A /* dummy.h */,
  463. 90B4C2B52680C7E90039117A /* dummy.swift */,
  464. )";
  465. strnew += lines[i].replace("$swift_runtime_binary_files", value) + "\n";
  466. } else if (lines[i].find("$swift_runtime_buildfile") != -1) {
  467. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90B4C2B52680C7E90039117A /* dummy.swift */; };";
  468. strnew += lines[i].replace("$swift_runtime_buildfile", value) + "\n";
  469. } else if (lines[i].find("$swift_runtime_build_phase") != -1) {
  470. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift */,";
  471. strnew += lines[i].replace("$swift_runtime_build_phase", value) + "\n";
  472. } else {
  473. strnew += lines[i] + "\n";
  474. }
  475. }
  476. // !BAS! I'm assuming the 9 in the original code was a typo. I've added -1 or else it seems to also be adding our terminating zero...
  477. // should apply the same fix in our macOS export.
  478. CharString cs = strnew.utf8();
  479. pfile.resize(cs.size() - 1);
  480. for (int i = 0; i < cs.size() - 1; i++) {
  481. pfile.write[i] = cs[i];
  482. }
  483. }
  484. String EditorExportPlatformIOS::_get_additional_plist_content() {
  485. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  486. String result;
  487. for (int i = 0; i < export_plugins.size(); ++i) {
  488. result += export_plugins[i]->get_ios_plist_content();
  489. }
  490. return result;
  491. }
  492. String EditorExportPlatformIOS::_get_linker_flags() {
  493. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  494. String result;
  495. for (int i = 0; i < export_plugins.size(); ++i) {
  496. String flags = export_plugins[i]->get_ios_linker_flags();
  497. if (flags.length() == 0) {
  498. continue;
  499. }
  500. if (result.length() > 0) {
  501. result += ' ';
  502. }
  503. result += flags;
  504. }
  505. // the flags will be enclosed in quotes, so need to escape them
  506. return result.replace("\"", "\\\"");
  507. }
  508. String EditorExportPlatformIOS::_get_cpp_code() {
  509. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  510. String result;
  511. for (int i = 0; i < export_plugins.size(); ++i) {
  512. result += export_plugins[i]->get_ios_cpp_code();
  513. }
  514. return result;
  515. }
  516. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  517. ERR_FAIL_COND(p_dst.is_null());
  518. ERR_FAIL_COND(p_src.is_null());
  519. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  520. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  521. int x_pos = (p_dst->get_width() - sw) / 2;
  522. int y_pos = (p_dst->get_height() - sh) / 2;
  523. int xs = (x_pos >= 0) ? 0 : -x_pos;
  524. int ys = (y_pos >= 0) ? 0 : -y_pos;
  525. if (sw + x_pos > p_dst->get_width()) {
  526. sw = p_dst->get_width() - x_pos;
  527. }
  528. if (sh + y_pos > p_dst->get_height()) {
  529. sh = p_dst->get_height() - y_pos;
  530. }
  531. for (int y = ys; y < sh; y++) {
  532. for (int x = xs; x < sw; x++) {
  533. Color sc = p_rot ? p_src->get_pixel(p_src->get_width() - y - 1, x) : p_src->get_pixel(x, y);
  534. Color dc = p_dst->get_pixel(x_pos + x, y_pos + y);
  535. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  536. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  537. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  538. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  539. p_dst->set_pixel(x_pos + x, y_pos + y, dc);
  540. }
  541. }
  542. }
  543. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  544. String json_description = "{\"images\":[";
  545. String sizes;
  546. Ref<DirAccess> da = DirAccess::open(p_iconset_dir);
  547. if (da.is_null()) {
  548. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));
  549. return ERR_CANT_OPEN;
  550. }
  551. Color boot_bg_color = GLOBAL_GET("application/boot_splash/bg_color");
  552. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  553. IconInfo info = icon_infos[i];
  554. int side_size = String(info.actual_size_side).to_int();
  555. String icon_path = p_preset->get(info.preset_key);
  556. if (icon_path.length() == 0) {
  557. // Resize main app icon
  558. icon_path = GLOBAL_GET("application/config/icon");
  559. Ref<Image> img = memnew(Image);
  560. Error err = ImageLoader::load_image(icon_path, img);
  561. if (err != OK) {
  562. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  563. return ERR_UNCONFIGURED;
  564. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  565. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  566. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  567. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  568. new_img->fill(boot_bg_color);
  569. _blend_and_rotate(new_img, img, false);
  570. err = new_img->save_png(p_iconset_dir + info.export_name);
  571. } else {
  572. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  573. err = img->save_png(p_iconset_dir + info.export_name);
  574. }
  575. if (err) {
  576. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  577. return err;
  578. }
  579. } else {
  580. // Load custom icon and resize if required
  581. Ref<Image> img = memnew(Image);
  582. Error err = ImageLoader::load_image(icon_path, img);
  583. if (err != OK) {
  584. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  585. return ERR_UNCONFIGURED;
  586. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  587. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  588. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  589. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  590. new_img->fill(boot_bg_color);
  591. _blend_and_rotate(new_img, img, false);
  592. err = new_img->save_png(p_iconset_dir + info.export_name);
  593. } else if (img->get_width() != side_size || img->get_height() != side_size) {
  594. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s): '%s' has incorrect size %s and was automatically resized to %s.", info.preset_key, icon_path, img->get_size(), Vector2i(side_size, side_size)));
  595. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  596. err = img->save_png(p_iconset_dir + info.export_name);
  597. } else {
  598. err = da->copy(icon_path, p_iconset_dir + info.export_name);
  599. }
  600. if (err) {
  601. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  602. return err;
  603. }
  604. }
  605. sizes += String(info.actual_size_side) + "\n";
  606. if (i > 0) {
  607. json_description += ",";
  608. }
  609. json_description += String("{");
  610. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  611. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  612. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  613. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  614. json_description += String("}");
  615. }
  616. json_description += "]}";
  617. Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  618. if (json_file.is_null()) {
  619. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));
  620. return ERR_CANT_CREATE;
  621. }
  622. CharString json_utf8 = json_description.utf8();
  623. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  624. Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  625. if (sizes_file.is_null()) {
  626. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));
  627. return ERR_CANT_CREATE;
  628. }
  629. CharString sizes_utf8 = sizes.utf8();
  630. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  631. return OK;
  632. }
  633. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  634. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  635. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  636. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  637. Ref<Image> image;
  638. String image_path = p_dest_dir.path_join("[email protected]");
  639. image.instantiate();
  640. Error err = ImageLoader::load_image(custom_launch_image_2x, image);
  641. if (err) {
  642. image.unref();
  643. return err;
  644. }
  645. if (image->save_png(image_path) != OK) {
  646. return ERR_FILE_CANT_WRITE;
  647. }
  648. image.unref();
  649. image_path = p_dest_dir.path_join("[email protected]");
  650. image.instantiate();
  651. err = ImageLoader::load_image(custom_launch_image_3x, image);
  652. if (err) {
  653. image.unref();
  654. return err;
  655. }
  656. if (image->save_png(image_path) != OK) {
  657. return ERR_FILE_CANT_WRITE;
  658. }
  659. } else {
  660. Ref<Image> splash;
  661. const String splash_path = GLOBAL_GET("application/boot_splash/image");
  662. if (!splash_path.is_empty()) {
  663. splash.instantiate();
  664. const Error err = ImageLoader::load_image(splash_path, splash);
  665. if (err) {
  666. splash.unref();
  667. }
  668. }
  669. if (splash.is_null()) {
  670. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  671. }
  672. // Using same image for both @2x and @3x
  673. // because Godot's own boot logo uses single image for all resolutions.
  674. // Also not using @1x image, because devices using this image variant
  675. // are not supported by iOS 9, which is minimal target.
  676. const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
  677. const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");
  678. if (splash->save_png(splash_png_path_2x) != OK) {
  679. return ERR_FILE_CANT_WRITE;
  680. }
  681. if (splash->save_png(splash_png_path_3x) != OK) {
  682. return ERR_FILE_CANT_WRITE;
  683. }
  684. }
  685. return OK;
  686. }
  687. Error EditorExportPlatformIOS::_walk_dir_recursive(Ref<DirAccess> &p_da, FileHandler p_handler, void *p_userdata) {
  688. Vector<String> dirs;
  689. String current_dir = p_da->get_current_dir();
  690. p_da->list_dir_begin();
  691. String path = p_da->get_next();
  692. while (!path.is_empty()) {
  693. if (p_da->current_is_dir()) {
  694. if (path != "." && path != "..") {
  695. dirs.push_back(path);
  696. }
  697. } else {
  698. Error err = p_handler(current_dir.path_join(path), p_userdata);
  699. if (err) {
  700. p_da->list_dir_end();
  701. return err;
  702. }
  703. }
  704. path = p_da->get_next();
  705. }
  706. p_da->list_dir_end();
  707. for (int i = 0; i < dirs.size(); ++i) {
  708. p_da->change_dir(dirs[i]);
  709. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  710. p_da->change_dir("..");
  711. if (err) {
  712. return err;
  713. }
  714. }
  715. return OK;
  716. }
  717. struct CodesignData {
  718. const Ref<EditorExportPreset> &preset;
  719. bool debug = false;
  720. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  721. preset(p_preset),
  722. debug(p_debug) {
  723. }
  724. };
  725. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  726. if (p_file.ends_with(".dylib")) {
  727. CodesignData *data = static_cast<CodesignData *>(p_userdata);
  728. print_line(String("Signing ") + p_file);
  729. String sign_id;
  730. if (data->debug) {
  731. sign_id = data->preset->get("application/code_sign_identity_debug").operator String().is_empty() ? "iPhone Developer" : data->preset->get("application/code_sign_identity_debug");
  732. } else {
  733. sign_id = data->preset->get("application/code_sign_identity_release").operator String().is_empty() ? "iPhone Distribution" : data->preset->get("application/code_sign_identity_release");
  734. }
  735. List<String> codesign_args;
  736. codesign_args.push_back("-f");
  737. codesign_args.push_back("-s");
  738. codesign_args.push_back(sign_id);
  739. codesign_args.push_back(p_file);
  740. String str;
  741. Error err = OS::get_singleton()->execute("codesign", codesign_args, &str, nullptr, true);
  742. print_verbose("codesign (" + p_file + "):\n" + str);
  743. return err;
  744. }
  745. return OK;
  746. }
  747. struct PbxId {
  748. private:
  749. static char _hex_char(uint8_t four_bits) {
  750. if (four_bits < 10) {
  751. return ('0' + four_bits);
  752. }
  753. return 'A' + (four_bits - 10);
  754. }
  755. static String _hex_pad(uint32_t num) {
  756. Vector<char> ret;
  757. ret.resize(sizeof(num) * 2);
  758. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  759. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  760. ret.write[i] = _hex_char(four_bits);
  761. }
  762. return String::utf8(ret.ptr(), ret.size());
  763. }
  764. public:
  765. uint32_t high_bits;
  766. uint32_t mid_bits;
  767. uint32_t low_bits;
  768. String str() const {
  769. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  770. }
  771. PbxId &operator++() {
  772. low_bits++;
  773. if (!low_bits) {
  774. mid_bits++;
  775. if (!mid_bits) {
  776. high_bits++;
  777. }
  778. }
  779. return *this;
  780. }
  781. };
  782. struct ExportLibsData {
  783. Vector<String> lib_paths;
  784. String dest_dir;
  785. };
  786. void EditorExportPlatformIOS::_check_xcframework_content(const String &p_path, int &r_total_libs, int &r_static_libs, int &r_dylibs, int &r_frameworks) const {
  787. Ref<PList> plist;
  788. plist.instantiate();
  789. plist->load_file(p_path.path_join("Info.plist"));
  790. Ref<PListNode> root_node = plist->get_root();
  791. if (root_node.is_null()) {
  792. return;
  793. }
  794. Dictionary root = root_node->get_value();
  795. if (!root.has("AvailableLibraries")) {
  796. return;
  797. }
  798. Ref<PListNode> libs_node = root["AvailableLibraries"];
  799. if (libs_node.is_null()) {
  800. return;
  801. }
  802. Array libs = libs_node->get_value();
  803. r_total_libs = libs.size();
  804. for (int j = 0; j < libs.size(); j++) {
  805. Ref<PListNode> lib_node = libs[j];
  806. if (lib_node.is_null()) {
  807. return;
  808. }
  809. Dictionary lib = lib_node->get_value();
  810. if (lib.has("BinaryPath")) {
  811. Ref<PListNode> path_node = lib["BinaryPath"];
  812. if (path_node.is_valid()) {
  813. String path = path_node->get_value();
  814. if (path.ends_with(".a")) {
  815. r_static_libs++;
  816. }
  817. if (path.ends_with(".dylib")) {
  818. r_dylibs++;
  819. }
  820. if (path.ends_with(".framework")) {
  821. r_frameworks++;
  822. }
  823. }
  824. }
  825. }
  826. }
  827. Error EditorExportPlatformIOS::_convert_to_framework(const String &p_source, const String &p_destination, const String &p_id) const {
  828. print_line("Converting to .framework", p_source, " -> ", p_destination);
  829. Ref<DirAccess> da = DirAccess::create_for_path(p_source);
  830. if (da.is_null()) {
  831. return ERR_CANT_OPEN;
  832. }
  833. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  834. if (filesystem_da.is_null()) {
  835. return ERR_CANT_OPEN;
  836. }
  837. if (!filesystem_da->dir_exists(p_destination)) {
  838. Error make_dir_err = filesystem_da->make_dir_recursive(p_destination);
  839. if (make_dir_err) {
  840. return make_dir_err;
  841. }
  842. }
  843. String asset = p_source.ends_with("/") ? p_source.left(p_source.length() - 1) : p_source;
  844. if (asset.ends_with(".xcframework")) {
  845. Ref<PList> plist;
  846. plist.instantiate();
  847. plist->load_file(p_source.path_join("Info.plist"));
  848. Ref<PListNode> root_node = plist->get_root();
  849. if (root_node.is_null()) {
  850. return ERR_CANT_OPEN;
  851. }
  852. Dictionary root = root_node->get_value();
  853. if (!root.has("AvailableLibraries")) {
  854. return ERR_CANT_OPEN;
  855. }
  856. Ref<PListNode> libs_node = root["AvailableLibraries"];
  857. if (libs_node.is_null()) {
  858. return ERR_CANT_OPEN;
  859. }
  860. Array libs = libs_node->get_value();
  861. for (int j = 0; j < libs.size(); j++) {
  862. Ref<PListNode> lib_node = libs[j];
  863. if (lib_node.is_null()) {
  864. return ERR_CANT_OPEN;
  865. }
  866. Dictionary lib = lib_node->get_value();
  867. if (lib.has("BinaryPath") && lib.has("LibraryPath") && lib.has("LibraryIdentifier")) {
  868. Ref<PListNode> bpath_node = lib["BinaryPath"];
  869. Ref<PListNode> lpath_node = lib["LibraryPath"];
  870. Ref<PListNode> lid_node = lib["LibraryIdentifier"];
  871. if (bpath_node.is_valid() && lpath_node.is_valid() && lid_node.is_valid()) {
  872. String binary_path = bpath_node->get_value();
  873. String library_identifier = lid_node->get_value();
  874. String file_name = binary_path.get_basename().get_file();
  875. String framework_name = file_name + ".framework";
  876. bpath_node->data_string = framework_name.utf8();
  877. lpath_node->data_string = framework_name.utf8();
  878. if (!filesystem_da->dir_exists(p_destination.path_join(library_identifier))) {
  879. filesystem_da->make_dir_recursive(p_destination.path_join(library_identifier));
  880. }
  881. _convert_to_framework(p_source.path_join(library_identifier).path_join(binary_path), p_destination.path_join(library_identifier).path_join(framework_name), p_id);
  882. if (lib.has("DebugSymbolsPath")) {
  883. Ref<PListNode> dpath_node = lib["DebugSymbolsPath"];
  884. if (dpath_node.is_valid()) {
  885. String dpath = dpath_node->get_value();
  886. if (da->dir_exists(p_source.path_join(library_identifier).path_join(dpath))) {
  887. da->copy_dir(p_source.path_join(library_identifier).path_join(dpath), p_destination.path_join(library_identifier).path_join("dSYMs"));
  888. }
  889. }
  890. }
  891. }
  892. }
  893. }
  894. String info_plist = plist->save_text();
  895. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  896. if (f.is_valid()) {
  897. f->store_string(info_plist);
  898. }
  899. } else {
  900. String file_name = p_destination.get_basename().get_file();
  901. String framework_name = file_name + ".framework";
  902. da->copy(p_source, p_destination.path_join(file_name));
  903. // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib
  904. {
  905. List<String> install_name_args;
  906. install_name_args.push_back("-id");
  907. install_name_args.push_back(String("@rpath").path_join(framework_name).path_join(file_name));
  908. install_name_args.push_back(p_destination.path_join(file_name));
  909. OS::get_singleton()->execute("install_name_tool", install_name_args);
  910. }
  911. // Creating Info.plist
  912. {
  913. String lib_clean_name = file_name;
  914. for (int i = 0; i < lib_clean_name.length(); i++) {
  915. if (!is_ascii_alphanumeric_char(lib_clean_name[i]) && lib_clean_name[i] != '.' && lib_clean_name[i] != '-') {
  916. lib_clean_name[i] = '-';
  917. }
  918. }
  919. String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  920. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  921. "<plist version=\"1.0\">\n"
  922. " <dict>\n"
  923. " <key>CFBundleShortVersionString</key>\n"
  924. " <string>1.0</string>\n"
  925. " <key>CFBundleIdentifier</key>\n"
  926. " <string>$id.framework.$cl_name</string>\n"
  927. " <key>CFBundleName</key>\n"
  928. " <string>$name</string>\n"
  929. " <key>CFBundleExecutable</key>\n"
  930. " <string>$name</string>\n"
  931. " <key>DTPlatformName</key>\n"
  932. " <string>iphoneos</string>\n"
  933. " <key>CFBundleInfoDictionaryVersion</key>\n"
  934. " <string>6.0</string>\n"
  935. " <key>CFBundleVersion</key>\n"
  936. " <string>1</string>\n"
  937. " <key>CFBundlePackageType</key>\n"
  938. " <string>FMWK</string>\n"
  939. " <key>MinimumOSVersion</key>\n"
  940. " <string>12.0</string>\n"
  941. " </dict>\n"
  942. "</plist>";
  943. String info_plist = info_plist_format.replace("$id", p_id).replace("$name", file_name).replace("$cl_name", lib_clean_name);
  944. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  945. if (f.is_valid()) {
  946. f->store_string(info_plist);
  947. }
  948. }
  949. }
  950. return OK;
  951. }
  952. void EditorExportPlatformIOS::_add_assets_to_project(const String &p_out_dir, const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  953. // that is just a random number, we just need Godot IDs not to clash with
  954. // existing IDs in the project.
  955. PbxId current_id = { 0x58938401, 0, 0 };
  956. String pbx_files;
  957. String pbx_frameworks_build;
  958. String pbx_frameworks_refs;
  959. String pbx_resources_build;
  960. String pbx_resources_refs;
  961. String pbx_embeded_frameworks;
  962. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  963. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = \"$name\"; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  964. for (int i = 0; i < p_additional_assets.size(); ++i) {
  965. String additional_asset_info_format = file_info_format;
  966. String build_id = (++current_id).str();
  967. String ref_id = (++current_id).str();
  968. String framework_id = "";
  969. const IOSExportAsset &asset = p_additional_assets[i];
  970. String type;
  971. if (asset.exported_path.ends_with(".framework")) {
  972. int total_libs = 0;
  973. int static_libs = 0;
  974. int dylibs = 0;
  975. int frameworks = 0;
  976. _check_xcframework_content(p_out_dir.path_join(asset.exported_path), total_libs, static_libs, dylibs, frameworks);
  977. if (asset.should_embed && (static_libs != total_libs)) {
  978. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  979. framework_id = (++current_id).str();
  980. pbx_embeded_frameworks += framework_id + ",\n";
  981. }
  982. type = "wrapper.framework";
  983. } else if (asset.exported_path.ends_with(".xcframework")) {
  984. if (asset.should_embed) {
  985. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  986. framework_id = (++current_id).str();
  987. pbx_embeded_frameworks += framework_id + ",\n";
  988. }
  989. type = "wrapper.xcframework";
  990. } else if (asset.exported_path.ends_with(".dylib")) {
  991. type = "compiled.mach-o.dylib";
  992. } else if (asset.exported_path.ends_with(".a")) {
  993. type = "archive.ar";
  994. } else {
  995. type = "file";
  996. }
  997. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  998. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  999. if (pbx_build.length() > 0) {
  1000. pbx_build += ",\n";
  1001. pbx_refs += ",\n";
  1002. }
  1003. pbx_build += build_id;
  1004. pbx_refs += ref_id;
  1005. Dictionary format_dict;
  1006. format_dict["build_id"] = build_id;
  1007. format_dict["ref_id"] = ref_id;
  1008. format_dict["name"] = asset.exported_path.get_file();
  1009. format_dict["file_path"] = asset.exported_path;
  1010. format_dict["file_type"] = type;
  1011. if (framework_id.length() > 0) {
  1012. format_dict["framework_id"] = framework_id;
  1013. }
  1014. pbx_files += additional_asset_info_format.format(format_dict, "$_");
  1015. }
  1016. // Note, frameworks like gamekit are always included in our project.pbxprof file
  1017. // even if turned off in capabilities.
  1018. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  1019. str = str.replace("$additional_pbx_files", pbx_files);
  1020. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  1021. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  1022. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  1023. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  1024. str = str.replace("$pbx_embeded_frameworks", pbx_embeded_frameworks);
  1025. CharString cs = str.utf8();
  1026. p_project_data.resize(cs.size() - 1);
  1027. for (int i = 0; i < cs.size() - 1; i++) {
  1028. p_project_data.write[i] = cs[i];
  1029. }
  1030. }
  1031. Error EditorExportPlatformIOS::_copy_asset(const Ref<EditorExportPreset> &p_preset, const String &p_out_dir, const String &p_asset, const String *p_custom_file_name, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) {
  1032. String binary_name = p_out_dir.get_file().get_basename();
  1033. Ref<DirAccess> da = DirAccess::create_for_path(p_asset);
  1034. if (da.is_null()) {
  1035. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't open directory: " + p_asset + ".");
  1036. }
  1037. bool file_exists = da->file_exists(p_asset);
  1038. bool dir_exists = da->dir_exists(p_asset);
  1039. if (!file_exists && !dir_exists) {
  1040. return ERR_FILE_NOT_FOUND;
  1041. }
  1042. String base_dir = p_asset.get_base_dir().replace("res://", "").replace(".godot/mono/temp/bin/", "");
  1043. String asset = p_asset.ends_with("/") ? p_asset.left(p_asset.length() - 1) : p_asset;
  1044. String destination_dir;
  1045. String destination;
  1046. String asset_path;
  1047. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1048. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  1049. if (p_is_framework && asset.ends_with(".dylib")) {
  1050. // For iOS we need to turn .dylib into .framework
  1051. // to be able to send application to AppStore
  1052. asset_path = String("dylibs").path_join(base_dir);
  1053. String file_name;
  1054. if (!p_custom_file_name) {
  1055. file_name = p_asset.get_basename().get_file();
  1056. } else {
  1057. file_name = *p_custom_file_name;
  1058. }
  1059. String framework_name = file_name + ".framework";
  1060. asset_path = asset_path.path_join(framework_name);
  1061. destination_dir = p_out_dir.path_join(asset_path);
  1062. destination = destination_dir.path_join(file_name);
  1063. // Convert to framework and copy.
  1064. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1065. if (err) {
  1066. return err;
  1067. }
  1068. } else if (p_is_framework && asset.ends_with(".xcframework")) {
  1069. // For iOS we need to turn .dylib inside .xcframework
  1070. // into .framework to be able to send application to AppStore
  1071. int total_libs = 0;
  1072. int static_libs = 0;
  1073. int dylibs = 0;
  1074. int frameworks = 0;
  1075. _check_xcframework_content(p_asset, total_libs, static_libs, dylibs, frameworks);
  1076. asset_path = String("dylibs").path_join(base_dir);
  1077. String file_name;
  1078. if (!p_custom_file_name) {
  1079. file_name = p_asset.get_file();
  1080. } else {
  1081. file_name = *p_custom_file_name;
  1082. }
  1083. asset_path = asset_path.path_join(file_name);
  1084. destination_dir = p_out_dir.path_join(asset_path);
  1085. destination = destination_dir;
  1086. if (dylibs > 0) {
  1087. // Convert to framework and copy.
  1088. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1089. if (err) {
  1090. return err;
  1091. }
  1092. } else {
  1093. // Copy as is.
  1094. if (!filesystem_da->dir_exists(destination_dir)) {
  1095. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1096. if (make_dir_err) {
  1097. return make_dir_err;
  1098. }
  1099. }
  1100. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1101. if (err) {
  1102. return err;
  1103. }
  1104. }
  1105. } else if (p_is_framework && asset.ends_with(".framework")) {
  1106. // Framework.
  1107. asset_path = String("dylibs").path_join(base_dir);
  1108. String file_name;
  1109. if (!p_custom_file_name) {
  1110. file_name = p_asset.get_file();
  1111. } else {
  1112. file_name = *p_custom_file_name;
  1113. }
  1114. asset_path = asset_path.path_join(file_name);
  1115. destination_dir = p_out_dir.path_join(asset_path);
  1116. destination = destination_dir;
  1117. // Copy as is.
  1118. if (!filesystem_da->dir_exists(destination_dir)) {
  1119. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1120. if (make_dir_err) {
  1121. return make_dir_err;
  1122. }
  1123. }
  1124. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1125. if (err) {
  1126. return err;
  1127. }
  1128. } else {
  1129. // Unknown resource.
  1130. asset_path = base_dir;
  1131. String file_name;
  1132. if (!p_custom_file_name) {
  1133. file_name = p_asset.get_file();
  1134. } else {
  1135. file_name = *p_custom_file_name;
  1136. }
  1137. destination_dir = p_out_dir.path_join(asset_path);
  1138. asset_path = asset_path.path_join(file_name);
  1139. destination = p_out_dir.path_join(asset_path);
  1140. // Copy as is.
  1141. if (!filesystem_da->dir_exists(destination_dir)) {
  1142. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1143. if (make_dir_err) {
  1144. return make_dir_err;
  1145. }
  1146. }
  1147. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1148. if (err) {
  1149. return err;
  1150. }
  1151. }
  1152. if (asset_path.ends_with("/")) {
  1153. asset_path = asset_path.left(asset_path.length() - 1);
  1154. }
  1155. IOSExportAsset exported_asset = { binary_name.path_join(asset_path), p_is_framework, p_should_embed };
  1156. r_exported_assets.push_back(exported_asset);
  1157. return OK;
  1158. }
  1159. Error EditorExportPlatformIOS::_export_additional_assets(const Ref<EditorExportPreset> &p_preset, const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) {
  1160. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  1161. const String &asset = p_assets[f_idx];
  1162. if (asset.begins_with("res://")) {
  1163. Error err = _copy_asset(p_preset, p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1164. ERR_FAIL_COND_V(err != OK, err);
  1165. } else if (ProjectSettings::get_singleton()->localize_path(asset).begins_with("res://")) {
  1166. Error err = _copy_asset(p_preset, p_out_dir, ProjectSettings::get_singleton()->localize_path(asset), nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1167. ERR_FAIL_COND_V(err != OK, err);
  1168. } else {
  1169. // either SDK-builtin or already a part of the export template
  1170. IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed };
  1171. r_exported_assets.push_back(exported_asset);
  1172. }
  1173. }
  1174. return OK;
  1175. }
  1176. Error EditorExportPlatformIOS::_export_additional_assets(const Ref<EditorExportPreset> &p_preset, const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  1177. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1178. for (int i = 0; i < export_plugins.size(); i++) {
  1179. Vector<String> linked_frameworks = export_plugins[i]->get_ios_frameworks();
  1180. Error err = _export_additional_assets(p_preset, p_out_dir, linked_frameworks, true, false, r_exported_assets);
  1181. ERR_FAIL_COND_V(err, err);
  1182. Vector<String> embedded_frameworks = export_plugins[i]->get_ios_embedded_frameworks();
  1183. err = _export_additional_assets(p_preset, p_out_dir, embedded_frameworks, true, true, r_exported_assets);
  1184. ERR_FAIL_COND_V(err, err);
  1185. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1186. for (int j = 0; j < project_static_libs.size(); j++) {
  1187. project_static_libs.write[j] = project_static_libs[j].get_file(); // Only the file name as it's copied to the project
  1188. }
  1189. err = _export_additional_assets(p_preset, p_out_dir, project_static_libs, true, false, r_exported_assets);
  1190. ERR_FAIL_COND_V(err, err);
  1191. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  1192. err = _export_additional_assets(p_preset, p_out_dir, ios_bundle_files, false, false, r_exported_assets);
  1193. ERR_FAIL_COND_V(err, err);
  1194. }
  1195. Vector<String> library_paths;
  1196. for (int i = 0; i < p_libraries.size(); ++i) {
  1197. library_paths.push_back(p_libraries[i].path);
  1198. }
  1199. Error err = _export_additional_assets(p_preset, p_out_dir, library_paths, true, true, r_exported_assets);
  1200. ERR_FAIL_COND_V(err, err);
  1201. return OK;
  1202. }
  1203. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) const {
  1204. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  1205. Vector<String> enabled_archs;
  1206. for (int i = 0; i < all_archs.size(); ++i) {
  1207. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  1208. if (is_enabled) {
  1209. enabled_archs.push_back(all_archs[i].name);
  1210. }
  1211. }
  1212. return enabled_archs;
  1213. }
  1214. Error EditorExportPlatformIOS::_export_ios_plugins(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &dest_dir, Vector<IOSExportAsset> &r_exported_assets, bool p_debug) {
  1215. String plugin_definition_cpp_code;
  1216. String plugin_initialization_cpp_code;
  1217. String plugin_deinitialization_cpp_code;
  1218. Vector<String> plugin_linked_dependencies;
  1219. Vector<String> plugin_embedded_dependencies;
  1220. Vector<String> plugin_files;
  1221. Vector<PluginConfigIOS> enabled_plugins = get_enabled_plugins(p_preset);
  1222. Vector<String> added_linked_dependenciy_names;
  1223. Vector<String> added_embedded_dependenciy_names;
  1224. HashMap<String, String> plist_values;
  1225. HashSet<String> plugin_linker_flags;
  1226. Error err;
  1227. for (int i = 0; i < enabled_plugins.size(); i++) {
  1228. PluginConfigIOS plugin = enabled_plugins[i];
  1229. // Export plugin binary.
  1230. String plugin_main_binary = PluginConfigIOS::get_plugin_main_binary(plugin, p_debug);
  1231. String plugin_binary_result_file = plugin.binary.get_file();
  1232. // We shouldn't embed .xcframework that contains static libraries.
  1233. // Static libraries are not embedded anyway.
  1234. err = _copy_asset(p_preset, dest_dir, plugin_main_binary, &plugin_binary_result_file, true, false, r_exported_assets);
  1235. ERR_FAIL_COND_V(err != OK, err);
  1236. // Adding dependencies.
  1237. // Use separate container for names to check for duplicates.
  1238. for (int j = 0; j < plugin.linked_dependencies.size(); j++) {
  1239. String dependency = plugin.linked_dependencies[j];
  1240. String name = dependency.get_file();
  1241. if (added_linked_dependenciy_names.has(name)) {
  1242. continue;
  1243. }
  1244. added_linked_dependenciy_names.push_back(name);
  1245. plugin_linked_dependencies.push_back(dependency);
  1246. }
  1247. for (int j = 0; j < plugin.system_dependencies.size(); j++) {
  1248. String dependency = plugin.system_dependencies[j];
  1249. String name = dependency.get_file();
  1250. if (added_linked_dependenciy_names.has(name)) {
  1251. continue;
  1252. }
  1253. added_linked_dependenciy_names.push_back(name);
  1254. plugin_linked_dependencies.push_back(dependency);
  1255. }
  1256. for (int j = 0; j < plugin.embedded_dependencies.size(); j++) {
  1257. String dependency = plugin.embedded_dependencies[j];
  1258. String name = dependency.get_file();
  1259. if (added_embedded_dependenciy_names.has(name)) {
  1260. continue;
  1261. }
  1262. added_embedded_dependenciy_names.push_back(name);
  1263. plugin_embedded_dependencies.push_back(dependency);
  1264. }
  1265. plugin_files.append_array(plugin.files_to_copy);
  1266. // Capabilities
  1267. // Also checking for duplicates.
  1268. for (int j = 0; j < plugin.capabilities.size(); j++) {
  1269. String capability = plugin.capabilities[j];
  1270. if (p_config_data.capabilities.has(capability)) {
  1271. continue;
  1272. }
  1273. p_config_data.capabilities.push_back(capability);
  1274. }
  1275. // Linker flags
  1276. // Checking duplicates
  1277. for (int j = 0; j < plugin.linker_flags.size(); j++) {
  1278. String linker_flag = plugin.linker_flags[j];
  1279. plugin_linker_flags.insert(linker_flag);
  1280. }
  1281. // Plist
  1282. // Using hash map container to remove duplicates
  1283. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  1284. String key = E.key;
  1285. const PluginConfigIOS::PlistItem &item = E.value;
  1286. String value;
  1287. switch (item.type) {
  1288. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  1289. String preset_name = "plugins_plist/" + key;
  1290. String input_value = p_preset->get(preset_name);
  1291. value = "<string>" + input_value + "</string>";
  1292. } break;
  1293. default:
  1294. value = item.value;
  1295. break;
  1296. }
  1297. if (key.is_empty() || value.is_empty()) {
  1298. continue;
  1299. }
  1300. String plist_key = "<key>" + key + "</key>";
  1301. plist_values[plist_key] = value;
  1302. }
  1303. // CPP Code
  1304. String definition_comment = "// Plugin: " + plugin.name + "\n";
  1305. String initialization_method = plugin.initialization_method + "();\n";
  1306. String deinitialization_method = plugin.deinitialization_method + "();\n";
  1307. plugin_definition_cpp_code += definition_comment +
  1308. "extern void " + initialization_method +
  1309. "extern void " + deinitialization_method + "\n";
  1310. plugin_initialization_cpp_code += "\t" + initialization_method;
  1311. plugin_deinitialization_cpp_code += "\t" + deinitialization_method;
  1312. if (plugin.use_swift_runtime) {
  1313. p_config_data.use_swift_runtime = true;
  1314. }
  1315. }
  1316. // Updating `Info.plist`
  1317. {
  1318. for (const KeyValue<String, String> &E : plist_values) {
  1319. String key = E.key;
  1320. String value = E.value;
  1321. if (key.is_empty() || value.is_empty()) {
  1322. continue;
  1323. }
  1324. p_config_data.plist_content += key + value + "\n";
  1325. }
  1326. }
  1327. // Export files
  1328. {
  1329. // Export linked plugin dependency
  1330. err = _export_additional_assets(p_preset, dest_dir, plugin_linked_dependencies, true, false, r_exported_assets);
  1331. ERR_FAIL_COND_V(err != OK, err);
  1332. // Export embedded plugin dependency
  1333. err = _export_additional_assets(p_preset, dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets);
  1334. ERR_FAIL_COND_V(err != OK, err);
  1335. // Export plugin files
  1336. err = _export_additional_assets(p_preset, dest_dir, plugin_files, false, false, r_exported_assets);
  1337. ERR_FAIL_COND_V(err != OK, err);
  1338. }
  1339. // Update CPP
  1340. {
  1341. Dictionary plugin_format;
  1342. plugin_format["definition"] = plugin_definition_cpp_code;
  1343. plugin_format["initialization"] = plugin_initialization_cpp_code;
  1344. plugin_format["deinitialization"] = plugin_deinitialization_cpp_code;
  1345. String plugin_cpp_code = "\n// Godot Plugins\n"
  1346. "void godot_ios_plugins_initialize();\n"
  1347. "void godot_ios_plugins_deinitialize();\n"
  1348. "// Exported Plugins\n\n"
  1349. "$definition"
  1350. "// Use Plugins\n"
  1351. "void godot_ios_plugins_initialize() {\n"
  1352. "$initialization"
  1353. "}\n\n"
  1354. "void godot_ios_plugins_deinitialize() {\n"
  1355. "$deinitialization"
  1356. "}\n";
  1357. p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_");
  1358. }
  1359. // Update Linker Flag Values
  1360. {
  1361. String result_linker_flags = " ";
  1362. for (const String &E : plugin_linker_flags) {
  1363. const String &flag = E;
  1364. if (flag.length() == 0) {
  1365. continue;
  1366. }
  1367. if (result_linker_flags.length() > 0) {
  1368. result_linker_flags += ' ';
  1369. }
  1370. result_linker_flags += flag;
  1371. }
  1372. result_linker_flags = result_linker_flags.replace("\"", "\\\"");
  1373. p_config_data.linker_flags += result_linker_flags;
  1374. }
  1375. return OK;
  1376. }
  1377. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1378. return _export_project_helper(p_preset, p_debug, p_path, p_flags, false, false);
  1379. }
  1380. Error EditorExportPlatformIOS::_export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags, bool p_simulator, bool p_oneclick) {
  1381. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1382. const String dest_dir = p_path.get_base_dir() + "/";
  1383. const String binary_name = p_path.get_file().get_basename();
  1384. const String binary_dir = dest_dir + binary_name;
  1385. if (!DirAccess::exists(dest_dir)) {
  1386. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), dest_dir));
  1387. return ERR_FILE_BAD_PATH;
  1388. }
  1389. bool export_project_only = p_preset->get("application/export_project_only");
  1390. if (p_oneclick) {
  1391. export_project_only = false; // Skip for one-click deploy.
  1392. }
  1393. EditorProgress ep("export", export_project_only ? TTR("Exporting for iOS (Project Files Only)") : TTR("Exporting for iOS"), export_project_only ? 2 : 5, true);
  1394. String team_id = p_preset->get("application/app_store_team_id");
  1395. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  1396. String src_pkg_name;
  1397. if (p_debug) {
  1398. src_pkg_name = p_preset->get("custom_template/debug");
  1399. } else {
  1400. src_pkg_name = p_preset->get("custom_template/release");
  1401. }
  1402. if (src_pkg_name.is_empty()) {
  1403. String err;
  1404. src_pkg_name = find_export_template("ios.zip", &err);
  1405. if (src_pkg_name.is_empty()) {
  1406. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Export template not found."));
  1407. return ERR_FILE_NOT_FOUND;
  1408. }
  1409. }
  1410. {
  1411. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1412. if (da.is_valid()) {
  1413. String current_dir = da->get_current_dir();
  1414. // remove leftovers from last export so they don't interfere
  1415. // in case some files are no longer needed
  1416. if (da->change_dir(binary_dir + ".xcodeproj") == OK) {
  1417. da->erase_contents_recursive();
  1418. }
  1419. if (da->change_dir(binary_dir) == OK) {
  1420. da->erase_contents_recursive();
  1421. }
  1422. da->change_dir(current_dir);
  1423. if (!da->dir_exists(binary_dir)) {
  1424. Error err = da->make_dir(binary_dir);
  1425. if (err != OK) {
  1426. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create the directory: \"%s\""), binary_dir));
  1427. return err;
  1428. }
  1429. }
  1430. }
  1431. }
  1432. if (ep.step("Making .pck", 0)) {
  1433. return ERR_SKIP;
  1434. }
  1435. String pack_path = binary_dir + ".pck";
  1436. Vector<SharedObject> libraries;
  1437. Error err = save_pack(p_preset, p_debug, pack_path, &libraries);
  1438. if (err) {
  1439. // Message is supplied by the subroutine method.
  1440. return err;
  1441. }
  1442. if (ep.step("Extracting and configuring Xcode project", 1)) {
  1443. return ERR_SKIP;
  1444. }
  1445. String library_to_use = "libgodot.ios." + String(p_debug ? "debug" : "release") + ".xcframework";
  1446. print_line("Static framework: " + library_to_use);
  1447. String pkg_name;
  1448. if (String(GLOBAL_GET("application/config/name")) != "") {
  1449. pkg_name = String(GLOBAL_GET("application/config/name"));
  1450. } else {
  1451. pkg_name = "Unnamed";
  1452. }
  1453. bool found_library = false;
  1454. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  1455. HashSet<String> files_to_parse;
  1456. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  1457. files_to_parse.insert(project_file);
  1458. files_to_parse.insert("godot_ios/export_options.plist");
  1459. files_to_parse.insert("godot_ios/dummy.cpp");
  1460. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  1461. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  1462. files_to_parse.insert("godot_ios/godot_ios.entitlements");
  1463. files_to_parse.insert("godot_ios/Launch Screen.storyboard");
  1464. IOSConfigData config_data = {
  1465. pkg_name,
  1466. binary_name,
  1467. _get_additional_plist_content(),
  1468. String(" ").join(_get_preset_architectures(p_preset)),
  1469. _get_linker_flags(),
  1470. _get_cpp_code(),
  1471. "",
  1472. "",
  1473. "",
  1474. "",
  1475. Vector<String>(),
  1476. false
  1477. };
  1478. config_data.plist_content += p_preset->get("application/additional_plist_content").operator String() + "\n";
  1479. Vector<IOSExportAsset> assets;
  1480. Ref<DirAccess> tmp_app_path = DirAccess::create_for_path(dest_dir);
  1481. if (tmp_app_path.is_null()) {
  1482. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), dest_dir));
  1483. return ERR_CANT_CREATE;
  1484. }
  1485. print_line("Unzipping...");
  1486. Ref<FileAccess> io_fa;
  1487. zlib_filefunc_def io = zipio_create_io(&io_fa);
  1488. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  1489. if (!src_pkg_zip) {
  1490. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Could not open export template (not a zip file?): \"%s\".", src_pkg_name));
  1491. return ERR_CANT_OPEN;
  1492. }
  1493. err = _export_ios_plugins(p_preset, config_data, binary_dir, assets, p_debug);
  1494. if (err != OK) {
  1495. // TODO: Improve error reporting by using `add_message` throughout all methods called via `_export_ios_plugins`.
  1496. // For now a generic top level message would be fine, but we're ought to use proper reporting here instead of
  1497. // just fail macros and non-descriptive error return values.
  1498. add_message(EXPORT_MESSAGE_ERROR, TTR("iOS Plugins"), vformat(TTR("Failed to export iOS plugins with code %d. Please check the output log."), err));
  1499. return err;
  1500. }
  1501. //export rest of the files
  1502. int ret = unzGoToFirstFile(src_pkg_zip);
  1503. Vector<uint8_t> project_file_data;
  1504. while (ret == UNZ_OK) {
  1505. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1506. bool is_execute = false;
  1507. #endif
  1508. //get filename
  1509. unz_file_info info;
  1510. char fname[16384];
  1511. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  1512. if (ret != UNZ_OK) {
  1513. break;
  1514. }
  1515. String file = String::utf8(fname);
  1516. print_line("READ: " + file);
  1517. Vector<uint8_t> data;
  1518. data.resize(info.uncompressed_size);
  1519. //read
  1520. unzOpenCurrentFile(src_pkg_zip);
  1521. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  1522. unzCloseCurrentFile(src_pkg_zip);
  1523. //write
  1524. if (files_to_parse.has(file)) {
  1525. _fix_config_file(p_preset, data, config_data, p_debug);
  1526. } else if (file.begins_with("libgodot.ios")) {
  1527. if (!file.begins_with(library_to_use) || file.ends_with(String("/empty"))) {
  1528. ret = unzGoToNextFile(src_pkg_zip);
  1529. continue; //ignore!
  1530. }
  1531. found_library = true;
  1532. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1533. is_execute = true;
  1534. #endif
  1535. file = file.replace(library_to_use, binary_name + ".xcframework");
  1536. }
  1537. if (file == project_file) {
  1538. project_file_data = data;
  1539. }
  1540. ///@TODO need to parse logo files
  1541. if (data.size() > 0) {
  1542. file = file.replace("godot_ios", binary_name);
  1543. print_line("ADDING: " + file + " size: " + itos(data.size()));
  1544. /* write it into our folder structure */
  1545. file = dest_dir + file;
  1546. /* make sure this folder exists */
  1547. String dir_name = file.get_base_dir();
  1548. if (!tmp_app_path->dir_exists(dir_name)) {
  1549. print_line("Creating " + dir_name);
  1550. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  1551. if (dir_err) {
  1552. unzClose(src_pkg_zip);
  1553. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), dir_name));
  1554. return ERR_CANT_CREATE;
  1555. }
  1556. }
  1557. /* write the file */
  1558. {
  1559. Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);
  1560. if (f.is_null()) {
  1561. unzClose(src_pkg_zip);
  1562. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), file));
  1563. return ERR_CANT_CREATE;
  1564. };
  1565. f->store_buffer(data.ptr(), data.size());
  1566. }
  1567. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1568. if (is_execute) {
  1569. // we need execute rights on this file
  1570. chmod(file.utf8().get_data(), 0755);
  1571. }
  1572. #endif
  1573. }
  1574. ret = unzGoToNextFile(src_pkg_zip);
  1575. }
  1576. /* we're done with our source zip */
  1577. unzClose(src_pkg_zip);
  1578. if (!found_library) {
  1579. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Requested template library '%s' not found. It might be missing from your template archive."), library_to_use));
  1580. return ERR_FILE_NOT_FOUND;
  1581. }
  1582. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  1583. Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
  1584. Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
  1585. Dictionary photolibrary_usage_descriptions = p_preset->get("privacy/photolibrary_usage_description_localized");
  1586. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  1587. if (translations.size() > 0) {
  1588. {
  1589. String fname = binary_dir + "/en.lproj";
  1590. tmp_app_path->make_dir_recursive(fname);
  1591. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1592. f->store_line("/* Localized versions of Info.plist keys */");
  1593. f->store_line("");
  1594. f->store_line("CFBundleDisplayName = \"" + GLOBAL_GET("application/config/name").operator String() + "\";");
  1595. f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
  1596. f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
  1597. f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photolibrary_usage_description").operator String() + "\";");
  1598. }
  1599. HashSet<String> languages;
  1600. for (const String &E : translations) {
  1601. Ref<Translation> tr = ResourceLoader::load(E);
  1602. if (tr.is_valid() && tr->get_locale() != "en") {
  1603. languages.insert(tr->get_locale());
  1604. }
  1605. }
  1606. for (const String &lang : languages) {
  1607. String fname = binary_dir + "/" + lang + ".lproj";
  1608. tmp_app_path->make_dir_recursive(fname);
  1609. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1610. f->store_line("/* Localized versions of Info.plist keys */");
  1611. f->store_line("");
  1612. if (appnames.has(lang)) {
  1613. f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
  1614. }
  1615. if (camera_usage_descriptions.has(lang)) {
  1616. f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
  1617. }
  1618. if (microphone_usage_descriptions.has(lang)) {
  1619. f->store_line("NSMicrophoneUsageDescription = \"" + microphone_usage_descriptions[lang].operator String() + "\";");
  1620. }
  1621. if (photolibrary_usage_descriptions.has(lang)) {
  1622. f->store_line("NSPhotoLibraryUsageDescription = \"" + photolibrary_usage_descriptions[lang].operator String() + "\";");
  1623. }
  1624. }
  1625. }
  1626. // Copy project static libs to the project
  1627. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1628. for (int i = 0; i < export_plugins.size(); i++) {
  1629. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1630. for (int j = 0; j < project_static_libs.size(); j++) {
  1631. const String &static_lib_path = project_static_libs[j];
  1632. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  1633. Error lib_copy_err = tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  1634. if (lib_copy_err != OK) {
  1635. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not copy a file at path \"%s\" to \"%s\"."), static_lib_path, dest_lib_file_path));
  1636. return lib_copy_err;
  1637. }
  1638. }
  1639. }
  1640. String iconset_dir = binary_dir + "/Images.xcassets/AppIcon.appiconset/";
  1641. err = OK;
  1642. if (!tmp_app_path->dir_exists(iconset_dir)) {
  1643. err = tmp_app_path->make_dir_recursive(iconset_dir);
  1644. }
  1645. if (err != OK) {
  1646. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), iconset_dir));
  1647. return err;
  1648. }
  1649. err = _export_icons(p_preset, iconset_dir);
  1650. if (err != OK) {
  1651. // Message is supplied by the subroutine method.
  1652. return err;
  1653. }
  1654. {
  1655. String splash_image_path = binary_dir + "/Images.xcassets/SplashImage.imageset/";
  1656. Ref<DirAccess> launch_screen_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1657. if (launch_screen_da.is_null()) {
  1658. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not access the filesystem."));
  1659. return ERR_CANT_CREATE;
  1660. }
  1661. print_line("Exporting launch screen storyboard");
  1662. err = _export_loading_screen_file(p_preset, splash_image_path);
  1663. if (err != OK) {
  1664. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create a file at path \"%s\" with code %d."), splash_image_path, err));
  1665. }
  1666. }
  1667. if (err != OK) {
  1668. return err;
  1669. }
  1670. print_line("Exporting additional assets");
  1671. _export_additional_assets(p_preset, binary_dir, libraries, assets);
  1672. _add_assets_to_project(dest_dir, p_preset, project_file_data, assets);
  1673. String project_file_name = binary_dir + ".xcodeproj/project.pbxproj";
  1674. {
  1675. Ref<FileAccess> f = FileAccess::open(project_file_name, FileAccess::WRITE);
  1676. if (f.is_null()) {
  1677. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), project_file_name));
  1678. return ERR_CANT_CREATE;
  1679. };
  1680. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  1681. }
  1682. #ifdef MACOS_ENABLED
  1683. {
  1684. if (ep.step("Code-signing dylibs", 2)) {
  1685. return ERR_SKIP;
  1686. }
  1687. Ref<DirAccess> dylibs_dir = DirAccess::open(binary_dir + "/dylibs");
  1688. ERR_FAIL_COND_V(dylibs_dir.is_null(), ERR_CANT_OPEN);
  1689. CodesignData codesign_data(p_preset, p_debug);
  1690. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  1691. if (err != OK) {
  1692. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Code signing failed, see editor log for details."));
  1693. return err;
  1694. }
  1695. }
  1696. if (export_project_only) {
  1697. return OK;
  1698. }
  1699. if (ep.step("Making .xcarchive", 3)) {
  1700. return ERR_SKIP;
  1701. }
  1702. String archive_path = p_path.get_basename() + ".xcarchive";
  1703. List<String> archive_args;
  1704. archive_args.push_back("-project");
  1705. archive_args.push_back(binary_dir + ".xcodeproj");
  1706. archive_args.push_back("-scheme");
  1707. archive_args.push_back(binary_name);
  1708. archive_args.push_back("-sdk");
  1709. if (p_simulator) {
  1710. archive_args.push_back("iphonesimulator");
  1711. } else {
  1712. archive_args.push_back("iphoneos");
  1713. }
  1714. archive_args.push_back("-configuration");
  1715. archive_args.push_back(p_debug ? "Debug" : "Release");
  1716. archive_args.push_back("-destination");
  1717. if (p_simulator) {
  1718. archive_args.push_back("generic/platform=iOS Simulator");
  1719. } else {
  1720. archive_args.push_back("generic/platform=iOS");
  1721. }
  1722. archive_args.push_back("archive");
  1723. archive_args.push_back("-allowProvisioningUpdates");
  1724. archive_args.push_back("-archivePath");
  1725. archive_args.push_back(archive_path);
  1726. String archive_str;
  1727. err = OS::get_singleton()->execute("xcodebuild", archive_args, &archive_str, nullptr, true);
  1728. if (err != OK) {
  1729. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  1730. return err;
  1731. }
  1732. print_line("xcodebuild (.xcarchive):\n" + archive_str);
  1733. if (!archive_str.contains("** ARCHIVE SUCCEEDED **")) {
  1734. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR("Xcode project build failed, see editor log for details."));
  1735. return FAILED;
  1736. }
  1737. if (!p_oneclick) {
  1738. if (ep.step("Making .ipa", 4)) {
  1739. return ERR_SKIP;
  1740. }
  1741. List<String> export_args;
  1742. export_args.push_back("-exportArchive");
  1743. export_args.push_back("-archivePath");
  1744. export_args.push_back(archive_path);
  1745. export_args.push_back("-exportOptionsPlist");
  1746. export_args.push_back(binary_dir + "/export_options.plist");
  1747. export_args.push_back("-allowProvisioningUpdates");
  1748. export_args.push_back("-exportPath");
  1749. export_args.push_back(dest_dir);
  1750. String export_str;
  1751. err = OS::get_singleton()->execute("xcodebuild", export_args, &export_str, nullptr, true);
  1752. if (err != OK) {
  1753. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  1754. return err;
  1755. }
  1756. print_line("xcodebuild (.ipa):\n" + export_str);
  1757. if (!export_str.contains("** EXPORT SUCCEEDED **")) {
  1758. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR(".ipa export failed, see editor log for details."));
  1759. return FAILED;
  1760. }
  1761. }
  1762. #else
  1763. add_message(EXPORT_MESSAGE_WARNING, TTR("Xcode Build"), TTR(".ipa can only be built on macOS. Leaving Xcode project without building the package."));
  1764. #endif
  1765. return OK;
  1766. }
  1767. bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  1768. #if defined(MODULE_MONO_ENABLED) && !defined(MACOS_ENABLED)
  1769. // TODO: Remove this restriction when we don't rely on macOS tools to package up the native libraries anymore.
  1770. r_error += TTR("Exporting to iOS when using C#/.NET is experimental and requires macOS.") + "\n";
  1771. return false;
  1772. #else
  1773. String err;
  1774. bool valid = false;
  1775. #if defined(MODULE_MONO_ENABLED)
  1776. // iOS export is still a work in progress, keep a message as a warning.
  1777. err += TTR("Exporting to iOS when using C#/.NET is experimental.") + "\n";
  1778. #endif
  1779. // Look for export templates (first official, and if defined custom templates).
  1780. bool dvalid = exists_export_template("ios.zip", &err);
  1781. bool rvalid = dvalid; // Both in the same ZIP.
  1782. if (p_preset->get("custom_template/debug") != "") {
  1783. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1784. if (!dvalid) {
  1785. err += TTR("Custom debug template not found.") + "\n";
  1786. }
  1787. }
  1788. if (p_preset->get("custom_template/release") != "") {
  1789. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1790. if (!rvalid) {
  1791. err += TTR("Custom release template not found.") + "\n";
  1792. }
  1793. }
  1794. valid = dvalid || rvalid;
  1795. r_missing_templates = !valid;
  1796. const String &additional_plist_content = p_preset->get("application/additional_plist_content");
  1797. if (!additional_plist_content.is_empty()) {
  1798. const String &plist = vformat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1799. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
  1800. "<plist version=\"1.0\">"
  1801. "<dict>\n"
  1802. "%s\n"
  1803. "</dict>\n"
  1804. "</plist>\n",
  1805. additional_plist_content);
  1806. String plist_err;
  1807. Ref<PList> plist_parser;
  1808. plist_parser.instantiate();
  1809. if (!plist_parser->load_string(plist, plist_err)) {
  1810. err += TTR("Invalid additional PList content: ") + plist_err + "\n";
  1811. valid = false;
  1812. }
  1813. }
  1814. if (!err.is_empty()) {
  1815. r_error = err;
  1816. }
  1817. return valid;
  1818. #endif // !(MODULE_MONO_ENABLED && !MACOS_ENABLED)
  1819. }
  1820. bool EditorExportPlatformIOS::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  1821. String err;
  1822. bool valid = true;
  1823. // Validate the project configuration.
  1824. List<ExportOption> options;
  1825. get_export_options(&options);
  1826. for (const EditorExportPlatform::ExportOption &E : options) {
  1827. if (get_export_option_visibility(p_preset.ptr(), E.option.name)) {
  1828. String warn = get_export_option_warning(p_preset.ptr(), E.option.name);
  1829. if (!warn.is_empty()) {
  1830. err += warn + "\n";
  1831. if (E.required) {
  1832. valid = false;
  1833. }
  1834. }
  1835. }
  1836. }
  1837. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  1838. valid = false;
  1839. }
  1840. if (!err.is_empty()) {
  1841. r_error = err;
  1842. }
  1843. return valid;
  1844. }
  1845. int EditorExportPlatformIOS::get_options_count() const {
  1846. MutexLock lock(device_lock);
  1847. return devices.size();
  1848. }
  1849. String EditorExportPlatformIOS::get_options_tooltip() const {
  1850. return TTR("Select device from the list");
  1851. }
  1852. Ref<ImageTexture> EditorExportPlatformIOS::get_option_icon(int p_index) const {
  1853. MutexLock lock(device_lock);
  1854. Ref<ImageTexture> icon;
  1855. if (p_index >= 0 || p_index < devices.size()) {
  1856. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  1857. if (theme.is_valid()) {
  1858. if (devices[p_index].simulator) {
  1859. icon = theme->get_icon("IOSSimulator", EditorStringName(EditorIcons));
  1860. } else if (devices[p_index].wifi) {
  1861. icon = theme->get_icon("IOSDeviceWireless", EditorStringName(EditorIcons));
  1862. } else {
  1863. icon = theme->get_icon("IOSDeviceWired", EditorStringName(EditorIcons));
  1864. }
  1865. }
  1866. }
  1867. return icon;
  1868. }
  1869. String EditorExportPlatformIOS::get_option_label(int p_index) const {
  1870. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1871. MutexLock lock(device_lock);
  1872. return devices[p_index].name;
  1873. }
  1874. String EditorExportPlatformIOS::get_option_tooltip(int p_index) const {
  1875. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1876. MutexLock lock(device_lock);
  1877. return "UUID: " + devices[p_index].id;
  1878. }
  1879. bool EditorExportPlatformIOS::is_package_name_valid(const String &p_package, String *r_error) const {
  1880. String pname = p_package;
  1881. if (pname.length() == 0) {
  1882. if (r_error) {
  1883. *r_error = TTR("Identifier is missing.");
  1884. }
  1885. return false;
  1886. }
  1887. for (int i = 0; i < pname.length(); i++) {
  1888. char32_t c = pname[i];
  1889. if (!(is_ascii_alphanumeric_char(c) || c == '-' || c == '.')) {
  1890. if (r_error) {
  1891. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  1892. }
  1893. return false;
  1894. }
  1895. }
  1896. return true;
  1897. }
  1898. #ifdef MACOS_ENABLED
  1899. bool EditorExportPlatformIOS::_check_xcode_install() {
  1900. static bool xcode_found = false;
  1901. if (!xcode_found) {
  1902. Vector<String> mdfind_paths;
  1903. List<String> mdfind_args;
  1904. mdfind_args.push_back("kMDItemCFBundleIdentifier=com.apple.dt.Xcode");
  1905. String output;
  1906. Error err = OS::get_singleton()->execute("mdfind", mdfind_args, &output);
  1907. if (err == OK) {
  1908. mdfind_paths = output.split("\n");
  1909. }
  1910. for (const String &found_path : mdfind_paths) {
  1911. xcode_found = !found_path.is_empty() && DirAccess::dir_exists_absolute(found_path.strip_edges());
  1912. if (xcode_found) {
  1913. break;
  1914. }
  1915. }
  1916. }
  1917. return xcode_found;
  1918. }
  1919. void EditorExportPlatformIOS::_check_for_changes_poll_thread(void *ud) {
  1920. EditorExportPlatformIOS *ea = static_cast<EditorExportPlatformIOS *>(ud);
  1921. while (!ea->quit_request.is_set()) {
  1922. // Nothing to do if we already know the plugins have changed.
  1923. if (!ea->plugins_changed.is_set()) {
  1924. MutexLock lock(ea->plugins_lock);
  1925. Vector<PluginConfigIOS> loaded_plugins = get_plugins();
  1926. if (ea->plugins.size() != loaded_plugins.size()) {
  1927. ea->plugins_changed.set();
  1928. } else {
  1929. for (int i = 0; i < ea->plugins.size(); i++) {
  1930. if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) {
  1931. ea->plugins_changed.set();
  1932. break;
  1933. }
  1934. }
  1935. }
  1936. }
  1937. // Check for devices updates.
  1938. Vector<Device> ldevices;
  1939. // Enum real devices (via ios_deploy, pre Xcode 15).
  1940. String idepl = EDITOR_GET("export/ios/ios_deploy");
  1941. if (ea->has_runnable_preset.is_set() && !idepl.is_empty()) {
  1942. String devices;
  1943. List<String> args;
  1944. args.push_back("-c");
  1945. args.push_back("-timeout");
  1946. args.push_back("1");
  1947. args.push_back("-j");
  1948. args.push_back("-u");
  1949. args.push_back("-I");
  1950. int ec = 0;
  1951. Error err = OS::get_singleton()->execute(idepl, args, &devices, &ec, true);
  1952. if (err == OK && ec == 0) {
  1953. Ref<JSON> json;
  1954. json.instantiate();
  1955. devices = "{ \"devices\":[" + devices.replace("}{", "},{") + "]}";
  1956. err = json->parse(devices);
  1957. if (err == OK) {
  1958. Dictionary data = json->get_data();
  1959. Array devices = data["devices"];
  1960. for (int i = 0; i < devices.size(); i++) {
  1961. Dictionary device_event = devices[i];
  1962. if (device_event["Event"] == "DeviceDetected") {
  1963. Dictionary device_info = device_event["Device"];
  1964. Device nd;
  1965. nd.id = device_info["DeviceIdentifier"];
  1966. nd.name = device_info["DeviceName"].operator String() + " (ios_deploy, " + ((device_event["Interface"] == "WIFI") ? "network" : "wired") + ")";
  1967. nd.wifi = device_event["Interface"] == "WIFI";
  1968. nd.use_ios_deploy = true;
  1969. nd.simulator = false;
  1970. ldevices.push_back(nd);
  1971. }
  1972. }
  1973. }
  1974. }
  1975. }
  1976. // Enum simulators.
  1977. if (ea->has_runnable_preset.is_set() && _check_xcode_install() && (FileAccess::exists("/usr/bin/xcrun") || FileAccess::exists("/bin/xcrun"))) {
  1978. {
  1979. String devices;
  1980. List<String> args;
  1981. args.push_back("devicectl");
  1982. args.push_back("list");
  1983. args.push_back("devices");
  1984. args.push_back("-j");
  1985. args.push_back("-");
  1986. args.push_back("-q");
  1987. int ec = 0;
  1988. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  1989. if (err == OK && ec == 0) {
  1990. Ref<JSON> json;
  1991. json.instantiate();
  1992. err = json->parse(devices);
  1993. if (err == OK) {
  1994. const Dictionary &data = json->get_data();
  1995. const Dictionary &result = data["result"];
  1996. const Array &devices = result["devices"];
  1997. for (int i = 0; i < devices.size(); i++) {
  1998. const Dictionary &device_info = devices[i];
  1999. const Dictionary &conn_props = device_info["connectionProperties"];
  2000. const Dictionary &dev_props = device_info["deviceProperties"];
  2001. if (conn_props["pairingState"] == "paired" && dev_props["developerModeStatus"] == "enabled") {
  2002. Device nd;
  2003. nd.id = device_info["identifier"];
  2004. nd.name = dev_props["name"].operator String() + " (devicectl, " + ((conn_props["transportType"] == "localNetwork") ? "network" : "wired") + ")";
  2005. nd.wifi = conn_props["transportType"] == "localNetwork";
  2006. nd.simulator = false;
  2007. ldevices.push_back(nd);
  2008. }
  2009. }
  2010. }
  2011. }
  2012. }
  2013. // Enum simulators.
  2014. if (ea->has_runnable_preset.is_set()) {
  2015. String devices;
  2016. List<String> args;
  2017. args.push_back("simctl");
  2018. args.push_back("list");
  2019. args.push_back("devices");
  2020. args.push_back("-j");
  2021. int ec = 0;
  2022. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  2023. if (err == OK && ec == 0) {
  2024. Ref<JSON> json;
  2025. json.instantiate();
  2026. err = json->parse(devices);
  2027. if (err == OK) {
  2028. const Dictionary &data = json->get_data();
  2029. const Dictionary &devices = data["devices"];
  2030. for (const Variant *key = devices.next(nullptr); key; key = devices.next(key)) {
  2031. const Array &os_devices = devices[*key];
  2032. for (int i = 0; i < os_devices.size(); i++) {
  2033. const Dictionary &device_info = os_devices[i];
  2034. if (device_info["isAvailable"].operator bool() && device_info["state"] == "Booted") {
  2035. Device nd;
  2036. nd.id = device_info["udid"];
  2037. nd.name = device_info["name"].operator String() + " (simulator)";
  2038. nd.simulator = true;
  2039. ldevices.push_back(nd);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. }
  2045. }
  2046. }
  2047. // Update device list.
  2048. {
  2049. MutexLock lock(ea->device_lock);
  2050. bool different = false;
  2051. if (ea->devices.size() != ldevices.size()) {
  2052. different = true;
  2053. } else {
  2054. for (int i = 0; i < ea->devices.size(); i++) {
  2055. if (ea->devices[i].id != ldevices[i].id) {
  2056. different = true;
  2057. break;
  2058. }
  2059. }
  2060. }
  2061. if (different) {
  2062. ea->devices = ldevices;
  2063. ea->devices_changed.set();
  2064. }
  2065. }
  2066. uint64_t sleep = 200;
  2067. uint64_t wait = 3000000;
  2068. uint64_t time = OS::get_singleton()->get_ticks_usec();
  2069. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  2070. OS::get_singleton()->delay_usec(1000 * sleep);
  2071. if (ea->quit_request.is_set()) {
  2072. break;
  2073. }
  2074. }
  2075. }
  2076. }
  2077. void EditorExportPlatformIOS::_update_preset_status() {
  2078. const int preset_count = EditorExport::get_singleton()->get_export_preset_count();
  2079. bool has_runnable = false;
  2080. for (int i = 0; i < preset_count; i++) {
  2081. const Ref<EditorExportPreset> &preset = EditorExport::get_singleton()->get_export_preset(i);
  2082. if (preset->get_platform() == this && preset->is_runnable()) {
  2083. has_runnable = true;
  2084. break;
  2085. }
  2086. }
  2087. if (has_runnable) {
  2088. has_runnable_preset.set();
  2089. } else {
  2090. has_runnable_preset.clear();
  2091. }
  2092. }
  2093. #endif
  2094. Error EditorExportPlatformIOS::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  2095. #ifdef MACOS_ENABLED
  2096. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  2097. String can_export_error;
  2098. bool can_export_missing_templates;
  2099. if (!can_export(p_preset, can_export_error, can_export_missing_templates)) {
  2100. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error);
  2101. return ERR_UNCONFIGURED;
  2102. }
  2103. MutexLock lock(device_lock);
  2104. EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device].name), 3);
  2105. String id = "tmpexport." + uitos(OS::get_singleton()->get_unix_time());
  2106. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2107. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + EditorPaths::get_singleton()->get_cache_dir() + "'.");
  2108. filesystem_da->make_dir_recursive(EditorPaths::get_singleton()->get_cache_dir().path_join(id));
  2109. String tmp_export_path = EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.ipa");
  2110. #define CLEANUP_AND_RETURN(m_err) \
  2111. { \
  2112. if (filesystem_da->change_dir(EditorPaths::get_singleton()->get_cache_dir().path_join(id)) == OK) { \
  2113. filesystem_da->erase_contents_recursive(); \
  2114. filesystem_da->change_dir(".."); \
  2115. filesystem_da->remove(id); \
  2116. } \
  2117. return m_err; \
  2118. } \
  2119. ((void)0)
  2120. Device dev = devices[p_device];
  2121. // Export before sending to device.
  2122. Error err = _export_project_helper(p_preset, true, tmp_export_path, p_debug_flags, dev.simulator, true);
  2123. if (err != OK) {
  2124. CLEANUP_AND_RETURN(err);
  2125. }
  2126. Vector<String> cmd_args_list;
  2127. String host = EDITOR_GET("network/debug/remote_host");
  2128. int remote_port = (int)EDITOR_GET("network/debug/remote_port");
  2129. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST) {
  2130. host = "localhost";
  2131. }
  2132. if (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2133. int port = EDITOR_GET("filesystem/file_server/port");
  2134. String passwd = EDITOR_GET("filesystem/file_server/password");
  2135. cmd_args_list.push_back("--remote-fs");
  2136. cmd_args_list.push_back(host + ":" + itos(port));
  2137. if (!passwd.is_empty()) {
  2138. cmd_args_list.push_back("--remote-fs-password");
  2139. cmd_args_list.push_back(passwd);
  2140. }
  2141. }
  2142. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  2143. cmd_args_list.push_back("--remote-debug");
  2144. cmd_args_list.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));
  2145. List<String> breakpoints;
  2146. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  2147. if (breakpoints.size()) {
  2148. cmd_args_list.push_back("--breakpoints");
  2149. String bpoints;
  2150. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  2151. bpoints += E->get().replace(" ", "%20");
  2152. if (E->next()) {
  2153. bpoints += ",";
  2154. }
  2155. }
  2156. cmd_args_list.push_back(bpoints);
  2157. }
  2158. }
  2159. if (p_debug_flags & DEBUG_FLAG_VIEW_COLLISIONS) {
  2160. cmd_args_list.push_back("--debug-collisions");
  2161. }
  2162. if (p_debug_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  2163. cmd_args_list.push_back("--debug-navigation");
  2164. }
  2165. if (dev.simulator) {
  2166. // Deploy and run on simulator.
  2167. if (ep.step("Installing to simulator...", 3)) {
  2168. CLEANUP_AND_RETURN(ERR_SKIP);
  2169. } else {
  2170. List<String> args;
  2171. args.push_back("simctl");
  2172. args.push_back("install");
  2173. args.push_back(dev.id);
  2174. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2175. String log;
  2176. int ec;
  2177. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2178. if (err != OK) {
  2179. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2180. CLEANUP_AND_RETURN(err);
  2181. }
  2182. if (ec != 0) {
  2183. print_line("simctl install:\n" + log);
  2184. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2185. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2186. }
  2187. }
  2188. if (ep.step("Running on simulator...", 4)) {
  2189. CLEANUP_AND_RETURN(ERR_SKIP);
  2190. } else {
  2191. List<String> args;
  2192. args.push_back("simctl");
  2193. args.push_back("launch");
  2194. args.push_back("--terminate-running-process");
  2195. args.push_back(dev.id);
  2196. args.push_back(p_preset->get("application/bundle_identifier"));
  2197. for (const String &E : cmd_args_list) {
  2198. args.push_back(E);
  2199. }
  2200. String log;
  2201. int ec;
  2202. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2203. if (err != OK) {
  2204. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2205. CLEANUP_AND_RETURN(err);
  2206. }
  2207. if (ec != 0) {
  2208. print_line("simctl launch:\n" + log);
  2209. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2210. }
  2211. }
  2212. } else if (dev.use_ios_deploy) {
  2213. // Deploy and run on real device (via ios-deploy).
  2214. if (ep.step("Installing and running on device...", 4)) {
  2215. CLEANUP_AND_RETURN(ERR_SKIP);
  2216. } else {
  2217. List<String> args;
  2218. args.push_back("-u");
  2219. args.push_back("-I");
  2220. args.push_back("--id");
  2221. args.push_back(dev.id);
  2222. args.push_back("--justlaunch");
  2223. args.push_back("--bundle");
  2224. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2225. String app_args;
  2226. for (const String &E : cmd_args_list) {
  2227. app_args += E + " ";
  2228. }
  2229. if (!app_args.is_empty()) {
  2230. args.push_back("--args");
  2231. args.push_back(app_args);
  2232. }
  2233. String idepl = EDITOR_GET("export/ios/ios_deploy");
  2234. if (idepl.is_empty()) {
  2235. idepl = "ios-deploy";
  2236. }
  2237. String log;
  2238. int ec;
  2239. err = OS::get_singleton()->execute(idepl, args, &log, &ec, true);
  2240. if (err != OK) {
  2241. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start ios-deploy executable."));
  2242. CLEANUP_AND_RETURN(err);
  2243. }
  2244. if (ec != 0) {
  2245. print_line("ios-deploy:\n" + log);
  2246. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation/running failed, see editor log for details."));
  2247. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2248. }
  2249. }
  2250. } else {
  2251. // Deploy and run on real device.
  2252. if (ep.step("Installing to device...", 3)) {
  2253. CLEANUP_AND_RETURN(ERR_SKIP);
  2254. } else {
  2255. List<String> args;
  2256. args.push_back("devicectl");
  2257. args.push_back("device");
  2258. args.push_back("install");
  2259. args.push_back("app");
  2260. args.push_back("-d");
  2261. args.push_back(dev.id);
  2262. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2263. String log;
  2264. int ec;
  2265. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2266. if (err != OK) {
  2267. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start device executable."));
  2268. CLEANUP_AND_RETURN(err);
  2269. }
  2270. if (ec != 0) {
  2271. print_line("device install:\n" + log);
  2272. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2273. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2274. }
  2275. }
  2276. if (ep.step("Running on device...", 4)) {
  2277. CLEANUP_AND_RETURN(ERR_SKIP);
  2278. } else {
  2279. List<String> args;
  2280. args.push_back("devicectl");
  2281. args.push_back("device");
  2282. args.push_back("process");
  2283. args.push_back("launch");
  2284. args.push_back("--terminate-existing");
  2285. args.push_back("-d");
  2286. args.push_back(dev.id);
  2287. args.push_back(p_preset->get("application/bundle_identifier"));
  2288. for (const String &E : cmd_args_list) {
  2289. args.push_back(E);
  2290. }
  2291. String log;
  2292. int ec;
  2293. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2294. if (err != OK) {
  2295. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start devicectl executable."));
  2296. CLEANUP_AND_RETURN(err);
  2297. }
  2298. if (ec != 0) {
  2299. print_line("devicectl launch:\n" + log);
  2300. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2301. }
  2302. }
  2303. }
  2304. CLEANUP_AND_RETURN(OK);
  2305. #undef CLEANUP_AND_RETURN
  2306. #else
  2307. return ERR_UNCONFIGURED;
  2308. #endif
  2309. }
  2310. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  2311. if (EditorNode::get_singleton()) {
  2312. #ifdef MODULE_SVG_ENABLED
  2313. Ref<Image> img = memnew(Image);
  2314. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  2315. ImageLoaderSVG::create_image_from_string(img, _ios_logo_svg, EDSCALE, upsample, false);
  2316. logo = ImageTexture::create_from_image(img);
  2317. ImageLoaderSVG::create_image_from_string(img, _ios_run_icon_svg, EDSCALE, upsample, false);
  2318. run_icon = ImageTexture::create_from_image(img);
  2319. #endif
  2320. plugins_changed.set();
  2321. devices_changed.set();
  2322. #ifdef MACOS_ENABLED
  2323. _update_preset_status();
  2324. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  2325. #endif
  2326. }
  2327. }
  2328. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  2329. #ifdef MACOS_ENABLED
  2330. quit_request.set();
  2331. if (check_for_changes_thread.is_started()) {
  2332. check_for_changes_thread.wait_to_finish();
  2333. }
  2334. #endif
  2335. }