export.cpp 62 KB

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