export_plugin.cpp 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261
  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/export/lipo.h"
  41. #include "editor/export/macho.h"
  42. #include "editor/import/resource_importer_texture_settings.h"
  43. #include "editor/plugins/script_editor_plugin.h"
  44. #include "editor/themes/editor_scale.h"
  45. #include "modules/modules_enabled.gen.h" // For mono and svg.
  46. #ifdef MODULE_SVG_ENABLED
  47. #include "modules/svg/image_loader_svg.h"
  48. #endif
  49. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  50. // Vulkan and OpenGL ES 3.0 both mandate ETC2 support.
  51. r_features->push_back("etc2");
  52. r_features->push_back("astc");
  53. Vector<String> architectures = _get_preset_architectures(p_preset);
  54. for (int i = 0; i < architectures.size(); ++i) {
  55. r_features->push_back(architectures[i]);
  56. }
  57. }
  58. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() const {
  59. Vector<ExportArchitecture> archs;
  60. archs.push_back(ExportArchitecture("arm64", true));
  61. return archs;
  62. }
  63. struct IconInfo {
  64. const char *preset_key;
  65. const char *idiom;
  66. const char *export_name;
  67. const char *actual_size_side;
  68. const char *scale;
  69. const char *unscaled_size;
  70. const bool force_opaque;
  71. };
  72. static const IconInfo icon_infos[] = {
  73. // Home screen on iPhone
  74. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "2x", "60x60", false },
  75. { PNAME("icons/iphone_120x120"), "iphone", "Icon-120.png", "120", "3x", "40x40", false },
  76. { PNAME("icons/iphone_180x180"), "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  77. // Home screen on iPad
  78. { PNAME("icons/ipad_76x76"), "ipad", "Icon-76.png", "76", "1x", "76x76", false },
  79. { PNAME("icons/ipad_152x152"), "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  80. { PNAME("icons/ipad_167x167"), "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  81. // App Store
  82. { PNAME("icons/app_store_1024x1024"), "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", true },
  83. // Spotlight
  84. { PNAME("icons/spotlight_40x40"), "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  85. { PNAME("icons/spotlight_80x80"), "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  86. { PNAME("icons/spotlight_80x80"), "ipad", "Icon-80.png", "80", "2x", "40x40", false },
  87. // Settings
  88. { PNAME("icons/settings_58x58"), "iphone", "Icon-58.png", "58", "2x", "29x29", false },
  89. { PNAME("icons/settings_58x58"), "ipad", "Icon-58.png", "58", "2x", "29x29", false },
  90. { PNAME("icons/settings_87x87"), "iphone", "Icon-87.png", "87", "3x", "29x29", false },
  91. // Notification
  92. { PNAME("icons/notification_40x40"), "iphone", "Icon-40.png", "40", "2x", "20x20", false },
  93. { PNAME("icons/notification_40x40"), "ipad", "Icon-40.png", "40", "2x", "20x20", false },
  94. { PNAME("icons/notification_60x60"), "iphone", "Icon-60.png", "60", "3x", "20x20", false }
  95. };
  96. struct APIAccessInfo {
  97. String prop_name;
  98. String type_name;
  99. Vector<String> prop_flag_value;
  100. Vector<String> prop_flag_name;
  101. int default_value;
  102. };
  103. static const APIAccessInfo api_info[] = {
  104. { "file_timestamp",
  105. "NSPrivacyAccessedAPICategoryFileTimestamp",
  106. { "DDA9.1", "C617.1", "3B52.1" },
  107. { "Display to user on-device:", "Inside app or group container", "Files provided to app by user" },
  108. 3 },
  109. { "system_boot_time",
  110. "NSPrivacyAccessedAPICategorySystemBootTime",
  111. { "35F9.1", "8FFB.1", "3D61.1" },
  112. { "Measure time on-device", "Calculate absolute event timestamps", "User-initiated bug report" },
  113. 1 },
  114. { "disk_space",
  115. "NSPrivacyAccessedAPICategoryDiskSpace",
  116. { "E174.1", "85F4.1", "7D9E.1", "B728.1" },
  117. { "Write or delete file on-device", "Display to user on-device", "User-initiated bug report", "Health research app" },
  118. 3 },
  119. { "active_keyboard",
  120. "NSPrivacyAccessedAPICategoryActiveKeyboards",
  121. { "3EC4.1", "54BD.1" },
  122. { "Custom keyboard app on-device", "Customize UI on-device:2" },
  123. 0 },
  124. { "user_defaults",
  125. "NSPrivacyAccessedAPICategoryUserDefaults",
  126. { "1C8F.1", "AC6B.1", "CA92.1" },
  127. { "Access info from same App Group", "Access managed app configuration", "Access info from same app" },
  128. 0 }
  129. };
  130. struct DataCollectionInfo {
  131. String prop_name;
  132. String type_name;
  133. };
  134. static const DataCollectionInfo data_collect_type_info[] = {
  135. { "name", "NSPrivacyCollectedDataTypeName" },
  136. { "email_address", "NSPrivacyCollectedDataTypeEmailAddress" },
  137. { "phone_number", "NSPrivacyCollectedDataTypePhoneNumber" },
  138. { "physical_address", "NSPrivacyCollectedDataTypePhysicalAddress" },
  139. { "other_contact_info", "NSPrivacyCollectedDataTypeOtherUserContactInfo" },
  140. { "health", "NSPrivacyCollectedDataTypeHealth" },
  141. { "fitness", "NSPrivacyCollectedDataTypeFitness" },
  142. { "payment_info", "NSPrivacyCollectedDataTypePaymentInfo" },
  143. { "credit_info", "NSPrivacyCollectedDataTypeCreditInfo" },
  144. { "other_financial_info", "NSPrivacyCollectedDataTypeOtherFinancialInfo" },
  145. { "precise_location", "NSPrivacyCollectedDataTypePreciseLocation" },
  146. { "coarse_location", "NSPrivacyCollectedDataTypeCoarseLocation" },
  147. { "sensitive_info", "NSPrivacyCollectedDataTypeSensitiveInfo" },
  148. { "contacts", "NSPrivacyCollectedDataTypeContacts" },
  149. { "emails_or_text_messages", "NSPrivacyCollectedDataTypeEmailsOrTextMessages" },
  150. { "photos_or_videos", "NSPrivacyCollectedDataTypePhotosorVideos" },
  151. { "audio_data", "NSPrivacyCollectedDataTypeAudioData" },
  152. { "gameplay_content", "NSPrivacyCollectedDataTypeGameplayContent" },
  153. { "customer_support", "NSPrivacyCollectedDataTypeCustomerSupport" },
  154. { "other_user_content", "NSPrivacyCollectedDataTypeOtherUserContent" },
  155. { "browsing_history", "NSPrivacyCollectedDataTypeBrowsingHistory" },
  156. { "search_hhistory", "NSPrivacyCollectedDataTypeSearchHistory" },
  157. { "user_id", "NSPrivacyCollectedDataTypeUserID" },
  158. { "device_id", "NSPrivacyCollectedDataTypeDeviceID" },
  159. { "purchase_history", "NSPrivacyCollectedDataTypePurchaseHistory" },
  160. { "product_interaction", "NSPrivacyCollectedDataTypeProductInteraction" },
  161. { "advertising_data", "NSPrivacyCollectedDataTypeAdvertisingData" },
  162. { "other_usage_data", "NSPrivacyCollectedDataTypeOtherUsageData" },
  163. { "crash_data", "NSPrivacyCollectedDataTypeCrashData" },
  164. { "performance_data", "NSPrivacyCollectedDataTypePerformanceData" },
  165. { "other_diagnostic_data", "NSPrivacyCollectedDataTypeOtherDiagnosticData" },
  166. { "environment_scanning", "NSPrivacyCollectedDataTypeEnvironmentScanning" },
  167. { "hands", "NSPrivacyCollectedDataTypeHands" },
  168. { "head", "NSPrivacyCollectedDataTypeHead" },
  169. { "other_data_types", "NSPrivacyCollectedDataTypeOtherDataTypes" },
  170. };
  171. static const DataCollectionInfo data_collect_purpose_info[] = {
  172. { "Analytics", "NSPrivacyCollectedDataTypePurposeAnalytics" },
  173. { "App Functionality", "NSPrivacyCollectedDataTypePurposeAppFunctionality" },
  174. { "Developer Advertising", "NSPrivacyCollectedDataTypePurposeDeveloperAdvertising" },
  175. { "Third-party Advertising", "NSPrivacyCollectedDataTypePurposeThirdPartyAdvertising" },
  176. { "Product Personalization", "NSPrivacyCollectedDataTypePurposeProductPersonalization" },
  177. { "Other", "NSPrivacyCollectedDataTypePurposeOther" },
  178. };
  179. String EditorExportPlatformIOS::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
  180. if (p_preset) {
  181. if (p_name == "application/app_store_team_id") {
  182. String team_id = p_preset->get("application/app_store_team_id");
  183. if (team_id.is_empty()) {
  184. return TTR("App Store Team ID not specified.") + "\n";
  185. }
  186. } else if (p_name == "application/bundle_identifier") {
  187. String identifier = p_preset->get("application/bundle_identifier");
  188. String pn_err;
  189. if (!is_package_name_valid(identifier, &pn_err)) {
  190. return TTR("Invalid Identifier:") + " " + pn_err;
  191. }
  192. } else if (p_name == "privacy/file_timestamp_access_reasons") {
  193. int access = p_preset->get("privacy/file_timestamp_access_reasons");
  194. if (access == 0) {
  195. return TTR("At least one file timestamp access reason should be selected.");
  196. }
  197. } else if (p_name == "privacy/disk_space_access_reasons") {
  198. int access = p_preset->get("privacy/disk_space_access_reasons");
  199. if (access == 0) {
  200. return TTR("At least one disk space access reason should be selected.");
  201. }
  202. } else if (p_name == "privacy/system_boot_time_access_reasons") {
  203. int access = p_preset->get("privacy/system_boot_time_access_reasons");
  204. if (access == 0) {
  205. return TTR("At least one system boot time access reason should be selected.");
  206. }
  207. }
  208. }
  209. return String();
  210. }
  211. void EditorExportPlatformIOS::_notification(int p_what) {
  212. #ifdef MACOS_ENABLED
  213. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  214. if (EditorExport::get_singleton()) {
  215. EditorExport::get_singleton()->connect_presets_runnable_updated(callable_mp(this, &EditorExportPlatformIOS::_update_preset_status));
  216. }
  217. }
  218. #endif
  219. }
  220. bool EditorExportPlatformIOS::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
  221. // Hide unsupported .NET embedding option.
  222. if (p_option == "dotnet/embed_build_outputs") {
  223. return false;
  224. }
  225. if (p_preset == nullptr) {
  226. return true;
  227. }
  228. bool advanced_options_enabled = p_preset->are_advanced_options_enabled();
  229. if (p_option.begins_with("privacy") || p_option == "application/generate_simulator_library_if_missing") {
  230. return advanced_options_enabled;
  231. }
  232. return true;
  233. }
  234. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
  235. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  236. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  237. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  238. for (int i = 0; i < architectures.size(); ++i) {
  239. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), architectures[i].name)), architectures[i].is_default));
  240. }
  241. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), "", false, true));
  242. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  243. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), ""));
  244. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  245. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  246. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  247. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  248. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  249. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), "", false, true));
  250. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  251. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  252. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  253. // TODO(sgc): set to iOS 14.0 for Metal
  254. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), "12.0"));
  255. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/additional_plist_content", PROPERTY_HINT_MULTILINE_TEXT), ""));
  256. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));
  257. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/export_project_only"), false));
  258. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/delete_old_export_files_unconditionally"), false));
  259. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/generate_simulator_library_if_missing"), true));
  260. Vector<PluginConfigIOS> found_plugins = get_plugins();
  261. for (int i = 0; i < found_plugins.size(); i++) {
  262. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), found_plugins[i].name)), false));
  263. }
  264. HashSet<String> plist_keys;
  265. for (int i = 0; i < found_plugins.size(); i++) {
  266. // Editable plugin plist values
  267. PluginConfigIOS plugin = found_plugins[i];
  268. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  269. switch (E.value.type) {
  270. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  271. String preset_name = "plugins_plist/" + E.key;
  272. if (!plist_keys.has(preset_name)) {
  273. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, preset_name), E.value.value));
  274. plist_keys.insert(preset_name);
  275. }
  276. } break;
  277. default:
  278. continue;
  279. }
  280. }
  281. }
  282. plugins_changed.clear();
  283. plugins = found_plugins;
  284. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  285. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  286. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_gaming_tier"), false));
  287. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_a12"), false));
  288. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  289. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  290. 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"), ""));
  291. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/camera_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  292. 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"), ""));
  293. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/microphone_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  294. 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"), ""));
  295. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/photolibrary_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  296. for (uint64_t i = 0; i < sizeof(api_info) / sizeof(api_info[0]); ++i) {
  297. String prop_name = vformat("privacy/%s_access_reasons", api_info[i].prop_name);
  298. String hint;
  299. for (int j = 0; j < api_info[i].prop_flag_value.size(); j++) {
  300. if (j != 0) {
  301. hint += ",";
  302. }
  303. hint += vformat("%s - %s:%d", api_info[i].prop_flag_value[j], api_info[i].prop_flag_name[j], (1 << j));
  304. }
  305. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, prop_name, PROPERTY_HINT_FLAGS, hint), api_info[i].default_value));
  306. }
  307. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "privacy/tracking_enabled"), false));
  308. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "privacy/tracking_domains"), Vector<String>()));
  309. {
  310. String hint;
  311. for (uint64_t i = 0; i < sizeof(data_collect_purpose_info) / sizeof(data_collect_purpose_info[0]); ++i) {
  312. if (i != 0) {
  313. hint += ",";
  314. }
  315. hint += vformat("%s:%d", data_collect_purpose_info[i].prop_name, (1 << i));
  316. }
  317. for (uint64_t i = 0; i < sizeof(data_collect_type_info) / sizeof(data_collect_type_info[0]); ++i) {
  318. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("privacy/collected_data/%s/collected", data_collect_type_info[i].prop_name)), false));
  319. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("privacy/collected_data/%s/linked_to_user", data_collect_type_info[i].prop_name)), false));
  320. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("privacy/collected_data/%s/used_for_tracking", data_collect_type_info[i].prop_name)), false));
  321. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, vformat("privacy/collected_data/%s/collection_purposes", data_collect_type_info[i].prop_name), PROPERTY_HINT_FLAGS, hint), 0));
  322. }
  323. }
  324. HashSet<String> used_names;
  325. for (uint64_t i = 0; i < sizeof(icon_infos) / sizeof(icon_infos[0]); ++i) {
  326. if (!used_names.has(icon_infos[i].preset_key)) {
  327. used_names.insert(icon_infos[i].preset_key);
  328. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, icon_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  329. }
  330. }
  331. 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));
  332. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  333. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE, "*.png,*.jpg,*.jpeg"), ""));
  334. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  335. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  336. }
  337. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  338. static const String export_method_string[] = {
  339. "app-store",
  340. "development",
  341. "ad-hoc",
  342. "enterprise"
  343. };
  344. static const String storyboard_image_scale_mode[] = {
  345. "center",
  346. "scaleAspectFit",
  347. "scaleAspectFill",
  348. "scaleToFill"
  349. };
  350. 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");
  351. 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");
  352. 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");
  353. 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");
  354. String str;
  355. String strnew;
  356. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  357. Vector<String> lines = str.split("\n");
  358. for (int i = 0; i < lines.size(); i++) {
  359. if (lines[i].contains("$binary")) {
  360. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  361. } else if (lines[i].contains("$modules_buildfile")) {
  362. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  363. } else if (lines[i].contains("$modules_fileref")) {
  364. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  365. } else if (lines[i].contains("$modules_buildphase")) {
  366. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  367. } else if (lines[i].contains("$modules_buildgrp")) {
  368. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  369. } else if (lines[i].contains("$name")) {
  370. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  371. } else if (lines[i].contains("$bundle_identifier")) {
  372. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  373. } else if (lines[i].contains("$short_version")) {
  374. strnew += lines[i].replace("$short_version", p_preset->get_version("application/short_version")) + "\n";
  375. } else if (lines[i].contains("$version")) {
  376. strnew += lines[i].replace("$version", p_preset->get_version("application/version")) + "\n";
  377. } else if (lines[i].contains("$min_version")) {
  378. strnew += lines[i].replace("$min_version", p_preset->get("application/min_ios_version")) + "\n";
  379. } else if (lines[i].contains("$signature")) {
  380. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  381. } else if (lines[i].contains("$team_id")) {
  382. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  383. } else if (lines[i].contains("$default_build_config")) {
  384. strnew += lines[i].replace("$default_build_config", p_debug ? "Debug" : "Release") + "\n";
  385. } else if (lines[i].contains("$export_method")) {
  386. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  387. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  388. } else if (lines[i].contains("$provisioning_profile_uuid_release")) {
  389. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get_or_env("application/provisioning_profile_uuid_release", ENV_IOS_PROFILE_UUID_RELEASE)) + "\n";
  390. } else if (lines[i].contains("$provisioning_profile_uuid_debug")) {
  391. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get_or_env("application/provisioning_profile_uuid_debug", ENV_IOS_PROFILE_UUID_DEBUG)) + "\n";
  392. } else if (lines[i].contains("$code_sign_style_debug")) {
  393. if (dbg_manual) {
  394. strnew += lines[i].replace("$code_sign_style_debug", "Manual") + "\n";
  395. } else {
  396. strnew += lines[i].replace("$code_sign_style_debug", "Automatic") + "\n";
  397. }
  398. } else if (lines[i].contains("$code_sign_style_release")) {
  399. if (rel_manual) {
  400. strnew += lines[i].replace("$code_sign_style_release", "Manual") + "\n";
  401. } else {
  402. strnew += lines[i].replace("$code_sign_style_release", "Automatic") + "\n";
  403. }
  404. } else if (lines[i].contains("$provisioning_profile_uuid")) {
  405. 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);
  406. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  407. } else if (lines[i].contains("$code_sign_identity_debug")) {
  408. strnew += lines[i].replace("$code_sign_identity_debug", dbg_sign_id) + "\n";
  409. } else if (lines[i].contains("$code_sign_identity_release")) {
  410. strnew += lines[i].replace("$code_sign_identity_release", rel_sign_id) + "\n";
  411. } else if (lines[i].contains("$additional_plist_content")) {
  412. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  413. } else if (lines[i].contains("$godot_archs")) {
  414. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  415. } else if (lines[i].contains("$linker_flags")) {
  416. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  417. } else if (lines[i].contains("$targeted_device_family")) {
  418. String xcode_value;
  419. switch ((int)p_preset->get("application/targeted_device_family")) {
  420. case 0: // iPhone
  421. xcode_value = "1";
  422. break;
  423. case 1: // iPad
  424. xcode_value = "2";
  425. break;
  426. case 2: // iPhone & iPad
  427. xcode_value = "1,2";
  428. break;
  429. }
  430. strnew += lines[i].replace("$targeted_device_family", xcode_value) + "\n";
  431. } else if (lines[i].contains("$cpp_code")) {
  432. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  433. } else if (lines[i].contains("$docs_in_place")) {
  434. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  435. } else if (lines[i].contains("$docs_sharing")) {
  436. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  437. } else if (lines[i].contains("$entitlements_push_notifications")) {
  438. bool is_on = p_preset->get("capabilities/push_notifications");
  439. strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n";
  440. } else if (lines[i].contains("$required_device_capabilities")) {
  441. String capabilities;
  442. // I've removed armv7 as we can run on 64bit only devices
  443. // Note that capabilities listed here are requirements for the app to be installed.
  444. // They don't enable anything.
  445. Vector<String> capabilities_list = p_config.capabilities;
  446. if ((bool)p_preset->get("capabilities/access_wifi") && !capabilities_list.has("wifi")) {
  447. capabilities_list.push_back("wifi");
  448. }
  449. if ((bool)p_preset->get("capabilities/performance_gaming_tier") && !capabilities_list.has("iphone-performance-gaming-tier")) {
  450. capabilities_list.push_back("iphone-performance-gaming-tier");
  451. }
  452. if ((bool)p_preset->get("capabilities/performance_a12") && !capabilities_list.has("iphone-ipad-minimum-performance-a12")) {
  453. capabilities_list.push_back("iphone-ipad-minimum-performance-a12");
  454. }
  455. for (int idx = 0; idx < capabilities_list.size(); idx++) {
  456. capabilities += "<string>" + capabilities_list[idx] + "</string>\n";
  457. }
  458. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  459. } else if (lines[i].contains("$interface_orientations")) {
  460. String orientations;
  461. const DisplayServer::ScreenOrientation screen_orientation =
  462. DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")));
  463. switch (screen_orientation) {
  464. case DisplayServer::SCREEN_LANDSCAPE:
  465. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  466. break;
  467. case DisplayServer::SCREEN_PORTRAIT:
  468. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  469. break;
  470. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  471. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  472. break;
  473. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  474. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  475. break;
  476. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  477. // Allow both landscape orientations depending on sensor direction.
  478. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  479. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  480. break;
  481. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  482. // Allow both portrait orientations depending on sensor direction.
  483. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  484. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  485. break;
  486. case DisplayServer::SCREEN_SENSOR:
  487. // Allow all screen orientations depending on sensor direction.
  488. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  489. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  490. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  491. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  492. break;
  493. }
  494. strnew += lines[i].replace("$interface_orientations", orientations);
  495. } else if (lines[i].contains("$ipad_interface_orientations")) {
  496. String orientations;
  497. const DisplayServer::ScreenOrientation screen_orientation =
  498. DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")));
  499. switch (screen_orientation) {
  500. case DisplayServer::SCREEN_LANDSCAPE:
  501. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  502. break;
  503. case DisplayServer::SCREEN_PORTRAIT:
  504. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  505. break;
  506. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  507. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  508. break;
  509. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  510. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  511. break;
  512. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  513. // Allow both landscape orientations depending on sensor direction.
  514. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  515. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  516. break;
  517. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  518. // Allow both portrait orientations depending on sensor direction.
  519. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  520. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  521. break;
  522. case DisplayServer::SCREEN_SENSOR:
  523. // Allow all screen orientations depending on sensor direction.
  524. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  525. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  526. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  527. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  528. break;
  529. }
  530. strnew += lines[i].replace("$ipad_interface_orientations", orientations);
  531. } else if (lines[i].contains("$camera_usage_description")) {
  532. String description = p_preset->get("privacy/camera_usage_description");
  533. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  534. } else if (lines[i].contains("$microphone_usage_description")) {
  535. String description = p_preset->get("privacy/microphone_usage_description");
  536. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  537. } else if (lines[i].contains("$photolibrary_usage_description")) {
  538. String description = p_preset->get("privacy/photolibrary_usage_description");
  539. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  540. } else if (lines[i].contains("$plist_launch_screen_name")) {
  541. String value = "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>";
  542. strnew += lines[i].replace("$plist_launch_screen_name", value) + "\n";
  543. } else if (lines[i].contains("$pbx_launch_screen_file_reference")) {
  544. String value = "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };";
  545. strnew += lines[i].replace("$pbx_launch_screen_file_reference", value) + "\n";
  546. } else if (lines[i].contains("$pbx_launch_screen_copy_files")) {
  547. String value = "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */,";
  548. strnew += lines[i].replace("$pbx_launch_screen_copy_files", value) + "\n";
  549. } else if (lines[i].contains("$pbx_launch_screen_build_phase")) {
  550. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */,";
  551. strnew += lines[i].replace("$pbx_launch_screen_build_phase", value) + "\n";
  552. } else if (lines[i].contains("$pbx_launch_screen_build_reference")) {
  553. String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };";
  554. strnew += lines[i].replace("$pbx_launch_screen_build_reference", value) + "\n";
  555. #ifndef DISABLE_DEPRECATED
  556. } else if (lines[i].contains("$pbx_launch_image_usage_setting")) {
  557. strnew += lines[i].replace("$pbx_launch_image_usage_setting", "") + "\n";
  558. #endif
  559. } else if (lines[i].contains("$launch_screen_image_mode")) {
  560. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  561. String value;
  562. switch (image_scale_mode) {
  563. case 0: {
  564. String logo_path = GLOBAL_GET("application/boot_splash/image");
  565. bool is_on = GLOBAL_GET("application/boot_splash/fullsize");
  566. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  567. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  568. } break;
  569. default: {
  570. value = storyboard_image_scale_mode[image_scale_mode - 1];
  571. }
  572. }
  573. strnew += lines[i].replace("$launch_screen_image_mode", value) + "\n";
  574. } else if (lines[i].contains("$launch_screen_background_color")) {
  575. bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
  576. Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : GLOBAL_GET("application/boot_splash/bg_color");
  577. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  578. Dictionary value_dictionary;
  579. value_dictionary["red"] = color.r;
  580. value_dictionary["green"] = color.g;
  581. value_dictionary["blue"] = color.b;
  582. value_dictionary["alpha"] = color.a;
  583. String value = value_format.format(value_dictionary, "$_");
  584. strnew += lines[i].replace("$launch_screen_background_color", value) + "\n";
  585. } else if (lines[i].contains("$pbx_locale_file_reference")) {
  586. String locale_files;
  587. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  588. if (translations.size() > 0) {
  589. HashSet<String> languages;
  590. for (const String &E : translations) {
  591. Ref<Translation> tr = ResourceLoader::load(E);
  592. if (tr.is_valid() && tr->get_locale() != "en") {
  593. languages.insert(tr->get_locale());
  594. }
  595. }
  596. int index = 0;
  597. for (const String &lang : languages) {
  598. 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";
  599. index++;
  600. }
  601. }
  602. strnew += lines[i].replace("$pbx_locale_file_reference", locale_files);
  603. } else if (lines[i].contains("$pbx_locale_build_reference")) {
  604. String locale_files;
  605. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  606. if (translations.size() > 0) {
  607. HashSet<String> languages;
  608. for (const String &E : translations) {
  609. Ref<Translation> tr = ResourceLoader::load(E);
  610. if (tr.is_valid() && tr->get_locale() != "en") {
  611. languages.insert(tr->get_locale());
  612. }
  613. }
  614. int index = 0;
  615. for (const String &lang : languages) {
  616. locale_files += "D0BCFE4518AEBDA2004A" + itos(index).pad_zeros(4) + " /* " + lang + " */,\n";
  617. index++;
  618. }
  619. }
  620. strnew += lines[i].replace("$pbx_locale_build_reference", locale_files);
  621. } else if (lines[i].contains("$swift_runtime_migration")) {
  622. String value = !p_config.use_swift_runtime ? "" : "LastSwiftMigration = 1250;";
  623. strnew += lines[i].replace("$swift_runtime_migration", value) + "\n";
  624. } else if (lines[i].contains("$swift_runtime_build_settings")) {
  625. String value = !p_config.use_swift_runtime ? "" : R"(
  626. CLANG_ENABLE_MODULES = YES;
  627. SWIFT_OBJC_BRIDGING_HEADER = "$binary/dummy.h";
  628. SWIFT_VERSION = 5.0;
  629. )";
  630. value = value.replace("$binary", p_config.binary_name);
  631. strnew += lines[i].replace("$swift_runtime_build_settings", value) + "\n";
  632. } else if (lines[i].contains("$swift_runtime_fileref")) {
  633. String value = !p_config.use_swift_runtime ? "" : R"(
  634. 90B4C2AA2680BC560039117A /* dummy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dummy.h"; sourceTree = "<group>"; };
  635. 90B4C2B52680C7E90039117A /* dummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "dummy.swift"; sourceTree = "<group>"; };
  636. )";
  637. strnew += lines[i].replace("$swift_runtime_fileref", value) + "\n";
  638. } else if (lines[i].contains("$swift_runtime_binary_files")) {
  639. String value = !p_config.use_swift_runtime ? "" : R"(
  640. 90B4C2AA2680BC560039117A /* dummy.h */,
  641. 90B4C2B52680C7E90039117A /* dummy.swift */,
  642. )";
  643. strnew += lines[i].replace("$swift_runtime_binary_files", value) + "\n";
  644. } else if (lines[i].contains("$swift_runtime_buildfile")) {
  645. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90B4C2B52680C7E90039117A /* dummy.swift */; };";
  646. strnew += lines[i].replace("$swift_runtime_buildfile", value) + "\n";
  647. } else if (lines[i].contains("$swift_runtime_build_phase")) {
  648. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift */,";
  649. strnew += lines[i].replace("$swift_runtime_build_phase", value) + "\n";
  650. } else if (lines[i].contains("$priv_collection")) {
  651. bool section_opened = false;
  652. for (uint64_t j = 0; j < sizeof(data_collect_type_info) / sizeof(data_collect_type_info[0]); ++j) {
  653. bool data_collected = p_preset->get(vformat("privacy/collected_data/%s/collected", data_collect_type_info[j].prop_name));
  654. bool linked = p_preset->get(vformat("privacy/collected_data/%s/linked_to_user", data_collect_type_info[j].prop_name));
  655. bool tracking = p_preset->get(vformat("privacy/collected_data/%s/used_for_tracking", data_collect_type_info[j].prop_name));
  656. int purposes = p_preset->get(vformat("privacy/collected_data/%s/collection_purposes", data_collect_type_info[j].prop_name));
  657. if (data_collected) {
  658. if (!section_opened) {
  659. section_opened = true;
  660. strnew += "\t<key>NSPrivacyCollectedDataTypes</key>\n";
  661. strnew += "\t<array>\n";
  662. }
  663. strnew += "\t\t<dict>\n";
  664. strnew += "\t\t\t<key>NSPrivacyCollectedDataType</key>\n";
  665. strnew += vformat("\t\t\t<string>%s</string>\n", data_collect_type_info[j].type_name);
  666. strnew += "\t\t\t\t<key>NSPrivacyCollectedDataTypeLinked</key>\n";
  667. if (linked) {
  668. strnew += "\t\t\t\t<true/>\n";
  669. } else {
  670. strnew += "\t\t\t\t<false/>\n";
  671. }
  672. strnew += "\t\t\t\t<key>NSPrivacyCollectedDataTypeTracking</key>\n";
  673. if (tracking) {
  674. strnew += "\t\t\t\t<true/>\n";
  675. } else {
  676. strnew += "\t\t\t\t<false/>\n";
  677. }
  678. if (purposes != 0) {
  679. strnew += "\t\t\t\t<key>NSPrivacyCollectedDataTypePurposes</key>\n";
  680. strnew += "\t\t\t\t<array>\n";
  681. for (uint64_t k = 0; k < sizeof(data_collect_purpose_info) / sizeof(data_collect_purpose_info[0]); ++k) {
  682. if (purposes & (1 << k)) {
  683. strnew += vformat("\t\t\t\t\t<string>%s</string>\n", data_collect_purpose_info[k].type_name);
  684. }
  685. }
  686. strnew += "\t\t\t\t</array>\n";
  687. }
  688. strnew += "\t\t\t</dict>\n";
  689. }
  690. }
  691. if (section_opened) {
  692. strnew += "\t</array>\n";
  693. }
  694. } else if (lines[i].contains("$priv_tracking")) {
  695. bool tracking = p_preset->get("privacy/tracking_enabled");
  696. strnew += "\t<key>NSPrivacyTracking</key>\n";
  697. if (tracking) {
  698. strnew += "\t<true/>\n";
  699. } else {
  700. strnew += "\t<false/>\n";
  701. }
  702. Vector<String> tracking_domains = p_preset->get("privacy/tracking_domains");
  703. if (!tracking_domains.is_empty()) {
  704. strnew += "\t<key>NSPrivacyTrackingDomains</key>\n";
  705. strnew += "\t<array>\n";
  706. for (const String &E : tracking_domains) {
  707. strnew += "\t\t<string>" + E + "</string>\n";
  708. }
  709. strnew += "\t</array>\n";
  710. }
  711. } else if (lines[i].contains("$priv_api_types")) {
  712. strnew += "\t<array>\n";
  713. for (uint64_t j = 0; j < sizeof(api_info) / sizeof(api_info[0]); ++j) {
  714. int api_access = p_preset->get(vformat("privacy/%s_access_reasons", api_info[j].prop_name));
  715. if (api_access != 0) {
  716. strnew += "\t\t<dict>\n";
  717. strnew += "\t\t\t<key>NSPrivacyAccessedAPITypeReasons</key>\n";
  718. strnew += "\t\t\t<array>\n";
  719. for (int k = 0; k < api_info[j].prop_flag_value.size(); k++) {
  720. if (api_access & (1 << k)) {
  721. strnew += vformat("\t\t\t\t<string>%s</string>\n", api_info[j].prop_flag_value[k]);
  722. }
  723. }
  724. strnew += "\t\t\t</array>\n";
  725. strnew += "\t\t\t<key>NSPrivacyAccessedAPIType</key>\n";
  726. strnew += vformat("\t\t\t<string>%s</string>\n", api_info[j].type_name);
  727. strnew += "\t\t</dict>\n";
  728. }
  729. }
  730. strnew += "\t</array>\n";
  731. } else {
  732. strnew += lines[i] + "\n";
  733. }
  734. }
  735. // !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...
  736. // should apply the same fix in our macOS export.
  737. CharString cs = strnew.utf8();
  738. pfile.resize(cs.size() - 1);
  739. for (int i = 0; i < cs.size() - 1; i++) {
  740. pfile.write[i] = cs[i];
  741. }
  742. }
  743. String EditorExportPlatformIOS::_get_additional_plist_content() {
  744. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  745. String result;
  746. for (int i = 0; i < export_plugins.size(); ++i) {
  747. result += export_plugins[i]->get_ios_plist_content();
  748. }
  749. return result;
  750. }
  751. String EditorExportPlatformIOS::_get_linker_flags() {
  752. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  753. String result;
  754. for (int i = 0; i < export_plugins.size(); ++i) {
  755. String flags = export_plugins[i]->get_ios_linker_flags();
  756. if (flags.length() == 0) {
  757. continue;
  758. }
  759. if (result.length() > 0) {
  760. result += ' ';
  761. }
  762. result += flags;
  763. }
  764. // the flags will be enclosed in quotes, so need to escape them
  765. return result.replace("\"", "\\\"");
  766. }
  767. String EditorExportPlatformIOS::_get_cpp_code() {
  768. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  769. String result;
  770. for (int i = 0; i < export_plugins.size(); ++i) {
  771. result += export_plugins[i]->get_ios_cpp_code();
  772. }
  773. return result;
  774. }
  775. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  776. ERR_FAIL_COND(p_dst.is_null());
  777. ERR_FAIL_COND(p_src.is_null());
  778. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  779. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  780. int x_pos = (p_dst->get_width() - sw) / 2;
  781. int y_pos = (p_dst->get_height() - sh) / 2;
  782. int xs = (x_pos >= 0) ? 0 : -x_pos;
  783. int ys = (y_pos >= 0) ? 0 : -y_pos;
  784. if (sw + x_pos > p_dst->get_width()) {
  785. sw = p_dst->get_width() - x_pos;
  786. }
  787. if (sh + y_pos > p_dst->get_height()) {
  788. sh = p_dst->get_height() - y_pos;
  789. }
  790. for (int y = ys; y < sh; y++) {
  791. for (int x = xs; x < sw; x++) {
  792. Color sc = p_rot ? p_src->get_pixel(p_src->get_width() - y - 1, x) : p_src->get_pixel(x, y);
  793. Color dc = p_dst->get_pixel(x_pos + x, y_pos + y);
  794. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  795. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  796. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  797. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  798. p_dst->set_pixel(x_pos + x, y_pos + y, dc);
  799. }
  800. }
  801. }
  802. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  803. String json_description = "{\"images\":[";
  804. String sizes;
  805. Ref<DirAccess> da = DirAccess::open(p_iconset_dir);
  806. if (da.is_null()) {
  807. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));
  808. return ERR_CANT_OPEN;
  809. }
  810. Color boot_bg_color = GLOBAL_GET("application/boot_splash/bg_color");
  811. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  812. IconInfo info = icon_infos[i];
  813. int side_size = String(info.actual_size_side).to_int();
  814. String icon_path = p_preset->get(info.preset_key);
  815. if (icon_path.length() == 0) {
  816. // Resize main app icon
  817. icon_path = GLOBAL_GET("application/config/icon");
  818. Ref<Image> img = memnew(Image);
  819. Error err = ImageLoader::load_image(icon_path, img);
  820. if (err != OK) {
  821. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  822. return ERR_UNCONFIGURED;
  823. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  824. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  825. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  826. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  827. new_img->fill(boot_bg_color);
  828. _blend_and_rotate(new_img, img, false);
  829. err = new_img->save_png(p_iconset_dir + info.export_name);
  830. } else {
  831. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  832. err = img->save_png(p_iconset_dir + info.export_name);
  833. }
  834. if (err) {
  835. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  836. return err;
  837. }
  838. } else {
  839. // Load custom icon and resize if required
  840. Ref<Image> img = memnew(Image);
  841. Error err = ImageLoader::load_image(icon_path, img);
  842. if (err != OK) {
  843. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  844. return ERR_UNCONFIGURED;
  845. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  846. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  847. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  848. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  849. new_img->fill(boot_bg_color);
  850. _blend_and_rotate(new_img, img, false);
  851. err = new_img->save_png(p_iconset_dir + info.export_name);
  852. } else if (img->get_width() != side_size || img->get_height() != side_size) {
  853. 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)));
  854. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  855. err = img->save_png(p_iconset_dir + info.export_name);
  856. } else {
  857. err = da->copy(icon_path, p_iconset_dir + info.export_name);
  858. }
  859. if (err) {
  860. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  861. return err;
  862. }
  863. }
  864. sizes += String(info.actual_size_side) + "\n";
  865. if (i > 0) {
  866. json_description += ",";
  867. }
  868. json_description += String("{");
  869. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  870. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  871. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  872. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  873. json_description += String("}");
  874. }
  875. json_description += "]}";
  876. Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  877. if (json_file.is_null()) {
  878. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));
  879. return ERR_CANT_CREATE;
  880. }
  881. CharString json_utf8 = json_description.utf8();
  882. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  883. Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  884. if (sizes_file.is_null()) {
  885. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));
  886. return ERR_CANT_CREATE;
  887. }
  888. CharString sizes_utf8 = sizes.utf8();
  889. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  890. return OK;
  891. }
  892. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  893. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  894. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  895. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  896. Ref<Image> image;
  897. String image_path = p_dest_dir.path_join("[email protected]");
  898. image.instantiate();
  899. Error err = ImageLoader::load_image(custom_launch_image_2x, image);
  900. if (err) {
  901. image.unref();
  902. return err;
  903. }
  904. if (image->save_png(image_path) != OK) {
  905. return ERR_FILE_CANT_WRITE;
  906. }
  907. image.unref();
  908. image_path = p_dest_dir.path_join("[email protected]");
  909. image.instantiate();
  910. err = ImageLoader::load_image(custom_launch_image_3x, image);
  911. if (err) {
  912. image.unref();
  913. return err;
  914. }
  915. if (image->save_png(image_path) != OK) {
  916. return ERR_FILE_CANT_WRITE;
  917. }
  918. } else {
  919. Ref<Image> splash;
  920. const String splash_path = GLOBAL_GET("application/boot_splash/image");
  921. if (!splash_path.is_empty()) {
  922. splash.instantiate();
  923. const Error err = ImageLoader::load_image(splash_path, splash);
  924. if (err) {
  925. splash.unref();
  926. }
  927. }
  928. if (splash.is_null()) {
  929. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  930. }
  931. // Using same image for both @2x and @3x
  932. // because Godot's own boot logo uses single image for all resolutions.
  933. // Also not using @1x image, because devices using this image variant
  934. // are not supported by iOS 9, which is minimal target.
  935. const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
  936. const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");
  937. if (splash->save_png(splash_png_path_2x) != OK) {
  938. return ERR_FILE_CANT_WRITE;
  939. }
  940. if (splash->save_png(splash_png_path_3x) != OK) {
  941. return ERR_FILE_CANT_WRITE;
  942. }
  943. }
  944. return OK;
  945. }
  946. Error EditorExportPlatformIOS::_walk_dir_recursive(Ref<DirAccess> &p_da, FileHandler p_handler, void *p_userdata) {
  947. Vector<String> dirs;
  948. String current_dir = p_da->get_current_dir();
  949. p_da->list_dir_begin();
  950. String path = p_da->get_next();
  951. while (!path.is_empty()) {
  952. if (p_da->current_is_dir()) {
  953. if (path != "." && path != "..") {
  954. dirs.push_back(path);
  955. }
  956. } else {
  957. Error err = p_handler(current_dir.path_join(path), p_userdata);
  958. if (err) {
  959. p_da->list_dir_end();
  960. return err;
  961. }
  962. }
  963. path = p_da->get_next();
  964. }
  965. p_da->list_dir_end();
  966. for (int i = 0; i < dirs.size(); ++i) {
  967. p_da->change_dir(dirs[i]);
  968. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  969. p_da->change_dir("..");
  970. if (err) {
  971. return err;
  972. }
  973. }
  974. return OK;
  975. }
  976. struct CodesignData {
  977. const Ref<EditorExportPreset> &preset;
  978. bool debug = false;
  979. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  980. preset(p_preset),
  981. debug(p_debug) {
  982. }
  983. };
  984. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  985. if (p_file.ends_with(".dylib")) {
  986. CodesignData *data = static_cast<CodesignData *>(p_userdata);
  987. print_line(String("Signing ") + p_file);
  988. String sign_id;
  989. if (data->debug) {
  990. sign_id = data->preset->get("application/code_sign_identity_debug").operator String().is_empty() ? "iPhone Developer" : data->preset->get("application/code_sign_identity_debug");
  991. } else {
  992. sign_id = data->preset->get("application/code_sign_identity_release").operator String().is_empty() ? "iPhone Distribution" : data->preset->get("application/code_sign_identity_release");
  993. }
  994. List<String> codesign_args;
  995. codesign_args.push_back("-f");
  996. codesign_args.push_back("-s");
  997. codesign_args.push_back(sign_id);
  998. codesign_args.push_back(p_file);
  999. String str;
  1000. Error err = OS::get_singleton()->execute("codesign", codesign_args, &str, nullptr, true);
  1001. print_verbose("codesign (" + p_file + "):\n" + str);
  1002. return err;
  1003. }
  1004. return OK;
  1005. }
  1006. struct PbxId {
  1007. private:
  1008. static char _hex_char(uint8_t four_bits) {
  1009. if (four_bits < 10) {
  1010. return ('0' + four_bits);
  1011. }
  1012. return 'A' + (four_bits - 10);
  1013. }
  1014. static String _hex_pad(uint32_t num) {
  1015. Vector<char> ret;
  1016. ret.resize(sizeof(num) * 2);
  1017. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  1018. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  1019. ret.write[i] = _hex_char(four_bits);
  1020. }
  1021. return String::utf8(ret.ptr(), ret.size());
  1022. }
  1023. public:
  1024. uint32_t high_bits;
  1025. uint32_t mid_bits;
  1026. uint32_t low_bits;
  1027. String str() const {
  1028. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  1029. }
  1030. PbxId &operator++() {
  1031. low_bits++;
  1032. if (!low_bits) {
  1033. mid_bits++;
  1034. if (!mid_bits) {
  1035. high_bits++;
  1036. }
  1037. }
  1038. return *this;
  1039. }
  1040. };
  1041. struct ExportLibsData {
  1042. Vector<String> lib_paths;
  1043. String dest_dir;
  1044. };
  1045. bool EditorExportPlatformIOS::_archive_has_arm64(const String &p_path, uint32_t *r_cputype, uint32_t *r_cpusubtype) const {
  1046. bool has_arm64_image = false;
  1047. if (FileAccess::exists(p_path)) {
  1048. if (LipO::is_lipo(p_path)) {
  1049. // LipO.
  1050. Ref<LipO> lipo;
  1051. lipo.instantiate();
  1052. if (lipo->open_file(p_path)) {
  1053. for (int i = 0; i < lipo->get_arch_count(); i++) {
  1054. if (lipo->get_arch_cputype(i) == 0x100000c && lipo->get_arch_cpusubtype(i) == 0) {
  1055. has_arm64_image = true;
  1056. break;
  1057. }
  1058. }
  1059. }
  1060. lipo->close();
  1061. } else {
  1062. // Single architecture archive.
  1063. Ref<FileAccess> sim_f = FileAccess::open(p_path, FileAccess::READ);
  1064. if (sim_f.is_valid()) {
  1065. char magic[9] = {};
  1066. sim_f->get_buffer((uint8_t *)&magic[0], 8);
  1067. if (String(magic) == String("!<arch>\n")) {
  1068. while (!sim_f->eof_reached()) {
  1069. // Read file metadata.
  1070. char name_short[17] = {};
  1071. char size_short[11] = {};
  1072. sim_f->get_buffer((uint8_t *)&name_short[0], 16);
  1073. sim_f->seek(sim_f->get_position() + 12 + 6 + 6 + 8); // Skip modification time, owner ID, group ID, file mode.
  1074. sim_f->get_buffer((uint8_t *)&size_short[0], 10);
  1075. sim_f->seek(sim_f->get_position() + 2); // Skip end marker.
  1076. int64_t file_size = String(size_short).to_int();
  1077. int64_t next_off = sim_f->get_position() + file_size;
  1078. String name = String(name_short); // Skip extended name.
  1079. if (name.is_empty() || file_size == 0) {
  1080. break;
  1081. }
  1082. if (name.begins_with("#1/")) {
  1083. int64_t name_len = String(name_short).replace("#1/", "").to_int();
  1084. sim_f->seek(sim_f->get_position() + name_len);
  1085. }
  1086. // Read file content.
  1087. uint32_t obj_magic = sim_f->get_32();
  1088. bool swap = (obj_magic == 0xcffaedfe || obj_magic == 0xcefaedfe);
  1089. if (obj_magic == 0xcefaedfe || obj_magic == 0xfeedface || obj_magic == 0xcffaedfe || obj_magic == 0xfeedfacf) {
  1090. uint32_t cputype = sim_f->get_32();
  1091. uint32_t cpusubtype = sim_f->get_32();
  1092. if (swap) {
  1093. cputype = BSWAP32(cputype);
  1094. cpusubtype = BSWAP32(cpusubtype);
  1095. }
  1096. if (r_cputype) {
  1097. *r_cputype = cputype;
  1098. }
  1099. if (r_cpusubtype) {
  1100. *r_cpusubtype = cpusubtype;
  1101. }
  1102. if (cputype == 0x100000c && cpusubtype == 0) {
  1103. has_arm64_image = true;
  1104. }
  1105. break;
  1106. }
  1107. sim_f->seek(next_off);
  1108. }
  1109. }
  1110. sim_f->close();
  1111. }
  1112. }
  1113. }
  1114. return has_arm64_image;
  1115. }
  1116. int EditorExportPlatformIOS::_archive_convert_to_simulator(const String &p_path) const {
  1117. int commands_patched = 0;
  1118. Ref<FileAccess> sim_f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  1119. if (sim_f.is_valid()) {
  1120. char magic[9] = {};
  1121. sim_f->get_buffer((uint8_t *)&magic[0], 8);
  1122. if (String(magic) == String("!<arch>\n")) {
  1123. while (!sim_f->eof_reached()) {
  1124. // Read file metadata.
  1125. char name_short[17] = {};
  1126. char size_short[11] = {};
  1127. sim_f->get_buffer((uint8_t *)&name_short[0], 16);
  1128. sim_f->seek(sim_f->get_position() + 12 + 6 + 6 + 8); // Skip modification time, owner ID, group ID, file mode.
  1129. sim_f->get_buffer((uint8_t *)&size_short[0], 10);
  1130. sim_f->seek(sim_f->get_position() + 2); // Skip end marker.
  1131. int64_t file_size = String(size_short).to_int();
  1132. int64_t next_off = sim_f->get_position() + file_size;
  1133. String name = String(name_short); // Skip extended name.
  1134. if (name.is_empty() || file_size == 0) {
  1135. break;
  1136. }
  1137. if (name.begins_with("#1/")) {
  1138. int64_t name_len = String(name_short).replace("#1/", "").to_int();
  1139. sim_f->seek(sim_f->get_position() + name_len);
  1140. }
  1141. // Read file content.
  1142. uint32_t obj_magic = sim_f->get_32();
  1143. bool swap = (obj_magic == 0xcffaedfe || obj_magic == 0xcefaedfe);
  1144. if (obj_magic == 0xcefaedfe || obj_magic == 0xfeedface || obj_magic == 0xcffaedfe || obj_magic == 0xfeedfacf) {
  1145. uint32_t cputype = sim_f->get_32();
  1146. uint32_t cpusubtype = sim_f->get_32();
  1147. uint32_t filetype = sim_f->get_32();
  1148. uint32_t ncmds = sim_f->get_32();
  1149. sim_f->get_32(); // Commands total size.
  1150. sim_f->get_32(); // Commands flags.
  1151. if (obj_magic == 0xcffaedfe || obj_magic == 0xfeedfacf) {
  1152. sim_f->get_32(); // Reserved, 64-bit only.
  1153. }
  1154. if (swap) {
  1155. ncmds = BSWAP32(ncmds);
  1156. cputype = BSWAP32(cputype);
  1157. cpusubtype = BSWAP32(cpusubtype);
  1158. filetype = BSWAP32(filetype);
  1159. }
  1160. if (cputype == 0x100000C && cpusubtype == 0 && filetype == 1) {
  1161. // ARM64, object file.
  1162. for (uint32_t i = 0; i < ncmds; i++) {
  1163. int64_t cmdofs = sim_f->get_position();
  1164. uint32_t cmdid = sim_f->get_32();
  1165. uint32_t cmdsize = sim_f->get_32();
  1166. if (swap) {
  1167. cmdid = BSWAP32(cmdid);
  1168. cmdsize = BSWAP32(cmdsize);
  1169. }
  1170. if (cmdid == MachO::LoadCommandID::LC_BUILD_VERSION) {
  1171. int64_t platform = sim_f->get_32();
  1172. if (swap) {
  1173. platform = BSWAP32(platform);
  1174. }
  1175. if (platform == MachO::PlatformID::PLATFORM_IOS) {
  1176. sim_f->seek(cmdofs + 4 + 4);
  1177. uint32_t new_id = MachO::PlatformID::PLATFORM_IOSSIMULATOR;
  1178. if (swap) {
  1179. new_id = BSWAP32(new_id);
  1180. }
  1181. sim_f->store_32(new_id);
  1182. commands_patched++;
  1183. }
  1184. }
  1185. sim_f->seek(cmdofs + cmdsize);
  1186. }
  1187. }
  1188. }
  1189. sim_f->seek(next_off);
  1190. }
  1191. }
  1192. sim_f->close();
  1193. }
  1194. return commands_patched;
  1195. }
  1196. void EditorExportPlatformIOS::_check_xcframework_content(const String &p_path, int &r_total_libs, int &r_static_libs, int &r_dylibs, int &r_frameworks) const {
  1197. Ref<PList> plist;
  1198. plist.instantiate();
  1199. plist->load_file(p_path.path_join("Info.plist"));
  1200. Ref<PListNode> root_node = plist->get_root();
  1201. if (root_node.is_null()) {
  1202. return;
  1203. }
  1204. Dictionary root = root_node->get_value();
  1205. if (!root.has("AvailableLibraries")) {
  1206. return;
  1207. }
  1208. Ref<PListNode> libs_node = root["AvailableLibraries"];
  1209. if (libs_node.is_null()) {
  1210. return;
  1211. }
  1212. Array libs = libs_node->get_value();
  1213. r_total_libs = libs.size();
  1214. for (int j = 0; j < libs.size(); j++) {
  1215. Ref<PListNode> lib_node = libs[j];
  1216. if (lib_node.is_null()) {
  1217. return;
  1218. }
  1219. Dictionary lib = lib_node->get_value();
  1220. if (lib.has("BinaryPath")) {
  1221. Ref<PListNode> path_node = lib["BinaryPath"];
  1222. if (path_node.is_valid()) {
  1223. String path = path_node->get_value();
  1224. if (path.ends_with(".a")) {
  1225. r_static_libs++;
  1226. }
  1227. if (path.ends_with(".dylib")) {
  1228. r_dylibs++;
  1229. }
  1230. if (path.ends_with(".framework")) {
  1231. r_frameworks++;
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }
  1237. Error EditorExportPlatformIOS::_convert_to_framework(const String &p_source, const String &p_destination, const String &p_id) const {
  1238. print_line("Converting to .framework", p_source, " -> ", p_destination);
  1239. Ref<DirAccess> da = DirAccess::create_for_path(p_source);
  1240. if (da.is_null()) {
  1241. return ERR_CANT_OPEN;
  1242. }
  1243. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1244. if (filesystem_da.is_null()) {
  1245. return ERR_CANT_OPEN;
  1246. }
  1247. if (!filesystem_da->dir_exists(p_destination)) {
  1248. Error make_dir_err = filesystem_da->make_dir_recursive(p_destination);
  1249. if (make_dir_err) {
  1250. return make_dir_err;
  1251. }
  1252. }
  1253. String asset = p_source.ends_with("/") ? p_source.left(p_source.length() - 1) : p_source;
  1254. if (asset.ends_with(".xcframework")) {
  1255. Ref<PList> plist;
  1256. plist.instantiate();
  1257. plist->load_file(p_source.path_join("Info.plist"));
  1258. Ref<PListNode> root_node = plist->get_root();
  1259. if (root_node.is_null()) {
  1260. return ERR_CANT_OPEN;
  1261. }
  1262. Dictionary root = root_node->get_value();
  1263. if (!root.has("AvailableLibraries")) {
  1264. return ERR_CANT_OPEN;
  1265. }
  1266. Ref<PListNode> libs_node = root["AvailableLibraries"];
  1267. if (libs_node.is_null()) {
  1268. return ERR_CANT_OPEN;
  1269. }
  1270. Array libs = libs_node->get_value();
  1271. for (int j = 0; j < libs.size(); j++) {
  1272. Ref<PListNode> lib_node = libs[j];
  1273. if (lib_node.is_null()) {
  1274. return ERR_CANT_OPEN;
  1275. }
  1276. Dictionary lib = lib_node->get_value();
  1277. if (lib.has("BinaryPath") && lib.has("LibraryPath") && lib.has("LibraryIdentifier")) {
  1278. Ref<PListNode> bpath_node = lib["BinaryPath"];
  1279. Ref<PListNode> lpath_node = lib["LibraryPath"];
  1280. Ref<PListNode> lid_node = lib["LibraryIdentifier"];
  1281. if (bpath_node.is_valid() && lpath_node.is_valid() && lid_node.is_valid()) {
  1282. String binary_path = bpath_node->get_value();
  1283. String library_identifier = lid_node->get_value();
  1284. String file_name = binary_path.get_basename().get_file();
  1285. String framework_name = file_name + ".framework";
  1286. bpath_node->data_string = framework_name.utf8();
  1287. lpath_node->data_string = framework_name.utf8();
  1288. if (!filesystem_da->dir_exists(p_destination.path_join(library_identifier))) {
  1289. filesystem_da->make_dir_recursive(p_destination.path_join(library_identifier));
  1290. }
  1291. _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);
  1292. if (lib.has("DebugSymbolsPath")) {
  1293. Ref<PListNode> dpath_node = lib["DebugSymbolsPath"];
  1294. if (dpath_node.is_valid()) {
  1295. String dpath = dpath_node->get_value();
  1296. if (da->dir_exists(p_source.path_join(library_identifier).path_join(dpath))) {
  1297. da->copy_dir(p_source.path_join(library_identifier).path_join(dpath), p_destination.path_join(library_identifier).path_join("dSYMs"));
  1298. }
  1299. }
  1300. }
  1301. }
  1302. }
  1303. }
  1304. String info_plist = plist->save_text();
  1305. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  1306. if (f.is_valid()) {
  1307. f->store_string(info_plist);
  1308. }
  1309. } else {
  1310. String file_name = p_destination.get_basename().get_file();
  1311. String framework_name = file_name + ".framework";
  1312. da->copy(p_source, p_destination.path_join(file_name));
  1313. // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib
  1314. {
  1315. List<String> install_name_args;
  1316. install_name_args.push_back("-id");
  1317. install_name_args.push_back(String("@rpath").path_join(framework_name).path_join(file_name));
  1318. install_name_args.push_back(p_destination.path_join(file_name));
  1319. OS::get_singleton()->execute("install_name_tool", install_name_args);
  1320. }
  1321. // Creating Info.plist
  1322. {
  1323. String lib_clean_name = file_name;
  1324. for (int i = 0; i < lib_clean_name.length(); i++) {
  1325. if (!is_ascii_alphanumeric_char(lib_clean_name[i]) && lib_clean_name[i] != '.' && lib_clean_name[i] != '-') {
  1326. lib_clean_name[i] = '-';
  1327. }
  1328. }
  1329. String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1330. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  1331. "<plist version=\"1.0\">\n"
  1332. " <dict>\n"
  1333. " <key>CFBundleShortVersionString</key>\n"
  1334. " <string>1.0</string>\n"
  1335. " <key>CFBundleIdentifier</key>\n"
  1336. " <string>$id.framework.$cl_name</string>\n"
  1337. " <key>CFBundleName</key>\n"
  1338. " <string>$name</string>\n"
  1339. " <key>CFBundleExecutable</key>\n"
  1340. " <string>$name</string>\n"
  1341. " <key>DTPlatformName</key>\n"
  1342. " <string>iphoneos</string>\n"
  1343. " <key>CFBundleInfoDictionaryVersion</key>\n"
  1344. " <string>6.0</string>\n"
  1345. " <key>CFBundleVersion</key>\n"
  1346. " <string>1</string>\n"
  1347. " <key>CFBundlePackageType</key>\n"
  1348. " <string>FMWK</string>\n"
  1349. " <key>MinimumOSVersion</key>\n"
  1350. " <string>12.0</string>\n"
  1351. " </dict>\n"
  1352. "</plist>";
  1353. String info_plist = info_plist_format.replace("$id", p_id).replace("$name", file_name).replace("$cl_name", lib_clean_name);
  1354. Ref<FileAccess> f = FileAccess::open(p_destination.path_join("Info.plist"), FileAccess::WRITE);
  1355. if (f.is_valid()) {
  1356. f->store_string(info_plist);
  1357. }
  1358. }
  1359. }
  1360. return OK;
  1361. }
  1362. 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) {
  1363. // that is just a random number, we just need Godot IDs not to clash with
  1364. // existing IDs in the project.
  1365. PbxId current_id = { 0x58938401, 0, 0 };
  1366. String pbx_files;
  1367. String pbx_frameworks_build;
  1368. String pbx_frameworks_refs;
  1369. String pbx_resources_build;
  1370. String pbx_resources_refs;
  1371. String pbx_embeded_frameworks;
  1372. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  1373. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = \"$name\"; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  1374. for (int i = 0; i < p_additional_assets.size(); ++i) {
  1375. String additional_asset_info_format = file_info_format;
  1376. String build_id = (++current_id).str();
  1377. String ref_id = (++current_id).str();
  1378. String framework_id = "";
  1379. const IOSExportAsset &asset = p_additional_assets[i];
  1380. String type;
  1381. if (asset.exported_path.ends_with(".framework")) {
  1382. if (asset.should_embed) {
  1383. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  1384. framework_id = (++current_id).str();
  1385. pbx_embeded_frameworks += framework_id + ",\n";
  1386. }
  1387. type = "wrapper.framework";
  1388. } else if (asset.exported_path.ends_with(".xcframework")) {
  1389. int total_libs = 0;
  1390. int static_libs = 0;
  1391. int dylibs = 0;
  1392. int frameworks = 0;
  1393. _check_xcframework_content(p_out_dir.path_join(asset.exported_path), total_libs, static_libs, dylibs, frameworks);
  1394. if (asset.should_embed && static_libs != total_libs) {
  1395. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  1396. framework_id = (++current_id).str();
  1397. pbx_embeded_frameworks += framework_id + ",\n";
  1398. }
  1399. type = "wrapper.xcframework";
  1400. } else if (asset.exported_path.ends_with(".dylib")) {
  1401. type = "compiled.mach-o.dylib";
  1402. } else if (asset.exported_path.ends_with(".a")) {
  1403. type = "archive.ar";
  1404. } else {
  1405. type = "file";
  1406. }
  1407. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  1408. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  1409. if (pbx_build.length() > 0) {
  1410. pbx_build += ",\n";
  1411. pbx_refs += ",\n";
  1412. }
  1413. pbx_build += build_id;
  1414. pbx_refs += ref_id;
  1415. Dictionary format_dict;
  1416. format_dict["build_id"] = build_id;
  1417. format_dict["ref_id"] = ref_id;
  1418. format_dict["name"] = asset.exported_path.get_file();
  1419. format_dict["file_path"] = asset.exported_path;
  1420. format_dict["file_type"] = type;
  1421. if (framework_id.length() > 0) {
  1422. format_dict["framework_id"] = framework_id;
  1423. }
  1424. pbx_files += additional_asset_info_format.format(format_dict, "$_");
  1425. }
  1426. // Note, frameworks like gamekit are always included in our project.pbxprof file
  1427. // even if turned off in capabilities.
  1428. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  1429. str = str.replace("$additional_pbx_files", pbx_files);
  1430. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  1431. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  1432. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  1433. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  1434. str = str.replace("$pbx_embeded_frameworks", pbx_embeded_frameworks);
  1435. CharString cs = str.utf8();
  1436. p_project_data.resize(cs.size() - 1);
  1437. for (int i = 0; i < cs.size() - 1; i++) {
  1438. p_project_data.write[i] = cs[i];
  1439. }
  1440. }
  1441. 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) {
  1442. String binary_name = p_out_dir.get_file().get_basename();
  1443. Ref<DirAccess> da = DirAccess::create_for_path(p_asset);
  1444. if (da.is_null()) {
  1445. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't open directory: " + p_asset + ".");
  1446. }
  1447. bool file_exists = da->file_exists(p_asset);
  1448. bool dir_exists = da->dir_exists(p_asset);
  1449. if (!file_exists && !dir_exists) {
  1450. return ERR_FILE_NOT_FOUND;
  1451. }
  1452. String base_dir = p_asset.get_base_dir().replace("res://", "").replace(".godot/mono/temp/bin/", "");
  1453. String asset = p_asset.ends_with("/") ? p_asset.left(p_asset.length() - 1) : p_asset;
  1454. String destination_dir;
  1455. String destination;
  1456. String asset_path;
  1457. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1458. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  1459. if (p_is_framework && asset.ends_with(".dylib")) {
  1460. // For iOS we need to turn .dylib into .framework
  1461. // to be able to send application to AppStore
  1462. asset_path = String("dylibs").path_join(base_dir);
  1463. String file_name;
  1464. if (!p_custom_file_name) {
  1465. file_name = p_asset.get_basename().get_file();
  1466. } else {
  1467. file_name = *p_custom_file_name;
  1468. }
  1469. String framework_name = file_name + ".framework";
  1470. asset_path = asset_path.path_join(framework_name);
  1471. destination_dir = p_out_dir.path_join(asset_path);
  1472. destination = destination_dir;
  1473. // Convert to framework and copy.
  1474. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1475. if (err) {
  1476. return err;
  1477. }
  1478. } else if (p_is_framework && asset.ends_with(".xcframework")) {
  1479. // For iOS we need to turn .dylib inside .xcframework
  1480. // into .framework to be able to send application to AppStore
  1481. int total_libs = 0;
  1482. int static_libs = 0;
  1483. int dylibs = 0;
  1484. int frameworks = 0;
  1485. _check_xcframework_content(p_asset, total_libs, static_libs, dylibs, frameworks);
  1486. asset_path = String("dylibs").path_join(base_dir);
  1487. String file_name;
  1488. if (!p_custom_file_name) {
  1489. file_name = p_asset.get_file();
  1490. } else {
  1491. file_name = *p_custom_file_name;
  1492. }
  1493. asset_path = asset_path.path_join(file_name);
  1494. destination_dir = p_out_dir.path_join(asset_path);
  1495. destination = destination_dir;
  1496. if (dylibs > 0) {
  1497. // Convert to framework and copy.
  1498. Error err = _convert_to_framework(p_asset, destination, p_preset->get("application/bundle_identifier"));
  1499. if (err) {
  1500. return err;
  1501. }
  1502. } else {
  1503. // Copy as is.
  1504. if (!filesystem_da->dir_exists(destination_dir)) {
  1505. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1506. if (make_dir_err) {
  1507. return make_dir_err;
  1508. }
  1509. }
  1510. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1511. if (err) {
  1512. return err;
  1513. }
  1514. }
  1515. } else if (p_is_framework && asset.ends_with(".framework")) {
  1516. // Framework.
  1517. asset_path = String("dylibs").path_join(base_dir);
  1518. String file_name;
  1519. if (!p_custom_file_name) {
  1520. file_name = p_asset.get_file();
  1521. } else {
  1522. file_name = *p_custom_file_name;
  1523. }
  1524. asset_path = asset_path.path_join(file_name);
  1525. destination_dir = p_out_dir.path_join(asset_path);
  1526. destination = destination_dir;
  1527. // Copy as is.
  1528. if (!filesystem_da->dir_exists(destination_dir)) {
  1529. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1530. if (make_dir_err) {
  1531. return make_dir_err;
  1532. }
  1533. }
  1534. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1535. if (err) {
  1536. return err;
  1537. }
  1538. } else {
  1539. // Unknown resource.
  1540. asset_path = base_dir;
  1541. String file_name;
  1542. if (!p_custom_file_name) {
  1543. file_name = p_asset.get_file();
  1544. } else {
  1545. file_name = *p_custom_file_name;
  1546. }
  1547. destination_dir = p_out_dir.path_join(asset_path);
  1548. asset_path = asset_path.path_join(file_name);
  1549. destination = p_out_dir.path_join(asset_path);
  1550. // Copy as is.
  1551. if (!filesystem_da->dir_exists(destination_dir)) {
  1552. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1553. if (make_dir_err) {
  1554. return make_dir_err;
  1555. }
  1556. }
  1557. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1558. if (err) {
  1559. return err;
  1560. }
  1561. }
  1562. if (asset_path.ends_with("/")) {
  1563. asset_path = asset_path.left(asset_path.length() - 1);
  1564. }
  1565. IOSExportAsset exported_asset = { binary_name.path_join(asset_path), p_is_framework, p_should_embed };
  1566. r_exported_assets.push_back(exported_asset);
  1567. return OK;
  1568. }
  1569. 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) {
  1570. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  1571. const String &asset = p_assets[f_idx];
  1572. if (asset.begins_with("res://")) {
  1573. Error err = _copy_asset(p_preset, p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1574. ERR_FAIL_COND_V(err != OK, err);
  1575. } else if (asset.is_absolute_path() && ProjectSettings::get_singleton()->localize_path(asset).begins_with("res://")) {
  1576. 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);
  1577. ERR_FAIL_COND_V(err != OK, err);
  1578. } else {
  1579. // either SDK-builtin or already a part of the export template
  1580. IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed };
  1581. r_exported_assets.push_back(exported_asset);
  1582. }
  1583. }
  1584. return OK;
  1585. }
  1586. 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) {
  1587. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1588. for (int i = 0; i < export_plugins.size(); i++) {
  1589. Vector<String> linked_frameworks = export_plugins[i]->get_ios_frameworks();
  1590. Error err = _export_additional_assets(p_preset, p_out_dir, linked_frameworks, true, false, r_exported_assets);
  1591. ERR_FAIL_COND_V(err, err);
  1592. Vector<String> embedded_frameworks = export_plugins[i]->get_ios_embedded_frameworks();
  1593. err = _export_additional_assets(p_preset, p_out_dir, embedded_frameworks, true, true, r_exported_assets);
  1594. ERR_FAIL_COND_V(err, err);
  1595. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1596. for (int j = 0; j < project_static_libs.size(); j++) {
  1597. project_static_libs.write[j] = project_static_libs[j].get_file(); // Only the file name as it's copied to the project
  1598. }
  1599. err = _export_additional_assets(p_preset, p_out_dir, project_static_libs, true, false, r_exported_assets);
  1600. ERR_FAIL_COND_V(err, err);
  1601. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  1602. err = _export_additional_assets(p_preset, p_out_dir, ios_bundle_files, false, false, r_exported_assets);
  1603. ERR_FAIL_COND_V(err, err);
  1604. }
  1605. Vector<String> library_paths;
  1606. for (int i = 0; i < p_libraries.size(); ++i) {
  1607. library_paths.push_back(p_libraries[i].path);
  1608. }
  1609. Error err = _export_additional_assets(p_preset, p_out_dir, library_paths, true, true, r_exported_assets);
  1610. ERR_FAIL_COND_V(err, err);
  1611. return OK;
  1612. }
  1613. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) const {
  1614. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  1615. Vector<String> enabled_archs;
  1616. for (int i = 0; i < all_archs.size(); ++i) {
  1617. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  1618. if (is_enabled) {
  1619. enabled_archs.push_back(all_archs[i].name);
  1620. }
  1621. }
  1622. return enabled_archs;
  1623. }
  1624. 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) {
  1625. String plugin_definition_cpp_code;
  1626. String plugin_initialization_cpp_code;
  1627. String plugin_deinitialization_cpp_code;
  1628. Vector<String> plugin_linked_dependencies;
  1629. Vector<String> plugin_embedded_dependencies;
  1630. Vector<String> plugin_files;
  1631. Vector<PluginConfigIOS> enabled_plugins = get_enabled_plugins(p_preset);
  1632. Vector<String> added_linked_dependenciy_names;
  1633. Vector<String> added_embedded_dependenciy_names;
  1634. HashMap<String, String> plist_values;
  1635. HashSet<String> plugin_linker_flags;
  1636. Error err;
  1637. for (int i = 0; i < enabled_plugins.size(); i++) {
  1638. PluginConfigIOS plugin = enabled_plugins[i];
  1639. // Export plugin binary.
  1640. String plugin_main_binary = PluginConfigIOS::get_plugin_main_binary(plugin, p_debug);
  1641. String plugin_binary_result_file = plugin.binary.get_file();
  1642. // We shouldn't embed .xcframework that contains static libraries.
  1643. // Static libraries are not embedded anyway.
  1644. err = _copy_asset(p_preset, dest_dir, plugin_main_binary, &plugin_binary_result_file, true, false, r_exported_assets);
  1645. ERR_FAIL_COND_V(err != OK, err);
  1646. // Adding dependencies.
  1647. // Use separate container for names to check for duplicates.
  1648. for (int j = 0; j < plugin.linked_dependencies.size(); j++) {
  1649. String dependency = plugin.linked_dependencies[j];
  1650. String name = dependency.get_file();
  1651. if (added_linked_dependenciy_names.has(name)) {
  1652. continue;
  1653. }
  1654. added_linked_dependenciy_names.push_back(name);
  1655. plugin_linked_dependencies.push_back(dependency);
  1656. }
  1657. for (int j = 0; j < plugin.system_dependencies.size(); j++) {
  1658. String dependency = plugin.system_dependencies[j];
  1659. String name = dependency.get_file();
  1660. if (added_linked_dependenciy_names.has(name)) {
  1661. continue;
  1662. }
  1663. added_linked_dependenciy_names.push_back(name);
  1664. plugin_linked_dependencies.push_back(dependency);
  1665. }
  1666. for (int j = 0; j < plugin.embedded_dependencies.size(); j++) {
  1667. String dependency = plugin.embedded_dependencies[j];
  1668. String name = dependency.get_file();
  1669. if (added_embedded_dependenciy_names.has(name)) {
  1670. continue;
  1671. }
  1672. added_embedded_dependenciy_names.push_back(name);
  1673. plugin_embedded_dependencies.push_back(dependency);
  1674. }
  1675. plugin_files.append_array(plugin.files_to_copy);
  1676. // Capabilities
  1677. // Also checking for duplicates.
  1678. for (int j = 0; j < plugin.capabilities.size(); j++) {
  1679. String capability = plugin.capabilities[j];
  1680. if (p_config_data.capabilities.has(capability)) {
  1681. continue;
  1682. }
  1683. p_config_data.capabilities.push_back(capability);
  1684. }
  1685. // Linker flags
  1686. // Checking duplicates
  1687. for (int j = 0; j < plugin.linker_flags.size(); j++) {
  1688. String linker_flag = plugin.linker_flags[j];
  1689. plugin_linker_flags.insert(linker_flag);
  1690. }
  1691. // Plist
  1692. // Using hash map container to remove duplicates
  1693. for (const KeyValue<String, PluginConfigIOS::PlistItem> &E : plugin.plist) {
  1694. String key = E.key;
  1695. const PluginConfigIOS::PlistItem &item = E.value;
  1696. String value;
  1697. switch (item.type) {
  1698. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  1699. String preset_name = "plugins_plist/" + key;
  1700. String input_value = p_preset->get(preset_name);
  1701. value = "<string>" + input_value + "</string>";
  1702. } break;
  1703. default:
  1704. value = item.value;
  1705. break;
  1706. }
  1707. if (key.is_empty() || value.is_empty()) {
  1708. continue;
  1709. }
  1710. String plist_key = "<key>" + key + "</key>";
  1711. plist_values[plist_key] = value;
  1712. }
  1713. // CPP Code
  1714. String definition_comment = "// Plugin: " + plugin.name + "\n";
  1715. String initialization_method = plugin.initialization_method + "();\n";
  1716. String deinitialization_method = plugin.deinitialization_method + "();\n";
  1717. plugin_definition_cpp_code += definition_comment +
  1718. "extern void " + initialization_method +
  1719. "extern void " + deinitialization_method + "\n";
  1720. plugin_initialization_cpp_code += "\t" + initialization_method;
  1721. plugin_deinitialization_cpp_code += "\t" + deinitialization_method;
  1722. if (plugin.use_swift_runtime) {
  1723. p_config_data.use_swift_runtime = true;
  1724. }
  1725. }
  1726. // Updating `Info.plist`
  1727. {
  1728. for (const KeyValue<String, String> &E : plist_values) {
  1729. String key = E.key;
  1730. String value = E.value;
  1731. if (key.is_empty() || value.is_empty()) {
  1732. continue;
  1733. }
  1734. p_config_data.plist_content += key + value + "\n";
  1735. }
  1736. }
  1737. // Export files
  1738. {
  1739. // Export linked plugin dependency
  1740. err = _export_additional_assets(p_preset, dest_dir, plugin_linked_dependencies, true, false, r_exported_assets);
  1741. ERR_FAIL_COND_V(err != OK, err);
  1742. // Export embedded plugin dependency
  1743. err = _export_additional_assets(p_preset, dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets);
  1744. ERR_FAIL_COND_V(err != OK, err);
  1745. // Export plugin files
  1746. err = _export_additional_assets(p_preset, dest_dir, plugin_files, false, false, r_exported_assets);
  1747. ERR_FAIL_COND_V(err != OK, err);
  1748. }
  1749. // Update CPP
  1750. {
  1751. Dictionary plugin_format;
  1752. plugin_format["definition"] = plugin_definition_cpp_code;
  1753. plugin_format["initialization"] = plugin_initialization_cpp_code;
  1754. plugin_format["deinitialization"] = plugin_deinitialization_cpp_code;
  1755. String plugin_cpp_code = "\n// Godot Plugins\n"
  1756. "void godot_ios_plugins_initialize();\n"
  1757. "void godot_ios_plugins_deinitialize();\n"
  1758. "// Exported Plugins\n\n"
  1759. "$definition"
  1760. "// Use Plugins\n"
  1761. "void godot_ios_plugins_initialize() {\n"
  1762. "$initialization"
  1763. "}\n\n"
  1764. "void godot_ios_plugins_deinitialize() {\n"
  1765. "$deinitialization"
  1766. "}\n";
  1767. p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_");
  1768. }
  1769. // Update Linker Flag Values
  1770. {
  1771. String result_linker_flags = " ";
  1772. for (const String &E : plugin_linker_flags) {
  1773. const String &flag = E;
  1774. if (flag.length() == 0) {
  1775. continue;
  1776. }
  1777. if (result_linker_flags.length() > 0) {
  1778. result_linker_flags += ' ';
  1779. }
  1780. result_linker_flags += flag;
  1781. }
  1782. result_linker_flags = result_linker_flags.replace("\"", "\\\"");
  1783. p_config_data.linker_flags += result_linker_flags;
  1784. }
  1785. return OK;
  1786. }
  1787. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1788. return _export_project_helper(p_preset, p_debug, p_path, p_flags, false, false);
  1789. }
  1790. 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) {
  1791. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1792. const String dest_dir = p_path.get_base_dir() + "/";
  1793. const String binary_name = p_path.get_file().get_basename();
  1794. const String binary_dir = dest_dir + binary_name;
  1795. if (!DirAccess::exists(dest_dir)) {
  1796. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), dest_dir));
  1797. return ERR_FILE_BAD_PATH;
  1798. }
  1799. bool export_project_only = p_preset->get("application/export_project_only");
  1800. if (p_oneclick) {
  1801. export_project_only = false; // Skip for one-click deploy.
  1802. }
  1803. EditorProgress ep("export", export_project_only ? TTR("Exporting for iOS (Project Files Only)") : TTR("Exporting for iOS"), export_project_only ? 2 : 5, true);
  1804. String team_id = p_preset->get("application/app_store_team_id");
  1805. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  1806. String src_pkg_name;
  1807. if (p_debug) {
  1808. src_pkg_name = p_preset->get("custom_template/debug");
  1809. } else {
  1810. src_pkg_name = p_preset->get("custom_template/release");
  1811. }
  1812. if (src_pkg_name.is_empty()) {
  1813. String err;
  1814. src_pkg_name = find_export_template("ios.zip", &err);
  1815. if (src_pkg_name.is_empty()) {
  1816. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Export template not found."));
  1817. return ERR_FILE_NOT_FOUND;
  1818. }
  1819. }
  1820. {
  1821. bool delete_old = p_preset->get("application/delete_old_export_files_unconditionally");
  1822. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1823. if (da.is_valid()) {
  1824. String current_dir = da->get_current_dir();
  1825. // Remove leftovers from last export so they don't interfere in case some files are no longer needed.
  1826. if (da->change_dir(binary_dir + ".xcodeproj") == OK) {
  1827. // Check directory content before deleting.
  1828. int expected_files = 0;
  1829. int total_files = 0;
  1830. if (!delete_old) {
  1831. da->list_dir_begin();
  1832. for (String n = da->get_next(); !n.is_empty(); n = da->get_next()) {
  1833. if (!n.begins_with(".")) { // Ignore ".", ".." and hidden files.
  1834. if (da->current_is_dir()) {
  1835. if (n == "xcshareddata" || n == "project.xcworkspace") {
  1836. expected_files++;
  1837. }
  1838. } else {
  1839. if (n == "project.pbxproj") {
  1840. expected_files++;
  1841. }
  1842. }
  1843. total_files++;
  1844. }
  1845. }
  1846. da->list_dir_end();
  1847. }
  1848. if ((total_files == 0) || (expected_files >= Math::floor(total_files * 0.8))) {
  1849. da->erase_contents_recursive();
  1850. } else {
  1851. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Unexpected files found in the export destination directory \"%s.xcodeproj\", delete it manually or select another destination."), binary_dir));
  1852. return ERR_CANT_CREATE;
  1853. }
  1854. }
  1855. da->change_dir(current_dir);
  1856. if (da->change_dir(binary_dir) == OK) {
  1857. // Check directory content before deleting.
  1858. int expected_files = 0;
  1859. int total_files = 0;
  1860. if (!delete_old) {
  1861. da->list_dir_begin();
  1862. for (String n = da->get_next(); !n.is_empty(); n = da->get_next()) {
  1863. if (!n.begins_with(".")) { // Ignore ".", ".." and hidden files.
  1864. if (da->current_is_dir()) {
  1865. if (n == "dylibs" || n == "Images.xcassets" || n.ends_with(".lproj") || n == "godot-publish-dotnet" || n.ends_with(".xcframework") || n.ends_with(".framework")) {
  1866. expected_files++;
  1867. }
  1868. } else {
  1869. if (n == binary_name + "-Info.plist" || n == binary_name + ".entitlements" || n == "Launch Screen.storyboard" || n == "export_options.plist" || n.begins_with("dummy.") || n.ends_with(".gdip")) {
  1870. expected_files++;
  1871. }
  1872. }
  1873. total_files++;
  1874. }
  1875. }
  1876. da->list_dir_end();
  1877. }
  1878. if ((total_files == 0) || (expected_files >= Math::floor(total_files * 0.8))) {
  1879. da->erase_contents_recursive();
  1880. } else {
  1881. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Unexpected files found in the export destination directory \"%s\", delete it manually or select another destination."), binary_dir));
  1882. return ERR_CANT_CREATE;
  1883. }
  1884. }
  1885. da->change_dir(current_dir);
  1886. if (!da->dir_exists(binary_dir)) {
  1887. Error err = da->make_dir(binary_dir);
  1888. if (err != OK) {
  1889. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create the directory: \"%s\""), binary_dir));
  1890. return err;
  1891. }
  1892. }
  1893. }
  1894. }
  1895. if (ep.step("Making .pck", 0)) {
  1896. return ERR_SKIP;
  1897. }
  1898. String pack_path = binary_dir + ".pck";
  1899. Vector<SharedObject> libraries;
  1900. Error err = save_pack(p_preset, p_debug, pack_path, &libraries);
  1901. if (err) {
  1902. // Message is supplied by the subroutine method.
  1903. return err;
  1904. }
  1905. if (ep.step("Extracting and configuring Xcode project", 1)) {
  1906. return ERR_SKIP;
  1907. }
  1908. String library_to_use = "libgodot.ios." + String(p_debug ? "debug" : "release") + ".xcframework";
  1909. print_line("Static framework: " + library_to_use);
  1910. String pkg_name;
  1911. if (String(GLOBAL_GET("application/config/name")) != "") {
  1912. pkg_name = String(GLOBAL_GET("application/config/name"));
  1913. } else {
  1914. pkg_name = "Unnamed";
  1915. }
  1916. bool found_library = false;
  1917. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  1918. HashSet<String> files_to_parse;
  1919. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  1920. files_to_parse.insert(project_file);
  1921. files_to_parse.insert("godot_ios/export_options.plist");
  1922. files_to_parse.insert("godot_ios/dummy.cpp");
  1923. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  1924. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  1925. files_to_parse.insert("godot_ios/godot_ios.entitlements");
  1926. files_to_parse.insert("godot_ios/Launch Screen.storyboard");
  1927. files_to_parse.insert("PrivacyInfo.xcprivacy");
  1928. IOSConfigData config_data = {
  1929. pkg_name,
  1930. binary_name,
  1931. _get_additional_plist_content(),
  1932. String(" ").join(_get_preset_architectures(p_preset)),
  1933. _get_linker_flags(),
  1934. _get_cpp_code(),
  1935. "",
  1936. "",
  1937. "",
  1938. "",
  1939. Vector<String>(),
  1940. false
  1941. };
  1942. config_data.plist_content += p_preset->get("application/additional_plist_content").operator String() + "\n";
  1943. Vector<IOSExportAsset> assets;
  1944. Ref<DirAccess> tmp_app_path = DirAccess::create_for_path(dest_dir);
  1945. if (tmp_app_path.is_null()) {
  1946. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), dest_dir));
  1947. return ERR_CANT_CREATE;
  1948. }
  1949. print_line("Unzipping...");
  1950. Ref<FileAccess> io_fa;
  1951. zlib_filefunc_def io = zipio_create_io(&io_fa);
  1952. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  1953. if (!src_pkg_zip) {
  1954. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Could not open export template (not a zip file?): \"%s\".", src_pkg_name));
  1955. return ERR_CANT_OPEN;
  1956. }
  1957. err = _export_ios_plugins(p_preset, config_data, binary_dir, assets, p_debug);
  1958. if (err != OK) {
  1959. // TODO: Improve error reporting by using `add_message` throughout all methods called via `_export_ios_plugins`.
  1960. // For now a generic top level message would be fine, but we're ought to use proper reporting here instead of
  1961. // just fail macros and non-descriptive error return values.
  1962. add_message(EXPORT_MESSAGE_ERROR, TTR("iOS Plugins"), vformat(TTR("Failed to export iOS plugins with code %d. Please check the output log."), err));
  1963. return err;
  1964. }
  1965. //export rest of the files
  1966. int ret = unzGoToFirstFile(src_pkg_zip);
  1967. Vector<uint8_t> project_file_data;
  1968. while (ret == UNZ_OK) {
  1969. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1970. bool is_execute = false;
  1971. #endif
  1972. //get filename
  1973. unz_file_info info;
  1974. char fname[16384];
  1975. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  1976. if (ret != UNZ_OK) {
  1977. break;
  1978. }
  1979. String file = String::utf8(fname);
  1980. print_line("READ: " + file);
  1981. Vector<uint8_t> data;
  1982. data.resize(info.uncompressed_size);
  1983. //read
  1984. unzOpenCurrentFile(src_pkg_zip);
  1985. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  1986. unzCloseCurrentFile(src_pkg_zip);
  1987. //write
  1988. if (files_to_parse.has(file)) {
  1989. _fix_config_file(p_preset, data, config_data, p_debug);
  1990. } else if (file.begins_with("libgodot.ios")) {
  1991. if (!file.begins_with(library_to_use) || file.ends_with(String("/empty"))) {
  1992. ret = unzGoToNextFile(src_pkg_zip);
  1993. continue; //ignore!
  1994. }
  1995. found_library = true;
  1996. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  1997. is_execute = true;
  1998. #endif
  1999. file = file.replace(library_to_use, binary_name + ".xcframework");
  2000. }
  2001. if (file == project_file) {
  2002. project_file_data = data;
  2003. }
  2004. ///@TODO need to parse logo files
  2005. if (data.size() > 0) {
  2006. file = file.replace("godot_ios", binary_name);
  2007. print_line("ADDING: " + file + " size: " + itos(data.size()));
  2008. /* write it into our folder structure */
  2009. file = dest_dir + file;
  2010. /* make sure this folder exists */
  2011. String dir_name = file.get_base_dir();
  2012. if (!tmp_app_path->dir_exists(dir_name)) {
  2013. print_line("Creating " + dir_name);
  2014. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  2015. if (dir_err) {
  2016. unzClose(src_pkg_zip);
  2017. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), dir_name));
  2018. return ERR_CANT_CREATE;
  2019. }
  2020. }
  2021. /* write the file */
  2022. {
  2023. Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);
  2024. if (f.is_null()) {
  2025. unzClose(src_pkg_zip);
  2026. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), file));
  2027. return ERR_CANT_CREATE;
  2028. };
  2029. f->store_buffer(data.ptr(), data.size());
  2030. }
  2031. #if defined(MACOS_ENABLED) || defined(LINUXBSD_ENABLED)
  2032. if (is_execute) {
  2033. // we need execute rights on this file
  2034. chmod(file.utf8().get_data(), 0755);
  2035. }
  2036. #endif
  2037. }
  2038. ret = unzGoToNextFile(src_pkg_zip);
  2039. }
  2040. // We're done with our source zip.
  2041. unzClose(src_pkg_zip);
  2042. if (!found_library) {
  2043. 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));
  2044. return ERR_FILE_NOT_FOUND;
  2045. }
  2046. // Check and generate missing ARM64 simulator library.
  2047. if (p_preset->get("application/generate_simulator_library_if_missing").operator bool()) {
  2048. String sim_lib_path = dest_dir + String(binary_name + ".xcframework").path_join("ios-arm64_x86_64-simulator").path_join("libgodot.a");
  2049. String dev_lib_path = dest_dir + String(binary_name + ".xcframework").path_join("ios-arm64").path_join("libgodot.a");
  2050. String tmp_lib_path = EditorPaths::get_singleton()->get_cache_dir().path_join(binary_name + "_lipo_");
  2051. uint32_t cputype = 0;
  2052. uint32_t cpusubtype = 0;
  2053. if (!_archive_has_arm64(sim_lib_path, &cputype, &cpusubtype) && _archive_has_arm64(dev_lib_path) && FileAccess::exists(dev_lib_path)) {
  2054. add_message(EXPORT_MESSAGE_INFO, TTR("Export"), TTR("ARM64 simulator library, generating from device library."));
  2055. Vector<String> tmp_lib_files;
  2056. Vector<Vector2i> tmp_lib_cputypes;
  2057. // Extract/copy simulator lib.
  2058. if (FileAccess::exists(sim_lib_path)) {
  2059. if (LipO::is_lipo(sim_lib_path)) {
  2060. Ref<LipO> lipo;
  2061. lipo.instantiate();
  2062. if (lipo->open_file(sim_lib_path)) {
  2063. for (int i = 0; i < lipo->get_arch_count(); i++) {
  2064. const String &f_name = tmp_lib_path + itos(tmp_lib_files.size());
  2065. lipo->extract_arch(i, f_name);
  2066. tmp_lib_files.push_back(f_name);
  2067. tmp_lib_cputypes.push_back(Vector2i(lipo->get_arch_cputype(i), lipo->get_arch_cpusubtype(i)));
  2068. }
  2069. }
  2070. } else {
  2071. const String &f_name = tmp_lib_path + itos(tmp_lib_files.size());
  2072. tmp_app_path->copy(sim_lib_path, f_name);
  2073. tmp_lib_files.push_back(f_name);
  2074. tmp_lib_cputypes.push_back(Vector2i(cputype, cpusubtype));
  2075. }
  2076. }
  2077. // Copy device lib.
  2078. if (LipO::is_lipo(dev_lib_path)) {
  2079. Ref<LipO> lipo;
  2080. lipo.instantiate();
  2081. if (lipo->open_file(dev_lib_path)) {
  2082. for (int i = 0; i < lipo->get_arch_count(); i++) {
  2083. if (lipo->get_arch_cputype(i) == 0x100000c && lipo->get_arch_cpusubtype(i) == 0) {
  2084. const String &f_name = tmp_lib_path + itos(tmp_lib_files.size());
  2085. lipo->extract_arch(i, f_name);
  2086. tmp_lib_files.push_back(f_name);
  2087. tmp_lib_cputypes.push_back(Vector2i(0x100000c, 0)); // ARM64.
  2088. break;
  2089. }
  2090. }
  2091. }
  2092. } else {
  2093. const String &f_name = tmp_lib_path + itos(tmp_lib_files.size());
  2094. tmp_app_path->copy(dev_lib_path, f_name);
  2095. tmp_lib_files.push_back(f_name);
  2096. tmp_lib_cputypes.push_back(Vector2i(0x100000c, 0)); // ARM64.
  2097. }
  2098. // Patch device lib.
  2099. int patch_count = _archive_convert_to_simulator(tmp_lib_path + itos(tmp_lib_files.size() - 1));
  2100. if (patch_count == 0) {
  2101. add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), TTR("Unable to generate ARM64 simulator library."));
  2102. } else {
  2103. // Repack.
  2104. Ref<LipO> lipo;
  2105. lipo.instantiate();
  2106. lipo->create_file(sim_lib_path, tmp_lib_files, tmp_lib_cputypes);
  2107. }
  2108. // Cleanup.
  2109. for (const String &E : tmp_lib_files) {
  2110. tmp_app_path->remove(E);
  2111. }
  2112. }
  2113. }
  2114. // Generate translations files.
  2115. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  2116. Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
  2117. Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
  2118. Dictionary photolibrary_usage_descriptions = p_preset->get("privacy/photolibrary_usage_description_localized");
  2119. Vector<String> translations = GLOBAL_GET("internationalization/locale/translations");
  2120. if (translations.size() > 0) {
  2121. {
  2122. String fname = binary_dir + "/en.lproj";
  2123. tmp_app_path->make_dir_recursive(fname);
  2124. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  2125. f->store_line("/* Localized versions of Info.plist keys */");
  2126. f->store_line("");
  2127. f->store_line("CFBundleDisplayName = \"" + GLOBAL_GET("application/config/name").operator String() + "\";");
  2128. f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
  2129. f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
  2130. f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photolibrary_usage_description").operator String() + "\";");
  2131. }
  2132. HashSet<String> languages;
  2133. for (const String &E : translations) {
  2134. Ref<Translation> tr = ResourceLoader::load(E);
  2135. if (tr.is_valid() && tr->get_locale() != "en") {
  2136. languages.insert(tr->get_locale());
  2137. }
  2138. }
  2139. for (const String &lang : languages) {
  2140. String fname = binary_dir + "/" + lang + ".lproj";
  2141. tmp_app_path->make_dir_recursive(fname);
  2142. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  2143. f->store_line("/* Localized versions of Info.plist keys */");
  2144. f->store_line("");
  2145. if (appnames.has(lang)) {
  2146. f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
  2147. }
  2148. if (camera_usage_descriptions.has(lang)) {
  2149. f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
  2150. }
  2151. if (microphone_usage_descriptions.has(lang)) {
  2152. f->store_line("NSMicrophoneUsageDescription = \"" + microphone_usage_descriptions[lang].operator String() + "\";");
  2153. }
  2154. if (photolibrary_usage_descriptions.has(lang)) {
  2155. f->store_line("NSPhotoLibraryUsageDescription = \"" + photolibrary_usage_descriptions[lang].operator String() + "\";");
  2156. }
  2157. }
  2158. }
  2159. // Copy project static libs to the project
  2160. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  2161. for (int i = 0; i < export_plugins.size(); i++) {
  2162. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  2163. for (int j = 0; j < project_static_libs.size(); j++) {
  2164. const String &static_lib_path = project_static_libs[j];
  2165. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  2166. Error lib_copy_err = tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  2167. if (lib_copy_err != OK) {
  2168. 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));
  2169. return lib_copy_err;
  2170. }
  2171. }
  2172. }
  2173. String iconset_dir = binary_dir + "/Images.xcassets/AppIcon.appiconset/";
  2174. err = OK;
  2175. if (!tmp_app_path->dir_exists(iconset_dir)) {
  2176. err = tmp_app_path->make_dir_recursive(iconset_dir);
  2177. }
  2178. if (err != OK) {
  2179. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create a directory at path \"%s\"."), iconset_dir));
  2180. return err;
  2181. }
  2182. err = _export_icons(p_preset, iconset_dir);
  2183. if (err != OK) {
  2184. // Message is supplied by the subroutine method.
  2185. return err;
  2186. }
  2187. {
  2188. String splash_image_path = binary_dir + "/Images.xcassets/SplashImage.imageset/";
  2189. Ref<DirAccess> launch_screen_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2190. if (launch_screen_da.is_null()) {
  2191. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not access the filesystem."));
  2192. return ERR_CANT_CREATE;
  2193. }
  2194. print_line("Exporting launch screen storyboard");
  2195. err = _export_loading_screen_file(p_preset, splash_image_path);
  2196. if (err != OK) {
  2197. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Failed to create a file at path \"%s\" with code %d."), splash_image_path, err));
  2198. }
  2199. }
  2200. if (err != OK) {
  2201. return err;
  2202. }
  2203. print_line("Exporting additional assets");
  2204. _export_additional_assets(p_preset, binary_dir, libraries, assets);
  2205. _add_assets_to_project(dest_dir, p_preset, project_file_data, assets);
  2206. String project_file_name = binary_dir + ".xcodeproj/project.pbxproj";
  2207. {
  2208. Ref<FileAccess> f = FileAccess::open(project_file_name, FileAccess::WRITE);
  2209. if (f.is_null()) {
  2210. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write to a file at path \"%s\"."), project_file_name));
  2211. return ERR_CANT_CREATE;
  2212. };
  2213. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  2214. }
  2215. #ifdef MACOS_ENABLED
  2216. {
  2217. if (ep.step("Code-signing dylibs", 2)) {
  2218. return ERR_SKIP;
  2219. }
  2220. Ref<DirAccess> dylibs_dir = DirAccess::open(binary_dir + "/dylibs");
  2221. ERR_FAIL_COND_V(dylibs_dir.is_null(), ERR_CANT_OPEN);
  2222. CodesignData codesign_data(p_preset, p_debug);
  2223. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  2224. if (err != OK) {
  2225. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Code signing failed, see editor log for details."));
  2226. return err;
  2227. }
  2228. }
  2229. if (export_project_only) {
  2230. return OK;
  2231. }
  2232. if (ep.step("Making .xcarchive", 3)) {
  2233. return ERR_SKIP;
  2234. }
  2235. String archive_path = p_path.get_basename() + ".xcarchive";
  2236. List<String> archive_args;
  2237. archive_args.push_back("-project");
  2238. archive_args.push_back(binary_dir + ".xcodeproj");
  2239. archive_args.push_back("-scheme");
  2240. archive_args.push_back(binary_name);
  2241. archive_args.push_back("-sdk");
  2242. if (p_simulator) {
  2243. archive_args.push_back("iphonesimulator");
  2244. } else {
  2245. archive_args.push_back("iphoneos");
  2246. }
  2247. archive_args.push_back("-configuration");
  2248. archive_args.push_back(p_debug ? "Debug" : "Release");
  2249. archive_args.push_back("-destination");
  2250. if (p_simulator) {
  2251. archive_args.push_back("generic/platform=iOS Simulator");
  2252. } else {
  2253. archive_args.push_back("generic/platform=iOS");
  2254. }
  2255. archive_args.push_back("archive");
  2256. archive_args.push_back("-allowProvisioningUpdates");
  2257. archive_args.push_back("-archivePath");
  2258. archive_args.push_back(archive_path);
  2259. String archive_str;
  2260. err = OS::get_singleton()->execute("xcodebuild", archive_args, &archive_str, nullptr, true);
  2261. if (err != OK) {
  2262. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  2263. return err;
  2264. }
  2265. print_line("xcodebuild (.xcarchive):\n" + archive_str);
  2266. if (!archive_str.contains("** ARCHIVE SUCCEEDED **")) {
  2267. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR("Xcode project build failed, see editor log for details."));
  2268. return FAILED;
  2269. }
  2270. if (!p_oneclick) {
  2271. if (ep.step("Making .ipa", 4)) {
  2272. return ERR_SKIP;
  2273. }
  2274. List<String> export_args;
  2275. export_args.push_back("-exportArchive");
  2276. export_args.push_back("-archivePath");
  2277. export_args.push_back(archive_path);
  2278. export_args.push_back("-exportOptionsPlist");
  2279. export_args.push_back(binary_dir + "/export_options.plist");
  2280. export_args.push_back("-allowProvisioningUpdates");
  2281. export_args.push_back("-exportPath");
  2282. export_args.push_back(dest_dir);
  2283. String export_str;
  2284. err = OS::get_singleton()->execute("xcodebuild", export_args, &export_str, nullptr, true);
  2285. if (err != OK) {
  2286. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), vformat(TTR("Failed to run xcodebuild with code %d"), err));
  2287. return err;
  2288. }
  2289. print_line("xcodebuild (.ipa):\n" + export_str);
  2290. if (!export_str.contains("** EXPORT SUCCEEDED **")) {
  2291. add_message(EXPORT_MESSAGE_ERROR, TTR("Xcode Build"), TTR(".ipa export failed, see editor log for details."));
  2292. return FAILED;
  2293. }
  2294. }
  2295. #else
  2296. add_message(EXPORT_MESSAGE_WARNING, TTR("Xcode Build"), TTR(".ipa can only be built on macOS. Leaving Xcode project without building the package."));
  2297. #endif
  2298. return OK;
  2299. }
  2300. bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  2301. #if defined(MODULE_MONO_ENABLED) && !defined(MACOS_ENABLED)
  2302. // TODO: Remove this restriction when we don't rely on macOS tools to package up the native libraries anymore.
  2303. r_error += TTR("Exporting to iOS when using C#/.NET is experimental and requires macOS.") + "\n";
  2304. return false;
  2305. #else
  2306. String err;
  2307. bool valid = false;
  2308. #if defined(MODULE_MONO_ENABLED)
  2309. // iOS export is still a work in progress, keep a message as a warning.
  2310. err += TTR("Exporting to iOS when using C#/.NET is experimental.") + "\n";
  2311. #endif
  2312. // Look for export templates (first official, and if defined custom templates).
  2313. bool dvalid = exists_export_template("ios.zip", &err);
  2314. bool rvalid = dvalid; // Both in the same ZIP.
  2315. if (p_preset->get("custom_template/debug") != "") {
  2316. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  2317. if (!dvalid) {
  2318. err += TTR("Custom debug template not found.") + "\n";
  2319. }
  2320. }
  2321. if (p_preset->get("custom_template/release") != "") {
  2322. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  2323. if (!rvalid) {
  2324. err += TTR("Custom release template not found.") + "\n";
  2325. }
  2326. }
  2327. valid = dvalid || rvalid;
  2328. r_missing_templates = !valid;
  2329. const String &additional_plist_content = p_preset->get("application/additional_plist_content");
  2330. if (!additional_plist_content.is_empty()) {
  2331. const String &plist = vformat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  2332. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
  2333. "<plist version=\"1.0\">"
  2334. "<dict>\n"
  2335. "%s\n"
  2336. "</dict>\n"
  2337. "</plist>\n",
  2338. additional_plist_content);
  2339. String plist_err;
  2340. Ref<PList> plist_parser;
  2341. plist_parser.instantiate();
  2342. if (!plist_parser->load_string(plist, plist_err)) {
  2343. err += TTR("Invalid additional PList content: ") + plist_err + "\n";
  2344. valid = false;
  2345. }
  2346. }
  2347. if (GLOBAL_GET("rendering/rendering_device/driver.ios") == "metal") {
  2348. float version = p_preset->get("application/min_ios_version").operator String().to_float();
  2349. if (version < 14.0) {
  2350. err += TTR("Metal renderer require iOS 14+.") + "\n";
  2351. }
  2352. }
  2353. if (!err.is_empty()) {
  2354. r_error = err;
  2355. }
  2356. return valid;
  2357. #endif // !(MODULE_MONO_ENABLED && !MACOS_ENABLED)
  2358. }
  2359. bool EditorExportPlatformIOS::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  2360. String err;
  2361. bool valid = true;
  2362. // Validate the project configuration.
  2363. List<ExportOption> options;
  2364. get_export_options(&options);
  2365. for (const EditorExportPlatform::ExportOption &E : options) {
  2366. if (get_export_option_visibility(p_preset.ptr(), E.option.name)) {
  2367. String warn = get_export_option_warning(p_preset.ptr(), E.option.name);
  2368. if (!warn.is_empty()) {
  2369. err += warn + "\n";
  2370. if (E.required) {
  2371. valid = false;
  2372. }
  2373. }
  2374. }
  2375. }
  2376. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  2377. valid = false;
  2378. }
  2379. if (!err.is_empty()) {
  2380. r_error = err;
  2381. }
  2382. return valid;
  2383. }
  2384. int EditorExportPlatformIOS::get_options_count() const {
  2385. MutexLock lock(device_lock);
  2386. return devices.size();
  2387. }
  2388. String EditorExportPlatformIOS::get_options_tooltip() const {
  2389. return TTR("Select device from the list");
  2390. }
  2391. Ref<ImageTexture> EditorExportPlatformIOS::get_option_icon(int p_index) const {
  2392. MutexLock lock(device_lock);
  2393. Ref<ImageTexture> icon;
  2394. if (p_index >= 0 || p_index < devices.size()) {
  2395. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  2396. if (theme.is_valid()) {
  2397. if (devices[p_index].simulator) {
  2398. icon = theme->get_icon("IOSSimulator", EditorStringName(EditorIcons));
  2399. } else if (devices[p_index].wifi) {
  2400. icon = theme->get_icon("IOSDeviceWireless", EditorStringName(EditorIcons));
  2401. } else {
  2402. icon = theme->get_icon("IOSDeviceWired", EditorStringName(EditorIcons));
  2403. }
  2404. }
  2405. }
  2406. return icon;
  2407. }
  2408. String EditorExportPlatformIOS::get_option_label(int p_index) const {
  2409. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  2410. MutexLock lock(device_lock);
  2411. return devices[p_index].name;
  2412. }
  2413. String EditorExportPlatformIOS::get_option_tooltip(int p_index) const {
  2414. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  2415. MutexLock lock(device_lock);
  2416. return "UUID: " + devices[p_index].id;
  2417. }
  2418. bool EditorExportPlatformIOS::is_package_name_valid(const String &p_package, String *r_error) const {
  2419. String pname = p_package;
  2420. if (pname.length() == 0) {
  2421. if (r_error) {
  2422. *r_error = TTR("Identifier is missing.");
  2423. }
  2424. return false;
  2425. }
  2426. for (int i = 0; i < pname.length(); i++) {
  2427. char32_t c = pname[i];
  2428. if (!(is_ascii_alphanumeric_char(c) || c == '-' || c == '.')) {
  2429. if (r_error) {
  2430. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  2431. }
  2432. return false;
  2433. }
  2434. }
  2435. return true;
  2436. }
  2437. #ifdef MACOS_ENABLED
  2438. bool EditorExportPlatformIOS::_check_xcode_install() {
  2439. static bool xcode_found = false;
  2440. if (!xcode_found) {
  2441. Vector<String> mdfind_paths;
  2442. List<String> mdfind_args;
  2443. mdfind_args.push_back("kMDItemCFBundleIdentifier=com.apple.dt.Xcode");
  2444. String output;
  2445. Error err = OS::get_singleton()->execute("mdfind", mdfind_args, &output);
  2446. if (err == OK) {
  2447. mdfind_paths = output.split("\n");
  2448. }
  2449. for (const String &found_path : mdfind_paths) {
  2450. xcode_found = !found_path.is_empty() && DirAccess::dir_exists_absolute(found_path.strip_edges());
  2451. if (xcode_found) {
  2452. break;
  2453. }
  2454. }
  2455. }
  2456. return xcode_found;
  2457. }
  2458. void EditorExportPlatformIOS::_check_for_changes_poll_thread(void *ud) {
  2459. EditorExportPlatformIOS *ea = static_cast<EditorExportPlatformIOS *>(ud);
  2460. while (!ea->quit_request.is_set()) {
  2461. // Nothing to do if we already know the plugins have changed.
  2462. if (!ea->plugins_changed.is_set()) {
  2463. MutexLock lock(ea->plugins_lock);
  2464. Vector<PluginConfigIOS> loaded_plugins = get_plugins();
  2465. if (ea->plugins.size() != loaded_plugins.size()) {
  2466. ea->plugins_changed.set();
  2467. } else {
  2468. for (int i = 0; i < ea->plugins.size(); i++) {
  2469. if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) {
  2470. ea->plugins_changed.set();
  2471. break;
  2472. }
  2473. }
  2474. }
  2475. }
  2476. // Check for devices updates.
  2477. Vector<Device> ldevices;
  2478. // Enum real devices (via ios_deploy, pre Xcode 15).
  2479. String idepl = EDITOR_GET("export/ios/ios_deploy");
  2480. if (ea->has_runnable_preset.is_set() && !idepl.is_empty()) {
  2481. String devices;
  2482. List<String> args;
  2483. args.push_back("-c");
  2484. args.push_back("-timeout");
  2485. args.push_back("1");
  2486. args.push_back("-j");
  2487. args.push_back("-u");
  2488. args.push_back("-I");
  2489. int ec = 0;
  2490. Error err = OS::get_singleton()->execute(idepl, args, &devices, &ec, true);
  2491. if (err == OK && ec == 0) {
  2492. Ref<JSON> json;
  2493. json.instantiate();
  2494. devices = "{ \"devices\":[" + devices.replace("}{", "},{") + "]}";
  2495. err = json->parse(devices);
  2496. if (err == OK) {
  2497. Dictionary data = json->get_data();
  2498. Array devices = data["devices"];
  2499. for (int i = 0; i < devices.size(); i++) {
  2500. Dictionary device_event = devices[i];
  2501. if (device_event["Event"] == "DeviceDetected") {
  2502. Dictionary device_info = device_event["Device"];
  2503. Device nd;
  2504. nd.id = device_info["DeviceIdentifier"];
  2505. nd.name = device_info["DeviceName"].operator String() + " (ios_deploy, " + ((device_event["Interface"] == "WIFI") ? "network" : "wired") + ")";
  2506. nd.wifi = device_event["Interface"] == "WIFI";
  2507. nd.use_ios_deploy = true;
  2508. nd.simulator = false;
  2509. ldevices.push_back(nd);
  2510. }
  2511. }
  2512. }
  2513. }
  2514. }
  2515. // Enum simulators.
  2516. if (ea->has_runnable_preset.is_set() && _check_xcode_install() && (FileAccess::exists("/usr/bin/xcrun") || FileAccess::exists("/bin/xcrun"))) {
  2517. {
  2518. String devices;
  2519. List<String> args;
  2520. args.push_back("devicectl");
  2521. args.push_back("list");
  2522. args.push_back("devices");
  2523. args.push_back("-j");
  2524. args.push_back("-");
  2525. args.push_back("-q");
  2526. int ec = 0;
  2527. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  2528. if (err == OK && ec == 0) {
  2529. Ref<JSON> json;
  2530. json.instantiate();
  2531. err = json->parse(devices);
  2532. if (err == OK) {
  2533. const Dictionary &data = json->get_data();
  2534. const Dictionary &result = data["result"];
  2535. const Array &devices = result["devices"];
  2536. for (int i = 0; i < devices.size(); i++) {
  2537. const Dictionary &device_info = devices[i];
  2538. const Dictionary &conn_props = device_info["connectionProperties"];
  2539. const Dictionary &dev_props = device_info["deviceProperties"];
  2540. if (conn_props["pairingState"] == "paired" && dev_props["developerModeStatus"] == "enabled") {
  2541. Device nd;
  2542. nd.id = device_info["identifier"];
  2543. nd.name = dev_props["name"].operator String() + " (devicectl, " + ((conn_props["transportType"] == "localNetwork") ? "network" : "wired") + ")";
  2544. nd.wifi = conn_props["transportType"] == "localNetwork";
  2545. nd.simulator = false;
  2546. ldevices.push_back(nd);
  2547. }
  2548. }
  2549. }
  2550. }
  2551. }
  2552. // Enum simulators.
  2553. if (ea->has_runnable_preset.is_set()) {
  2554. String devices;
  2555. List<String> args;
  2556. args.push_back("simctl");
  2557. args.push_back("list");
  2558. args.push_back("devices");
  2559. args.push_back("-j");
  2560. int ec = 0;
  2561. Error err = OS::get_singleton()->execute("xcrun", args, &devices, &ec, true);
  2562. if (err == OK && ec == 0) {
  2563. Ref<JSON> json;
  2564. json.instantiate();
  2565. err = json->parse(devices);
  2566. if (err == OK) {
  2567. const Dictionary &data = json->get_data();
  2568. const Dictionary &devices = data["devices"];
  2569. for (const Variant *key = devices.next(nullptr); key; key = devices.next(key)) {
  2570. const Array &os_devices = devices[*key];
  2571. for (int i = 0; i < os_devices.size(); i++) {
  2572. const Dictionary &device_info = os_devices[i];
  2573. if (device_info["isAvailable"].operator bool() && device_info["state"] == "Booted") {
  2574. Device nd;
  2575. nd.id = device_info["udid"];
  2576. nd.name = device_info["name"].operator String() + " (simulator)";
  2577. nd.simulator = true;
  2578. ldevices.push_back(nd);
  2579. }
  2580. }
  2581. }
  2582. }
  2583. }
  2584. }
  2585. }
  2586. // Update device list.
  2587. {
  2588. MutexLock lock(ea->device_lock);
  2589. bool different = false;
  2590. if (ea->devices.size() != ldevices.size()) {
  2591. different = true;
  2592. } else {
  2593. for (int i = 0; i < ea->devices.size(); i++) {
  2594. if (ea->devices[i].id != ldevices[i].id) {
  2595. different = true;
  2596. break;
  2597. }
  2598. }
  2599. }
  2600. if (different) {
  2601. ea->devices = ldevices;
  2602. ea->devices_changed.set();
  2603. }
  2604. }
  2605. uint64_t sleep = 200;
  2606. uint64_t wait = 3000000;
  2607. uint64_t time = OS::get_singleton()->get_ticks_usec();
  2608. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  2609. OS::get_singleton()->delay_usec(1000 * sleep);
  2610. if (ea->quit_request.is_set()) {
  2611. break;
  2612. }
  2613. }
  2614. }
  2615. }
  2616. void EditorExportPlatformIOS::_update_preset_status() {
  2617. const int preset_count = EditorExport::get_singleton()->get_export_preset_count();
  2618. bool has_runnable = false;
  2619. for (int i = 0; i < preset_count; i++) {
  2620. const Ref<EditorExportPreset> &preset = EditorExport::get_singleton()->get_export_preset(i);
  2621. if (preset->get_platform() == this && preset->is_runnable()) {
  2622. has_runnable = true;
  2623. break;
  2624. }
  2625. }
  2626. if (has_runnable) {
  2627. has_runnable_preset.set();
  2628. } else {
  2629. has_runnable_preset.clear();
  2630. }
  2631. devices_changed.set();
  2632. }
  2633. #endif
  2634. Error EditorExportPlatformIOS::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  2635. #ifdef MACOS_ENABLED
  2636. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  2637. String can_export_error;
  2638. bool can_export_missing_templates;
  2639. if (!can_export(p_preset, can_export_error, can_export_missing_templates)) {
  2640. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error);
  2641. return ERR_UNCONFIGURED;
  2642. }
  2643. MutexLock lock(device_lock);
  2644. EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device].name), 3);
  2645. String id = "tmpexport." + uitos(OS::get_singleton()->get_unix_time());
  2646. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2647. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + EditorPaths::get_singleton()->get_cache_dir() + "'.");
  2648. filesystem_da->make_dir_recursive(EditorPaths::get_singleton()->get_cache_dir().path_join(id));
  2649. String tmp_export_path = EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.ipa");
  2650. #define CLEANUP_AND_RETURN(m_err) \
  2651. { \
  2652. if (filesystem_da->change_dir(EditorPaths::get_singleton()->get_cache_dir().path_join(id)) == OK) { \
  2653. filesystem_da->erase_contents_recursive(); \
  2654. filesystem_da->change_dir(".."); \
  2655. filesystem_da->remove(id); \
  2656. } \
  2657. return m_err; \
  2658. } \
  2659. ((void)0)
  2660. Device dev = devices[p_device];
  2661. // Export before sending to device.
  2662. Error err = _export_project_helper(p_preset, true, tmp_export_path, p_debug_flags, dev.simulator, true);
  2663. if (err != OK) {
  2664. CLEANUP_AND_RETURN(err);
  2665. }
  2666. Vector<String> cmd_args_list;
  2667. String host = EDITOR_GET("network/debug/remote_host");
  2668. int remote_port = (int)EDITOR_GET("network/debug/remote_port");
  2669. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST) {
  2670. host = "localhost";
  2671. }
  2672. if (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2673. int port = EDITOR_GET("filesystem/file_server/port");
  2674. String passwd = EDITOR_GET("filesystem/file_server/password");
  2675. cmd_args_list.push_back("--remote-fs");
  2676. cmd_args_list.push_back(host + ":" + itos(port));
  2677. if (!passwd.is_empty()) {
  2678. cmd_args_list.push_back("--remote-fs-password");
  2679. cmd_args_list.push_back(passwd);
  2680. }
  2681. }
  2682. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  2683. cmd_args_list.push_back("--remote-debug");
  2684. cmd_args_list.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));
  2685. List<String> breakpoints;
  2686. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  2687. if (breakpoints.size()) {
  2688. cmd_args_list.push_back("--breakpoints");
  2689. String bpoints;
  2690. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  2691. bpoints += E->get().replace(" ", "%20");
  2692. if (E->next()) {
  2693. bpoints += ",";
  2694. }
  2695. }
  2696. cmd_args_list.push_back(bpoints);
  2697. }
  2698. }
  2699. if (p_debug_flags & DEBUG_FLAG_VIEW_COLLISIONS) {
  2700. cmd_args_list.push_back("--debug-collisions");
  2701. }
  2702. if (p_debug_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  2703. cmd_args_list.push_back("--debug-navigation");
  2704. }
  2705. if (dev.simulator) {
  2706. // Deploy and run on simulator.
  2707. if (ep.step("Installing to simulator...", 3)) {
  2708. CLEANUP_AND_RETURN(ERR_SKIP);
  2709. } else {
  2710. List<String> args;
  2711. args.push_back("simctl");
  2712. args.push_back("install");
  2713. args.push_back(dev.id);
  2714. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2715. String log;
  2716. int ec;
  2717. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2718. if (err != OK) {
  2719. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2720. CLEANUP_AND_RETURN(err);
  2721. }
  2722. if (ec != 0) {
  2723. print_line("simctl install:\n" + log);
  2724. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2725. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2726. }
  2727. }
  2728. if (ep.step("Running on simulator...", 4)) {
  2729. CLEANUP_AND_RETURN(ERR_SKIP);
  2730. } else {
  2731. List<String> args;
  2732. args.push_back("simctl");
  2733. args.push_back("launch");
  2734. args.push_back("--terminate-running-process");
  2735. args.push_back(dev.id);
  2736. args.push_back(p_preset->get("application/bundle_identifier"));
  2737. for (const String &E : cmd_args_list) {
  2738. args.push_back(E);
  2739. }
  2740. String log;
  2741. int ec;
  2742. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2743. if (err != OK) {
  2744. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start simctl executable."));
  2745. CLEANUP_AND_RETURN(err);
  2746. }
  2747. if (ec != 0) {
  2748. print_line("simctl launch:\n" + log);
  2749. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2750. }
  2751. }
  2752. } else if (dev.use_ios_deploy) {
  2753. // Deploy and run on real device (via ios-deploy).
  2754. if (ep.step("Installing and running on device...", 4)) {
  2755. CLEANUP_AND_RETURN(ERR_SKIP);
  2756. } else {
  2757. List<String> args;
  2758. args.push_back("-u");
  2759. args.push_back("-I");
  2760. args.push_back("--id");
  2761. args.push_back(dev.id);
  2762. args.push_back("--justlaunch");
  2763. args.push_back("--bundle");
  2764. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2765. String app_args;
  2766. for (const String &E : cmd_args_list) {
  2767. app_args += E + " ";
  2768. }
  2769. if (!app_args.is_empty()) {
  2770. args.push_back("--args");
  2771. args.push_back(app_args);
  2772. }
  2773. String idepl = EDITOR_GET("export/ios/ios_deploy");
  2774. if (idepl.is_empty()) {
  2775. idepl = "ios-deploy";
  2776. }
  2777. String log;
  2778. int ec;
  2779. err = OS::get_singleton()->execute(idepl, args, &log, &ec, true);
  2780. if (err != OK) {
  2781. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start ios-deploy executable."));
  2782. CLEANUP_AND_RETURN(err);
  2783. }
  2784. if (ec != 0) {
  2785. print_line("ios-deploy:\n" + log);
  2786. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation/running failed, see editor log for details."));
  2787. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2788. }
  2789. }
  2790. } else {
  2791. // Deploy and run on real device.
  2792. if (ep.step("Installing to device...", 3)) {
  2793. CLEANUP_AND_RETURN(ERR_SKIP);
  2794. } else {
  2795. List<String> args;
  2796. args.push_back("devicectl");
  2797. args.push_back("device");
  2798. args.push_back("install");
  2799. args.push_back("app");
  2800. args.push_back("-d");
  2801. args.push_back(dev.id);
  2802. args.push_back(EditorPaths::get_singleton()->get_cache_dir().path_join(id).path_join("export.xcarchive/Products/Applications/export.app"));
  2803. String log;
  2804. int ec;
  2805. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2806. if (err != OK) {
  2807. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start device executable."));
  2808. CLEANUP_AND_RETURN(err);
  2809. }
  2810. if (ec != 0) {
  2811. print_line("device install:\n" + log);
  2812. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Installation failed, see editor log for details."));
  2813. CLEANUP_AND_RETURN(ERR_UNCONFIGURED);
  2814. }
  2815. }
  2816. if (ep.step("Running on device...", 4)) {
  2817. CLEANUP_AND_RETURN(ERR_SKIP);
  2818. } else {
  2819. List<String> args;
  2820. args.push_back("devicectl");
  2821. args.push_back("device");
  2822. args.push_back("process");
  2823. args.push_back("launch");
  2824. args.push_back("--terminate-existing");
  2825. args.push_back("-d");
  2826. args.push_back(dev.id);
  2827. args.push_back(p_preset->get("application/bundle_identifier"));
  2828. for (const String &E : cmd_args_list) {
  2829. args.push_back(E);
  2830. }
  2831. String log;
  2832. int ec;
  2833. err = OS::get_singleton()->execute("xcrun", args, &log, &ec, true);
  2834. if (err != OK) {
  2835. add_message(EXPORT_MESSAGE_WARNING, TTR("Run"), TTR("Could not start devicectl executable."));
  2836. CLEANUP_AND_RETURN(err);
  2837. }
  2838. if (ec != 0) {
  2839. print_line("devicectl launch:\n" + log);
  2840. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Running failed, see editor log for details."));
  2841. }
  2842. }
  2843. }
  2844. CLEANUP_AND_RETURN(OK);
  2845. #undef CLEANUP_AND_RETURN
  2846. #else
  2847. return ERR_UNCONFIGURED;
  2848. #endif
  2849. }
  2850. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  2851. if (EditorNode::get_singleton()) {
  2852. #ifdef MODULE_SVG_ENABLED
  2853. Ref<Image> img = memnew(Image);
  2854. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  2855. ImageLoaderSVG::create_image_from_string(img, _ios_logo_svg, EDSCALE, upsample, false);
  2856. logo = ImageTexture::create_from_image(img);
  2857. ImageLoaderSVG::create_image_from_string(img, _ios_run_icon_svg, EDSCALE, upsample, false);
  2858. run_icon = ImageTexture::create_from_image(img);
  2859. #endif
  2860. plugins_changed.set();
  2861. devices_changed.set();
  2862. #ifdef MACOS_ENABLED
  2863. _update_preset_status();
  2864. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  2865. #endif
  2866. }
  2867. }
  2868. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  2869. #ifdef MACOS_ENABLED
  2870. quit_request.set();
  2871. if (check_for_changes_thread.is_started()) {
  2872. check_for_changes_thread.wait_to_finish();
  2873. }
  2874. #endif
  2875. }