export_plugin.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. Ref<FileAccess> io_fa;
  628. zlib_filefunc_def io = zipio_create_io(&io_fa);
  629. if (ep.step(TTR("Creating app bundle"), 0)) {
  630. return ERR_SKIP;
  631. }
  632. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  633. if (!src_pkg_zip) {
  634. EditorNode::add_io_error(TTR("Could not find template app to export:") + "\n" + src_pkg_name);
  635. return ERR_FILE_NOT_FOUND;
  636. }
  637. int ret = unzGoToFirstFile(src_pkg_zip);
  638. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  639. String pkg_name;
  640. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  641. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  642. } else {
  643. pkg_name = "Unnamed";
  644. }
  645. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  646. String export_format;
  647. if (use_dmg() && p_path.ends_with("dmg")) {
  648. export_format = "dmg";
  649. } else if (p_path.ends_with("zip")) {
  650. export_format = "zip";
  651. } else if (p_path.ends_with("app")) {
  652. export_format = "app";
  653. } else {
  654. EditorNode::add_io_error("Invalid export format");
  655. return ERR_CANT_CREATE;
  656. }
  657. // Create our application bundle.
  658. String tmp_app_dir_name = pkg_name + ".app";
  659. String tmp_base_path_name;
  660. String tmp_app_path_name;
  661. String scr_path;
  662. if (export_format == "app") {
  663. tmp_base_path_name = p_path.get_base_dir();
  664. tmp_app_path_name = p_path;
  665. scr_path = p_path.get_basename() + ".command";
  666. } else {
  667. tmp_base_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name);
  668. tmp_app_path_name = tmp_base_path_name.plus_file(tmp_app_dir_name);
  669. scr_path = tmp_base_path_name.plus_file(pkg_name + ".command");
  670. }
  671. print_verbose("Exporting to " + tmp_app_path_name);
  672. Error err = OK;
  673. Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_base_path_name);
  674. if (tmp_app_dir.is_null()) {
  675. err = ERR_CANT_CREATE;
  676. }
  677. DirAccess::remove_file_or_error(scr_path);
  678. if (DirAccess::exists(tmp_app_path_name)) {
  679. if (tmp_app_dir->change_dir(tmp_app_path_name) == OK) {
  680. tmp_app_dir->erase_contents_recursive();
  681. }
  682. }
  683. Array helpers = p_preset->get("codesign/entitlements/app_sandbox/helper_executables");
  684. // Create our folder structure.
  685. if (err == OK) {
  686. print_verbose("Creating " + tmp_app_path_name + "/Contents/MacOS");
  687. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  688. }
  689. if (err == OK) {
  690. print_verbose("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  691. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  692. }
  693. if ((err == OK) && helpers.size() > 0) {
  694. print_line("Creating " + tmp_app_path_name + "/Contents/Helpers");
  695. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Helpers");
  696. }
  697. if (err == OK) {
  698. print_verbose("Creating " + tmp_app_path_name + "/Contents/Resources");
  699. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  700. }
  701. Dictionary appnames = ProjectSettings::get_singleton()->get("application/config/name_localized");
  702. Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
  703. Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
  704. Dictionary location_usage_descriptions = p_preset->get("privacy/location_usage_description_localized");
  705. Dictionary address_book_usage_descriptions = p_preset->get("privacy/address_book_usage_description_localized");
  706. Dictionary calendar_usage_descriptions = p_preset->get("privacy/calendar_usage_description_localized");
  707. Dictionary photos_library_usage_descriptions = p_preset->get("privacy/photos_library_usage_description_localized");
  708. Dictionary desktop_folder_usage_descriptions = p_preset->get("privacy/desktop_folder_usage_description_localized");
  709. Dictionary documents_folder_usage_descriptions = p_preset->get("privacy/documents_folder_usage_description_localized");
  710. Dictionary downloads_folder_usage_descriptions = p_preset->get("privacy/downloads_folder_usage_description_localized");
  711. Dictionary network_volumes_usage_descriptions = p_preset->get("privacy/network_volumes_usage_description_localized");
  712. Dictionary removable_volumes_usage_descriptions = p_preset->get("privacy/removable_volumes_usage_description_localized");
  713. Dictionary copyrights = p_preset->get("application/copyright_localized");
  714. Vector<String> translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  715. if (translations.size() > 0) {
  716. {
  717. String fname = tmp_app_path_name + "/Contents/Resources/en.lproj";
  718. tmp_app_dir->make_dir_recursive(fname);
  719. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  720. f->store_line("/* Localized versions of Info.plist keys */");
  721. f->store_line("");
  722. f->store_line("CFBundleDisplayName = \"" + ProjectSettings::get_singleton()->get("application/config/name").operator String() + "\";");
  723. if (!((String)p_preset->get("privacy/microphone_usage_description")).is_empty()) {
  724. f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
  725. }
  726. if (!((String)p_preset->get("privacy/camera_usage_description")).is_empty()) {
  727. f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
  728. }
  729. if (!((String)p_preset->get("privacy/location_usage_description")).is_empty()) {
  730. f->store_line("NSLocationUsageDescription = \"" + p_preset->get("privacy/location_usage_description").operator String() + "\";");
  731. }
  732. if (!((String)p_preset->get("privacy/address_book_usage_description")).is_empty()) {
  733. f->store_line("NSContactsUsageDescription = \"" + p_preset->get("privacy/address_book_usage_description").operator String() + "\";");
  734. }
  735. if (!((String)p_preset->get("privacy/calendar_usage_description")).is_empty()) {
  736. f->store_line("NSCalendarsUsageDescription = \"" + p_preset->get("privacy/calendar_usage_description").operator String() + "\";");
  737. }
  738. if (!((String)p_preset->get("privacy/photos_library_usage_description")).is_empty()) {
  739. f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photos_library_usage_description").operator String() + "\";");
  740. }
  741. if (!((String)p_preset->get("privacy/desktop_folder_usage_description")).is_empty()) {
  742. f->store_line("NSDesktopFolderUsageDescription = \"" + p_preset->get("privacy/desktop_folder_usage_description").operator String() + "\";");
  743. }
  744. if (!((String)p_preset->get("privacy/documents_folder_usage_description")).is_empty()) {
  745. f->store_line("NSDocumentsFolderUsageDescription = \"" + p_preset->get("privacy/documents_folder_usage_description").operator String() + "\";");
  746. }
  747. if (!((String)p_preset->get("privacy/downloads_folder_usage_description")).is_empty()) {
  748. f->store_line("NSDownloadsFolderUsageDescription = \"" + p_preset->get("privacy/downloads_folder_usage_description").operator String() + "\";");
  749. }
  750. if (!((String)p_preset->get("privacy/network_volumes_usage_description")).is_empty()) {
  751. f->store_line("NSNetworkVolumesUsageDescription = \"" + p_preset->get("privacy/network_volumes_usage_description").operator String() + "\";");
  752. }
  753. if (!((String)p_preset->get("privacy/removable_volumes_usage_description")).is_empty()) {
  754. f->store_line("NSRemovableVolumesUsageDescription = \"" + p_preset->get("privacy/removable_volumes_usage_description").operator String() + "\";");
  755. }
  756. f->store_line("NSHumanReadableCopyright = \"" + p_preset->get("application/copyright").operator String() + "\";");
  757. }
  758. for (const String &E : translations) {
  759. Ref<Translation> tr = ResourceLoader::load(E);
  760. if (tr.is_valid()) {
  761. String lang = tr->get_locale();
  762. String fname = tmp_app_path_name + "/Contents/Resources/" + lang + ".lproj";
  763. tmp_app_dir->make_dir_recursive(fname);
  764. Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
  765. f->store_line("/* Localized versions of Info.plist keys */");
  766. f->store_line("");
  767. if (appnames.has(lang)) {
  768. f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
  769. }
  770. if (microphone_usage_descriptions.has(lang)) {
  771. f->store_line("NSMicrophoneUsageDescription = \"" + microphone_usage_descriptions[lang].operator String() + "\";");
  772. }
  773. if (camera_usage_descriptions.has(lang)) {
  774. f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
  775. }
  776. if (location_usage_descriptions.has(lang)) {
  777. f->store_line("NSLocationUsageDescription = \"" + location_usage_descriptions[lang].operator String() + "\";");
  778. }
  779. if (address_book_usage_descriptions.has(lang)) {
  780. f->store_line("NSContactsUsageDescription = \"" + address_book_usage_descriptions[lang].operator String() + "\";");
  781. }
  782. if (calendar_usage_descriptions.has(lang)) {
  783. f->store_line("NSCalendarsUsageDescription = \"" + calendar_usage_descriptions[lang].operator String() + "\";");
  784. }
  785. if (photos_library_usage_descriptions.has(lang)) {
  786. f->store_line("NSPhotoLibraryUsageDescription = \"" + photos_library_usage_descriptions[lang].operator String() + "\";");
  787. }
  788. if (desktop_folder_usage_descriptions.has(lang)) {
  789. f->store_line("NSDesktopFolderUsageDescription = \"" + desktop_folder_usage_descriptions[lang].operator String() + "\";");
  790. }
  791. if (documents_folder_usage_descriptions.has(lang)) {
  792. f->store_line("NSDocumentsFolderUsageDescription = \"" + documents_folder_usage_descriptions[lang].operator String() + "\";");
  793. }
  794. if (downloads_folder_usage_descriptions.has(lang)) {
  795. f->store_line("NSDownloadsFolderUsageDescription = \"" + downloads_folder_usage_descriptions[lang].operator String() + "\";");
  796. }
  797. if (network_volumes_usage_descriptions.has(lang)) {
  798. f->store_line("NSNetworkVolumesUsageDescription = \"" + network_volumes_usage_descriptions[lang].operator String() + "\";");
  799. }
  800. if (removable_volumes_usage_descriptions.has(lang)) {
  801. f->store_line("NSRemovableVolumesUsageDescription = \"" + removable_volumes_usage_descriptions[lang].operator String() + "\";");
  802. }
  803. if (copyrights.has(lang)) {
  804. f->store_line("NSHumanReadableCopyright = \"" + copyrights[lang].operator String() + "\";");
  805. }
  806. }
  807. }
  808. }
  809. // Now process our template.
  810. bool found_binary = false;
  811. Vector<String> dylibs_found;
  812. while (ret == UNZ_OK && err == OK) {
  813. bool is_execute = false;
  814. // Get filename.
  815. unz_file_info info;
  816. char fname[16384];
  817. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  818. if (ret != UNZ_OK) {
  819. break;
  820. }
  821. String file = String::utf8(fname);
  822. Vector<uint8_t> data;
  823. data.resize(info.uncompressed_size);
  824. // Read.
  825. unzOpenCurrentFile(src_pkg_zip);
  826. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  827. unzCloseCurrentFile(src_pkg_zip);
  828. // Write.
  829. file = file.replace_first("osx_template.app/", "");
  830. if (((info.external_fa >> 16L) & 0120000) == 0120000) {
  831. #ifndef UNIX_ENABLED
  832. WARN_PRINT(vformat("Relative symlinks are not supported on this OS, the exported project might be broken!"));
  833. #endif
  834. // Handle symlinks in the archive.
  835. file = tmp_app_path_name.plus_file(file);
  836. if (err == OK) {
  837. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  838. }
  839. if (err == OK) {
  840. String lnk_data = String::utf8((const char *)data.ptr(), data.size());
  841. err = tmp_app_dir->create_link(lnk_data, file);
  842. print_verbose(vformat("ADDING SYMLINK %s => %s\n", file, lnk_data));
  843. }
  844. ret = unzGoToNextFile(src_pkg_zip);
  845. continue; // next
  846. }
  847. if (file == "Contents/Info.plist") {
  848. _fix_plist(p_preset, data, pkg_name);
  849. }
  850. if (file.begins_with("Contents/MacOS/godot_")) {
  851. if (file != "Contents/MacOS/" + binary_to_use) {
  852. ret = unzGoToNextFile(src_pkg_zip);
  853. continue; // skip
  854. }
  855. found_binary = true;
  856. is_execute = true;
  857. file = "Contents/MacOS/" + pkg_name;
  858. }
  859. if (file == "Contents/Resources/icon.icns") {
  860. // See if there is an icon.
  861. String iconpath;
  862. if (p_preset->get("application/icon") != "") {
  863. iconpath = p_preset->get("application/icon");
  864. } else {
  865. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  866. }
  867. if (!iconpath.is_empty()) {
  868. if (iconpath.get_extension() == "icns") {
  869. Ref<FileAccess> icon = FileAccess::open(iconpath, FileAccess::READ);
  870. if (icon.is_valid()) {
  871. data.resize(icon->get_length());
  872. icon->get_buffer(&data.write[0], icon->get_length());
  873. }
  874. } else {
  875. Ref<Image> icon;
  876. icon.instantiate();
  877. icon->load(iconpath);
  878. if (!icon->is_empty()) {
  879. _make_icon(icon, data);
  880. }
  881. }
  882. }
  883. }
  884. if (data.size() > 0) {
  885. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  886. if (!p_debug) {
  887. ret = unzGoToNextFile(src_pkg_zip);
  888. continue; // skip
  889. }
  890. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  891. }
  892. if (file.find("/data.mono.osx.64.release/") != -1) {
  893. if (p_debug) {
  894. ret = unzGoToNextFile(src_pkg_zip);
  895. continue; // skip
  896. }
  897. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  898. }
  899. if (file.ends_with(".dylib")) {
  900. dylibs_found.push_back(file);
  901. }
  902. print_verbose("ADDING: " + file + " size: " + itos(data.size()));
  903. // Write it into our application bundle.
  904. file = tmp_app_path_name.plus_file(file);
  905. if (err == OK) {
  906. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  907. }
  908. if (err == OK) {
  909. Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);
  910. if (f.is_valid()) {
  911. f->store_buffer(data.ptr(), data.size());
  912. if (is_execute) {
  913. // chmod with 0755 if the file is executable.
  914. FileAccess::set_unix_permissions(file, 0755);
  915. }
  916. } else {
  917. err = ERR_CANT_CREATE;
  918. }
  919. }
  920. }
  921. ret = unzGoToNextFile(src_pkg_zip);
  922. }
  923. // We're done with our source zip.
  924. unzClose(src_pkg_zip);
  925. if (!found_binary) {
  926. ERR_PRINT(vformat("Requested template binary '%s' not found. It might be missing from your template archive.", binary_to_use));
  927. err = ERR_FILE_NOT_FOUND;
  928. }
  929. // Save console script.
  930. if (err == OK) {
  931. int con_scr = p_preset->get("debug/export_console_script");
  932. if ((con_scr == 1 && p_debug) || (con_scr == 2)) {
  933. err = _export_debug_script(p_preset, pkg_name, tmp_app_path_name.get_file() + "/Contents/MacOS/" + pkg_name, scr_path);
  934. FileAccess::set_unix_permissions(scr_path, 0755);
  935. }
  936. }
  937. if (err == OK) {
  938. if (ep.step(TTR("Making PKG"), 1)) {
  939. return ERR_SKIP;
  940. }
  941. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  942. Vector<SharedObject> shared_objects;
  943. err = save_pack(p_preset, p_debug, pack_path, &shared_objects);
  944. // See if we can code sign our new package.
  945. bool sign_enabled = p_preset->get("codesign/enable");
  946. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  947. String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + "_helper.entitlements");
  948. if (sign_enabled && (ent_path.is_empty())) {
  949. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  950. Ref<FileAccess> ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  951. if (ent_f.is_valid()) {
  952. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  953. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  954. ent_f->store_line("<plist version=\"1.0\">");
  955. ent_f->store_line("<dict>");
  956. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  957. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  958. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  959. ent_f->store_line("<true/>");
  960. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  961. ent_f->store_line("<true/>");
  962. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  963. ent_f->store_line("<true/>");
  964. } else {
  965. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  966. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  967. ent_f->store_line("<true/>");
  968. }
  969. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  970. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  971. ent_f->store_line("<true/>");
  972. }
  973. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  974. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  975. ent_f->store_line("<true/>");
  976. }
  977. }
  978. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  979. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  980. ent_f->store_line("<true/>");
  981. }
  982. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  983. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  984. ent_f->store_line("<true/>");
  985. }
  986. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  987. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  988. ent_f->store_line("<true/>");
  989. }
  990. if ((bool)p_preset->get("codesign/entitlements/location")) {
  991. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  992. ent_f->store_line("<true/>");
  993. }
  994. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  995. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  996. ent_f->store_line("<true/>");
  997. }
  998. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  999. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  1000. ent_f->store_line("<true/>");
  1001. }
  1002. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  1003. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  1004. ent_f->store_line("<true/>");
  1005. }
  1006. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  1007. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  1008. ent_f->store_line("<true/>");
  1009. }
  1010. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  1011. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  1012. ent_f->store_line("<true/>");
  1013. }
  1014. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  1015. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  1016. ent_f->store_line("<true/>");
  1017. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  1018. ent_f->store_line("<key>com.apple.security.network.server</key>");
  1019. ent_f->store_line("<true/>");
  1020. }
  1021. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  1022. ent_f->store_line("<key>com.apple.security.network.client</key>");
  1023. ent_f->store_line("<true/>");
  1024. }
  1025. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  1026. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  1027. ent_f->store_line("<true/>");
  1028. }
  1029. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  1030. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  1031. ent_f->store_line("<true/>");
  1032. }
  1033. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  1034. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  1035. ent_f->store_line("<true/>");
  1036. }
  1037. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  1038. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  1039. ent_f->store_line("<true/>");
  1040. }
  1041. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  1042. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  1043. ent_f->store_line("<true/>");
  1044. }
  1045. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  1046. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  1047. ent_f->store_line("<true/>");
  1048. }
  1049. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  1050. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  1051. ent_f->store_line("<true/>");
  1052. }
  1053. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  1054. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  1055. ent_f->store_line("<true/>");
  1056. }
  1057. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  1058. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  1059. ent_f->store_line("<true/>");
  1060. }
  1061. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  1062. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  1063. ent_f->store_line("<true/>");
  1064. }
  1065. }
  1066. ent_f->store_line("</dict>");
  1067. ent_f->store_line("</plist>");
  1068. } else {
  1069. err = ERR_CANT_CREATE;
  1070. }
  1071. if ((err == OK) && helpers.size() > 0) {
  1072. ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
  1073. if (ent_f.is_valid()) {
  1074. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1075. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  1076. ent_f->store_line("<plist version=\"1.0\">");
  1077. ent_f->store_line("<dict>");
  1078. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  1079. ent_f->store_line("<true/>");
  1080. ent_f->store_line("<key>com.apple.security.inherit</key>");
  1081. ent_f->store_line("<true/>");
  1082. ent_f->store_line("</dict>");
  1083. ent_f->store_line("</plist>");
  1084. } else {
  1085. err = ERR_CANT_CREATE;
  1086. }
  1087. }
  1088. }
  1089. if ((err == OK) && helpers.size() > 0) {
  1090. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1091. for (int i = 0; i < helpers.size(); i++) {
  1092. String hlp_path = helpers[i];
  1093. err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
  1094. if (err == OK && sign_enabled) {
  1095. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path, false);
  1096. }
  1097. FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
  1098. }
  1099. }
  1100. bool ad_hoc = true;
  1101. if (err == OK) {
  1102. #ifdef OSX_ENABLED
  1103. String sign_identity = p_preset->get("codesign/identity");
  1104. #else
  1105. String sign_identity = "-";
  1106. #endif
  1107. ad_hoc = (sign_identity == "" || sign_identity == "-");
  1108. bool lib_validation = p_preset->get("codesign/entitlements/disable_library_validation");
  1109. if ((!dylibs_found.is_empty() || !shared_objects.is_empty()) && sign_enabled && ad_hoc && !lib_validation) {
  1110. ERR_PRINT("Ad-hoc signed applications require the 'Disable Library Validation' entitlement to load dynamic libraries.");
  1111. err = ERR_CANT_CREATE;
  1112. }
  1113. }
  1114. if (err == OK) {
  1115. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1116. for (int i = 0; i < shared_objects.size(); i++) {
  1117. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  1118. if (shared_objects[i].target.is_empty()) {
  1119. String path_in_app = tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file();
  1120. err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, true);
  1121. } else {
  1122. String path_in_app = tmp_app_path_name.plus_file(shared_objects[i].target).plus_file(src_path.get_file());
  1123. err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, false);
  1124. }
  1125. if (err != OK) {
  1126. break;
  1127. }
  1128. }
  1129. Vector<Ref<EditorExportPlugin>> export_plugins{ EditorExport::get_singleton()->get_export_plugins() };
  1130. for (int i = 0; i < export_plugins.size(); ++i) {
  1131. err = _export_osx_plugins_for(export_plugins[i], tmp_app_path_name, da, sign_enabled, p_preset, ent_path);
  1132. if (err != OK) {
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. if (sign_enabled) {
  1138. for (int i = 0; i < dylibs_found.size(); i++) {
  1139. if (err == OK) {
  1140. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path, false);
  1141. }
  1142. }
  1143. }
  1144. if (err == OK && sign_enabled) {
  1145. if (ep.step(TTR("Code signing bundle"), 2)) {
  1146. return ERR_SKIP;
  1147. }
  1148. err = _code_sign(p_preset, tmp_app_path_name, ent_path);
  1149. }
  1150. if (export_format == "dmg") {
  1151. // Create a DMG.
  1152. if (err == OK) {
  1153. if (ep.step(TTR("Making DMG"), 3)) {
  1154. return ERR_SKIP;
  1155. }
  1156. err = _create_dmg(p_path, pkg_name, tmp_base_path_name);
  1157. }
  1158. // Sign DMG.
  1159. if (err == OK && sign_enabled && !ad_hoc) {
  1160. if (ep.step(TTR("Code signing DMG"), 3)) {
  1161. return ERR_SKIP;
  1162. }
  1163. err = _code_sign(p_preset, p_path, ent_path, false);
  1164. }
  1165. } else if (export_format == "zip") {
  1166. // Create ZIP.
  1167. if (err == OK) {
  1168. if (ep.step(TTR("Making ZIP"), 3)) {
  1169. return ERR_SKIP;
  1170. }
  1171. if (FileAccess::exists(p_path)) {
  1172. OS::get_singleton()->move_to_trash(p_path);
  1173. }
  1174. Ref<FileAccess> io_fa_dst;
  1175. zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst);
  1176. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  1177. _zip_folder_recursive(zip, tmp_base_path_name, "", pkg_name);
  1178. zipClose(zip, nullptr);
  1179. }
  1180. }
  1181. #ifdef OSX_ENABLED
  1182. bool noto_enabled = p_preset->get("notarization/enable");
  1183. if (err == OK && noto_enabled) {
  1184. if (export_format == "app") {
  1185. WARN_PRINT("Notarization requires the app to be archived first, select the DMG or ZIP export format instead.");
  1186. } else {
  1187. if (ep.step(TTR("Sending archive for notarization"), 4)) {
  1188. return ERR_SKIP;
  1189. }
  1190. err = _notarize(p_preset, p_path);
  1191. }
  1192. }
  1193. #endif
  1194. // Clean up temporary entitlements files.
  1195. DirAccess::remove_file_or_error(hlp_ent_path);
  1196. // Clean up temporary .app dir and generated entitlements.
  1197. if ((String)(p_preset->get("codesign/entitlements/custom_file")) == "") {
  1198. tmp_app_dir->remove(ent_path);
  1199. }
  1200. if (export_format != "app") {
  1201. if (tmp_app_dir->change_dir(tmp_base_path_name) == OK) {
  1202. tmp_app_dir->erase_contents_recursive();
  1203. tmp_app_dir->change_dir("..");
  1204. tmp_app_dir->remove(pkg_name);
  1205. }
  1206. }
  1207. }
  1208. return err;
  1209. }
  1210. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  1211. String dir = p_folder.is_empty() ? p_root_path : p_root_path.plus_file(p_folder);
  1212. Ref<DirAccess> da = DirAccess::open(dir);
  1213. da->list_dir_begin();
  1214. String f = da->get_next();
  1215. while (!f.is_empty()) {
  1216. if (f == "." || f == "..") {
  1217. f = da->get_next();
  1218. continue;
  1219. }
  1220. if (da->is_link(f)) {
  1221. OS::Time time = OS::get_singleton()->get_time();
  1222. OS::Date date = OS::get_singleton()->get_date();
  1223. zip_fileinfo zipfi;
  1224. zipfi.tmz_date.tm_hour = time.hour;
  1225. zipfi.tmz_date.tm_mday = date.day;
  1226. zipfi.tmz_date.tm_min = time.minute;
  1227. 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/
  1228. zipfi.tmz_date.tm_sec = time.second;
  1229. zipfi.tmz_date.tm_year = date.year;
  1230. zipfi.dosDate = 0;
  1231. // 0120000: symbolic link type
  1232. // 0000755: permissions rwxr-xr-x
  1233. // 0000644: permissions rw-r--r--
  1234. uint32_t _mode = 0120644;
  1235. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1236. zipfi.internal_fa = 0;
  1237. zipOpenNewFileInZip4(p_zip,
  1238. p_folder.plus_file(f).utf8().get_data(),
  1239. &zipfi,
  1240. nullptr,
  1241. 0,
  1242. nullptr,
  1243. 0,
  1244. nullptr,
  1245. Z_DEFLATED,
  1246. Z_DEFAULT_COMPRESSION,
  1247. 0,
  1248. -MAX_WBITS,
  1249. DEF_MEM_LEVEL,
  1250. Z_DEFAULT_STRATEGY,
  1251. nullptr,
  1252. 0,
  1253. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1254. 0);
  1255. String target = da->read_link(f);
  1256. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  1257. zipCloseFileInZip(p_zip);
  1258. } else if (da->current_is_dir()) {
  1259. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  1260. } else {
  1261. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name)) || p_folder.ends_with("Helpers") || f.ends_with(".command");
  1262. OS::Time time = OS::get_singleton()->get_time();
  1263. OS::Date date = OS::get_singleton()->get_date();
  1264. zip_fileinfo zipfi;
  1265. zipfi.tmz_date.tm_hour = time.hour;
  1266. zipfi.tmz_date.tm_mday = date.day;
  1267. zipfi.tmz_date.tm_min = time.minute;
  1268. 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/
  1269. zipfi.tmz_date.tm_sec = time.second;
  1270. zipfi.tmz_date.tm_year = date.year;
  1271. zipfi.dosDate = 0;
  1272. // 0100000: regular file type
  1273. // 0000755: permissions rwxr-xr-x
  1274. // 0000644: permissions rw-r--r--
  1275. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  1276. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1277. zipfi.internal_fa = 0;
  1278. zipOpenNewFileInZip4(p_zip,
  1279. p_folder.plus_file(f).utf8().get_data(),
  1280. &zipfi,
  1281. nullptr,
  1282. 0,
  1283. nullptr,
  1284. 0,
  1285. nullptr,
  1286. Z_DEFLATED,
  1287. Z_DEFAULT_COMPRESSION,
  1288. 0,
  1289. -MAX_WBITS,
  1290. DEF_MEM_LEVEL,
  1291. Z_DEFAULT_STRATEGY,
  1292. nullptr,
  1293. 0,
  1294. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1295. 0);
  1296. Ref<FileAccess> fa = FileAccess::open(dir.plus_file(f), FileAccess::READ);
  1297. if (fa.is_null()) {
  1298. ERR_FAIL_MSG(vformat("Can't open file to read from path \"%s\".", dir.plus_file(f)));
  1299. }
  1300. const int bufsize = 16384;
  1301. uint8_t buf[bufsize];
  1302. while (true) {
  1303. uint64_t got = fa->get_buffer(buf, bufsize);
  1304. if (got == 0) {
  1305. break;
  1306. }
  1307. zipWriteInFileInZip(p_zip, buf, got);
  1308. }
  1309. zipCloseFileInZip(p_zip);
  1310. }
  1311. f = da->get_next();
  1312. }
  1313. da->list_dir_end();
  1314. }
  1315. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1316. String err;
  1317. bool valid = false;
  1318. // Look for export templates (custom templates).
  1319. bool dvalid = false;
  1320. bool rvalid = false;
  1321. if (p_preset->get("custom_template/debug") != "") {
  1322. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1323. if (!dvalid) {
  1324. err += TTR("Custom debug template not found.") + "\n";
  1325. }
  1326. }
  1327. if (p_preset->get("custom_template/release") != "") {
  1328. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1329. if (!rvalid) {
  1330. err += TTR("Custom release template not found.") + "\n";
  1331. }
  1332. }
  1333. // Look for export templates (official templates, check only is custom templates are not set).
  1334. if (!dvalid || !rvalid) {
  1335. dvalid = exists_export_template("osx.zip", &err);
  1336. rvalid = dvalid; // Both in the same ZIP.
  1337. }
  1338. valid = dvalid || rvalid;
  1339. r_missing_templates = !valid;
  1340. String identifier = p_preset->get("application/bundle_identifier");
  1341. String pn_err;
  1342. if (!is_package_name_valid(identifier, &pn_err)) {
  1343. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  1344. valid = false;
  1345. }
  1346. bool sign_enabled = p_preset->get("codesign/enable");
  1347. #ifdef OSX_ENABLED
  1348. bool noto_enabled = p_preset->get("notarization/enable");
  1349. bool ad_hoc = ((p_preset->get("codesign/identity") == "") || (p_preset->get("codesign/identity") == "-"));
  1350. if (!ad_hoc && (bool)EditorSettings::get_singleton()->get("export/macos/force_builtin_codesign")) {
  1351. err += TTR("Warning: Built-in \"codesign\" is selected in the Editor Settings. Code signing is limited to ad-hoc signature only.") + "\n";
  1352. }
  1353. if (!ad_hoc && !FileAccess::exists("/usr/bin/codesign") && !FileAccess::exists("/bin/codesign")) {
  1354. err += TTR("Warning: Xcode command line tools are not installed, using built-in \"codesign\". Code signing is limited to ad-hoc signature only.") + "\n";
  1355. }
  1356. if (noto_enabled) {
  1357. if (ad_hoc) {
  1358. err += TTR("Notarization: Notarization with an ad-hoc signature is not supported.") + "\n";
  1359. valid = false;
  1360. }
  1361. if (!sign_enabled) {
  1362. err += TTR("Notarization: Code signing is required for notarization.") + "\n";
  1363. valid = false;
  1364. }
  1365. if (!(bool)p_preset->get("codesign/hardened_runtime")) {
  1366. err += TTR("Notarization: Hardened runtime is required for notarization.") + "\n";
  1367. valid = false;
  1368. }
  1369. if (!(bool)p_preset->get("codesign/timestamp")) {
  1370. err += TTR("Notarization: Timestamping is required for notarization.") + "\n";
  1371. valid = false;
  1372. }
  1373. if (p_preset->get("notarization/apple_id_name") == "") {
  1374. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  1375. valid = false;
  1376. }
  1377. if (p_preset->get("notarization/apple_id_password") == "") {
  1378. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  1379. valid = false;
  1380. }
  1381. } else {
  1382. err += TTR("Warning: Notarization is disabled. The exported project will be blocked by Gatekeeper if it's downloaded from an unknown source.") + "\n";
  1383. if (!sign_enabled) {
  1384. err += TTR("Code signing is disabled. The exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1385. } else {
  1386. if ((bool)p_preset->get("codesign/hardened_runtime") && ad_hoc) {
  1387. err += TTR("Hardened Runtime is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1388. }
  1389. if ((bool)p_preset->get("codesign/timestamp") && ad_hoc) {
  1390. err += TTR("Timestamping is not compatible with ad-hoc signature, and will be disabled!") + "\n";
  1391. }
  1392. }
  1393. }
  1394. #else
  1395. 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";
  1396. if (!sign_enabled) {
  1397. err += TTR("Code signing is disabled. The exported project will not run on Macs with enabled Gatekeeper and Apple Silicon powered Macs.") + "\n";
  1398. }
  1399. #endif
  1400. if (sign_enabled) {
  1401. if ((bool)p_preset->get("codesign/entitlements/audio_input") && ((String)p_preset->get("privacy/microphone_usage_description")).is_empty()) {
  1402. err += TTR("Privacy: Microphone access is enabled, but usage description is not specified.") + "\n";
  1403. valid = false;
  1404. }
  1405. if ((bool)p_preset->get("codesign/entitlements/camera") && ((String)p_preset->get("privacy/camera_usage_description")).is_empty()) {
  1406. err += TTR("Privacy: Camera access is enabled, but usage description is not specified.") + "\n";
  1407. valid = false;
  1408. }
  1409. if ((bool)p_preset->get("codesign/entitlements/location") && ((String)p_preset->get("privacy/location_usage_description")).is_empty()) {
  1410. err += TTR("Privacy: Location information access is enabled, but usage description is not specified.") + "\n";
  1411. valid = false;
  1412. }
  1413. if ((bool)p_preset->get("codesign/entitlements/address_book") && ((String)p_preset->get("privacy/address_book_usage_description")).is_empty()) {
  1414. err += TTR("Privacy: Address book access is enabled, but usage description is not specified.") + "\n";
  1415. valid = false;
  1416. }
  1417. if ((bool)p_preset->get("codesign/entitlements/calendars") && ((String)p_preset->get("privacy/calendar_usage_description")).is_empty()) {
  1418. err += TTR("Privacy: Calendar access is enabled, but usage description is not specified.") + "\n";
  1419. valid = false;
  1420. }
  1421. if ((bool)p_preset->get("codesign/entitlements/photos_library") && ((String)p_preset->get("privacy/photos_library_usage_description")).is_empty()) {
  1422. err += TTR("Privacy: Photo library access is enabled, but usage description is not specified.") + "\n";
  1423. valid = false;
  1424. }
  1425. }
  1426. if (!err.is_empty()) {
  1427. r_error = err;
  1428. }
  1429. return valid;
  1430. }
  1431. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  1432. Ref<Image> img = memnew(Image(_osx_logo));
  1433. logo.instantiate();
  1434. logo->create_from_image(img);
  1435. }
  1436. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  1437. }