export_plugin.cpp 68 KB

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