export.cpp 49 KB

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