export_plugin.cpp 74 KB

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