2
0

export.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/version.h"
  38. #include "editor/editor_export.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "platform/iphone/logo.gen.h"
  42. #include "string.h"
  43. #include <sys/stat.h>
  44. class EditorExportPlatformIOS : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformIOS, EditorExportPlatform);
  46. int version_code;
  47. Ref<ImageTexture> logo;
  48. typedef Error (*FileHandler)(String p_file, void *p_userdata);
  49. static Error _walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata);
  50. static Error _codesign(String p_file, void *p_userdata);
  51. struct IOSConfigData {
  52. String pkg_name;
  53. String binary_name;
  54. String plist_content;
  55. String architectures;
  56. String linker_flags;
  57. String cpp_code;
  58. String modules_buildfile;
  59. String modules_fileref;
  60. String modules_buildphase;
  61. String modules_buildgrp;
  62. };
  63. struct ExportArchitecture {
  64. String name;
  65. bool is_default;
  66. ExportArchitecture() :
  67. name(""),
  68. is_default(false) {
  69. }
  70. ExportArchitecture(String p_name, bool p_is_default) {
  71. name = p_name;
  72. is_default = p_is_default;
  73. }
  74. };
  75. struct IOSExportAsset {
  76. String exported_path;
  77. bool is_framework; // framework is anything linked to the binary, otherwise it's a resource
  78. };
  79. String _get_additional_plist_content();
  80. String _get_linker_flags();
  81. String _get_cpp_code();
  82. void _fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug);
  83. Error _export_loading_screens(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
  84. Error _export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir);
  85. Vector<ExportArchitecture> _get_supported_architectures();
  86. Vector<String> _get_preset_architectures(const Ref<EditorExportPreset> &p_preset);
  87. void _add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets);
  88. Error _export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, Vector<IOSExportAsset> &r_exported_assets);
  89. Error _export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets);
  90. bool is_package_name_valid(const String &p_package, String *r_error = NULL) const {
  91. String pname = p_package;
  92. if (pname.length() == 0) {
  93. if (r_error) {
  94. *r_error = TTR("Identifier is missing.");
  95. }
  96. return false;
  97. }
  98. for (int i = 0; i < pname.length(); i++) {
  99. CharType c = pname[i];
  100. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  101. if (r_error) {
  102. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  103. }
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. protected:
  110. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  111. virtual void get_export_options(List<ExportOption> *r_options);
  112. public:
  113. virtual String get_name() const { return "iOS"; }
  114. virtual String get_os_name() const { return "iOS"; }
  115. virtual Ref<Texture> get_logo() const { return logo; }
  116. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  117. List<String> list;
  118. list.push_back("ipa");
  119. return list;
  120. }
  121. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  122. virtual void add_module_code(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &p_name, const String &p_fid, const String &p_gid);
  123. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  124. virtual void get_platform_features(List<String> *r_features) {
  125. r_features->push_back("mobile");
  126. r_features->push_back("iOS");
  127. }
  128. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  129. }
  130. EditorExportPlatformIOS();
  131. ~EditorExportPlatformIOS();
  132. };
  133. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  134. String driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name");
  135. if (driver == "GLES2") {
  136. r_features->push_back("etc");
  137. } else if (driver == "GLES3") {
  138. r_features->push_back("etc2");
  139. if (ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2")) {
  140. r_features->push_back("etc");
  141. }
  142. }
  143. Vector<String> architectures = _get_preset_architectures(p_preset);
  144. for (int i = 0; i < architectures.size(); ++i) {
  145. r_features->push_back(architectures[i]);
  146. }
  147. }
  148. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() {
  149. Vector<ExportArchitecture> archs;
  150. archs.push_back(ExportArchitecture("armv7", false)); // Disabled by default, not included in official templates.
  151. archs.push_back(ExportArchitecture("arm64", true));
  152. return archs;
  153. }
  154. struct LoadingScreenInfo {
  155. const char *preset_key;
  156. const char *export_name;
  157. };
  158. static const LoadingScreenInfo loading_screen_infos[] = {
  159. { "landscape_launch_screens/iphone_2436x1125", "Default-Landscape-X.png" },
  160. { "landscape_launch_screens/iphone_2208x1242", "[email protected]" },
  161. { "landscape_launch_screens/ipad_1024x768", "Default-Landscape.png" },
  162. { "landscape_launch_screens/ipad_2048x1536", "[email protected]" },
  163. { "portrait_launch_screens/iphone_640x960", "[email protected]" },
  164. { "portrait_launch_screens/iphone_640x1136", "[email protected]" },
  165. { "portrait_launch_screens/iphone_750x1334", "[email protected]" },
  166. { "portrait_launch_screens/iphone_1125x2436", "Default-Portrait-X.png" },
  167. { "portrait_launch_screens/ipad_768x1024", "Default-Portrait.png" },
  168. { "portrait_launch_screens/ipad_1536x2048", "[email protected]" },
  169. { "portrait_launch_screens/iphone_1242x2208", "[email protected]" }
  170. };
  171. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) {
  172. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  173. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  174. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), ""));
  175. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug"), ""));
  176. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), "iPhone Developer"));
  177. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  178. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release"), ""));
  179. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  180. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  181. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  182. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  183. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  184. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  185. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  186. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  187. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  188. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/arkit"), false));
  189. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/camera"), false));
  190. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  191. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/game_center"), true));
  192. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/in_app_purchases"), false));
  193. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  194. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  195. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  196. 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"), ""));
  197. 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"), ""));
  198. 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"), ""));
  199. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
  200. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_left"), true));
  201. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_right"), true));
  202. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_upside_down"), true));
  203. 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
  204. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/ipad_76x76", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad
  205. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/app_store_1024x1024", PROPERTY_HINT_FILE, "*.png"), "")); // App Store
  206. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/iphone_180x180", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone with retina HD display
  207. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_152x152", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad with retina display
  208. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_167x167", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad Pro
  209. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_40x40", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight
  210. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_80x80", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight on devices with retina display
  211. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  212. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, loading_screen_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png"), ""));
  213. }
  214. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  215. for (int i = 0; i < architectures.size(); ++i) {
  216. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architectures/" + architectures[i].name), architectures[i].is_default));
  217. }
  218. }
  219. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  220. static const String export_method_string[] = {
  221. "app-store",
  222. "development",
  223. "ad-hoc",
  224. "enterprise"
  225. };
  226. String str;
  227. String strnew;
  228. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  229. Vector<String> lines = str.split("\n");
  230. for (int i = 0; i < lines.size(); i++) {
  231. if (lines[i].find("$binary") != -1) {
  232. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  233. } else if (lines[i].find("$modules_buildfile") != -1) {
  234. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  235. } else if (lines[i].find("$modules_fileref") != -1) {
  236. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  237. } else if (lines[i].find("$modules_buildphase") != -1) {
  238. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  239. } else if (lines[i].find("$modules_buildgrp") != -1) {
  240. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  241. } else if (lines[i].find("$name") != -1) {
  242. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  243. } else if (lines[i].find("$info") != -1) {
  244. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  245. } else if (lines[i].find("$identifier") != -1) {
  246. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  247. } else if (lines[i].find("$short_version") != -1) {
  248. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  249. } else if (lines[i].find("$version") != -1) {
  250. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  251. } else if (lines[i].find("$signature") != -1) {
  252. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  253. } else if (lines[i].find("$copyright") != -1) {
  254. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  255. } else if (lines[i].find("$team_id") != -1) {
  256. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  257. } else if (lines[i].find("$export_method") != -1) {
  258. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  259. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  260. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  261. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get("application/provisioning_profile_uuid_release")) + "\n";
  262. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  263. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get("application/provisioning_profile_uuid_debug")) + "\n";
  264. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  265. String uuid = p_debug ? p_preset->get("application/provisioning_profile_uuid_debug") : p_preset->get("application/provisioning_profile_uuid_release");
  266. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  267. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  268. strnew += lines[i].replace("$code_sign_identity_debug", p_preset->get("application/code_sign_identity_debug")) + "\n";
  269. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  270. strnew += lines[i].replace("$code_sign_identity_release", p_preset->get("application/code_sign_identity_release")) + "\n";
  271. } else if (lines[i].find("$additional_plist_content") != -1) {
  272. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  273. } else if (lines[i].find("$godot_archs") != -1) {
  274. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  275. } else if (lines[i].find("$linker_flags") != -1) {
  276. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  277. } else if (lines[i].find("$cpp_code") != -1) {
  278. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  279. } else if (lines[i].find("$docs_in_place") != -1) {
  280. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  281. } else if (lines[i].find("$docs_sharing") != -1) {
  282. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  283. } else if (lines[i].find("$access_wifi") != -1) {
  284. bool is_on = p_preset->get("capabilities/access_wifi");
  285. strnew += lines[i].replace("$access_wifi", is_on ? "1" : "0") + "\n";
  286. } else if (lines[i].find("$game_center") != -1) {
  287. bool is_on = p_preset->get("capabilities/game_center");
  288. strnew += lines[i].replace("$game_center", is_on ? "1" : "0") + "\n";
  289. } else if (lines[i].find("$in_app_purchases") != -1) {
  290. bool is_on = p_preset->get("capabilities/in_app_purchases");
  291. strnew += lines[i].replace("$in_app_purchases", is_on ? "1" : "0") + "\n";
  292. } else if (lines[i].find("$push_notifications") != -1) {
  293. bool is_on = p_preset->get("capabilities/push_notifications");
  294. strnew += lines[i].replace("$push_notifications", is_on ? "1" : "0") + "\n";
  295. } else if (lines[i].find("$required_device_capabilities") != -1) {
  296. String capabilities;
  297. // I've removed armv7 as we can run on 64bit only devices
  298. // Note that capabilities listed here are requirements for the app to be installed.
  299. // They don't enable anything.
  300. if ((bool)p_preset->get("capabilities/arkit")) {
  301. capabilities += "<string>arkit</string>\n";
  302. }
  303. if ((bool)p_preset->get("capabilities/game_center")) {
  304. capabilities += "<string>gamekit</string>\n";
  305. }
  306. if ((bool)p_preset->get("capabilities/access_wifi")) {
  307. capabilities += "<string>wifi</string>\n";
  308. }
  309. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  310. } else if (lines[i].find("$interface_orientations") != -1) {
  311. String orientations;
  312. if ((bool)p_preset->get("orientation/portrait")) {
  313. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  314. }
  315. if ((bool)p_preset->get("orientation/landscape_left")) {
  316. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  317. }
  318. if ((bool)p_preset->get("orientation/landscape_right")) {
  319. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  320. }
  321. if ((bool)p_preset->get("orientation/portrait_upside_down")) {
  322. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  323. }
  324. strnew += lines[i].replace("$interface_orientations", orientations);
  325. } else if (lines[i].find("$camera_usage_description") != -1) {
  326. String description = p_preset->get("privacy/camera_usage_description");
  327. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  328. } else if (lines[i].find("$microphone_usage_description") != -1) {
  329. String description = p_preset->get("privacy/microphone_usage_description");
  330. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  331. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  332. String description = p_preset->get("privacy/photolibrary_usage_description");
  333. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  334. } else {
  335. strnew += lines[i] + "\n";
  336. }
  337. }
  338. // !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...
  339. // should apply the same fix in our OSX export.
  340. CharString cs = strnew.utf8();
  341. pfile.resize(cs.size() - 1);
  342. for (int i = 0; i < cs.size() - 1; i++) {
  343. pfile.write[i] = cs[i];
  344. }
  345. }
  346. String EditorExportPlatformIOS::_get_additional_plist_content() {
  347. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  348. String result;
  349. for (int i = 0; i < export_plugins.size(); ++i) {
  350. result += export_plugins[i]->get_ios_plist_content();
  351. }
  352. return result;
  353. }
  354. String EditorExportPlatformIOS::_get_linker_flags() {
  355. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  356. String result;
  357. for (int i = 0; i < export_plugins.size(); ++i) {
  358. String flags = export_plugins[i]->get_ios_linker_flags();
  359. if (flags.length() == 0) continue;
  360. if (result.length() > 0) {
  361. result += ' ';
  362. }
  363. result += flags;
  364. }
  365. // the flags will be enclosed in quotes, so need to escape them
  366. return result.replace("\"", "\\\"");
  367. }
  368. String EditorExportPlatformIOS::_get_cpp_code() {
  369. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  370. String result;
  371. for (int i = 0; i < export_plugins.size(); ++i) {
  372. result += export_plugins[i]->get_ios_cpp_code();
  373. }
  374. return result;
  375. }
  376. struct IconInfo {
  377. const char *preset_key;
  378. const char *idiom;
  379. const char *export_name;
  380. const char *actual_size_side;
  381. const char *scale;
  382. const char *unscaled_size;
  383. bool is_required;
  384. };
  385. static const IconInfo icon_infos[] = {
  386. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "2x", "60x60", true },
  387. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "3x", "40x40", true },
  388. { "required_icons/ipad_76x76", "ipad", "Icon-76.png", "76", "1x", "76x76", false },
  389. { "required_icons/app_store_1024x1024", "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", false },
  390. { "optional_icons/iphone_180x180", "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  391. { "optional_icons/ipad_152x152", "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  392. { "optional_icons/ipad_167x167", "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  393. { "optional_icons/spotlight_40x40", "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  394. { "optional_icons/spotlight_80x80", "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  395. { "optional_icons/spotlight_80x80", "ipad", "Icon-80.png", "80", "2x", "40x40", false }
  396. };
  397. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  398. String json_description = "{\"images\":[";
  399. String sizes;
  400. DirAccess *da = DirAccess::open(p_iconset_dir);
  401. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_iconset_dir + "'.");
  402. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  403. IconInfo info = icon_infos[i];
  404. String icon_path = p_preset->get(info.preset_key);
  405. if (icon_path.length() == 0) {
  406. if (info.is_required) {
  407. ERR_PRINT("Required icon is not specified in the preset");
  408. return ERR_UNCONFIGURED;
  409. }
  410. continue;
  411. }
  412. Error err = da->copy(icon_path, p_iconset_dir + info.export_name);
  413. if (err) {
  414. memdelete(da);
  415. String err_str = String("Failed to export icon: ") + icon_path;
  416. ERR_PRINT(err_str.utf8().get_data());
  417. return err;
  418. }
  419. sizes += String(info.actual_size_side) + "\n";
  420. if (i > 0) {
  421. json_description += ",";
  422. }
  423. json_description += String("{");
  424. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  425. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  426. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  427. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  428. json_description += String("}");
  429. }
  430. json_description += "]}";
  431. memdelete(da);
  432. FileAccess *json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  433. ERR_FAIL_COND_V(!json_file, ERR_CANT_CREATE);
  434. CharString json_utf8 = json_description.utf8();
  435. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  436. memdelete(json_file);
  437. FileAccess *sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  438. ERR_FAIL_COND_V(!sizes_file, ERR_CANT_CREATE);
  439. CharString sizes_utf8 = sizes.utf8();
  440. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  441. memdelete(sizes_file);
  442. return OK;
  443. }
  444. Error EditorExportPlatformIOS::_export_loading_screens(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  445. DirAccess *da = DirAccess::open(p_dest_dir);
  446. ERR_FAIL_COND_V_MSG(!da, ERR_CANT_OPEN, "Cannot open directory '" + p_dest_dir + "'.");
  447. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  448. LoadingScreenInfo info = loading_screen_infos[i];
  449. String loading_screen_file = p_preset->get(info.preset_key);
  450. if (loading_screen_file.size() > 0) {
  451. Error err = da->copy(loading_screen_file, p_dest_dir + info.export_name);
  452. if (err) {
  453. memdelete(da);
  454. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from path '" + loading_screen_file + "'.";
  455. ERR_PRINT(err_str.utf8().get_data());
  456. return err;
  457. }
  458. }
  459. }
  460. memdelete(da);
  461. return OK;
  462. }
  463. Error EditorExportPlatformIOS::_walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata) {
  464. Vector<String> dirs;
  465. String path;
  466. String current_dir = p_da->get_current_dir();
  467. p_da->list_dir_begin();
  468. while ((path = p_da->get_next()).length() != 0) {
  469. if (p_da->current_is_dir()) {
  470. if (path != "." && path != "..") {
  471. dirs.push_back(path);
  472. }
  473. } else {
  474. Error err = p_handler(current_dir.plus_file(path), p_userdata);
  475. if (err) {
  476. p_da->list_dir_end();
  477. return err;
  478. }
  479. }
  480. }
  481. p_da->list_dir_end();
  482. for (int i = 0; i < dirs.size(); ++i) {
  483. String dir = dirs[i];
  484. p_da->change_dir(dir);
  485. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  486. p_da->change_dir("..");
  487. if (err) {
  488. return err;
  489. }
  490. }
  491. return OK;
  492. }
  493. struct CodesignData {
  494. const Ref<EditorExportPreset> &preset;
  495. bool debug;
  496. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  497. preset(p_preset),
  498. debug(p_debug) {
  499. }
  500. };
  501. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  502. if (p_file.ends_with(".dylib")) {
  503. CodesignData *data = (CodesignData *)p_userdata;
  504. print_line(String("Signing ") + p_file);
  505. List<String> codesign_args;
  506. codesign_args.push_back("-f");
  507. codesign_args.push_back("-s");
  508. codesign_args.push_back(data->preset->get(data->debug ? "application/code_sign_identity_debug" : "application/code_sign_identity_release"));
  509. codesign_args.push_back(p_file);
  510. return OS::get_singleton()->execute("codesign", codesign_args, true);
  511. }
  512. return OK;
  513. }
  514. struct PbxId {
  515. private:
  516. static char _hex_char(uint8_t four_bits) {
  517. if (four_bits < 10) {
  518. return ('0' + four_bits);
  519. }
  520. return 'A' + (four_bits - 10);
  521. }
  522. static String _hex_pad(uint32_t num) {
  523. Vector<char> ret;
  524. ret.resize(sizeof(num) * 2);
  525. for (uint64_t i = 0; i < sizeof(num) * 2; ++i) {
  526. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  527. ret.write[i] = _hex_char(four_bits);
  528. }
  529. return String::utf8(ret.ptr(), ret.size());
  530. }
  531. public:
  532. uint32_t high_bits;
  533. uint32_t mid_bits;
  534. uint32_t low_bits;
  535. String str() const {
  536. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  537. }
  538. PbxId &operator++() {
  539. low_bits++;
  540. if (!low_bits) {
  541. mid_bits++;
  542. if (!mid_bits) {
  543. high_bits++;
  544. }
  545. }
  546. return *this;
  547. }
  548. };
  549. struct ExportLibsData {
  550. Vector<String> lib_paths;
  551. String dest_dir;
  552. };
  553. void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  554. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  555. Vector<String> frameworks;
  556. for (int i = 0; i < export_plugins.size(); ++i) {
  557. Vector<String> plugin_frameworks = export_plugins[i]->get_ios_frameworks();
  558. for (int j = 0; j < plugin_frameworks.size(); ++j) {
  559. frameworks.push_back(plugin_frameworks[j]);
  560. }
  561. }
  562. // that is just a random number, we just need Godot IDs not to clash with
  563. // existing IDs in the project.
  564. PbxId current_id = { 0x58938401, 0, 0 };
  565. String pbx_files;
  566. String pbx_frameworks_build;
  567. String pbx_frameworks_refs;
  568. String pbx_resources_build;
  569. String pbx_resources_refs;
  570. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  571. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = $name; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  572. for (int i = 0; i < p_additional_assets.size(); ++i) {
  573. String build_id = (++current_id).str();
  574. String ref_id = (++current_id).str();
  575. const IOSExportAsset &asset = p_additional_assets[i];
  576. String type;
  577. if (asset.exported_path.ends_with(".framework")) {
  578. type = "wrapper.framework";
  579. } else if (asset.exported_path.ends_with(".dylib")) {
  580. type = "compiled.mach-o.dylib";
  581. } else if (asset.exported_path.ends_with(".a")) {
  582. type = "archive.ar";
  583. } else {
  584. type = "file";
  585. }
  586. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  587. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  588. if (pbx_build.length() > 0) {
  589. pbx_build += ",\n";
  590. pbx_refs += ",\n";
  591. }
  592. pbx_build += build_id;
  593. pbx_refs += ref_id;
  594. Dictionary format_dict;
  595. format_dict["build_id"] = build_id;
  596. format_dict["ref_id"] = ref_id;
  597. format_dict["name"] = asset.exported_path.get_file();
  598. format_dict["file_path"] = asset.exported_path;
  599. format_dict["file_type"] = type;
  600. pbx_files += file_info_format.format(format_dict, "$_");
  601. }
  602. // Note, frameworks like gamekit are always included in our project.pbxprof file
  603. // even if turned off in capabilities.
  604. // We do need our ARKit framework
  605. if ((bool)p_preset->get("capabilities/arkit")) {
  606. String build_id = (++current_id).str();
  607. String ref_id = (++current_id).str();
  608. if (pbx_frameworks_build.length() > 0) {
  609. pbx_frameworks_build += ",\n";
  610. pbx_frameworks_refs += ",\n";
  611. }
  612. pbx_frameworks_build += build_id;
  613. pbx_frameworks_refs += ref_id;
  614. Dictionary format_dict;
  615. format_dict["build_id"] = build_id;
  616. format_dict["ref_id"] = ref_id;
  617. format_dict["name"] = "ARKit.framework";
  618. format_dict["file_path"] = "System/Library/Frameworks/ARKit.framework";
  619. format_dict["file_type"] = "wrapper.framework";
  620. pbx_files += file_info_format.format(format_dict, "$_");
  621. }
  622. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  623. str = str.replace("$additional_pbx_files", pbx_files);
  624. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  625. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  626. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  627. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  628. CharString cs = str.utf8();
  629. p_project_data.resize(cs.size() - 1);
  630. for (int i = 0; i < cs.size() - 1; i++) {
  631. p_project_data.write[i] = cs[i];
  632. }
  633. }
  634. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, Vector<IOSExportAsset> &r_exported_assets) {
  635. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  636. ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'.");
  637. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  638. String asset = p_assets[f_idx];
  639. if (!asset.begins_with("res://")) {
  640. // either SDK-builtin or already a part of the export template
  641. IOSExportAsset exported_asset = { asset, p_is_framework };
  642. r_exported_assets.push_back(exported_asset);
  643. } else {
  644. DirAccess *da = DirAccess::create_for_path(asset);
  645. if (!da) {
  646. memdelete(filesystem_da);
  647. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't create directory: " + asset + ".");
  648. }
  649. bool file_exists = da->file_exists(asset);
  650. bool dir_exists = da->dir_exists(asset);
  651. if (!file_exists && !dir_exists) {
  652. memdelete(da);
  653. memdelete(filesystem_da);
  654. return ERR_FILE_NOT_FOUND;
  655. }
  656. String additional_dir = p_is_framework && asset.ends_with(".dylib") ? "/dylibs/" : "/";
  657. String destination_dir = p_out_dir + additional_dir + asset.get_base_dir().replace("res://", "");
  658. if (!filesystem_da->dir_exists(destination_dir)) {
  659. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  660. if (make_dir_err) {
  661. memdelete(da);
  662. memdelete(filesystem_da);
  663. return make_dir_err;
  664. }
  665. }
  666. String destination = destination_dir.plus_file(asset.get_file());
  667. Error err = dir_exists ? da->copy_dir(asset, destination) : da->copy(asset, destination);
  668. memdelete(da);
  669. if (err) {
  670. memdelete(filesystem_da);
  671. return err;
  672. }
  673. IOSExportAsset exported_asset = { destination, p_is_framework };
  674. r_exported_assets.push_back(exported_asset);
  675. }
  676. }
  677. memdelete(filesystem_da);
  678. return OK;
  679. }
  680. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  681. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  682. for (int i = 0; i < export_plugins.size(); i++) {
  683. Vector<String> frameworks = export_plugins[i]->get_ios_frameworks();
  684. Error err = _export_additional_assets(p_out_dir, frameworks, true, r_exported_assets);
  685. ERR_FAIL_COND_V(err, err);
  686. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  687. err = _export_additional_assets(p_out_dir, ios_bundle_files, false, r_exported_assets);
  688. ERR_FAIL_COND_V(err, err);
  689. }
  690. Vector<String> library_paths;
  691. for (int i = 0; i < p_libraries.size(); ++i) {
  692. library_paths.push_back(p_libraries[i].path);
  693. }
  694. Error err = _export_additional_assets(p_out_dir, library_paths, true, r_exported_assets);
  695. ERR_FAIL_COND_V(err, err);
  696. return OK;
  697. }
  698. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) {
  699. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  700. Vector<String> enabled_archs;
  701. for (int i = 0; i < all_archs.size(); ++i) {
  702. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  703. if (is_enabled) {
  704. enabled_archs.push_back(all_archs[i].name);
  705. }
  706. }
  707. return enabled_archs;
  708. }
  709. void EditorExportPlatformIOS::add_module_code(const Ref<EditorExportPreset> &p_preset, EditorExportPlatformIOS::IOSConfigData &p_config_data, const String &p_name, const String &p_fid, const String &p_gid) {
  710. if ((bool)p_preset->get("capabilities/" + p_name)) {
  711. //add module static library
  712. print_line("ADDING MODULE: " + p_name);
  713. p_config_data.modules_buildfile += p_gid + " /* libgodot_" + p_name + "_module.a in Frameworks */ = {isa = PBXBuildFile; fileRef = " + p_fid + " /* libgodot_" + p_name + "_module.a */; };\n\t\t";
  714. p_config_data.modules_fileref += p_fid + " /* libgodot_" + p_name + "_module.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = godot_" + p_name + "_module ; path = \"libgodot_" + p_name + "_module.a\"; sourceTree = \"<group>\"; };\n\t\t";
  715. p_config_data.modules_buildphase += p_gid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t";
  716. p_config_data.modules_buildgrp += p_fid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t";
  717. } else {
  718. //add stub function for disabled module
  719. p_config_data.cpp_code += "void register_" + p_name + "_types() { /*stub*/ };\n";
  720. p_config_data.cpp_code += "void unregister_" + p_name + "_types() { /*stub*/ };\n";
  721. }
  722. }
  723. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  724. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  725. String src_pkg_name;
  726. String dest_dir = p_path.get_base_dir() + "/";
  727. String binary_name = p_path.get_file().get_basename();
  728. EditorProgress ep("export", "Exporting for iOS", 5, true);
  729. String team_id = p_preset->get("application/app_store_team_id");
  730. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  731. if (p_debug)
  732. src_pkg_name = p_preset->get("custom_template/debug");
  733. else
  734. src_pkg_name = p_preset->get("custom_template/release");
  735. if (src_pkg_name == "") {
  736. String err;
  737. src_pkg_name = find_export_template("iphone.zip", &err);
  738. if (src_pkg_name == "") {
  739. EditorNode::add_io_error(err);
  740. return ERR_FILE_NOT_FOUND;
  741. }
  742. }
  743. if (!DirAccess::exists(dest_dir)) {
  744. return ERR_FILE_BAD_PATH;
  745. }
  746. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  747. if (da) {
  748. String current_dir = da->get_current_dir();
  749. // remove leftovers from last export so they don't interfere
  750. // in case some files are no longer needed
  751. if (da->change_dir(dest_dir + binary_name + ".xcodeproj") == OK) {
  752. da->erase_contents_recursive();
  753. }
  754. if (da->change_dir(dest_dir + binary_name) == OK) {
  755. da->erase_contents_recursive();
  756. }
  757. da->change_dir(current_dir);
  758. if (!da->dir_exists(dest_dir + binary_name)) {
  759. Error err = da->make_dir(dest_dir + binary_name);
  760. if (err) {
  761. memdelete(da);
  762. return err;
  763. }
  764. }
  765. memdelete(da);
  766. }
  767. if (ep.step("Making .pck", 0)) {
  768. return ERR_SKIP;
  769. }
  770. String pack_path = dest_dir + binary_name + ".pck";
  771. Vector<SharedObject> libraries;
  772. Error err = save_pack(p_preset, pack_path, &libraries);
  773. if (err)
  774. return err;
  775. if (ep.step("Extracting and configuring Xcode project", 1)) {
  776. return ERR_SKIP;
  777. }
  778. String library_to_use = "libgodot.iphone." + String(p_debug ? "debug" : "release") + ".fat.a";
  779. print_line("Static library: " + library_to_use);
  780. String pkg_name;
  781. if (p_preset->get("application/name") != "")
  782. pkg_name = p_preset->get("application/name"); // app_name
  783. else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "")
  784. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  785. else
  786. pkg_name = "Unnamed";
  787. bool found_library = false;
  788. int total_size = 0;
  789. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  790. Set<String> files_to_parse;
  791. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  792. files_to_parse.insert(project_file);
  793. files_to_parse.insert("godot_ios/export_options.plist");
  794. files_to_parse.insert("godot_ios/dummy.cpp");
  795. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  796. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  797. IOSConfigData config_data = {
  798. pkg_name,
  799. binary_name,
  800. _get_additional_plist_content(),
  801. String(" ").join(_get_preset_architectures(p_preset)),
  802. _get_linker_flags(),
  803. _get_cpp_code(),
  804. "",
  805. "",
  806. "",
  807. ""
  808. };
  809. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  810. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE);
  811. print_line("Unzipping...");
  812. FileAccess *src_f = NULL;
  813. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  814. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  815. if (!src_pkg_zip) {
  816. EditorNode::add_io_error("Could not open export template (not a zip file?):\n" + src_pkg_name);
  817. return ERR_CANT_OPEN;
  818. }
  819. add_module_code(p_preset, config_data, "arkit", "F9B95E6E2391205500AF0000", "F9C95E812391205C00BF0000");
  820. add_module_code(p_preset, config_data, "camera", "F9B95E6E2391205500AF0001", "F9C95E812391205C00BF0001");
  821. //export rest of the files
  822. int ret = unzGoToFirstFile(src_pkg_zip);
  823. Vector<uint8_t> project_file_data;
  824. while (ret == UNZ_OK) {
  825. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  826. bool is_execute = false;
  827. #endif
  828. //get filename
  829. unz_file_info info;
  830. char fname[16384];
  831. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  832. String file = fname;
  833. print_line("READ: " + file);
  834. Vector<uint8_t> data;
  835. data.resize(info.uncompressed_size);
  836. //read
  837. unzOpenCurrentFile(src_pkg_zip);
  838. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  839. unzCloseCurrentFile(src_pkg_zip);
  840. //write
  841. file = file.replace_first("iphone/", "");
  842. if (files_to_parse.has(file)) {
  843. _fix_config_file(p_preset, data, config_data, p_debug);
  844. } else if (file.begins_with("libgodot.iphone")) {
  845. if (file != library_to_use) {
  846. ret = unzGoToNextFile(src_pkg_zip);
  847. continue; //ignore!
  848. }
  849. found_library = true;
  850. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  851. is_execute = true;
  852. #endif
  853. file = "godot_ios.a";
  854. } else if (file.begins_with("libgodot_arkit")) {
  855. if ((bool)p_preset->get("capabilities/arkit") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) {
  856. file = "libgodot_arkit_module.a";
  857. } else {
  858. ret = unzGoToNextFile(src_pkg_zip);
  859. continue; //ignore!
  860. }
  861. } else if (file.begins_with("libgodot_camera")) {
  862. if ((bool)p_preset->get("capabilities/camera") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) {
  863. file = "libgodot_camera_module.a";
  864. } else {
  865. ret = unzGoToNextFile(src_pkg_zip);
  866. continue; //ignore!
  867. }
  868. }
  869. if (file == project_file) {
  870. project_file_data = data;
  871. }
  872. ///@TODO need to parse logo files
  873. if (data.size() > 0) {
  874. file = file.replace("godot_ios", binary_name);
  875. print_line("ADDING: " + file + " size: " + itos(data.size()));
  876. total_size += data.size();
  877. /* write it into our folder structure */
  878. file = dest_dir + file;
  879. /* make sure this folder exists */
  880. String dir_name = file.get_base_dir();
  881. if (!tmp_app_path->dir_exists(dir_name)) {
  882. print_line("Creating " + dir_name);
  883. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  884. if (dir_err) {
  885. ERR_PRINTS("Can't create '" + dir_name + "'.");
  886. unzClose(src_pkg_zip);
  887. memdelete(tmp_app_path);
  888. return ERR_CANT_CREATE;
  889. }
  890. }
  891. /* write the file */
  892. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  893. if (!f) {
  894. ERR_PRINTS("Can't write '" + file + "'.");
  895. unzClose(src_pkg_zip);
  896. memdelete(tmp_app_path);
  897. return ERR_CANT_CREATE;
  898. };
  899. f->store_buffer(data.ptr(), data.size());
  900. f->close();
  901. memdelete(f);
  902. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  903. if (is_execute) {
  904. // we need execute rights on this file
  905. chmod(file.utf8().get_data(), 0755);
  906. }
  907. #endif
  908. }
  909. ret = unzGoToNextFile(src_pkg_zip);
  910. }
  911. /* we're done with our source zip */
  912. unzClose(src_pkg_zip);
  913. if (!found_library) {
  914. ERR_PRINTS("Requested template library '" + library_to_use + "' not found. It might be missing from your template archive.");
  915. memdelete(tmp_app_path);
  916. return ERR_FILE_NOT_FOUND;
  917. }
  918. String iconset_dir = dest_dir + binary_name + "/Images.xcassets/AppIcon.appiconset/";
  919. err = OK;
  920. if (!tmp_app_path->dir_exists(iconset_dir)) {
  921. err = tmp_app_path->make_dir_recursive(iconset_dir);
  922. }
  923. memdelete(tmp_app_path);
  924. if (err)
  925. return err;
  926. err = _export_icons(p_preset, iconset_dir);
  927. if (err)
  928. return err;
  929. err = _export_loading_screens(p_preset, dest_dir + binary_name + "/Images.xcassets/LaunchImage.launchimage/");
  930. if (err)
  931. return err;
  932. print_line("Exporting additional assets");
  933. Vector<IOSExportAsset> assets;
  934. _export_additional_assets(dest_dir + binary_name, libraries, assets);
  935. _add_assets_to_project(p_preset, project_file_data, assets);
  936. String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj";
  937. FileAccess *f = FileAccess::open(project_file_name, FileAccess::WRITE);
  938. if (!f) {
  939. ERR_PRINTS("Can't write '" + project_file_name + "'.");
  940. return ERR_CANT_CREATE;
  941. };
  942. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  943. f->close();
  944. memdelete(f);
  945. #ifdef OSX_ENABLED
  946. if (ep.step("Code-signing dylibs", 2)) {
  947. return ERR_SKIP;
  948. }
  949. DirAccess *dylibs_dir = DirAccess::open(dest_dir + binary_name + "/dylibs");
  950. ERR_FAIL_COND_V(!dylibs_dir, ERR_CANT_OPEN);
  951. CodesignData codesign_data(p_preset, p_debug);
  952. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  953. memdelete(dylibs_dir);
  954. ERR_FAIL_COND_V(err, err);
  955. if (ep.step("Making .xcarchive", 3)) {
  956. return ERR_SKIP;
  957. }
  958. String archive_path = p_path.get_basename() + ".xcarchive";
  959. List<String> archive_args;
  960. archive_args.push_back("-project");
  961. archive_args.push_back(dest_dir + binary_name + ".xcodeproj");
  962. archive_args.push_back("-scheme");
  963. archive_args.push_back(binary_name);
  964. archive_args.push_back("-sdk");
  965. archive_args.push_back("iphoneos");
  966. archive_args.push_back("-configuration");
  967. archive_args.push_back(p_debug ? "Debug" : "Release");
  968. archive_args.push_back("-destination");
  969. archive_args.push_back("generic/platform=iOS");
  970. archive_args.push_back("archive");
  971. archive_args.push_back("-archivePath");
  972. archive_args.push_back(archive_path);
  973. err = OS::get_singleton()->execute("xcodebuild", archive_args, true);
  974. ERR_FAIL_COND_V(err, err);
  975. if (ep.step("Making .ipa", 4)) {
  976. return ERR_SKIP;
  977. }
  978. List<String> export_args;
  979. export_args.push_back("-exportArchive");
  980. export_args.push_back("-archivePath");
  981. export_args.push_back(archive_path);
  982. export_args.push_back("-exportOptionsPlist");
  983. export_args.push_back(dest_dir + binary_name + "/export_options.plist");
  984. export_args.push_back("-allowProvisioningUpdates");
  985. export_args.push_back("-exportPath");
  986. export_args.push_back(dest_dir);
  987. err = OS::get_singleton()->execute("xcodebuild", export_args, true);
  988. ERR_FAIL_COND_V(err, err);
  989. #else
  990. print_line(".ipa can only be built on macOS. Leaving Xcode project without building the package.");
  991. #endif
  992. return OK;
  993. }
  994. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  995. String err;
  996. bool valid = false;
  997. // Look for export templates (first official, and if defined custom templates).
  998. bool dvalid = exists_export_template("iphone.zip", &err);
  999. bool rvalid = dvalid; // Both in the same ZIP.
  1000. if (p_preset->get("custom_template/debug") != "") {
  1001. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1002. if (!dvalid) {
  1003. err += TTR("Custom debug template not found.") + "\n";
  1004. }
  1005. }
  1006. if (p_preset->get("custom_template/release") != "") {
  1007. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1008. if (!rvalid) {
  1009. err += TTR("Custom release template not found.") + "\n";
  1010. }
  1011. }
  1012. valid = dvalid || rvalid;
  1013. r_missing_templates = !valid;
  1014. // Validate the rest of the configuration.
  1015. String team_id = p_preset->get("application/app_store_team_id");
  1016. if (team_id.length() == 0) {
  1017. err += TTR("App Store Team ID not specified - cannot configure the project.") + "\n";
  1018. valid = false;
  1019. }
  1020. String identifier = p_preset->get("application/identifier");
  1021. String pn_err;
  1022. if (!is_package_name_valid(identifier, &pn_err)) {
  1023. err += TTR("Invalid Identifier:") + " " + pn_err + "\n";
  1024. valid = false;
  1025. }
  1026. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  1027. IconInfo info = icon_infos[i];
  1028. String icon_path = p_preset->get(info.preset_key);
  1029. if (icon_path.length() == 0) {
  1030. if (info.is_required) {
  1031. err += TTR("Required icon is not specified in the preset.") + "\n";
  1032. valid = false;
  1033. }
  1034. break;
  1035. }
  1036. }
  1037. String etc_error = test_etc2();
  1038. if (etc_error != String()) {
  1039. valid = false;
  1040. err += etc_error;
  1041. }
  1042. if (!err.empty())
  1043. r_error = err;
  1044. return valid;
  1045. }
  1046. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  1047. Ref<Image> img = memnew(Image(_iphone_logo));
  1048. logo.instance();
  1049. logo->create_from_image(img);
  1050. }
  1051. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  1052. }
  1053. void register_iphone_exporter() {
  1054. Ref<EditorExportPlatformIOS> platform;
  1055. platform.instance();
  1056. EditorExport::get_singleton()->add_export_platform(platform);
  1057. }