export_plugin.cpp 68 KB

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