export_plugin.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*************************************************************************/
  2. /* export_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export_plugin.h"
  31. 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. bool ad_hoc = (p_preset->get("codesign/identity") == "" || p_preset->get("codesign/identity") == "-");
  329. if (p_preset->get("codesign/timestamp")) {
  330. if (ad_hoc) {
  331. WARN_PRINT("Timestamping is not compatible with ad-hoc signature, and was disabled!");
  332. } else {
  333. args.push_back("--timestamp");
  334. }
  335. }
  336. if (p_preset->get("codesign/hardened_runtime")) {
  337. if (ad_hoc) {
  338. WARN_PRINT("Hardened Runtime is not compatible with ad-hoc signature, and was disabled!");
  339. } else {
  340. args.push_back("--options");
  341. args.push_back("runtime");
  342. }
  343. }
  344. if (p_path.get_extension() != "dmg") {
  345. args.push_back("--entitlements");
  346. args.push_back(p_ent_path);
  347. }
  348. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  349. for (int i = 0; i < user_args.size(); i++) {
  350. String user_arg = user_args[i].strip_edges();
  351. if (!user_arg.is_empty()) {
  352. args.push_back(user_arg);
  353. }
  354. }
  355. args.push_back("-s");
  356. if (ad_hoc) {
  357. args.push_back("-");
  358. } else {
  359. args.push_back(p_preset->get("codesign/identity"));
  360. }
  361. args.push_back("-v"); /* provide some more feedback */
  362. if (p_preset->get("codesign/replace_existing_signature")) {
  363. args.push_back("-f");
  364. }
  365. args.push_back(p_path);
  366. String str;
  367. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  368. ERR_FAIL_COND_V(err != OK, err);
  369. print_line("codesign (" + p_path + "):\n" + str);
  370. if (str.find("no identity found") != -1) {
  371. EditorNode::add_io_error("codesign: no identity found");
  372. return FAILED;
  373. }
  374. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  375. EditorNode::add_io_error("codesign: invalid entitlements file");
  376. return FAILED;
  377. }
  378. #endif
  379. return OK;
  380. }
  381. Error EditorExportPlatformOSX::_code_sign_directory(const Ref<EditorExportPreset> &p_preset, const String &p_path,
  382. const String &p_ent_path, bool p_should_error_on_non_code) {
  383. #ifdef OSX_ENABLED
  384. static Vector<String> extensions_to_sign;
  385. if (extensions_to_sign.is_empty()) {
  386. extensions_to_sign.push_back("dylib");
  387. extensions_to_sign.push_back("framework");
  388. }
  389. Error dir_access_error;
  390. DirAccessRef dir_access{ DirAccess::open(p_path, &dir_access_error) };
  391. if (dir_access_error != OK) {
  392. return dir_access_error;
  393. }
  394. dir_access->list_dir_begin();
  395. String current_file{ dir_access->get_next() };
  396. while (!current_file.is_empty()) {
  397. String current_file_path{ p_path.plus_file(current_file) };
  398. if (current_file == ".." || current_file == ".") {
  399. current_file = dir_access->get_next();
  400. continue;
  401. }
  402. if (extensions_to_sign.find(current_file.get_extension()) > -1) {
  403. Error code_sign_error{ _code_sign(p_preset, current_file_path, p_ent_path) };
  404. if (code_sign_error != OK) {
  405. return code_sign_error;
  406. }
  407. } else if (dir_access->current_is_dir()) {
  408. Error code_sign_error{ _code_sign_directory(p_preset, current_file_path, p_ent_path, p_should_error_on_non_code) };
  409. if (code_sign_error != OK) {
  410. return code_sign_error;
  411. }
  412. } else if (p_should_error_on_non_code) {
  413. ERR_PRINT(vformat("Cannot sign file %s.", current_file));
  414. return Error::FAILED;
  415. }
  416. current_file = dir_access->get_next();
  417. }
  418. #endif
  419. return OK;
  420. }
  421. Error EditorExportPlatformOSX::_copy_and_sign_files(DirAccessRef &dir_access, const String &p_src_path,
  422. const String &p_in_app_path, bool p_sign_enabled,
  423. const Ref<EditorExportPreset> &p_preset, const String &p_ent_path,
  424. bool p_should_error_on_non_code_sign) {
  425. Error err{ OK };
  426. if (dir_access->dir_exists(p_src_path)) {
  427. #ifndef UNIX_ENABLED
  428. WARN_PRINT("Relative symlinks are not supported, exported " + p_src_path.get_file() + " might be broken!");
  429. #endif
  430. print_verbose("export framework: " + p_src_path + " -> " + p_in_app_path);
  431. err = dir_access->make_dir_recursive(p_in_app_path);
  432. if (err == OK) {
  433. err = dir_access->copy_dir(p_src_path, p_in_app_path, -1, true);
  434. }
  435. } else {
  436. print_verbose("export dylib: " + p_src_path + " -> " + p_in_app_path);
  437. err = dir_access->copy(p_src_path, p_in_app_path);
  438. }
  439. if (err == OK && p_sign_enabled) {
  440. if (dir_access->dir_exists(p_src_path) && p_src_path.get_extension().is_empty()) {
  441. // If it is a directory, find and sign all dynamic libraries.
  442. err = _code_sign_directory(p_preset, p_in_app_path, p_ent_path, p_should_error_on_non_code_sign);
  443. } else {
  444. err = _code_sign(p_preset, p_in_app_path, p_ent_path);
  445. }
  446. }
  447. return err;
  448. }
  449. Error EditorExportPlatformOSX::_export_osx_plugins_for(Ref<EditorExportPlugin> p_editor_export_plugin,
  450. const String &p_app_path_name, DirAccessRef &dir_access,
  451. bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset,
  452. const String &p_ent_path) {
  453. Error error{ OK };
  454. const Vector<String> &osx_plugins{ p_editor_export_plugin->get_osx_plugin_files() };
  455. for (int i = 0; i < osx_plugins.size(); ++i) {
  456. String src_path{ ProjectSettings::get_singleton()->globalize_path(osx_plugins[i]) };
  457. String path_in_app{ p_app_path_name + "/Contents/PlugIns/" + src_path.get_file() };
  458. error = _copy_and_sign_files(dir_access, src_path, path_in_app, p_sign_enabled, p_preset, p_ent_path, false);
  459. if (error != OK) {
  460. break;
  461. }
  462. }
  463. return error;
  464. }
  465. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  466. List<String> args;
  467. if (FileAccess::exists(p_dmg_path)) {
  468. OS::get_singleton()->move_to_trash(p_dmg_path);
  469. }
  470. args.push_back("create");
  471. args.push_back(p_dmg_path);
  472. args.push_back("-volname");
  473. args.push_back(p_pkg_name);
  474. args.push_back("-fs");
  475. args.push_back("HFS+");
  476. args.push_back("-srcfolder");
  477. args.push_back(p_app_path_name);
  478. String str;
  479. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  480. ERR_FAIL_COND_V(err != OK, err);
  481. print_line("hdiutil returned: " + str);
  482. if (str.find("create failed") != -1) {
  483. if (str.find("File exists") != -1) {
  484. EditorNode::add_io_error("hdiutil: create failed - file exists");
  485. } else {
  486. EditorNode::add_io_error("hdiutil: create failed");
  487. }
  488. return FAILED;
  489. }
  490. return OK;
  491. }
  492. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  493. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  494. String src_pkg_name;
  495. EditorProgress ep("export", "Exporting for OSX", 3, true);
  496. if (p_debug) {
  497. src_pkg_name = p_preset->get("custom_template/debug");
  498. } else {
  499. src_pkg_name = p_preset->get("custom_template/release");
  500. }
  501. if (src_pkg_name.is_empty()) {
  502. String err;
  503. src_pkg_name = find_export_template("osx.zip", &err);
  504. if (src_pkg_name.is_empty()) {
  505. EditorNode::add_io_error(err);
  506. return ERR_FILE_NOT_FOUND;
  507. }
  508. }
  509. if (!DirAccess::exists(p_path.get_base_dir())) {
  510. return ERR_FILE_BAD_PATH;
  511. }
  512. FileAccess *src_f = nullptr;
  513. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  514. if (ep.step("Creating app", 0)) {
  515. return ERR_SKIP;
  516. }
  517. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  518. if (!src_pkg_zip) {
  519. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  520. return ERR_FILE_NOT_FOUND;
  521. }
  522. int ret = unzGoToFirstFile(src_pkg_zip);
  523. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  524. String pkg_name;
  525. if (p_preset->get("application/name") != "") {
  526. pkg_name = p_preset->get("application/name"); // app_name
  527. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  528. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  529. } else {
  530. pkg_name = "Unnamed";
  531. }
  532. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  533. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  534. // Create our application bundle.
  535. String tmp_app_dir_name = pkg_name + ".app";
  536. String tmp_app_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(tmp_app_dir_name);
  537. print_line("Exporting to " + tmp_app_path_name);
  538. Error err = OK;
  539. DirAccessRef tmp_app_dir = DirAccess::create_for_path(tmp_app_path_name);
  540. if (!tmp_app_dir) {
  541. err = ERR_CANT_CREATE;
  542. }
  543. Array helpers = p_preset->get("codesign/entitlements/app_sandbox/helper_executables");
  544. // Create our folder structure.
  545. if (err == OK) {
  546. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  547. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  548. }
  549. if (err == OK) {
  550. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  551. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  552. }
  553. if ((err == OK) && helpers.size() > 0) {
  554. print_line("Creating " + tmp_app_path_name + "/Contents/Helpers");
  555. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Helpers");
  556. }
  557. if (err == OK) {
  558. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  559. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  560. }
  561. // Now process our template.
  562. bool found_binary = false;
  563. Vector<String> dylibs_found;
  564. while (ret == UNZ_OK && err == OK) {
  565. bool is_execute = false;
  566. // Get filename.
  567. unz_file_info info;
  568. char fname[16384];
  569. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  570. String file = String::utf8(fname);
  571. Vector<uint8_t> data;
  572. data.resize(info.uncompressed_size);
  573. // Read.
  574. unzOpenCurrentFile(src_pkg_zip);
  575. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  576. unzCloseCurrentFile(src_pkg_zip);
  577. // Write.
  578. file = file.replace_first("osx_template.app/", "");
  579. if (file == "Contents/Info.plist") {
  580. _fix_plist(p_preset, data, pkg_name);
  581. }
  582. if (file.begins_with("Contents/MacOS/godot_")) {
  583. if (file != "Contents/MacOS/" + binary_to_use) {
  584. ret = unzGoToNextFile(src_pkg_zip);
  585. continue; // skip
  586. }
  587. found_binary = true;
  588. is_execute = true;
  589. file = "Contents/MacOS/" + pkg_name;
  590. }
  591. if (file == "Contents/Resources/icon.icns") {
  592. // See if there is an icon.
  593. String iconpath;
  594. if (p_preset->get("application/icon") != "") {
  595. iconpath = p_preset->get("application/icon");
  596. } else {
  597. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  598. }
  599. if (!iconpath.is_empty()) {
  600. if (iconpath.get_extension() == "icns") {
  601. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  602. if (icon) {
  603. data.resize(icon->get_length());
  604. icon->get_buffer(&data.write[0], icon->get_length());
  605. icon->close();
  606. memdelete(icon);
  607. }
  608. } else {
  609. Ref<Image> icon;
  610. icon.instantiate();
  611. icon->load(iconpath);
  612. if (!icon->is_empty()) {
  613. _make_icon(icon, data);
  614. }
  615. }
  616. }
  617. }
  618. if (data.size() > 0) {
  619. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  620. if (!p_debug) {
  621. ret = unzGoToNextFile(src_pkg_zip);
  622. continue; // skip
  623. }
  624. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  625. }
  626. if (file.find("/data.mono.osx.64.release/") != -1) {
  627. if (p_debug) {
  628. ret = unzGoToNextFile(src_pkg_zip);
  629. continue; // skip
  630. }
  631. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  632. }
  633. if (file.ends_with(".dylib")) {
  634. dylibs_found.push_back(file);
  635. }
  636. print_line("ADDING: " + file + " size: " + itos(data.size()));
  637. // Write it into our application bundle.
  638. file = tmp_app_path_name.plus_file(file);
  639. if (err == OK) {
  640. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  641. }
  642. if (err == OK) {
  643. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  644. if (f) {
  645. f->store_buffer(data.ptr(), data.size());
  646. f->close();
  647. if (is_execute) {
  648. // chmod with 0755 if the file is executable.
  649. FileAccess::set_unix_permissions(file, 0755);
  650. }
  651. memdelete(f);
  652. } else {
  653. err = ERR_CANT_CREATE;
  654. }
  655. }
  656. }
  657. ret = unzGoToNextFile(src_pkg_zip);
  658. }
  659. // We're done with our source zip.
  660. unzClose(src_pkg_zip);
  661. if (!found_binary) {
  662. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  663. err = ERR_FILE_NOT_FOUND;
  664. }
  665. if (err == OK) {
  666. if (ep.step("Making PKG", 1)) {
  667. return ERR_SKIP;
  668. }
  669. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  670. Vector<SharedObject> shared_objects;
  671. err = save_pack(p_preset, pack_path, &shared_objects);
  672. // See if we can code sign our new package.
  673. bool sign_enabled = p_preset->get("codesign/enable");
  674. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  675. String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + "_helper.entitlements");
  676. if (sign_enabled && (ent_path.is_empty())) {
  677. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  678. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  679. if (ent_f) {
  680. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  681. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  682. ent_f->store_line("<plist version=\"1.0\">");
  683. ent_f->store_line("<dict>");
  684. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  685. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  686. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  687. ent_f->store_line("<true/>");
  688. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  689. ent_f->store_line("<true/>");
  690. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  691. ent_f->store_line("<true/>");
  692. } else {
  693. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  694. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  695. ent_f->store_line("<true/>");
  696. }
  697. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  698. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  699. ent_f->store_line("<true/>");
  700. }
  701. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  702. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  703. ent_f->store_line("<true/>");
  704. }
  705. }
  706. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  707. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  708. ent_f->store_line("<true/>");
  709. }
  710. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  711. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  712. ent_f->store_line("<true/>");
  713. }
  714. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  715. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  716. ent_f->store_line("<true/>");
  717. }
  718. if ((bool)p_preset->get("codesign/entitlements/location")) {
  719. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  720. ent_f->store_line("<true/>");
  721. }
  722. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  723. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  724. ent_f->store_line("<true/>");
  725. }
  726. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  727. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  728. ent_f->store_line("<true/>");
  729. }
  730. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  731. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  732. ent_f->store_line("<true/>");
  733. }
  734. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  735. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  736. ent_f->store_line("<true/>");
  737. }
  738. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  739. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  740. ent_f->store_line("<true/>");
  741. }
  742. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  743. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  744. ent_f->store_line("<true/>");
  745. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  746. ent_f->store_line("<key>com.apple.security.network.server</key>");
  747. ent_f->store_line("<true/>");
  748. }
  749. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  750. ent_f->store_line("<key>com.apple.security.network.client</key>");
  751. ent_f->store_line("<true/>");
  752. }
  753. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  754. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  755. ent_f->store_line("<true/>");
  756. }
  757. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  758. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  759. ent_f->store_line("<true/>");
  760. }
  761. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  762. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  763. ent_f->store_line("<true/>");
  764. }
  765. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  766. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  767. ent_f->store_line("<true/>");
  768. }
  769. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  770. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  771. ent_f->store_line("<true/>");
  772. }
  773. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  774. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  775. ent_f->store_line("<true/>");
  776. }
  777. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  778. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  779. ent_f->store_line("<true/>");
  780. }
  781. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  782. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  783. ent_f->store_line("<true/>");
  784. }
  785. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  786. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  787. ent_f->store_line("<true/>");
  788. }
  789. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  790. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  791. ent_f->store_line("<true/>");
  792. }
  793. }
  794. ent_f->store_line("</dict>");
  795. ent_f->store_line("</plist>");
  796. ent_f->close();
  797. memdelete(ent_f);
  798. } else {
  799. err = ERR_CANT_CREATE;
  800. }
  801. if ((err == OK) && helpers.size() > 0) {
  802. ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
  803. if (ent_f) {
  804. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  805. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  806. ent_f->store_line("<plist version=\"1.0\">");
  807. ent_f->store_line("<dict>");
  808. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  809. ent_f->store_line("<true/>");
  810. ent_f->store_line("<key>com.apple.security.inherit</key>");
  811. ent_f->store_line("<true/>");
  812. ent_f->store_line("</dict>");
  813. ent_f->store_line("</plist>");
  814. ent_f->close();
  815. memdelete(ent_f);
  816. } else {
  817. err = ERR_CANT_CREATE;
  818. }
  819. }
  820. }
  821. if ((err == OK) && helpers.size() > 0) {
  822. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  823. for (int i = 0; i < helpers.size(); i++) {
  824. String hlp_path = helpers[i];
  825. err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
  826. if (err == OK && sign_enabled) {
  827. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path);
  828. }
  829. FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
  830. }
  831. }
  832. if (err == OK) {
  833. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  834. for (int i = 0; i < shared_objects.size(); i++) {
  835. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  836. String path_in_app{ tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file() };
  837. err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, true);
  838. if (err != OK) {
  839. break;
  840. }
  841. }
  842. Vector<Ref<EditorExportPlugin>> export_plugins{ EditorExport::get_singleton()->get_export_plugins() };
  843. for (int i = 0; i < export_plugins.size(); ++i) {
  844. err = _export_osx_plugins_for(export_plugins[i], tmp_app_path_name, da, sign_enabled, p_preset, ent_path);
  845. if (err != OK) {
  846. break;
  847. }
  848. }
  849. }
  850. if (sign_enabled) {
  851. for (int i = 0; i < dylibs_found.size(); i++) {
  852. if (err == OK) {
  853. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  854. }
  855. }
  856. }
  857. if (err == OK && sign_enabled) {
  858. if (ep.step("Code signing bundle", 2)) {
  859. return ERR_SKIP;
  860. }
  861. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  862. }
  863. if (export_format == "dmg") {
  864. // Create a DMG.
  865. if (err == OK) {
  866. if (ep.step("Making DMG", 3)) {
  867. return ERR_SKIP;
  868. }
  869. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  870. }
  871. // Sign DMG.
  872. if (err == OK && sign_enabled) {
  873. if (ep.step("Code signing DMG", 3)) {
  874. return ERR_SKIP;
  875. }
  876. err = _code_sign(p_preset, p_path, ent_path);
  877. }
  878. } else {
  879. // Create ZIP.
  880. if (err == OK) {
  881. if (ep.step("Making ZIP", 3)) {
  882. return ERR_SKIP;
  883. }
  884. if (FileAccess::exists(p_path)) {
  885. OS::get_singleton()->move_to_trash(p_path);
  886. }
  887. FileAccess *dst_f = nullptr;
  888. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  889. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  890. _zip_folder_recursive(zip, EditorPaths::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  891. zipClose(zip, nullptr);
  892. }
  893. }
  894. bool noto_enabled = p_preset->get("notarization/enable");
  895. if (err == OK && noto_enabled) {
  896. if (ep.step("Sending archive for notarization", 4)) {
  897. return ERR_SKIP;
  898. }
  899. err = _notarize(p_preset, p_path);
  900. }
  901. // Clean up temporary entitlements files.
  902. DirAccess::remove_file_or_error(hlp_ent_path);
  903. // Clean up temporary .app dir.
  904. tmp_app_dir->change_dir(tmp_app_path_name);
  905. tmp_app_dir->erase_contents_recursive();
  906. tmp_app_dir->change_dir("..");
  907. tmp_app_dir->remove(tmp_app_dir_name);
  908. }
  909. return err;
  910. }
  911. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  912. String dir = p_root_path.plus_file(p_folder);
  913. DirAccessRef da = DirAccess::open(dir);
  914. da->list_dir_begin();
  915. String f = da->get_next();
  916. while (!f.is_empty()) {
  917. if (f == "." || f == "..") {
  918. f = da->get_next();
  919. continue;
  920. }
  921. if (da->is_link(f)) {
  922. OS::Time time = OS::get_singleton()->get_time();
  923. OS::Date date = OS::get_singleton()->get_date();
  924. zip_fileinfo zipfi;
  925. zipfi.tmz_date.tm_hour = time.hour;
  926. zipfi.tmz_date.tm_mday = date.day;
  927. zipfi.tmz_date.tm_min = time.minute;
  928. 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/
  929. zipfi.tmz_date.tm_sec = time.second;
  930. zipfi.tmz_date.tm_year = date.year;
  931. zipfi.dosDate = 0;
  932. // 0120000: symbolic link type
  933. // 0000755: permissions rwxr-xr-x
  934. // 0000644: permissions rw-r--r--
  935. uint32_t _mode = 0120644;
  936. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  937. zipfi.internal_fa = 0;
  938. zipOpenNewFileInZip4(p_zip,
  939. p_folder.plus_file(f).utf8().get_data(),
  940. &zipfi,
  941. nullptr,
  942. 0,
  943. nullptr,
  944. 0,
  945. nullptr,
  946. Z_DEFLATED,
  947. Z_DEFAULT_COMPRESSION,
  948. 0,
  949. -MAX_WBITS,
  950. DEF_MEM_LEVEL,
  951. Z_DEFAULT_STRATEGY,
  952. nullptr,
  953. 0,
  954. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  955. 0);
  956. String target = da->read_link(f);
  957. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  958. zipCloseFileInZip(p_zip);
  959. } else if (da->current_is_dir()) {
  960. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  961. } else {
  962. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name)) || p_folder.ends_with("Helpers");
  963. OS::Time time = OS::get_singleton()->get_time();
  964. OS::Date date = OS::get_singleton()->get_date();
  965. zip_fileinfo zipfi;
  966. zipfi.tmz_date.tm_hour = time.hour;
  967. zipfi.tmz_date.tm_mday = date.day;
  968. zipfi.tmz_date.tm_min = time.minute;
  969. 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/
  970. zipfi.tmz_date.tm_sec = time.second;
  971. zipfi.tmz_date.tm_year = date.year;
  972. zipfi.dosDate = 0;
  973. // 0100000: regular file type
  974. // 0000755: permissions rwxr-xr-x
  975. // 0000644: permissions rw-r--r--
  976. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  977. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  978. zipfi.internal_fa = 0;
  979. zipOpenNewFileInZip4(p_zip,
  980. p_folder.plus_file(f).utf8().get_data(),
  981. &zipfi,
  982. nullptr,
  983. 0,
  984. nullptr,
  985. 0,
  986. nullptr,
  987. Z_DEFLATED,
  988. Z_DEFAULT_COMPRESSION,
  989. 0,
  990. -MAX_WBITS,
  991. DEF_MEM_LEVEL,
  992. Z_DEFAULT_STRATEGY,
  993. nullptr,
  994. 0,
  995. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  996. 0);
  997. FileAccessRef fa = FileAccess::open(dir.plus_file(f), FileAccess::READ);
  998. if (!fa) {
  999. ERR_FAIL_MSG("Can't open file to read from path '" + String(dir.plus_file(f)) + "'.");
  1000. }
  1001. const int bufsize = 16384;
  1002. uint8_t buf[bufsize];
  1003. while (true) {
  1004. uint64_t got = fa->get_buffer(buf, bufsize);
  1005. if (got == 0) {
  1006. break;
  1007. }
  1008. zipWriteInFileInZip(p_zip, buf, got);
  1009. }
  1010. zipCloseFileInZip(p_zip);
  1011. }
  1012. f = da->get_next();
  1013. }
  1014. da->list_dir_end();
  1015. }
  1016. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1017. String err;
  1018. bool valid = false;
  1019. // Look for export templates (custom templates).
  1020. bool dvalid = false;
  1021. bool rvalid = false;
  1022. if (p_preset->get("custom_template/debug") != "") {
  1023. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1024. if (!dvalid) {
  1025. err += TTR("Custom debug template not found.") + "\n";
  1026. }
  1027. }
  1028. if (p_preset->get("custom_template/release") != "") {
  1029. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1030. if (!rvalid) {
  1031. err += TTR("Custom release template not found.") + "\n";
  1032. }
  1033. }
  1034. // Look for export templates (official templates, check only is custom templates are not set).
  1035. if (!dvalid || !rvalid) {
  1036. dvalid = exists_export_template("osx.zip", &err);
  1037. rvalid = dvalid; // Both in the same ZIP.
  1038. }
  1039. valid = dvalid || rvalid;
  1040. r_missing_templates = !valid;
  1041. String identifier = p_preset->get("application/bundle_identifier");
  1042. String pn_err;
  1043. if (!is_package_name_valid(identifier, &pn_err)) {
  1044. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  1045. valid = false;
  1046. }
  1047. #ifdef OSX_ENABLED
  1048. bool sign_enabled = p_preset->get("codesign/enable");
  1049. bool noto_enabled = p_preset->get("notarization/enable");
  1050. bool ad_hoc = ((p_preset->get("codesign/identity") == "") || (p_preset->get("codesign/identity") == "-"));
  1051. if (noto_enabled) {
  1052. if (ad_hoc) {
  1053. err += TTR("Notarization: Notarization with the ad-hoc signature is not supported.") + "\n";
  1054. valid = false;
  1055. }
  1056. if (!sign_enabled) {
  1057. err += TTR("Notarization: Code signing is required for notarization.") + "\n";
  1058. valid = false;
  1059. }
  1060. if (!(bool)p_preset->get("codesign/hardened_runtime")) {
  1061. err += TTR("Notarization: Hardened runtime is required for notarization.") + "\n";
  1062. valid = false;
  1063. }
  1064. if (!(bool)p_preset->get("codesign/timestamp")) {
  1065. err += TTR("Notarization: Timestamping is required for notarization.") + "\n";
  1066. valid = false;
  1067. }
  1068. if (p_preset->get("notarization/apple_id_name") == "") {
  1069. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  1070. valid = false;
  1071. }
  1072. if (p_preset->get("notarization/apple_id_password") == "") {
  1073. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  1074. valid = false;
  1075. }
  1076. } else {
  1077. err += TTR("Notarization is disabled. Exported project will be blocked by Gatekeeper, if it's downloaded from an unknown source.") + "\n";
  1078. if (!sign_enabled) {
  1079. err += TTR("Code signing is disabled. Exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1080. } else {
  1081. if ((bool)p_preset->get("codesign/hardened_runtime") && ad_hoc) {
  1082. err += TTR("Hardened Runtime is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1083. }
  1084. if ((bool)p_preset->get("codesign/timestamp") && ad_hoc) {
  1085. err += TTR("Timestamping is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1086. }
  1087. }
  1088. }
  1089. #else
  1090. err += TTR("macOS code signing and Notarization is not supported on the host OS. Exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1091. #endif
  1092. if (!err.is_empty()) {
  1093. r_error = err;
  1094. }
  1095. return valid;
  1096. }
  1097. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  1098. Ref<Image> img = memnew(Image(_osx_logo));
  1099. logo.instantiate();
  1100. logo->create_from_image(img);
  1101. }
  1102. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  1103. }