export.cpp 44 KB

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