export_plugin.cpp 73 KB

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