export_plugin.cpp 69 KB

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