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