export.cpp 73 KB

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