2
0

export_plugin.cpp 104 KB

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