export.cpp 74 KB

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