2
0

export.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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(const Ref<EditorExportPreset> &p_preset, 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", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the camera"), ""));
  227. 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"), ""));
  228. 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"), ""));
  229. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
  230. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_left"), true));
  231. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_right"), true));
  232. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_upside_down"), true));
  233. 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
  234. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/ipad_76x76", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad
  235. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/app_store_1024x1024", PROPERTY_HINT_FILE, "*.png"), "")); // App Store
  236. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/iphone_180x180", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone with retina HD display
  237. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_152x152", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad with retina display
  238. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/ipad_167x167", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPad Pro
  239. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_40x40", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight
  240. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "optional_icons/spotlight_80x80", PROPERTY_HINT_FILE, "*.png"), "")); // Spotlight on devices with retina display
  241. for (unsigned int i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  242. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, loading_screen_infos[i].preset_key, PROPERTY_HINT_FILE, "*.png"), ""));
  243. }
  244. Vector<ExportArchitecture> architectures = _get_supported_architectures();
  245. for (int i = 0; i < architectures.size(); ++i) {
  246. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architectures/" + architectures[i].name), architectures[i].is_default));
  247. }
  248. }
  249. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug) {
  250. static const String export_method_string[] = {
  251. "app-store",
  252. "development",
  253. "ad-hoc",
  254. "enterprise"
  255. };
  256. String str;
  257. String strnew;
  258. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  259. Vector<String> lines = str.split("\n");
  260. for (int i = 0; i < lines.size(); i++) {
  261. if (lines[i].find("$binary") != -1) {
  262. strnew += lines[i].replace("$binary", p_config.binary_name) + "\n";
  263. } else if (lines[i].find("$name") != -1) {
  264. strnew += lines[i].replace("$name", p_config.pkg_name) + "\n";
  265. } else if (lines[i].find("$info") != -1) {
  266. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  267. } else if (lines[i].find("$identifier") != -1) {
  268. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  269. } else if (lines[i].find("$short_version") != -1) {
  270. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  271. } else if (lines[i].find("$version") != -1) {
  272. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  273. } else if (lines[i].find("$signature") != -1) {
  274. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  275. } else if (lines[i].find("$copyright") != -1) {
  276. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  277. } else if (lines[i].find("$team_id") != -1) {
  278. strnew += lines[i].replace("$team_id", p_preset->get("application/app_store_team_id")) + "\n";
  279. } else if (lines[i].find("$export_method") != -1) {
  280. int export_method = p_preset->get(p_debug ? "application/export_method_debug" : "application/export_method_release");
  281. strnew += lines[i].replace("$export_method", export_method_string[export_method]) + "\n";
  282. } else if (lines[i].find("$provisioning_profile_uuid_release") != -1) {
  283. strnew += lines[i].replace("$provisioning_profile_uuid_release", p_preset->get("application/provisioning_profile_uuid_release")) + "\n";
  284. } else if (lines[i].find("$provisioning_profile_uuid_debug") != -1) {
  285. strnew += lines[i].replace("$provisioning_profile_uuid_debug", p_preset->get("application/provisioning_profile_uuid_debug")) + "\n";
  286. } else if (lines[i].find("$provisioning_profile_uuid") != -1) {
  287. String uuid = p_debug ? p_preset->get("application/provisioning_profile_uuid_debug") : p_preset->get("application/provisioning_profile_uuid_release");
  288. strnew += lines[i].replace("$provisioning_profile_uuid", uuid) + "\n";
  289. } else if (lines[i].find("$code_sign_identity_debug") != -1) {
  290. strnew += lines[i].replace("$code_sign_identity_debug", p_preset->get("application/code_sign_identity_debug")) + "\n";
  291. } else if (lines[i].find("$code_sign_identity_release") != -1) {
  292. strnew += lines[i].replace("$code_sign_identity_release", p_preset->get("application/code_sign_identity_release")) + "\n";
  293. } else if (lines[i].find("$additional_plist_content") != -1) {
  294. strnew += lines[i].replace("$additional_plist_content", p_config.plist_content) + "\n";
  295. } else if (lines[i].find("$godot_archs") != -1) {
  296. strnew += lines[i].replace("$godot_archs", p_config.architectures) + "\n";
  297. } else if (lines[i].find("$linker_flags") != -1) {
  298. strnew += lines[i].replace("$linker_flags", p_config.linker_flags) + "\n";
  299. } else if (lines[i].find("$cpp_code") != -1) {
  300. strnew += lines[i].replace("$cpp_code", p_config.cpp_code) + "\n";
  301. } else if (lines[i].find("$access_wifi") != -1) {
  302. bool is_on = p_preset->get("capabilities/access_wifi");
  303. strnew += lines[i].replace("$access_wifi", is_on ? "1" : "0") + "\n";
  304. } else if (lines[i].find("$game_center") != -1) {
  305. bool is_on = p_preset->get("capabilities/game_center");
  306. strnew += lines[i].replace("$game_center", is_on ? "1" : "0") + "\n";
  307. } else if (lines[i].find("$in_app_purchases") != -1) {
  308. bool is_on = p_preset->get("capabilities/in_app_purchases");
  309. strnew += lines[i].replace("$in_app_purchases", is_on ? "1" : "0") + "\n";
  310. } else if (lines[i].find("$push_notifications") != -1) {
  311. bool is_on = p_preset->get("capabilities/push_notifications");
  312. strnew += lines[i].replace("$push_notifications", is_on ? "1" : "0") + "\n";
  313. } else if (lines[i].find("$required_device_capabilities") != -1) {
  314. String capabilities;
  315. // I've removed armv7 as we can run on 64bit only devices
  316. // Note that capabilities listed here are requirements for the app to be installed.
  317. // They don't enable anything.
  318. if ((bool)p_preset->get("capabilities/arkit")) {
  319. capabilities += "<string>arkit</string>\n";
  320. }
  321. if ((bool)p_preset->get("capabilities/game_center")) {
  322. capabilities += "<string>gamekit</string>\n";
  323. }
  324. if ((bool)p_preset->get("capabilities/access_wifi")) {
  325. capabilities += "<string>wifi</string>\n";
  326. }
  327. strnew += lines[i].replace("$required_device_capabilities", capabilities);
  328. } else if (lines[i].find("$interface_orientations") != -1) {
  329. String orientations;
  330. if ((bool)p_preset->get("orientation/portrait")) {
  331. orientations += "<string>UIInterfaceOrientationPortrait</string>\n";
  332. }
  333. if ((bool)p_preset->get("orientation/landscape_left")) {
  334. orientations += "<string>UIInterfaceOrientationLandscapeLeft</string>\n";
  335. }
  336. if ((bool)p_preset->get("orientation/landscape_right")) {
  337. orientations += "<string>UIInterfaceOrientationLandscapeRight</string>\n";
  338. }
  339. if ((bool)p_preset->get("orientation/portrait_upside_down")) {
  340. orientations += "<string>UIInterfaceOrientationPortraitUpsideDown</string>\n";
  341. }
  342. strnew += lines[i].replace("$interface_orientations", orientations);
  343. } else if (lines[i].find("$camera_usage_description") != -1) {
  344. String description = p_preset->get("privacy/camera_usage_description");
  345. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  346. } else if (lines[i].find("$microphone_usage_description") != -1) {
  347. String description = p_preset->get("privacy/microphone_usage_description");
  348. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  349. } else if (lines[i].find("$photolibrary_usage_description") != -1) {
  350. String description = p_preset->get("privacy/photolibrary_usage_description");
  351. strnew += lines[i].replace("$photolibrary_usage_description", description) + "\n";
  352. } else {
  353. strnew += lines[i] + "\n";
  354. }
  355. }
  356. // !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...
  357. // should apply the same fix in our OSX export.
  358. CharString cs = strnew.utf8();
  359. pfile.resize(cs.size() - 1);
  360. for (int i = 0; i < cs.size() - 1; i++) {
  361. pfile.write[i] = cs[i];
  362. }
  363. }
  364. String EditorExportPlatformIOS::_get_additional_plist_content() {
  365. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  366. String result;
  367. for (int i = 0; i < export_plugins.size(); ++i) {
  368. result += export_plugins[i]->get_ios_plist_content();
  369. }
  370. return result;
  371. }
  372. String EditorExportPlatformIOS::_get_linker_flags() {
  373. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  374. String result;
  375. for (int i = 0; i < export_plugins.size(); ++i) {
  376. String flags = export_plugins[i]->get_ios_linker_flags();
  377. if (flags.length() == 0) continue;
  378. if (result.length() > 0) {
  379. result += ' ';
  380. }
  381. result += flags;
  382. }
  383. // the flags will be enclosed in quotes, so need to escape them
  384. return result.replace("\"", "\\\"");
  385. }
  386. String EditorExportPlatformIOS::_get_cpp_code() {
  387. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  388. String result;
  389. for (int i = 0; i < export_plugins.size(); ++i) {
  390. result += export_plugins[i]->get_ios_cpp_code();
  391. }
  392. return result;
  393. }
  394. struct IconInfo {
  395. const char *preset_key;
  396. const char *idiom;
  397. const char *export_name;
  398. const char *actual_size_side;
  399. const char *scale;
  400. const char *unscaled_size;
  401. bool is_required;
  402. };
  403. static const IconInfo icon_infos[] = {
  404. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "2x", "60x60", true },
  405. { "required_icons/iphone_120x120", "iphone", "Icon-120.png", "120", "3x", "40x40", true },
  406. { "required_icons/ipad_76x76", "ipad", "Icon-76.png", "76", "1x", "76x76", false },
  407. { "required_icons/app_store_1024x1024", "ios-marketing", "Icon-1024.png", "1024", "1x", "1024x1024", false },
  408. { "optional_icons/iphone_180x180", "iphone", "Icon-180.png", "180", "3x", "60x60", false },
  409. { "optional_icons/ipad_152x152", "ipad", "Icon-152.png", "152", "2x", "76x76", false },
  410. { "optional_icons/ipad_167x167", "ipad", "Icon-167.png", "167", "2x", "83.5x83.5", false },
  411. { "optional_icons/spotlight_40x40", "ipad", "Icon-40.png", "40", "1x", "40x40", false },
  412. { "optional_icons/spotlight_80x80", "iphone", "Icon-80.png", "80", "2x", "40x40", false },
  413. { "optional_icons/spotlight_80x80", "ipad", "Icon-80.png", "80", "2x", "40x40", false }
  414. };
  415. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  416. String json_description = "{\"images\":[";
  417. String sizes;
  418. DirAccess *da = DirAccess::open(p_iconset_dir);
  419. ERR_FAIL_COND_V(!da, ERR_CANT_OPEN);
  420. for (unsigned int i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  421. IconInfo info = icon_infos[i];
  422. String icon_path = p_preset->get(info.preset_key);
  423. if (icon_path.length() == 0) {
  424. if (info.is_required) {
  425. ERR_PRINT("Required icon is not specified in the preset");
  426. return ERR_UNCONFIGURED;
  427. }
  428. continue;
  429. }
  430. Error err = da->copy(icon_path, p_iconset_dir + info.export_name);
  431. if (err) {
  432. memdelete(da);
  433. String err_str = String("Failed to export icon: ") + icon_path;
  434. ERR_PRINT(err_str.utf8().get_data());
  435. return err;
  436. }
  437. sizes += String(info.actual_size_side) + "\n";
  438. if (i > 0) {
  439. json_description += ",";
  440. }
  441. json_description += String("{");
  442. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  443. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  444. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  445. json_description += String("\"filename\":") + "\"" + info.export_name + "\"";
  446. json_description += String("}");
  447. }
  448. json_description += "]}";
  449. memdelete(da);
  450. FileAccess *json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  451. ERR_FAIL_COND_V(!json_file, ERR_CANT_CREATE);
  452. CharString json_utf8 = json_description.utf8();
  453. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  454. memdelete(json_file);
  455. FileAccess *sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  456. ERR_FAIL_COND_V(!sizes_file, ERR_CANT_CREATE);
  457. CharString sizes_utf8 = sizes.utf8();
  458. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  459. memdelete(sizes_file);
  460. return OK;
  461. }
  462. Error EditorExportPlatformIOS::_export_loading_screens(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  463. DirAccess *da = DirAccess::open(p_dest_dir);
  464. ERR_FAIL_COND_V(!da, ERR_CANT_OPEN);
  465. for (unsigned int i = 0; i < sizeof(loading_screen_infos) / sizeof(loading_screen_infos[0]); ++i) {
  466. LoadingScreenInfo info = loading_screen_infos[i];
  467. String loading_screen_file = p_preset->get(info.preset_key);
  468. if (loading_screen_file.size() > 0) {
  469. Error err = da->copy(loading_screen_file, p_dest_dir + info.export_name);
  470. if (err) {
  471. memdelete(da);
  472. String err_str = String("Failed to export loading screen (") + info.preset_key + ") from path: " + loading_screen_file;
  473. ERR_PRINT(err_str.utf8().get_data());
  474. return err;
  475. }
  476. }
  477. }
  478. memdelete(da);
  479. return OK;
  480. }
  481. Error EditorExportPlatformIOS::_walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata) {
  482. Vector<String> dirs;
  483. String path;
  484. String current_dir = p_da->get_current_dir();
  485. p_da->list_dir_begin();
  486. while ((path = p_da->get_next()).length() != 0) {
  487. if (p_da->current_is_dir()) {
  488. if (path != "." && path != "..") {
  489. dirs.push_back(path);
  490. }
  491. } else {
  492. Error err = p_handler(current_dir.plus_file(path), p_userdata);
  493. if (err) {
  494. p_da->list_dir_end();
  495. return err;
  496. }
  497. }
  498. }
  499. p_da->list_dir_end();
  500. for (int i = 0; i < dirs.size(); ++i) {
  501. String dir = dirs[i];
  502. p_da->change_dir(dir);
  503. Error err = _walk_dir_recursive(p_da, p_handler, p_userdata);
  504. p_da->change_dir("..");
  505. if (err) {
  506. return err;
  507. }
  508. }
  509. return OK;
  510. }
  511. struct CodesignData {
  512. const Ref<EditorExportPreset> &preset;
  513. bool debug;
  514. CodesignData(const Ref<EditorExportPreset> &p_preset, bool p_debug) :
  515. preset(p_preset),
  516. debug(p_debug) {
  517. }
  518. };
  519. Error EditorExportPlatformIOS::_codesign(String p_file, void *p_userdata) {
  520. if (p_file.ends_with(".dylib")) {
  521. CodesignData *data = (CodesignData *)p_userdata;
  522. print_line(String("Signing ") + p_file);
  523. List<String> codesign_args;
  524. codesign_args.push_back("-f");
  525. codesign_args.push_back("-s");
  526. codesign_args.push_back(data->preset->get(data->debug ? "application/code_sign_identity_debug" : "application/code_sign_identity_release"));
  527. codesign_args.push_back(p_file);
  528. return OS::get_singleton()->execute("codesign", codesign_args, true);
  529. }
  530. return OK;
  531. }
  532. struct PbxId {
  533. private:
  534. static char _hex_char(uint8_t four_bits) {
  535. if (four_bits < 10) {
  536. return ('0' + four_bits);
  537. }
  538. return 'A' + (four_bits - 10);
  539. }
  540. static String _hex_pad(uint32_t num) {
  541. Vector<char> ret;
  542. ret.resize(sizeof(num) * 2);
  543. for (unsigned int i = 0; i < sizeof(num) * 2; ++i) {
  544. uint8_t four_bits = (num >> (sizeof(num) * 8 - (i + 1) * 4)) & 0xF;
  545. ret.write[i] = _hex_char(four_bits);
  546. }
  547. return String::utf8(ret.ptr(), ret.size());
  548. }
  549. public:
  550. uint32_t high_bits;
  551. uint32_t mid_bits;
  552. uint32_t low_bits;
  553. String str() const {
  554. return _hex_pad(high_bits) + _hex_pad(mid_bits) + _hex_pad(low_bits);
  555. }
  556. PbxId &operator++() {
  557. low_bits++;
  558. if (!low_bits) {
  559. mid_bits++;
  560. if (!mid_bits) {
  561. high_bits++;
  562. }
  563. }
  564. return *this;
  565. }
  566. };
  567. struct ExportLibsData {
  568. Vector<String> lib_paths;
  569. String dest_dir;
  570. };
  571. void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets) {
  572. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  573. Vector<String> frameworks;
  574. for (int i = 0; i < export_plugins.size(); ++i) {
  575. Vector<String> plugin_frameworks = export_plugins[i]->get_ios_frameworks();
  576. for (int j = 0; j < plugin_frameworks.size(); ++j) {
  577. frameworks.push_back(plugin_frameworks[j]);
  578. }
  579. }
  580. // that is just a random number, we just need Godot IDs not to clash with
  581. // existing IDs in the project.
  582. PbxId current_id = { 0x58938401, 0, 0 };
  583. String pbx_files;
  584. String pbx_frameworks_build;
  585. String pbx_frameworks_refs;
  586. String pbx_resources_build;
  587. String pbx_resources_refs;
  588. const String file_info_format = String("$build_id = {isa = PBXBuildFile; fileRef = $ref_id; };\n") +
  589. "$ref_id = {isa = PBXFileReference; lastKnownFileType = $file_type; name = $name; path = \"$file_path\"; sourceTree = \"<group>\"; };\n";
  590. for (int i = 0; i < p_additional_assets.size(); ++i) {
  591. String build_id = (++current_id).str();
  592. String ref_id = (++current_id).str();
  593. const IOSExportAsset &asset = p_additional_assets[i];
  594. String type;
  595. if (asset.exported_path.ends_with(".framework")) {
  596. type = "wrapper.framework";
  597. } else if (asset.exported_path.ends_with(".dylib")) {
  598. type = "compiled.mach-o.dylib";
  599. } else if (asset.exported_path.ends_with(".a")) {
  600. type = "archive.ar";
  601. } else {
  602. type = "file";
  603. }
  604. String &pbx_build = asset.is_framework ? pbx_frameworks_build : pbx_resources_build;
  605. String &pbx_refs = asset.is_framework ? pbx_frameworks_refs : pbx_resources_refs;
  606. if (pbx_build.length() > 0) {
  607. pbx_build += ",\n";
  608. pbx_refs += ",\n";
  609. }
  610. pbx_build += build_id;
  611. pbx_refs += ref_id;
  612. Dictionary format_dict;
  613. format_dict["build_id"] = build_id;
  614. format_dict["ref_id"] = ref_id;
  615. format_dict["name"] = asset.exported_path.get_file();
  616. format_dict["file_path"] = asset.exported_path;
  617. format_dict["file_type"] = type;
  618. pbx_files += file_info_format.format(format_dict, "$_");
  619. }
  620. // Note, frameworks like gamekit are always included in our project.pbxprof file
  621. // even if turned off in capabilities.
  622. // We do need our ARKit framework
  623. if ((bool)p_preset->get("capabilities/arkit")) {
  624. String build_id = (++current_id).str();
  625. String ref_id = (++current_id).str();
  626. if (pbx_frameworks_build.length() > 0) {
  627. pbx_frameworks_build += ",\n";
  628. pbx_frameworks_refs += ",\n";
  629. }
  630. pbx_frameworks_build += build_id;
  631. pbx_frameworks_refs += ref_id;
  632. Dictionary format_dict;
  633. format_dict["build_id"] = build_id;
  634. format_dict["ref_id"] = ref_id;
  635. format_dict["name"] = "ARKit.framework";
  636. format_dict["file_path"] = "System/Library/Frameworks/ARKit.framework";
  637. format_dict["file_type"] = "wrapper.framework";
  638. pbx_files += file_info_format.format(format_dict, "$_");
  639. }
  640. String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size());
  641. str = str.replace("$additional_pbx_files", pbx_files);
  642. str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build);
  643. str = str.replace("$additional_pbx_frameworks_refs", pbx_frameworks_refs);
  644. str = str.replace("$additional_pbx_resources_build", pbx_resources_build);
  645. str = str.replace("$additional_pbx_resources_refs", pbx_resources_refs);
  646. CharString cs = str.utf8();
  647. p_project_data.resize(cs.size() - 1);
  648. for (int i = 0; i < cs.size() - 1; i++) {
  649. p_project_data.write[i] = cs[i];
  650. }
  651. }
  652. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, Vector<IOSExportAsset> &r_exported_assets) {
  653. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  654. ERR_FAIL_COND_V(!filesystem_da, ERR_CANT_CREATE);
  655. for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) {
  656. String asset = p_assets[f_idx];
  657. if (!asset.begins_with("res://")) {
  658. // either SDK-builtin or already a part of the export template
  659. IOSExportAsset exported_asset = { asset, p_is_framework };
  660. r_exported_assets.push_back(exported_asset);
  661. } else {
  662. DirAccess *da = DirAccess::create_for_path(asset);
  663. if (!da) {
  664. memdelete(filesystem_da);
  665. ERR_EXPLAIN("Can't create directory: " + asset);
  666. ERR_FAIL_V(ERR_CANT_CREATE);
  667. }
  668. bool file_exists = da->file_exists(asset);
  669. bool dir_exists = da->dir_exists(asset);
  670. if (!file_exists && !dir_exists) {
  671. memdelete(da);
  672. memdelete(filesystem_da);
  673. return ERR_FILE_NOT_FOUND;
  674. }
  675. String additional_dir = p_is_framework && asset.ends_with(".dylib") ? "/dylibs/" : "/";
  676. String destination_dir = p_out_dir + additional_dir + asset.get_base_dir().replace("res://", "");
  677. if (!filesystem_da->dir_exists(destination_dir)) {
  678. Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir);
  679. if (make_dir_err) {
  680. memdelete(da);
  681. memdelete(filesystem_da);
  682. return make_dir_err;
  683. }
  684. }
  685. String destination = destination_dir.plus_file(asset.get_file());
  686. Error err = dir_exists ? da->copy_dir(asset, destination) : da->copy(asset, destination);
  687. memdelete(da);
  688. if (err) {
  689. memdelete(filesystem_da);
  690. return err;
  691. }
  692. IOSExportAsset exported_asset = { destination, p_is_framework };
  693. r_exported_assets.push_back(exported_asset);
  694. }
  695. }
  696. memdelete(filesystem_da);
  697. return OK;
  698. }
  699. Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) {
  700. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  701. for (int i = 0; i < export_plugins.size(); i++) {
  702. Vector<String> frameworks = export_plugins[i]->get_ios_frameworks();
  703. Error err = _export_additional_assets(p_out_dir, frameworks, true, r_exported_assets);
  704. ERR_FAIL_COND_V(err, err);
  705. Vector<String> ios_bundle_files = export_plugins[i]->get_ios_bundle_files();
  706. err = _export_additional_assets(p_out_dir, ios_bundle_files, false, r_exported_assets);
  707. ERR_FAIL_COND_V(err, err);
  708. }
  709. Vector<String> library_paths;
  710. for (int i = 0; i < p_libraries.size(); ++i) {
  711. library_paths.push_back(p_libraries[i].path);
  712. }
  713. Error err = _export_additional_assets(p_out_dir, library_paths, true, r_exported_assets);
  714. ERR_FAIL_COND_V(err, err);
  715. return OK;
  716. }
  717. Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<EditorExportPreset> &p_preset) {
  718. Vector<ExportArchitecture> all_archs = _get_supported_architectures();
  719. Vector<String> enabled_archs;
  720. for (int i = 0; i < all_archs.size(); ++i) {
  721. bool is_enabled = p_preset->get("architectures/" + all_archs[i].name);
  722. if (is_enabled) {
  723. enabled_archs.push_back(all_archs[i].name);
  724. }
  725. }
  726. return enabled_archs;
  727. }
  728. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  729. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  730. String src_pkg_name;
  731. String dest_dir = p_path.get_base_dir() + "/";
  732. String binary_name = p_path.get_file().get_basename();
  733. EditorProgress ep("export", "Exporting for iOS", 5, true);
  734. String team_id = p_preset->get("application/app_store_team_id");
  735. ERR_EXPLAIN("App Store Team ID not specified - cannot configure the project.");
  736. ERR_FAIL_COND_V(team_id.length() == 0, ERR_CANT_OPEN);
  737. if (p_debug)
  738. src_pkg_name = p_preset->get("custom_package/debug");
  739. else
  740. src_pkg_name = p_preset->get("custom_package/release");
  741. if (src_pkg_name == "") {
  742. String err;
  743. src_pkg_name = find_export_template("iphone.zip", &err);
  744. if (src_pkg_name == "") {
  745. EditorNode::add_io_error(err);
  746. return ERR_FILE_NOT_FOUND;
  747. }
  748. }
  749. if (!DirAccess::exists(dest_dir)) {
  750. return ERR_FILE_BAD_PATH;
  751. }
  752. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  753. if (da) {
  754. String current_dir = da->get_current_dir();
  755. // remove leftovers from last export so they don't interfere
  756. // in case some files are no longer needed
  757. if (da->change_dir(dest_dir + binary_name + ".xcodeproj") == OK) {
  758. da->erase_contents_recursive();
  759. }
  760. if (da->change_dir(dest_dir + binary_name) == OK) {
  761. da->erase_contents_recursive();
  762. }
  763. da->change_dir(current_dir);
  764. if (!da->dir_exists(dest_dir + binary_name)) {
  765. Error err = da->make_dir(dest_dir + binary_name);
  766. if (err) {
  767. memdelete(da);
  768. return err;
  769. }
  770. }
  771. memdelete(da);
  772. }
  773. if (ep.step("Making .pck", 0)) {
  774. return ERR_SKIP;
  775. }
  776. String pack_path = dest_dir + binary_name + ".pck";
  777. Vector<SharedObject> libraries;
  778. Error err = save_pack(p_preset, pack_path, &libraries);
  779. if (err)
  780. return err;
  781. if (ep.step("Extracting and configuring Xcode project", 1)) {
  782. return ERR_SKIP;
  783. }
  784. String library_to_use = "libgodot.iphone." + String(p_debug ? "debug" : "release") + ".fat.a";
  785. print_line("Static library: " + library_to_use);
  786. String pkg_name;
  787. if (p_preset->get("application/name") != "")
  788. pkg_name = p_preset->get("application/name"); // app_name
  789. else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "")
  790. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  791. else
  792. pkg_name = "Unnamed";
  793. bool found_library = false;
  794. int total_size = 0;
  795. const String project_file = "godot_ios.xcodeproj/project.pbxproj";
  796. Set<String> files_to_parse;
  797. files_to_parse.insert("godot_ios/godot_ios-Info.plist");
  798. files_to_parse.insert(project_file);
  799. files_to_parse.insert("godot_ios/export_options.plist");
  800. files_to_parse.insert("godot_ios/dummy.cpp");
  801. files_to_parse.insert("godot_ios.xcodeproj/project.xcworkspace/contents.xcworkspacedata");
  802. files_to_parse.insert("godot_ios.xcodeproj/xcshareddata/xcschemes/godot_ios.xcscheme");
  803. IOSConfigData config_data = {
  804. pkg_name,
  805. binary_name,
  806. _get_additional_plist_content(),
  807. String(" ").join(_get_preset_architectures(p_preset)),
  808. _get_linker_flags(),
  809. _get_cpp_code()
  810. };
  811. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  812. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE);
  813. print_line("Unzipping...");
  814. FileAccess *src_f = NULL;
  815. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  816. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  817. if (!src_pkg_zip) {
  818. EditorNode::add_io_error("Could not open export template (not a zip file?):\n" + src_pkg_name);
  819. return ERR_CANT_OPEN;
  820. }
  821. int ret = unzGoToFirstFile(src_pkg_zip);
  822. Vector<uint8_t> project_file_data;
  823. while (ret == UNZ_OK) {
  824. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  825. bool is_execute = false;
  826. #endif
  827. //get filename
  828. unz_file_info info;
  829. char fname[16384];
  830. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  831. String file = fname;
  832. print_line("READ: " + file);
  833. Vector<uint8_t> data;
  834. data.resize(info.uncompressed_size);
  835. //read
  836. unzOpenCurrentFile(src_pkg_zip);
  837. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  838. unzCloseCurrentFile(src_pkg_zip);
  839. //write
  840. file = file.replace_first("iphone/", "");
  841. if (files_to_parse.has(file)) {
  842. _fix_config_file(p_preset, data, config_data, p_debug);
  843. } else if (file.begins_with("libgodot.iphone")) {
  844. if (file != library_to_use) {
  845. ret = unzGoToNextFile(src_pkg_zip);
  846. continue; //ignore!
  847. }
  848. found_library = true;
  849. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  850. is_execute = true;
  851. #endif
  852. file = "godot_ios.a";
  853. }
  854. if (file == project_file) {
  855. project_file_data = data;
  856. }
  857. ///@TODO need to parse logo files
  858. if (data.size() > 0) {
  859. file = file.replace("godot_ios", binary_name);
  860. print_line("ADDING: " + file + " size: " + itos(data.size()));
  861. total_size += data.size();
  862. /* write it into our folder structure */
  863. file = dest_dir + file;
  864. /* make sure this folder exists */
  865. String dir_name = file.get_base_dir();
  866. if (!tmp_app_path->dir_exists(dir_name)) {
  867. print_line("Creating " + dir_name);
  868. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  869. if (dir_err) {
  870. ERR_PRINTS("Can't create '" + dir_name + "'.");
  871. unzClose(src_pkg_zip);
  872. memdelete(tmp_app_path);
  873. return ERR_CANT_CREATE;
  874. }
  875. }
  876. /* write the file */
  877. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  878. if (!f) {
  879. ERR_PRINTS("Can't write '" + file + "'.");
  880. unzClose(src_pkg_zip);
  881. memdelete(tmp_app_path);
  882. return ERR_CANT_CREATE;
  883. };
  884. f->store_buffer(data.ptr(), data.size());
  885. f->close();
  886. memdelete(f);
  887. #if defined(OSX_ENABLED) || defined(X11_ENABLED)
  888. if (is_execute) {
  889. // we need execute rights on this file
  890. chmod(file.utf8().get_data(), 0755);
  891. }
  892. #endif
  893. }
  894. ret = unzGoToNextFile(src_pkg_zip);
  895. }
  896. /* we're done with our source zip */
  897. unzClose(src_pkg_zip);
  898. if (!found_library) {
  899. ERR_PRINTS("Requested template library '" + library_to_use + "' not found. It might be missing from your template archive.");
  900. memdelete(tmp_app_path);
  901. return ERR_FILE_NOT_FOUND;
  902. }
  903. String iconset_dir = dest_dir + binary_name + "/Images.xcassets/AppIcon.appiconset/";
  904. err = OK;
  905. if (!tmp_app_path->dir_exists(iconset_dir)) {
  906. err = tmp_app_path->make_dir_recursive(iconset_dir);
  907. }
  908. memdelete(tmp_app_path);
  909. if (err)
  910. return err;
  911. err = _export_icons(p_preset, iconset_dir);
  912. if (err)
  913. return err;
  914. err = _export_loading_screens(p_preset, dest_dir + binary_name + "/Images.xcassets/LaunchImage.launchimage/");
  915. if (err)
  916. return err;
  917. print_line("Exporting additional assets");
  918. Vector<IOSExportAsset> assets;
  919. _export_additional_assets(dest_dir + binary_name, libraries, assets);
  920. _add_assets_to_project(p_preset, project_file_data, assets);
  921. String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj";
  922. FileAccess *f = FileAccess::open(project_file_name, FileAccess::WRITE);
  923. if (!f) {
  924. ERR_PRINTS("Can't write '" + project_file_name + "'.");
  925. return ERR_CANT_CREATE;
  926. };
  927. f->store_buffer(project_file_data.ptr(), project_file_data.size());
  928. f->close();
  929. memdelete(f);
  930. #ifdef OSX_ENABLED
  931. if (ep.step("Code-signing dylibs", 2)) {
  932. return ERR_SKIP;
  933. }
  934. DirAccess *dylibs_dir = DirAccess::open(dest_dir + binary_name + "/dylibs");
  935. ERR_FAIL_COND_V(!dylibs_dir, ERR_CANT_OPEN);
  936. CodesignData codesign_data(p_preset, p_debug);
  937. err = _walk_dir_recursive(dylibs_dir, _codesign, &codesign_data);
  938. memdelete(dylibs_dir);
  939. ERR_FAIL_COND_V(err, err);
  940. if (ep.step("Making .xcarchive", 3)) {
  941. return ERR_SKIP;
  942. }
  943. String archive_path = p_path.get_basename() + ".xcarchive";
  944. List<String> archive_args;
  945. archive_args.push_back("-project");
  946. archive_args.push_back(dest_dir + binary_name + ".xcodeproj");
  947. archive_args.push_back("-scheme");
  948. archive_args.push_back(binary_name);
  949. archive_args.push_back("-sdk");
  950. archive_args.push_back("iphoneos");
  951. archive_args.push_back("-configuration");
  952. archive_args.push_back(p_debug ? "Debug" : "Release");
  953. archive_args.push_back("-destination");
  954. archive_args.push_back("generic/platform=iOS");
  955. archive_args.push_back("archive");
  956. archive_args.push_back("-archivePath");
  957. archive_args.push_back(archive_path);
  958. err = OS::get_singleton()->execute("xcodebuild", archive_args, true);
  959. ERR_FAIL_COND_V(err, err);
  960. if (ep.step("Making .ipa", 4)) {
  961. return ERR_SKIP;
  962. }
  963. List<String> export_args;
  964. export_args.push_back("-exportArchive");
  965. export_args.push_back("-archivePath");
  966. export_args.push_back(archive_path);
  967. export_args.push_back("-exportOptionsPlist");
  968. export_args.push_back(dest_dir + binary_name + "/export_options.plist");
  969. export_args.push_back("-allowProvisioningUpdates");
  970. export_args.push_back("-exportPath");
  971. export_args.push_back(dest_dir);
  972. err = OS::get_singleton()->execute("xcodebuild", export_args, true);
  973. ERR_FAIL_COND_V(err, err);
  974. #else
  975. print_line(".ipa can only be built on macOS. Leaving Xcode project without building the package.");
  976. #endif
  977. return OK;
  978. }
  979. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  980. String err;
  981. r_missing_templates = find_export_template("iphone.zip") == String();
  982. if (p_preset->get("custom_package/debug") != "") {
  983. if (FileAccess::exists(p_preset->get("custom_package/debug"))) {
  984. r_missing_templates = false;
  985. } else {
  986. err += TTR("Custom debug template not found.") + "\n";
  987. }
  988. }
  989. if (p_preset->get("custom_package/release") != "") {
  990. if (FileAccess::exists(p_preset->get("custom_package/release"))) {
  991. r_missing_templates = false;
  992. } else {
  993. err += TTR("Custom release template not found.") + "\n";
  994. }
  995. }
  996. bool valid = !r_missing_templates;
  997. String team_id = p_preset->get("application/app_store_team_id");
  998. if (team_id.length() == 0) {
  999. err += TTR("App Store Team ID not specified - cannot configure the project.") + "\n";
  1000. valid = false;
  1001. }
  1002. String identifier = p_preset->get("application/identifier");
  1003. String pn_err;
  1004. if (!is_package_name_valid(identifier, &pn_err)) {
  1005. err += TTR("Invalid Identifier:") + " " + pn_err + "\n";
  1006. valid = false;
  1007. }
  1008. for (unsigned int i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  1009. IconInfo info = icon_infos[i];
  1010. String icon_path = p_preset->get(info.preset_key);
  1011. if (icon_path.length() == 0) {
  1012. if (info.is_required) {
  1013. err += TTR("Required icon is not specified in the preset.") + "\n";
  1014. valid = false;
  1015. }
  1016. break;
  1017. }
  1018. }
  1019. String etc_error = test_etc2();
  1020. if (etc_error != String()) {
  1021. valid = false;
  1022. err += etc_error;
  1023. }
  1024. if (!err.empty())
  1025. r_error = err;
  1026. return valid;
  1027. }
  1028. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  1029. Ref<Image> img = memnew(Image(_iphone_logo));
  1030. logo.instance();
  1031. logo->create_from_image(img);
  1032. }
  1033. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  1034. }
  1035. void register_iphone_exporter() {
  1036. Ref<EditorExportPlatformIOS> platform;
  1037. platform.instance();
  1038. EditorExport::get_singleton()->add_export_platform(platform);
  1039. }