export_plugin.cpp 70 KB

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