export_plugin.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. int total_size = 0;
  471. Vector<String> dylibs_found;
  472. while (ret == UNZ_OK && err == OK) {
  473. bool is_execute = false;
  474. // Get filename.
  475. unz_file_info info;
  476. char fname[16384];
  477. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  478. String file = fname;
  479. Vector<uint8_t> data;
  480. data.resize(info.uncompressed_size);
  481. // Read.
  482. unzOpenCurrentFile(src_pkg_zip);
  483. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  484. unzCloseCurrentFile(src_pkg_zip);
  485. // Write.
  486. file = file.replace_first("osx_template.app/", "");
  487. if (file == "Contents/Info.plist") {
  488. _fix_plist(p_preset, data, pkg_name);
  489. }
  490. if (file.begins_with("Contents/MacOS/godot_")) {
  491. if (file != "Contents/MacOS/" + binary_to_use) {
  492. ret = unzGoToNextFile(src_pkg_zip);
  493. continue; // skip
  494. }
  495. found_binary = true;
  496. is_execute = true;
  497. file = "Contents/MacOS/" + pkg_name;
  498. }
  499. if (file == "Contents/Resources/icon.icns") {
  500. // See if there is an icon.
  501. String iconpath;
  502. if (p_preset->get("application/icon") != "") {
  503. iconpath = p_preset->get("application/icon");
  504. } else {
  505. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  506. }
  507. if (iconpath != "") {
  508. if (iconpath.get_extension() == "icns") {
  509. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  510. if (icon) {
  511. data.resize(icon->get_length());
  512. icon->get_buffer(&data.write[0], icon->get_length());
  513. icon->close();
  514. memdelete(icon);
  515. }
  516. } else {
  517. Ref<Image> icon;
  518. icon.instantiate();
  519. icon->load(iconpath);
  520. if (!icon->is_empty()) {
  521. _make_icon(icon, data);
  522. }
  523. }
  524. }
  525. }
  526. if (data.size() > 0) {
  527. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  528. if (!p_debug) {
  529. ret = unzGoToNextFile(src_pkg_zip);
  530. continue; // skip
  531. }
  532. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  533. }
  534. if (file.find("/data.mono.osx.64.release/") != -1) {
  535. if (p_debug) {
  536. ret = unzGoToNextFile(src_pkg_zip);
  537. continue; // skip
  538. }
  539. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  540. }
  541. if (file.ends_with(".dylib")) {
  542. dylibs_found.push_back(file);
  543. }
  544. print_line("ADDING: " + file + " size: " + itos(data.size()));
  545. total_size += data.size();
  546. // Write it into our application bundle.
  547. file = tmp_app_path_name.plus_file(file);
  548. if (err == OK) {
  549. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  550. }
  551. if (err == OK) {
  552. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  553. if (f) {
  554. f->store_buffer(data.ptr(), data.size());
  555. f->close();
  556. if (is_execute) {
  557. // chmod with 0755 if the file is executable.
  558. FileAccess::set_unix_permissions(file, 0755);
  559. }
  560. memdelete(f);
  561. } else {
  562. err = ERR_CANT_CREATE;
  563. }
  564. }
  565. }
  566. ret = unzGoToNextFile(src_pkg_zip);
  567. }
  568. // We're done with our source zip.
  569. unzClose(src_pkg_zip);
  570. if (!found_binary) {
  571. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  572. err = ERR_FILE_NOT_FOUND;
  573. }
  574. if (err == OK) {
  575. if (ep.step("Making PKG", 1)) {
  576. return ERR_SKIP;
  577. }
  578. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  579. Vector<SharedObject> shared_objects;
  580. err = save_pack(p_preset, pack_path, &shared_objects);
  581. // See if we can code sign our new package.
  582. bool sign_enabled = p_preset->get("codesign/enable");
  583. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  584. String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + "_helper.entitlements");
  585. if (sign_enabled && (ent_path == "")) {
  586. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  587. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  588. if (ent_f) {
  589. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  590. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  591. ent_f->store_line("<plist version=\"1.0\">");
  592. ent_f->store_line("<dict>");
  593. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  594. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  595. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  596. ent_f->store_line("<true/>");
  597. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  598. ent_f->store_line("<true/>");
  599. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  600. ent_f->store_line("<true/>");
  601. } else {
  602. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  603. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  604. ent_f->store_line("<true/>");
  605. }
  606. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  607. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  608. ent_f->store_line("<true/>");
  609. }
  610. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  611. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  612. ent_f->store_line("<true/>");
  613. }
  614. }
  615. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  616. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  617. ent_f->store_line("<true/>");
  618. }
  619. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  620. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  621. ent_f->store_line("<true/>");
  622. }
  623. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  624. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  625. ent_f->store_line("<true/>");
  626. }
  627. if ((bool)p_preset->get("codesign/entitlements/location")) {
  628. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  629. ent_f->store_line("<true/>");
  630. }
  631. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  632. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  633. ent_f->store_line("<true/>");
  634. }
  635. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  636. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  637. ent_f->store_line("<true/>");
  638. }
  639. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  640. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  641. ent_f->store_line("<true/>");
  642. }
  643. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  644. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  645. ent_f->store_line("<true/>");
  646. }
  647. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  648. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  649. ent_f->store_line("<true/>");
  650. }
  651. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  652. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  653. ent_f->store_line("<true/>");
  654. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  655. ent_f->store_line("<key>com.apple.security.network.server</key>");
  656. ent_f->store_line("<true/>");
  657. }
  658. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  659. ent_f->store_line("<key>com.apple.security.network.client</key>");
  660. ent_f->store_line("<true/>");
  661. }
  662. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  663. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  664. ent_f->store_line("<true/>");
  665. }
  666. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  667. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  668. ent_f->store_line("<true/>");
  669. }
  670. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  671. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  672. ent_f->store_line("<true/>");
  673. }
  674. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  675. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  676. ent_f->store_line("<true/>");
  677. }
  678. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  679. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  680. ent_f->store_line("<true/>");
  681. }
  682. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  683. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  684. ent_f->store_line("<true/>");
  685. }
  686. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  687. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  688. ent_f->store_line("<true/>");
  689. }
  690. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  691. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  692. ent_f->store_line("<true/>");
  693. }
  694. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  695. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  696. ent_f->store_line("<true/>");
  697. }
  698. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  699. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  700. ent_f->store_line("<true/>");
  701. }
  702. }
  703. ent_f->store_line("</dict>");
  704. ent_f->store_line("</plist>");
  705. ent_f->close();
  706. memdelete(ent_f);
  707. } else {
  708. err = ERR_CANT_CREATE;
  709. }
  710. if ((err == OK) && helpers.size() > 0) {
  711. ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
  712. if (ent_f) {
  713. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  714. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  715. ent_f->store_line("<plist version=\"1.0\">");
  716. ent_f->store_line("<dict>");
  717. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  718. ent_f->store_line("<true/>");
  719. ent_f->store_line("<key>com.apple.security.inherit</key>");
  720. ent_f->store_line("<true/>");
  721. ent_f->store_line("</dict>");
  722. ent_f->store_line("</plist>");
  723. ent_f->close();
  724. memdelete(ent_f);
  725. } else {
  726. err = ERR_CANT_CREATE;
  727. }
  728. }
  729. }
  730. if ((err == OK) && helpers.size() > 0) {
  731. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  732. for (int i = 0; i < helpers.size(); i++) {
  733. String hlp_path = helpers[i];
  734. err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
  735. if (err == OK && sign_enabled) {
  736. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path);
  737. }
  738. FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
  739. }
  740. }
  741. if (err == OK) {
  742. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  743. for (int i = 0; i < shared_objects.size(); i++) {
  744. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  745. if (da->dir_exists(src_path)) {
  746. #ifndef UNIX_ENABLED
  747. WARN_PRINT("Relative symlinks are not supported, exported " + src_path.get_file() + " might be broken!");
  748. #endif
  749. print_verbose("export framework: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  750. err = da->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  751. if (err == OK) {
  752. err = da->copy_dir(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), -1, true);
  753. }
  754. } else {
  755. print_verbose("export dylib: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  756. err = da->copy(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  757. }
  758. if (err == OK && sign_enabled) {
  759. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), ent_path);
  760. }
  761. }
  762. }
  763. if (sign_enabled) {
  764. for (int i = 0; i < dylibs_found.size(); i++) {
  765. if (err == OK) {
  766. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  767. }
  768. }
  769. }
  770. if (err == OK && sign_enabled) {
  771. if (ep.step("Code signing bundle", 2)) {
  772. return ERR_SKIP;
  773. }
  774. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  775. }
  776. if (export_format == "dmg") {
  777. // Create a DMG.
  778. if (err == OK) {
  779. if (ep.step("Making DMG", 3)) {
  780. return ERR_SKIP;
  781. }
  782. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  783. }
  784. // Sign DMG.
  785. if (err == OK && sign_enabled) {
  786. if (ep.step("Code signing DMG", 3)) {
  787. return ERR_SKIP;
  788. }
  789. err = _code_sign(p_preset, p_path, ent_path);
  790. }
  791. } else {
  792. // Create ZIP.
  793. if (err == OK) {
  794. if (ep.step("Making ZIP", 3)) {
  795. return ERR_SKIP;
  796. }
  797. if (FileAccess::exists(p_path)) {
  798. OS::get_singleton()->move_to_trash(p_path);
  799. }
  800. FileAccess *dst_f = nullptr;
  801. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  802. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  803. _zip_folder_recursive(zip, EditorPaths::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  804. zipClose(zip, nullptr);
  805. }
  806. }
  807. bool noto_enabled = p_preset->get("notarization/enable");
  808. if (err == OK && noto_enabled) {
  809. if (ep.step("Sending archive for notarization", 4)) {
  810. return ERR_SKIP;
  811. }
  812. err = _notarize(p_preset, p_path);
  813. }
  814. // Clean up temporary entitlements files.
  815. DirAccess::remove_file_or_error(hlp_ent_path);
  816. // Clean up temporary .app dir.
  817. tmp_app_dir->change_dir(tmp_app_path_name);
  818. tmp_app_dir->erase_contents_recursive();
  819. tmp_app_dir->change_dir("..");
  820. tmp_app_dir->remove(tmp_app_dir_name);
  821. }
  822. return err;
  823. }
  824. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  825. String dir = p_root_path.plus_file(p_folder);
  826. DirAccessRef da = DirAccess::open(dir);
  827. da->list_dir_begin();
  828. String f;
  829. while ((f = da->get_next()) != "") {
  830. if (f == "." || f == "..") {
  831. continue;
  832. }
  833. if (da->is_link(f)) {
  834. OS::Time time = OS::get_singleton()->get_time();
  835. OS::Date date = OS::get_singleton()->get_date();
  836. zip_fileinfo zipfi;
  837. zipfi.tmz_date.tm_hour = time.hour;
  838. zipfi.tmz_date.tm_mday = date.day;
  839. zipfi.tmz_date.tm_min = time.minute;
  840. 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/
  841. zipfi.tmz_date.tm_sec = time.second;
  842. zipfi.tmz_date.tm_year = date.year;
  843. zipfi.dosDate = 0;
  844. // 0120000: symbolic link type
  845. // 0000755: permissions rwxr-xr-x
  846. // 0000644: permissions rw-r--r--
  847. uint32_t _mode = 0120644;
  848. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  849. zipfi.internal_fa = 0;
  850. zipOpenNewFileInZip4(p_zip,
  851. p_folder.plus_file(f).utf8().get_data(),
  852. &zipfi,
  853. nullptr,
  854. 0,
  855. nullptr,
  856. 0,
  857. nullptr,
  858. Z_DEFLATED,
  859. Z_DEFAULT_COMPRESSION,
  860. 0,
  861. -MAX_WBITS,
  862. DEF_MEM_LEVEL,
  863. Z_DEFAULT_STRATEGY,
  864. nullptr,
  865. 0,
  866. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  867. 0);
  868. String target = da->read_link(f);
  869. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  870. zipCloseFileInZip(p_zip);
  871. } else if (da->current_is_dir()) {
  872. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  873. } else {
  874. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name)) || p_folder.ends_with("Helpers");
  875. OS::Time time = OS::get_singleton()->get_time();
  876. OS::Date date = OS::get_singleton()->get_date();
  877. zip_fileinfo zipfi;
  878. zipfi.tmz_date.tm_hour = time.hour;
  879. zipfi.tmz_date.tm_mday = date.day;
  880. zipfi.tmz_date.tm_min = time.minute;
  881. 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/
  882. zipfi.tmz_date.tm_sec = time.second;
  883. zipfi.tmz_date.tm_year = date.year;
  884. zipfi.dosDate = 0;
  885. // 0100000: regular file type
  886. // 0000755: permissions rwxr-xr-x
  887. // 0000644: permissions rw-r--r--
  888. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  889. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  890. zipfi.internal_fa = 0;
  891. zipOpenNewFileInZip4(p_zip,
  892. p_folder.plus_file(f).utf8().get_data(),
  893. &zipfi,
  894. nullptr,
  895. 0,
  896. nullptr,
  897. 0,
  898. nullptr,
  899. Z_DEFLATED,
  900. Z_DEFAULT_COMPRESSION,
  901. 0,
  902. -MAX_WBITS,
  903. DEF_MEM_LEVEL,
  904. Z_DEFAULT_STRATEGY,
  905. nullptr,
  906. 0,
  907. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  908. 0);
  909. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  910. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  911. zipCloseFileInZip(p_zip);
  912. }
  913. }
  914. da->list_dir_end();
  915. }
  916. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  917. String err;
  918. bool valid = false;
  919. // Look for export templates (first official, and if defined custom templates).
  920. bool dvalid = exists_export_template("osx.zip", &err);
  921. bool rvalid = dvalid; // Both in the same ZIP.
  922. if (p_preset->get("custom_template/debug") != "") {
  923. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  924. if (!dvalid) {
  925. err += TTR("Custom debug template not found.") + "\n";
  926. }
  927. }
  928. if (p_preset->get("custom_template/release") != "") {
  929. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  930. if (!rvalid) {
  931. err += TTR("Custom release template not found.") + "\n";
  932. }
  933. }
  934. valid = dvalid || rvalid;
  935. r_missing_templates = !valid;
  936. String identifier = p_preset->get("application/bundle_identifier");
  937. String pn_err;
  938. if (!is_package_name_valid(identifier, &pn_err)) {
  939. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  940. valid = false;
  941. }
  942. bool sign_enabled = p_preset->get("codesign/enable");
  943. bool noto_enabled = p_preset->get("notarization/enable");
  944. if (noto_enabled) {
  945. if (!sign_enabled) {
  946. err += TTR("Notarization: code signing required.") + "\n";
  947. valid = false;
  948. }
  949. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  950. if (!hr_enabled) {
  951. err += TTR("Notarization: hardened runtime required.") + "\n";
  952. valid = false;
  953. }
  954. if (p_preset->get("notarization/apple_id_name") == "") {
  955. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  956. valid = false;
  957. }
  958. if (p_preset->get("notarization/apple_id_password") == "") {
  959. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  960. valid = false;
  961. }
  962. }
  963. if (!err.is_empty()) {
  964. r_error = err;
  965. }
  966. return valid;
  967. }
  968. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  969. Ref<Image> img = memnew(Image(_osx_logo));
  970. logo.instantiate();
  971. logo->create_from_image(img);
  972. }
  973. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  974. }