export.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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 = false;
  69. ExportArchitecture() {}
  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 = nullptr) 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<Texture2D> 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 == "Vulkan") {
  138. // FIXME: Review if this is correct.
  139. r_features->push_back("etc2");
  140. }
  141. Vector<String> architectures = _get_preset_architectures(p_preset);
  142. for (int i = 0; i < architectures.size(); ++i) {
  143. r_features->push_back(architectures[i]);
  144. }
  145. }
  146. Vector<EditorExportPlatformIOS::ExportArchitecture> EditorExportPlatformIOS::_get_supported_architectures() {
  147. Vector<ExportArchitecture> archs;
  148. archs.push_back(ExportArchitecture("armv7", false)); // Disabled by default, not included in official templates.
  149. archs.push_back(ExportArchitecture("arm64", true));
  150. return archs;
  151. }
  152. struct LoadingScreenInfo {
  153. const char *preset_key;
  154. const char *export_name;
  155. int width;
  156. int height;
  157. bool rotate;
  158. };
  159. static const LoadingScreenInfo loading_screen_infos[] = {
  160. { "landscape_launch_screens/iphone_2436x1125", "Default-Landscape-X.png", 2436, 1125, false },
  161. { "landscape_launch_screens/iphone_2208x1242", "[email protected]", 2208, 1242, false },
  162. { "landscape_launch_screens/ipad_1024x768", "Default-Landscape.png", 1024, 768, false },
  163. { "landscape_launch_screens/ipad_2048x1536", "[email protected]", 2048, 1536, false },
  164. { "portrait_launch_screens/iphone_640x960", "[email protected]", 640, 960, true },
  165. { "portrait_launch_screens/iphone_640x1136", "[email protected]", 640, 1136, true },
  166. { "portrait_launch_screens/iphone_750x1334", "[email protected]", 750, 1334, true },
  167. { "portrait_launch_screens/iphone_1125x2436", "Default-Portrait-X.png", 1125, 2436, true },
  168. { "portrait_launch_screens/ipad_768x1024", "Default-Portrait.png", 768, 1024, true },
  169. { "portrait_launch_screens/ipad_1536x2048", "[email protected]", 1536, 2048, true },
  170. { "portrait_launch_screens/iphone_1242x2208", "[email protected]", 1242, 2208, true }
  171. };
  172. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) {
  173. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  174. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  175. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_store_team_id"), ""));
  176. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_debug"), ""));
  177. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_debug", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Developer"), "iPhone Developer"));
  178. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_debug", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 1));
  179. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/provisioning_profile_uuid_release"), ""));
  180. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/code_sign_identity_release", PROPERTY_HINT_PLACEHOLDER_TEXT, "iPhone Distribution"), ""));
  181. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_method_release", PROPERTY_HINT_ENUM, "App Store,Development,Ad-Hoc,Enterprise"), 0));
  182. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  183. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  184. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  185. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  186. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  187. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  188. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  189. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/arkit"), false));
  190. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/camera"), false));
  191. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false));
  192. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/game_center"), true));
  193. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/in_app_purchases"), false));
  194. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false));
  195. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false));
  196. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_itunes_sharing"), false));
  197. 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"), ""));
  198. 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"), ""));
  199. 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"), ""));
  200. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
  201. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_left"), true));
  202. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_right"), true));
  203. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_upside_down"), true));
  204. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "icons/generate_missing"), false));
  205. 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
  206. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/ipad_76x76", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad
  207. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/app_store_1024x1024", PROPERTY_HINT_FILE, "*.png"), "")); // App Store
  208. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/iphone_180x180", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone with retina HD display
  209. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_152x152", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad with retina display
  210. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_167x167", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad Pro
  211. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_40x40", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight
  212. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_80x80", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight on devices with retina display
  213. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "launch_screens/generate_missing"), false));
  214. for (uint64_t i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  215. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, loading_screen_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png"), ""));
  216. }
  217. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  218. for (int i = 0; i < architectures.size(); ++i) {
  219. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architectures/" + architectures[i].name), architectures[i].is_default));
  220. }
  221. }
  222. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  223. static const String export_method_string[] = {
  224. "app-store",
  225. "development",
  226. "ad-hoc",
  227. "enterprise"
  228. };
  229. String str;
  230. String strnew;
  231. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  232. Vector<String> lines = str.split("\n");
  233. for (int i = 0; i < lines.size(); i++) {
  234. if (lines[i].find("$binary") != -1) {
  235. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  236. } else if (lines[i].find("$modules_buildfile") != -1) {
  237. strnew += lines[i].replace("$modules_buildfile", p_config.modules_buildfile) + "\n";
  238. } else if (lines[i].find("$modules_fileref") != -1) {
  239. strnew += lines[i].replace("$modules_fileref", p_config.modules_fileref) + "\n";
  240. } else if (lines[i].find("$modules_buildphase") != -1) {
  241. strnew += lines[i].replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
  242. } else if (lines[i].find("$modules_buildgrp") != -1) {
  243. strnew += lines[i].replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
  244. } else if (lines[i].find("$name") != -1) {
  245. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  246. } else if (lines[i].find("$info") != -1) {
  247. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  248. } else if (lines[i].find("$bundle_identifier") != -1) {
  249. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  250. } else if (lines[i].find("$short_version") != -1) {
  251. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  252. } else if (lines[i].find("$version") != -1) {
  253. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  254. } else if (lines[i].find("$signature") != -1) {
  255. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  256. } else if (lines[i].find("$copyright") != -1) {
  257. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  258. } else if (lines[i].find("$team_id") != -1) {
  259. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  260. } else if (lines[i].find("$export_method") != -1) {
  261. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  262. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  263. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  264. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get("application/provisioning_profile_uuid_release")) + "\n";
  265. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  266. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get("application/provisioning_profile_uuid_debug")) + "\n";
  267. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  268. String uuid = p_debug ? p_preset->get("application/provisioning_profile_uuid_debug") : p_preset->get("application/provisioning_profile_uuid_release");
  269. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  270. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  271. strnew += lines[i].replace("$code_sign_identity_debug", p_preset->get("application/code_sign_identity_debug")) + "\n";
  272. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  273. strnew += lines[i].replace("$code_sign_identity_release", p_preset->get("application/code_sign_identity_release")) + "\n";
  274. } else if (lines[i].find("$additional_plist_content") != -1) {
  275. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  276. } else if (lines[i].find("$godot_archs") != -1) {
  277. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  278. } else if (lines[i].find("$linker_flags") != -1) {
  279. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  280. } else if (lines[i].find("$cpp_code") != -1) {
  281. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  282. } else if (lines[i].find("$docs_in_place") != -1) {
  283. strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n";
  284. } else if (lines[i].find("$docs_sharing") != -1) {
  285. strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n";
  286. } else if (lines[i].find("$access_wifi") != -1) {
  287. bool is_on = p_preset->get("capabilities/access_wifi");
  288. strnew += lines[i].replace("$access_wifi", is_on ? "1" : "0") + "\n";
  289. } else if (lines[i].find("$game_center") != -1) {
  290. bool is_on = p_preset->get("capabilities/game_center");
  291. strnew += lines[i].replace("$game_center", is_on ? "1" : "0") + "\n";
  292. } else if (lines[i].find("$in_app_purchases") != -1) {
  293. bool is_on = p_preset->get("capabilities/in_app_purchases");
  294. strnew += lines[i].replace("$in_app_purchases", is_on ? "1" : "0") + "\n";
  295. } else if (lines[i].find("$push_notifications") != -1) {
  296. bool is_on = p_preset->get("capabilities/push_notifications");
  297. strnew += lines[i].replace("$push_notifications", is_on ? "1" : "0") + "\n";
  298. } else if (lines[i].find("$required_device_capabilities") != -1) {
  299. String capabilities;
  300. // I've removed armv7 as we can run on 64bit only devices
  301. // Note that capabilities listed here are requirements for the app to be installed.
  302. // They don't enable anything.
  303. if ((bool)p_preset->get("capabilities/arkit")) {
  304. capabilities += "<string>arkit</string>\n";
  305. }
  306. if ((bool)p_preset->get("capabilities/game_center")) {
  307. capabilities += "<string>gamekit</string>\n";
  308. }
  309. if ((bool)p_preset->get("capabilities/access_wifi")) {
  310. capabilities += "<string>wifi</string>\n";
  311. }
  312. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  313. } else if (lines[i].find("$interface_orientations") != -1) {
  314. String orientations;
  315. if ((bool)p_preset->get("orientation/portrait")) {
  316. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  317. }
  318. if ((bool)p_preset->get("orientation/landscape_left")) {
  319. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  320. }
  321. if ((bool)p_preset->get("orientation/landscape_right")) {
  322. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  323. }
  324. if ((bool)p_preset->get("orientation/portrait_upside_down")) {
  325. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  326. }
  327. strnew += lines[i].replace("$interface_orientations", orientations);
  328. } else if (lines[i].find("$camera_usage_description") != -1) {
  329. String description = p_preset->get("privacy/camera_usage_description");
  330. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  331. } else if (lines[i].find("$microphone_usage_description") != -1) {
  332. String description = p_preset->get("privacy/microphone_usage_description");
  333. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  334. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  335. String description = p_preset->get("privacy/photolibrary_usage_description");
  336. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  337. } else {
  338. strnew += lines[i] + "\n";
  339. }
  340. }
  341. // !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...
  342. // should apply the same fix in our OSX export.
  343. CharString cs = strnew.utf8();
  344. pfile.resize(cs.size() - 1);
  345. for (int i = 0; i < cs.size() - 1; i++) {
  346. pfile.write[i] = cs[i];
  347. }
  348. }
  349. String EditorExportPlatformIOS::_get_additional_plist_content() {
  350. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  351. String result;
  352. for (int i = 0; i < export_plugins.size(); ++i) {
  353. result += export_plugins[i]->get_ios_plist_content();
  354. }
  355. return result;
  356. }
  357. String EditorExportPlatformIOS::_get_linker_flags() {
  358. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  359. String result;
  360. for (int i = 0; i < export_plugins.size(); ++i) {
  361. String flags = export_plugins[i]->get_ios_linker_flags();
  362. if (flags.length() == 0) {
  363. continue;
  364. }
  365. if (result.length() > 0) {
  366. result += ' ';
  367. }
  368. result += flags;
  369. }
  370. // the flags will be enclosed in quotes, so need to escape them
  371. return result.replace("\"", "\\\"");
  372. }
  373. String EditorExportPlatformIOS::_get_cpp_code() {
  374. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  375. String result;
  376. for (int i = 0; i < export_plugins.size(); ++i) {
  377. result += export_plugins[i]->get_ios_cpp_code();
  378. }
  379. return result;
  380. }
  381. void EditorExportPlatformIOS::_blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot) {
  382. ERR_FAIL_COND(p_dst.is_null());
  383. ERR_FAIL_COND(p_src.is_null());
  384. int sw = p_rot ? p_src->get_height() : p_src->get_width();
  385. int sh = p_rot ? p_src->get_width() : p_src->get_height();
  386. int x_pos = (p_dst->get_width() - sw) / 2;
  387. int y_pos = (p_dst->get_height() - sh) / 2;
  388. int xs = (x_pos >= 0) ? 0 : -x_pos;
  389. int ys = (y_pos >= 0) ? 0 : -y_pos;
  390. if (sw + x_pos > p_dst->get_width()) {
  391. sw = p_dst->get_width() - x_pos;
  392. }
  393. if (sh + y_pos > p_dst->get_height()) {
  394. sh = p_dst->get_height() - y_pos;
  395. }
  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. }
  813. err = _export_additional_assets(p_out_dir, project_static_libs, true, r_exported_assets);
  814. ERR_FAIL_COND_V(err, err);
  815. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  816. err = _export_additional_assets(p_out_dir, ios_bundle_files, false, r_exported_assets);
  817. ERR_FAIL_COND_V(err, err);
  818. }
  819. Vector<String> library_paths;
  820. for (int i = 0; i < p_libraries.size(); ++i) {
  821. library_paths.push_back(p_libraries[i].path);
  822. }
  823. Error err = _export_additional_assets(p_out_dir, library_paths, true, r_exported_assets);
  824. ERR_FAIL_COND_V(err, err);
  825. return OK;
  826. }
  827. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) {
  828. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  829. Vector<String> enabled_archs;
  830. for (int i = 0; i < all_archs.size(); ++i) {
  831. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  832. if (is_enabled) {
  833. enabled_archs.push_back(all_archs[i].name);
  834. }
  835. }
  836. return enabled_archs;
  837. }
  838. 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) {
  839. if ((bool)p_preset->get("capabilities/" + p_name)) {
  840. //add module static library
  841. print_line("ADDING MODULE: " + p_name);
  842. 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";
  843. 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";
  844. p_config_data.modules_buildphase += p_gid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t";
  845. p_config_data.modules_buildgrp += p_fid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t";
  846. } else {
  847. //add stub function for disabled module
  848. p_config_data.cpp_code += "void register_" + p_name + "_types() { /*stub*/ };\n";
  849. p_config_data.cpp_code += "void unregister_" + p_name + "_types() { /*stub*/ };\n";
  850. }
  851. }
  852. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  853. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  854. String src_pkg_name;
  855. String dest_dir = p_path.get_base_dir() + "/";
  856. String binary_name = p_path.get_file().get_basename();
  857. EditorProgress ep("export", "Exporting for iOS", 5, true);
  858. String team_id = p_preset->get("application/app_store_team_id");
  859. ERR_FAIL_COND_V_MSG(team_id.length() == 0, ERR_CANT_OPEN, "App Store Team ID not specified - cannot configure the project.");
  860. if (p_debug) {
  861. src_pkg_name = p_preset->get("custom_template/debug");
  862. } else {
  863. src_pkg_name = p_preset->get("custom_template/release");
  864. }
  865. if (src_pkg_name == "") {
  866. String err;
  867. src_pkg_name = find_export_template("iphone.zip", &err);
  868. if (src_pkg_name == "") {
  869. EditorNode::add_io_error(err);
  870. return ERR_FILE_NOT_FOUND;
  871. }
  872. }
  873. if (!DirAccess::exists(dest_dir)) {
  874. return ERR_FILE_BAD_PATH;
  875. }
  876. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  877. if (da) {
  878. String current_dir = da->get_current_dir();
  879. // remove leftovers from last export so they don't interfere
  880. // in case some files are no longer needed
  881. if (da->change_dir(dest_dir + binary_name + ".xcodeproj") == OK) {
  882. da->erase_contents_recursive();
  883. }
  884. if (da->change_dir(dest_dir + binary_name) == OK) {
  885. da->erase_contents_recursive();
  886. }
  887. da->change_dir(current_dir);
  888. if (!da->dir_exists(dest_dir + binary_name)) {
  889. Error err = da->make_dir(dest_dir + binary_name);
  890. if (err) {
  891. memdelete(da);
  892. return err;
  893. }
  894. }
  895. memdelete(da);
  896. }
  897. if (ep.step("Making .pck", 0)) {
  898. return ERR_SKIP;
  899. }
  900. String pack_path = dest_dir + binary_name + ".pck";
  901. Vector<SharedObject> libraries;
  902. Error err = save_pack(p_preset, pack_path, &libraries);
  903. if (err) {
  904. return err;
  905. }
  906. if (ep.step("Extracting and configuring Xcode project", 1)) {
  907. return ERR_SKIP;
  908. }
  909. String library_to_use = "libgodot.iphone." + String(p_debug ? "debug" : "release") + ".fat.a";
  910. print_line("Static library: " + library_to_use);
  911. String pkg_name;
  912. if (p_preset->get("application/name") != "") {
  913. pkg_name = p_preset->get("application/name"); // app_name
  914. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  915. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  916. } else {
  917. pkg_name = "Unnamed";
  918. }
  919. bool found_library = false;
  920. int total_size = 0;
  921. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  922. Set<String> files_to_parse;
  923. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  924. files_to_parse.insert(project_file);
  925. files_to_parse.insert("godot_ios/export_options.plist");
  926. files_to_parse.insert("godot_ios/dummy.cpp");
  927. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  928. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  929. IOSConfigData config_data = {
  930. pkg_name,
  931. binary_name,
  932. _get_additional_plist_content(),
  933. String(" ").join(_get_preset_architectures(p_preset)),
  934. _get_linker_flags(),
  935. _get_cpp_code(),
  936. "",
  937. "",
  938. "",
  939. ""
  940. };
  941. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  942. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE);
  943. print_line("Unzipping...");
  944. FileAccess *src_f = nullptr;
  945. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  946. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  947. if (!src_pkg_zip) {
  948. EditorNode::add_io_error("Could not open export template (not a zip file?):\n" + src_pkg_name);
  949. return ERR_CANT_OPEN;
  950. }
  951. add_module_code(p_preset, config_data, "arkit", "F9B95E6E2391205500AF0000", "F9C95E812391205C00BF0000");
  952. add_module_code(p_preset, config_data, "camera", "F9B95E6E2391205500AF0001", "F9C95E812391205C00BF0001");
  953. //export rest of the files
  954. int ret = unzGoToFirstFile(src_pkg_zip);
  955. Vector<uint8_t> project_file_data;
  956. while (ret == UNZ_OK) {
  957. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  958. bool is_execute = false;
  959. #endif
  960. //get filename
  961. unz_file_info info;
  962. char fname[16384];
  963. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  964. String file = fname;
  965. print_line("READ: " + file);
  966. Vector<uint8_t> data;
  967. data.resize(info.uncompressed_size);
  968. //read
  969. unzOpenCurrentFile(src_pkg_zip);
  970. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  971. unzCloseCurrentFile(src_pkg_zip);
  972. //write
  973. file = file.replace_first("iphone/", "");
  974. if (files_to_parse.has(file)) {
  975. _fix_config_file(p_preset, data, config_data, p_debug);
  976. } else if (file.begins_with("libgodot.iphone")) {
  977. if (file != library_to_use) {
  978. ret = unzGoToNextFile(src_pkg_zip);
  979. continue; //ignore!
  980. }
  981. found_library = true;
  982. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  983. is_execute = true;
  984. #endif
  985. file = "godot_ios.a";
  986. } else if (file.begins_with("libgodot_arkit")) {
  987. if ((bool)p_preset->get("capabilities/arkit") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) {
  988. file = "libgodot_arkit_module.a";
  989. } else {
  990. ret = unzGoToNextFile(src_pkg_zip);
  991. continue; //ignore!
  992. }
  993. } else if (file.begins_with("libgodot_camera")) {
  994. if ((bool)p_preset->get("capabilities/camera") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) {
  995. file = "libgodot_camera_module.a";
  996. } else {
  997. ret = unzGoToNextFile(src_pkg_zip);
  998. continue; //ignore!
  999. }
  1000. }
  1001. if (file == project_file) {
  1002. project_file_data = data;
  1003. }
  1004. ///@TODO need to parse logo files
  1005. if (data.size() > 0) {
  1006. file = file.replace("godot_ios", binary_name);
  1007. print_line("ADDING: " + file + " size: " + itos(data.size()));
  1008. total_size += data.size();
  1009. /* write it into our folder structure */
  1010. file = dest_dir + file;
  1011. /* make sure this folder exists */
  1012. String dir_name = file.get_base_dir();
  1013. if (!tmp_app_path->dir_exists(dir_name)) {
  1014. print_line("Creating " + dir_name);
  1015. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  1016. if (dir_err) {
  1017. ERR_PRINT("Can't create '" + dir_name + "'.");
  1018. unzClose(src_pkg_zip);
  1019. memdelete(tmp_app_path);
  1020. return ERR_CANT_CREATE;
  1021. }
  1022. }
  1023. /* write the file */
  1024. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  1025. if (!f) {
  1026. ERR_PRINT("Can't write '" + file + "'.");
  1027. unzClose(src_pkg_zip);
  1028. memdelete(tmp_app_path);
  1029. return ERR_CANT_CREATE;
  1030. };
  1031. f->store_buffer(data.ptr(), data.size());
  1032. f->close();
  1033. memdelete(f);
  1034. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  1035. if (is_execute) {
  1036. // we need execute rights on this file
  1037. chmod(file.utf8().get_data(), 0755);
  1038. }
  1039. #endif
  1040. }
  1041. ret = unzGoToNextFile(src_pkg_zip);
  1042. }
  1043. /* we're done with our source zip */
  1044. unzClose(src_pkg_zip);
  1045. if (!found_library) {
  1046. ERR_PRINT("Requested template library '" + library_to_use + "' not found. It might be missing from your template archive.");
  1047. memdelete(tmp_app_path);
  1048. return ERR_FILE_NOT_FOUND;
  1049. }
  1050. // Copy project static libs to the project
  1051. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1052. for (int i = 0; i < export_plugins.size(); i++) {
  1053. Vector<String> project_static_libs = export_plugins[i]->get_ios_project_static_libs();
  1054. for (int j = 0; j < project_static_libs.size(); j++) {
  1055. const String &static_lib_path = project_static_libs[j];
  1056. String dest_lib_file_path = dest_dir + static_lib_path.get_file();
  1057. Error lib_copy_err = tmp_app_path->copy(static_lib_path, dest_lib_file_path);
  1058. if (lib_copy_err != OK) {
  1059. ERR_PRINT("Can't copy '" + static_lib_path + "'.");
  1060. memdelete(tmp_app_path);
  1061. return lib_copy_err;
  1062. }
  1063. }
  1064. }
  1065. String iconset_dir = dest_dir + binary_name + "/Images.xcassets/AppIcon.appiconset/";
  1066. err = OK;
  1067. if (!tmp_app_path->dir_exists(iconset_dir)) {
  1068. err = tmp_app_path->make_dir_recursive(iconset_dir);
  1069. }
  1070. memdelete(tmp_app_path);
  1071. if (err) {
  1072. return err;
  1073. }
  1074. err = _export_icons(p_preset, iconset_dir);
  1075. if (err) {
  1076. return err;
  1077. }
  1078. err = _export_loading_screens(p_preset, dest_dir + binary_name + "/Images.xcassets/LaunchImage.launchimage/");
  1079. if (err) {
  1080. return err;
  1081. }
  1082. print_line("Exporting additional assets");
  1083. Vector<IOSExportAsset> assets;
  1084. _export_additional_assets(dest_dir + binary_name, libraries, assets);
  1085. _add_assets_to_project(p_preset, project_file_data, assets);
  1086. String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj";
  1087. FileAccess *f = FileAccess::open(project_file_name, FileAccess::WRITE);
  1088. if (!f) {
  1089. ERR_PRINT("Can't write '" + project_file_name + "'.");
  1090. return ERR_CANT_CREATE;
  1091. };
  1092. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  1093. f->close();
  1094. memdelete(f);
  1095. #ifdef OSX_ENABLED
  1096. if (ep.step("Code-signing dylibs", 2)) {
  1097. return ERR_SKIP;
  1098. }
  1099. DirAccess *dylibs_dir = DirAccess::open(dest_dir + binary_name + "/dylibs");
  1100. ERR_FAIL_COND_V(!dylibs_dir, ERR_CANT_OPEN);
  1101. CodesignData codesign_data(p_preset, p_debug);
  1102. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  1103. memdelete(dylibs_dir);
  1104. ERR_FAIL_COND_V(err, err);
  1105. if (ep.step("Making .xcarchive", 3)) {
  1106. return ERR_SKIP;
  1107. }
  1108. String archive_path = p_path.get_basename() + ".xcarchive";
  1109. List<String> archive_args;
  1110. archive_args.push_back("-project");
  1111. archive_args.push_back(dest_dir + binary_name + ".xcodeproj");
  1112. archive_args.push_back("-scheme");
  1113. archive_args.push_back(binary_name);
  1114. archive_args.push_back("-sdk");
  1115. archive_args.push_back("iphoneos");
  1116. archive_args.push_back("-configuration");
  1117. archive_args.push_back(p_debug ? "Debug" : "Release");
  1118. archive_args.push_back("-destination");
  1119. archive_args.push_back("generic/platform=iOS");
  1120. archive_args.push_back("archive");
  1121. archive_args.push_back("-archivePath");
  1122. archive_args.push_back(archive_path);
  1123. err = OS::get_singleton()->execute("xcodebuild", archive_args, true);
  1124. ERR_FAIL_COND_V(err, err);
  1125. if (ep.step("Making .ipa", 4)) {
  1126. return ERR_SKIP;
  1127. }
  1128. List<String> export_args;
  1129. export_args.push_back("-exportArchive");
  1130. export_args.push_back("-archivePath");
  1131. export_args.push_back(archive_path);
  1132. export_args.push_back("-exportOptionsPlist");
  1133. export_args.push_back(dest_dir + binary_name + "/export_options.plist");
  1134. export_args.push_back("-allowProvisioningUpdates");
  1135. export_args.push_back("-exportPath");
  1136. export_args.push_back(dest_dir);
  1137. err = OS::get_singleton()->execute("xcodebuild", export_args, true);
  1138. ERR_FAIL_COND_V(err, err);
  1139. #else
  1140. print_line(".ipa can only be built on macOS. Leaving Xcode project without building the package.");
  1141. #endif
  1142. return OK;
  1143. }
  1144. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1145. String err;
  1146. bool valid = false;
  1147. // Look for export templates (first official, and if defined custom templates).
  1148. bool dvalid = exists_export_template("iphone.zip", &err);
  1149. bool rvalid = dvalid; // Both in the same ZIP.
  1150. if (p_preset->get("custom_template/debug") != "") {
  1151. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1152. if (!dvalid) {
  1153. err += TTR("Custom debug template not found.") + "\n";
  1154. }
  1155. }
  1156. if (p_preset->get("custom_template/release") != "") {
  1157. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1158. if (!rvalid) {
  1159. err += TTR("Custom release template not found.") + "\n";
  1160. }
  1161. }
  1162. valid = dvalid || rvalid;
  1163. r_missing_templates = !valid;
  1164. // Validate the rest of the configuration.
  1165. String team_id = p_preset->get("application/app_store_team_id");
  1166. if (team_id.length() == 0) {
  1167. err += TTR("App Store Team ID not specified - cannot configure the project.") + "\n";
  1168. valid = false;
  1169. }
  1170. String identifier = p_preset->get("application/bundle_identifier");
  1171. String pn_err;
  1172. if (!is_package_name_valid(identifier, &pn_err)) {
  1173. err += TTR("Invalid Identifier:") + " " + pn_err + "\n";
  1174. valid = false;
  1175. }
  1176. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  1177. IconInfo info = icon_infos[i];
  1178. String icon_path = p_preset->get(info.preset_key);
  1179. if (icon_path.length() == 0) {
  1180. if (info.is_required) {
  1181. err += TTR("Required icon is not specified in the preset.") + "\n";
  1182. valid = false;
  1183. }
  1184. break;
  1185. }
  1186. }
  1187. String etc_error = test_etc2();
  1188. if (etc_error != String()) {
  1189. valid = false;
  1190. err += etc_error;
  1191. }
  1192. if (!err.empty()) {
  1193. r_error = err;
  1194. }
  1195. return valid;
  1196. }
  1197. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  1198. Ref<Image> img = memnew(Image(_iphone_logo));
  1199. logo.instance();
  1200. logo->create_from_image(img);
  1201. }
  1202. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  1203. }
  1204. void register_iphone_exporter() {
  1205. Ref<EditorExportPlatformIOS> platform;
  1206. platform.instance();
  1207. EditorExport::get_singleton()->add_export_platform(platform);
  1208. }