export.cpp 65 KB

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