export_plugin.cpp 74 KB

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