export_plugin.cpp 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  1. /**************************************************************************/
  2. /* export_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "logo_svg.gen.h"
  32. #include "run_icon_svg.gen.h"
  33. #include "core/io/json.h"
  34. #include "core/io/plist.h"
  35. #include "core/string/translation.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_paths.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_string_names.h"
  40. #include "editor/export/editor_export.h"
  41. #include "editor/import/resource_importer_texture_settings.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "modules/modules_enabled.gen.h" // For mono and svg.
  44. #ifdef MODULE_SVG_ENABLED
  45. #include "modules/svg/image_loader_svg.h"
  46. #endif
  47. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  48. // Vulkan and OpenGL ES 3.0 both mandate ETC2 support.
  49. r_features->push_back("etc2");
  50. r_features->push_back("astc");
  51. Vector<String> architectures = _get_preset_architectures(p_preset);
  52. for (int i = 0; i < architectures.size(); ++i) {
  53. r_features->push_back(architectures[i]);
  54. }
  55. }
  56. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() const {
  57. Vector<ExportArchitecture> archs;
  58. archs.push_back(ExportArchitecture("arm64", true));
  59. return archs;
  60. }
  61. struct IconInfo {
  62. const char *preset_key;
  63. const char *idiom;
  64. const char *export_name;
  65. const char *actual_size_side;
  66. const char *scale;
  67. const char *unscaled_size;
  68. const bool force_opaque;
  69. };
  70. static const IconInfo icon_infos[] = {
  71. // Home screen on iPhone
  72. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "2x", "60x60", false },
  73. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "3x", "40x40", false },
  74. { PNAME("icons/iphone_180x180"), "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  75. // Home screen on iPad
  76. { PNAME("icons/ipad_76x76"), "ipad", "Icon-76.png", "76", "1x", "76x76", false },
  77. { PNAME("icons/ipad_152x152"), "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  78. { PNAME("icons/ipad_167x167"), "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  79. // App Store
  80. { PNAME("icons/app_store_1024x1024"), "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", true },
  81. // Spotlight
  82. { PNAME("icons/spotlight_40x40"), "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  83. { PNAME("icons/spotlight_80x80"), "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  84. { PNAME("icons/spotlight_80x80"), "ipad", "Icon-80.png", "80", "2x", "40x40", false },
  85. // Settings
  86. { PNAME("icons/settings_58x58"), "iphone", "Icon-58.png", "58", "2x", "29x29", false },
  87. { PNAME("icons/settings_58x58"), "ipad", "Icon-58.png", "58", "2x", "29x29", false },
  88. { PNAME("icons/settings_87x87"), "iphone", "Icon-87.png", "87", "3x", "29x29", false },
  89. // Notification
  90. { PNAME("icons/notification_40x40"), "iphone", "Icon-40.png", "40", "2x", "20x20", false },
  91. { PNAME("icons/notification_40x40"), "ipad", "Icon-40.png", "40", "2x", "20x20", false },
  92. { PNAME("icons/notification_60x60"), "iphone", "Icon-60.png", "60", "3x", "20x20", false }
  93. };
  94. String EditorExportPlatformIOS::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
  95. if (p_preset) {
  96. if (p_name == "application/app_store_team_id") {
  97. String team_id = p_preset->get("application/app_store_team_id");
  98. if (team_id.is_empty()) {
  99. return TTR("App Store Team ID not specified.") + "\n";
  100. }
  101. } else if (p_name == "application/bundle_identifier") {
  102. String identifier = p_preset->get("application/bundle_identifier");
  103. String pn_err;
  104. if (!is_package_name_valid(identifier, &pn_err)) {
  105. return TTR("Invalid Identifier:") + " " + pn_err;
  106. }
  107. }
  108. }
  109. return String();
  110. }
  111. bool EditorExportPlatformIOS::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
  112. return true;
  113. }
  114. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
  115. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  117. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  118. for (int i = 0; i < architectures.size(); ++i) {
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), architectures[i].name)), architectures[i].is_default));
  120. }
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), "", false, true));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), ""));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), "", false, true));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  132. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  133. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), "12.0"));
  134. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/additional_plist_content", PROPERTY_HINT_MULTILINE_TEXT), ""));
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/export_project_only"), false));
  137. Vector<PluginConfigIOS> found_plugins = get_plugins();
  138. for (int i = 0; i < found_plugins.size(); i++) {
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), found_plugins[i].name)), false));
  140. }
  141. HashSet<String> plist_keys;
  142. for (int i = 0; i < found_plugins.size(); i++) {
  143. // Editable plugin plist values
  144. PluginConfigIOS plugin = found_plugins[i];
  145. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  146. switch (E.value.type) {
  147. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  148. String preset_name = "plugins_plist/" + E.key;
  149. if (!plist_keys.has(preset_name)) {
  150. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, preset_name), E.value.value));
  151. plist_keys.insert(preset_name);
  152. }
  153. } break;
  154. default:
  155. continue;
  156. }
  157. }
  158. }
  159. plugins_changed.clear();
  160. plugins = found_plugins;
  161. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  162. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  163. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_gaming_tier"), false));
  164. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_a12"), false));
  165. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  166. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  167. 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"), ""));
  168. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/camera_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  169. 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"), ""));
  170. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/microphone_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  171. 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"), ""));
  172. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/photolibrary_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  173. HashSet<String> used_names;
  174. for (uint64_t i = 0; i < sizeof(icon_infos) / sizeof(icon_infos[0]); ++i) {
  175. if (!used_names.has(icon_infos[i].preset_key)) {
  176. used_names.insert(icon_infos[i].preset_key);
  177. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, icon_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  178. }
  179. }
  180. 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));
  181. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  182. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  183. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  184. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  185. }
  186. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  187. static const String export_method_string[] = {
  188. "app-store",
  189. "development",
  190. "ad-hoc",
  191. "enterprise"
  192. };
  193. static const String storyboard_image_scale_mode[] = {
  194. "center",
  195. "scaleAspectFit",
  196. "scaleAspectFill",
  197. "scaleToFill"
  198. };
  199. 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");
  200. 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");
  201. bool dbg_manual = !p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG).operator String().is_empty() || (dbg_sign_id != "iPhone Developer" && dbg_sign_id != "iPhone Distribution");
  202. bool rel_manual = !p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE).operator String().is_empty() || (rel_sign_id != "iPhone Developer" && rel_sign_id != "iPhone Distribution");
  203. String str;
  204. String strnew;
  205. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  206. Vector<String> lines = str.split("\n");
  207. for (int i = 0; i < lines.size(); i++) {
  208. if (lines[i].find("$binary") != -1) {
  209. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  210. } else if (lines[i].find("$modules_buildfile") != -1) {
  211. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  212. } else if (lines[i].find("$modules_fileref") != -1) {
  213. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  214. } else if (lines[i].find("$modules_buildphase") != -1) {
  215. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  216. } else if (lines[i].find("$modules_buildgrp") != -1) {
  217. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  218. } else if (lines[i].find("$name") != -1) {
  219. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  220. } else if (lines[i].find("$bundle_identifier") != -1) {
  221. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  222. } else if (lines[i].find("$short_version") != -1) {
  223. strnew += lines[i].replace("$short_version", p_preset->get_version("application/short_version")) + "\n";
  224. } else if (lines[i].find("$version") != -1) {
  225. strnew += lines[i].replace("$version", p_preset->get_version("application/version")) + "\n";
  226. } else if (lines[i].find("$min_version") != -1) {
  227. strnew += lines[i].replace("$min_version", p_preset->get("application/min_ios_version")) + "\n";
  228. } else if (lines[i].find("$signature") != -1) {
  229. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  230. } else if (lines[i].find("$team_id") != -1) {
  231. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  232. } else if (lines[i].find("$default_build_config") != -1) {
  233. strnew += lines[i].replace("$default_build_config", p_debug ? "Debug" : "Release") + "\n";
  234. } else if (lines[i].find("$export_method") != -1) {
  235. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  236. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  237. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  238. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE)) + "\n";
  239. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  240. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG)) + "\n";
  241. } else if (lines[i].find("$code_sign_style_debug") != -1) {
  242. if (dbg_manual) {
  243. strnew += lines[i].replace("$code_sign_style_debug", "Manual") + "\n";
  244. } else {
  245. strnew += lines[i].replace("$code_sign_style_debug", "Automatic") + "\n";
  246. }
  247. } else if (lines[i].find("$code_sign_style_release") != -1) {
  248. if (rel_manual) {
  249. strnew += lines[i].replace("$code_sign_style_release", "Manual") + "\n";
  250. } else {
  251. strnew += lines[i].replace("$code_sign_style_release", "Automatic") + "\n";
  252. }
  253. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  254. String uuid = p_debug ? p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG) : p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE);
  255. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  256. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  257. strnew += lines[i].replace("$code_sign_identity_debug", dbg_sign_id) + "\n";
  258. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  259. strnew += lines[i].replace("$code_sign_identity_release", rel_sign_id) + "\n";
  260. } else if (lines[i].find("$additional_plist_content") != -1) {
  261. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  262. } else if (lines[i].find("$godot_archs") != -1) {
  263. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  264. } else if (lines[i].find("$linker_flags") != -1) {
  265. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  266. } else if (lines[i].find("$targeted_device_family") != -1) {
  267. String xcode_value;
  268. switch ((int)p_preset->get("application/targeted_device_family")) {
  269. case 0: // iPhone
  270. xcode_value = "1";
  271. break;
  272. case 1: // iPad
  273. xcode_value = "2";
  274. break;
  275. case 2: // iPhone & iPad
  276. xcode_value = "1,2";
  277. break;
  278. }
  279. strnew += lines[i].replace("$targeted_device_family", xcode_value) + "\n";
  280. } else if (lines[i].find("$cpp_code") != -1) {
  281. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  282. } else if (lines[i].find("$docs_in_place") != -1) {
  283. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  284. } else if (lines[i].find("$docs_sharing") != -1) {
  285. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  286. } else if (lines[i].find("$entitlements_push_notifications") != -1) {
  287. bool is_on = p_preset->get("capabilities/push_notifications");
  288. strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n";
  289. } else if (lines[i].find("$required_device_capabilities") != -1) {
  290. String capabilities;
  291. // I've removed armv7 as we can run on 64bit only devices
  292. // Note that capabilities listed here are requirements for the app to be installed.
  293. // They don't enable anything.
  294. Vector<String> capabilities_list = p_config.capabilities;
  295. if ((bool)p_preset->get("capabilities/access_wifi") && !capabilities_list.has("wifi")) {
  296. capabilities_list.push_back("wifi");
  297. }
  298. if ((bool)p_preset->get("capabilities/performance_gaming_tier") && !capabilities_list.has("iphone-performance-gaming-tier")) {
  299. capabilities_list.push_back("iphone-performance-gaming-tier");
  300. }
  301. if ((bool)p_preset->get("capabilities/performance_a12") && !capabilities_list.has("iphone-ipad-minimum-performance-a12")) {
  302. capabilities_list.push_back("iphone-ipad-minimum-performance-a12");
  303. }
  304. for (int idx = 0; idx < capabilities_list.size(); idx++) {
  305. capabilities += "<string>" + capabilities_list[idx] + "</string>\n";
  306. }
  307. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  308. } else if (lines[i].find("$interface_orientations") != -1) {
  309. String orientations;
  310. const DisplayServer::ScreenOrientation screen_orientation =
  311. DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")));
  312. switch (screen_orientation) {
  313. case DisplayServer::SCREEN_LANDSCAPE:
  314. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  315. break;
  316. case DisplayServer::SCREEN_PORTRAIT:
  317. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  318. break;
  319. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  320. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  321. break;
  322. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  323. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  324. break;
  325. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  326. // Allow both landscape orientations depending on sensor direction.
  327. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  328. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  329. break;
  330. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  331. // Allow both portrait orientations depending on sensor direction.
  332. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  333. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  334. break;
  335. case DisplayServer::SCREEN_SENSOR:
  336. // Allow all screen orientations depending on sensor direction.
  337. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  338. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  339. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  340. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  341. break;
  342. }
  343. strnew += lines[i].replace("$interface_orientations", orientations);
  344. } else if (lines[i].find("$camera_usage_description") != -1) {
  345. String description = p_preset->get("privacy/camera_usage_description");
  346. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  347. } else if (lines[i].find("$microphone_usage_description") != -1) {
  348. String description = p_preset->get("privacy/microphone_usage_description");
  349. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  350. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  351. String description = p_preset->get("privacy/photolibrary_usage_description");
  352. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  353. } else if (lines[i].find("$plist_launch_screen_name") != -1) {
  354. String value = "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>";
  355. strnew += lines[i].replace("$plist_launch_screen_name", value) + "\n";
  356. } else if (lines[i].find("$pbx_launch_screen_file_reference") != -1) {
  357. String value = "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };";
  358. strnew += lines[i].replace("$pbx_launch_screen_file_reference", value) + "\n";
  359. } else if (lines[i].find("$pbx_launch_screen_copy_files") != -1) {
  360. String value = "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */,";
  361. strnew += lines[i].replace("$pbx_launch_screen_copy_files", value) + "\n";
  362. } else if (lines[i].find("$pbx_launch_screen_build_phase") != -1) {
  363. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */,";
  364. strnew += lines[i].replace("$pbx_launch_screen_build_phase", value) + "\n";
  365. } else if (lines[i].find("$pbx_launch_screen_build_reference") != -1) {
  366. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };";
  367. strnew += lines[i].replace("$pbx_launch_screen_build_reference", value) + "\n";
  368. } else if (lines[i].find("$launch_screen_image_mode") != -1) {
  369. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  370. String value;
  371. switch (image_scale_mode) {
  372. case 0: {
  373. String logo_path = GLOBAL_GET("application/boot_splash/image");
  374. bool is_on = GLOBAL_GET("application/boot_splash/fullsize");
  375. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  376. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  377. } break;
  378. default: {
  379. value = storyboard_image_scale_mode[image_scale_mode - 1];
  380. }
  381. }
  382. strnew += lines[i].replace("$launch_screen_image_mode", value) + "\n";
  383. } else if (lines[i].find("$launch_screen_background_color") != -1) {
  384. bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
  385. Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : GLOBAL_GET("application/boot_splash/bg_color");
  386. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  387. Dictionary value_dictionary;
  388. value_dictionary["red"] = color.r;
  389. value_dictionary["green"] = color.g;
  390. value_dictionary["blue"] = color.b;
  391. value_dictionary["alpha"] = color.a;
  392. String value = value_format.format(value_dictionary, "$_");
  393. strnew += lines[i].replace("$launch_screen_background_color", value) + "\n";
  394. } else if (lines[i].find("$pbx_locale_file_reference") != -1) {
  395. String locale_files;
  396. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  397. if (translations.size() > 0) {
  398. HashSet<String> languages;
  399. for (const String &E : translations) {
  400. Ref<Translation> tr = ResourceLoader::load(E);
  401. if (tr.is_valid() && tr->get_locale() != "en") {
  402. languages.insert(tr->get_locale());
  403. }
  404. }
  405. int index = 0;
  406. for (const String &lang : languages) {
  407. locale_files += "D0BCFE4518AEBDA2004A" + itos(index).pad_zeros(4) + " /* " + lang + " */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = " + lang + "; path = " + lang + ".lproj/InfoPlist.strings; sourceTree = \"<group>\"; };\n";
  408. index++;
  409. }
  410. }
  411. strnew += lines[i].replace("$pbx_locale_file_reference", locale_files);
  412. } else if (lines[i].find("$pbx_locale_build_reference") != -1) {
  413. String locale_files;
  414. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  415. if (translations.size() > 0) {
  416. HashSet<String> languages;
  417. for (const String &E : translations) {
  418. Ref<Translation> tr = ResourceLoader::load(E);
  419. if (tr.is_valid() && tr->get_locale() != "en") {
  420. languages.insert(tr->get_locale());
  421. }
  422. }
  423. int index = 0;
  424. for (const String &lang : languages) {
  425. locale_files += "D0BCFE4518AEBDA2004A" + itos(index).pad_zeros(4) + " /* " + lang + " */,\n";
  426. index++;
  427. }
  428. }
  429. strnew += lines[i].replace("$pbx_locale_build_reference", locale_files);
  430. } else if (lines[i].find("$swift_runtime_migration") != -1) {
  431. String value = !p_config.use_swift_runtime ? "" : "LastSwiftMigration = 1250;";
  432. strnew += lines[i].replace("$swift_runtime_migration", value) + "\n";
  433. } else if (lines[i].find("$swift_runtime_build_settings") != -1) {
  434. String value = !p_config.use_swift_runtime ? "" : R"(
  435. CLANG_ENABLE_MODULES = YES;
  436. SWIFT_OBJC_BRIDGING_HEADER = "$binary/dummy.h";
  437. SWIFT_VERSION = 5.0;
  438. )";
  439. value = value.replace("$binary", p_config.binary_name);
  440. strnew += lines[i].replace("$swift_runtime_build_settings", value) + "\n";
  441. } else if (lines[i].find("$swift_runtime_fileref") != -1) {
  442. String value = !p_config.use_swift_runtime ? "" : R"(
  443. 90B4C2AA2680BC560039117A /* dummy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dummy.h"; sourceTree = "<group>"; };
  444. 90B4C2B52680C7E90039117A /* dummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "dummy.swift"; sourceTree = "<group>"; };
  445. )";
  446. strnew += lines[i].replace("$swift_runtime_fileref", value) + "\n";
  447. } else if (lines[i].find("$swift_runtime_binary_files") != -1) {
  448. String value = !p_config.use_swift_runtime ? "" : R"(
  449. 90B4C2AA2680BC560039117A /* dummy.h */,
  450. 90B4C2B52680C7E90039117A /* dummy.swift */,
  451. )";
  452. strnew += lines[i].replace("$swift_runtime_binary_files", value) + "\n";
  453. } else if (lines[i].find("$swift_runtime_buildfile") != -1) {
  454. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90B4C2B52680C7E90039117A /* dummy.swift */; };";
  455. strnew += lines[i].replace("$swift_runtime_buildfile", value) + "\n";
  456. } else if (lines[i].find("$swift_runtime_build_phase") != -1) {
  457. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift */,";
  458. strnew += lines[i].replace("$swift_runtime_build_phase", value) + "\n";
  459. } else {
  460. strnew += lines[i] + "\n";
  461. }
  462. }
  463. // !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...
  464. // should apply the same fix in our macOS export.
  465. CharString cs = strnew.utf8();
  466. pfile.resize(cs.size() - 1);
  467. for (int i = 0; i < cs.size() - 1; i++) {
  468. pfile.write[i] = cs[i];
  469. }
  470. }
  471. String EditorExportPlatformIOS::_get_additional_plist_content() {
  472. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  473. String result;
  474. for (int i = 0; i < export_plugins.size(); ++i) {
  475. result += export_plugins[i]->get_ios_plist_content();
  476. }
  477. return result;
  478. }
  479. String EditorExportPlatformIOS::_get_linker_flags() {
  480. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  481. String result;
  482. for (int i = 0; i < export_plugins.size(); ++i) {
  483. String flags = export_plugins[i]->get_ios_linker_flags();
  484. if (flags.length() == 0) {
  485. continue;
  486. }
  487. if (result.length() > 0) {
  488. result += ' ';
  489. }
  490. result += flags;
  491. }
  492. // the flags will be enclosed in quotes, so need to escape them
  493. return result.replace("\"", "\\\"");
  494. }
  495. String EditorExportPlatformIOS::_get_cpp_code() {
  496. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  497. String result;
  498. for (int i = 0; i < export_plugins.size(); ++i) {
  499. result += export_plugins[i]->get_ios_cpp_code();
  500. }
  501. return result;
  502. }
  503. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  504. ERR_FAIL_COND(p_dst.is_null());
  505. ERR_FAIL_COND(p_src.is_null());
  506. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  507. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  508. int x_pos = (p_dst->get_width() - sw) / 2;
  509. int y_pos = (p_dst->get_height() - sh) / 2;
  510. int xs = (x_pos >= 0) ? 0 : -x_pos;
  511. int ys = (y_pos >= 0) ? 0 : -y_pos;
  512. if (sw + x_pos > p_dst->get_width()) {
  513. sw = p_dst->get_width() - x_pos;
  514. }
  515. if (sh + y_pos > p_dst->get_height()) {
  516. sh = p_dst->get_height() - y_pos;
  517. }
  518. for (int y = ys; y < sh; y++) {
  519. for (int x = xs; x < sw; x++) {
  520. Color sc = p_rot ? p_src->get_pixel(p_src->get_width() - y - 1, x) : p_src->get_pixel(x, y);
  521. Color dc = p_dst->get_pixel(x_pos + x, y_pos + y);
  522. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  523. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  524. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  525. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  526. p_dst->set_pixel(x_pos + x, y_pos + y, dc);
  527. }
  528. }
  529. }
  530. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  531. String json_description = "{\"images\":[";
  532. String sizes;
  533. Ref<DirAccess> da = DirAccess::open(p_iconset_dir);
  534. if (da.is_null()) {
  535. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));
  536. return ERR_CANT_OPEN;
  537. }
  538. Color boot_bg_color = GLOBAL_GET("application/boot_splash/bg_color");
  539. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  540. IconInfo info = icon_infos[i];
  541. int side_size = String(info.actual_size_side).to_int();
  542. String icon_path = p_preset->get(info.preset_key);
  543. if (icon_path.length() == 0) {
  544. // Resize main app icon
  545. icon_path = GLOBAL_GET("application/config/icon");
  546. Ref<Image> img = memnew(Image);
  547. Error err = ImageLoader::load_image(icon_path, img);
  548. if (err != OK) {
  549. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  550. return ERR_UNCONFIGURED;
  551. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  552. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  553. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  554. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  555. new_img->fill(boot_bg_color);
  556. _blend_and_rotate(new_img, img, false);
  557. err = new_img->save_png(p_iconset_dir + info.export_name);
  558. } else {
  559. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  560. err = img->save_png(p_iconset_dir + info.export_name);
  561. }
  562. if (err) {
  563. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  564. return err;
  565. }
  566. } else {
  567. // Load custom icon and resize if required
  568. Ref<Image> img = memnew(Image);
  569. Error err = ImageLoader::load_image(icon_path, img);
  570. if (err != OK) {
  571. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  572. return ERR_UNCONFIGURED;
  573. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  574. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  575. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  576. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  577. new_img->fill(boot_bg_color);
  578. _blend_and_rotate(new_img, img, false);
  579. err = new_img->save_png(p_iconset_dir + info.export_name);
  580. } else if (img->get_width() != side_size || img->get_height() != side_size) {
  581. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s): '%s' has incorrect size %s and was automatically resized to %s.", info.preset_key, icon_path, img->get_size(), Vector2i(side_size, side_size)));
  582. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  583. err = img->save_png(p_iconset_dir + info.export_name);
  584. } else {
  585. err = da->copy(icon_path, p_iconset_dir + info.export_name);
  586. }
  587. if (err) {
  588. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  589. return err;
  590. }
  591. }
  592. sizes += String(info.actual_size_side) + "\n";
  593. if (i > 0) {
  594. json_description += ",";
  595. }
  596. json_description += String("{");
  597. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  598. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  599. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  600. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  601. json_description += String("}");
  602. }
  603. json_description += "]}";
  604. Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  605. if (json_file.is_null()) {
  606. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));
  607. return ERR_CANT_CREATE;
  608. }
  609. CharString json_utf8 = json_description.utf8();
  610. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  611. Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  612. if (sizes_file.is_null()) {
  613. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));
  614. return ERR_CANT_CREATE;
  615. }
  616. CharString sizes_utf8 = sizes.utf8();
  617. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  618. return OK;
  619. }
  620. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  621. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  622. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  623. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  624. Ref<Image> image;
  625. String image_path = p_dest_dir.path_join("[email protected]");
  626. image.instantiate();
  627. Error err = ImageLoader::load_image(custom_launch_image_2x, image);
  628. if (err) {
  629. image.unref();
  630. return err;
  631. }
  632. if (image->save_png(image_path) != OK) {
  633. return ERR_FILE_CANT_WRITE;
  634. }
  635. image.unref();
  636. image_path = p_dest_dir.path_join("[email protected]");
  637. image.instantiate();
  638. err = ImageLoader::load_image(custom_launch_image_3x, image);
  639. if (err) {
  640. image.unref();
  641. return err;
  642. }
  643. if (image->save_png(image_path) != OK) {
  644. return ERR_FILE_CANT_WRITE;
  645. }
  646. } else {
  647. Ref<Image> splash;
  648. const String splash_path = GLOBAL_GET("application/boot_splash/image");
  649. if (!splash_path.is_empty()) {
  650. splash.instantiate();
  651. const Error err = ImageLoader::load_image(splash_path, splash);
  652. if (err) {
  653. splash.unref();
  654. }
  655. }
  656. if (splash.is_null()) {
  657. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  658. }
  659. // Using same image for both @2x and @3x
  660. // because Godot's own boot logo uses single image for all resolutions.
  661. // Also not using @1x image, because devices using this image variant
  662. // are not supported by iOS 9, which is minimal target.
  663. const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
  664. const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");
  665. if (splash->save_png(splash_png_path_2x) != OK) {
  666. return ERR_FILE_CANT_WRITE;
  667. }
  668. if (splash->save_png(splash_png_path_3x) != OK) {
  669. return ERR_FILE_CANT_WRITE;
  670. }
  671. }
  672. return OK;
  673. }
  674. Error EditorExportPlatformIOS::_walk_dir_recursive(Ref<DirAccess> &p_da, FileHandler p_handler, void *p_userdata) {
  675. Vector<String> dirs;
  676. String current_dir = p_da->get_current_dir();
  677. p_da->list_dir_begin();
  678. String path = p_da->get_next();
  679. while (!path.is_empty()) {
  680. if (p_da->current_is_dir()) {
  681. if (path != "." && path != "..") {
  682. dirs.push_back(path);
  683. }
  684. } else {
  685. Error err = p_handler(current_dir.path_join(path), p_userdata);
  686. if (err) {
  687. p_da->list_dir_end();
  688. return err;
  689. }
  690. }
  691. path = p_da->get_next();
  692. }
  693. p_da->list_dir_end();
  694. for (int i = 0; i < dirs.size(); ++i) {
  695. p_da->change_dir(dirs[i]);
  696. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  697. p_da->change_dir("..");
  698. if (err) {
  699. return err;
  700. }
  701. }
  702. return OK;
  703. }
  704. struct CodesignData {
  705. const Ref<EditorExportPreset> &preset;
  706. bool debug = false;
  707. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  708. preset(p_preset),
  709. debug(p_debug) {
  710. }
  711. };
  712. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  713. if (p_file.ends_with(".dylib")) {
  714. CodesignData *data = static_cast<CodesignData *>(p_userdata);
  715. print_line(String("Signing ") + p_file);
  716. String sign_id;
  717. if (data->debug) {
  718. sign_id = data->preset->get("application/code_sign_identity_debug").operator String().is_empty() ? "iPhone Developer" : data->preset->get("application/code_sign_identity_debug");
  719. } else {
  720. sign_id = data->preset->get("application/code_sign_identity_release").operator String().is_empty() ? "iPhone Distribution" : data->preset->get("application/code_sign_identity_release");
  721. }
  722. List<String> codesign_args;
  723. codesign_args.push_back("-f");
  724. codesign_args.push_back("-s");
  725. codesign_args.push_back(sign_id);
  726. codesign_args.push_back(p_file);
  727. String str;
  728. Error err = OS::get_singleton()->execute("codesign", codesign_args, &str, nullptr, true);
  729. print_verbose("codesign (" + p_file + "):\n" + str);
  730. return err;
  731. }
  732. return OK;
  733. }
  734. struct PbxId {
  735. private:
  736. static char _hex_char(uint8_t four_bits) {
  737. if (four_bits < 10) {
  738. return ('0' + four_bits);
  739. }
  740. return 'A' + (four_bits - 10);
  741. }
  742. static String _hex_pad(uint32_t num) {
  743. Vector<char> ret;
  744. ret.resize(sizeof(num) * 2);
  745. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  746. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  747. ret.write[i] = _hex_char(four_bits);
  748. }
  749. return String::utf8(ret.ptr(), ret.size());
  750. }
  751. public:
  752. uint32_t high_bits;
  753. uint32_t mid_bits;
  754. uint32_t low_bits;
  755. String str() const {
  756. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  757. }
  758. PbxId &operator++() {
  759. low_bits++;
  760. if (!low_bits) {
  761. mid_bits++;
  762. if (!mid_bits) {
  763. high_bits++;
  764. }
  765. }
  766. return *this;
  767. }
  768. };
  769. struct ExportLibsData {
  770. Vector<String> lib_paths;
  771. String dest_dir;
  772. };
  773. void EditorExportPlatformIOS::_check_xcframework_content(const String &p_path, int &r_total_libs, int &r_static_libs, int &r_dylibs, int &r_frameworks) const {
  774. Ref<PList> plist;
  775. plist.instantiate();
  776. plist->load_file(p_path.path_join("Info.plist"));
  777. Ref<PListNode> root_node = plist->get_root();
  778. if (root_node.is_null()) {
  779. return;
  780. }
  781. Dictionary root = root_node->get_value();
  782. if (!root.has("AvailableLibraries")) {
  783. return;
  784. }
  785. Ref<PListNode> libs_node = root["AvailableLibraries"];
  786. if (libs_node.is_null()) {
  787. return;
  788. }
  789. Array libs = libs_node->get_value();
  790. r_total_libs = libs.size();
  791. for (int j = 0; j < libs.size(); j++) {
  792. Ref<PListNode> lib_node = libs[j];
  793. if (lib_node.is_null()) {
  794. return;
  795. }
  796. Dictionary lib = lib_node->get_value();
  797. if (lib.has("BinaryPath")) {
  798. Ref<PListNode> path_node = lib["BinaryPath"];
  799. if (path_node.is_valid()) {
  800. String path = path_node->get_value();
  801. if (path.ends_with(".a")) {
  802. r_static_libs++;
  803. }
  804. if (path.ends_with(".dylib")) {
  805. r_dylibs++;
  806. }
  807. if (path.ends_with(".framework")) {
  808. r_frameworks++;
  809. }
  810. }
  811. }
  812. }
  813. }
  814. Error EditorExportPlatformIOS::_convert_to_framework(const String &p_source, const String &p_destination, const String &p_id) const {
  815. print_line("Converting to .framework", p_source, " -> ", p_destination);
  816. Ref<DirAccess> da = DirAccess::create_for_path(p_source);
  817. if (da.is_null()) {
  818. return ERR_CANT_OPEN;
  819. }
  820. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  821. if (filesystem_da.is_null()) {
  822. return ERR_CANT_OPEN;
  823. }
  824. if (!filesystem_da->dir_exists(p_destination)) {
  825. Error make_dir_err = filesystem_da->make_dir_recursive(p_destination);
  826. if (make_dir_err) {
  827. return make_dir_err;
  828. }
  829. }
  830. String asset = p_source.ends_with("/") ? p_source.left(p_source.length() - 1) : p_source;
  831. if (asset.ends_with(".xcframework")) {
  832. Ref<PList> plist;
  833. plist.instantiate();
  834. plist->load_file(p_source.path_join("Info.plist"));
  835. Ref<PListNode> root_node = plist->get_root();
  836. if (root_node.is_null()) {
  837. return ERR_CANT_OPEN;
  838. }
  839. Dictionary root = root_node->get_value();
  840. if (!root.has("AvailableLibraries")) {
  841. return ERR_CANT_OPEN;
  842. }
  843. Ref<PListNode> libs_node = root["AvailableLibraries"];
  844. if (libs_node.is_null()) {
  845. return ERR_CANT_OPEN;
  846. }
  847. Array libs = libs_node->get_value();
  848. for (int j = 0; j < libs.size(); j++) {
  849. Ref<PListNode> lib_node = libs[j];
  850. if (lib_node.is_null()) {
  851. return ERR_CANT_OPEN;
  852. }
  853. Dictionary lib = lib_node->get_value();
  854. if (lib.has("BinaryPath") && lib.has("LibraryPath") && lib.has("LibraryIdentifier")) {
  855. Ref<PListNode> bpath_node = lib["BinaryPath"];
  856. Ref<PListNode> lpath_node = lib["LibraryPath"];
  857. Ref<PListNode> lid_node = lib["LibraryIdentifier"];
  858. if (bpath_node.is_valid() && lpath_node.is_valid() && lid_node.is_valid()) {
  859. String binary_path = bpath_node->get_value();
  860. String library_identifier = lid_node->get_value();
  861. String file_name = binary_path.get_basename().get_file();
  862. String framework_name = file_name + ".framework";
  863. bpath_node->data_string = framework_name.utf8();
  864. lpath_node->data_string = framework_name.utf8();
  865. if (!filesystem_da->dir_exists(p_destination.path_join(library_identifier))) {
  866. filesystem_da->make_dir_recursive(p_destination.path_join(library_identifier));
  867. }
  868. _convert_to_framework(p_source.path_join(library_identifier).path_join(binary_path), p_destination.path_join(library_identifier).path_join(framework_name), p_id);
  869. if (lib.has("DebugSymbolsPath")) {
  870. Ref<PListNode> dpath_node = lib["DebugSymbolsPath"];
  871. if (dpath_node.is_valid()) {
  872. String dpath = dpath_node->get_value();
  873. if (da->dir_exists(p_source.path_join(library_identifier).path_join(dpath))) {
  874. da->copy_dir(p_source.path_join(library_identifier).path_join(dpath), p_destination.path_join(library_identifier).path_join("dSYMs"));
  875. }
  876. }
  877. }
  878. }
  879. }
  880. }
  881. String info_plist = plist->save_text();
  882. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  883. if (f.is_valid()) {
  884. f->store_string(info_plist);
  885. }
  886. } else {
  887. String file_name = p_destination.get_basename().get_file();
  888. String framework_name = file_name + ".framework";
  889. da->copy(p_source, p_destination.path_join(file_name));
  890. // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib
  891. {
  892. List<String> install_name_args;
  893. install_name_args.push_back("-id");
  894. install_name_args.push_back(String("@rpath").path_join(framework_name).path_join(file_name));
  895. install_name_args.push_back(p_destination.path_join(file_name));
  896. OS::get_singleton()->execute("install_name_tool", install_name_args);
  897. }
  898. // Creating Info.plist
  899. {
  900. String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  901. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  902. "<plist version=\"1.0\">\n"
  903. " <dict>\n"
  904. " <key>CFBundleShortVersionString</key>\n"
  905. " <string>1.0</string>\n"
  906. " <key>CFBundleIdentifier</key>\n"
  907. " <string>$id.framework.$name</string>\n"
  908. " <key>CFBundleName</key>\n"
  909. " <string>$name</string>\n"
  910. " <key>CFBundleExecutable</key>\n"
  911. " <string>$name</string>\n"
  912. " <key>DTPlatformName</key>\n"
  913. " <string>iphoneos</string>\n"
  914. " <key>CFBundleInfoDictionaryVersion</key>\n"
  915. " <string>6.0</string>\n"
  916. " <key>CFBundleVersion</key>\n"
  917. " <string>1</string>\n"
  918. " <key>CFBundlePackageType</key>\n"
  919. " <string>FMWK</string>\n"
  920. " <key>MinimumOSVersion</key>\n"
  921. " <string>12.0</string>\n"
  922. " </dict>\n"
  923. "</plist>";
  924. String info_plist = info_plist_format.replace("$id", p_id).replace("$name", file_name);
  925. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  926. if (f.is_valid()) {
  927. f->store_string(info_plist);
  928. }
  929. }
  930. }
  931. return OK;
  932. }
  933. void EditorExportPlatformIOS::_add_assets_to_project(const String &p_out_dir, const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  934. // that is just a random number, we just need Godot IDs not to clash with
  935. // existing IDs in the project.
  936. PbxId current_id = { 0x58938401, 0, 0 };
  937. String pbx_files;
  938. String pbx_frameworks_build;
  939. String pbx_frameworks_refs;
  940. String pbx_resources_build;
  941. String pbx_resources_refs;
  942. String pbx_embeded_frameworks;
  943. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  944. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = \"$name\"; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  945. for (int i = 0; i < p_additional_assets.size(); ++i) {
  946. String additional_asset_info_format = file_info_format;
  947. String build_id = (++current_id).str();
  948. String ref_id = (++current_id).str();
  949. String framework_id = "";
  950. const IOSExportAsset &asset = p_additional_assets[i];
  951. String type;
  952. if (asset.exported_path.ends_with(".framework")) {
  953. int total_libs = 0;
  954. int static_libs = 0;
  955. int dylibs = 0;
  956. int frameworks = 0;
  957. _check_xcframework_content(p_out_dir.path_join(asset.exported_path), total_libs, static_libs, dylibs, frameworks);
  958. if (asset.should_embed && (static_libs != total_libs)) {
  959. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  960. framework_id = (++current_id).str();
  961. pbx_embeded_frameworks += framework_id + ",\n";
  962. }
  963. type = "wrapper.framework";
  964. } else if (asset.exported_path.ends_with(".xcframework")) {
  965. if (asset.should_embed) {
  966. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  967. framework_id = (++current_id).str();
  968. pbx_embeded_frameworks += framework_id + ",\n";
  969. }
  970. type = "wrapper.xcframework";
  971. } else if (asset.exported_path.ends_with(".dylib")) {
  972. type = "compiled.mach-o.dylib";
  973. } else if (asset.exported_path.ends_with(".a")) {
  974. type = "archive.ar";
  975. } else {
  976. type = "file";
  977. }
  978. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  979. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  980. if (pbx_build.length() > 0) {
  981. pbx_build += ",\n";
  982. pbx_refs += ",\n";
  983. }
  984. pbx_build += build_id;
  985. pbx_refs += ref_id;
  986. Dictionary format_dict;
  987. format_dict["build_id"] = build_id;
  988. format_dict["ref_id"] = ref_id;
  989. format_dict["name"] = asset.exported_path.get_file();
  990. format_dict["file_path"] = asset.exported_path;
  991. format_dict["file_type"] = type;
  992. if (framework_id.length() > 0) {
  993. format_dict["framework_id"] = framework_id;
  994. }
  995. pbx_files += additional_asset_info_format.format(format_dict, "$_");
  996. }
  997. // Note, frameworks like gamekit are always included in our project.pbxprof file
  998. // even if turned off in capabilities.
  999. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  1000. str = str.replace("$additional_pbx_files", pbx_files);
  1001. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  1002. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  1003. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  1004. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  1005. str = str.replace("$pbx_embeded_frameworks", pbx_embeded_frameworks);
  1006. CharString cs = str.utf8();
  1007. p_project_data.resize(cs.size() - 1);
  1008. for (int i = 0; i < cs.size() - 1; i++) {
  1009. p_project_data.write[i] = cs[i];
  1010. }
  1011. }
  1012. Error EditorExportPlatformIOS::_copy_asset(const Ref<EditorExportPreset> &p_preset, 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) {
  1013. String binary_name = p_out_dir.get_file().get_basename();
  1014. Ref<DirAccess> da = DirAccess::create_for_path(p_asset);
  1015. if (da.is_null()) {
  1016. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't open directory: " + p_asset + ".");
  1017. }
  1018. bool file_exists = da->file_exists(p_asset);
  1019. bool dir_exists = da->dir_exists(p_asset);
  1020. if (!file_exists && !dir_exists) {
  1021. return ERR_FILE_NOT_FOUND;
  1022. }
  1023. String base_dir = p_asset.get_base_dir().replace("res://", "").replace(".godot/mono/temp/bin/", "");
  1024. String asset = p_asset.ends_with("/") ? p_asset.left(p_asset.length() - 1) : p_asset;
  1025. String destination_dir;
  1026. String destination;
  1027. String asset_path;
  1028. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1029. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  1030. if (p_is_framework && asset.ends_with(".dylib")) {
  1031. // For iOS we need to turn .dylib into .framework
  1032. // to be able to send application to AppStore
  1033. asset_path = String("dylibs").path_join(base_dir);
  1034. String file_name;
  1035. if (!p_custom_file_name) {
  1036. file_name = p_asset.get_basename().get_file();
  1037. } else {
  1038. file_name = *p_custom_file_name;
  1039. }
  1040. String framework_name = file_name + ".framework";
  1041. asset_path = asset_path.path_join(framework_name);
  1042. destination_dir = p_out_dir.path_join(asset_path);
  1043. destination = destination_dir.path_join(file_name);
  1044. // Convert to framework and copy.
  1045. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1046. if (err) {
  1047. return err;
  1048. }
  1049. } else if (p_is_framework && asset.ends_with(".xcframework")) {
  1050. // For iOS we need to turn .dylib inside .xcframework
  1051. // into .framework to be able to send application to AppStore
  1052. int total_libs = 0;
  1053. int static_libs = 0;
  1054. int dylibs = 0;
  1055. int frameworks = 0;
  1056. _check_xcframework_content(p_asset, total_libs, static_libs, dylibs, frameworks);
  1057. asset_path = String("dylibs").path_join(base_dir);
  1058. String file_name;
  1059. if (!p_custom_file_name) {
  1060. file_name = p_asset.get_file();
  1061. } else {
  1062. file_name = *p_custom_file_name;
  1063. }
  1064. asset_path = asset_path.path_join(file_name);
  1065. destination_dir = p_out_dir.path_join(asset_path);
  1066. destination = destination_dir;
  1067. if (dylibs > 0) {
  1068. // Convert to framework and copy.
  1069. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1070. if (err) {
  1071. return err;
  1072. }
  1073. } else {
  1074. // Copy as is.
  1075. if (!filesystem_da->dir_exists(destination_dir)) {
  1076. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1077. if (make_dir_err) {
  1078. return make_dir_err;
  1079. }
  1080. }
  1081. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1082. if (err) {
  1083. return err;
  1084. }
  1085. }
  1086. } else if (p_is_framework && asset.ends_with(".framework")) {
  1087. // Framework.
  1088. asset_path = String("dylibs").path_join(base_dir);
  1089. String file_name;
  1090. if (!p_custom_file_name) {
  1091. file_name = p_asset.get_file();
  1092. } else {
  1093. file_name = *p_custom_file_name;
  1094. }
  1095. asset_path = asset_path.path_join(file_name);
  1096. destination_dir = p_out_dir.path_join(asset_path);
  1097. destination = destination_dir;
  1098. // Copy as is.
  1099. if (!filesystem_da->dir_exists(destination_dir)) {
  1100. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1101. if (make_dir_err) {
  1102. return make_dir_err;
  1103. }
  1104. }
  1105. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1106. if (err) {
  1107. return err;
  1108. }
  1109. } else {
  1110. // Unknown resource.
  1111. asset_path = base_dir;
  1112. String file_name;
  1113. if (!p_custom_file_name) {
  1114. file_name = p_asset.get_file();
  1115. } else {
  1116. file_name = *p_custom_file_name;
  1117. }
  1118. destination_dir = p_out_dir.path_join(asset_path);
  1119. asset_path = asset_path.path_join(file_name);
  1120. destination = p_out_dir.path_join(asset_path);
  1121. // Copy as is.
  1122. if (!filesystem_da->dir_exists(destination_dir)) {
  1123. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1124. if (make_dir_err) {
  1125. return make_dir_err;
  1126. }
  1127. }
  1128. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1129. if (err) {
  1130. return err;
  1131. }
  1132. }
  1133. if (asset_path.ends_with("/")) {
  1134. asset_path = asset_path.left(asset_path.length() - 1);
  1135. }
  1136. IOSExportAsset exported_asset = { binary_name.path_join(asset_path), p_is_framework, p_should_embed };
  1137. r_exported_assets.push_back(exported_asset);
  1138. return OK;
  1139. }
  1140. Error EditorExportPlatformIOS::_export_additional_assets(const Ref<EditorExportPreset> &p_preset, const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) {
  1141. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  1142. const String &asset = p_assets[f_idx];
  1143. if (asset.begins_with("res://")) {
  1144. Error err = _copy_asset(p_preset, p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1145. ERR_FAIL_COND_V(err != OK, err);
  1146. } else if (ProjectSettings::get_singleton()->localize_path(asset).begins_with("res://")) {
  1147. Error err = _copy_asset(p_preset, p_out_dir, ProjectSettings::get_singleton()->localize_path(asset), nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1148. ERR_FAIL_COND_V(err != OK, err);
  1149. } else {
  1150. // either SDK-builtin or already a part of the export template
  1151. IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed };
  1152. r_exported_assets.push_back(exported_asset);
  1153. }
  1154. }
  1155. return OK;
  1156. }
  1157. Error EditorExportPlatformIOS::_export_additional_assets(const Ref<EditorExportPreset> &p_preset, const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  1158. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1159. for (int i = 0; i < export_plugins.size(); i++) {
  1160. Vector<String> linked_frameworks = export_plugins[i]->get_ios_frameworks();
  1161. Error err = _export_additional_assets(p_preset, p_out_dir, linked_frameworks, true, false, r_exported_assets);
  1162. ERR_FAIL_COND_V(err, err);
  1163. Vector<String> embedded_frameworks = export_plugins[i]->get_ios_embedded_frameworks();
  1164. err = _export_additional_assets(p_preset, p_out_dir, embedded_frameworks, true, true, r_exported_assets);
  1165. ERR_FAIL_COND_V(err, err);
  1166. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1167. for (int j = 0; j < project_static_libs.size(); j++) {
  1168. project_static_libs.write[j] = project_static_libs[j].get_file(); // Only the file name as it's copied to the project
  1169. }
  1170. err = _export_additional_assets(p_preset, p_out_dir, project_static_libs, true, false, r_exported_assets);
  1171. ERR_FAIL_COND_V(err, err);
  1172. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  1173. err = _export_additional_assets(p_preset, p_out_dir, ios_bundle_files, false, false, r_exported_assets);
  1174. ERR_FAIL_COND_V(err, err);
  1175. }
  1176. Vector<String> library_paths;
  1177. for (int i = 0; i < p_libraries.size(); ++i) {
  1178. library_paths.push_back(p_libraries[i].path);
  1179. }
  1180. Error err = _export_additional_assets(p_preset, p_out_dir, library_paths, true, true, r_exported_assets);
  1181. ERR_FAIL_COND_V(err, err);
  1182. return OK;
  1183. }
  1184. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) const {
  1185. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  1186. Vector<String> enabled_archs;
  1187. for (int i = 0; i < all_archs.size(); ++i) {
  1188. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  1189. if (is_enabled) {
  1190. enabled_archs.push_back(all_archs[i].name);
  1191. }
  1192. }
  1193. return enabled_archs;
  1194. }
  1195. 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) {
  1196. String plugin_definition_cpp_code;
  1197. String plugin_initialization_cpp_code;
  1198. String plugin_deinitialization_cpp_code;
  1199. Vector<String> plugin_linked_dependencies;
  1200. Vector<String> plugin_embedded_dependencies;
  1201. Vector<String> plugin_files;
  1202. Vector<PluginConfigIOS> enabled_plugins = get_enabled_plugins(p_preset);
  1203. Vector<String> added_linked_dependenciy_names;
  1204. Vector<String> added_embedded_dependenciy_names;
  1205. HashMap<String, String> plist_values;
  1206. HashSet<String> plugin_linker_flags;
  1207. Error err;
  1208. for (int i = 0; i < enabled_plugins.size(); i++) {
  1209. PluginConfigIOS plugin = enabled_plugins[i];
  1210. // Export plugin binary.
  1211. String plugin_main_binary = PluginConfigIOS::get_plugin_main_binary(plugin, p_debug);
  1212. String plugin_binary_result_file = plugin.binary.get_file();
  1213. // We shouldn't embed .xcframework that contains static libraries.
  1214. // Static libraries are not embedded anyway.
  1215. err = _copy_asset(p_preset, dest_dir, plugin_main_binary, &plugin_binary_result_file, true, false, r_exported_assets);
  1216. ERR_FAIL_COND_V(err != OK, err);
  1217. // Adding dependencies.
  1218. // Use separate container for names to check for duplicates.
  1219. for (int j = 0; j < plugin.linked_dependencies.size(); j++) {
  1220. String dependency = plugin.linked_dependencies[j];
  1221. String name = dependency.get_file();
  1222. if (added_linked_dependenciy_names.has(name)) {
  1223. continue;
  1224. }
  1225. added_linked_dependenciy_names.push_back(name);
  1226. plugin_linked_dependencies.push_back(dependency);
  1227. }
  1228. for (int j = 0; j < plugin.system_dependencies.size(); j++) {
  1229. String dependency = plugin.system_dependencies[j];
  1230. String name = dependency.get_file();
  1231. if (added_linked_dependenciy_names.has(name)) {
  1232. continue;
  1233. }
  1234. added_linked_dependenciy_names.push_back(name);
  1235. plugin_linked_dependencies.push_back(dependency);
  1236. }
  1237. for (int j = 0; j < plugin.embedded_dependencies.size(); j++) {
  1238. String dependency = plugin.embedded_dependencies[j];
  1239. String name = dependency.get_file();
  1240. if (added_embedded_dependenciy_names.has(name)) {
  1241. continue;
  1242. }
  1243. added_embedded_dependenciy_names.push_back(name);
  1244. plugin_embedded_dependencies.push_back(dependency);
  1245. }
  1246. plugin_files.append_array(plugin.files_to_copy);
  1247. // Capabilities
  1248. // Also checking for duplicates.
  1249. for (int j = 0; j < plugin.capabilities.size(); j++) {
  1250. String capability = plugin.capabilities[j];
  1251. if (p_config_data.capabilities.has(capability)) {
  1252. continue;
  1253. }
  1254. p_config_data.capabilities.push_back(capability);
  1255. }
  1256. // Linker flags
  1257. // Checking duplicates
  1258. for (int j = 0; j < plugin.linker_flags.size(); j++) {
  1259. String linker_flag = plugin.linker_flags[j];
  1260. plugin_linker_flags.insert(linker_flag);
  1261. }
  1262. // Plist
  1263. // Using hash map container to remove duplicates
  1264. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  1265. String key = E.key;
  1266. const PluginConfigIOS::PlistItem &item = E.value;
  1267. String value;
  1268. switch (item.type) {
  1269. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  1270. String preset_name = "plugins_plist/" + key;
  1271. String input_value = p_preset->get(preset_name);
  1272. value = "<string>" + input_value + "</string>";
  1273. } break;
  1274. default:
  1275. value = item.value;
  1276. break;
  1277. }
  1278. if (key.is_empty() || value.is_empty()) {
  1279. continue;
  1280. }
  1281. String plist_key = "<key>" + key + "</key>";
  1282. plist_values[plist_key] = value;
  1283. }
  1284. // CPP Code
  1285. String definition_comment = "// Plugin: " + plugin.name + "\n";
  1286. String initialization_method = plugin.initialization_method + "();\n";
  1287. String deinitialization_method = plugin.deinitialization_method + "();\n";
  1288. plugin_definition_cpp_code += definition_comment +
  1289. "extern void " + initialization_method +
  1290. "extern void " + deinitialization_method + "\n";
  1291. plugin_initialization_cpp_code += "\t" + initialization_method;
  1292. plugin_deinitialization_cpp_code += "\t" + deinitialization_method;
  1293. if (plugin.use_swift_runtime) {
  1294. p_config_data.use_swift_runtime = true;
  1295. }
  1296. }
  1297. // Updating `Info.plist`
  1298. {
  1299. for (const KeyValue<String, String> &E : plist_values) {
  1300. String key = E.key;
  1301. String value = E.value;
  1302. if (key.is_empty() || value.is_empty()) {
  1303. continue;
  1304. }
  1305. p_config_data.plist_content += key + value + "\n";
  1306. }
  1307. }
  1308. // Export files
  1309. {
  1310. // Export linked plugin dependency
  1311. err = _export_additional_assets(p_preset, dest_dir, plugin_linked_dependencies, true, false, r_exported_assets);
  1312. ERR_FAIL_COND_V(err != OK, err);
  1313. // Export embedded plugin dependency
  1314. err = _export_additional_assets(p_preset, dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets);
  1315. ERR_FAIL_COND_V(err != OK, err);
  1316. // Export plugin files
  1317. err = _export_additional_assets(p_preset, dest_dir, plugin_files, false, false, r_exported_assets);
  1318. ERR_FAIL_COND_V(err != OK, err);
  1319. }
  1320. // Update CPP
  1321. {
  1322. Dictionary plugin_format;
  1323. plugin_format["definition"] = plugin_definition_cpp_code;
  1324. plugin_format["initialization"] = plugin_initialization_cpp_code;
  1325. plugin_format["deinitialization"] = plugin_deinitialization_cpp_code;
  1326. String plugin_cpp_code = "\n// Godot Plugins\n"
  1327. "void godot_ios_plugins_initialize();\n"
  1328. "void godot_ios_plugins_deinitialize();\n"
  1329. "// Exported Plugins\n\n"
  1330. "$definition"
  1331. "// Use Plugins\n"
  1332. "void godot_ios_plugins_initialize() {\n"
  1333. "$initialization"
  1334. "}\n\n"
  1335. "void godot_ios_plugins_deinitialize() {\n"
  1336. "$deinitialization"
  1337. "}\n";
  1338. p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_");
  1339. }
  1340. // Update Linker Flag Values
  1341. {
  1342. String result_linker_flags = " ";
  1343. for (const String &E : plugin_linker_flags) {
  1344. const String &flag = E;
  1345. if (flag.length() == 0) {
  1346. continue;
  1347. }
  1348. if (result_linker_flags.length() > 0) {
  1349. result_linker_flags += ' ';
  1350. }
  1351. result_linker_flags += flag;
  1352. }
  1353. result_linker_flags = result_linker_flags.replace("\"", "\\\"");
  1354. p_config_data.linker_flags += result_linker_flags;
  1355. }
  1356. return OK;
  1357. }
  1358. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1359. return _export_project_helper(p_preset, p_debug, p_path, p_flags, false, false);
  1360. }
  1361. Error EditorExportPlatformIOS::_export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags, bool p_simulator, bool p_oneclick) {
  1362. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1363. const String dest_dir = p_path.get_base_dir() + "/";
  1364. const String binary_name = p_path.get_file().get_basename();
  1365. const String binary_dir = dest_dir + binary_name;
  1366. if (!DirAccess::exists(dest_dir)) {
  1367. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), dest_dir));
  1368. return ERR_FILE_BAD_PATH;
  1369. }
  1370. bool export_project_only = p_preset->get("application/export_project_only");
  1371. if (p_oneclick) {
  1372. export_project_only = false; // Skip for one-click deploy.
  1373. }
  1374. EditorProgress ep("export", export_project_only ? TTR("Exporting for iOS (Project Files Only)") : TTR("Exporting for iOS"), export_project_only ? 2 : 5, true);
  1375. String team_id = p_preset->get("application/app_store_team_id");
  1376. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  1377. String src_pkg_name;
  1378. if (p_debug) {
  1379. src_pkg_name = p_preset->get("custom_template/debug");
  1380. } else {
  1381. src_pkg_name = p_preset->get("custom_template/release");
  1382. }
  1383. if (src_pkg_name.is_empty()) {
  1384. String err;
  1385. src_pkg_name = find_export_template("ios.zip", &err);
  1386. if (src_pkg_name.is_empty()) {
  1387. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Export template not found."));
  1388. return ERR_FILE_NOT_FOUND;
  1389. }
  1390. }
  1391. {
  1392. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1393. if (da.is_valid()) {
  1394. String current_dir = da->get_current_dir();
  1395. // remove leftovers from last export so they don't interfere
  1396. // in case some files are no longer needed
  1397. if (da->change_dir(binary_dir + ".xcodeproj") == OK) {
  1398. da->erase_contents_recursive();
  1399. }
  1400. if (da->change_dir(binary_dir) == OK) {
  1401. da->erase_contents_recursive();
  1402. }
  1403. da->change_dir(current_dir);
  1404. if (!da->dir_exists(binary_dir)) {
  1405. Error err = da->make_dir(binary_dir);
  1406. if (err != OK) {
  1407. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create the directory: \"%s\""), binary_dir));
  1408. return err;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. if (ep.step("Making .pck", 0)) {
  1414. return ERR_SKIP;
  1415. }
  1416. String pack_path = binary_dir + ".pck";
  1417. Vector<SharedObject> libraries;
  1418. Error err = save_pack(p_preset, p_debug, pack_path, &libraries);
  1419. if (err) {
  1420. // Message is supplied by the subroutine method.
  1421. return err;
  1422. }
  1423. if (ep.step("Extracting and configuring Xcode project", 1)) {
  1424. return ERR_SKIP;
  1425. }
  1426. String library_to_use = "libgodot.ios." + String(p_debug ? "debug" : "release") + ".xcframework";
  1427. print_line("Static framework: " + library_to_use);
  1428. String pkg_name;
  1429. if (String(GLOBAL_GET("application/config/name")) != "") {
  1430. pkg_name = String(GLOBAL_GET("application/config/name"));
  1431. } else {
  1432. pkg_name = "Unnamed";
  1433. }
  1434. bool found_library = false;
  1435. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  1436. HashSet<String> files_to_parse;
  1437. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  1438. files_to_parse.insert(project_file);
  1439. files_to_parse.insert("godot_ios/export_options.plist");
  1440. files_to_parse.insert("godot_ios/dummy.cpp");
  1441. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  1442. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  1443. files_to_parse.insert("godot_ios/godot_ios.entitlements");
  1444. files_to_parse.insert("godot_ios/Launch Screen.storyboard");
  1445. IOSConfigData config_data = {
  1446. pkg_name,
  1447. binary_name,
  1448. _get_additional_plist_content(),
  1449. String(" ").join(_get_preset_architectures(p_preset)),
  1450. _get_linker_flags(),
  1451. _get_cpp_code(),
  1452. "",
  1453. "",
  1454. "",
  1455. "",
  1456. Vector<String>(),
  1457. false
  1458. };
  1459. config_data.plist_content += p_preset->get("application/additional_plist_content").operator String() + "\n";
  1460. Vector<IOSExportAsset> assets;
  1461. Ref<DirAccess> tmp_app_path = DirAccess::create_for_path(dest_dir);
  1462. if (tmp_app_path.is_null()) {
  1463. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), dest_dir));
  1464. return ERR_CANT_CREATE;
  1465. }
  1466. print_line("Unzipping...");
  1467. Ref<FileAccess> io_fa;
  1468. zlib_filefunc_def io = zipio_create_io(&io_fa);
  1469. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  1470. if (!src_pkg_zip) {
  1471. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Could not open export template (not a zip file?): \"%s\".", src_pkg_name));
  1472. return ERR_CANT_OPEN;
  1473. }
  1474. err = _export_ios_plugins(p_preset, config_data, binary_dir, assets, p_debug);
  1475. if (err != OK) {
  1476. // TODO: Improve error reporting by using `add_message` throughout all methods called via `_export_ios_plugins`.
  1477. // For now a generic top level message would be fine, but we're ought to use proper reporting here instead of
  1478. // just fail macros and non-descriptive error return values.
  1479. add_message(EXPORT_MESSAGE_ERROR, TTR("iOS Plugins"), vformat(TTR("Failed to export iOS plugins with code %d. Please check the output log."), err));
  1480. return err;
  1481. }
  1482. //export rest of the files
  1483. int ret = unzGoToFirstFile(src_pkg_zip);
  1484. Vector<uint8_t> project_file_data;
  1485. while (ret == UNZ_OK) {
  1486. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1487. bool is_execute = false;
  1488. #endif
  1489. //get filename
  1490. unz_file_info info;
  1491. char fname[16384];
  1492. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  1493. if (ret != UNZ_OK) {
  1494. break;
  1495. }
  1496. String file = String::utf8(fname);
  1497. print_line("READ: " + file);
  1498. Vector<uint8_t> data;
  1499. data.resize(info.uncompressed_size);
  1500. //read
  1501. unzOpenCurrentFile(src_pkg_zip);
  1502. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  1503. unzCloseCurrentFile(src_pkg_zip);
  1504. //write
  1505. if (files_to_parse.has(file)) {
  1506. _fix_config_file(p_preset, data, config_data, p_debug);
  1507. } else if (file.begins_with("libgodot.ios")) {
  1508. if (!file.begins_with(library_to_use) || file.ends_with(String("/empty"))) {
  1509. ret = unzGoToNextFile(src_pkg_zip);
  1510. continue; //ignore!
  1511. }
  1512. found_library = true;
  1513. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1514. is_execute = true;
  1515. #endif
  1516. file = file.replace(library_to_use, binary_name + ".xcframework");
  1517. }
  1518. if (file == project_file) {
  1519. project_file_data = data;
  1520. }
  1521. ///@TODO need to parse logo files
  1522. if (data.size() > 0) {
  1523. file = file.replace("godot_ios", binary_name);
  1524. print_line("ADDING: " + file + " size: " + itos(data.size()));
  1525. /* write it into our folder structure */
  1526. file = dest_dir + file;
  1527. /* make sure this folder exists */
  1528. String dir_name = file.get_base_dir();
  1529. if (!tmp_app_path->dir_exists(dir_name)) {
  1530. print_line("Creating " + dir_name);
  1531. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  1532. if (dir_err) {
  1533. unzClose(src_pkg_zip);
  1534. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), dir_name));
  1535. return ERR_CANT_CREATE;
  1536. }
  1537. }
  1538. /* write the file */
  1539. {
  1540. Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);
  1541. if (f.is_null()) {
  1542. unzClose(src_pkg_zip);
  1543. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), file));
  1544. return ERR_CANT_CREATE;
  1545. };
  1546. f->store_buffer(data.ptr(), data.size());
  1547. }
  1548. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1549. if (is_execute) {
  1550. // we need execute rights on this file
  1551. chmod(file.utf8().get_data(), 0755);
  1552. }
  1553. #endif
  1554. }
  1555. ret = unzGoToNextFile(src_pkg_zip);
  1556. }
  1557. /* we're done with our source zip */
  1558. unzClose(src_pkg_zip);
  1559. if (!found_library) {
  1560. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Requested template library '%s' not found. It might be missing from your template archive."), library_to_use));
  1561. return ERR_FILE_NOT_FOUND;
  1562. }
  1563. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  1564. Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
  1565. Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
  1566. Dictionary photolibrary_usage_descriptions = p_preset->get("privacy/photolibrary_usage_description_localized");
  1567. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  1568. if (translations.size() > 0) {
  1569. {
  1570. String fname = binary_dir + "/en.lproj";
  1571. tmp_app_path->make_dir_recursive(fname);
  1572. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1573. f->store_line("/* Localized versions of Info.plist keys */");
  1574. f->store_line("");
  1575. f->store_line("CFBundleDisplayName = \"" + GLOBAL_GET("application/config/name").operator String() + "\";");
  1576. f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
  1577. f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
  1578. f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photolibrary_usage_description").operator String() + "\";");
  1579. }
  1580. HashSet<String> languages;
  1581. for (const String &E : translations) {
  1582. Ref<Translation> tr = ResourceLoader::load(E);
  1583. if (tr.is_valid() && tr->get_locale() != "en") {
  1584. languages.insert(tr->get_locale());
  1585. }
  1586. }
  1587. for (const String &lang : languages) {
  1588. String fname = binary_dir + "/" + lang + ".lproj";
  1589. tmp_app_path->make_dir_recursive(fname);
  1590. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1591. f->store_line("/* Localized versions of Info.plist keys */");
  1592. f->store_line("");
  1593. if (appnames.has(lang)) {
  1594. f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
  1595. }
  1596. if (camera_usage_descriptions.has(lang)) {
  1597. f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
  1598. }
  1599. if (microphone_usage_descriptions.has(lang)) {
  1600. f->store_line("NSMicrophoneUsageDescription = \"" + microphone_usage_descriptions[lang].operator String() + "\";");
  1601. }
  1602. if (photolibrary_usage_descriptions.has(lang)) {
  1603. f->store_line("NSPhotoLibraryUsageDescription = \"" + photolibrary_usage_descriptions[lang].operator String() + "\";");
  1604. }
  1605. }
  1606. }
  1607. // Copy project static libs to the project
  1608. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1609. for (int i = 0; i < export_plugins.size(); i++) {
  1610. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1611. for (int j = 0; j < project_static_libs.size(); j++) {
  1612. const String &static_lib_path = project_static_libs[j];
  1613. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  1614. Error lib_copy_err = tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  1615. if (lib_copy_err != OK) {
  1616. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not copy a file at path \"%s\" to \"%s\"."), static_lib_path, dest_lib_file_path));
  1617. return lib_copy_err;
  1618. }
  1619. }
  1620. }
  1621. String iconset_dir = binary_dir + "/Images.xcassets/AppIcon.appiconset/";
  1622. err = OK;
  1623. if (!tmp_app_path->dir_exists(iconset_dir)) {
  1624. err = tmp_app_path->make_dir_recursive(iconset_dir);
  1625. }
  1626. if (err != OK) {
  1627. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), iconset_dir));
  1628. return err;
  1629. }
  1630. err = _export_icons(p_preset, iconset_dir);
  1631. if (err != OK) {
  1632. // Message is supplied by the subroutine method.
  1633. return err;
  1634. }
  1635. {
  1636. String splash_image_path = binary_dir + "/Images.xcassets/SplashImage.imageset/";
  1637. Ref<DirAccess> launch_screen_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1638. if (launch_screen_da.is_null()) {
  1639. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not access the filesystem."));
  1640. return ERR_CANT_CREATE;
  1641. }
  1642. print_line("Exporting launch screen storyboard");
  1643. err = _export_loading_screen_file(p_preset, splash_image_path);
  1644. if (err != OK) {
  1645. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create a file at path \"%s\" with code %d."), splash_image_path, err));
  1646. }
  1647. }
  1648. if (err != OK) {
  1649. return err;
  1650. }
  1651. print_line("Exporting additional assets");
  1652. _export_additional_assets(p_preset, binary_dir, libraries, assets);
  1653. _add_assets_to_project(dest_dir, p_preset, project_file_data, assets);
  1654. String project_file_name = binary_dir + ".xcodeproj/project.pbxproj";
  1655. {
  1656. Ref<FileAccess> f = FileAccess::open(project_file_name, FileAccess::WRITE);
  1657. if (f.is_null()) {
  1658. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), project_file_name));
  1659. return ERR_CANT_CREATE;
  1660. };
  1661. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  1662. }
  1663. #ifdef MACOS_ENABLED
  1664. {
  1665. if (ep.step("Code-signing dylibs", 2)) {
  1666. return ERR_SKIP;
  1667. }
  1668. Ref<DirAccess> dylibs_dir = DirAccess::open(binary_dir + "/dylibs");
  1669. ERR_FAIL_COND_V(dylibs_dir.is_null(), ERR_CANT_OPEN);
  1670. CodesignData codesign_data(p_preset, p_debug);
  1671. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  1672. if (err != OK) {
  1673. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Code signing failed, see editor log for details."));
  1674. return err;
  1675. }
  1676. }
  1677. if (export_project_only) {
  1678. return OK;
  1679. }
  1680. if (ep.step("Making .xcarchive", 3)) {
  1681. return ERR_SKIP;
  1682. }
  1683. String archive_path = p_path.get_basename() + ".xcarchive";
  1684. List<String> archive_args;
  1685. archive_args.push_back("-project");
  1686. archive_args.push_back(binary_dir + ".xcodeproj");
  1687. archive_args.push_back("-scheme");
  1688. archive_args.push_back(binary_name);
  1689. archive_args.push_back("-sdk");
  1690. if (p_simulator) {
  1691. archive_args.push_back("iphonesimulator");
  1692. } else {
  1693. archive_args.push_back("iphoneos");
  1694. }
  1695. archive_args.push_back("-configuration");
  1696. archive_args.push_back(p_debug ? "Debug" : "Release");
  1697. archive_args.push_back("-destination");
  1698. if (p_simulator) {
  1699. archive_args.push_back("generic/platform=iOS Simulator");
  1700. } else {
  1701. archive_args.push_back("generic/platform=iOS");
  1702. }
  1703. archive_args.push_back("archive");
  1704. archive_args.push_back("-allowProvisioningUpdates");
  1705. archive_args.push_back("-archivePath");
  1706. archive_args.push_back(archive_path);
  1707. String archive_str;
  1708. err = OS::get_singleton()->execute("xcodebuild", archive_args, &archive_str, nullptr, true);
  1709. if (err != OK) {
  1710. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  1711. return err;
  1712. }
  1713. print_line("xcodebuild (.xcarchive):\n" + archive_str);
  1714. if (!archive_str.contains("** ARCHIVE SUCCEEDED **")) {
  1715. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR("Xcode project build failed, see editor log for details."));
  1716. return FAILED;
  1717. }
  1718. if (!p_oneclick) {
  1719. if (ep.step("Making .ipa", 4)) {
  1720. return ERR_SKIP;
  1721. }
  1722. List<String> export_args;
  1723. export_args.push_back("-exportArchive");
  1724. export_args.push_back("-archivePath");
  1725. export_args.push_back(archive_path);
  1726. export_args.push_back("-exportOptionsPlist");
  1727. export_args.push_back(binary_dir + "/export_options.plist");
  1728. export_args.push_back("-allowProvisioningUpdates");
  1729. export_args.push_back("-exportPath");
  1730. export_args.push_back(dest_dir);
  1731. String export_str;
  1732. err = OS::get_singleton()->execute("xcodebuild", export_args, &export_str, nullptr, true);
  1733. if (err != OK) {
  1734. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  1735. return err;
  1736. }
  1737. print_line("xcodebuild (.ipa):\n" + export_str);
  1738. if (!export_str.contains("** EXPORT SUCCEEDED **")) {
  1739. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR(".ipa export failed, see editor log for details."));
  1740. return FAILED;
  1741. }
  1742. }
  1743. #else
  1744. add_message(EXPORT_MESSAGE_WARNING, TTR("Xcode Build"), TTR(".ipa can only be built on macOS. Leaving Xcode project without building the package."));
  1745. #endif
  1746. return OK;
  1747. }
  1748. bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  1749. #if defined(MODULE_MONO_ENABLED) && !defined(MACOS_ENABLED)
  1750. // TODO: Remove this restriction when we don't rely on macOS tools to package up the native libraries anymore.
  1751. r_error += TTR("Exporting to iOS when using C#/.NET is experimental and requires macOS.") + "\n";
  1752. return false;
  1753. #else
  1754. String err;
  1755. bool valid = false;
  1756. #if defined(MODULE_MONO_ENABLED)
  1757. // iOS export is still a work in progress, keep a message as a warning.
  1758. err += TTR("Exporting to iOS when using C#/.NET is experimental.") + "\n";
  1759. #endif
  1760. // Look for export templates (first official, and if defined custom templates).
  1761. bool dvalid = exists_export_template("ios.zip", &err);
  1762. bool rvalid = dvalid; // Both in the same ZIP.
  1763. if (p_preset->get("custom_template/debug") != "") {
  1764. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1765. if (!dvalid) {
  1766. err += TTR("Custom debug template not found.") + "\n";
  1767. }
  1768. }
  1769. if (p_preset->get("custom_template/release") != "") {
  1770. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1771. if (!rvalid) {
  1772. err += TTR("Custom release template not found.") + "\n";
  1773. }
  1774. }
  1775. valid = dvalid || rvalid;
  1776. r_missing_templates = !valid;
  1777. const String &additional_plist_content = p_preset->get("application/additional_plist_content");
  1778. if (!additional_plist_content.is_empty()) {
  1779. const String &plist = vformat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1780. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
  1781. "<plist version=\"1.0\">"
  1782. "<dict>\n"
  1783. "%s\n"
  1784. "</dict>\n"
  1785. "</plist>\n",
  1786. additional_plist_content);
  1787. String plist_err;
  1788. Ref<PList> plist_parser;
  1789. plist_parser.instantiate();
  1790. if (!plist_parser->load_string(plist, plist_err)) {
  1791. err += TTR("Invalid additional PList content: ") + plist_err + "\n";
  1792. valid = false;
  1793. }
  1794. }
  1795. if (!err.is_empty()) {
  1796. r_error = err;
  1797. }
  1798. return valid;
  1799. #endif // !(MODULE_MONO_ENABLED && !MACOS_ENABLED)
  1800. }
  1801. bool EditorExportPlatformIOS::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  1802. String err;
  1803. bool valid = true;
  1804. // Validate the project configuration.
  1805. List<ExportOption> options;
  1806. get_export_options(&options);
  1807. for (const EditorExportPlatform::ExportOption &E : options) {
  1808. if (get_export_option_visibility(p_preset.ptr(), E.option.name)) {
  1809. String warn = get_export_option_warning(p_preset.ptr(), E.option.name);
  1810. if (!warn.is_empty()) {
  1811. err += warn + "\n";
  1812. if (E.required) {
  1813. valid = false;
  1814. }
  1815. }
  1816. }
  1817. }
  1818. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  1819. valid = false;
  1820. }
  1821. if (!err.is_empty()) {
  1822. r_error = err;
  1823. }
  1824. return valid;
  1825. }
  1826. int EditorExportPlatformIOS::get_options_count() const {
  1827. MutexLock lock(device_lock);
  1828. return devices.size();
  1829. }
  1830. String EditorExportPlatformIOS::get_options_tooltip() const {
  1831. return TTR("Select device from the list");
  1832. }
  1833. Ref<ImageTexture> EditorExportPlatformIOS::get_option_icon(int p_index) const {
  1834. MutexLock lock(device_lock);
  1835. Ref<ImageTexture> icon;
  1836. if (p_index >= 0 || p_index < devices.size()) {
  1837. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  1838. if (theme.is_valid()) {
  1839. if (devices[p_index].simulator) {
  1840. icon = theme->get_icon("IOSSimulator", EditorStringName(EditorIcons));
  1841. } else if (devices[p_index].wifi) {
  1842. icon = theme->get_icon("IOSDeviceWireless", EditorStringName(EditorIcons));
  1843. } else {
  1844. icon = theme->get_icon("IOSDeviceWired", EditorStringName(EditorIcons));
  1845. }
  1846. }
  1847. }
  1848. return icon;
  1849. }
  1850. String EditorExportPlatformIOS::get_option_label(int p_index) const {
  1851. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1852. MutexLock lock(device_lock);
  1853. return devices[p_index].name;
  1854. }
  1855. String EditorExportPlatformIOS::get_option_tooltip(int p_index) const {
  1856. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1857. MutexLock lock(device_lock);
  1858. return "UUID: " + devices[p_index].id;
  1859. }
  1860. bool EditorExportPlatformIOS::is_package_name_valid(const String &p_package, String *r_error) const {
  1861. String pname = p_package;
  1862. if (pname.length() == 0) {
  1863. if (r_error) {
  1864. *r_error = TTR("Identifier is missing.");
  1865. }
  1866. return false;
  1867. }
  1868. for (int i = 0; i < pname.length(); i++) {
  1869. char32_t c = pname[i];
  1870. if (!(is_ascii_alphanumeric_char(c) || c == '-' || c == '.')) {
  1871. if (r_error) {
  1872. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  1873. }
  1874. return false;
  1875. }
  1876. }
  1877. return true;
  1878. }
  1879. #ifdef MACOS_ENABLED
  1880. bool EditorExportPlatformIOS::_check_xcode_install() {
  1881. static bool xcode_found = false;
  1882. if (!xcode_found) {
  1883. Vector<String> mdfind_paths;
  1884. List<String> mdfind_args;
  1885. mdfind_args.push_back("kMDItemCFBundleIdentifier=com.apple.dt.Xcode");
  1886. String output;
  1887. Error err = OS::get_singleton()->execute("mdfind", mdfind_args, &output);
  1888. if (err == OK) {
  1889. mdfind_paths = output.split("\n");
  1890. }
  1891. for (const String &found_path : mdfind_paths) {
  1892. xcode_found = !found_path.is_empty() && DirAccess::dir_exists_absolute(found_path.strip_edges());
  1893. if (xcode_found) {
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. return xcode_found;
  1899. }
  1900. void EditorExportPlatformIOS::_check_for_changes_poll_thread(void *ud) {
  1901. EditorExportPlatformIOS *ea = static_cast<EditorExportPlatformIOS *>(ud);
  1902. while (!ea->quit_request.is_set()) {
  1903. // Nothing to do if we already know the plugins have changed.
  1904. if (!ea->plugins_changed.is_set()) {
  1905. MutexLock lock(ea->plugins_lock);
  1906. Vector<PluginConfigIOS> loaded_plugins = get_plugins();
  1907. if (ea->plugins.size() != loaded_plugins.size()) {
  1908. ea->plugins_changed.set();
  1909. } else {
  1910. for (int i = 0; i < ea->plugins.size(); i++) {
  1911. if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) {
  1912. ea->plugins_changed.set();
  1913. break;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. // Check for devices updates.
  1919. Vector<Device> ldevices;
  1920. // Enum real devices (via ios_deploy, pre Xcode 15).
  1921. String idepl = EDITOR_GET("export/ios/ios_deploy");
  1922. if (!idepl.is_empty()) {
  1923. String devices;
  1924. List<String> args;
  1925. args.push_back("-c");
  1926. args.push_back("-timeout");
  1927. args.push_back("1");
  1928. args.push_back("-j");
  1929. args.push_back("-u");
  1930. args.push_back("-I");
  1931. int ec = 0;
  1932. Error err = OS::get_singleton()->execute(idepl, args, &devices, &ec, true);
  1933. if (err == OK && ec == 0) {
  1934. Ref<JSON> json;
  1935. json.instantiate();
  1936. devices = "{ \"devices\":[" + devices.replace("}{", "},{") + "]}";
  1937. err = json->parse(devices);
  1938. if (err == OK) {
  1939. Dictionary data = json->get_data();
  1940. Array devices = data["devices"];
  1941. for (int i = 0; i < devices.size(); i++) {
  1942. Dictionary device_event = devices[i];
  1943. if (device_event["Event"] == "DeviceDetected") {
  1944. Dictionary device_info = device_event["Device"];
  1945. Device nd;
  1946. nd.id = device_info["DeviceIdentifier"];
  1947. nd.name = device_info["DeviceName"].operator String() + " (ios_deploy, " + ((device_event["Interface"] == "WIFI") ? "network" : "wired") + ")";
  1948. nd.wifi = device_event["Interface"] == "WIFI";
  1949. nd.use_ios_deploy = true;
  1950. nd.simulator = false;
  1951. ldevices.push_back(nd);
  1952. }
  1953. }
  1954. }
  1955. }
  1956. }
  1957. // Enum simulators.
  1958. if (_check_xcode_install() && (FileAccess::exists("/usr/bin/xcrun") || FileAccess::exists("/bin/xcrun"))) {
  1959. {
  1960. String devices;
  1961. List<String> args;
  1962. args.push_back("devicectl");
  1963. args.push_back("list");
  1964. args.push_back("devices");
  1965. args.push_back("-j");
  1966. args.push_back("-");
  1967. args.push_back("-q");
  1968. int ec = 0;
  1969. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  1970. if (err == OK && ec == 0) {
  1971. Ref<JSON> json;
  1972. json.instantiate();
  1973. err = json->parse(devices);
  1974. if (err == OK) {
  1975. const Dictionary &data = json->get_data();
  1976. const Dictionary &result = data["result"];
  1977. const Array &devices = result["devices"];
  1978. for (int i = 0; i < devices.size(); i++) {
  1979. const Dictionary &device_info = devices[i];
  1980. const Dictionary &conn_props = device_info["connectionProperties"];
  1981. const Dictionary &dev_props = device_info["deviceProperties"];
  1982. if (conn_props["pairingState"] == "paired" && dev_props["developerModeStatus"] == "enabled") {
  1983. Device nd;
  1984. nd.id = device_info["identifier"];
  1985. nd.name = dev_props["name"].operator String() + " (devicectl, " + ((conn_props["transportType"] == "localNetwork") ? "network" : "wired") + ")";
  1986. nd.wifi = conn_props["transportType"] == "localNetwork";
  1987. nd.simulator = false;
  1988. ldevices.push_back(nd);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. // Enum simulators.
  1995. {
  1996. String devices;
  1997. List<String> args;
  1998. args.push_back("simctl");
  1999. args.push_back("list");
  2000. args.push_back("devices");
  2001. args.push_back("-j");
  2002. int ec = 0;
  2003. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  2004. if (err == OK && ec == 0) {
  2005. Ref<JSON> json;
  2006. json.instantiate();
  2007. err = json->parse(devices);
  2008. if (err == OK) {
  2009. const Dictionary &data = json->get_data();
  2010. const Dictionary &devices = data["devices"];
  2011. for (const Variant *key = devices.next(nullptr); key; key = devices.next(key)) {
  2012. const Array &os_devices = devices[*key];
  2013. for (int i = 0; i < os_devices.size(); i++) {
  2014. const Dictionary &device_info = os_devices[i];
  2015. if (device_info["isAvailable"].operator bool() && device_info["state"] == "Booted") {
  2016. Device nd;
  2017. nd.id = device_info["udid"];
  2018. nd.name = device_info["name"].operator String() + " (simulator)";
  2019. nd.simulator = true;
  2020. ldevices.push_back(nd);
  2021. }
  2022. }
  2023. }
  2024. }
  2025. }
  2026. }
  2027. }
  2028. // Update device list.
  2029. {
  2030. MutexLock lock(ea->device_lock);
  2031. bool different = false;
  2032. if (ea->devices.size() != ldevices.size()) {
  2033. different = true;
  2034. } else {
  2035. for (int i = 0; i < ea->devices.size(); i++) {
  2036. if (ea->devices[i].id != ldevices[i].id) {
  2037. different = true;
  2038. break;
  2039. }
  2040. }
  2041. }
  2042. if (different) {
  2043. ea->devices = ldevices;
  2044. ea->devices_changed.set();
  2045. }
  2046. }
  2047. uint64_t sleep = 200;
  2048. uint64_t wait = 3000000;
  2049. uint64_t time = OS::get_singleton()->get_ticks_usec();
  2050. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  2051. OS::get_singleton()->delay_usec(1000 * sleep);
  2052. if (ea->quit_request.is_set()) {
  2053. break;
  2054. }
  2055. }
  2056. }
  2057. }
  2058. #endif
  2059. Error EditorExportPlatformIOS::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  2060. #ifdef MACOS_ENABLED
  2061. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  2062. String can_export_error;
  2063. bool can_export_missing_templates;
  2064. if (!can_export(p_preset, can_export_error, can_export_missing_templates)) {
  2065. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error);
  2066. return ERR_UNCONFIGURED;
  2067. }
  2068. MutexLock lock(device_lock);
  2069. EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device].name), 3);
  2070. String id = "tmpexport." + uitos(OS::get_singleton()->get_unix_time());
  2071. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2072. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + EditorPaths::get_singleton()->get_cache_dir() + "'.");
  2073. filesystem_da->make_dir_recursive(EditorPaths::get_singleton()->get_cache_dir().path_join(id));
  2074. String tmp_export_path = EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.ipa");
  2075. #define CLEANUP_AND_RETURN(m_err) \
  2076. { \
  2077. if (filesystem_da->change_dir(EditorPaths::get_singleton()->get_cache_dir().path_join(id)) == OK) { \
  2078. filesystem_da->erase_contents_recursive(); \
  2079. filesystem_da->change_dir(".."); \
  2080. filesystem_da->remove(id); \
  2081. } \
  2082. return m_err; \
  2083. } \
  2084. ((void)0)
  2085. Device dev = devices[p_device];
  2086. // Export before sending to device.
  2087. Error err = _export_project_helper(p_preset, true, tmp_export_path, p_debug_flags, dev.simulator, true);
  2088. if (err != OK) {
  2089. CLEANUP_AND_RETURN(err);
  2090. }
  2091. Vector<String> cmd_args_list;
  2092. String host = EDITOR_GET("network/debug/remote_host");
  2093. int remote_port = (int)EDITOR_GET("network/debug/remote_port");
  2094. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST) {
  2095. host = "localhost";
  2096. }
  2097. if (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2098. int port = EDITOR_GET("filesystem/file_server/port");
  2099. String passwd = EDITOR_GET("filesystem/file_server/password");
  2100. cmd_args_list.push_back("--remote-fs");
  2101. cmd_args_list.push_back(host + ":" + itos(port));
  2102. if (!passwd.is_empty()) {
  2103. cmd_args_list.push_back("--remote-fs-password");
  2104. cmd_args_list.push_back(passwd);
  2105. }
  2106. }
  2107. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  2108. cmd_args_list.push_back("--remote-debug");
  2109. cmd_args_list.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));
  2110. List<String> breakpoints;
  2111. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  2112. if (breakpoints.size()) {
  2113. cmd_args_list.push_back("--breakpoints");
  2114. String bpoints;
  2115. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  2116. bpoints += E->get().replace(" ", "%20");
  2117. if (E->next()) {
  2118. bpoints += ",";
  2119. }
  2120. }
  2121. cmd_args_list.push_back(bpoints);
  2122. }
  2123. }
  2124. if (p_debug_flags & DEBUG_FLAG_VIEW_COLLISIONS) {
  2125. cmd_args_list.push_back("--debug-collisions");
  2126. }
  2127. if (p_debug_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  2128. cmd_args_list.push_back("--debug-navigation");
  2129. }
  2130. if (dev.simulator) {
  2131. // Deploy and run on simulator.
  2132. if (ep.step("Installing to simulator...", 3)) {
  2133. CLEANUP_AND_RETURN(ERR_SKIP);
  2134. } else {
  2135. List<String> args;
  2136. args.push_back("simctl");
  2137. args.push_back("install");
  2138. args.push_back(dev.id);
  2139. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2140. String log;
  2141. int ec;
  2142. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2143. if (err != OK) {
  2144. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2145. CLEANUP_AND_RETURN(err);
  2146. }
  2147. if (ec != 0) {
  2148. print_line("simctl install:\n" + log);
  2149. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2150. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2151. }
  2152. }
  2153. if (ep.step("Running on simulator...", 4)) {
  2154. CLEANUP_AND_RETURN(ERR_SKIP);
  2155. } else {
  2156. List<String> args;
  2157. args.push_back("simctl");
  2158. args.push_back("launch");
  2159. args.push_back("--terminate-running-process");
  2160. args.push_back(dev.id);
  2161. args.push_back(p_preset->get("application/bundle_identifier"));
  2162. for (const String &E : cmd_args_list) {
  2163. args.push_back(E);
  2164. }
  2165. String log;
  2166. int ec;
  2167. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2168. if (err != OK) {
  2169. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2170. CLEANUP_AND_RETURN(err);
  2171. }
  2172. if (ec != 0) {
  2173. print_line("simctl launch:\n" + log);
  2174. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2175. }
  2176. }
  2177. } else if (dev.use_ios_deploy) {
  2178. // Deploy and run on real device (via ios-deploy).
  2179. if (ep.step("Installing and running on device...", 4)) {
  2180. CLEANUP_AND_RETURN(ERR_SKIP);
  2181. } else {
  2182. List<String> args;
  2183. args.push_back("-u");
  2184. args.push_back("-I");
  2185. args.push_back("--id");
  2186. args.push_back(dev.id);
  2187. args.push_back("--justlaunch");
  2188. args.push_back("--bundle");
  2189. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2190. String app_args;
  2191. for (const String &E : cmd_args_list) {
  2192. app_args += E + " ";
  2193. }
  2194. if (!app_args.is_empty()) {
  2195. args.push_back("--args");
  2196. args.push_back(app_args);
  2197. }
  2198. String idepl = EDITOR_GET("export/ios/ios_deploy");
  2199. if (idepl.is_empty()) {
  2200. idepl = "ios-deploy";
  2201. }
  2202. String log;
  2203. int ec;
  2204. err = OS::get_singleton()->execute(idepl, args, &log, &ec, true);
  2205. if (err != OK) {
  2206. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start ios-deploy executable."));
  2207. CLEANUP_AND_RETURN(err);
  2208. }
  2209. if (ec != 0) {
  2210. print_line("ios-deploy:\n" + log);
  2211. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation/running failed, see editor log for details."));
  2212. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2213. }
  2214. }
  2215. } else {
  2216. // Deploy and run on real device.
  2217. if (ep.step("Installing to device...", 3)) {
  2218. CLEANUP_AND_RETURN(ERR_SKIP);
  2219. } else {
  2220. List<String> args;
  2221. args.push_back("devicectl");
  2222. args.push_back("device");
  2223. args.push_back("install");
  2224. args.push_back("app");
  2225. args.push_back("-d");
  2226. args.push_back(dev.id);
  2227. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2228. String log;
  2229. int ec;
  2230. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2231. if (err != OK) {
  2232. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start device executable."));
  2233. CLEANUP_AND_RETURN(err);
  2234. }
  2235. if (ec != 0) {
  2236. print_line("device install:\n" + log);
  2237. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2238. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2239. }
  2240. }
  2241. if (ep.step("Running on device...", 4)) {
  2242. CLEANUP_AND_RETURN(ERR_SKIP);
  2243. } else {
  2244. List<String> args;
  2245. args.push_back("devicectl");
  2246. args.push_back("device");
  2247. args.push_back("process");
  2248. args.push_back("launch");
  2249. args.push_back("--terminate-existing");
  2250. args.push_back("-d");
  2251. args.push_back(dev.id);
  2252. args.push_back(p_preset->get("application/bundle_identifier"));
  2253. for (const String &E : cmd_args_list) {
  2254. args.push_back(E);
  2255. }
  2256. String log;
  2257. int ec;
  2258. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2259. if (err != OK) {
  2260. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start devicectl executable."));
  2261. CLEANUP_AND_RETURN(err);
  2262. }
  2263. if (ec != 0) {
  2264. print_line("devicectl launch:\n" + log);
  2265. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2266. }
  2267. }
  2268. }
  2269. CLEANUP_AND_RETURN(OK);
  2270. #undef CLEANUP_AND_RETURN
  2271. #else
  2272. return ERR_UNCONFIGURED;
  2273. #endif
  2274. }
  2275. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  2276. if (EditorNode::get_singleton()) {
  2277. #ifdef MODULE_SVG_ENABLED
  2278. Ref<Image> img = memnew(Image);
  2279. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  2280. ImageLoaderSVG::create_image_from_string(img, _ios_logo_svg, EDSCALE, upsample, false);
  2281. logo = ImageTexture::create_from_image(img);
  2282. ImageLoaderSVG::create_image_from_string(img, _ios_run_icon_svg, EDSCALE, upsample, false);
  2283. run_icon = ImageTexture::create_from_image(img);
  2284. #endif
  2285. plugins_changed.set();
  2286. devices_changed.set();
  2287. #ifdef MACOS_ENABLED
  2288. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  2289. #endif
  2290. }
  2291. }
  2292. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  2293. #ifdef MACOS_ENABLED
  2294. quit_request.set();
  2295. if (check_for_changes_thread.is_started()) {
  2296. check_for_changes_thread.wait_to_finish();
  2297. }
  2298. #endif
  2299. }