export_plugin.cpp 102 KB

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