export_plugin.cpp 68 KB

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