export_plugin.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /*************************************************************************/
  2. /* export_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  32. String driver = ProjectSettings::get_singleton()->get("rendering/driver/driver_name");
  33. r_features->push_back("pvrtc");
  34. if (driver == "Vulkan") {
  35. // FIXME: Review if this is correct.
  36. r_features->push_back("etc2");
  37. }
  38. Vector<String> architectures = _get_preset_architectures(p_preset);
  39. for (int i = 0; i < architectures.size(); ++i) {
  40. r_features->push_back(architectures[i]);
  41. }
  42. }
  43. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() {
  44. Vector<ExportArchitecture> archs;
  45. archs.push_back(ExportArchitecture("armv7", false)); // Disabled by default, not included in official templates.
  46. archs.push_back(ExportArchitecture("arm64", true));
  47. return archs;
  48. }
  49. struct LoadingScreenInfo {
  50. const char *preset_key;
  51. const char *export_name;
  52. int width = 0;
  53. int height = 0;
  54. bool rotate = false;
  55. };
  56. static const LoadingScreenInfo loading_screen_infos[] = {
  57. { "landscape_launch_screens/iphone_2436x1125", "Default-Landscape-X.png", 2436, 1125, false },
  58. { "landscape_launch_screens/iphone_2208x1242", "[email protected]", 2208, 1242, false },
  59. { "landscape_launch_screens/ipad_1024x768", "Default-Landscape.png", 1024, 768, false },
  60. { "landscape_launch_screens/ipad_2048x1536", "[email protected]", 2048, 1536, false },
  61. { "portrait_launch_screens/iphone_640x960", "[email protected]", 640, 960, true },
  62. { "portrait_launch_screens/iphone_640x1136", "[email protected]", 640, 1136, true },
  63. { "portrait_launch_screens/iphone_750x1334", "[email protected]", 750, 1334, true },
  64. { "portrait_launch_screens/iphone_1125x2436", "Default-Portrait-X.png", 1125, 2436, true },
  65. { "portrait_launch_screens/ipad_768x1024", "Default-Portrait.png", 768, 1024, true },
  66. { "portrait_launch_screens/ipad_1536x2048", "[email protected]", 1536, 2048, true },
  67. { "portrait_launch_screens/iphone_1242x2208", "[email protected]", 1242, 2208, true }
  68. };
  69. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) {
  70. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  72. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  73. for (int i = 0; i < architectures.size(); ++i) {
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architectures/" + architectures[i].name), architectures[i].is_default));
  75. }
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), ""));
  77. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug"), ""));
  78. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), "iPhone Developer"));
  79. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  80. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release"), ""));
  81. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  82. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  83. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  84. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  85. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  86. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  87. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  88. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  89. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  91. Vector<PluginConfigIOS> found_plugins = get_plugins();
  92. for (int i = 0; i < found_plugins.size(); i++) {
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "plugins/" + found_plugins[i].name), false));
  94. }
  95. Set<String> plist_keys;
  96. for (int i = 0; i < found_plugins.size(); i++) {
  97. // Editable plugin plist values
  98. PluginConfigIOS plugin = found_plugins[i];
  99. const String *K = nullptr;
  100. while ((K = plugin.plist.next(K))) {
  101. String key = *K;
  102. PluginConfigIOS::PlistItem item = plugin.plist[key];
  103. switch (item.type) {
  104. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  105. String preset_name = "plugins_plist/" + key;
  106. if (!plist_keys.has(preset_name)) {
  107. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, preset_name), item.value));
  108. plist_keys.insert(preset_name);
  109. }
  110. } break;
  111. default:
  112. continue;
  113. }
  114. }
  115. }
  116. plugins_changed.clear();
  117. plugins = found_plugins;
  118. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  122. 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"), ""));
  123. 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"), ""));
  124. 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"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "icons/generate_missing"), false));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/iphone_120x120", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone/iPod Touch with retina display
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/ipad_76x76", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/app_store_1024x1024", PROPERTY_HINT_FILE, "*.png"), "")); // App Store
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/iphone_180x180", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone with retina HD display
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_152x152", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad with retina display
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_167x167", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad Pro
  132. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_40x40", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight
  133. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_80x80", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight on devices with retina display
  134. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_launch_screen_storyboard"), false));
  135. 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));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE, "*.png"), ""));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE, "*.png"), ""));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "launch_screens/generate_missing"), false));
  141. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  142. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, loading_screen_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png"), ""));
  143. }
  144. }
  145. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  146. static const String export_method_string[] = {
  147. "app-store",
  148. "development",
  149. "ad-hoc",
  150. "enterprise"
  151. };
  152. static const String storyboard_image_scale_mode[] = {
  153. "center",
  154. "scaleAspectFit",
  155. "scaleAspectFill",
  156. "scaleToFill"
  157. };
  158. String str;
  159. String strnew;
  160. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  161. Vector<String> lines = str.split("\n");
  162. for (int i = 0; i < lines.size(); i++) {
  163. if (lines[i].find("$binary") != -1) {
  164. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  165. } else if (lines[i].find("$modules_buildfile") != -1) {
  166. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  167. } else if (lines[i].find("$modules_fileref") != -1) {
  168. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  169. } else if (lines[i].find("$modules_buildphase") != -1) {
  170. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  171. } else if (lines[i].find("$modules_buildgrp") != -1) {
  172. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  173. } else if (lines[i].find("$name") != -1) {
  174. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  175. } else if (lines[i].find("$info") != -1) {
  176. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  177. } else if (lines[i].find("$bundle_identifier") != -1) {
  178. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  179. } else if (lines[i].find("$short_version") != -1) {
  180. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  181. } else if (lines[i].find("$version") != -1) {
  182. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  183. } else if (lines[i].find("$signature") != -1) {
  184. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  185. } else if (lines[i].find("$copyright") != -1) {
  186. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  187. } else if (lines[i].find("$team_id") != -1) {
  188. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  189. } else if (lines[i].find("$default_build_config") != -1) {
  190. strnew += lines[i].replace("$default_build_config", p_debug ? "Debug" : "Release") + "\n";
  191. } else if (lines[i].find("$export_method") != -1) {
  192. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  193. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  194. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  195. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get("application/provisioning_profile_uuid_release")) + "\n";
  196. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  197. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get("application/provisioning_profile_uuid_debug")) + "\n";
  198. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  199. String uuid = p_debug ? p_preset->get("application/provisioning_profile_uuid_debug") : p_preset->get("application/provisioning_profile_uuid_release");
  200. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  201. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  202. strnew += lines[i].replace("$code_sign_identity_debug", p_preset->get("application/code_sign_identity_debug")) + "\n";
  203. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  204. strnew += lines[i].replace("$code_sign_identity_release", p_preset->get("application/code_sign_identity_release")) + "\n";
  205. } else if (lines[i].find("$additional_plist_content") != -1) {
  206. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  207. } else if (lines[i].find("$godot_archs") != -1) {
  208. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  209. } else if (lines[i].find("$linker_flags") != -1) {
  210. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  211. } else if (lines[i].find("$targeted_device_family") != -1) {
  212. String xcode_value;
  213. switch ((int)p_preset->get("application/targeted_device_family")) {
  214. case 0: // iPhone
  215. xcode_value = "1";
  216. break;
  217. case 1: // iPad
  218. xcode_value = "2";
  219. break;
  220. case 2: // iPhone & iPad
  221. xcode_value = "1,2";
  222. break;
  223. }
  224. strnew += lines[i].replace("$targeted_device_family", xcode_value) + "\n";
  225. } else if (lines[i].find("$cpp_code") != -1) {
  226. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  227. } else if (lines[i].find("$docs_in_place") != -1) {
  228. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  229. } else if (lines[i].find("$docs_sharing") != -1) {
  230. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  231. } else if (lines[i].find("$entitlements_push_notifications") != -1) {
  232. bool is_on = p_preset->get("capabilities/push_notifications");
  233. strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n";
  234. } else if (lines[i].find("$required_device_capabilities") != -1) {
  235. String capabilities;
  236. // I've removed armv7 as we can run on 64bit only devices
  237. // Note that capabilities listed here are requirements for the app to be installed.
  238. // They don't enable anything.
  239. Vector<String> capabilities_list = p_config.capabilities;
  240. if ((bool)p_preset->get("capabilities/access_wifi") && !capabilities_list.has("wifi")) {
  241. capabilities_list.push_back("wifi");
  242. }
  243. for (int idx = 0; idx < capabilities_list.size(); idx++) {
  244. capabilities += "<string>" + capabilities_list[idx] + "</string>\n";
  245. }
  246. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  247. } else if (lines[i].find("$interface_orientations") != -1) {
  248. String orientations;
  249. const DisplayServer::ScreenOrientation screen_orientation =
  250. DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")));
  251. switch (screen_orientation) {
  252. case DisplayServer::SCREEN_LANDSCAPE:
  253. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  254. break;
  255. case DisplayServer::SCREEN_PORTRAIT:
  256. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  257. break;
  258. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  259. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  260. break;
  261. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  262. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  263. break;
  264. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  265. // Allow both landscape orientations depending on sensor direction.
  266. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  267. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  268. break;
  269. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  270. // Allow both portrait orientations depending on sensor direction.
  271. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  272. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  273. break;
  274. case DisplayServer::SCREEN_SENSOR:
  275. // Allow all screen orientations depending on sensor direction.
  276. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  277. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  278. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  279. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  280. break;
  281. }
  282. strnew += lines[i].replace("$interface_orientations", orientations);
  283. } else if (lines[i].find("$camera_usage_description") != -1) {
  284. String description = p_preset->get("privacy/camera_usage_description");
  285. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  286. } else if (lines[i].find("$microphone_usage_description") != -1) {
  287. String description = p_preset->get("privacy/microphone_usage_description");
  288. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  289. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  290. String description = p_preset->get("privacy/photolibrary_usage_description");
  291. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  292. } else if (lines[i].find("$plist_launch_screen_name") != -1) {
  293. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  294. String value = is_on ? "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>" : "";
  295. strnew += lines[i].replace("$plist_launch_screen_name", value) + "\n";
  296. } else if (lines[i].find("$pbx_launch_screen_file_reference") != -1) {
  297. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  298. String value = is_on ? "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };" : "";
  299. strnew += lines[i].replace("$pbx_launch_screen_file_reference", value) + "\n";
  300. } else if (lines[i].find("$pbx_launch_screen_copy_files") != -1) {
  301. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  302. String value = is_on ? "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */," : "";
  303. strnew += lines[i].replace("$pbx_launch_screen_copy_files", value) + "\n";
  304. } else if (lines[i].find("$pbx_launch_screen_build_phase") != -1) {
  305. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  306. String value = is_on ? "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */," : "";
  307. strnew += lines[i].replace("$pbx_launch_screen_build_phase", value) + "\n";
  308. } else if (lines[i].find("$pbx_launch_screen_build_reference") != -1) {
  309. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  310. String value = is_on ? "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };" : "";
  311. strnew += lines[i].replace("$pbx_launch_screen_build_reference", value) + "\n";
  312. } else if (lines[i].find("$pbx_launch_image_usage_setting") != -1) {
  313. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  314. String value = is_on ? "" : "ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;";
  315. strnew += lines[i].replace("$pbx_launch_image_usage_setting", value) + "\n";
  316. } else if (lines[i].find("$launch_screen_image_mode") != -1) {
  317. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  318. String value;
  319. switch (image_scale_mode) {
  320. case 0: {
  321. String logo_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  322. bool is_on = ProjectSettings::get_singleton()->get("application/boot_splash/fullsize");
  323. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  324. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  325. } break;
  326. default: {
  327. value = storyboard_image_scale_mode[image_scale_mode - 1];
  328. }
  329. }
  330. strnew += lines[i].replace("$launch_screen_image_mode", value) + "\n";
  331. } else if (lines[i].find("$launch_screen_background_color") != -1) {
  332. bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
  333. Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : ProjectSettings::get_singleton()->get("application/boot_splash/bg_color");
  334. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  335. Dictionary value_dictionary;
  336. value_dictionary["red"] = color.r;
  337. value_dictionary["green"] = color.g;
  338. value_dictionary["blue"] = color.b;
  339. value_dictionary["alpha"] = color.a;
  340. String value = value_format.format(value_dictionary, "$_");
  341. strnew += lines[i].replace("$launch_screen_background_color", value) + "\n";
  342. } else {
  343. strnew += lines[i] + "\n";
  344. }
  345. }
  346. // !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...
  347. // should apply the same fix in our OSX export.
  348. CharString cs = strnew.utf8();
  349. pfile.resize(cs.size() - 1);
  350. for (int i = 0; i < cs.size() - 1; i++) {
  351. pfile.write[i] = cs[i];
  352. }
  353. }
  354. String EditorExportPlatformIOS::_get_additional_plist_content() {
  355. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  356. String result;
  357. for (int i = 0; i < export_plugins.size(); ++i) {
  358. result += export_plugins[i]->get_ios_plist_content();
  359. }
  360. return result;
  361. }
  362. String EditorExportPlatformIOS::_get_linker_flags() {
  363. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  364. String result;
  365. for (int i = 0; i < export_plugins.size(); ++i) {
  366. String flags = export_plugins[i]->get_ios_linker_flags();
  367. if (flags.length() == 0) {
  368. continue;
  369. }
  370. if (result.length() > 0) {
  371. result += ' ';
  372. }
  373. result += flags;
  374. }
  375. // the flags will be enclosed in quotes, so need to escape them
  376. return result.replace("\"", "\\\"");
  377. }
  378. String EditorExportPlatformIOS::_get_cpp_code() {
  379. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  380. String result;
  381. for (int i = 0; i < export_plugins.size(); ++i) {
  382. result += export_plugins[i]->get_ios_cpp_code();
  383. }
  384. return result;
  385. }
  386. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  387. ERR_FAIL_COND(p_dst.is_null());
  388. ERR_FAIL_COND(p_src.is_null());
  389. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  390. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  391. int x_pos = (p_dst->get_width() - sw) / 2;
  392. int y_pos = (p_dst->get_height() - sh) / 2;
  393. int xs = (x_pos >= 0) ? 0 : -x_pos;
  394. int ys = (y_pos >= 0) ? 0 : -y_pos;
  395. if (sw + x_pos > p_dst->get_width()) {
  396. sw = p_dst->get_width() - x_pos;
  397. }
  398. if (sh + y_pos > p_dst->get_height()) {
  399. sh = p_dst->get_height() - y_pos;
  400. }
  401. for (int y = ys; y < sh; y++) {
  402. for (int x = xs; x < sw; x++) {
  403. Color sc = p_rot ? p_src->get_pixel(p_src->get_width() - y - 1, x) : p_src->get_pixel(x, y);
  404. Color dc = p_dst->get_pixel(x_pos + x, y_pos + y);
  405. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  406. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  407. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  408. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  409. p_dst->set_pixel(x_pos + x, y_pos + y, dc);
  410. }
  411. }
  412. }
  413. struct IconInfo {
  414. const char *preset_key;
  415. const char *idiom;
  416. const char *export_name;
  417. const char *actual_size_side;
  418. const char *scale;
  419. const char *unscaled_size;
  420. bool is_required = false;
  421. };
  422. static const IconInfo icon_infos[] = {
  423. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "2x", "60x60", true },
  424. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "3x", "40x40", true },
  425. { "required_icons/ipad_76x76", "ipad", "Icon-76.png", "76", "1x", "76x76", true },
  426. { "required_icons/app_store_1024x1024", "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", true },
  427. { "optional_icons/iphone_180x180", "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  428. { "optional_icons/ipad_152x152", "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  429. { "optional_icons/ipad_167x167", "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  430. { "optional_icons/spotlight_40x40", "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  431. { "optional_icons/spotlight_80x80", "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  432. { "optional_icons/spotlight_80x80", "ipad", "Icon-80.png", "80", "2x", "40x40", false }
  433. };
  434. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  435. String json_description = "{\"images\":[";
  436. String sizes;
  437. DirAccess *da = DirAccess::open(p_iconset_dir);
  438. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_iconset_dir + "'.");
  439. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  440. IconInfo info = icon_infos[i];
  441. int side_size = String(info.actual_size_side).to_int();
  442. String icon_path = p_preset->get(info.preset_key);
  443. if (icon_path.length() == 0) {
  444. if ((bool)p_preset->get("icons/generate_missing")) {
  445. // Resize main app icon
  446. icon_path = ProjectSettings::get_singleton()->get("application/config/icon");
  447. Ref<Image> img = memnew(Image);
  448. Error err = ImageLoader::load_image(icon_path, img);
  449. if (err != OK) {
  450. ERR_PRINT("Invalid icon (" + String(info.preset_key) + "): '" + icon_path + "'.");
  451. return ERR_UNCONFIGURED;
  452. }
  453. img->resize(side_size, side_size);
  454. err = img->save_png(p_iconset_dir + info.export_name);
  455. if (err) {
  456. String err_str = String("Failed to export icon(" + String(info.preset_key) + "): '" + icon_path + "'.");
  457. ERR_PRINT(err_str.utf8().get_data());
  458. return err;
  459. }
  460. } else {
  461. if (info.is_required) {
  462. String err_str = String("Required icon (") + info.preset_key + ") is not specified in the preset.";
  463. ERR_PRINT(err_str);
  464. return ERR_UNCONFIGURED;
  465. } else {
  466. String err_str = String("Icon (") + info.preset_key + ") is not specified in the preset.";
  467. WARN_PRINT(err_str);
  468. }
  469. continue;
  470. }
  471. } else {
  472. // Load custom icon
  473. Ref<Image> img = memnew(Image);
  474. Error err = ImageLoader::load_image(icon_path, img);
  475. if (err != OK) {
  476. ERR_PRINT("Invalid icon (" + String(info.preset_key) + "): '" + icon_path + "'.");
  477. return ERR_UNCONFIGURED;
  478. }
  479. if (img->get_width() != side_size || img->get_height() != side_size) {
  480. ERR_PRINT("Invalid icon size (" + String(info.preset_key) + "): '" + icon_path + "'.");
  481. return ERR_UNCONFIGURED;
  482. }
  483. err = da->copy(icon_path, p_iconset_dir + info.export_name);
  484. if (err) {
  485. memdelete(da);
  486. String err_str = String("Failed to export icon(" + String(info.preset_key) + "): '" + icon_path + "'.");
  487. ERR_PRINT(err_str.utf8().get_data());
  488. return err;
  489. }
  490. }
  491. sizes += String(info.actual_size_side) + "\n";
  492. if (i > 0) {
  493. json_description += ",";
  494. }
  495. json_description += String("{");
  496. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  497. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  498. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  499. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  500. json_description += String("}");
  501. }
  502. json_description += "]}";
  503. memdelete(da);
  504. FileAccess *json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  505. ERR_FAIL_COND_V(!json_file, ERR_CANT_CREATE);
  506. CharString json_utf8 = json_description.utf8();
  507. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  508. memdelete(json_file);
  509. FileAccess *sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  510. ERR_FAIL_COND_V(!sizes_file, ERR_CANT_CREATE);
  511. CharString sizes_utf8 = sizes.utf8();
  512. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  513. memdelete(sizes_file);
  514. return OK;
  515. }
  516. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  517. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  518. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  519. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  520. Ref<Image> image;
  521. String image_path = p_dest_dir.plus_file("[email protected]");
  522. image.instantiate();
  523. Error err = image->load(custom_launch_image_2x);
  524. if (err) {
  525. image.unref();
  526. return err;
  527. }
  528. if (image->save_png(image_path) != OK) {
  529. return ERR_FILE_CANT_WRITE;
  530. }
  531. image.unref();
  532. image_path = p_dest_dir.plus_file("[email protected]");
  533. image.instantiate();
  534. err = image->load(custom_launch_image_3x);
  535. if (err) {
  536. image.unref();
  537. return err;
  538. }
  539. if (image->save_png(image_path) != OK) {
  540. return ERR_FILE_CANT_WRITE;
  541. }
  542. } else {
  543. Ref<Image> splash;
  544. const String splash_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  545. if (!splash_path.is_empty()) {
  546. splash.instantiate();
  547. const Error err = splash->load(splash_path);
  548. if (err) {
  549. splash.unref();
  550. }
  551. }
  552. if (splash.is_null()) {
  553. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  554. }
  555. // Using same image for both @2x and @3x
  556. // because Godot's own boot logo uses single image for all resolutions.
  557. // Also not using @1x image, because devices using this image variant
  558. // are not supported by iOS 9, which is minimal target.
  559. const String splash_png_path_2x = p_dest_dir.plus_file("[email protected]");
  560. const String splash_png_path_3x = p_dest_dir.plus_file("[email protected]");
  561. if (splash->save_png(splash_png_path_2x) != OK) {
  562. return ERR_FILE_CANT_WRITE;
  563. }
  564. if (splash->save_png(splash_png_path_3x) != OK) {
  565. return ERR_FILE_CANT_WRITE;
  566. }
  567. }
  568. return OK;
  569. }
  570. Error EditorExportPlatformIOS::_export_loading_screen_images(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  571. DirAccess *da = DirAccess::open(p_dest_dir);
  572. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_dest_dir + "'.");
  573. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  574. LoadingScreenInfo info = loading_screen_infos[i];
  575. String loading_screen_file = p_preset->get(info.preset_key);
  576. if (loading_screen_file.size() > 0) {
  577. // Load custom loading screens
  578. Ref<Image> img = memnew(Image);
  579. Error err = ImageLoader::load_image(loading_screen_file, img);
  580. if (err != OK) {
  581. ERR_PRINT("Invalid loading screen (" + String(info.preset_key) + "): '" + loading_screen_file + "'.");
  582. return ERR_UNCONFIGURED;
  583. }
  584. if (img->get_width() != info.width || img->get_height() != info.height) {
  585. ERR_PRINT("Invalid loading screen size (" + String(info.preset_key) + "): '" + loading_screen_file + "'.");
  586. return ERR_UNCONFIGURED;
  587. }
  588. err = da->copy(loading_screen_file, p_dest_dir + info.export_name);
  589. if (err) {
  590. memdelete(da);
  591. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from path '" + loading_screen_file + "'.";
  592. ERR_PRINT(err_str.utf8().get_data());
  593. return err;
  594. }
  595. } else if ((bool)p_preset->get("launch_screens/generate_missing")) {
  596. // Generate loading screen from the splash screen
  597. Color boot_bg_color = ProjectSettings::get_singleton()->get("application/boot_splash/bg_color");
  598. String boot_logo_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  599. bool boot_logo_scale = ProjectSettings::get_singleton()->get("application/boot_splash/fullsize");
  600. Ref<Image> img = memnew(Image);
  601. img->create(info.width, info.height, false, Image::FORMAT_RGBA8);
  602. img->fill(boot_bg_color);
  603. Ref<Image> img_bs;
  604. if (boot_logo_path.length() > 0) {
  605. img_bs = Ref<Image>(memnew(Image));
  606. ImageLoader::load_image(boot_logo_path, img_bs);
  607. }
  608. if (!img_bs.is_valid()) {
  609. img_bs = Ref<Image>(memnew(Image(boot_splash_png)));
  610. }
  611. if (img_bs.is_valid()) {
  612. float aspect_ratio = (float)img_bs->get_width() / (float)img_bs->get_height();
  613. if (info.rotate) {
  614. if (boot_logo_scale) {
  615. if (info.width * aspect_ratio <= info.height) {
  616. img_bs->resize(info.width * aspect_ratio, info.width);
  617. } else {
  618. img_bs->resize(info.height, info.height / aspect_ratio);
  619. }
  620. }
  621. } else {
  622. if (boot_logo_scale) {
  623. if (info.height * aspect_ratio <= info.width) {
  624. img_bs->resize(info.height * aspect_ratio, info.height);
  625. } else {
  626. img_bs->resize(info.width, info.width / aspect_ratio);
  627. }
  628. }
  629. }
  630. _blend_and_rotate(img, img_bs, info.rotate);
  631. }
  632. Error err = img->save_png(p_dest_dir + info.export_name);
  633. if (err) {
  634. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from splash screen.";
  635. WARN_PRINT(err_str.utf8().get_data());
  636. }
  637. } else {
  638. String err_str = String("No loading screen (") + info.preset_key + ") specified.";
  639. WARN_PRINT(err_str.utf8().get_data());
  640. }
  641. }
  642. memdelete(da);
  643. return OK;
  644. }
  645. Error EditorExportPlatformIOS::_walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata) {
  646. Vector<String> dirs;
  647. String path;
  648. String current_dir = p_da->get_current_dir();
  649. p_da->list_dir_begin();
  650. while ((path = p_da->get_next()).length() != 0) {
  651. if (p_da->current_is_dir()) {
  652. if (path != "." && path != "..") {
  653. dirs.push_back(path);
  654. }
  655. } else {
  656. Error err = p_handler(current_dir.plus_file(path), p_userdata);
  657. if (err) {
  658. p_da->list_dir_end();
  659. return err;
  660. }
  661. }
  662. }
  663. p_da->list_dir_end();
  664. for (int i = 0; i < dirs.size(); ++i) {
  665. String dir = dirs[i];
  666. p_da->change_dir(dir);
  667. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  668. p_da->change_dir("..");
  669. if (err) {
  670. return err;
  671. }
  672. }
  673. return OK;
  674. }
  675. struct CodesignData {
  676. const Ref<EditorExportPreset> &preset;
  677. bool debug = false;
  678. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  679. preset(p_preset),
  680. debug(p_debug) {
  681. }
  682. };
  683. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  684. if (p_file.ends_with(".dylib")) {
  685. CodesignData *data = (CodesignData *)p_userdata;
  686. print_line(String("Signing ") + p_file);
  687. List<String> codesign_args;
  688. codesign_args.push_back("-f");
  689. codesign_args.push_back("-s");
  690. codesign_args.push_back(data->preset->get(data->debug ? "application/code_sign_identity_debug" : "application/code_sign_identity_release"));
  691. codesign_args.push_back(p_file);
  692. return OS::get_singleton()->execute("codesign", codesign_args);
  693. }
  694. return OK;
  695. }
  696. struct PbxId {
  697. private:
  698. static char _hex_char(uint8_t four_bits) {
  699. if (four_bits < 10) {
  700. return ('0' + four_bits);
  701. }
  702. return 'A' + (four_bits - 10);
  703. }
  704. static String _hex_pad(uint32_t num) {
  705. Vector<char> ret;
  706. ret.resize(sizeof(num) * 2);
  707. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  708. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  709. ret.write[i] = _hex_char(four_bits);
  710. }
  711. return String::utf8(ret.ptr(), ret.size());
  712. }
  713. public:
  714. uint32_t high_bits;
  715. uint32_t mid_bits;
  716. uint32_t low_bits;
  717. String str() const {
  718. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  719. }
  720. PbxId &operator++() {
  721. low_bits++;
  722. if (!low_bits) {
  723. mid_bits++;
  724. if (!mid_bits) {
  725. high_bits++;
  726. }
  727. }
  728. return *this;
  729. }
  730. };
  731. struct ExportLibsData {
  732. Vector<String> lib_paths;
  733. String dest_dir;
  734. };
  735. void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  736. // that is just a random number, we just need Godot IDs not to clash with
  737. // existing IDs in the project.
  738. PbxId current_id = { 0x58938401, 0, 0 };
  739. String pbx_files;
  740. String pbx_frameworks_build;
  741. String pbx_frameworks_refs;
  742. String pbx_resources_build;
  743. String pbx_resources_refs;
  744. String pbx_embeded_frameworks;
  745. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  746. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = \"$name\"; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  747. for (int i = 0; i < p_additional_assets.size(); ++i) {
  748. String additional_asset_info_format = file_info_format;
  749. String build_id = (++current_id).str();
  750. String ref_id = (++current_id).str();
  751. String framework_id = "";
  752. const IOSExportAsset &asset = p_additional_assets[i];
  753. String type;
  754. if (asset.exported_path.ends_with(".framework")) {
  755. if (asset.should_embed) {
  756. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  757. framework_id = (++current_id).str();
  758. pbx_embeded_frameworks += framework_id + ",\n";
  759. }
  760. type = "wrapper.framework";
  761. } else if (asset.exported_path.ends_with(".xcframework")) {
  762. if (asset.should_embed) {
  763. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  764. framework_id = (++current_id).str();
  765. pbx_embeded_frameworks += framework_id + ",\n";
  766. }
  767. type = "wrapper.xcframework";
  768. } else if (asset.exported_path.ends_with(".dylib")) {
  769. type = "compiled.mach-o.dylib";
  770. } else if (asset.exported_path.ends_with(".a")) {
  771. type = "archive.ar";
  772. } else {
  773. type = "file";
  774. }
  775. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  776. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  777. if (pbx_build.length() > 0) {
  778. pbx_build += ",\n";
  779. pbx_refs += ",\n";
  780. }
  781. pbx_build += build_id;
  782. pbx_refs += ref_id;
  783. Dictionary format_dict;
  784. format_dict["build_id"] = build_id;
  785. format_dict["ref_id"] = ref_id;
  786. format_dict["name"] = asset.exported_path.get_file();
  787. format_dict["file_path"] = asset.exported_path;
  788. format_dict["file_type"] = type;
  789. if (framework_id.length() > 0) {
  790. format_dict["framework_id"] = framework_id;
  791. }
  792. pbx_files += additional_asset_info_format.format(format_dict, "$_");
  793. }
  794. // Note, frameworks like gamekit are always included in our project.pbxprof file
  795. // even if turned off in capabilities.
  796. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  797. str = str.replace("$additional_pbx_files", pbx_files);
  798. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  799. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  800. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  801. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  802. str = str.replace("$pbx_embeded_frameworks", pbx_embeded_frameworks);
  803. CharString cs = str.utf8();
  804. p_project_data.resize(cs.size() - 1);
  805. for (int i = 0; i < cs.size() - 1; i++) {
  806. p_project_data.write[i] = cs[i];
  807. }
  808. }
  809. Error EditorExportPlatformIOS::_copy_asset(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) {
  810. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  811. ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  812. String binary_name = p_out_dir.get_file().get_basename();
  813. DirAccess *da = DirAccess::create_for_path(p_asset);
  814. if (!da) {
  815. memdelete(filesystem_da);
  816. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't create directory: " + p_asset + ".");
  817. }
  818. bool file_exists = da->file_exists(p_asset);
  819. bool dir_exists = da->dir_exists(p_asset);
  820. if (!file_exists && !dir_exists) {
  821. memdelete(da);
  822. memdelete(filesystem_da);
  823. return ERR_FILE_NOT_FOUND;
  824. }
  825. String base_dir = p_asset.get_base_dir().replace("res://", "");
  826. String destination_dir;
  827. String destination;
  828. String asset_path;
  829. bool create_framework = false;
  830. if (p_is_framework && p_asset.ends_with(".dylib")) {
  831. // For iOS we need to turn .dylib into .framework
  832. // to be able to send application to AppStore
  833. asset_path = String("dylibs").plus_file(base_dir);
  834. String file_name;
  835. if (!p_custom_file_name) {
  836. file_name = p_asset.get_basename().get_file();
  837. } else {
  838. file_name = *p_custom_file_name;
  839. }
  840. String framework_name = file_name + ".framework";
  841. asset_path = asset_path.plus_file(framework_name);
  842. destination_dir = p_out_dir.plus_file(asset_path);
  843. destination = destination_dir.plus_file(file_name);
  844. create_framework = true;
  845. } else if (p_is_framework && (p_asset.ends_with(".framework") || p_asset.ends_with(".xcframework"))) {
  846. asset_path = String("dylibs").plus_file(base_dir);
  847. String file_name;
  848. if (!p_custom_file_name) {
  849. file_name = p_asset.get_file();
  850. } else {
  851. file_name = *p_custom_file_name;
  852. }
  853. asset_path = asset_path.plus_file(file_name);
  854. destination_dir = p_out_dir.plus_file(asset_path);
  855. destination = destination_dir;
  856. } else {
  857. asset_path = base_dir;
  858. String file_name;
  859. if (!p_custom_file_name) {
  860. file_name = p_asset.get_file();
  861. } else {
  862. file_name = *p_custom_file_name;
  863. }
  864. destination_dir = p_out_dir.plus_file(asset_path);
  865. asset_path = asset_path.plus_file(file_name);
  866. destination = p_out_dir.plus_file(asset_path);
  867. }
  868. if (!filesystem_da->dir_exists(destination_dir)) {
  869. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  870. if (make_dir_err) {
  871. memdelete(da);
  872. memdelete(filesystem_da);
  873. return make_dir_err;
  874. }
  875. }
  876. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  877. memdelete(da);
  878. if (err) {
  879. memdelete(filesystem_da);
  880. return err;
  881. }
  882. IOSExportAsset exported_asset = { binary_name.plus_file(asset_path), p_is_framework, p_should_embed };
  883. r_exported_assets.push_back(exported_asset);
  884. if (create_framework) {
  885. String file_name;
  886. if (!p_custom_file_name) {
  887. file_name = p_asset.get_basename().get_file();
  888. } else {
  889. file_name = *p_custom_file_name;
  890. }
  891. String framework_name = file_name + ".framework";
  892. // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib
  893. {
  894. List<String> install_name_args;
  895. install_name_args.push_back("-id");
  896. install_name_args.push_back(String("@rpath").plus_file(framework_name).plus_file(file_name));
  897. install_name_args.push_back(destination);
  898. OS::get_singleton()->execute("install_name_tool", install_name_args);
  899. }
  900. // Creating Info.plist
  901. {
  902. String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  903. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  904. "<plist version=\"1.0\">\n"
  905. "<dict>\n"
  906. "<key>CFBundleShortVersionString</key>\n"
  907. "<string>1.0</string>\n"
  908. "<key>CFBundleIdentifier</key>\n"
  909. "<string>com.gdnative.framework.$name</string>\n"
  910. "<key>CFBundleName</key>\n"
  911. "<string>$name</string>\n"
  912. "<key>CFBundleExecutable</key>\n"
  913. "<string>$name</string>\n"
  914. "<key>DTPlatformName</key>\n"
  915. "<string>iphoneos</string>\n"
  916. "<key>CFBundleInfoDictionaryVersion</key>\n"
  917. "<string>6.0</string>\n"
  918. "<key>CFBundleVersion</key>\n"
  919. "<string>1</string>\n"
  920. "<key>CFBundlePackageType</key>\n"
  921. "<string>FMWK</string>\n"
  922. "<key>MinimumOSVersion</key>\n"
  923. "<string>10.0</string>\n"
  924. "</dict>\n"
  925. "</plist>";
  926. String info_plist = info_plist_format.replace("$name", file_name);
  927. FileAccess *f = FileAccess::open(destination_dir.plus_file("Info.plist"), FileAccess::WRITE);
  928. if (f) {
  929. f->store_string(info_plist);
  930. f->close();
  931. memdelete(f);
  932. }
  933. }
  934. }
  935. memdelete(filesystem_da);
  936. return OK;
  937. }
  938. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) {
  939. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  940. String asset = p_assets[f_idx];
  941. if (!asset.begins_with("res://")) {
  942. // either SDK-builtin or already a part of the export template
  943. IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed };
  944. r_exported_assets.push_back(exported_asset);
  945. } else {
  946. Error err = _copy_asset(p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets);
  947. ERR_FAIL_COND_V(err, err);
  948. }
  949. }
  950. return OK;
  951. }
  952. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  953. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  954. for (int i = 0; i < export_plugins.size(); i++) {
  955. Vector<String> linked_frameworks = export_plugins[i]->get_ios_frameworks();
  956. Error err = _export_additional_assets(p_out_dir, linked_frameworks, true, false, r_exported_assets);
  957. ERR_FAIL_COND_V(err, err);
  958. Vector<String> embedded_frameworks = export_plugins[i]->get_ios_embedded_frameworks();
  959. err = _export_additional_assets(p_out_dir, embedded_frameworks, true, true, r_exported_assets);
  960. ERR_FAIL_COND_V(err, err);
  961. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  962. for (int j = 0; j < project_static_libs.size(); j++) {
  963. project_static_libs.write[j] = project_static_libs[j].get_file(); // Only the file name as it's copied to the project
  964. }
  965. err = _export_additional_assets(p_out_dir, project_static_libs, true, true, r_exported_assets);
  966. ERR_FAIL_COND_V(err, err);
  967. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  968. err = _export_additional_assets(p_out_dir, ios_bundle_files, false, false, r_exported_assets);
  969. ERR_FAIL_COND_V(err, err);
  970. }
  971. Vector<String> library_paths;
  972. for (int i = 0; i < p_libraries.size(); ++i) {
  973. library_paths.push_back(p_libraries[i].path);
  974. }
  975. Error err = _export_additional_assets(p_out_dir, library_paths, true, true, r_exported_assets);
  976. ERR_FAIL_COND_V(err, err);
  977. return OK;
  978. }
  979. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) {
  980. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  981. Vector<String> enabled_archs;
  982. for (int i = 0; i < all_archs.size(); ++i) {
  983. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  984. if (is_enabled) {
  985. enabled_archs.push_back(all_archs[i].name);
  986. }
  987. }
  988. return enabled_archs;
  989. }
  990. 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) {
  991. String plugin_definition_cpp_code;
  992. String plugin_initialization_cpp_code;
  993. String plugin_deinitialization_cpp_code;
  994. Vector<String> plugin_linked_dependencies;
  995. Vector<String> plugin_embedded_dependencies;
  996. Vector<String> plugin_files;
  997. Vector<PluginConfigIOS> enabled_plugins = get_enabled_plugins(p_preset);
  998. Vector<String> added_linked_dependenciy_names;
  999. Vector<String> added_embedded_dependenciy_names;
  1000. HashMap<String, String> plist_values;
  1001. Set<String> plugin_linker_flags;
  1002. Error err;
  1003. for (int i = 0; i < enabled_plugins.size(); i++) {
  1004. PluginConfigIOS plugin = enabled_plugins[i];
  1005. // Export plugin binary.
  1006. String plugin_main_binary = PluginConfigIOS::get_plugin_main_binary(plugin, p_debug);
  1007. String plugin_binary_result_file = plugin.binary.get_file();
  1008. // We shouldn't embed .xcframework that contains static libraries.
  1009. // Static libraries are not embedded anyway.
  1010. err = _copy_asset(dest_dir, plugin_main_binary, &plugin_binary_result_file, true, false, r_exported_assets);
  1011. ERR_FAIL_COND_V(err, err);
  1012. // Adding dependencies.
  1013. // Use separate container for names to check for duplicates.
  1014. for (int j = 0; j < plugin.linked_dependencies.size(); j++) {
  1015. String dependency = plugin.linked_dependencies[j];
  1016. String name = dependency.get_file();
  1017. if (added_linked_dependenciy_names.has(name)) {
  1018. continue;
  1019. }
  1020. added_linked_dependenciy_names.push_back(name);
  1021. plugin_linked_dependencies.push_back(dependency);
  1022. }
  1023. for (int j = 0; j < plugin.system_dependencies.size(); j++) {
  1024. String dependency = plugin.system_dependencies[j];
  1025. String name = dependency.get_file();
  1026. if (added_linked_dependenciy_names.has(name)) {
  1027. continue;
  1028. }
  1029. added_linked_dependenciy_names.push_back(name);
  1030. plugin_linked_dependencies.push_back(dependency);
  1031. }
  1032. for (int j = 0; j < plugin.embedded_dependencies.size(); j++) {
  1033. String dependency = plugin.embedded_dependencies[j];
  1034. String name = dependency.get_file();
  1035. if (added_embedded_dependenciy_names.has(name)) {
  1036. continue;
  1037. }
  1038. added_embedded_dependenciy_names.push_back(name);
  1039. plugin_embedded_dependencies.push_back(dependency);
  1040. }
  1041. plugin_files.append_array(plugin.files_to_copy);
  1042. // Capabilities
  1043. // Also checking for duplicates.
  1044. for (int j = 0; j < plugin.capabilities.size(); j++) {
  1045. String capability = plugin.capabilities[j];
  1046. if (p_config_data.capabilities.has(capability)) {
  1047. continue;
  1048. }
  1049. p_config_data.capabilities.push_back(capability);
  1050. }
  1051. // Linker flags
  1052. // Checking duplicates
  1053. for (int j = 0; j < plugin.linker_flags.size(); j++) {
  1054. String linker_flag = plugin.linker_flags[j];
  1055. plugin_linker_flags.insert(linker_flag);
  1056. }
  1057. // Plist
  1058. // Using hash map container to remove duplicates
  1059. const String *K = nullptr;
  1060. while ((K = plugin.plist.next(K))) {
  1061. String key = *K;
  1062. PluginConfigIOS::PlistItem item = plugin.plist[key];
  1063. String value;
  1064. switch (item.type) {
  1065. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  1066. String preset_name = "plugins_plist/" + key;
  1067. String input_value = p_preset->get(preset_name);
  1068. value = "<string>" + input_value + "</string>";
  1069. } break;
  1070. default:
  1071. value = item.value;
  1072. break;
  1073. }
  1074. if (key.is_empty() || value.is_empty()) {
  1075. continue;
  1076. }
  1077. String plist_key = "<key>" + key + "</key>";
  1078. plist_values[plist_key] = value;
  1079. }
  1080. // CPP Code
  1081. String definition_comment = "// Plugin: " + plugin.name + "\n";
  1082. String initialization_method = plugin.initialization_method + "();\n";
  1083. String deinitialization_method = plugin.deinitialization_method + "();\n";
  1084. plugin_definition_cpp_code += definition_comment +
  1085. "extern void " + initialization_method +
  1086. "extern void " + deinitialization_method + "\n";
  1087. plugin_initialization_cpp_code += "\t" + initialization_method;
  1088. plugin_deinitialization_cpp_code += "\t" + deinitialization_method;
  1089. }
  1090. // Updating `Info.plist`
  1091. {
  1092. const String *K = nullptr;
  1093. while ((K = plist_values.next(K))) {
  1094. String key = *K;
  1095. String value = plist_values[key];
  1096. if (key.is_empty() || value.is_empty()) {
  1097. continue;
  1098. }
  1099. p_config_data.plist_content += key + value + "\n";
  1100. }
  1101. }
  1102. // Export files
  1103. {
  1104. // Export linked plugin dependency
  1105. err = _export_additional_assets(dest_dir, plugin_linked_dependencies, true, false, r_exported_assets);
  1106. ERR_FAIL_COND_V(err, err);
  1107. // Export embedded plugin dependency
  1108. err = _export_additional_assets(dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets);
  1109. ERR_FAIL_COND_V(err, err);
  1110. // Export plugin files
  1111. err = _export_additional_assets(dest_dir, plugin_files, false, false, r_exported_assets);
  1112. ERR_FAIL_COND_V(err, err);
  1113. }
  1114. // Update CPP
  1115. {
  1116. Dictionary plugin_format;
  1117. plugin_format["definition"] = plugin_definition_cpp_code;
  1118. plugin_format["initialization"] = plugin_initialization_cpp_code;
  1119. plugin_format["deinitialization"] = plugin_deinitialization_cpp_code;
  1120. String plugin_cpp_code = "\n// Godot Plugins\n"
  1121. "void godot_ios_plugins_initialize();\n"
  1122. "void godot_ios_plugins_deinitialize();\n"
  1123. "// Exported Plugins\n\n"
  1124. "$definition"
  1125. "// Use Plugins\n"
  1126. "void godot_ios_plugins_initialize() {\n"
  1127. "$initialization"
  1128. "}\n\n"
  1129. "void godot_ios_plugins_deinitialize() {\n"
  1130. "$deinitialization"
  1131. "}\n";
  1132. p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_");
  1133. }
  1134. // Update Linker Flag Values
  1135. {
  1136. String result_linker_flags = " ";
  1137. for (Set<String>::Element *E = plugin_linker_flags.front(); E; E = E->next()) {
  1138. const String &flag = E->get();
  1139. if (flag.length() == 0) {
  1140. continue;
  1141. }
  1142. if (result_linker_flags.length() > 0) {
  1143. result_linker_flags += ' ';
  1144. }
  1145. result_linker_flags += flag;
  1146. }
  1147. result_linker_flags = result_linker_flags.replace("\"", "\\\"");
  1148. p_config_data.linker_flags += result_linker_flags;
  1149. }
  1150. return OK;
  1151. }
  1152. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1153. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1154. String src_pkg_name;
  1155. String dest_dir = p_path.get_base_dir() + "/";
  1156. String binary_name = p_path.get_file().get_basename();
  1157. EditorProgress ep("export", "Exporting for iOS", 5, true);
  1158. String team_id = p_preset->get("application/app_store_team_id");
  1159. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  1160. if (p_debug) {
  1161. src_pkg_name = p_preset->get("custom_template/debug");
  1162. } else {
  1163. src_pkg_name = p_preset->get("custom_template/release");
  1164. }
  1165. if (src_pkg_name == "") {
  1166. String err;
  1167. src_pkg_name = find_export_template("iphone.zip", &err);
  1168. if (src_pkg_name == "") {
  1169. EditorNode::add_io_error(err);
  1170. return ERR_FILE_NOT_FOUND;
  1171. }
  1172. }
  1173. if (!DirAccess::exists(dest_dir)) {
  1174. return ERR_FILE_BAD_PATH;
  1175. }
  1176. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1177. if (da) {
  1178. String current_dir = da->get_current_dir();
  1179. // remove leftovers from last export so they don't interfere
  1180. // in case some files are no longer needed
  1181. if (da->change_dir(dest_dir + binary_name + ".xcodeproj") == OK) {
  1182. da->erase_contents_recursive();
  1183. }
  1184. if (da->change_dir(dest_dir + binary_name) == OK) {
  1185. da->erase_contents_recursive();
  1186. }
  1187. da->change_dir(current_dir);
  1188. if (!da->dir_exists(dest_dir + binary_name)) {
  1189. Error err = da->make_dir(dest_dir + binary_name);
  1190. if (err) {
  1191. memdelete(da);
  1192. return err;
  1193. }
  1194. }
  1195. memdelete(da);
  1196. }
  1197. if (ep.step("Making .pck", 0)) {
  1198. return ERR_SKIP;
  1199. }
  1200. String pack_path = dest_dir + binary_name + ".pck";
  1201. Vector<SharedObject> libraries;
  1202. Error err = save_pack(p_preset, pack_path, &libraries);
  1203. if (err) {
  1204. return err;
  1205. }
  1206. if (ep.step("Extracting and configuring Xcode project", 1)) {
  1207. return ERR_SKIP;
  1208. }
  1209. String library_to_use = "libgodot.iphone." + String(p_debug ? "debug" : "release") + ".xcframework";
  1210. print_line("Static framework: " + library_to_use);
  1211. String pkg_name;
  1212. if (p_preset->get("application/name") != "") {
  1213. pkg_name = p_preset->get("application/name"); // app_name
  1214. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  1215. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  1216. } else {
  1217. pkg_name = "Unnamed";
  1218. }
  1219. bool found_library = false;
  1220. int total_size = 0;
  1221. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  1222. Set<String> files_to_parse;
  1223. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  1224. files_to_parse.insert(project_file);
  1225. files_to_parse.insert("godot_ios/export_options.plist");
  1226. files_to_parse.insert("godot_ios/dummy.cpp");
  1227. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  1228. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  1229. files_to_parse.insert("godot_ios/godot_ios.entitlements");
  1230. files_to_parse.insert("godot_ios/Launch Screen.storyboard");
  1231. IOSConfigData config_data = {
  1232. pkg_name,
  1233. binary_name,
  1234. _get_additional_plist_content(),
  1235. String(" ").join(_get_preset_architectures(p_preset)),
  1236. _get_linker_flags(),
  1237. _get_cpp_code(),
  1238. "",
  1239. "",
  1240. "",
  1241. "",
  1242. Vector<String>()
  1243. };
  1244. Vector<IOSExportAsset> assets;
  1245. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  1246. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE);
  1247. print_line("Unzipping...");
  1248. FileAccess *src_f = nullptr;
  1249. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  1250. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  1251. if (!src_pkg_zip) {
  1252. EditorNode::add_io_error("Could not open export template (not a zip file?):\n" + src_pkg_name);
  1253. return ERR_CANT_OPEN;
  1254. }
  1255. err = _export_ios_plugins(p_preset, config_data, dest_dir + binary_name, assets, p_debug);
  1256. ERR_FAIL_COND_V(err, err);
  1257. //export rest of the files
  1258. int ret = unzGoToFirstFile(src_pkg_zip);
  1259. Vector<uint8_t> project_file_data;
  1260. while (ret == UNZ_OK) {
  1261. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1262. bool is_execute = false;
  1263. #endif
  1264. //get filename
  1265. unz_file_info info;
  1266. char fname[16384];
  1267. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  1268. String file = fname;
  1269. print_line("READ: " + file);
  1270. Vector<uint8_t> data;
  1271. data.resize(info.uncompressed_size);
  1272. //read
  1273. unzOpenCurrentFile(src_pkg_zip);
  1274. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  1275. unzCloseCurrentFile(src_pkg_zip);
  1276. //write
  1277. file = file.replace_first("iphone/", "");
  1278. if (files_to_parse.has(file)) {
  1279. _fix_config_file(p_preset, data, config_data, p_debug);
  1280. } else if (file.begins_with("libgodot.iphone")) {
  1281. if (!file.begins_with(library_to_use) || file.ends_with(String("/empty"))) {
  1282. ret = unzGoToNextFile(src_pkg_zip);
  1283. continue; //ignore!
  1284. }
  1285. found_library = true;
  1286. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1287. is_execute = true;
  1288. #endif
  1289. file = file.replace(library_to_use, binary_name + ".xcframework");
  1290. }
  1291. if (file == project_file) {
  1292. project_file_data = data;
  1293. }
  1294. ///@TODO need to parse logo files
  1295. if (data.size() > 0) {
  1296. file = file.replace("godot_ios", binary_name);
  1297. print_line("ADDING: " + file + " size: " + itos(data.size()));
  1298. total_size += data.size();
  1299. /* write it into our folder structure */
  1300. file = dest_dir + file;
  1301. /* make sure this folder exists */
  1302. String dir_name = file.get_base_dir();
  1303. if (!tmp_app_path->dir_exists(dir_name)) {
  1304. print_line("Creating " + dir_name);
  1305. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  1306. if (dir_err) {
  1307. ERR_PRINT("Can't create '" + dir_name + "'.");
  1308. unzClose(src_pkg_zip);
  1309. memdelete(tmp_app_path);
  1310. return ERR_CANT_CREATE;
  1311. }
  1312. }
  1313. /* write the file */
  1314. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  1315. if (!f) {
  1316. ERR_PRINT("Can't write '" + file + "'.");
  1317. unzClose(src_pkg_zip);
  1318. memdelete(tmp_app_path);
  1319. return ERR_CANT_CREATE;
  1320. };
  1321. f->store_buffer(data.ptr(), data.size());
  1322. f->close();
  1323. memdelete(f);
  1324. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1325. if (is_execute) {
  1326. // we need execute rights on this file
  1327. chmod(file.utf8().get_data(), 0755);
  1328. }
  1329. #endif
  1330. }
  1331. ret = unzGoToNextFile(src_pkg_zip);
  1332. }
  1333. /* we're done with our source zip */
  1334. unzClose(src_pkg_zip);
  1335. if (!found_library) {
  1336. ERR_PRINT("Requested template library '" + library_to_use + "' not found. It might be missing from your template archive.");
  1337. memdelete(tmp_app_path);
  1338. return ERR_FILE_NOT_FOUND;
  1339. }
  1340. // Copy project static libs to the project
  1341. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1342. for (int i = 0; i < export_plugins.size(); i++) {
  1343. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1344. for (int j = 0; j < project_static_libs.size(); j++) {
  1345. const String &static_lib_path = project_static_libs[j];
  1346. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  1347. Error lib_copy_err = tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  1348. if (lib_copy_err != OK) {
  1349. ERR_PRINT("Can't copy '" + static_lib_path + "'.");
  1350. memdelete(tmp_app_path);
  1351. return lib_copy_err;
  1352. }
  1353. }
  1354. }
  1355. String iconset_dir = dest_dir + binary_name + "/Images.xcassets/AppIcon.appiconset/";
  1356. err = OK;
  1357. if (!tmp_app_path->dir_exists(iconset_dir)) {
  1358. err = tmp_app_path->make_dir_recursive(iconset_dir);
  1359. }
  1360. memdelete(tmp_app_path);
  1361. if (err) {
  1362. return err;
  1363. }
  1364. err = _export_icons(p_preset, iconset_dir);
  1365. if (err) {
  1366. return err;
  1367. }
  1368. bool use_storyboard = p_preset->get("storyboard/use_launch_screen_storyboard");
  1369. String launch_image_path = dest_dir + binary_name + "/Images.xcassets/LaunchImage.launchimage/";
  1370. String splash_image_path = dest_dir + binary_name + "/Images.xcassets/SplashImage.imageset/";
  1371. DirAccess *launch_screen_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1372. if (!launch_screen_da) {
  1373. return ERR_CANT_CREATE;
  1374. }
  1375. if (use_storyboard) {
  1376. print_line("Using Launch Storyboard");
  1377. if (launch_screen_da->change_dir(launch_image_path) == OK) {
  1378. launch_screen_da->erase_contents_recursive();
  1379. launch_screen_da->remove(launch_image_path);
  1380. }
  1381. err = _export_loading_screen_file(p_preset, splash_image_path);
  1382. } else {
  1383. print_line("Using Launch Images");
  1384. const String launch_screen_path = dest_dir + binary_name + "/Launch Screen.storyboard";
  1385. launch_screen_da->remove(launch_screen_path);
  1386. if (launch_screen_da->change_dir(splash_image_path) == OK) {
  1387. launch_screen_da->erase_contents_recursive();
  1388. launch_screen_da->remove(splash_image_path);
  1389. }
  1390. err = _export_loading_screen_images(p_preset, launch_image_path);
  1391. }
  1392. memdelete(launch_screen_da);
  1393. if (err) {
  1394. return err;
  1395. }
  1396. print_line("Exporting additional assets");
  1397. _export_additional_assets(dest_dir + binary_name, libraries, assets);
  1398. _add_assets_to_project(p_preset, project_file_data, assets);
  1399. String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj";
  1400. FileAccess *f = FileAccess::open(project_file_name, FileAccess::WRITE);
  1401. if (!f) {
  1402. ERR_PRINT("Can't write '" + project_file_name + "'.");
  1403. return ERR_CANT_CREATE;
  1404. };
  1405. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  1406. f->close();
  1407. memdelete(f);
  1408. #ifdef OSX_ENABLED
  1409. if (ep.step("Code-signing dylibs", 2)) {
  1410. return ERR_SKIP;
  1411. }
  1412. DirAccess *dylibs_dir = DirAccess::open(dest_dir + binary_name + "/dylibs");
  1413. ERR_FAIL_COND_V(!dylibs_dir, ERR_CANT_OPEN);
  1414. CodesignData codesign_data(p_preset, p_debug);
  1415. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  1416. memdelete(dylibs_dir);
  1417. ERR_FAIL_COND_V(err, err);
  1418. if (ep.step("Making .xcarchive", 3)) {
  1419. return ERR_SKIP;
  1420. }
  1421. String archive_path = p_path.get_basename() + ".xcarchive";
  1422. List<String> archive_args;
  1423. archive_args.push_back("-project");
  1424. archive_args.push_back(dest_dir + binary_name + ".xcodeproj");
  1425. archive_args.push_back("-scheme");
  1426. archive_args.push_back(binary_name);
  1427. archive_args.push_back("-sdk");
  1428. archive_args.push_back("iphoneos");
  1429. archive_args.push_back("-configuration");
  1430. archive_args.push_back(p_debug ? "Debug" : "Release");
  1431. archive_args.push_back("-destination");
  1432. archive_args.push_back("generic/platform=iOS");
  1433. archive_args.push_back("archive");
  1434. archive_args.push_back("-archivePath");
  1435. archive_args.push_back(archive_path);
  1436. err = OS::get_singleton()->execute("xcodebuild", archive_args);
  1437. ERR_FAIL_COND_V(err, err);
  1438. if (ep.step("Making .ipa", 4)) {
  1439. return ERR_SKIP;
  1440. }
  1441. List<String> export_args;
  1442. export_args.push_back("-exportArchive");
  1443. export_args.push_back("-archivePath");
  1444. export_args.push_back(archive_path);
  1445. export_args.push_back("-exportOptionsPlist");
  1446. export_args.push_back(dest_dir + binary_name + "/export_options.plist");
  1447. export_args.push_back("-allowProvisioningUpdates");
  1448. export_args.push_back("-exportPath");
  1449. export_args.push_back(dest_dir);
  1450. err = OS::get_singleton()->execute("xcodebuild", export_args);
  1451. ERR_FAIL_COND_V(err, err);
  1452. #else
  1453. print_line(".ipa can only be built on macOS. Leaving Xcode project without building the package.");
  1454. #endif
  1455. return OK;
  1456. }
  1457. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1458. String err;
  1459. bool valid = false;
  1460. // Look for export templates (first official, and if defined custom templates).
  1461. bool dvalid = exists_export_template("iphone.zip", &err);
  1462. bool rvalid = dvalid; // Both in the same ZIP.
  1463. if (p_preset->get("custom_template/debug") != "") {
  1464. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1465. if (!dvalid) {
  1466. err += TTR("Custom debug template not found.") + "\n";
  1467. }
  1468. }
  1469. if (p_preset->get("custom_template/release") != "") {
  1470. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1471. if (!rvalid) {
  1472. err += TTR("Custom release template not found.") + "\n";
  1473. }
  1474. }
  1475. valid = dvalid || rvalid;
  1476. r_missing_templates = !valid;
  1477. // Validate the rest of the configuration.
  1478. String team_id = p_preset->get("application/app_store_team_id");
  1479. if (team_id.length() == 0) {
  1480. err += TTR("App Store Team ID not specified - cannot configure the project.") + "\n";
  1481. valid = false;
  1482. }
  1483. String identifier = p_preset->get("application/bundle_identifier");
  1484. String pn_err;
  1485. if (!is_package_name_valid(identifier, &pn_err)) {
  1486. err += TTR("Invalid Identifier:") + " " + pn_err + "\n";
  1487. valid = false;
  1488. }
  1489. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  1490. IconInfo info = icon_infos[i];
  1491. String icon_path = p_preset->get(info.preset_key);
  1492. if (icon_path.length() == 0) {
  1493. if (info.is_required) {
  1494. err += TTR("Required icon is not specified in the preset.") + "\n";
  1495. valid = false;
  1496. }
  1497. break;
  1498. }
  1499. }
  1500. String etc_error = test_etc2_or_pvrtc();
  1501. if (etc_error != String()) {
  1502. valid = false;
  1503. err += etc_error;
  1504. }
  1505. if (!err.is_empty()) {
  1506. r_error = err;
  1507. }
  1508. return valid;
  1509. }
  1510. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  1511. Ref<Image> img = memnew(Image(_iphone_logo));
  1512. logo.instantiate();
  1513. logo->create_from_image(img);
  1514. plugins_changed.set();
  1515. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  1516. }
  1517. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  1518. quit_request.set();
  1519. check_for_changes_thread.wait_to_finish();
  1520. }