export.cpp 55 KB

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