export.cpp 64 KB

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