export.cpp 67 KB

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