export_plugin.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*************************************************************************/
  2. /* export_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export_plugin.h"
  31. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  32. if (p_preset->get("texture_format/s3tc")) {
  33. r_features->push_back("s3tc");
  34. }
  35. if (p_preset->get("texture_format/etc")) {
  36. r_features->push_back("etc");
  37. }
  38. if (p_preset->get("texture_format/etc2")) {
  39. r_features->push_back("etc2");
  40. }
  41. r_features->push_back("64");
  42. }
  43. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  44. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  45. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  46. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  47. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  48. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  49. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  50. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  51. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_category", PROPERTY_HINT_ENUM, "Business,Developer-tools,Education,Entertainment,Finance,Games,Action-games,Adventure-games,Arcade-games,Board-games,Card-games,Casino-games,Dice-games,Educational-games,Family-games,Kids-games,Music-games,Puzzle-games,Racing-games,Role-playing-games,Simulation-games,Sports-games,Strategy-games,Trivia-games,Word-games,Graphics-design,Healthcare-fitness,Lifestyle,Medical,Music,News,Photography,Productivity,Reference,Social-networking,Sports,Travel,Utilities,Video,Weather"), "Games"));
  52. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  53. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  54. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  55. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  56. 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"), ""));
  57. 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"), ""));
  58. #ifdef OSX_ENABLED
  59. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), true));
  60. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  61. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  62. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  63. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/replace_existing_signature"), true));
  64. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements/custom_file", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  65. if (!Engine::get_singleton()->has_singleton("GodotSharp")) {
  66. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  67. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_jit_code_execution"), false));
  68. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_unsigned_executable_memory"), false));
  69. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_dyld_environment_variables"), false));
  70. }
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/disable_library_validation"), false));
  72. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/audio_input"), false));
  73. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/camera"), false));
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/location"), false));
  75. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/address_book"), false));
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/calendars"), false));
  77. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/photos_library"), false));
  78. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/apple_events"), false));
  79. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/debugging"), false));
  80. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/enabled"), false));
  81. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_server"), false));
  82. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_client"), false));
  83. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_usb"), false));
  84. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_bluetooth"), false));
  85. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_downloads", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  86. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_pictures", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  87. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_music", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  88. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_movies", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  89. r_options->push_back(ExportOption(PropertyInfo(Variant::ARRAY, "codesign/entitlements/app_sandbox/helper_executables", PROPERTY_HINT_ARRAY_TYPE, itos(Variant::STRING) + "/" + itos(PROPERTY_HINT_GLOBAL_FILE) + ":"), Array()));
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  91. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  92. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_password", PROPERTY_HINT_PLACEHOLDER_TEXT, "Enable two-factor authentication and provide app-specific password"), ""));
  94. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_team_id", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide team ID if your Apple ID belongs to multiple teams"), ""));
  95. #endif
  96. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  97. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  99. }
  100. void _rgba8_to_packbits_encode(int p_ch, int p_size, Vector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  101. int src_len = p_size * p_size;
  102. Vector<uint8_t> result;
  103. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  104. int res_size = 0;
  105. uint8_t buf[128];
  106. int buf_size = 0;
  107. int i = 0;
  108. while (i < src_len) {
  109. uint8_t cur = p_source.ptr()[i * 4 + p_ch];
  110. if (i < src_len - 2) {
  111. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(i + 2) * 4 + p_ch] == cur)) {
  112. if (buf_size > 0) {
  113. result.write[res_size++] = (uint8_t)(buf_size - 1);
  114. memcpy(&result.write[res_size], &buf, buf_size);
  115. res_size += buf_size;
  116. buf_size = 0;
  117. }
  118. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  119. bool hit_lim = true;
  120. for (int j = 3; j <= lim; j++) {
  121. if (p_source.ptr()[(i + j) * 4 + p_ch] != cur) {
  122. hit_lim = false;
  123. i = i + j - 1;
  124. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  125. result.write[res_size++] = cur;
  126. break;
  127. }
  128. }
  129. if (hit_lim) {
  130. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  131. result.write[res_size++] = cur;
  132. i = i + lim;
  133. }
  134. } else {
  135. buf[buf_size++] = cur;
  136. if (buf_size == 128) {
  137. result.write[res_size++] = (uint8_t)(buf_size - 1);
  138. memcpy(&result.write[res_size], &buf, buf_size);
  139. res_size += buf_size;
  140. buf_size = 0;
  141. }
  142. }
  143. } else {
  144. buf[buf_size++] = cur;
  145. result.write[res_size++] = (uint8_t)(buf_size - 1);
  146. memcpy(&result.write[res_size], &buf, buf_size);
  147. res_size += buf_size;
  148. buf_size = 0;
  149. }
  150. i++;
  151. }
  152. int ofs = p_dest.size();
  153. p_dest.resize(p_dest.size() + res_size);
  154. memcpy(&p_dest.write[ofs], result.ptr(), res_size);
  155. }
  156. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  157. Ref<ImageTexture> it = memnew(ImageTexture);
  158. Vector<uint8_t> data;
  159. data.resize(8);
  160. data.write[0] = 'i';
  161. data.write[1] = 'c';
  162. data.write[2] = 'n';
  163. data.write[3] = 's';
  164. struct MacOSIconInfo {
  165. const char *name;
  166. const char *mask_name;
  167. bool is_png;
  168. int size;
  169. };
  170. static const MacOSIconInfo icon_infos[] = {
  171. { "ic10", "", true, 1024 }, //1024×1024 32-bit PNG and 512×512@2x 32-bit "retina" PNG
  172. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  173. { "ic14", "", true, 512 }, //256×256@2x 32-bit "retina" PNG
  174. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  175. { "ic13", "", true, 256 }, //128×128@2x 32-bit "retina" PNG
  176. { "ic07", "", true, 128 }, //128×128 32-bit PNG
  177. { "ic12", "", true, 64 }, //32×32@2× 32-bit "retina" PNG
  178. { "ic11", "", true, 32 }, //16×16@2× 32-bit "retina" PNG
  179. { "il32", "l8mk", false, 32 }, //32×32 24-bit RLE + 8-bit uncompressed mask
  180. { "is32", "s8mk", false, 16 } //16×16 24-bit RLE + 8-bit uncompressed mask
  181. };
  182. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  183. Ref<Image> copy = p_icon; // does this make sense? doesn't this just increase the reference count instead of making a copy? Do we even need a copy?
  184. copy->convert(Image::FORMAT_RGBA8);
  185. copy->resize(icon_infos[i].size, icon_infos[i].size);
  186. if (icon_infos[i].is_png) {
  187. // Encode PNG icon.
  188. it->create_from_image(copy);
  189. String path = EditorPaths::get_singleton()->get_cache_dir().plus_file("icon.png");
  190. ResourceSaver::save(path, it);
  191. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  192. if (!f) {
  193. // Clean up generated file.
  194. DirAccess::remove_file_or_error(path);
  195. ERR_FAIL();
  196. }
  197. int ofs = data.size();
  198. uint64_t len = f->get_length();
  199. data.resize(data.size() + len + 8);
  200. f->get_buffer(&data.write[ofs + 8], len);
  201. memdelete(f);
  202. len += 8;
  203. len = BSWAP32(len);
  204. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  205. encode_uint32(len, &data.write[ofs + 4]);
  206. // Clean up generated file.
  207. DirAccess::remove_file_or_error(path);
  208. } else {
  209. Vector<uint8_t> src_data = copy->get_data();
  210. //encode 24bit RGB RLE icon
  211. {
  212. int ofs = data.size();
  213. data.resize(data.size() + 8);
  214. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  215. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  216. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  217. int len = data.size() - ofs;
  218. len = BSWAP32(len);
  219. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  220. encode_uint32(len, &data.write[ofs + 4]);
  221. }
  222. //encode 8bit mask uncompressed icon
  223. {
  224. int ofs = data.size();
  225. int len = copy->get_width() * copy->get_height();
  226. data.resize(data.size() + len + 8);
  227. for (int j = 0; j < len; j++) {
  228. data.write[ofs + 8 + j] = src_data.ptr()[j * 4 + 3];
  229. }
  230. len += 8;
  231. len = BSWAP32(len);
  232. memcpy(&data.write[ofs], icon_infos[i].mask_name, 4);
  233. encode_uint32(len, &data.write[ofs + 4]);
  234. }
  235. }
  236. }
  237. uint32_t total_len = data.size();
  238. total_len = BSWAP32(total_len);
  239. encode_uint32(total_len, &data.write[4]);
  240. p_data = data;
  241. }
  242. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  243. String str;
  244. String strnew;
  245. str.parse_utf8((const char *)plist.ptr(), plist.size());
  246. Vector<String> lines = str.split("\n");
  247. for (int i = 0; i < lines.size(); i++) {
  248. if (lines[i].find("$binary") != -1) {
  249. strnew += lines[i].replace("$binary", p_binary) + "\n";
  250. } else if (lines[i].find("$name") != -1) {
  251. strnew += lines[i].replace("$name", p_binary) + "\n";
  252. } else if (lines[i].find("$info") != -1) {
  253. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  254. } else if (lines[i].find("$bundle_identifier") != -1) {
  255. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  256. } else if (lines[i].find("$short_version") != -1) {
  257. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  258. } else if (lines[i].find("$version") != -1) {
  259. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  260. } else if (lines[i].find("$signature") != -1) {
  261. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  262. } else if (lines[i].find("$app_category") != -1) {
  263. String cat = p_preset->get("application/app_category");
  264. strnew += lines[i].replace("$app_category", cat.to_lower()) + "\n";
  265. } else if (lines[i].find("$copyright") != -1) {
  266. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  267. } else if (lines[i].find("$highres") != -1) {
  268. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  269. } else if (lines[i].find("$camera_usage_description") != -1) {
  270. String description = p_preset->get("privacy/camera_usage_description");
  271. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  272. } else if (lines[i].find("$microphone_usage_description") != -1) {
  273. String description = p_preset->get("privacy/microphone_usage_description");
  274. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  275. } else {
  276. strnew += lines[i] + "\n";
  277. }
  278. }
  279. CharString cs = strnew.utf8();
  280. plist.resize(cs.size() - 1);
  281. for (int i = 0; i < cs.size() - 1; i++) {
  282. plist.write[i] = cs[i];
  283. }
  284. }
  285. /**
  286. * If we're running the OSX version of the Godot editor we'll:
  287. * - export our application bundle to a temporary folder
  288. * - attempt to code sign it
  289. * - and then wrap it up in a DMG
  290. */
  291. Error EditorExportPlatformOSX::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  292. #ifdef OSX_ENABLED
  293. List<String> args;
  294. args.push_back("altool");
  295. args.push_back("--notarize-app");
  296. args.push_back("--primary-bundle-id");
  297. args.push_back(p_preset->get("application/bundle_identifier"));
  298. args.push_back("--username");
  299. args.push_back(p_preset->get("notarization/apple_id_name"));
  300. args.push_back("--password");
  301. args.push_back(p_preset->get("notarization/apple_id_password"));
  302. args.push_back("--type");
  303. args.push_back("osx");
  304. if (p_preset->get("notarization/apple_team_id")) {
  305. args.push_back("--asc-provider");
  306. args.push_back(p_preset->get("notarization/apple_team_id"));
  307. }
  308. args.push_back("--file");
  309. args.push_back(p_path);
  310. String str;
  311. Error err = OS::get_singleton()->execute("xcrun", args, &str, nullptr, true);
  312. ERR_FAIL_COND_V(err != OK, err);
  313. print_line("altool (" + p_path + "):\n" + str);
  314. if (str.find("RequestUUID") == -1) {
  315. EditorNode::add_io_error("altool: " + str);
  316. return FAILED;
  317. } else {
  318. print_line("Note: The notarization process generally takes less than an hour. When the process is completed, you'll receive an email.");
  319. print_line(" You can check progress manually by opening a Terminal and running the following command:");
  320. print_line(" \"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  321. }
  322. #endif
  323. return OK;
  324. }
  325. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path) {
  326. #ifdef OSX_ENABLED
  327. List<String> args;
  328. if (p_preset->get("codesign/timestamp")) {
  329. args.push_back("--timestamp");
  330. }
  331. if (p_preset->get("codesign/hardened_runtime")) {
  332. args.push_back("--options");
  333. args.push_back("runtime");
  334. }
  335. if (p_path.get_extension() != "dmg") {
  336. args.push_back("--entitlements");
  337. args.push_back(p_ent_path);
  338. }
  339. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  340. for (int i = 0; i < user_args.size(); i++) {
  341. String user_arg = user_args[i].strip_edges();
  342. if (!user_arg.is_empty()) {
  343. args.push_back(user_arg);
  344. }
  345. }
  346. args.push_back("-s");
  347. if (p_preset->get("codesign/identity") == "") {
  348. args.push_back("-");
  349. } else {
  350. args.push_back(p_preset->get("codesign/identity"));
  351. }
  352. args.push_back("-v"); /* provide some more feedback */
  353. if (p_preset->get("codesign/replace_existing_signature")) {
  354. args.push_back("-f");
  355. }
  356. args.push_back(p_path);
  357. String str;
  358. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  359. ERR_FAIL_COND_V(err != OK, err);
  360. print_line("codesign (" + p_path + "):\n" + str);
  361. if (str.find("no identity found") != -1) {
  362. EditorNode::add_io_error("codesign: no identity found");
  363. return FAILED;
  364. }
  365. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  366. EditorNode::add_io_error("codesign: invalid entitlements file");
  367. return FAILED;
  368. }
  369. #endif
  370. return OK;
  371. }
  372. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  373. List<String> args;
  374. if (FileAccess::exists(p_dmg_path)) {
  375. OS::get_singleton()->move_to_trash(p_dmg_path);
  376. }
  377. args.push_back("create");
  378. args.push_back(p_dmg_path);
  379. args.push_back("-volname");
  380. args.push_back(p_pkg_name);
  381. args.push_back("-fs");
  382. args.push_back("HFS+");
  383. args.push_back("-srcfolder");
  384. args.push_back(p_app_path_name);
  385. String str;
  386. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  387. ERR_FAIL_COND_V(err != OK, err);
  388. print_line("hdiutil returned: " + str);
  389. if (str.find("create failed") != -1) {
  390. if (str.find("File exists") != -1) {
  391. EditorNode::add_io_error("hdiutil: create failed - file exists");
  392. } else {
  393. EditorNode::add_io_error("hdiutil: create failed");
  394. }
  395. return FAILED;
  396. }
  397. return OK;
  398. }
  399. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  400. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  401. String src_pkg_name;
  402. EditorProgress ep("export", "Exporting for OSX", 3, true);
  403. if (p_debug) {
  404. src_pkg_name = p_preset->get("custom_template/debug");
  405. } else {
  406. src_pkg_name = p_preset->get("custom_template/release");
  407. }
  408. if (src_pkg_name == "") {
  409. String err;
  410. src_pkg_name = find_export_template("osx.zip", &err);
  411. if (src_pkg_name == "") {
  412. EditorNode::add_io_error(err);
  413. return ERR_FILE_NOT_FOUND;
  414. }
  415. }
  416. if (!DirAccess::exists(p_path.get_base_dir())) {
  417. return ERR_FILE_BAD_PATH;
  418. }
  419. FileAccess *src_f = nullptr;
  420. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  421. if (ep.step("Creating app", 0)) {
  422. return ERR_SKIP;
  423. }
  424. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  425. if (!src_pkg_zip) {
  426. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  427. return ERR_FILE_NOT_FOUND;
  428. }
  429. int ret = unzGoToFirstFile(src_pkg_zip);
  430. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  431. String pkg_name;
  432. if (p_preset->get("application/name") != "") {
  433. pkg_name = p_preset->get("application/name"); // app_name
  434. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  435. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  436. } else {
  437. pkg_name = "Unnamed";
  438. }
  439. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  440. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  441. // Create our application bundle.
  442. String tmp_app_dir_name = pkg_name + ".app";
  443. String tmp_app_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(tmp_app_dir_name);
  444. print_line("Exporting to " + tmp_app_path_name);
  445. Error err = OK;
  446. DirAccessRef tmp_app_dir = DirAccess::create_for_path(tmp_app_path_name);
  447. if (!tmp_app_dir) {
  448. err = ERR_CANT_CREATE;
  449. }
  450. Array helpers = p_preset->get("codesign/entitlements/app_sandbox/helper_executables");
  451. // Create our folder structure.
  452. if (err == OK) {
  453. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  454. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  455. }
  456. if (err == OK) {
  457. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  458. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  459. }
  460. if ((err == OK) && helpers.size() > 0) {
  461. print_line("Creating " + tmp_app_path_name + "/Contents/Helpers");
  462. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Helpers");
  463. }
  464. if (err == OK) {
  465. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  466. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  467. }
  468. // Now process our template.
  469. bool found_binary = false;
  470. Vector<String> dylibs_found;
  471. while (ret == UNZ_OK && err == OK) {
  472. bool is_execute = false;
  473. // Get filename.
  474. unz_file_info info;
  475. char fname[16384];
  476. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  477. String file = fname;
  478. Vector<uint8_t> data;
  479. data.resize(info.uncompressed_size);
  480. // Read.
  481. unzOpenCurrentFile(src_pkg_zip);
  482. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  483. unzCloseCurrentFile(src_pkg_zip);
  484. // Write.
  485. file = file.replace_first("osx_template.app/", "");
  486. if (file == "Contents/Info.plist") {
  487. _fix_plist(p_preset, data, pkg_name);
  488. }
  489. if (file.begins_with("Contents/MacOS/godot_")) {
  490. if (file != "Contents/MacOS/" + binary_to_use) {
  491. ret = unzGoToNextFile(src_pkg_zip);
  492. continue; // skip
  493. }
  494. found_binary = true;
  495. is_execute = true;
  496. file = "Contents/MacOS/" + pkg_name;
  497. }
  498. if (file == "Contents/Resources/icon.icns") {
  499. // See if there is an icon.
  500. String iconpath;
  501. if (p_preset->get("application/icon") != "") {
  502. iconpath = p_preset->get("application/icon");
  503. } else {
  504. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  505. }
  506. if (iconpath != "") {
  507. if (iconpath.get_extension() == "icns") {
  508. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  509. if (icon) {
  510. data.resize(icon->get_length());
  511. icon->get_buffer(&data.write[0], icon->get_length());
  512. icon->close();
  513. memdelete(icon);
  514. }
  515. } else {
  516. Ref<Image> icon;
  517. icon.instantiate();
  518. icon->load(iconpath);
  519. if (!icon->is_empty()) {
  520. _make_icon(icon, data);
  521. }
  522. }
  523. }
  524. }
  525. if (data.size() > 0) {
  526. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  527. if (!p_debug) {
  528. ret = unzGoToNextFile(src_pkg_zip);
  529. continue; // skip
  530. }
  531. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  532. }
  533. if (file.find("/data.mono.osx.64.release/") != -1) {
  534. if (p_debug) {
  535. ret = unzGoToNextFile(src_pkg_zip);
  536. continue; // skip
  537. }
  538. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  539. }
  540. if (file.ends_with(".dylib")) {
  541. dylibs_found.push_back(file);
  542. }
  543. print_line("ADDING: " + file + " size: " + itos(data.size()));
  544. // Write it into our application bundle.
  545. file = tmp_app_path_name.plus_file(file);
  546. if (err == OK) {
  547. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  548. }
  549. if (err == OK) {
  550. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  551. if (f) {
  552. f->store_buffer(data.ptr(), data.size());
  553. f->close();
  554. if (is_execute) {
  555. // chmod with 0755 if the file is executable.
  556. FileAccess::set_unix_permissions(file, 0755);
  557. }
  558. memdelete(f);
  559. } else {
  560. err = ERR_CANT_CREATE;
  561. }
  562. }
  563. }
  564. ret = unzGoToNextFile(src_pkg_zip);
  565. }
  566. // We're done with our source zip.
  567. unzClose(src_pkg_zip);
  568. if (!found_binary) {
  569. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  570. err = ERR_FILE_NOT_FOUND;
  571. }
  572. if (err == OK) {
  573. if (ep.step("Making PKG", 1)) {
  574. return ERR_SKIP;
  575. }
  576. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  577. Vector<SharedObject> shared_objects;
  578. err = save_pack(p_preset, pack_path, &shared_objects);
  579. // See if we can code sign our new package.
  580. bool sign_enabled = p_preset->get("codesign/enable");
  581. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  582. String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + "_helper.entitlements");
  583. if (sign_enabled && (ent_path == "")) {
  584. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  585. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  586. if (ent_f) {
  587. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  588. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  589. ent_f->store_line("<plist version=\"1.0\">");
  590. ent_f->store_line("<dict>");
  591. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  592. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  593. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  594. ent_f->store_line("<true/>");
  595. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  596. ent_f->store_line("<true/>");
  597. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  598. ent_f->store_line("<true/>");
  599. } else {
  600. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  601. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  602. ent_f->store_line("<true/>");
  603. }
  604. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  605. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  606. ent_f->store_line("<true/>");
  607. }
  608. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  609. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  610. ent_f->store_line("<true/>");
  611. }
  612. }
  613. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  614. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  615. ent_f->store_line("<true/>");
  616. }
  617. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  618. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  619. ent_f->store_line("<true/>");
  620. }
  621. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  622. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  623. ent_f->store_line("<true/>");
  624. }
  625. if ((bool)p_preset->get("codesign/entitlements/location")) {
  626. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  627. ent_f->store_line("<true/>");
  628. }
  629. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  630. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  631. ent_f->store_line("<true/>");
  632. }
  633. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  634. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  635. ent_f->store_line("<true/>");
  636. }
  637. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  638. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  639. ent_f->store_line("<true/>");
  640. }
  641. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  642. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  643. ent_f->store_line("<true/>");
  644. }
  645. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  646. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  647. ent_f->store_line("<true/>");
  648. }
  649. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  650. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  651. ent_f->store_line("<true/>");
  652. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  653. ent_f->store_line("<key>com.apple.security.network.server</key>");
  654. ent_f->store_line("<true/>");
  655. }
  656. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  657. ent_f->store_line("<key>com.apple.security.network.client</key>");
  658. ent_f->store_line("<true/>");
  659. }
  660. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  661. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  662. ent_f->store_line("<true/>");
  663. }
  664. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  665. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  666. ent_f->store_line("<true/>");
  667. }
  668. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  669. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  670. ent_f->store_line("<true/>");
  671. }
  672. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  673. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  674. ent_f->store_line("<true/>");
  675. }
  676. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  677. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  678. ent_f->store_line("<true/>");
  679. }
  680. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  681. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  682. ent_f->store_line("<true/>");
  683. }
  684. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  685. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  686. ent_f->store_line("<true/>");
  687. }
  688. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  689. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  690. ent_f->store_line("<true/>");
  691. }
  692. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  693. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  694. ent_f->store_line("<true/>");
  695. }
  696. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  697. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  698. ent_f->store_line("<true/>");
  699. }
  700. }
  701. ent_f->store_line("</dict>");
  702. ent_f->store_line("</plist>");
  703. ent_f->close();
  704. memdelete(ent_f);
  705. } else {
  706. err = ERR_CANT_CREATE;
  707. }
  708. if ((err == OK) && helpers.size() > 0) {
  709. ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
  710. if (ent_f) {
  711. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  712. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  713. ent_f->store_line("<plist version=\"1.0\">");
  714. ent_f->store_line("<dict>");
  715. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  716. ent_f->store_line("<true/>");
  717. ent_f->store_line("<key>com.apple.security.inherit</key>");
  718. ent_f->store_line("<true/>");
  719. ent_f->store_line("</dict>");
  720. ent_f->store_line("</plist>");
  721. ent_f->close();
  722. memdelete(ent_f);
  723. } else {
  724. err = ERR_CANT_CREATE;
  725. }
  726. }
  727. }
  728. if ((err == OK) && helpers.size() > 0) {
  729. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  730. for (int i = 0; i < helpers.size(); i++) {
  731. String hlp_path = helpers[i];
  732. err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
  733. if (err == OK && sign_enabled) {
  734. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path);
  735. }
  736. FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
  737. }
  738. }
  739. if (err == OK) {
  740. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  741. for (int i = 0; i < shared_objects.size(); i++) {
  742. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  743. if (da->dir_exists(src_path)) {
  744. #ifndef UNIX_ENABLED
  745. WARN_PRINT("Relative symlinks are not supported, exported " + src_path.get_file() + " might be broken!");
  746. #endif
  747. print_verbose("export framework: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  748. err = da->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  749. if (err == OK) {
  750. err = da->copy_dir(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), -1, true);
  751. }
  752. } else {
  753. print_verbose("export dylib: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  754. err = da->copy(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  755. }
  756. if (err == OK && sign_enabled) {
  757. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), ent_path);
  758. }
  759. }
  760. }
  761. if (sign_enabled) {
  762. for (int i = 0; i < dylibs_found.size(); i++) {
  763. if (err == OK) {
  764. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  765. }
  766. }
  767. }
  768. if (err == OK && sign_enabled) {
  769. if (ep.step("Code signing bundle", 2)) {
  770. return ERR_SKIP;
  771. }
  772. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  773. }
  774. if (export_format == "dmg") {
  775. // Create a DMG.
  776. if (err == OK) {
  777. if (ep.step("Making DMG", 3)) {
  778. return ERR_SKIP;
  779. }
  780. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  781. }
  782. // Sign DMG.
  783. if (err == OK && sign_enabled) {
  784. if (ep.step("Code signing DMG", 3)) {
  785. return ERR_SKIP;
  786. }
  787. err = _code_sign(p_preset, p_path, ent_path);
  788. }
  789. } else {
  790. // Create ZIP.
  791. if (err == OK) {
  792. if (ep.step("Making ZIP", 3)) {
  793. return ERR_SKIP;
  794. }
  795. if (FileAccess::exists(p_path)) {
  796. OS::get_singleton()->move_to_trash(p_path);
  797. }
  798. FileAccess *dst_f = nullptr;
  799. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  800. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  801. _zip_folder_recursive(zip, EditorPaths::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  802. zipClose(zip, nullptr);
  803. }
  804. }
  805. bool noto_enabled = p_preset->get("notarization/enable");
  806. if (err == OK && noto_enabled) {
  807. if (ep.step("Sending archive for notarization", 4)) {
  808. return ERR_SKIP;
  809. }
  810. err = _notarize(p_preset, p_path);
  811. }
  812. // Clean up temporary entitlements files.
  813. DirAccess::remove_file_or_error(hlp_ent_path);
  814. // Clean up temporary .app dir.
  815. tmp_app_dir->change_dir(tmp_app_path_name);
  816. tmp_app_dir->erase_contents_recursive();
  817. tmp_app_dir->change_dir("..");
  818. tmp_app_dir->remove(tmp_app_dir_name);
  819. }
  820. return err;
  821. }
  822. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  823. String dir = p_root_path.plus_file(p_folder);
  824. DirAccessRef da = DirAccess::open(dir);
  825. da->list_dir_begin();
  826. String f;
  827. while ((f = da->get_next()) != "") {
  828. if (f == "." || f == "..") {
  829. continue;
  830. }
  831. if (da->is_link(f)) {
  832. OS::Time time = OS::get_singleton()->get_time();
  833. OS::Date date = OS::get_singleton()->get_date();
  834. zip_fileinfo zipfi;
  835. zipfi.tmz_date.tm_hour = time.hour;
  836. zipfi.tmz_date.tm_mday = date.day;
  837. zipfi.tmz_date.tm_min = time.minute;
  838. zipfi.tmz_date.tm_mon = date.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
  839. zipfi.tmz_date.tm_sec = time.second;
  840. zipfi.tmz_date.tm_year = date.year;
  841. zipfi.dosDate = 0;
  842. // 0120000: symbolic link type
  843. // 0000755: permissions rwxr-xr-x
  844. // 0000644: permissions rw-r--r--
  845. uint32_t _mode = 0120644;
  846. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  847. zipfi.internal_fa = 0;
  848. zipOpenNewFileInZip4(p_zip,
  849. p_folder.plus_file(f).utf8().get_data(),
  850. &zipfi,
  851. nullptr,
  852. 0,
  853. nullptr,
  854. 0,
  855. nullptr,
  856. Z_DEFLATED,
  857. Z_DEFAULT_COMPRESSION,
  858. 0,
  859. -MAX_WBITS,
  860. DEF_MEM_LEVEL,
  861. Z_DEFAULT_STRATEGY,
  862. nullptr,
  863. 0,
  864. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  865. 0);
  866. String target = da->read_link(f);
  867. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  868. zipCloseFileInZip(p_zip);
  869. } else if (da->current_is_dir()) {
  870. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  871. } else {
  872. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name)) || p_folder.ends_with("Helpers");
  873. OS::Time time = OS::get_singleton()->get_time();
  874. OS::Date date = OS::get_singleton()->get_date();
  875. zip_fileinfo zipfi;
  876. zipfi.tmz_date.tm_hour = time.hour;
  877. zipfi.tmz_date.tm_mday = date.day;
  878. zipfi.tmz_date.tm_min = time.minute;
  879. zipfi.tmz_date.tm_mon = date.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
  880. zipfi.tmz_date.tm_sec = time.second;
  881. zipfi.tmz_date.tm_year = date.year;
  882. zipfi.dosDate = 0;
  883. // 0100000: regular file type
  884. // 0000755: permissions rwxr-xr-x
  885. // 0000644: permissions rw-r--r--
  886. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  887. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  888. zipfi.internal_fa = 0;
  889. zipOpenNewFileInZip4(p_zip,
  890. p_folder.plus_file(f).utf8().get_data(),
  891. &zipfi,
  892. nullptr,
  893. 0,
  894. nullptr,
  895. 0,
  896. nullptr,
  897. Z_DEFLATED,
  898. Z_DEFAULT_COMPRESSION,
  899. 0,
  900. -MAX_WBITS,
  901. DEF_MEM_LEVEL,
  902. Z_DEFAULT_STRATEGY,
  903. nullptr,
  904. 0,
  905. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  906. 0);
  907. FileAccessRef fa = FileAccess::open(dir.plus_file(f), FileAccess::READ);
  908. if (!fa) {
  909. ERR_FAIL_MSG("Can't open file to read from path '" + String(dir.plus_file(f)) + "'.");
  910. }
  911. const int bufsize = 16384;
  912. uint8_t buf[bufsize];
  913. while (true) {
  914. uint64_t got = fa->get_buffer(buf, bufsize);
  915. if (got == 0) {
  916. break;
  917. }
  918. zipWriteInFileInZip(p_zip, buf, got);
  919. }
  920. zipCloseFileInZip(p_zip);
  921. }
  922. }
  923. da->list_dir_end();
  924. }
  925. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  926. String err;
  927. bool valid = false;
  928. // Look for export templates (first official, and if defined custom templates).
  929. bool dvalid = exists_export_template("osx.zip", &err);
  930. bool rvalid = dvalid; // Both in the same ZIP.
  931. if (p_preset->get("custom_template/debug") != "") {
  932. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  933. if (!dvalid) {
  934. err += TTR("Custom debug template not found.") + "\n";
  935. }
  936. }
  937. if (p_preset->get("custom_template/release") != "") {
  938. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  939. if (!rvalid) {
  940. err += TTR("Custom release template not found.") + "\n";
  941. }
  942. }
  943. valid = dvalid || rvalid;
  944. r_missing_templates = !valid;
  945. String identifier = p_preset->get("application/bundle_identifier");
  946. String pn_err;
  947. if (!is_package_name_valid(identifier, &pn_err)) {
  948. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  949. valid = false;
  950. }
  951. bool sign_enabled = p_preset->get("codesign/enable");
  952. bool noto_enabled = p_preset->get("notarization/enable");
  953. if (noto_enabled) {
  954. if (!sign_enabled) {
  955. err += TTR("Notarization: code signing required.") + "\n";
  956. valid = false;
  957. }
  958. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  959. if (!hr_enabled) {
  960. err += TTR("Notarization: hardened runtime required.") + "\n";
  961. valid = false;
  962. }
  963. if (p_preset->get("notarization/apple_id_name") == "") {
  964. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  965. valid = false;
  966. }
  967. if (p_preset->get("notarization/apple_id_password") == "") {
  968. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  969. valid = false;
  970. }
  971. }
  972. if (!err.is_empty()) {
  973. r_error = err;
  974. }
  975. return valid;
  976. }
  977. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  978. Ref<Image> img = memnew(Image(_osx_logo));
  979. logo.instantiate();
  980. logo->create_from_image(img);
  981. }
  982. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  983. }