export_plugin.cpp 101 KB

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