export_plugin.cpp 102 KB

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