export_plugin.cpp 72 KB

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