export_plugin.cpp 102 KB

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