export_plugin.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. #include "codesign.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_paths.h"
  34. #include "modules/modules_enabled.gen.h" // For regex.
  35. void EditorExportPlatformMacOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  36. if (p_preset->get("texture_format/s3tc")) {
  37. r_features->push_back("s3tc");
  38. }
  39. if (p_preset->get("texture_format/etc")) {
  40. r_features->push_back("etc");
  41. }
  42. if (p_preset->get("texture_format/etc2")) {
  43. r_features->push_back("etc2");
  44. }
  45. r_features->push_back("64");
  46. }
  47. bool EditorExportPlatformMacOS::get_export_option_visibility(const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  48. // These options are not supported by built-in codesign, used on non macOS host.
  49. if (!OS::get_singleton()->has_feature("macos")) {
  50. if (p_option == "codesign/identity" || p_option == "codesign/timestamp" || p_option == "codesign/hardened_runtime" || p_option == "codesign/custom_options" || p_option.begins_with("notarization/")) {
  51. return false;
  52. }
  53. }
  54. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  55. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  56. if (p_option == "codesign/entitlements/allow_jit_code_execution" || p_option == "codesign/entitlements/allow_unsigned_executable_memory" || p_option == "codesign/entitlements/allow_dyld_environment_variables") {
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. void EditorExportPlatformMacOS::get_export_options(List<ExportOption> *r_options) {
  63. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  64. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  65. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "debug/export_console_script", PROPERTY_HINT_ENUM, "No,Debug Only,Debug and Release"), 1));
  66. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  67. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  68. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  69. 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"));
  70. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  72. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  73. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "application/copyright_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  75. 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"), ""));
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/microphone_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  77. 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"), ""));
  78. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/camera_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  79. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/location_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the location information"), ""));
  80. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/location_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  81. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/address_book_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the address book"), ""));
  82. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/address_book_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  83. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/calendar_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the calendar"), ""));
  84. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/calendar_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  85. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/photos_library_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the photo library"), ""));
  86. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/photos_library_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  87. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/desktop_folder_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use Desktop folder"), ""));
  88. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/desktop_folder_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  89. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/documents_folder_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use Documents folder"), ""));
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/documents_folder_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  91. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/downloads_folder_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use Downloads folder"), ""));
  92. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/downloads_folder_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/network_volumes_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use network volumes"), ""));
  94. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/network_volumes_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  95. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/removable_volumes_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use removable volumes"), ""));
  96. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "privacy/removable_volumes_usage_description_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary()));
  97. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), true));
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/replace_existing_signature"), true));
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements/custom_file", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_jit_code_execution"), false));
  104. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_unsigned_executable_memory"), false));
  105. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_dyld_environment_variables"), false));
  106. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/disable_library_validation"), false));
  107. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/audio_input"), false));
  108. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/camera"), false));
  109. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/location"), false));
  110. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/address_book"), false));
  111. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/calendars"), false));
  112. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/photos_library"), false));
  113. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/apple_events"), false));
  114. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/debugging"), false));
  115. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/enabled"), false));
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_server"), false));
  117. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_client"), false));
  118. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_usb"), false));
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_bluetooth"), false));
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_downloads", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_pictures", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_music", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_movies", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  124. 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()));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  128. 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"), ""));
  129. 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"), ""));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  132. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  133. }
  134. void _rgba8_to_packbits_encode(int p_ch, int p_size, Vector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  135. int src_len = p_size * p_size;
  136. Vector<uint8_t> result;
  137. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  138. int res_size = 0;
  139. uint8_t buf[128];
  140. int buf_size = 0;
  141. int i = 0;
  142. while (i < src_len) {
  143. uint8_t cur = p_source.ptr()[i * 4 + p_ch];
  144. if (i < src_len - 2) {
  145. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(i + 2) * 4 + p_ch] == cur)) {
  146. if (buf_size > 0) {
  147. result.write[res_size++] = (uint8_t)(buf_size - 1);
  148. memcpy(&result.write[res_size], &buf, buf_size);
  149. res_size += buf_size;
  150. buf_size = 0;
  151. }
  152. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  153. bool hit_lim = true;
  154. for (int j = 3; j <= lim; j++) {
  155. if (p_source.ptr()[(i + j) * 4 + p_ch] != cur) {
  156. hit_lim = false;
  157. i = i + j - 1;
  158. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  159. result.write[res_size++] = cur;
  160. break;
  161. }
  162. }
  163. if (hit_lim) {
  164. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  165. result.write[res_size++] = cur;
  166. i = i + lim;
  167. }
  168. } else {
  169. buf[buf_size++] = cur;
  170. if (buf_size == 128) {
  171. result.write[res_size++] = (uint8_t)(buf_size - 1);
  172. memcpy(&result.write[res_size], &buf, buf_size);
  173. res_size += buf_size;
  174. buf_size = 0;
  175. }
  176. }
  177. } else {
  178. buf[buf_size++] = cur;
  179. result.write[res_size++] = (uint8_t)(buf_size - 1);
  180. memcpy(&result.write[res_size], &buf, buf_size);
  181. res_size += buf_size;
  182. buf_size = 0;
  183. }
  184. i++;
  185. }
  186. int ofs = p_dest.size();
  187. p_dest.resize(p_dest.size() + res_size);
  188. memcpy(&p_dest.write[ofs], result.ptr(), res_size);
  189. }
  190. void EditorExportPlatformMacOS::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  191. Ref<ImageTexture> it = memnew(ImageTexture);
  192. Vector<uint8_t> data;
  193. data.resize(8);
  194. data.write[0] = 'i';
  195. data.write[1] = 'c';
  196. data.write[2] = 'n';
  197. data.write[3] = 's';
  198. struct MacOSIconInfo {
  199. const char *name;
  200. const char *mask_name;
  201. bool is_png;
  202. int size;
  203. };
  204. static const MacOSIconInfo icon_infos[] = {
  205. { "ic10", "", true, 1024 }, //1024×1024 32-bit PNG and 512×512@2x 32-bit "retina" PNG
  206. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  207. { "ic14", "", true, 512 }, //256×256@2x 32-bit "retina" PNG
  208. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  209. { "ic13", "", true, 256 }, //128×128@2x 32-bit "retina" PNG
  210. { "ic07", "", true, 128 }, //128×128 32-bit PNG
  211. { "ic12", "", true, 64 }, //32×32@2× 32-bit "retina" PNG
  212. { "ic11", "", true, 32 }, //16×16@2× 32-bit "retina" PNG
  213. { "il32", "l8mk", false, 32 }, //32×32 24-bit RLE + 8-bit uncompressed mask
  214. { "is32", "s8mk", false, 16 } //16×16 24-bit RLE + 8-bit uncompressed mask
  215. };
  216. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  217. 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?
  218. copy->convert(Image::FORMAT_RGBA8);
  219. copy->resize(icon_infos[i].size, icon_infos[i].size);
  220. if (icon_infos[i].is_png) {
  221. // Encode PNG icon.
  222. it->set_image(copy);
  223. String path = EditorPaths::get_singleton()->get_cache_dir().plus_file("icon.png");
  224. ResourceSaver::save(path, it);
  225. {
  226. Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ);
  227. if (f.is_null()) {
  228. // Clean up generated file.
  229. DirAccess::remove_file_or_error(path);
  230. add_message(EXPORT_MESSAGE_ERROR, TTR("Icon Creation"), vformat(TTR("Could not open icon file \"%s\"."), path));
  231. return;
  232. }
  233. int ofs = data.size();
  234. uint64_t len = f->get_length();
  235. data.resize(data.size() + len + 8);
  236. f->get_buffer(&data.write[ofs + 8], len);
  237. len += 8;
  238. len = BSWAP32(len);
  239. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  240. encode_uint32(len, &data.write[ofs + 4]);
  241. }
  242. // Clean up generated file.
  243. DirAccess::remove_file_or_error(path);
  244. } else {
  245. Vector<uint8_t> src_data = copy->get_data();
  246. //encode 24bit RGB RLE icon
  247. {
  248. int ofs = data.size();
  249. data.resize(data.size() + 8);
  250. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  251. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  252. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  253. int len = data.size() - ofs;
  254. len = BSWAP32(len);
  255. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  256. encode_uint32(len, &data.write[ofs + 4]);
  257. }
  258. //encode 8bit mask uncompressed icon
  259. {
  260. int ofs = data.size();
  261. int len = copy->get_width() * copy->get_height();
  262. data.resize(data.size() + len + 8);
  263. for (int j = 0; j < len; j++) {
  264. data.write[ofs + 8 + j] = src_data.ptr()[j * 4 + 3];
  265. }
  266. len += 8;
  267. len = BSWAP32(len);
  268. memcpy(&data.write[ofs], icon_infos[i].mask_name, 4);
  269. encode_uint32(len, &data.write[ofs + 4]);
  270. }
  271. }
  272. }
  273. uint32_t total_len = data.size();
  274. total_len = BSWAP32(total_len);
  275. encode_uint32(total_len, &data.write[4]);
  276. p_data = data;
  277. }
  278. void EditorExportPlatformMacOS::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  279. String str;
  280. String strnew;
  281. str.parse_utf8((const char *)plist.ptr(), plist.size());
  282. Vector<String> lines = str.split("\n");
  283. for (int i = 0; i < lines.size(); i++) {
  284. if (lines[i].find("$binary") != -1) {
  285. strnew += lines[i].replace("$binary", p_binary) + "\n";
  286. } else if (lines[i].find("$name") != -1) {
  287. strnew += lines[i].replace("$name", ProjectSettings::get_singleton()->get("application/config/name")) + "\n";
  288. } else if (lines[i].find("$bundle_identifier") != -1) {
  289. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  290. } else if (lines[i].find("$short_version") != -1) {
  291. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  292. } else if (lines[i].find("$version") != -1) {
  293. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  294. } else if (lines[i].find("$signature") != -1) {
  295. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  296. } else if (lines[i].find("$app_category") != -1) {
  297. String cat = p_preset->get("application/app_category");
  298. strnew += lines[i].replace("$app_category", cat.to_lower()) + "\n";
  299. } else if (lines[i].find("$copyright") != -1) {
  300. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  301. } else if (lines[i].find("$highres") != -1) {
  302. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "\t<true/>" : "\t<false/>") + "\n";
  303. } else if (lines[i].find("$usage_descriptions") != -1) {
  304. String descriptions;
  305. if (!((String)p_preset->get("privacy/microphone_usage_description")).is_empty()) {
  306. descriptions += "\t<key>NSMicrophoneUsageDescription</key>\n";
  307. descriptions += "\t<string>" + (String)p_preset->get("privacy/microphone_usage_description") + "</string>\n";
  308. }
  309. if (!((String)p_preset->get("privacy/camera_usage_description")).is_empty()) {
  310. descriptions += "\t<key>NSCameraUsageDescription</key>\n";
  311. descriptions += "\t<string>" + (String)p_preset->get("privacy/camera_usage_description") + "</string>\n";
  312. }
  313. if (!((String)p_preset->get("privacy/location_usage_description")).is_empty()) {
  314. descriptions += "\t<key>NSLocationUsageDescription</key>\n";
  315. descriptions += "\t<string>" + (String)p_preset->get("privacy/location_usage_description") + "</string>\n";
  316. }
  317. if (!((String)p_preset->get("privacy/address_book_usage_description")).is_empty()) {
  318. descriptions += "\t<key>NSContactsUsageDescription</key>\n";
  319. descriptions += "\t<string>" + (String)p_preset->get("privacy/address_book_usage_description") + "</string>\n";
  320. }
  321. if (!((String)p_preset->get("privacy/calendar_usage_description")).is_empty()) {
  322. descriptions += "\t<key>NSCalendarsUsageDescription</key>\n";
  323. descriptions += "\t<string>" + (String)p_preset->get("privacy/calendar_usage_description") + "</string>\n";
  324. }
  325. if (!((String)p_preset->get("privacy/photos_library_usage_description")).is_empty()) {
  326. descriptions += "\t<key>NSPhotoLibraryUsageDescription</key>\n";
  327. descriptions += "\t<string>" + (String)p_preset->get("privacy/photos_library_usage_description") + "</string>\n";
  328. }
  329. if (!((String)p_preset->get("privacy/desktop_folder_usage_description")).is_empty()) {
  330. descriptions += "\t<key>NSDesktopFolderUsageDescription</key>\n";
  331. descriptions += "\t<string>" + (String)p_preset->get("privacy/desktop_folder_usage_description") + "</string>\n";
  332. }
  333. if (!((String)p_preset->get("privacy/documents_folder_usage_description")).is_empty()) {
  334. descriptions += "\t<key>NSDocumentsFolderUsageDescription</key>\n";
  335. descriptions += "\t<string>" + (String)p_preset->get("privacy/documents_folder_usage_description") + "</string>\n";
  336. }
  337. if (!((String)p_preset->get("privacy/downloads_folder_usage_description")).is_empty()) {
  338. descriptions += "\t<key>NSDownloadsFolderUsageDescription</key>\n";
  339. descriptions += "\t<string>" + (String)p_preset->get("privacy/downloads_folder_usage_description") + "</string>\n";
  340. }
  341. if (!((String)p_preset->get("privacy/network_volumes_usage_description")).is_empty()) {
  342. descriptions += "\t<key>NSNetworkVolumesUsageDescription</key>\n";
  343. descriptions += "\t<string>" + (String)p_preset->get("privacy/network_volumes_usage_description") + "</string>\n";
  344. }
  345. if (!((String)p_preset->get("privacy/removable_volumes_usage_description")).is_empty()) {
  346. descriptions += "\t<key>NSRemovableVolumesUsageDescription</key>\n";
  347. descriptions += "\t<string>" + (String)p_preset->get("privacy/removable_volumes_usage_description") + "</string>\n";
  348. }
  349. if (!descriptions.is_empty()) {
  350. strnew += lines[i].replace("$usage_descriptions", descriptions);
  351. }
  352. } else {
  353. strnew += lines[i] + "\n";
  354. }
  355. }
  356. CharString cs = strnew.utf8();
  357. plist.resize(cs.size() - 1);
  358. for (int i = 0; i < cs.size() - 1; i++) {
  359. plist.write[i] = cs[i];
  360. }
  361. }
  362. /**
  363. * If we're running the macOS version of the Godot editor we'll:
  364. * - export our application bundle to a temporary folder
  365. * - attempt to code sign it
  366. * - and then wrap it up in a DMG
  367. */
  368. Error EditorExportPlatformMacOS::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  369. #ifdef MACOS_ENABLED
  370. List<String> args;
  371. args.push_back("altool");
  372. args.push_back("--notarize-app");
  373. args.push_back("--primary-bundle-id");
  374. args.push_back(p_preset->get("application/bundle_identifier"));
  375. args.push_back("--username");
  376. args.push_back(p_preset->get("notarization/apple_id_name"));
  377. args.push_back("--password");
  378. args.push_back(p_preset->get("notarization/apple_id_password"));
  379. args.push_back("--type");
  380. args.push_back("osx");
  381. if (p_preset->get("notarization/apple_team_id")) {
  382. args.push_back("--asc-provider");
  383. args.push_back(p_preset->get("notarization/apple_team_id"));
  384. }
  385. args.push_back("--file");
  386. args.push_back(p_path);
  387. String str;
  388. Error err = OS::get_singleton()->execute("xcrun", args, &str, nullptr, true);
  389. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  390. add_message(EXPORT_MESSAGE_WARNING, TTR("Notarization"), TTR("Could not start xcrun executable."));
  391. return err;
  392. }
  393. print_verbose("altool (" + p_path + "):\n" + str);
  394. int rq_offset = str.find("RequestUUID");
  395. if (rq_offset == -1) {
  396. add_message(EXPORT_MESSAGE_WARNING, TTR("Notarization"), TTR("Notarization failed."));
  397. return FAILED;
  398. } else {
  399. int next_nl = str.find("\n", rq_offset);
  400. String request_uuid = (next_nl == -1) ? str.substr(rq_offset + 14, -1) : str.substr(rq_offset + 14, next_nl - rq_offset - 14);
  401. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), vformat(TTR("Notarization request UUID: \"%s\""), request_uuid));
  402. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), TTR("The notarization process generally takes less than an hour. When the process is completed, you'll receive an email."));
  403. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), "\t" + TTR("You can check progress manually by opening a Terminal and running the following command:"));
  404. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), "\t\t\"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  405. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), "\t" + TTR("Run the following command to staple the notarization ticket to the exported application (optional):"));
  406. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), "\t\t\"xcrun stapler staple <app path>\"");
  407. }
  408. #endif
  409. return OK;
  410. }
  411. Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_warn) {
  412. bool force_builtin_codesign = EditorSettings::get_singleton()->get("export/macos/force_builtin_codesign");
  413. bool ad_hoc = (p_preset->get("codesign/identity") == "" || p_preset->get("codesign/identity") == "-");
  414. if ((!FileAccess::exists("/usr/bin/codesign") && !FileAccess::exists("/bin/codesign")) || force_builtin_codesign) {
  415. print_verbose("using built-in codesign...");
  416. #ifdef MODULE_REGEX_ENABLED
  417. #ifdef MACOS_ENABLED
  418. if (p_preset->get("codesign/timestamp") && p_warn) {
  419. add_message(EXPORT_MESSAGE_INFO, TTR("Code Signing"), TTR("Timestamping is not compatible with ad-hoc signature, and was disabled!"));
  420. }
  421. if (p_preset->get("codesign/hardened_runtime") && p_warn) {
  422. add_message(EXPORT_MESSAGE_INFO, TTR("Code Signing"), TTR("Hardened Runtime is not compatible with ad-hoc signature, and was disabled!"));
  423. }
  424. #endif
  425. String error_msg;
  426. Error err = CodeSign::codesign(false, p_preset->get("codesign/replace_existing_signature"), p_path, p_ent_path, error_msg);
  427. if (err != OK) {
  428. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Built-in CodeSign failed with error \"%s\"."), error_msg));
  429. return FAILED;
  430. }
  431. #else
  432. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Built-in CodeSign require regex module."));
  433. #endif
  434. return OK;
  435. } else {
  436. print_verbose("using external codesign...");
  437. List<String> args;
  438. if (p_preset->get("codesign/timestamp")) {
  439. if (ad_hoc) {
  440. if (p_warn) {
  441. add_message(EXPORT_MESSAGE_INFO, TTR("Code Signing"), TTR("Timestamping is not compatible with ad-hoc signature, and was disabled!"));
  442. }
  443. } else {
  444. args.push_back("--timestamp");
  445. }
  446. }
  447. if (p_preset->get("codesign/hardened_runtime")) {
  448. if (ad_hoc) {
  449. if (p_warn) {
  450. add_message(EXPORT_MESSAGE_INFO, TTR("Code Signing"), TTR("Hardened Runtime is not compatible with ad-hoc signature, and was disabled!"));
  451. }
  452. } else {
  453. args.push_back("--options");
  454. args.push_back("runtime");
  455. }
  456. }
  457. if (p_path.get_extension() != "dmg") {
  458. args.push_back("--entitlements");
  459. args.push_back(p_ent_path);
  460. }
  461. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  462. for (int i = 0; i < user_args.size(); i++) {
  463. String user_arg = user_args[i].strip_edges();
  464. if (!user_arg.is_empty()) {
  465. args.push_back(user_arg);
  466. }
  467. }
  468. args.push_back("-s");
  469. if (ad_hoc) {
  470. args.push_back("-");
  471. } else {
  472. args.push_back(p_preset->get("codesign/identity"));
  473. }
  474. args.push_back("-v"); /* provide some more feedback */
  475. if (p_preset->get("codesign/replace_existing_signature")) {
  476. args.push_back("-f");
  477. }
  478. args.push_back(p_path);
  479. String str;
  480. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  481. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  482. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start codesign executable, make sure Xcode command line tools are installed."));
  483. return err;
  484. }
  485. print_verbose("codesign (" + p_path + "):\n" + str);
  486. if (str.find("no identity found") != -1) {
  487. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  488. return FAILED;
  489. }
  490. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  491. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid entitlements file."));
  492. return FAILED;
  493. }
  494. return OK;
  495. }
  496. }
  497. Error EditorExportPlatformMacOS::_code_sign_directory(const Ref<EditorExportPreset> &p_preset, const String &p_path,
  498. const String &p_ent_path, bool p_should_error_on_non_code) {
  499. #ifdef MACOS_ENABLED
  500. static Vector<String> extensions_to_sign;
  501. if (extensions_to_sign.is_empty()) {
  502. extensions_to_sign.push_back("dylib");
  503. extensions_to_sign.push_back("framework");
  504. }
  505. Error dir_access_error;
  506. Ref<DirAccess> dir_access{ DirAccess::open(p_path, &dir_access_error) };
  507. if (dir_access_error != OK) {
  508. return dir_access_error;
  509. }
  510. dir_access->list_dir_begin();
  511. String current_file{ dir_access->get_next() };
  512. while (!current_file.is_empty()) {
  513. String current_file_path{ p_path.plus_file(current_file) };
  514. if (current_file == ".." || current_file == ".") {
  515. current_file = dir_access->get_next();
  516. continue;
  517. }
  518. if (extensions_to_sign.find(current_file.get_extension()) > -1) {
  519. Error code_sign_error{ _code_sign(p_preset, current_file_path, p_ent_path, false) };
  520. if (code_sign_error != OK) {
  521. return code_sign_error;
  522. }
  523. } else if (dir_access->current_is_dir()) {
  524. Error code_sign_error{ _code_sign_directory(p_preset, current_file_path, p_ent_path, p_should_error_on_non_code) };
  525. if (code_sign_error != OK) {
  526. return code_sign_error;
  527. }
  528. } else if (p_should_error_on_non_code) {
  529. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Cannot sign file %s."), current_file));
  530. return Error::FAILED;
  531. }
  532. current_file = dir_access->get_next();
  533. }
  534. #endif
  535. return OK;
  536. }
  537. Error EditorExportPlatformMacOS::_copy_and_sign_files(Ref<DirAccess> &dir_access, const String &p_src_path,
  538. const String &p_in_app_path, bool p_sign_enabled,
  539. const Ref<EditorExportPreset> &p_preset, const String &p_ent_path,
  540. bool p_should_error_on_non_code_sign) {
  541. Error err{ OK };
  542. if (dir_access->dir_exists(p_src_path)) {
  543. #ifndef UNIX_ENABLED
  544. add_message(EXPORT_MESSAGE_INFO, TTR("Export"), vformat(TTR("Relative symlinks are not supported, exported \"%s\" might be broken!"), p_src_path.get_file()));
  545. #endif
  546. print_verbose("export framework: " + p_src_path + " -> " + p_in_app_path);
  547. err = dir_access->make_dir_recursive(p_in_app_path);
  548. if (err == OK) {
  549. err = dir_access->copy_dir(p_src_path, p_in_app_path, -1, true);
  550. }
  551. } else {
  552. print_verbose("export dylib: " + p_src_path + " -> " + p_in_app_path);
  553. err = dir_access->copy(p_src_path, p_in_app_path);
  554. }
  555. if (err == OK && p_sign_enabled) {
  556. if (dir_access->dir_exists(p_src_path) && p_src_path.get_extension().is_empty()) {
  557. // If it is a directory, find and sign all dynamic libraries.
  558. err = _code_sign_directory(p_preset, p_in_app_path, p_ent_path, p_should_error_on_non_code_sign);
  559. } else {
  560. err = _code_sign(p_preset, p_in_app_path, p_ent_path, false);
  561. }
  562. }
  563. return err;
  564. }
  565. Error EditorExportPlatformMacOS::_export_macos_plugins_for(Ref<EditorExportPlugin> p_editor_export_plugin,
  566. const String &p_app_path_name, Ref<DirAccess> &dir_access,
  567. bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset,
  568. const String &p_ent_path) {
  569. Error error{ OK };
  570. const Vector<String> &macos_plugins{ p_editor_export_plugin->get_macos_plugin_files() };
  571. for (int i = 0; i < macos_plugins.size(); ++i) {
  572. String src_path{ ProjectSettings::get_singleton()->globalize_path(macos_plugins[i]) };
  573. String path_in_app{ p_app_path_name + "/Contents/PlugIns/" + src_path.get_file() };
  574. error = _copy_and_sign_files(dir_access, src_path, path_in_app, p_sign_enabled, p_preset, p_ent_path, false);
  575. if (error != OK) {
  576. break;
  577. }
  578. }
  579. return error;
  580. }
  581. Error EditorExportPlatformMacOS::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  582. List<String> args;
  583. if (FileAccess::exists(p_dmg_path)) {
  584. OS::get_singleton()->move_to_trash(p_dmg_path);
  585. }
  586. args.push_back("create");
  587. args.push_back(p_dmg_path);
  588. args.push_back("-volname");
  589. args.push_back(p_pkg_name);
  590. args.push_back("-fs");
  591. args.push_back("HFS+");
  592. args.push_back("-srcfolder");
  593. args.push_back(p_app_path_name);
  594. String str;
  595. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  596. if (err != OK) {
  597. add_message(EXPORT_MESSAGE_ERROR, TTR("DMG Creation"), TTR("Could not start hdiutil executable."));
  598. return err;
  599. }
  600. print_verbose("hdiutil returned: " + str);
  601. if (str.find("create failed") != -1) {
  602. if (str.find("File exists") != -1) {
  603. add_message(EXPORT_MESSAGE_ERROR, TTR("DMG Creation"), TTR("`hdiutil create` failed - file exists."));
  604. } else {
  605. add_message(EXPORT_MESSAGE_ERROR, TTR("DMG Creation"), TTR("`hdiutil create` failed."));
  606. }
  607. return FAILED;
  608. }
  609. return OK;
  610. }
  611. Error EditorExportPlatformMacOS::_export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path) {
  612. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE);
  613. if (f.is_null()) {
  614. add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), vformat(TTR("Could not open file \"%s\"."), p_path));
  615. return ERR_CANT_CREATE;
  616. }
  617. f->store_line("#!/bin/sh");
  618. f->store_line("echo -ne '\\033c\\033]0;" + p_app_name + "\\a'");
  619. f->store_line("");
  620. f->store_line("function app_realpath() {");
  621. f->store_line(" SOURCE=$1");
  622. f->store_line(" while [ -h \"$SOURCE\" ]; do");
  623. f->store_line(" DIR=$(dirname \"$SOURCE\")");
  624. f->store_line(" SOURCE=$(readlink \"$SOURCE\")");
  625. f->store_line(" [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE");
  626. f->store_line(" done");
  627. f->store_line(" echo \"$( cd -P \"$( dirname \"$SOURCE\" )\" >/dev/null 2>&1 && pwd )\"");
  628. f->store_line("}");
  629. f->store_line("");
  630. f->store_line("BASE_PATH=\"$(app_realpath \"${BASH_SOURCE[0]}\")\"");
  631. f->store_line("\"$BASE_PATH/" + p_pkg_name + "\" \"$@\"");
  632. f->store_line("");
  633. return OK;
  634. }
  635. Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  636. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  637. String src_pkg_name;
  638. EditorProgress ep("export", "Exporting for macOS", 3, true);
  639. if (p_debug) {
  640. src_pkg_name = p_preset->get("custom_template/debug");
  641. } else {
  642. src_pkg_name = p_preset->get("custom_template/release");
  643. }
  644. if (src_pkg_name.is_empty()) {
  645. String err;
  646. src_pkg_name = find_export_template("macos.zip", &err);
  647. if (src_pkg_name.is_empty()) {
  648. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("Export template not found."));
  649. return ERR_FILE_NOT_FOUND;
  650. }
  651. }
  652. if (!DirAccess::exists(p_path.get_base_dir())) {
  653. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), TTR("The given export path doesn't exist."));
  654. return ERR_FILE_BAD_PATH;
  655. }
  656. Ref<FileAccess> io_fa;
  657. zlib_filefunc_def io = zipio_create_io(&io_fa);
  658. if (ep.step(TTR("Creating app bundle"), 0)) {
  659. return ERR_SKIP;
  660. }
  661. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  662. if (!src_pkg_zip) {
  663. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not find template app to export: \"%s\"."), src_pkg_name));
  664. return ERR_FILE_NOT_FOUND;
  665. }
  666. int ret = unzGoToFirstFile(src_pkg_zip);
  667. String binary_to_use = "godot_macos_" + String(p_debug ? "debug" : "release") + ".universal";
  668. String pkg_name;
  669. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  670. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  671. } else {
  672. pkg_name = "Unnamed";
  673. }
  674. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  675. String export_format;
  676. if (use_dmg() && p_path.ends_with("dmg")) {
  677. export_format = "dmg";
  678. } else if (p_path.ends_with("zip")) {
  679. export_format = "zip";
  680. } else if (p_path.ends_with("app")) {
  681. export_format = "app";
  682. } else {
  683. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid export format."));
  684. return ERR_CANT_CREATE;
  685. }
  686. // Create our application bundle.
  687. String tmp_app_dir_name = pkg_name + ".app";
  688. String tmp_base_path_name;
  689. String tmp_app_path_name;
  690. String scr_path;
  691. if (export_format == "app") {
  692. tmp_base_path_name = p_path.get_base_dir();
  693. tmp_app_path_name = p_path;
  694. scr_path = p_path.get_basename() + ".command";
  695. } else {
  696. tmp_base_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name);
  697. tmp_app_path_name = tmp_base_path_name.plus_file(tmp_app_dir_name);
  698. scr_path = tmp_base_path_name.plus_file(pkg_name + ".command");
  699. }
  700. print_verbose("Exporting to " + tmp_app_path_name);
  701. Error err = OK;
  702. Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_base_path_name);
  703. if (tmp_app_dir.is_null()) {
  704. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory: \"%s\"."), tmp_base_path_name));
  705. err = ERR_CANT_CREATE;
  706. }
  707. DirAccess::remove_file_or_error(scr_path);
  708. if (DirAccess::exists(tmp_app_path_name)) {
  709. String old_dir = tmp_app_dir->get_current_dir();
  710. if (tmp_app_dir->change_dir(tmp_app_path_name) == OK) {
  711. tmp_app_dir->erase_contents_recursive();
  712. tmp_app_dir->change_dir(old_dir);
  713. }
  714. }
  715. Array helpers = p_preset->get("codesign/entitlements/app_sandbox/helper_executables");
  716. // Create our folder structure.
  717. if (err == OK) {
  718. print_verbose("Creating " + tmp_app_path_name + "/Contents/MacOS");
  719. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  720. if (err != OK) {
  721. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), tmp_app_path_name + "/Contents/MacOS"));
  722. }
  723. }
  724. if (err == OK) {
  725. print_verbose("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  726. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  727. if (err != OK) {
  728. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), tmp_app_path_name + "/Contents/Frameworks"));
  729. }
  730. }
  731. if ((err == OK) && helpers.size() > 0) {
  732. print_line("Creating " + tmp_app_path_name + "/Contents/Helpers");
  733. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Helpers");
  734. if (err != OK) {
  735. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), tmp_app_path_name + "/Contents/Helpers"));
  736. }
  737. }
  738. if (err == OK) {
  739. print_verbose("Creating " + tmp_app_path_name + "/Contents/Resources");
  740. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  741. if (err != OK) {
  742. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), tmp_app_path_name + "/Contents/Resources"));
  743. }
  744. }
  745. Dictionary appnames = ProjectSettings::get_singleton()->get("application/config/name_localized");
  746. Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
  747. Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
  748. Dictionary location_usage_descriptions = p_preset->get("privacy/location_usage_description_localized");
  749. Dictionary address_book_usage_descriptions = p_preset->get("privacy/address_book_usage_description_localized");
  750. Dictionary calendar_usage_descriptions = p_preset->get("privacy/calendar_usage_description_localized");
  751. Dictionary photos_library_usage_descriptions = p_preset->get("privacy/photos_library_usage_description_localized");
  752. Dictionary desktop_folder_usage_descriptions = p_preset->get("privacy/desktop_folder_usage_description_localized");
  753. Dictionary documents_folder_usage_descriptions = p_preset->get("privacy/documents_folder_usage_description_localized");
  754. Dictionary downloads_folder_usage_descriptions = p_preset->get("privacy/downloads_folder_usage_description_localized");
  755. Dictionary network_volumes_usage_descriptions = p_preset->get("privacy/network_volumes_usage_description_localized");
  756. Dictionary removable_volumes_usage_descriptions = p_preset->get("privacy/removable_volumes_usage_description_localized");
  757. Dictionary copyrights = p_preset->get("application/copyright_localized");
  758. Vector<String> translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  759. if (translations.size() > 0) {
  760. {
  761. String fname = tmp_app_path_name + "/Contents/Resources/en.lproj";
  762. tmp_app_dir->make_dir_recursive(fname);
  763. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  764. f->store_line("/* Localized versions of Info.plist keys */");
  765. f->store_line("");
  766. f->store_line("CFBundleDisplayName = \"" + ProjectSettings::get_singleton()->get("application/config/name").operator String() + "\";");
  767. if (!((String)p_preset->get("privacy/microphone_usage_description")).is_empty()) {
  768. f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
  769. }
  770. if (!((String)p_preset->get("privacy/camera_usage_description")).is_empty()) {
  771. f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
  772. }
  773. if (!((String)p_preset->get("privacy/location_usage_description")).is_empty()) {
  774. f->store_line("NSLocationUsageDescription = \"" + p_preset->get("privacy/location_usage_description").operator String() + "\";");
  775. }
  776. if (!((String)p_preset->get("privacy/address_book_usage_description")).is_empty()) {
  777. f->store_line("NSContactsUsageDescription = \"" + p_preset->get("privacy/address_book_usage_description").operator String() + "\";");
  778. }
  779. if (!((String)p_preset->get("privacy/calendar_usage_description")).is_empty()) {
  780. f->store_line("NSCalendarsUsageDescription = \"" + p_preset->get("privacy/calendar_usage_description").operator String() + "\";");
  781. }
  782. if (!((String)p_preset->get("privacy/photos_library_usage_description")).is_empty()) {
  783. f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photos_library_usage_description").operator String() + "\";");
  784. }
  785. if (!((String)p_preset->get("privacy/desktop_folder_usage_description")).is_empty()) {
  786. f->store_line("NSDesktopFolderUsageDescription = \"" + p_preset->get("privacy/desktop_folder_usage_description").operator String() + "\";");
  787. }
  788. if (!((String)p_preset->get("privacy/documents_folder_usage_description")).is_empty()) {
  789. f->store_line("NSDocumentsFolderUsageDescription = \"" + p_preset->get("privacy/documents_folder_usage_description").operator String() + "\";");
  790. }
  791. if (!((String)p_preset->get("privacy/downloads_folder_usage_description")).is_empty()) {
  792. f->store_line("NSDownloadsFolderUsageDescription = \"" + p_preset->get("privacy/downloads_folder_usage_description").operator String() + "\";");
  793. }
  794. if (!((String)p_preset->get("privacy/network_volumes_usage_description")).is_empty()) {
  795. f->store_line("NSNetworkVolumesUsageDescription = \"" + p_preset->get("privacy/network_volumes_usage_description").operator String() + "\";");
  796. }
  797. if (!((String)p_preset->get("privacy/removable_volumes_usage_description")).is_empty()) {
  798. f->store_line("NSRemovableVolumesUsageDescription = \"" + p_preset->get("privacy/removable_volumes_usage_description").operator String() + "\";");
  799. }
  800. f->store_line("NSHumanReadableCopyright = \"" + p_preset->get("application/copyright").operator String() + "\";");
  801. }
  802. for (const String &E : translations) {
  803. Ref<Translation> tr = ResourceLoader::load(E);
  804. if (tr.is_valid()) {
  805. String lang = tr->get_locale();
  806. String fname = tmp_app_path_name + "/Contents/Resources/" + lang + ".lproj";
  807. tmp_app_dir->make_dir_recursive(fname);
  808. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  809. f->store_line("/* Localized versions of Info.plist keys */");
  810. f->store_line("");
  811. if (appnames.has(lang)) {
  812. f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
  813. }
  814. if (microphone_usage_descriptions.has(lang)) {
  815. f->store_line("NSMicrophoneUsageDescription = \"" + microphone_usage_descriptions[lang].operator String() + "\";");
  816. }
  817. if (camera_usage_descriptions.has(lang)) {
  818. f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
  819. }
  820. if (location_usage_descriptions.has(lang)) {
  821. f->store_line("NSLocationUsageDescription = \"" + location_usage_descriptions[lang].operator String() + "\";");
  822. }
  823. if (address_book_usage_descriptions.has(lang)) {
  824. f->store_line("NSContactsUsageDescription = \"" + address_book_usage_descriptions[lang].operator String() + "\";");
  825. }
  826. if (calendar_usage_descriptions.has(lang)) {
  827. f->store_line("NSCalendarsUsageDescription = \"" + calendar_usage_descriptions[lang].operator String() + "\";");
  828. }
  829. if (photos_library_usage_descriptions.has(lang)) {
  830. f->store_line("NSPhotoLibraryUsageDescription = \"" + photos_library_usage_descriptions[lang].operator String() + "\";");
  831. }
  832. if (desktop_folder_usage_descriptions.has(lang)) {
  833. f->store_line("NSDesktopFolderUsageDescription = \"" + desktop_folder_usage_descriptions[lang].operator String() + "\";");
  834. }
  835. if (documents_folder_usage_descriptions.has(lang)) {
  836. f->store_line("NSDocumentsFolderUsageDescription = \"" + documents_folder_usage_descriptions[lang].operator String() + "\";");
  837. }
  838. if (downloads_folder_usage_descriptions.has(lang)) {
  839. f->store_line("NSDownloadsFolderUsageDescription = \"" + downloads_folder_usage_descriptions[lang].operator String() + "\";");
  840. }
  841. if (network_volumes_usage_descriptions.has(lang)) {
  842. f->store_line("NSNetworkVolumesUsageDescription = \"" + network_volumes_usage_descriptions[lang].operator String() + "\";");
  843. }
  844. if (removable_volumes_usage_descriptions.has(lang)) {
  845. f->store_line("NSRemovableVolumesUsageDescription = \"" + removable_volumes_usage_descriptions[lang].operator String() + "\";");
  846. }
  847. if (copyrights.has(lang)) {
  848. f->store_line("NSHumanReadableCopyright = \"" + copyrights[lang].operator String() + "\";");
  849. }
  850. }
  851. }
  852. }
  853. // Now process our template.
  854. bool found_binary = false;
  855. Vector<String> dylibs_found;
  856. while (ret == UNZ_OK && err == OK) {
  857. bool is_execute = false;
  858. // Get filename.
  859. unz_file_info info;
  860. char fname[16384];
  861. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  862. if (ret != UNZ_OK) {
  863. break;
  864. }
  865. String file = String::utf8(fname);
  866. Vector<uint8_t> data;
  867. data.resize(info.uncompressed_size);
  868. // Read.
  869. unzOpenCurrentFile(src_pkg_zip);
  870. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  871. unzCloseCurrentFile(src_pkg_zip);
  872. // Write.
  873. file = file.replace_first("macos_template.app/", "");
  874. if (((info.external_fa >> 16L) & 0120000) == 0120000) {
  875. #ifndef UNIX_ENABLED
  876. add_message(EXPORT_MESSAGE_INFO, TTR("Export"), TTR("Relative symlinks are not supported on this OS, the exported project might be broken!"));
  877. #endif
  878. // Handle symlinks in the archive.
  879. file = tmp_app_path_name.plus_file(file);
  880. if (err == OK) {
  881. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  882. if (err != OK) {
  883. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), file.get_base_dir()));
  884. }
  885. }
  886. if (err == OK) {
  887. String lnk_data = String::utf8((const char *)data.ptr(), data.size());
  888. err = tmp_app_dir->create_link(lnk_data, file);
  889. if (err != OK) {
  890. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not created symlink \"%s\" -> \"%s\"."), lnk_data, file));
  891. }
  892. print_verbose(vformat("ADDING SYMLINK %s => %s\n", file, lnk_data));
  893. }
  894. ret = unzGoToNextFile(src_pkg_zip);
  895. continue; // next
  896. }
  897. if (file == "Contents/Info.plist") {
  898. _fix_plist(p_preset, data, pkg_name);
  899. }
  900. if (file.begins_with("Contents/MacOS/godot_")) {
  901. if (file != "Contents/MacOS/" + binary_to_use) {
  902. ret = unzGoToNextFile(src_pkg_zip);
  903. continue; // skip
  904. }
  905. found_binary = true;
  906. is_execute = true;
  907. file = "Contents/MacOS/" + pkg_name;
  908. }
  909. if (file == "Contents/Resources/icon.icns") {
  910. // See if there is an icon.
  911. String iconpath;
  912. if (p_preset->get("application/icon") != "") {
  913. iconpath = p_preset->get("application/icon");
  914. } else {
  915. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  916. }
  917. if (!iconpath.is_empty()) {
  918. if (iconpath.get_extension() == "icns") {
  919. Ref<FileAccess> icon = FileAccess::open(iconpath, FileAccess::READ);
  920. if (icon.is_valid()) {
  921. data.resize(icon->get_length());
  922. icon->get_buffer(&data.write[0], icon->get_length());
  923. }
  924. } else {
  925. Ref<Image> icon;
  926. icon.instantiate();
  927. icon->load(iconpath);
  928. if (!icon->is_empty()) {
  929. _make_icon(icon, data);
  930. }
  931. }
  932. }
  933. }
  934. if (data.size() > 0) {
  935. if (file.find("/data.mono.macos.release_debug.universal/") != -1) {
  936. if (!p_debug) {
  937. ret = unzGoToNextFile(src_pkg_zip);
  938. continue; // skip
  939. }
  940. file = file.replace("/data.mono.macos.release_debug.universal/", "/GodotSharp/");
  941. }
  942. if (file.find("/data.mono.macos.release.universal/") != -1) {
  943. if (p_debug) {
  944. ret = unzGoToNextFile(src_pkg_zip);
  945. continue; // skip
  946. }
  947. file = file.replace("/data.mono.macos.release.universal/", "/GodotSharp/");
  948. }
  949. if (file.ends_with(".dylib")) {
  950. dylibs_found.push_back(file);
  951. }
  952. print_verbose("ADDING: " + file + " size: " + itos(data.size()));
  953. // Write it into our application bundle.
  954. file = tmp_app_path_name.plus_file(file);
  955. if (err == OK) {
  956. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  957. if (err != OK) {
  958. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), file.get_base_dir()));
  959. }
  960. }
  961. if (err == OK) {
  962. Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);
  963. if (f.is_valid()) {
  964. f->store_buffer(data.ptr(), data.size());
  965. f.unref();
  966. if (is_execute) {
  967. // chmod with 0755 if the file is executable.
  968. FileAccess::set_unix_permissions(file, 0755);
  969. }
  970. } else {
  971. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not open \"%s\"."), file));
  972. err = ERR_CANT_CREATE;
  973. }
  974. }
  975. }
  976. ret = unzGoToNextFile(src_pkg_zip);
  977. }
  978. // We're done with our source zip.
  979. unzClose(src_pkg_zip);
  980. if (!found_binary) {
  981. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Requested template binary \"%s\" not found. It might be missing from your template archive."), binary_to_use));
  982. err = ERR_FILE_NOT_FOUND;
  983. }
  984. // Save console script.
  985. if (err == OK) {
  986. int con_scr = p_preset->get("debug/export_console_script");
  987. if ((con_scr == 1 && p_debug) || (con_scr == 2)) {
  988. err = _export_debug_script(p_preset, pkg_name, tmp_app_path_name.get_file() + "/Contents/MacOS/" + pkg_name, scr_path);
  989. FileAccess::set_unix_permissions(scr_path, 0755);
  990. if (err != OK) {
  991. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not create console script."));
  992. }
  993. }
  994. }
  995. if (err == OK) {
  996. if (ep.step(TTR("Making PKG"), 1)) {
  997. return ERR_SKIP;
  998. }
  999. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  1000. Vector<SharedObject> shared_objects;
  1001. err = save_pack(p_preset, p_debug, pack_path, &shared_objects);
  1002. // See if we can code sign our new package.
  1003. bool sign_enabled = p_preset->get("codesign/enable");
  1004. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  1005. String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + "_helper.entitlements");
  1006. if (sign_enabled && (ent_path.is_empty())) {
  1007. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  1008. Ref<FileAccess> ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  1009. if (ent_f.is_valid()) {
  1010. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1011. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  1012. ent_f->store_line("<plist version=\"1.0\">");
  1013. ent_f->store_line("<dict>");
  1014. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  1015. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  1016. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  1017. ent_f->store_line("<true/>");
  1018. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  1019. ent_f->store_line("<true/>");
  1020. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  1021. ent_f->store_line("<true/>");
  1022. } else {
  1023. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  1024. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  1025. ent_f->store_line("<true/>");
  1026. }
  1027. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  1028. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  1029. ent_f->store_line("<true/>");
  1030. }
  1031. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  1032. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  1033. ent_f->store_line("<true/>");
  1034. }
  1035. }
  1036. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  1037. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  1038. ent_f->store_line("<true/>");
  1039. }
  1040. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  1041. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  1042. ent_f->store_line("<true/>");
  1043. }
  1044. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  1045. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  1046. ent_f->store_line("<true/>");
  1047. }
  1048. if ((bool)p_preset->get("codesign/entitlements/location")) {
  1049. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  1050. ent_f->store_line("<true/>");
  1051. }
  1052. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  1053. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  1054. ent_f->store_line("<true/>");
  1055. }
  1056. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  1057. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  1058. ent_f->store_line("<true/>");
  1059. }
  1060. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  1061. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  1062. ent_f->store_line("<true/>");
  1063. }
  1064. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  1065. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  1066. ent_f->store_line("<true/>");
  1067. }
  1068. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  1069. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  1070. ent_f->store_line("<true/>");
  1071. }
  1072. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  1073. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  1074. ent_f->store_line("<true/>");
  1075. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  1076. ent_f->store_line("<key>com.apple.security.network.server</key>");
  1077. ent_f->store_line("<true/>");
  1078. }
  1079. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  1080. ent_f->store_line("<key>com.apple.security.network.client</key>");
  1081. ent_f->store_line("<true/>");
  1082. }
  1083. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  1084. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  1085. ent_f->store_line("<true/>");
  1086. }
  1087. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  1088. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  1089. ent_f->store_line("<true/>");
  1090. }
  1091. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  1092. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  1093. ent_f->store_line("<true/>");
  1094. }
  1095. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  1096. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  1097. ent_f->store_line("<true/>");
  1098. }
  1099. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  1100. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  1101. ent_f->store_line("<true/>");
  1102. }
  1103. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  1104. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  1105. ent_f->store_line("<true/>");
  1106. }
  1107. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  1108. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  1109. ent_f->store_line("<true/>");
  1110. }
  1111. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  1112. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  1113. ent_f->store_line("<true/>");
  1114. }
  1115. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  1116. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  1117. ent_f->store_line("<true/>");
  1118. }
  1119. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  1120. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  1121. ent_f->store_line("<true/>");
  1122. }
  1123. }
  1124. ent_f->store_line("</dict>");
  1125. ent_f->store_line("</plist>");
  1126. } else {
  1127. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not create entitlements file."));
  1128. err = ERR_CANT_CREATE;
  1129. }
  1130. if ((err == OK) && helpers.size() > 0) {
  1131. ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
  1132. if (ent_f.is_valid()) {
  1133. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1134. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  1135. ent_f->store_line("<plist version=\"1.0\">");
  1136. ent_f->store_line("<dict>");
  1137. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  1138. ent_f->store_line("<true/>");
  1139. ent_f->store_line("<key>com.apple.security.inherit</key>");
  1140. ent_f->store_line("<true/>");
  1141. ent_f->store_line("</dict>");
  1142. ent_f->store_line("</plist>");
  1143. } else {
  1144. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not create helper entitlements file."));
  1145. err = ERR_CANT_CREATE;
  1146. }
  1147. }
  1148. }
  1149. if ((err == OK) && helpers.size() > 0) {
  1150. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1151. for (int i = 0; i < helpers.size(); i++) {
  1152. String hlp_path = helpers[i];
  1153. err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
  1154. if (err == OK && sign_enabled) {
  1155. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path, false);
  1156. }
  1157. FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
  1158. }
  1159. }
  1160. bool ad_hoc = true;
  1161. if (err == OK) {
  1162. #ifdef MACOS_ENABLED
  1163. String sign_identity = p_preset->get("codesign/identity");
  1164. #else
  1165. String sign_identity = "-";
  1166. #endif
  1167. ad_hoc = (sign_identity == "" || sign_identity == "-");
  1168. bool lib_validation = p_preset->get("codesign/entitlements/disable_library_validation");
  1169. if ((!dylibs_found.is_empty() || !shared_objects.is_empty()) && sign_enabled && ad_hoc && !lib_validation) {
  1170. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Ad-hoc signed applications require the 'Disable Library Validation' entitlement to load dynamic libraries."));
  1171. err = ERR_CANT_CREATE;
  1172. }
  1173. }
  1174. if (err == OK) {
  1175. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1176. for (int i = 0; i < shared_objects.size(); i++) {
  1177. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  1178. if (shared_objects[i].target.is_empty()) {
  1179. String path_in_app = tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file();
  1180. err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, true);
  1181. } else {
  1182. String path_in_app = tmp_app_path_name.plus_file(shared_objects[i].target).plus_file(src_path.get_file());
  1183. err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, false);
  1184. }
  1185. if (err != OK) {
  1186. break;
  1187. }
  1188. }
  1189. Vector<Ref<EditorExportPlugin>> export_plugins{ EditorExport::get_singleton()->get_export_plugins() };
  1190. for (int i = 0; i < export_plugins.size(); ++i) {
  1191. err = _export_macos_plugins_for(export_plugins[i], tmp_app_path_name, da, sign_enabled, p_preset, ent_path);
  1192. if (err != OK) {
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. if (sign_enabled) {
  1198. for (int i = 0; i < dylibs_found.size(); i++) {
  1199. if (err == OK) {
  1200. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path, false);
  1201. }
  1202. }
  1203. }
  1204. if (err == OK && sign_enabled) {
  1205. if (ep.step(TTR("Code signing bundle"), 2)) {
  1206. return ERR_SKIP;
  1207. }
  1208. err = _code_sign(p_preset, tmp_app_path_name, ent_path);
  1209. }
  1210. if (export_format == "dmg") {
  1211. // Create a DMG.
  1212. if (err == OK) {
  1213. if (ep.step(TTR("Making DMG"), 3)) {
  1214. return ERR_SKIP;
  1215. }
  1216. err = _create_dmg(p_path, pkg_name, tmp_base_path_name);
  1217. }
  1218. // Sign DMG.
  1219. if (err == OK && sign_enabled && !ad_hoc) {
  1220. if (ep.step(TTR("Code signing DMG"), 3)) {
  1221. return ERR_SKIP;
  1222. }
  1223. err = _code_sign(p_preset, p_path, ent_path, false);
  1224. }
  1225. } else if (export_format == "zip") {
  1226. // Create ZIP.
  1227. if (err == OK) {
  1228. if (ep.step(TTR("Making ZIP"), 3)) {
  1229. return ERR_SKIP;
  1230. }
  1231. if (FileAccess::exists(p_path)) {
  1232. OS::get_singleton()->move_to_trash(p_path);
  1233. }
  1234. Ref<FileAccess> io_fa_dst;
  1235. zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst);
  1236. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  1237. _zip_folder_recursive(zip, tmp_base_path_name, "", pkg_name);
  1238. zipClose(zip, nullptr);
  1239. }
  1240. }
  1241. #ifdef MACOS_ENABLED
  1242. bool noto_enabled = p_preset->get("notarization/enable");
  1243. if (err == OK && noto_enabled) {
  1244. if (export_format == "app") {
  1245. add_message(EXPORT_MESSAGE_INFO, TTR("Notarization"), TTR("Notarization requires the app to be archived first, select the DMG or ZIP export format instead."));
  1246. } else {
  1247. if (ep.step(TTR("Sending archive for notarization"), 4)) {
  1248. return ERR_SKIP;
  1249. }
  1250. err = _notarize(p_preset, p_path);
  1251. }
  1252. }
  1253. #endif
  1254. // Clean up temporary entitlements files.
  1255. DirAccess::remove_file_or_error(hlp_ent_path);
  1256. // Clean up temporary .app dir and generated entitlements.
  1257. if ((String)(p_preset->get("codesign/entitlements/custom_file")) == "") {
  1258. tmp_app_dir->remove(ent_path);
  1259. }
  1260. if (export_format != "app") {
  1261. if (tmp_app_dir->change_dir(tmp_base_path_name) == OK) {
  1262. tmp_app_dir->erase_contents_recursive();
  1263. tmp_app_dir->change_dir("..");
  1264. tmp_app_dir->remove(pkg_name);
  1265. }
  1266. }
  1267. }
  1268. return err;
  1269. }
  1270. void EditorExportPlatformMacOS::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  1271. String dir = p_folder.is_empty() ? p_root_path : p_root_path.plus_file(p_folder);
  1272. Ref<DirAccess> da = DirAccess::open(dir);
  1273. da->list_dir_begin();
  1274. String f = da->get_next();
  1275. while (!f.is_empty()) {
  1276. if (f == "." || f == "..") {
  1277. f = da->get_next();
  1278. continue;
  1279. }
  1280. if (da->is_link(f)) {
  1281. OS::Time time = OS::get_singleton()->get_time();
  1282. OS::Date date = OS::get_singleton()->get_date();
  1283. zip_fileinfo zipfi;
  1284. zipfi.tmz_date.tm_hour = time.hour;
  1285. zipfi.tmz_date.tm_mday = date.day;
  1286. zipfi.tmz_date.tm_min = time.minute;
  1287. 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/
  1288. zipfi.tmz_date.tm_sec = time.second;
  1289. zipfi.tmz_date.tm_year = date.year;
  1290. zipfi.dosDate = 0;
  1291. // 0120000: symbolic link type
  1292. // 0000755: permissions rwxr-xr-x
  1293. // 0000644: permissions rw-r--r--
  1294. uint32_t _mode = 0120644;
  1295. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1296. zipfi.internal_fa = 0;
  1297. zipOpenNewFileInZip4(p_zip,
  1298. p_folder.plus_file(f).utf8().get_data(),
  1299. &zipfi,
  1300. nullptr,
  1301. 0,
  1302. nullptr,
  1303. 0,
  1304. nullptr,
  1305. Z_DEFLATED,
  1306. Z_DEFAULT_COMPRESSION,
  1307. 0,
  1308. -MAX_WBITS,
  1309. DEF_MEM_LEVEL,
  1310. Z_DEFAULT_STRATEGY,
  1311. nullptr,
  1312. 0,
  1313. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1314. 0);
  1315. String target = da->read_link(f);
  1316. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  1317. zipCloseFileInZip(p_zip);
  1318. } else if (da->current_is_dir()) {
  1319. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  1320. } else {
  1321. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name)) || p_folder.ends_with("Helpers") || f.ends_with(".command");
  1322. OS::Time time = OS::get_singleton()->get_time();
  1323. OS::Date date = OS::get_singleton()->get_date();
  1324. zip_fileinfo zipfi;
  1325. zipfi.tmz_date.tm_hour = time.hour;
  1326. zipfi.tmz_date.tm_mday = date.day;
  1327. zipfi.tmz_date.tm_min = time.minute;
  1328. 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/
  1329. zipfi.tmz_date.tm_sec = time.second;
  1330. zipfi.tmz_date.tm_year = date.year;
  1331. zipfi.dosDate = 0;
  1332. // 0100000: regular file type
  1333. // 0000755: permissions rwxr-xr-x
  1334. // 0000644: permissions rw-r--r--
  1335. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  1336. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1337. zipfi.internal_fa = 0;
  1338. zipOpenNewFileInZip4(p_zip,
  1339. p_folder.plus_file(f).utf8().get_data(),
  1340. &zipfi,
  1341. nullptr,
  1342. 0,
  1343. nullptr,
  1344. 0,
  1345. nullptr,
  1346. Z_DEFLATED,
  1347. Z_DEFAULT_COMPRESSION,
  1348. 0,
  1349. -MAX_WBITS,
  1350. DEF_MEM_LEVEL,
  1351. Z_DEFAULT_STRATEGY,
  1352. nullptr,
  1353. 0,
  1354. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1355. 0);
  1356. Ref<FileAccess> fa = FileAccess::open(dir.plus_file(f), FileAccess::READ);
  1357. if (fa.is_null()) {
  1358. add_message(EXPORT_MESSAGE_ERROR, TTR("ZIP Creation"), vformat(TTR("Could not open file to read from path \"%s\"."), dir.plus_file(f)));
  1359. return;
  1360. }
  1361. const int bufsize = 16384;
  1362. uint8_t buf[bufsize];
  1363. while (true) {
  1364. uint64_t got = fa->get_buffer(buf, bufsize);
  1365. if (got == 0) {
  1366. break;
  1367. }
  1368. zipWriteInFileInZip(p_zip, buf, got);
  1369. }
  1370. zipCloseFileInZip(p_zip);
  1371. }
  1372. f = da->get_next();
  1373. }
  1374. da->list_dir_end();
  1375. }
  1376. bool EditorExportPlatformMacOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1377. String err;
  1378. bool valid = false;
  1379. // Look for export templates (custom templates).
  1380. bool dvalid = false;
  1381. bool rvalid = false;
  1382. if (p_preset->get("custom_template/debug") != "") {
  1383. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1384. if (!dvalid) {
  1385. err += TTR("Custom debug template not found.") + "\n";
  1386. }
  1387. }
  1388. if (p_preset->get("custom_template/release") != "") {
  1389. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1390. if (!rvalid) {
  1391. err += TTR("Custom release template not found.") + "\n";
  1392. }
  1393. }
  1394. // Look for export templates (official templates, check only is custom templates are not set).
  1395. if (!dvalid || !rvalid) {
  1396. dvalid = exists_export_template("macos.zip", &err);
  1397. rvalid = dvalid; // Both in the same ZIP.
  1398. }
  1399. valid = dvalid || rvalid;
  1400. r_missing_templates = !valid;
  1401. String identifier = p_preset->get("application/bundle_identifier");
  1402. String pn_err;
  1403. if (!is_package_name_valid(identifier, &pn_err)) {
  1404. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  1405. valid = false;
  1406. }
  1407. bool sign_enabled = p_preset->get("codesign/enable");
  1408. #ifdef MACOS_ENABLED
  1409. bool noto_enabled = p_preset->get("notarization/enable");
  1410. bool ad_hoc = ((p_preset->get("codesign/identity") == "") || (p_preset->get("codesign/identity") == "-"));
  1411. if (!ad_hoc && (bool)EditorSettings::get_singleton()->get("export/macos/force_builtin_codesign")) {
  1412. err += TTR("Warning: Built-in \"codesign\" is selected in the Editor Settings. Code signing is limited to ad-hoc signature only.") + "\n";
  1413. }
  1414. if (!ad_hoc && !FileAccess::exists("/usr/bin/codesign") && !FileAccess::exists("/bin/codesign")) {
  1415. err += TTR("Warning: Xcode command line tools are not installed, using built-in \"codesign\". Code signing is limited to ad-hoc signature only.") + "\n";
  1416. }
  1417. if (noto_enabled) {
  1418. if (ad_hoc) {
  1419. err += TTR("Notarization: Notarization with an ad-hoc signature is not supported.") + "\n";
  1420. valid = false;
  1421. }
  1422. if (!sign_enabled) {
  1423. err += TTR("Notarization: Code signing is required for notarization.") + "\n";
  1424. valid = false;
  1425. }
  1426. if (!(bool)p_preset->get("codesign/hardened_runtime")) {
  1427. err += TTR("Notarization: Hardened runtime is required for notarization.") + "\n";
  1428. valid = false;
  1429. }
  1430. if (!(bool)p_preset->get("codesign/timestamp")) {
  1431. err += TTR("Notarization: Timestamping is required for notarization.") + "\n";
  1432. valid = false;
  1433. }
  1434. if (p_preset->get("notarization/apple_id_name") == "") {
  1435. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  1436. valid = false;
  1437. }
  1438. if (p_preset->get("notarization/apple_id_password") == "") {
  1439. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  1440. valid = false;
  1441. }
  1442. } else {
  1443. err += TTR("Warning: Notarization is disabled. The exported project will be blocked by Gatekeeper if it's downloaded from an unknown source.") + "\n";
  1444. if (!sign_enabled) {
  1445. err += TTR("Code signing is disabled. The exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1446. } else {
  1447. if ((bool)p_preset->get("codesign/hardened_runtime") && ad_hoc) {
  1448. err += TTR("Hardened Runtime is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1449. }
  1450. if ((bool)p_preset->get("codesign/timestamp") && ad_hoc) {
  1451. err += TTR("Timestamping is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1452. }
  1453. }
  1454. }
  1455. #else
  1456. err += TTR("Warning: Notarization is not supported from this OS. The exported project will be blocked by Gatekeeper if it's downloaded from an unknown source.") + "\n";
  1457. if (!sign_enabled) {
  1458. err += TTR("Code signing is disabled. The exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1459. }
  1460. #endif
  1461. if (sign_enabled) {
  1462. if ((bool)p_preset->get("codesign/entitlements/audio_input") && ((String)p_preset->get("privacy/microphone_usage_description")).is_empty()) {
  1463. err += TTR("Privacy: Microphone access is enabled, but usage description is not specified.") + "\n";
  1464. valid = false;
  1465. }
  1466. if ((bool)p_preset->get("codesign/entitlements/camera") && ((String)p_preset->get("privacy/camera_usage_description")).is_empty()) {
  1467. err += TTR("Privacy: Camera access is enabled, but usage description is not specified.") + "\n";
  1468. valid = false;
  1469. }
  1470. if ((bool)p_preset->get("codesign/entitlements/location") && ((String)p_preset->get("privacy/location_usage_description")).is_empty()) {
  1471. err += TTR("Privacy: Location information access is enabled, but usage description is not specified.") + "\n";
  1472. valid = false;
  1473. }
  1474. if ((bool)p_preset->get("codesign/entitlements/address_book") && ((String)p_preset->get("privacy/address_book_usage_description")).is_empty()) {
  1475. err += TTR("Privacy: Address book access is enabled, but usage description is not specified.") + "\n";
  1476. valid = false;
  1477. }
  1478. if ((bool)p_preset->get("codesign/entitlements/calendars") && ((String)p_preset->get("privacy/calendar_usage_description")).is_empty()) {
  1479. err += TTR("Privacy: Calendar access is enabled, but usage description is not specified.") + "\n";
  1480. valid = false;
  1481. }
  1482. if ((bool)p_preset->get("codesign/entitlements/photos_library") && ((String)p_preset->get("privacy/photos_library_usage_description")).is_empty()) {
  1483. err += TTR("Privacy: Photo library access is enabled, but usage description is not specified.") + "\n";
  1484. valid = false;
  1485. }
  1486. }
  1487. if (!err.is_empty()) {
  1488. r_error = err;
  1489. }
  1490. return valid;
  1491. }
  1492. EditorExportPlatformMacOS::EditorExportPlatformMacOS() {
  1493. logo = ImageTexture::create_from_image(memnew(Image(_macos_logo)));
  1494. }
  1495. EditorExportPlatformMacOS::~EditorExportPlatformMacOS() {
  1496. }