export.cpp 69 KB

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