export.cpp 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  31. #include "core/io/image_loader.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/io/zip_io.h"
  35. #include "core/os/file_access.h"
  36. #include "core/os/os.h"
  37. #include "core/project_settings.h"
  38. #include "core/safe_refcount.h"
  39. #include "core/version.h"
  40. #include "editor/editor_export.h"
  41. #include "editor/editor_node.h"
  42. #include "editor/editor_settings.h"
  43. #include "main/splash.gen.h"
  44. #include "platform/iphone/logo.gen.h"
  45. #include "platform/iphone/plugin/godot_plugin_config.h"
  46. #include "string.h"
  47. #include <sys/stat.h>
  48. class EditorExportPlatformIOS : public EditorExportPlatform {
  49. GDCLASS(EditorExportPlatformIOS, EditorExportPlatform);
  50. int version_code;
  51. Ref<ImageTexture> logo;
  52. // Plugins
  53. SafeFlag plugins_changed;
  54. Thread check_for_changes_thread;
  55. SafeFlag quit_request;
  56. Mutex plugins_lock;
  57. Vector<PluginConfigIOS> plugins;
  58. typedef Error (*FileHandler)(String p_file, void *p_userdata);
  59. static Error _walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata);
  60. static Error _codesign(String p_file, void *p_userdata);
  61. void _blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot);
  62. struct IOSConfigData {
  63. String pkg_name;
  64. String binary_name;
  65. String plist_content;
  66. String architectures;
  67. String linker_flags;
  68. String cpp_code;
  69. String modules_buildfile;
  70. String modules_fileref;
  71. String modules_buildphase;
  72. String modules_buildgrp;
  73. Vector<String> capabilities;
  74. bool use_swift_runtime;
  75. };
  76. struct ExportArchitecture {
  77. String name;
  78. bool is_default;
  79. ExportArchitecture() :
  80. name(""),
  81. is_default(false) {
  82. }
  83. ExportArchitecture(String p_name, bool p_is_default) {
  84. name = p_name;
  85. is_default = p_is_default;
  86. }
  87. };
  88. struct IOSExportAsset {
  89. String exported_path;
  90. bool is_framework; // framework is anything linked to the binary, otherwise it's a resource
  91. bool should_embed;
  92. };
  93. String _get_additional_plist_content();
  94. String _get_linker_flags();
  95. String _get_cpp_code();
  96. void _fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug);
  97. Error _export_loading_screen_images(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
  98. Error _export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
  99. Error _export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir);
  100. Vector<ExportArchitecture> _get_supported_architectures();
  101. Vector<String> _get_preset_architectures(const Ref<EditorExportPreset> &p_preset);
  102. void _add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets);
  103. Error _copy_asset(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);
  104. Error _export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets);
  105. Error _export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets);
  106. Error _export_ios_plugins(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &dest_dir, Vector<IOSExportAsset> &r_exported_assets, bool p_debug);
  107. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  108. String pname = p_package;
  109. if (pname.length() == 0) {
  110. if (r_error) {
  111. *r_error = TTR("Identifier is missing.");
  112. }
  113. return false;
  114. }
  115. for (int i = 0; i < pname.length(); i++) {
  116. CharType c = pname[i];
  117. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  118. if (r_error) {
  119. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  120. }
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. static void _check_for_changes_poll_thread(void *ud) {
  127. EditorExportPlatformIOS *ea = (EditorExportPlatformIOS *)ud;
  128. while (!ea->quit_request.is_set()) {
  129. // Nothing to do if we already know the plugins have changed.
  130. if (!ea->plugins_changed.is_set()) {
  131. ea->plugins_lock.lock();
  132. Vector<PluginConfigIOS> loaded_plugins = get_plugins();
  133. if (ea->plugins.size() != loaded_plugins.size()) {
  134. ea->plugins_changed.set();
  135. } else {
  136. for (int i = 0; i < ea->plugins.size(); i++) {
  137. if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) {
  138. ea->plugins_changed.set();
  139. break;
  140. }
  141. }
  142. }
  143. ea->plugins_lock.unlock();
  144. }
  145. uint64_t wait = 3000000;
  146. uint64_t time = OS::get_singleton()->get_ticks_usec();
  147. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  148. OS::get_singleton()->delay_usec(300000);
  149. if (ea->quit_request.is_set()) {
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. protected:
  156. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  157. virtual void get_export_options(List<ExportOption> *r_options);
  158. public:
  159. virtual String get_name() const { return "iOS"; }
  160. virtual String get_os_name() const { return "iOS"; }
  161. virtual Ref<Texture> get_logo() const { return logo; }
  162. virtual bool should_update_export_options() {
  163. bool export_options_changed = plugins_changed.is_set();
  164. if (export_options_changed) {
  165. // don't clear unless we're reporting true, to avoid race
  166. plugins_changed.clear();
  167. }
  168. return export_options_changed;
  169. }
  170. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  171. List<String> list;
  172. list.push_back("ipa");
  173. return list;
  174. }
  175. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  176. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  177. virtual void get_platform_features(List<String> *r_features) {
  178. r_features->push_back("mobile");
  179. r_features->push_back("iOS");
  180. }
  181. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  182. }
  183. EditorExportPlatformIOS();
  184. ~EditorExportPlatformIOS();
  185. /// List the gdip files in the directory specified by the p_path parameter.
  186. static Vector<String> list_plugin_config_files(const String &p_path, bool p_check_directories) {
  187. Vector<String> dir_files;
  188. DirAccessRef da = DirAccess::open(p_path);
  189. if (da) {
  190. da->list_dir_begin();
  191. while (true) {
  192. String file = da->get_next();
  193. if (file.empty()) {
  194. break;
  195. }
  196. if (file == "." || file == "..") {
  197. continue;
  198. }
  199. if (da->current_is_hidden()) {
  200. continue;
  201. }
  202. if (da->current_is_dir()) {
  203. if (p_check_directories) {
  204. Vector<String> directory_files = list_plugin_config_files(p_path.plus_file(file), false);
  205. for (int i = 0; i < directory_files.size(); ++i) {
  206. dir_files.push_back(file.plus_file(directory_files[i]));
  207. }
  208. }
  209. continue;
  210. }
  211. if (file.ends_with(PluginConfigIOS::PLUGIN_CONFIG_EXT)) {
  212. dir_files.push_back(file);
  213. }
  214. }
  215. da->list_dir_end();
  216. }
  217. return dir_files;
  218. }
  219. static Vector<PluginConfigIOS> get_plugins() {
  220. Vector<PluginConfigIOS> loaded_plugins;
  221. String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().plus_file("ios/plugins");
  222. if (DirAccess::exists(plugins_dir)) {
  223. Vector<String> plugins_filenames = list_plugin_config_files(plugins_dir, true);
  224. if (!plugins_filenames.empty()) {
  225. Ref<ConfigFile> config_file = memnew(ConfigFile);
  226. for (int i = 0; i < plugins_filenames.size(); i++) {
  227. PluginConfigIOS config = load_plugin_config(config_file, plugins_dir.plus_file(plugins_filenames[i]));
  228. if (config.valid_config) {
  229. loaded_plugins.push_back(config);
  230. } else {
  231. print_error("Invalid plugin config file " + plugins_filenames[i]);
  232. }
  233. }
  234. }
  235. }
  236. return loaded_plugins;
  237. }
  238. static Vector<PluginConfigIOS> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) {
  239. Vector<PluginConfigIOS> enabled_plugins;
  240. Vector<PluginConfigIOS> all_plugins = get_plugins();
  241. for (int i = 0; i < all_plugins.size(); i++) {
  242. PluginConfigIOS plugin = all_plugins[i];
  243. bool enabled = p_presets->get("plugins/" + plugin.name);
  244. if (enabled) {
  245. enabled_plugins.push_back(plugin);
  246. }
  247. }
  248. return enabled_plugins;
  249. }
  250. };
  251. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  252. String driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name");
  253. r_features->push_back("pvrtc");
  254. if (driver == "GLES3") {
  255. r_features->push_back("etc2");
  256. }
  257. Vector<String> architectures = _get_preset_architectures(p_preset);
  258. for (int i = 0; i < architectures.size(); ++i) {
  259. r_features->push_back(architectures[i]);
  260. }
  261. }
  262. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() {
  263. Vector<ExportArchitecture> archs;
  264. archs.push_back(ExportArchitecture("armv7", false)); // Disabled by default, not included in official templates.
  265. archs.push_back(ExportArchitecture("arm64", true));
  266. return archs;
  267. }
  268. struct LoadingScreenInfo {
  269. const char *preset_key;
  270. const char *export_name;
  271. int width;
  272. int height;
  273. bool rotate;
  274. };
  275. static const LoadingScreenInfo loading_screen_infos[] = {
  276. { PNAME("landscape_launch_screens/iphone_2436x1125"), "Default-Landscape-X.png", 2436, 1125, false },
  277. { PNAME("landscape_launch_screens/iphone_2208x1242"), "[email protected]", 2208, 1242, false },
  278. { PNAME("landscape_launch_screens/ipad_1024x768"), "Default-Landscape.png", 1024, 768, false },
  279. { PNAME("landscape_launch_screens/ipad_2048x1536"), "[email protected]", 2048, 1536, false },
  280. { PNAME("portrait_launch_screens/iphone_640x960"), "[email protected]", 640, 960, true },
  281. { PNAME("portrait_launch_screens/iphone_640x1136"), "[email protected]", 640, 1136, true },
  282. { PNAME("portrait_launch_screens/iphone_750x1334"), "[email protected]", 750, 1334, true },
  283. { PNAME("portrait_launch_screens/iphone_1125x2436"), "Default-Portrait-X.png", 1125, 2436, true },
  284. { PNAME("portrait_launch_screens/ipad_768x1024"), "Default-Portrait.png", 768, 1024, true },
  285. { PNAME("portrait_launch_screens/ipad_1536x2048"), "[email protected]", 1536, 2048, true },
  286. { PNAME("portrait_launch_screens/iphone_1242x2208"), "[email protected]", 1242, 2208, true }
  287. };
  288. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) {
  289. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  290. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  291. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  292. for (int i = 0; i < architectures.size(); ++i) {
  293. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), architectures[i].name)), architectures[i].is_default));
  294. }
  295. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), ""));
  296. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug"), ""));
  297. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), ""));
  298. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  299. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release"), ""));
  300. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  301. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  302. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  303. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  304. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  305. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  306. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  307. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  308. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  309. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  310. Vector<PluginConfigIOS> found_plugins = get_plugins();
  311. for (int i = 0; i < found_plugins.size(); i++) {
  312. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), found_plugins[i].name)), false));
  313. }
  314. Set<String> plist_keys;
  315. for (int i = 0; i < found_plugins.size(); i++) {
  316. // Editable plugin plist values
  317. PluginConfigIOS plugin = found_plugins[i];
  318. const String *K = nullptr;
  319. while ((K = plugin.plist.next(K))) {
  320. String key = *K;
  321. PluginConfigIOS::PlistItem item = plugin.plist[key];
  322. switch (item.type) {
  323. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  324. String preset_name = "plugins_plist/" + key;
  325. if (!plist_keys.has(preset_name)) {
  326. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, preset_name), item.value));
  327. plist_keys.insert(preset_name);
  328. }
  329. } break;
  330. default:
  331. continue;
  332. }
  333. }
  334. }
  335. plugins_changed.clear();
  336. plugins = found_plugins;
  337. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  338. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  339. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  340. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  341. 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"), ""));
  342. 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"), ""));
  343. 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"), ""));
  344. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/iphone_120x120", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone/iPod Touch with Retina display
  345. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/iphone_180x180", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone with Retina HD display
  346. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/ipad_76x76", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad
  347. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/ipad_152x152", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad with Retina display
  348. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/ipad_167x167", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad Pro
  349. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/app_store_1024x1024", PROPERTY_HINT_FILE, "*.png"), "")); // App Store
  350. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/spotlight_40x40", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight
  351. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "icons/spotlight_80x80", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight on devices with Retina display
  352. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_launch_screen_storyboard"), false));
  353. 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));
  354. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE, "*.png"), ""));
  355. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE, "*.png"), ""));
  356. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  357. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  358. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  359. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, loading_screen_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png"), ""));
  360. }
  361. }
  362. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  363. static const String export_method_string[] = {
  364. "app-store",
  365. "development",
  366. "ad-hoc",
  367. "enterprise"
  368. };
  369. static const String storyboard_image_scale_mode[] = {
  370. "center",
  371. "scaleAspectFit",
  372. "scaleAspectFill",
  373. "scaleToFill"
  374. };
  375. String dbg_sign_id = p_preset->get("application/code_sign_identity_debug").operator String().empty() ? "iPhone Developer" : p_preset->get("application/code_sign_identity_debug");
  376. String rel_sign_id = p_preset->get("application/code_sign_identity_release").operator String().empty() ? "iPhone Distribution" : p_preset->get("application/code_sign_identity_release");
  377. bool dbg_manual = !p_preset->get("application/provisioning_profile_uuid_debug").operator String().empty() || (dbg_sign_id != "iPhone Developer");
  378. bool rel_manual = !p_preset->get("application/provisioning_profile_uuid_release").operator String().empty() || (rel_sign_id != "iPhone Distribution");
  379. String str;
  380. String strnew;
  381. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  382. Vector<String> lines = str.split("\n");
  383. for (int i = 0; i < lines.size(); i++) {
  384. if (lines[i].find("$binary") != -1) {
  385. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  386. } else if (lines[i].find("$modules_buildfile") != -1) {
  387. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  388. } else if (lines[i].find("$modules_fileref") != -1) {
  389. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  390. } else if (lines[i].find("$modules_buildphase") != -1) {
  391. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  392. } else if (lines[i].find("$modules_buildgrp") != -1) {
  393. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  394. } else if (lines[i].find("$name") != -1) {
  395. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  396. } else if (lines[i].find("$info") != -1) {
  397. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  398. } else if (lines[i].find("$identifier") != -1) {
  399. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  400. } else if (lines[i].find("$short_version") != -1) {
  401. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  402. } else if (lines[i].find("$version") != -1) {
  403. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  404. } else if (lines[i].find("$signature") != -1) {
  405. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  406. } else if (lines[i].find("$copyright") != -1) {
  407. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  408. } else if (lines[i].find("$team_id") != -1) {
  409. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  410. } else if (lines[i].find("$default_build_config") != -1) {
  411. strnew += lines[i].replace("$default_build_config", p_debug ? "Debug" : "Release") + "\n";
  412. } else if (lines[i].find("$export_method") != -1) {
  413. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  414. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  415. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  416. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get("application/provisioning_profile_uuid_release")) + "\n";
  417. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  418. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get("application/provisioning_profile_uuid_debug")) + "\n";
  419. } else if (lines[i].find("$code_sign_style_debug") != -1) {
  420. if (dbg_manual) {
  421. strnew += lines[i].replace("$code_sign_style_debug", "Manual") + "\n";
  422. } else {
  423. strnew += lines[i].replace("$code_sign_style_debug", "Automatic") + "\n";
  424. }
  425. } else if (lines[i].find("$code_sign_style_release") != -1) {
  426. if (rel_manual) {
  427. strnew += lines[i].replace("$code_sign_style_release", "Manual") + "\n";
  428. } else {
  429. strnew += lines[i].replace("$code_sign_style_release", "Automatic") + "\n";
  430. }
  431. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  432. String uuid = p_debug ? p_preset->get("application/provisioning_profile_uuid_debug") : p_preset->get("application/provisioning_profile_uuid_release");
  433. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  434. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  435. strnew += lines[i].replace("$code_sign_identity_debug", dbg_sign_id) + "\n";
  436. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  437. strnew += lines[i].replace("$code_sign_identity_release", rel_sign_id) + "\n";
  438. } else if (lines[i].find("$additional_plist_content") != -1) {
  439. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  440. } else if (lines[i].find("$godot_archs") != -1) {
  441. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  442. } else if (lines[i].find("$linker_flags") != -1) {
  443. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  444. } else if (lines[i].find("$targeted_device_family") != -1) {
  445. String xcode_value;
  446. switch ((int)p_preset->get("application/targeted_device_family")) {
  447. case 0: // iPhone
  448. xcode_value = "1";
  449. break;
  450. case 1: // iPad
  451. xcode_value = "2";
  452. break;
  453. case 2: // iPhone & iPad
  454. xcode_value = "1,2";
  455. break;
  456. }
  457. strnew += lines[i].replace("$targeted_device_family", xcode_value) + "\n";
  458. } else if (lines[i].find("$cpp_code") != -1) {
  459. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  460. } else if (lines[i].find("$docs_in_place") != -1) {
  461. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  462. } else if (lines[i].find("$docs_sharing") != -1) {
  463. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  464. } else if (lines[i].find("$entitlements_push_notifications") != -1) {
  465. bool is_on = p_preset->get("capabilities/push_notifications");
  466. strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n";
  467. } else if (lines[i].find("$required_device_capabilities") != -1) {
  468. String capabilities;
  469. // I've removed armv7 as we can run on 64bit only devices
  470. // Note that capabilities listed here are requirements for the app to be installed.
  471. // They don't enable anything.
  472. Vector<String> capabilities_list = p_config.capabilities;
  473. if ((bool)p_preset->get("capabilities/access_wifi") && capabilities_list.find("wifi") != -1) {
  474. capabilities_list.push_back("wifi");
  475. }
  476. for (int idx = 0; idx < capabilities_list.size(); idx++) {
  477. capabilities += "<string>" + capabilities_list[idx] + "</string>\n";
  478. }
  479. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  480. } else if (lines[i].find("$interface_orientations") != -1) {
  481. String orientations;
  482. const OS::ScreenOrientation screen_orientation =
  483. OS::get_singleton()->get_screen_orientation_from_string(GLOBAL_GET("display/window/handheld/orientation"));
  484. switch (screen_orientation) {
  485. case OS::SCREEN_LANDSCAPE:
  486. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  487. break;
  488. case OS::SCREEN_PORTRAIT:
  489. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  490. break;
  491. case OS::SCREEN_REVERSE_LANDSCAPE:
  492. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  493. break;
  494. case OS::SCREEN_REVERSE_PORTRAIT:
  495. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  496. break;
  497. case OS::SCREEN_SENSOR_LANDSCAPE:
  498. // Allow both landscape orientations depending on sensor direction.
  499. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  500. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  501. break;
  502. case OS::SCREEN_SENSOR_PORTRAIT:
  503. // Allow both portrait orientations depending on sensor direction.
  504. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  505. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  506. break;
  507. case OS::SCREEN_SENSOR:
  508. // Allow all screen orientations depending on sensor direction.
  509. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  510. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  511. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  512. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  513. break;
  514. }
  515. strnew += lines[i].replace("$interface_orientations", orientations);
  516. } else if (lines[i].find("$camera_usage_description") != -1) {
  517. String description = p_preset->get("privacy/camera_usage_description");
  518. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  519. } else if (lines[i].find("$microphone_usage_description") != -1) {
  520. String description = p_preset->get("privacy/microphone_usage_description");
  521. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  522. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  523. String description = p_preset->get("privacy/photolibrary_usage_description");
  524. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  525. } else if (lines[i].find("$plist_launch_screen_name") != -1) {
  526. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  527. String value = is_on ? "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>" : "";
  528. strnew += lines[i].replace("$plist_launch_screen_name", value) + "\n";
  529. } else if (lines[i].find("$pbx_launch_screen_file_reference") != -1) {
  530. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  531. String value = is_on ? "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };" : "";
  532. strnew += lines[i].replace("$pbx_launch_screen_file_reference", value) + "\n";
  533. } else if (lines[i].find("$pbx_launch_screen_copy_files") != -1) {
  534. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  535. String value = is_on ? "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */," : "";
  536. strnew += lines[i].replace("$pbx_launch_screen_copy_files", value) + "\n";
  537. } else if (lines[i].find("$pbx_launch_screen_build_phase") != -1) {
  538. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  539. String value = is_on ? "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */," : "";
  540. strnew += lines[i].replace("$pbx_launch_screen_build_phase", value) + "\n";
  541. } else if (lines[i].find("$pbx_launch_screen_build_reference") != -1) {
  542. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  543. String value = is_on ? "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };" : "";
  544. strnew += lines[i].replace("$pbx_launch_screen_build_reference", value) + "\n";
  545. } else if (lines[i].find("$pbx_launch_image_usage_setting") != -1) {
  546. bool is_on = p_preset->get("storyboard/use_launch_screen_storyboard");
  547. String value = is_on ? "" : "ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;";
  548. strnew += lines[i].replace("$pbx_launch_image_usage_setting", value) + "\n";
  549. } else if (lines[i].find("$launch_screen_image_mode") != -1) {
  550. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  551. String value;
  552. switch (image_scale_mode) {
  553. case 0: {
  554. String logo_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  555. bool is_on = ProjectSettings::get_singleton()->get("application/boot_splash/fullsize");
  556. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  557. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  558. } break;
  559. default: {
  560. value = storyboard_image_scale_mode[image_scale_mode - 1];
  561. }
  562. }
  563. strnew += lines[i].replace("$launch_screen_image_mode", value) + "\n";
  564. } else if (lines[i].find("$launch_screen_background_color") != -1) {
  565. bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
  566. Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : ProjectSettings::get_singleton()->get("application/boot_splash/bg_color");
  567. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  568. Dictionary value_dictionary;
  569. value_dictionary["red"] = color.r;
  570. value_dictionary["green"] = color.g;
  571. value_dictionary["blue"] = color.b;
  572. value_dictionary["alpha"] = color.a;
  573. String value = value_format.format(value_dictionary, "$_");
  574. strnew += lines[i].replace("$launch_screen_background_color", value) + "\n";
  575. } else if (lines[i].find("$pbx_locale_file_reference") != -1) {
  576. String locale_files;
  577. Vector<String> translations = ProjectSettings::get_singleton()->get("locale/translations");
  578. if (translations.size() > 0) {
  579. for (int j = 0; j < translations.size(); j++) {
  580. Ref<Translation> tr = ResourceLoader::load(translations[j]);
  581. if (tr.is_valid()) {
  582. String lang = tr->get_locale();
  583. locale_files += "D0BCFE4518AEBDA2004A" + itos(j).pad_zeros(4) + " /* " + lang + " */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = " + lang + "; path = " + lang + ".lproj/InfoPlist.strings; sourceTree = \"<group>\"; };";
  584. }
  585. }
  586. }
  587. strnew += lines[i].replace("$pbx_locale_file_reference", locale_files);
  588. } else if (lines[i].find("$pbx_locale_build_reference") != -1) {
  589. String locale_files;
  590. Vector<String> translations = ProjectSettings::get_singleton()->get("locale/translations");
  591. if (translations.size() > 0) {
  592. for (int j = 0; j < translations.size(); j++) {
  593. Ref<Translation> tr = ResourceLoader::load(translations[j]);
  594. if (tr.is_valid()) {
  595. String lang = tr->get_locale();
  596. locale_files += "D0BCFE4518AEBDA2004A" + itos(j).pad_zeros(4) + " /* " + lang + " */,";
  597. }
  598. }
  599. }
  600. strnew += lines[i].replace("$pbx_locale_build_reference", locale_files);
  601. } else if (lines[i].find("$swift_runtime_migration") != -1) {
  602. String value = !p_config.use_swift_runtime ? "" : "LastSwiftMigration = 1250;";
  603. strnew += lines[i].replace("$swift_runtime_migration", value) + "\n";
  604. } else if (lines[i].find("$swift_runtime_build_settings") != -1) {
  605. String value = !p_config.use_swift_runtime ? "" : R"(
  606. CLANG_ENABLE_MODULES = YES;
  607. SWIFT_OBJC_BRIDGING_HEADER = "$binary/dummy.h";
  608. SWIFT_VERSION = 5.0;
  609. )";
  610. value = value.replace("$binary", p_config.binary_name);
  611. strnew += lines[i].replace("$swift_runtime_build_settings", value) + "\n";
  612. } else if (lines[i].find("$swift_runtime_fileref") != -1) {
  613. String value = !p_config.use_swift_runtime ? "" : R"(
  614. 90B4C2AA2680BC560039117A /* dummy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dummy.h"; sourceTree = "<group>"; };
  615. 90B4C2B52680C7E90039117A /* dummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "dummy.swift"; sourceTree = "<group>"; };
  616. )";
  617. strnew += lines[i].replace("$swift_runtime_fileref", value) + "\n";
  618. } else if (lines[i].find("$swift_runtime_binary_files") != -1) {
  619. String value = !p_config.use_swift_runtime ? "" : R"(
  620. 90B4C2AA2680BC560039117A /* dummy.h */,
  621. 90B4C2B52680C7E90039117A /* dummy.swift */,
  622. )";
  623. strnew += lines[i].replace("$swift_runtime_binary_files", value) + "\n";
  624. } else if (lines[i].find("$swift_runtime_buildfile") != -1) {
  625. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90B4C2B52680C7E90039117A /* dummy.swift */; };";
  626. strnew += lines[i].replace("$swift_runtime_buildfile", value) + "\n";
  627. } else if (lines[i].find("$swift_runtime_build_phase") != -1) {
  628. String value = !p_config.use_swift_runtime ? "" : "90B4C2B62680C7E90039117A /* dummy.swift */,";
  629. strnew += lines[i].replace("$swift_runtime_build_phase", value) + "\n";
  630. } else {
  631. strnew += lines[i] + "\n";
  632. }
  633. }
  634. // !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...
  635. // should apply the same fix in our OSX export.
  636. CharString cs = strnew.utf8();
  637. pfile.resize(cs.size() - 1);
  638. for (int i = 0; i < cs.size() - 1; i++) {
  639. pfile.write[i] = cs[i];
  640. }
  641. }
  642. String EditorExportPlatformIOS::_get_additional_plist_content() {
  643. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  644. String result;
  645. for (int i = 0; i < export_plugins.size(); ++i) {
  646. result += export_plugins[i]->get_ios_plist_content();
  647. }
  648. return result;
  649. }
  650. String EditorExportPlatformIOS::_get_linker_flags() {
  651. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  652. String result;
  653. for (int i = 0; i < export_plugins.size(); ++i) {
  654. String flags = export_plugins[i]->get_ios_linker_flags();
  655. if (flags.length() == 0) {
  656. continue;
  657. }
  658. if (result.length() > 0) {
  659. result += ' ';
  660. }
  661. result += flags;
  662. }
  663. // the flags will be enclosed in quotes, so need to escape them
  664. return result.replace("\"", "\\\"");
  665. }
  666. String EditorExportPlatformIOS::_get_cpp_code() {
  667. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  668. String result;
  669. for (int i = 0; i < export_plugins.size(); ++i) {
  670. result += export_plugins[i]->get_ios_cpp_code();
  671. }
  672. return result;
  673. }
  674. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  675. ERR_FAIL_COND(p_dst.is_null());
  676. ERR_FAIL_COND(p_src.is_null());
  677. p_dst->lock();
  678. p_src->lock();
  679. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  680. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  681. int x_pos = (p_dst->get_width() - sw) / 2;
  682. int y_pos = (p_dst->get_height() - sh) / 2;
  683. int xs = (x_pos >= 0) ? 0 : -x_pos;
  684. int ys = (y_pos >= 0) ? 0 : -y_pos;
  685. if (sw + x_pos > p_dst->get_width()) {
  686. sw = p_dst->get_width() - x_pos;
  687. }
  688. if (sh + y_pos > p_dst->get_height()) {
  689. sh = p_dst->get_height() - y_pos;
  690. }
  691. for (int y = ys; y < sh; y++) {
  692. for (int x = xs; x < sw; x++) {
  693. Color sc = p_rot ? p_src->get_pixel(p_src->get_width() - y - 1, x) : p_src->get_pixel(x, y);
  694. Color dc = p_dst->get_pixel(x_pos + x, y_pos + y);
  695. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  696. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  697. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  698. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  699. p_dst->set_pixel(x_pos + x, y_pos + y, dc);
  700. }
  701. }
  702. p_dst->unlock();
  703. p_src->unlock();
  704. }
  705. struct IconInfo {
  706. const char *preset_key;
  707. const char *idiom;
  708. const char *export_name;
  709. const char *actual_size_side;
  710. const char *scale;
  711. const char *unscaled_size;
  712. };
  713. static const IconInfo icon_infos[] = {
  714. // Home screen on iPhone
  715. { "icons/iphone_120x120", "iphone", "Icon-120.png", "120", "2x", "60x60" },
  716. { "icons/iphone_120x120", "iphone", "Icon-120.png", "120", "3x", "40x40" },
  717. { "icons/iphone_180x180", "iphone", "Icon-180.png", "180", "3x", "60x60" },
  718. // Home screen on iPad
  719. { "icons/ipad_76x76", "ipad", "Icon-76.png", "76", "1x", "76x76" },
  720. { "icons/ipad_152x152", "ipad", "Icon-152.png", "152", "2x", "76x76" },
  721. { "icons/ipad_167x167", "ipad", "Icon-167.png", "167", "2x", "83.5x83.5" },
  722. // App Store
  723. { "icons/app_store_1024x1024", "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024" },
  724. // Spotlight
  725. { "icons/spotlight_40x40", "ipad", "Icon-40.png", "40", "1x", "40x40" },
  726. { "icons/spotlight_80x80", "iphone", "Icon-80.png", "80", "2x", "40x40" },
  727. { "icons/spotlight_80x80", "ipad", "Icon-80.png", "80", "2x", "40x40" }
  728. };
  729. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  730. String json_description = "{\"images\":[";
  731. String sizes;
  732. DirAccess *da = DirAccess::open(p_iconset_dir);
  733. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_iconset_dir + "'.");
  734. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  735. IconInfo info = icon_infos[i];
  736. int side_size = String(info.actual_size_side).to_int();
  737. String icon_path = p_preset->get(info.preset_key);
  738. if (icon_path.length() == 0) {
  739. // Resize main app icon
  740. icon_path = ProjectSettings::get_singleton()->get("application/config/icon");
  741. Ref<Image> img = memnew(Image);
  742. Error err = ImageLoader::load_image(icon_path, img);
  743. if (err != OK) {
  744. ERR_PRINT("Invalid icon (" + String(info.preset_key) + "): '" + icon_path + "'.");
  745. return ERR_UNCONFIGURED;
  746. }
  747. img->resize(side_size, side_size);
  748. err = img->save_png(p_iconset_dir + info.export_name);
  749. if (err) {
  750. String err_str = String("Failed to export icon(" + String(info.preset_key) + "): '" + icon_path + "'.");
  751. ERR_PRINT(err_str.utf8().get_data());
  752. return err;
  753. }
  754. } else {
  755. // Load custom icon and resize if required
  756. Ref<Image> img = memnew(Image);
  757. Error err = ImageLoader::load_image(icon_path, img);
  758. if (err != OK) {
  759. ERR_PRINT("Invalid icon (" + String(info.preset_key) + "): '" + icon_path + "'.");
  760. return ERR_UNCONFIGURED;
  761. }
  762. if (img->get_width() != side_size || img->get_height() != side_size) {
  763. WARN_PRINT("Icon (" + String(info.preset_key) + "): '" + icon_path + "' has incorrect size (" + String::num_int64(img->get_width()) + "x" + String::num_int64(img->get_height()) + ") and was automatically resized to " + String::num_int64(side_size) + "x" + String::num_int64(side_size) + ".");
  764. img->resize(side_size, side_size);
  765. err = img->save_png(p_iconset_dir + info.export_name);
  766. } else {
  767. err = da->copy(icon_path, p_iconset_dir + info.export_name);
  768. }
  769. if (err) {
  770. memdelete(da);
  771. String err_str = String("Failed to export icon(" + String(info.preset_key) + "): '" + icon_path + "'.");
  772. ERR_PRINT(err_str.utf8().get_data());
  773. return err;
  774. }
  775. }
  776. sizes += String(info.actual_size_side) + "\n";
  777. if (i > 0) {
  778. json_description += ",";
  779. }
  780. json_description += String("{");
  781. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  782. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  783. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  784. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  785. json_description += String("}");
  786. }
  787. json_description += "]}";
  788. memdelete(da);
  789. FileAccess *json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  790. ERR_FAIL_COND_V(!json_file, ERR_CANT_CREATE);
  791. CharString json_utf8 = json_description.utf8();
  792. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  793. memdelete(json_file);
  794. FileAccess *sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  795. ERR_FAIL_COND_V(!sizes_file, ERR_CANT_CREATE);
  796. CharString sizes_utf8 = sizes.utf8();
  797. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  798. memdelete(sizes_file);
  799. return OK;
  800. }
  801. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  802. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  803. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  804. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  805. Ref<Image> image;
  806. String image_path = p_dest_dir.plus_file("[email protected]");
  807. image.instance();
  808. Error err = image->load(custom_launch_image_2x);
  809. if (err) {
  810. image.unref();
  811. return err;
  812. }
  813. if (image->save_png(image_path) != OK) {
  814. return ERR_FILE_CANT_WRITE;
  815. }
  816. image.unref();
  817. image_path = p_dest_dir.plus_file("[email protected]");
  818. image.instance();
  819. err = image->load(custom_launch_image_3x);
  820. if (err) {
  821. image.unref();
  822. return err;
  823. }
  824. if (image->save_png(image_path) != OK) {
  825. return ERR_FILE_CANT_WRITE;
  826. }
  827. } else {
  828. Ref<Image> splash;
  829. const String splash_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  830. if (!splash_path.empty()) {
  831. splash.instance();
  832. const Error err = splash->load(splash_path);
  833. if (err) {
  834. splash.unref();
  835. }
  836. }
  837. if (splash.is_null()) {
  838. splash = Ref<Image>(memnew(Image(boot_splash_png)));
  839. }
  840. // Using same image for both @2x and @3x
  841. // because Godot's own boot logo uses single image for all resolutions.
  842. // Also not using @1x image, because devices using this image variant
  843. // are not supported by iOS 9, which is minimal target.
  844. const String splash_png_path_2x = p_dest_dir.plus_file("[email protected]");
  845. const String splash_png_path_3x = p_dest_dir.plus_file("[email protected]");
  846. if (splash->save_png(splash_png_path_2x) != OK) {
  847. return ERR_FILE_CANT_WRITE;
  848. }
  849. if (splash->save_png(splash_png_path_3x) != OK) {
  850. return ERR_FILE_CANT_WRITE;
  851. }
  852. }
  853. return OK;
  854. }
  855. Error EditorExportPlatformIOS::_export_loading_screen_images(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  856. DirAccess *da = DirAccess::open(p_dest_dir);
  857. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_dest_dir + "'.");
  858. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  859. LoadingScreenInfo info = loading_screen_infos[i];
  860. String loading_screen_file = p_preset->get(info.preset_key);
  861. Color boot_bg_color = ProjectSettings::get_singleton()->get("application/boot_splash/bg_color");
  862. String boot_logo_path = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  863. bool boot_logo_scale = ProjectSettings::get_singleton()->get("application/boot_splash/fullsize");
  864. if (loading_screen_file.size() > 0) {
  865. // Load custom loading screens, and resize if required.
  866. Ref<Image> img = memnew(Image);
  867. Error err = ImageLoader::load_image(loading_screen_file, img);
  868. if (err != OK) {
  869. ERR_PRINT("Invalid loading screen (" + String(info.preset_key) + "): '" + loading_screen_file + "'.");
  870. return ERR_UNCONFIGURED;
  871. }
  872. if (img->get_width() != info.width || img->get_height() != info.height) {
  873. WARN_PRINT("Loading screen (" + String(info.preset_key) + "): '" + loading_screen_file + "' has incorrect size (" + String::num_int64(img->get_width()) + "x" + String::num_int64(img->get_height()) + ") and was automatically resized to " + String::num_int64(info.width) + "x" + String::num_int64(info.height) + ".");
  874. float aspect_ratio = (float)img->get_width() / (float)img->get_height();
  875. if (boot_logo_scale) {
  876. if (info.height * aspect_ratio <= info.width) {
  877. img->resize(info.height * aspect_ratio, info.height);
  878. } else {
  879. img->resize(info.width, info.width / aspect_ratio);
  880. }
  881. }
  882. Ref<Image> new_img = memnew(Image);
  883. new_img->create(info.width, info.height, false, Image::FORMAT_RGBA8);
  884. new_img->fill(boot_bg_color);
  885. _blend_and_rotate(new_img, img, false);
  886. err = new_img->save_png(p_dest_dir + info.export_name);
  887. } else {
  888. err = da->copy(loading_screen_file, p_dest_dir + info.export_name);
  889. }
  890. if (err) {
  891. memdelete(da);
  892. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from path '" + loading_screen_file + "'.";
  893. ERR_PRINT(err_str.utf8().get_data());
  894. return err;
  895. }
  896. } else {
  897. // Generate loading screen from the splash screen
  898. Ref<Image> img = memnew(Image);
  899. img->create(info.width, info.height, false, Image::FORMAT_RGBA8);
  900. img->fill(boot_bg_color);
  901. Ref<Image> img_bs;
  902. if (boot_logo_path.length() > 0) {
  903. img_bs = Ref<Image>(memnew(Image));
  904. ImageLoader::load_image(boot_logo_path, img_bs);
  905. }
  906. if (!img_bs.is_valid()) {
  907. img_bs = Ref<Image>(memnew(Image(boot_splash_png)));
  908. }
  909. if (img_bs.is_valid()) {
  910. float aspect_ratio = (float)img_bs->get_width() / (float)img_bs->get_height();
  911. if (info.rotate) {
  912. if (boot_logo_scale) {
  913. if (info.width * aspect_ratio <= info.height) {
  914. img_bs->resize(info.width * aspect_ratio, info.width);
  915. } else {
  916. img_bs->resize(info.height, info.height / aspect_ratio);
  917. }
  918. }
  919. } else {
  920. if (boot_logo_scale) {
  921. if (info.height * aspect_ratio <= info.width) {
  922. img_bs->resize(info.height * aspect_ratio, info.height);
  923. } else {
  924. img_bs->resize(info.width, info.width / aspect_ratio);
  925. }
  926. }
  927. }
  928. _blend_and_rotate(img, img_bs, info.rotate);
  929. }
  930. Error err = img->save_png(p_dest_dir + info.export_name);
  931. if (err) {
  932. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from splash screen.";
  933. WARN_PRINT(err_str.utf8().get_data());
  934. }
  935. }
  936. }
  937. memdelete(da);
  938. return OK;
  939. }
  940. Error EditorExportPlatformIOS::_walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata) {
  941. Vector<String> dirs;
  942. String path;
  943. String current_dir = p_da->get_current_dir();
  944. p_da->list_dir_begin();
  945. while ((path = p_da->get_next()).length() != 0) {
  946. if (p_da->current_is_dir()) {
  947. if (path != "." && path != "..") {
  948. dirs.push_back(path);
  949. }
  950. } else {
  951. Error err = p_handler(current_dir.plus_file(path), p_userdata);
  952. if (err) {
  953. p_da->list_dir_end();
  954. return err;
  955. }
  956. }
  957. }
  958. p_da->list_dir_end();
  959. for (int i = 0; i < dirs.size(); ++i) {
  960. String dir = dirs[i];
  961. p_da->change_dir(dir);
  962. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  963. p_da->change_dir("..");
  964. if (err) {
  965. return err;
  966. }
  967. }
  968. return OK;
  969. }
  970. struct CodesignData {
  971. const Ref<EditorExportPreset> &preset;
  972. bool debug;
  973. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  974. preset(p_preset),
  975. debug(p_debug) {
  976. }
  977. };
  978. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  979. if (p_file.ends_with(".dylib")) {
  980. CodesignData *data = (CodesignData *)p_userdata;
  981. print_line(String("Signing ") + p_file);
  982. String sign_id;
  983. if (data->debug) {
  984. sign_id = data->preset->get("application/code_sign_identity_debug").operator String().empty() ? "iPhone Developer" : data->preset->get("application/code_sign_identity_debug");
  985. } else {
  986. sign_id = data->preset->get("application/code_sign_identity_release").operator String().empty() ? "iPhone Distribution" : data->preset->get("application/code_sign_identity_release");
  987. }
  988. List<String> codesign_args;
  989. codesign_args.push_back("-f");
  990. codesign_args.push_back("-s");
  991. codesign_args.push_back(sign_id);
  992. codesign_args.push_back(p_file);
  993. String str;
  994. Error err = OS::get_singleton()->execute("codesign", codesign_args, true, NULL, &str, NULL, true);
  995. print_verbose("codesign (" + p_file + "):\n" + str);
  996. return err;
  997. }
  998. return OK;
  999. }
  1000. struct PbxId {
  1001. private:
  1002. static char _hex_char(uint8_t four_bits) {
  1003. if (four_bits < 10) {
  1004. return ('0' + four_bits);
  1005. }
  1006. return 'A' + (four_bits - 10);
  1007. }
  1008. static String _hex_pad(uint32_t num) {
  1009. Vector<char> ret;
  1010. ret.resize(sizeof(num) * 2);
  1011. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  1012. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  1013. ret.write[i] = _hex_char(four_bits);
  1014. }
  1015. return String::utf8(ret.ptr(), ret.size());
  1016. }
  1017. public:
  1018. uint32_t high_bits;
  1019. uint32_t mid_bits;
  1020. uint32_t low_bits;
  1021. String str() const {
  1022. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  1023. }
  1024. PbxId &operator++() {
  1025. low_bits++;
  1026. if (!low_bits) {
  1027. mid_bits++;
  1028. if (!mid_bits) {
  1029. high_bits++;
  1030. }
  1031. }
  1032. return *this;
  1033. }
  1034. };
  1035. struct ExportLibsData {
  1036. Vector<String> lib_paths;
  1037. String dest_dir;
  1038. };
  1039. void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  1040. // that is just a random number, we just need Godot IDs not to clash with
  1041. // existing IDs in the project.
  1042. PbxId current_id = { 0x58938401, 0, 0 };
  1043. String pbx_files;
  1044. String pbx_frameworks_build;
  1045. String pbx_frameworks_refs;
  1046. String pbx_resources_build;
  1047. String pbx_resources_refs;
  1048. String pbx_embeded_frameworks;
  1049. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  1050. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = \"$name\"; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  1051. for (int i = 0; i < p_additional_assets.size(); ++i) {
  1052. String additional_asset_info_format = file_info_format;
  1053. String build_id = (++current_id).str();
  1054. String ref_id = (++current_id).str();
  1055. String framework_id = "";
  1056. const IOSExportAsset &asset = p_additional_assets[i];
  1057. String type;
  1058. if (asset.exported_path.ends_with(".framework")) {
  1059. if (asset.should_embed) {
  1060. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  1061. framework_id = (++current_id).str();
  1062. pbx_embeded_frameworks += framework_id + ",\n";
  1063. }
  1064. type = "wrapper.framework";
  1065. } else if (asset.exported_path.ends_with(".xcframework")) {
  1066. if (asset.should_embed) {
  1067. additional_asset_info_format += "$framework_id = {isa = PBXBuildFile; fileRef = $ref_id; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };\n";
  1068. framework_id = (++current_id).str();
  1069. pbx_embeded_frameworks += framework_id + ",\n";
  1070. }
  1071. type = "wrapper.xcframework";
  1072. } else if (asset.exported_path.ends_with(".dylib")) {
  1073. type = "compiled.mach-o.dylib";
  1074. } else if (asset.exported_path.ends_with(".a")) {
  1075. type = "archive.ar";
  1076. } else {
  1077. type = "file";
  1078. }
  1079. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  1080. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  1081. if (pbx_build.length() > 0) {
  1082. pbx_build += ",\n";
  1083. pbx_refs += ",\n";
  1084. }
  1085. pbx_build += build_id;
  1086. pbx_refs += ref_id;
  1087. Dictionary format_dict;
  1088. format_dict["build_id"] = build_id;
  1089. format_dict["ref_id"] = ref_id;
  1090. format_dict["name"] = asset.exported_path.get_file();
  1091. format_dict["file_path"] = asset.exported_path;
  1092. format_dict["file_type"] = type;
  1093. if (framework_id.length() > 0) {
  1094. format_dict["framework_id"] = framework_id;
  1095. }
  1096. pbx_files += additional_asset_info_format.format(format_dict, "$_");
  1097. }
  1098. // Note, frameworks like gamekit are always included in our project.pbxprof file
  1099. // even if turned off in capabilities.
  1100. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  1101. str = str.replace("$additional_pbx_files", pbx_files);
  1102. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  1103. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  1104. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  1105. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  1106. str = str.replace("$pbx_embeded_frameworks", pbx_embeded_frameworks);
  1107. CharString cs = str.utf8();
  1108. p_project_data.resize(cs.size() - 1);
  1109. for (int i = 0; i < cs.size() - 1; i++) {
  1110. p_project_data.write[i] = cs[i];
  1111. }
  1112. }
  1113. Error EditorExportPlatformIOS::_copy_asset(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) {
  1114. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1115. ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  1116. String binary_name = p_out_dir.get_file().get_basename();
  1117. DirAccess *da = DirAccess::create_for_path(p_asset);
  1118. if (!da) {
  1119. memdelete(filesystem_da);
  1120. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't create directory: " + p_asset + ".");
  1121. }
  1122. bool file_exists = da->file_exists(p_asset);
  1123. bool dir_exists = da->dir_exists(p_asset);
  1124. if (!file_exists && !dir_exists) {
  1125. memdelete(da);
  1126. memdelete(filesystem_da);
  1127. return ERR_FILE_NOT_FOUND;
  1128. }
  1129. String base_dir = p_asset.get_base_dir().replace("res://", "");
  1130. String destination_dir;
  1131. String destination;
  1132. String asset_path;
  1133. bool create_framework = false;
  1134. if (p_is_framework && p_asset.ends_with(".dylib")) {
  1135. // For iOS we need to turn .dylib into .framework
  1136. // to be able to send application to AppStore
  1137. asset_path = String("dylibs").plus_file(base_dir);
  1138. String file_name;
  1139. if (!p_custom_file_name) {
  1140. file_name = p_asset.get_basename().get_file();
  1141. } else {
  1142. file_name = *p_custom_file_name;
  1143. }
  1144. String framework_name = file_name + ".framework";
  1145. asset_path = asset_path.plus_file(framework_name);
  1146. destination_dir = p_out_dir.plus_file(asset_path);
  1147. destination = destination_dir.plus_file(file_name);
  1148. create_framework = true;
  1149. } else if (p_is_framework && (p_asset.ends_with(".framework") || p_asset.ends_with(".xcframework"))) {
  1150. asset_path = String("dylibs").plus_file(base_dir);
  1151. String file_name;
  1152. if (!p_custom_file_name) {
  1153. file_name = p_asset.get_file();
  1154. } else {
  1155. file_name = *p_custom_file_name;
  1156. }
  1157. asset_path = asset_path.plus_file(file_name);
  1158. destination_dir = p_out_dir.plus_file(asset_path);
  1159. destination = destination_dir;
  1160. } else {
  1161. asset_path = base_dir;
  1162. String file_name;
  1163. if (!p_custom_file_name) {
  1164. file_name = p_asset.get_file();
  1165. } else {
  1166. file_name = *p_custom_file_name;
  1167. }
  1168. destination_dir = p_out_dir.plus_file(asset_path);
  1169. asset_path = asset_path.plus_file(file_name);
  1170. destination = p_out_dir.plus_file(asset_path);
  1171. }
  1172. if (!filesystem_da->dir_exists(destination_dir)) {
  1173. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  1174. if (make_dir_err) {
  1175. memdelete(da);
  1176. memdelete(filesystem_da);
  1177. return make_dir_err;
  1178. }
  1179. }
  1180. Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination);
  1181. memdelete(da);
  1182. if (err) {
  1183. memdelete(filesystem_da);
  1184. return err;
  1185. }
  1186. IOSExportAsset exported_asset = { binary_name.plus_file(asset_path), p_is_framework, p_should_embed };
  1187. r_exported_assets.push_back(exported_asset);
  1188. if (create_framework) {
  1189. String file_name;
  1190. if (!p_custom_file_name) {
  1191. file_name = p_asset.get_basename().get_file();
  1192. } else {
  1193. file_name = *p_custom_file_name;
  1194. }
  1195. String framework_name = file_name + ".framework";
  1196. // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib
  1197. {
  1198. List<String> install_name_args;
  1199. install_name_args.push_back("-id");
  1200. install_name_args.push_back(String("@rpath").plus_file(framework_name).plus_file(file_name));
  1201. install_name_args.push_back(destination);
  1202. OS::get_singleton()->execute("install_name_tool", install_name_args, true);
  1203. }
  1204. // Creating Info.plist
  1205. {
  1206. String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1207. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  1208. "<plist version=\"1.0\">\n"
  1209. "<dict>\n"
  1210. "<key>CFBundleShortVersionString</key>\n"
  1211. "<string>1.0</string>\n"
  1212. "<key>CFBundleIdentifier</key>\n"
  1213. "<string>com.gdnative.framework.$name</string>\n"
  1214. "<key>CFBundleName</key>\n"
  1215. "<string>$name</string>\n"
  1216. "<key>CFBundleExecutable</key>\n"
  1217. "<string>$name</string>\n"
  1218. "<key>DTPlatformName</key>\n"
  1219. "<string>iphoneos</string>\n"
  1220. "<key>CFBundleInfoDictionaryVersion</key>\n"
  1221. "<string>6.0</string>\n"
  1222. "<key>CFBundleVersion</key>\n"
  1223. "<string>1</string>\n"
  1224. "<key>CFBundlePackageType</key>\n"
  1225. "<string>FMWK</string>\n"
  1226. "<key>MinimumOSVersion</key>\n"
  1227. "<string>10.0</string>\n"
  1228. "</dict>\n"
  1229. "</plist>";
  1230. String info_plist = info_plist_format.replace("$name", file_name);
  1231. FileAccess *f = FileAccess::open(destination_dir.plus_file("Info.plist"), FileAccess::WRITE);
  1232. if (f) {
  1233. f->store_string(info_plist);
  1234. f->close();
  1235. memdelete(f);
  1236. }
  1237. }
  1238. }
  1239. memdelete(filesystem_da);
  1240. return OK;
  1241. }
  1242. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) {
  1243. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  1244. String asset = p_assets[f_idx];
  1245. if (!asset.begins_with("res://")) {
  1246. // either SDK-builtin or already a part of the export template
  1247. IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed };
  1248. r_exported_assets.push_back(exported_asset);
  1249. } else {
  1250. Error err = _copy_asset(p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets);
  1251. ERR_FAIL_COND_V(err, err);
  1252. }
  1253. }
  1254. return OK;
  1255. }
  1256. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  1257. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1258. for (int i = 0; i < export_plugins.size(); i++) {
  1259. Vector<String> linked_frameworks = export_plugins[i]->get_ios_frameworks();
  1260. Error err = _export_additional_assets(p_out_dir, linked_frameworks, true, false, r_exported_assets);
  1261. ERR_FAIL_COND_V(err, err);
  1262. Vector<String> embedded_frameworks = export_plugins[i]->get_ios_embedded_frameworks();
  1263. err = _export_additional_assets(p_out_dir, embedded_frameworks, true, true, r_exported_assets);
  1264. ERR_FAIL_COND_V(err, err);
  1265. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1266. for (int j = 0; j < project_static_libs.size(); j++) {
  1267. project_static_libs.write[j] = project_static_libs[j].get_file(); // Only the file name as it's copied to the project
  1268. }
  1269. err = _export_additional_assets(p_out_dir, project_static_libs, true, false, r_exported_assets);
  1270. ERR_FAIL_COND_V(err, err);
  1271. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  1272. err = _export_additional_assets(p_out_dir, ios_bundle_files, false, false, r_exported_assets);
  1273. ERR_FAIL_COND_V(err, err);
  1274. }
  1275. Vector<String> library_paths;
  1276. for (int i = 0; i < p_libraries.size(); ++i) {
  1277. library_paths.push_back(p_libraries[i].path);
  1278. }
  1279. Error err = _export_additional_assets(p_out_dir, library_paths, true, true, r_exported_assets);
  1280. ERR_FAIL_COND_V(err, err);
  1281. return OK;
  1282. }
  1283. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) {
  1284. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  1285. Vector<String> enabled_archs;
  1286. for (int i = 0; i < all_archs.size(); ++i) {
  1287. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  1288. if (is_enabled) {
  1289. enabled_archs.push_back(all_archs[i].name);
  1290. }
  1291. }
  1292. return enabled_archs;
  1293. }
  1294. 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) {
  1295. String plugin_definition_cpp_code;
  1296. String plugin_initialization_cpp_code;
  1297. String plugin_deinitialization_cpp_code;
  1298. Vector<String> plugin_linked_dependencies;
  1299. Vector<String> plugin_embedded_dependencies;
  1300. Vector<String> plugin_files;
  1301. Vector<PluginConfigIOS> enabled_plugins = get_enabled_plugins(p_preset);
  1302. Vector<String> added_linked_dependenciy_names;
  1303. Vector<String> added_embedded_dependenciy_names;
  1304. HashMap<String, String> plist_values;
  1305. Set<String> plugin_linker_flags;
  1306. Error err;
  1307. for (int i = 0; i < enabled_plugins.size(); i++) {
  1308. PluginConfigIOS plugin = enabled_plugins[i];
  1309. // Export plugin binary.
  1310. String plugin_main_binary = get_plugin_main_binary(plugin, p_debug);
  1311. String plugin_binary_result_file = plugin.binary.get_file();
  1312. // We shouldn't embed .xcframework that contains static libraries.
  1313. // Static libraries are not embedded anyway.
  1314. err = _copy_asset(dest_dir, plugin_main_binary, &plugin_binary_result_file, true, false, r_exported_assets);
  1315. ERR_FAIL_COND_V(err, err);
  1316. // Adding dependencies.
  1317. // Use separate container for names to check for duplicates.
  1318. for (int j = 0; j < plugin.linked_dependencies.size(); j++) {
  1319. String dependency = plugin.linked_dependencies[j];
  1320. String name = dependency.get_file();
  1321. if (added_linked_dependenciy_names.find(name) != -1) {
  1322. continue;
  1323. }
  1324. added_linked_dependenciy_names.push_back(name);
  1325. plugin_linked_dependencies.push_back(dependency);
  1326. }
  1327. for (int j = 0; j < plugin.system_dependencies.size(); j++) {
  1328. String dependency = plugin.system_dependencies[j];
  1329. String name = dependency.get_file();
  1330. if (added_linked_dependenciy_names.find(name) != -1) {
  1331. continue;
  1332. }
  1333. added_linked_dependenciy_names.push_back(name);
  1334. plugin_linked_dependencies.push_back(dependency);
  1335. }
  1336. for (int j = 0; j < plugin.embedded_dependencies.size(); j++) {
  1337. String dependency = plugin.embedded_dependencies[j];
  1338. String name = dependency.get_file();
  1339. if (added_embedded_dependenciy_names.find(name) != -1) {
  1340. continue;
  1341. }
  1342. added_embedded_dependenciy_names.push_back(name);
  1343. plugin_embedded_dependencies.push_back(dependency);
  1344. }
  1345. plugin_files.append_array(plugin.files_to_copy);
  1346. // Capabilities
  1347. // Also checking for duplicates.
  1348. for (int j = 0; j < plugin.capabilities.size(); j++) {
  1349. String capability = plugin.capabilities[j];
  1350. if (p_config_data.capabilities.find(capability) != -1) {
  1351. continue;
  1352. }
  1353. p_config_data.capabilities.push_back(capability);
  1354. }
  1355. // Linker flags
  1356. // Checking duplicates
  1357. for (int j = 0; j < plugin.linker_flags.size(); j++) {
  1358. String linker_flag = plugin.linker_flags[j];
  1359. plugin_linker_flags.insert(linker_flag);
  1360. }
  1361. // Plist
  1362. // Using hash map container to remove duplicates
  1363. const String *K = nullptr;
  1364. while ((K = plugin.plist.next(K))) {
  1365. String key = *K;
  1366. PluginConfigIOS::PlistItem item = plugin.plist[key];
  1367. String value;
  1368. switch (item.type) {
  1369. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  1370. String preset_name = "plugins_plist/" + key;
  1371. String input_value = p_preset->get(preset_name);
  1372. value = "<string>" + input_value + "</string>";
  1373. } break;
  1374. default:
  1375. value = item.value;
  1376. break;
  1377. }
  1378. if (key.empty() || value.empty()) {
  1379. continue;
  1380. }
  1381. String plist_key = "<key>" + key + "</key>";
  1382. plist_values[plist_key] = value;
  1383. }
  1384. // CPP Code
  1385. String definition_comment = "// Plugin: " + plugin.name + "\n";
  1386. String initialization_method = plugin.initialization_method + "();\n";
  1387. String deinitialization_method = plugin.deinitialization_method + "();\n";
  1388. plugin_definition_cpp_code += definition_comment +
  1389. "extern void " + initialization_method +
  1390. "extern void " + deinitialization_method + "\n";
  1391. plugin_initialization_cpp_code += "\t" + initialization_method;
  1392. plugin_deinitialization_cpp_code += "\t" + deinitialization_method;
  1393. if (plugin.use_swift_runtime) {
  1394. p_config_data.use_swift_runtime = true;
  1395. }
  1396. }
  1397. // Updating `Info.plist`
  1398. {
  1399. const String *K = nullptr;
  1400. while ((K = plist_values.next(K))) {
  1401. String key = *K;
  1402. String value = plist_values[key];
  1403. if (key.empty() || value.empty()) {
  1404. continue;
  1405. }
  1406. p_config_data.plist_content += key + value + "\n";
  1407. }
  1408. }
  1409. // Export files
  1410. {
  1411. // Export linked plugin dependency
  1412. err = _export_additional_assets(dest_dir, plugin_linked_dependencies, true, false, r_exported_assets);
  1413. ERR_FAIL_COND_V(err, err);
  1414. // Export embedded plugin dependency
  1415. err = _export_additional_assets(dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets);
  1416. ERR_FAIL_COND_V(err, err);
  1417. // Export plugin files
  1418. err = _export_additional_assets(dest_dir, plugin_files, false, false, r_exported_assets);
  1419. ERR_FAIL_COND_V(err, err);
  1420. }
  1421. // Update CPP
  1422. {
  1423. Dictionary plugin_format;
  1424. plugin_format["definition"] = plugin_definition_cpp_code;
  1425. plugin_format["initialization"] = plugin_initialization_cpp_code;
  1426. plugin_format["deinitialization"] = plugin_deinitialization_cpp_code;
  1427. String plugin_cpp_code = "\n// Godot Plugins\n"
  1428. "void godot_ios_plugins_initialize();\n"
  1429. "void godot_ios_plugins_deinitialize();\n"
  1430. "// Exported Plugins\n\n"
  1431. "$definition"
  1432. "// Use Plugins\n"
  1433. "void godot_ios_plugins_initialize() {\n"
  1434. "$initialization"
  1435. "}\n\n"
  1436. "void godot_ios_plugins_deinitialize() {\n"
  1437. "$deinitialization"
  1438. "}\n";
  1439. p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_");
  1440. }
  1441. // Update Linker Flag Values
  1442. {
  1443. String result_linker_flags = " ";
  1444. for (Set<String>::Element *E = plugin_linker_flags.front(); E; E = E->next()) {
  1445. const String &flag = E->get();
  1446. if (flag.length() == 0) {
  1447. continue;
  1448. }
  1449. if (result_linker_flags.length() > 0) {
  1450. result_linker_flags += ' ';
  1451. }
  1452. result_linker_flags += flag;
  1453. }
  1454. result_linker_flags = result_linker_flags.replace("\"", "\\\"");
  1455. p_config_data.linker_flags += result_linker_flags;
  1456. }
  1457. return OK;
  1458. }
  1459. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1460. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1461. String src_pkg_name;
  1462. String dest_dir = p_path.get_base_dir() + "/";
  1463. String binary_name = p_path.get_file().get_basename();
  1464. EditorProgress ep("export", "Exporting for iOS", 5, true);
  1465. String team_id = p_preset->get("application/app_store_team_id");
  1466. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  1467. if (p_debug) {
  1468. src_pkg_name = p_preset->get("custom_template/debug");
  1469. } else {
  1470. src_pkg_name = p_preset->get("custom_template/release");
  1471. }
  1472. if (src_pkg_name == "") {
  1473. String err;
  1474. src_pkg_name = find_export_template("iphone.zip", &err);
  1475. if (src_pkg_name == "") {
  1476. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Export template not found."));
  1477. return ERR_FILE_NOT_FOUND;
  1478. }
  1479. }
  1480. if (!DirAccess::exists(dest_dir)) {
  1481. return ERR_FILE_BAD_PATH;
  1482. }
  1483. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1484. if (da) {
  1485. String current_dir = da->get_current_dir();
  1486. // remove leftovers from last export so they don't interfere
  1487. // in case some files are no longer needed
  1488. if (da->change_dir(dest_dir + binary_name + ".xcodeproj") == OK) {
  1489. da->erase_contents_recursive();
  1490. }
  1491. if (da->change_dir(dest_dir + binary_name) == OK) {
  1492. da->erase_contents_recursive();
  1493. }
  1494. da->change_dir(current_dir);
  1495. if (!da->dir_exists(dest_dir + binary_name)) {
  1496. Error err = da->make_dir(dest_dir + binary_name);
  1497. if (err) {
  1498. memdelete(da);
  1499. return err;
  1500. }
  1501. }
  1502. memdelete(da);
  1503. }
  1504. if (ep.step("Making .pck", 0)) {
  1505. return ERR_SKIP;
  1506. }
  1507. String pack_path = dest_dir + binary_name + ".pck";
  1508. Vector<SharedObject> libraries;
  1509. Error err = save_pack(p_preset, pack_path, &libraries);
  1510. if (err) {
  1511. return err;
  1512. }
  1513. if (ep.step("Extracting and configuring Xcode project", 1)) {
  1514. return ERR_SKIP;
  1515. }
  1516. String library_to_use = "libgodot.iphone." + String(p_debug ? "debug" : "release") + ".xcframework";
  1517. print_line("Static framework: " + library_to_use);
  1518. String pkg_name;
  1519. if (p_preset->get("application/name") != "") {
  1520. pkg_name = p_preset->get("application/name"); // app_name
  1521. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  1522. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  1523. } else {
  1524. pkg_name = "Unnamed";
  1525. }
  1526. bool found_library = false;
  1527. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  1528. Set<String> files_to_parse;
  1529. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  1530. files_to_parse.insert(project_file);
  1531. files_to_parse.insert("godot_ios/export_options.plist");
  1532. files_to_parse.insert("godot_ios/dummy.cpp");
  1533. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  1534. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  1535. files_to_parse.insert("godot_ios/godot_ios.entitlements");
  1536. files_to_parse.insert("godot_ios/Launch Screen.storyboard");
  1537. IOSConfigData config_data = {
  1538. pkg_name,
  1539. binary_name,
  1540. _get_additional_plist_content(),
  1541. String(" ").join(_get_preset_architectures(p_preset)),
  1542. _get_linker_flags(),
  1543. _get_cpp_code(),
  1544. "",
  1545. "",
  1546. "",
  1547. "",
  1548. Vector<String>(),
  1549. false
  1550. };
  1551. Vector<IOSExportAsset> assets;
  1552. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  1553. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE);
  1554. print_line("Unzipping...");
  1555. FileAccess *src_f = nullptr;
  1556. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  1557. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  1558. if (!src_pkg_zip) {
  1559. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Could not open export template (not a zip file?): \"%s\".", src_pkg_name));
  1560. return ERR_CANT_OPEN;
  1561. }
  1562. err = _export_ios_plugins(p_preset, config_data, dest_dir + binary_name, assets, p_debug);
  1563. ERR_FAIL_COND_V(err, err);
  1564. //export rest of the files
  1565. int ret = unzGoToFirstFile(src_pkg_zip);
  1566. Vector<uint8_t> project_file_data;
  1567. while (ret == UNZ_OK) {
  1568. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1569. bool is_execute = false;
  1570. #endif
  1571. //get filename
  1572. unz_file_info info;
  1573. char fname[16384];
  1574. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  1575. String file = String::utf8(fname);
  1576. print_line("READ: " + file);
  1577. Vector<uint8_t> data;
  1578. data.resize(info.uncompressed_size);
  1579. //read
  1580. unzOpenCurrentFile(src_pkg_zip);
  1581. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  1582. unzCloseCurrentFile(src_pkg_zip);
  1583. //write
  1584. file = file.replace_first("iphone/", "");
  1585. if (files_to_parse.has(file)) {
  1586. _fix_config_file(p_preset, data, config_data, p_debug);
  1587. } else if (file.begins_with("libgodot.iphone")) {
  1588. if (!file.begins_with(library_to_use) || file.ends_with(String("/empty"))) {
  1589. ret = unzGoToNextFile(src_pkg_zip);
  1590. continue; //ignore!
  1591. }
  1592. found_library = true;
  1593. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1594. is_execute = true;
  1595. #endif
  1596. file = file.replace(library_to_use, binary_name + ".xcframework");
  1597. }
  1598. if (file == project_file) {
  1599. project_file_data = data;
  1600. }
  1601. ///@TODO need to parse logo files
  1602. if (data.size() > 0) {
  1603. file = file.replace("godot_ios", binary_name);
  1604. print_line("ADDING: " + file + " size: " + itos(data.size()));
  1605. /* write it into our folder structure */
  1606. file = dest_dir + file;
  1607. /* make sure this folder exists */
  1608. String dir_name = file.get_base_dir();
  1609. if (!tmp_app_path->dir_exists(dir_name)) {
  1610. print_line("Creating " + dir_name);
  1611. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  1612. if (dir_err) {
  1613. ERR_PRINT("Can't create '" + dir_name + "'.");
  1614. unzClose(src_pkg_zip);
  1615. memdelete(tmp_app_path);
  1616. return ERR_CANT_CREATE;
  1617. }
  1618. }
  1619. /* write the file */
  1620. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  1621. if (!f) {
  1622. ERR_PRINT("Can't write '" + file + "'.");
  1623. unzClose(src_pkg_zip);
  1624. memdelete(tmp_app_path);
  1625. return ERR_CANT_CREATE;
  1626. };
  1627. f->store_buffer(data.ptr(), data.size());
  1628. f->close();
  1629. memdelete(f);
  1630. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1631. if (is_execute) {
  1632. // we need execute rights on this file
  1633. chmod(file.utf8().get_data(), 0755);
  1634. }
  1635. #endif
  1636. }
  1637. ret = unzGoToNextFile(src_pkg_zip);
  1638. }
  1639. /* we're done with our source zip */
  1640. unzClose(src_pkg_zip);
  1641. if (!found_library) {
  1642. ERR_PRINT("Requested template library '" + library_to_use + "' not found. It might be missing from your template archive.");
  1643. memdelete(tmp_app_path);
  1644. return ERR_FILE_NOT_FOUND;
  1645. }
  1646. Vector<String> translations = ProjectSettings::get_singleton()->get("locale/translations");
  1647. if (translations.size() > 0) {
  1648. {
  1649. String fname = dest_dir + binary_name + "/en.lproj";
  1650. tmp_app_path->make_dir_recursive(fname);
  1651. FileAccessRef f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1652. f->store_line("CFBundleDisplayName = \"" + ProjectSettings::get_singleton()->get("application/config/name").operator String() + "\";");
  1653. }
  1654. for (int i = 0; i < translations.size(); i++) {
  1655. Ref<Translation> tr = ResourceLoader::load(translations[i]);
  1656. if (tr.is_valid()) {
  1657. String fname = dest_dir + binary_name + "/" + tr->get_locale() + ".lproj";
  1658. tmp_app_path->make_dir_recursive(fname);
  1659. FileAccessRef f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  1660. String prop = "application/config/name_" + tr->get_locale();
  1661. if (ProjectSettings::get_singleton()->has_setting(prop)) {
  1662. f->store_line("CFBundleDisplayName = \"" + ProjectSettings::get_singleton()->get(prop).operator String() + "\";");
  1663. }
  1664. }
  1665. }
  1666. }
  1667. // Copy project static libs to the project
  1668. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1669. for (int i = 0; i < export_plugins.size(); i++) {
  1670. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1671. for (int j = 0; j < project_static_libs.size(); j++) {
  1672. const String &static_lib_path = project_static_libs[j];
  1673. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  1674. bool dir_exists = tmp_app_path->dir_exists(static_lib_path);
  1675. Error lib_copy_err = dir_exists ? tmp_app_path->copy_dir(static_lib_path, dest_lib_file_path) : tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  1676. if (lib_copy_err != OK) {
  1677. ERR_PRINT("Can't copy '" + static_lib_path + "'.");
  1678. memdelete(tmp_app_path);
  1679. return lib_copy_err;
  1680. }
  1681. }
  1682. }
  1683. String iconset_dir = dest_dir + binary_name + "/Images.xcassets/AppIcon.appiconset/";
  1684. err = OK;
  1685. if (!tmp_app_path->dir_exists(iconset_dir)) {
  1686. err = tmp_app_path->make_dir_recursive(iconset_dir);
  1687. }
  1688. memdelete(tmp_app_path);
  1689. if (err) {
  1690. return err;
  1691. }
  1692. err = _export_icons(p_preset, iconset_dir);
  1693. if (err) {
  1694. return err;
  1695. }
  1696. bool use_storyboard = p_preset->get("storyboard/use_launch_screen_storyboard");
  1697. String launch_image_path = dest_dir + binary_name + "/Images.xcassets/LaunchImage.launchimage/";
  1698. String splash_image_path = dest_dir + binary_name + "/Images.xcassets/SplashImage.imageset/";
  1699. DirAccess *launch_screen_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1700. if (!launch_screen_da) {
  1701. return ERR_CANT_CREATE;
  1702. }
  1703. if (use_storyboard) {
  1704. print_line("Using Launch Storyboard");
  1705. if (launch_screen_da->change_dir(launch_image_path) == OK) {
  1706. launch_screen_da->erase_contents_recursive();
  1707. launch_screen_da->remove(launch_image_path);
  1708. }
  1709. err = _export_loading_screen_file(p_preset, splash_image_path);
  1710. } else {
  1711. print_line("Using Launch Images");
  1712. const String launch_screen_path = dest_dir + binary_name + "/Launch Screen.storyboard";
  1713. launch_screen_da->remove(launch_screen_path);
  1714. if (launch_screen_da->change_dir(splash_image_path) == OK) {
  1715. launch_screen_da->erase_contents_recursive();
  1716. launch_screen_da->remove(splash_image_path);
  1717. }
  1718. err = _export_loading_screen_images(p_preset, launch_image_path);
  1719. }
  1720. memdelete(launch_screen_da);
  1721. if (err) {
  1722. return err;
  1723. }
  1724. print_line("Exporting additional assets");
  1725. _export_additional_assets(dest_dir + binary_name, libraries, assets);
  1726. _add_assets_to_project(p_preset, project_file_data, assets);
  1727. String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj";
  1728. FileAccess *f = FileAccess::open(project_file_name, FileAccess::WRITE);
  1729. if (!f) {
  1730. ERR_PRINT("Can't write '" + project_file_name + "'.");
  1731. return ERR_CANT_CREATE;
  1732. };
  1733. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  1734. f->close();
  1735. memdelete(f);
  1736. #ifdef OSX_ENABLED
  1737. if (ep.step("Code-signing dylibs", 2)) {
  1738. return ERR_SKIP;
  1739. }
  1740. DirAccess *dylibs_dir = DirAccess::open(dest_dir + binary_name + "/dylibs");
  1741. ERR_FAIL_COND_V(!dylibs_dir, ERR_CANT_OPEN);
  1742. CodesignData codesign_data(p_preset, p_debug);
  1743. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  1744. memdelete(dylibs_dir);
  1745. ERR_FAIL_COND_V(err, err);
  1746. if (ep.step("Making .xcarchive", 3)) {
  1747. return ERR_SKIP;
  1748. }
  1749. String archive_path = p_path.get_basename() + ".xcarchive";
  1750. List<String> archive_args;
  1751. archive_args.push_back("-project");
  1752. archive_args.push_back(dest_dir + binary_name + ".xcodeproj");
  1753. archive_args.push_back("-scheme");
  1754. archive_args.push_back(binary_name);
  1755. archive_args.push_back("-sdk");
  1756. archive_args.push_back("iphoneos");
  1757. archive_args.push_back("-configuration");
  1758. archive_args.push_back(p_debug ? "Debug" : "Release");
  1759. archive_args.push_back("-destination");
  1760. archive_args.push_back("generic/platform=iOS");
  1761. archive_args.push_back("archive");
  1762. archive_args.push_back("-allowProvisioningUpdates");
  1763. archive_args.push_back("-archivePath");
  1764. archive_args.push_back(archive_path);
  1765. String archive_str;
  1766. err = OS::get_singleton()->execute("xcodebuild", archive_args, true, NULL, &archive_str, NULL, true);
  1767. ERR_FAIL_COND_V(err, err);
  1768. print_line("xcodebuild (.xcarchive):\n" + archive_str);
  1769. if (ep.step("Making .ipa", 4)) {
  1770. return ERR_SKIP;
  1771. }
  1772. List<String> export_args;
  1773. export_args.push_back("-exportArchive");
  1774. export_args.push_back("-archivePath");
  1775. export_args.push_back(archive_path);
  1776. export_args.push_back("-exportOptionsPlist");
  1777. export_args.push_back(dest_dir + binary_name + "/export_options.plist");
  1778. export_args.push_back("-allowProvisioningUpdates");
  1779. export_args.push_back("-exportPath");
  1780. export_args.push_back(dest_dir);
  1781. String export_str;
  1782. err = OS::get_singleton()->execute("xcodebuild", export_args, true, NULL, &export_str, NULL, true);
  1783. ERR_FAIL_COND_V(err, err);
  1784. print_line("xcodebuild (.ipa):\n" + export_str);
  1785. #else
  1786. print_line(".ipa can only be built on macOS. Leaving Xcode project without building the package.");
  1787. #endif
  1788. return OK;
  1789. }
  1790. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1791. String err;
  1792. bool valid = false;
  1793. // Look for export templates (first official, and if defined custom templates).
  1794. bool dvalid = exists_export_template("iphone.zip", &err);
  1795. bool rvalid = dvalid; // Both in the same ZIP.
  1796. if (p_preset->get("custom_template/debug") != "") {
  1797. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1798. if (!dvalid) {
  1799. err += TTR("Custom debug template not found.") + "\n";
  1800. }
  1801. }
  1802. if (p_preset->get("custom_template/release") != "") {
  1803. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1804. if (!rvalid) {
  1805. err += TTR("Custom release template not found.") + "\n";
  1806. }
  1807. }
  1808. valid = dvalid || rvalid;
  1809. r_missing_templates = !valid;
  1810. // Validate the rest of the configuration.
  1811. String team_id = p_preset->get("application/app_store_team_id");
  1812. if (team_id.length() == 0) {
  1813. err += TTR("App Store Team ID not specified - cannot configure the project.") + "\n";
  1814. valid = false;
  1815. }
  1816. String identifier = p_preset->get("application/identifier");
  1817. String pn_err;
  1818. if (!is_package_name_valid(identifier, &pn_err)) {
  1819. err += TTR("Invalid Identifier:") + " " + pn_err + "\n";
  1820. valid = false;
  1821. }
  1822. String etc_error = test_etc2_or_pvrtc();
  1823. if (etc_error != String()) {
  1824. valid = false;
  1825. err += etc_error;
  1826. }
  1827. if (!err.empty()) {
  1828. r_error = err;
  1829. }
  1830. return valid;
  1831. }
  1832. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  1833. Ref<Image> img = memnew(Image(_iphone_logo));
  1834. logo.instance();
  1835. logo->create_from_image(img);
  1836. plugins_changed.set();
  1837. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  1838. }
  1839. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  1840. quit_request.set();
  1841. check_for_changes_thread.wait_to_finish();
  1842. }
  1843. void register_iphone_exporter() {
  1844. Ref<EditorExportPlatformIOS> platform;
  1845. platform.instance();
  1846. EditorExport::get_singleton()->add_export_platform(platform);
  1847. }