export.cpp 85 KB

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