export.cpp 71 KB

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