export.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/io/zip_io.h"
  37. #include "core/os/os.h"
  38. #include "core/version.h"
  39. #include "editor/editor_export.h"
  40. #include "editor/editor_node.h"
  41. #include "editor/editor_settings.h"
  42. #include "platform/osx/logo.gen.h"
  43. #include <sys/stat.h>
  44. class EditorExportPlatformOSX : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  46. int version_code = 0;
  47. Ref<ImageTexture> logo;
  48. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  49. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  50. Error _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  51. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path);
  52. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  53. void _zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name);
  54. #ifdef OSX_ENABLED
  55. bool use_codesign() const { return true; }
  56. bool use_dmg() const { return true; }
  57. #else
  58. bool use_codesign() const { return false; }
  59. bool use_dmg() const { return false; }
  60. #endif
  61. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  62. String pname = p_package;
  63. if (pname.length() == 0) {
  64. if (r_error) {
  65. *r_error = TTR("Identifier is missing.");
  66. }
  67. return false;
  68. }
  69. for (int i = 0; i < pname.length(); i++) {
  70. char32_t c = pname[i];
  71. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  72. if (r_error) {
  73. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  74. }
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. protected:
  81. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) override;
  82. virtual void get_export_options(List<ExportOption> *r_options) override;
  83. public:
  84. virtual String get_name() const override { return "macOS"; }
  85. virtual String get_os_name() const override { return "macOS"; }
  86. virtual Ref<Texture2D> get_logo() const override { return logo; }
  87. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override {
  88. List<String> list;
  89. if (use_dmg()) {
  90. list.push_back("dmg");
  91. }
  92. list.push_back("zip");
  93. return list;
  94. }
  95. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  96. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override;
  97. virtual void get_platform_features(List<String> *r_features) override {
  98. r_features->push_back("pc");
  99. r_features->push_back("s3tc");
  100. r_features->push_back("macOS");
  101. }
  102. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) override {
  103. }
  104. EditorExportPlatformOSX();
  105. ~EditorExportPlatformOSX();
  106. };
  107. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  108. if (p_preset->get("texture_format/s3tc")) {
  109. r_features->push_back("s3tc");
  110. }
  111. if (p_preset->get("texture_format/etc")) {
  112. r_features->push_back("etc");
  113. }
  114. if (p_preset->get("texture_format/etc2")) {
  115. r_features->push_back("etc2");
  116. }
  117. r_features->push_back("64");
  118. }
  119. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  127. 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"));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  132. 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"), ""));
  133. 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"), ""));
  134. #ifdef OSX_ENABLED
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), true));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/replace_existing_signature"), true));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements/custom_file", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  141. if (!Engine::get_singleton()->has_singleton("GodotSharp")) {
  142. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  143. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_jit_code_execution"), false));
  144. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_unsigned_executable_memory"), false));
  145. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_dyld_environment_variables"), false));
  146. }
  147. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/disable_library_validation"), false));
  148. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/audio_input"), false));
  149. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/camera"), false));
  150. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/location"), false));
  151. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/address_book"), false));
  152. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/calendars"), false));
  153. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/photos_library"), false));
  154. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/apple_events"), false));
  155. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/debugging"), false));
  156. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/enabled"), false));
  157. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_server"), false));
  158. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_client"), false));
  159. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_usb"), false));
  160. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_bluetooth"), false));
  161. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_downloads", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  162. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_pictures", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  163. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_music", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  164. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_movies", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  165. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  166. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  167. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  168. 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"), ""));
  169. 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"), ""));
  170. #endif
  171. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  172. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  173. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  174. }
  175. void _rgba8_to_packbits_encode(int p_ch, int p_size, Vector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  176. int src_len = p_size * p_size;
  177. Vector<uint8_t> result;
  178. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  179. int res_size = 0;
  180. uint8_t buf[128];
  181. int buf_size = 0;
  182. int i = 0;
  183. while (i < src_len) {
  184. uint8_t cur = p_source.ptr()[i * 4 + p_ch];
  185. if (i < src_len - 2) {
  186. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(i + 2) * 4 + p_ch] == cur)) {
  187. if (buf_size > 0) {
  188. result.write[res_size++] = (uint8_t)(buf_size - 1);
  189. memcpy(&result.write[res_size], &buf, buf_size);
  190. res_size += buf_size;
  191. buf_size = 0;
  192. }
  193. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  194. bool hit_lim = true;
  195. for (int j = 3; j <= lim; j++) {
  196. if (p_source.ptr()[(i + j) * 4 + p_ch] != cur) {
  197. hit_lim = false;
  198. i = i + j - 1;
  199. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  200. result.write[res_size++] = cur;
  201. break;
  202. }
  203. }
  204. if (hit_lim) {
  205. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  206. result.write[res_size++] = cur;
  207. i = i + lim;
  208. }
  209. } else {
  210. buf[buf_size++] = cur;
  211. if (buf_size == 128) {
  212. result.write[res_size++] = (uint8_t)(buf_size - 1);
  213. memcpy(&result.write[res_size], &buf, buf_size);
  214. res_size += buf_size;
  215. buf_size = 0;
  216. }
  217. }
  218. } else {
  219. buf[buf_size++] = cur;
  220. result.write[res_size++] = (uint8_t)(buf_size - 1);
  221. memcpy(&result.write[res_size], &buf, buf_size);
  222. res_size += buf_size;
  223. buf_size = 0;
  224. }
  225. i++;
  226. }
  227. int ofs = p_dest.size();
  228. p_dest.resize(p_dest.size() + res_size);
  229. memcpy(&p_dest.write[ofs], result.ptr(), res_size);
  230. }
  231. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  232. Ref<ImageTexture> it = memnew(ImageTexture);
  233. Vector<uint8_t> data;
  234. data.resize(8);
  235. data.write[0] = 'i';
  236. data.write[1] = 'c';
  237. data.write[2] = 'n';
  238. data.write[3] = 's';
  239. struct MacOSIconInfo {
  240. const char *name;
  241. const char *mask_name;
  242. bool is_png;
  243. int size;
  244. };
  245. static const MacOSIconInfo icon_infos[] = {
  246. { "ic10", "", true, 1024 }, //1024x1024 32-bit PNG and 512x512@2x 32-bit "retina" PNG
  247. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  248. { "ic14", "", true, 512 }, //256x256@2x 32-bit "retina" PNG
  249. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  250. { "ic13", "", true, 256 }, //128x128@2x 32-bit "retina" PNG
  251. { "ic07", "", true, 128 }, //128x128 32-bit PNG
  252. { "ic12", "", true, 64 }, //32x32@2x 32-bit "retina" PNG
  253. { "ic11", "", true, 32 }, //16x16@2x 32-bit "retina" PNG
  254. { "il32", "l8mk", false, 32 }, //32x32 24-bit RLE + 8-bit uncompressed mask
  255. { "is32", "s8mk", false, 16 } //16x16 24-bit RLE + 8-bit uncompressed mask
  256. };
  257. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  258. 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?
  259. copy->convert(Image::FORMAT_RGBA8);
  260. copy->resize(icon_infos[i].size, icon_infos[i].size);
  261. if (icon_infos[i].is_png) {
  262. // Encode PNG icon.
  263. it->create_from_image(copy);
  264. String path = EditorPaths::get_singleton()->get_cache_dir().plus_file("icon.png");
  265. ResourceSaver::save(path, it);
  266. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  267. if (!f) {
  268. // Clean up generated file.
  269. DirAccess::remove_file_or_error(path);
  270. ERR_FAIL();
  271. }
  272. int ofs = data.size();
  273. uint64_t len = f->get_length();
  274. data.resize(data.size() + len + 8);
  275. f->get_buffer(&data.write[ofs + 8], len);
  276. memdelete(f);
  277. len += 8;
  278. len = BSWAP32(len);
  279. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  280. encode_uint32(len, &data.write[ofs + 4]);
  281. // Clean up generated file.
  282. DirAccess::remove_file_or_error(path);
  283. } else {
  284. Vector<uint8_t> src_data = copy->get_data();
  285. //encode 24bit RGB RLE icon
  286. {
  287. int ofs = data.size();
  288. data.resize(data.size() + 8);
  289. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  290. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  291. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  292. int len = data.size() - ofs;
  293. len = BSWAP32(len);
  294. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  295. encode_uint32(len, &data.write[ofs + 4]);
  296. }
  297. //encode 8bit mask uncompressed icon
  298. {
  299. int ofs = data.size();
  300. int len = copy->get_width() * copy->get_height();
  301. data.resize(data.size() + len + 8);
  302. for (int j = 0; j < len; j++) {
  303. data.write[ofs + 8 + j] = src_data.ptr()[j * 4 + 3];
  304. }
  305. len += 8;
  306. len = BSWAP32(len);
  307. memcpy(&data.write[ofs], icon_infos[i].mask_name, 4);
  308. encode_uint32(len, &data.write[ofs + 4]);
  309. }
  310. }
  311. }
  312. uint32_t total_len = data.size();
  313. total_len = BSWAP32(total_len);
  314. encode_uint32(total_len, &data.write[4]);
  315. p_data = data;
  316. }
  317. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  318. String str;
  319. String strnew;
  320. str.parse_utf8((const char *)plist.ptr(), plist.size());
  321. Vector<String> lines = str.split("\n");
  322. for (int i = 0; i < lines.size(); i++) {
  323. if (lines[i].find("$binary") != -1) {
  324. strnew += lines[i].replace("$binary", p_binary) + "\n";
  325. } else if (lines[i].find("$name") != -1) {
  326. strnew += lines[i].replace("$name", p_binary) + "\n";
  327. } else if (lines[i].find("$info") != -1) {
  328. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  329. } else if (lines[i].find("$bundle_identifier") != -1) {
  330. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  331. } else if (lines[i].find("$short_version") != -1) {
  332. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  333. } else if (lines[i].find("$version") != -1) {
  334. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  335. } else if (lines[i].find("$signature") != -1) {
  336. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  337. } else if (lines[i].find("$app_category") != -1) {
  338. String cat = p_preset->get("application/app_category");
  339. strnew += lines[i].replace("$app_category", cat.to_lower()) + "\n";
  340. } else if (lines[i].find("$copyright") != -1) {
  341. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  342. } else if (lines[i].find("$highres") != -1) {
  343. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  344. } else if (lines[i].find("$camera_usage_description") != -1) {
  345. String description = p_preset->get("privacy/camera_usage_description");
  346. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  347. } else if (lines[i].find("$microphone_usage_description") != -1) {
  348. String description = p_preset->get("privacy/microphone_usage_description");
  349. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  350. } else {
  351. strnew += lines[i] + "\n";
  352. }
  353. }
  354. CharString cs = strnew.utf8();
  355. plist.resize(cs.size() - 1);
  356. for (int i = 0; i < cs.size() - 1; i++) {
  357. plist.write[i] = cs[i];
  358. }
  359. }
  360. /**
  361. If we're running the OSX version of the Godot editor we'll:
  362. - export our application bundle to a temporary folder
  363. - attempt to code sign it
  364. - and then wrap it up in a DMG
  365. **/
  366. Error EditorExportPlatformOSX::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  367. #ifdef OSX_ENABLED
  368. List<String> args;
  369. args.push_back("altool");
  370. args.push_back("--notarize-app");
  371. args.push_back("--primary-bundle-id");
  372. args.push_back(p_preset->get("application/bundle_identifier"));
  373. args.push_back("--username");
  374. args.push_back(p_preset->get("notarization/apple_id_name"));
  375. args.push_back("--password");
  376. args.push_back(p_preset->get("notarization/apple_id_password"));
  377. args.push_back("--type");
  378. args.push_back("osx");
  379. if (p_preset->get("notarization/apple_team_id")) {
  380. args.push_back("--asc-provider");
  381. args.push_back(p_preset->get("notarization/apple_team_id"));
  382. }
  383. args.push_back("--file");
  384. args.push_back(p_path);
  385. String str;
  386. Error err = OS::get_singleton()->execute("xcrun", args, &str, nullptr, true);
  387. ERR_FAIL_COND_V(err != OK, err);
  388. print_line("altool (" + p_path + "):\n" + str);
  389. if (str.find("RequestUUID") == -1) {
  390. EditorNode::add_io_error("altool: " + str);
  391. return FAILED;
  392. } else {
  393. print_line("Note: The notarization process generally takes less than an hour. When the process is completed, you'll receive an email.");
  394. print_line(" You can check progress manually by opening a Terminal and running the following command:");
  395. print_line(" \"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  396. }
  397. #endif
  398. return OK;
  399. }
  400. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path) {
  401. #ifdef OSX_ENABLED
  402. List<String> args;
  403. if (p_preset->get("codesign/timestamp")) {
  404. args.push_back("--timestamp");
  405. }
  406. if (p_preset->get("codesign/hardened_runtime")) {
  407. args.push_back("--options");
  408. args.push_back("runtime");
  409. }
  410. if (p_path.get_extension() != "dmg") {
  411. args.push_back("--entitlements");
  412. args.push_back(p_ent_path);
  413. }
  414. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  415. for (int i = 0; i < user_args.size(); i++) {
  416. String user_arg = user_args[i].strip_edges();
  417. if (!user_arg.is_empty()) {
  418. args.push_back(user_arg);
  419. }
  420. }
  421. args.push_back("-s");
  422. if (p_preset->get("codesign/identity") == "") {
  423. args.push_back("-");
  424. } else {
  425. args.push_back(p_preset->get("codesign/identity"));
  426. }
  427. args.push_back("-v"); /* provide some more feedback */
  428. if (p_preset->get("codesign/replace_existing_signature")) {
  429. args.push_back("-f");
  430. }
  431. args.push_back(p_path);
  432. String str;
  433. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  434. ERR_FAIL_COND_V(err != OK, err);
  435. print_line("codesign (" + p_path + "):\n" + str);
  436. if (str.find("no identity found") != -1) {
  437. EditorNode::add_io_error("codesign: no identity found");
  438. return FAILED;
  439. }
  440. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  441. EditorNode::add_io_error("codesign: invalid entitlements file");
  442. return FAILED;
  443. }
  444. #endif
  445. return OK;
  446. }
  447. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  448. List<String> args;
  449. if (FileAccess::exists(p_dmg_path)) {
  450. OS::get_singleton()->move_to_trash(p_dmg_path);
  451. }
  452. args.push_back("create");
  453. args.push_back(p_dmg_path);
  454. args.push_back("-volname");
  455. args.push_back(p_pkg_name);
  456. args.push_back("-fs");
  457. args.push_back("HFS+");
  458. args.push_back("-srcfolder");
  459. args.push_back(p_app_path_name);
  460. String str;
  461. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  462. ERR_FAIL_COND_V(err != OK, err);
  463. print_line("hdiutil returned: " + str);
  464. if (str.find("create failed") != -1) {
  465. if (str.find("File exists") != -1) {
  466. EditorNode::add_io_error("hdiutil: create failed - file exists");
  467. } else {
  468. EditorNode::add_io_error("hdiutil: create failed");
  469. }
  470. return FAILED;
  471. }
  472. return OK;
  473. }
  474. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  475. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  476. String src_pkg_name;
  477. EditorProgress ep("export", "Exporting for OSX", 3, true);
  478. if (p_debug) {
  479. src_pkg_name = p_preset->get("custom_template/debug");
  480. } else {
  481. src_pkg_name = p_preset->get("custom_template/release");
  482. }
  483. if (src_pkg_name == "") {
  484. String err;
  485. src_pkg_name = find_export_template("osx.zip", &err);
  486. if (src_pkg_name == "") {
  487. EditorNode::add_io_error(err);
  488. return ERR_FILE_NOT_FOUND;
  489. }
  490. }
  491. if (!DirAccess::exists(p_path.get_base_dir())) {
  492. return ERR_FILE_BAD_PATH;
  493. }
  494. FileAccess *src_f = nullptr;
  495. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  496. if (ep.step("Creating app", 0)) {
  497. return ERR_SKIP;
  498. }
  499. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  500. if (!src_pkg_zip) {
  501. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  502. return ERR_FILE_NOT_FOUND;
  503. }
  504. int ret = unzGoToFirstFile(src_pkg_zip);
  505. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  506. String pkg_name;
  507. if (p_preset->get("application/name") != "") {
  508. pkg_name = p_preset->get("application/name"); // app_name
  509. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  510. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  511. } else {
  512. pkg_name = "Unnamed";
  513. }
  514. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  515. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  516. // Create our application bundle.
  517. String tmp_app_dir_name = pkg_name + ".app";
  518. String tmp_app_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(tmp_app_dir_name);
  519. print_line("Exporting to " + tmp_app_path_name);
  520. Error err = OK;
  521. DirAccessRef tmp_app_dir = DirAccess::create_for_path(tmp_app_path_name);
  522. if (!tmp_app_dir) {
  523. err = ERR_CANT_CREATE;
  524. }
  525. // Create our folder structure.
  526. if (err == OK) {
  527. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  528. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  529. }
  530. if (err == OK) {
  531. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  532. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  533. }
  534. if (err == OK) {
  535. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  536. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  537. }
  538. // Now process our template.
  539. bool found_binary = false;
  540. int total_size = 0;
  541. Vector<String> dylibs_found;
  542. while (ret == UNZ_OK && err == OK) {
  543. bool is_execute = false;
  544. // Get filename.
  545. unz_file_info info;
  546. char fname[16384];
  547. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  548. String file = fname;
  549. Vector<uint8_t> data;
  550. data.resize(info.uncompressed_size);
  551. // Read.
  552. unzOpenCurrentFile(src_pkg_zip);
  553. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  554. unzCloseCurrentFile(src_pkg_zip);
  555. // Write.
  556. file = file.replace_first("osx_template.app/", "");
  557. if (file == "Contents/Info.plist") {
  558. _fix_plist(p_preset, data, pkg_name);
  559. }
  560. if (file.begins_with("Contents/MacOS/godot_")) {
  561. if (file != "Contents/MacOS/" + binary_to_use) {
  562. ret = unzGoToNextFile(src_pkg_zip);
  563. continue; // skip
  564. }
  565. found_binary = true;
  566. is_execute = true;
  567. file = "Contents/MacOS/" + pkg_name;
  568. }
  569. if (file == "Contents/Resources/icon.icns") {
  570. // See if there is an icon.
  571. String iconpath;
  572. if (p_preset->get("application/icon") != "") {
  573. iconpath = p_preset->get("application/icon");
  574. } else {
  575. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  576. }
  577. if (iconpath != "") {
  578. if (iconpath.get_extension() == "icns") {
  579. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  580. if (icon) {
  581. data.resize(icon->get_length());
  582. icon->get_buffer(&data.write[0], icon->get_length());
  583. icon->close();
  584. memdelete(icon);
  585. }
  586. } else {
  587. Ref<Image> icon;
  588. icon.instantiate();
  589. icon->load(iconpath);
  590. if (!icon->is_empty()) {
  591. _make_icon(icon, data);
  592. }
  593. }
  594. }
  595. }
  596. if (data.size() > 0) {
  597. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  598. if (!p_debug) {
  599. ret = unzGoToNextFile(src_pkg_zip);
  600. continue; // skip
  601. }
  602. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  603. }
  604. if (file.find("/data.mono.osx.64.release/") != -1) {
  605. if (p_debug) {
  606. ret = unzGoToNextFile(src_pkg_zip);
  607. continue; // skip
  608. }
  609. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  610. }
  611. if (file.ends_with(".dylib")) {
  612. dylibs_found.push_back(file);
  613. }
  614. print_line("ADDING: " + file + " size: " + itos(data.size()));
  615. total_size += data.size();
  616. // Write it into our application bundle.
  617. file = tmp_app_path_name.plus_file(file);
  618. if (err == OK) {
  619. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  620. }
  621. if (err == OK) {
  622. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  623. if (f) {
  624. f->store_buffer(data.ptr(), data.size());
  625. f->close();
  626. if (is_execute) {
  627. // chmod with 0755 if the file is executable.
  628. FileAccess::set_unix_permissions(file, 0755);
  629. }
  630. memdelete(f);
  631. } else {
  632. err = ERR_CANT_CREATE;
  633. }
  634. }
  635. }
  636. ret = unzGoToNextFile(src_pkg_zip);
  637. }
  638. // We're done with our source zip.
  639. unzClose(src_pkg_zip);
  640. if (!found_binary) {
  641. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  642. err = ERR_FILE_NOT_FOUND;
  643. }
  644. if (err == OK) {
  645. if (ep.step("Making PKG", 1)) {
  646. return ERR_SKIP;
  647. }
  648. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  649. Vector<SharedObject> shared_objects;
  650. err = save_pack(p_preset, pack_path, &shared_objects);
  651. // See if we can code sign our new package.
  652. bool sign_enabled = p_preset->get("codesign/enable");
  653. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  654. if (sign_enabled && (ent_path == "")) {
  655. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  656. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  657. if (ent_f) {
  658. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  659. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  660. ent_f->store_line("<plist version=\"1.0\">");
  661. ent_f->store_line("<dict>");
  662. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  663. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  664. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  665. ent_f->store_line("<true/>");
  666. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  667. ent_f->store_line("<true/>");
  668. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  669. ent_f->store_line("<true/>");
  670. } else {
  671. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  672. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  673. ent_f->store_line("<true/>");
  674. }
  675. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  676. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  677. ent_f->store_line("<true/>");
  678. }
  679. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  680. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  681. ent_f->store_line("<true/>");
  682. }
  683. }
  684. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  685. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  686. ent_f->store_line("<true/>");
  687. }
  688. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  689. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  690. ent_f->store_line("<true/>");
  691. }
  692. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  693. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  694. ent_f->store_line("<true/>");
  695. }
  696. if ((bool)p_preset->get("codesign/entitlements/location")) {
  697. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  698. ent_f->store_line("<true/>");
  699. }
  700. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  701. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  702. ent_f->store_line("<true/>");
  703. }
  704. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  705. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  706. ent_f->store_line("<true/>");
  707. }
  708. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  709. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  710. ent_f->store_line("<true/>");
  711. }
  712. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  713. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  714. ent_f->store_line("<true/>");
  715. }
  716. if ((bool)p_preset->get("codesign/entitlements/debugging")) {
  717. ent_f->store_line("<key>com.apple.security.get-task-allow</key>");
  718. ent_f->store_line("<true/>");
  719. }
  720. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  721. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  722. ent_f->store_line("<true/>");
  723. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  724. ent_f->store_line("<key>com.apple.security.network.server</key>");
  725. ent_f->store_line("<true/>");
  726. }
  727. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  728. ent_f->store_line("<key>com.apple.security.network.client</key>");
  729. ent_f->store_line("<true/>");
  730. }
  731. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  732. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  733. ent_f->store_line("<true/>");
  734. }
  735. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  736. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  737. ent_f->store_line("<true/>");
  738. }
  739. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  740. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  741. ent_f->store_line("<true/>");
  742. }
  743. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  744. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  745. ent_f->store_line("<true/>");
  746. }
  747. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  748. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  749. ent_f->store_line("<true/>");
  750. }
  751. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  752. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  753. ent_f->store_line("<true/>");
  754. }
  755. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  756. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  757. ent_f->store_line("<true/>");
  758. }
  759. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  760. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  761. ent_f->store_line("<true/>");
  762. }
  763. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  764. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  765. ent_f->store_line("<true/>");
  766. }
  767. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  768. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  769. ent_f->store_line("<true/>");
  770. }
  771. }
  772. ent_f->store_line("</dict>");
  773. ent_f->store_line("</plist>");
  774. ent_f->close();
  775. memdelete(ent_f);
  776. } else {
  777. err = ERR_CANT_CREATE;
  778. }
  779. }
  780. if (err == OK) {
  781. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  782. for (int i = 0; i < shared_objects.size(); i++) {
  783. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  784. if (da->dir_exists(src_path)) {
  785. #ifndef UNIX_ENABLED
  786. WARN_PRINT("Relative symlinks are not supported, exported " + src_path.get_file() + " might be broken!");
  787. #endif
  788. print_verbose("export framework: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  789. err = da->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  790. if (err == OK) {
  791. err = da->copy_dir(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), -1, true);
  792. }
  793. } else {
  794. print_verbose("export dylib: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  795. err = da->copy(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  796. }
  797. if (err == OK && sign_enabled) {
  798. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), ent_path);
  799. }
  800. }
  801. memdelete(da);
  802. }
  803. if (sign_enabled) {
  804. for (int i = 0; i < dylibs_found.size(); i++) {
  805. if (err == OK) {
  806. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  807. }
  808. }
  809. }
  810. if (err == OK && sign_enabled) {
  811. if (ep.step("Code signing bundle", 2)) {
  812. return ERR_SKIP;
  813. }
  814. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  815. }
  816. if (export_format == "dmg") {
  817. // Create a DMG.
  818. if (err == OK) {
  819. if (ep.step("Making DMG", 3)) {
  820. return ERR_SKIP;
  821. }
  822. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  823. }
  824. // Sign DMG.
  825. if (err == OK && sign_enabled) {
  826. if (ep.step("Code signing DMG", 3)) {
  827. return ERR_SKIP;
  828. }
  829. err = _code_sign(p_preset, p_path, ent_path);
  830. }
  831. } else {
  832. // Create ZIP.
  833. if (err == OK) {
  834. if (ep.step("Making ZIP", 3)) {
  835. return ERR_SKIP;
  836. }
  837. if (FileAccess::exists(p_path)) {
  838. OS::get_singleton()->move_to_trash(p_path);
  839. }
  840. FileAccess *dst_f = nullptr;
  841. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  842. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  843. _zip_folder_recursive(zip, EditorPaths::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  844. zipClose(zip, nullptr);
  845. }
  846. }
  847. bool noto_enabled = p_preset->get("notarization/enable");
  848. if (err == OK && noto_enabled) {
  849. if (ep.step("Sending archive for notarization", 4)) {
  850. return ERR_SKIP;
  851. }
  852. err = _notarize(p_preset, p_path);
  853. }
  854. // Clean up temporary .app dir.
  855. tmp_app_dir->change_dir(tmp_app_path_name);
  856. tmp_app_dir->erase_contents_recursive();
  857. tmp_app_dir->change_dir("..");
  858. tmp_app_dir->remove(tmp_app_dir_name);
  859. }
  860. return err;
  861. }
  862. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  863. String dir = p_root_path.plus_file(p_folder);
  864. DirAccess *da = DirAccess::open(dir);
  865. da->list_dir_begin();
  866. String f;
  867. while ((f = da->get_next()) != "") {
  868. if (f == "." || f == "..") {
  869. continue;
  870. }
  871. if (da->is_link(f)) {
  872. OS::Time time = OS::get_singleton()->get_time();
  873. OS::Date date = OS::get_singleton()->get_date();
  874. zip_fileinfo zipfi;
  875. zipfi.tmz_date.tm_hour = time.hour;
  876. zipfi.tmz_date.tm_mday = date.day;
  877. zipfi.tmz_date.tm_min = time.minute;
  878. 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/
  879. zipfi.tmz_date.tm_sec = time.second;
  880. zipfi.tmz_date.tm_year = date.year;
  881. zipfi.dosDate = 0;
  882. // 0120000: symbolic link type
  883. // 0000755: permissions rwxr-xr-x
  884. // 0000644: permissions rw-r--r--
  885. uint32_t _mode = 0120644;
  886. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  887. zipfi.internal_fa = 0;
  888. zipOpenNewFileInZip4(p_zip,
  889. p_folder.plus_file(f).utf8().get_data(),
  890. &zipfi,
  891. nullptr,
  892. 0,
  893. nullptr,
  894. 0,
  895. nullptr,
  896. Z_DEFLATED,
  897. Z_DEFAULT_COMPRESSION,
  898. 0,
  899. -MAX_WBITS,
  900. DEF_MEM_LEVEL,
  901. Z_DEFAULT_STRATEGY,
  902. nullptr,
  903. 0,
  904. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  905. 0);
  906. String target = da->read_link(f);
  907. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  908. zipCloseFileInZip(p_zip);
  909. } else if (da->current_is_dir()) {
  910. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  911. } else {
  912. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name));
  913. OS::Time time = OS::get_singleton()->get_time();
  914. OS::Date date = OS::get_singleton()->get_date();
  915. zip_fileinfo zipfi;
  916. zipfi.tmz_date.tm_hour = time.hour;
  917. zipfi.tmz_date.tm_mday = date.day;
  918. zipfi.tmz_date.tm_min = time.minute;
  919. 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/
  920. zipfi.tmz_date.tm_sec = time.second;
  921. zipfi.tmz_date.tm_year = date.year;
  922. zipfi.dosDate = 0;
  923. // 0100000: regular file type
  924. // 0000755: permissions rwxr-xr-x
  925. // 0000644: permissions rw-r--r--
  926. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  927. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  928. zipfi.internal_fa = 0;
  929. zipOpenNewFileInZip4(p_zip,
  930. p_folder.plus_file(f).utf8().get_data(),
  931. &zipfi,
  932. nullptr,
  933. 0,
  934. nullptr,
  935. 0,
  936. nullptr,
  937. Z_DEFLATED,
  938. Z_DEFAULT_COMPRESSION,
  939. 0,
  940. -MAX_WBITS,
  941. DEF_MEM_LEVEL,
  942. Z_DEFAULT_STRATEGY,
  943. nullptr,
  944. 0,
  945. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  946. 0);
  947. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  948. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  949. zipCloseFileInZip(p_zip);
  950. }
  951. }
  952. da->list_dir_end();
  953. memdelete(da);
  954. }
  955. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  956. String err;
  957. bool valid = false;
  958. // Look for export templates (first official, and if defined custom templates).
  959. bool dvalid = exists_export_template("osx.zip", &err);
  960. bool rvalid = dvalid; // Both in the same ZIP.
  961. if (p_preset->get("custom_template/debug") != "") {
  962. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  963. if (!dvalid) {
  964. err += TTR("Custom debug template not found.") + "\n";
  965. }
  966. }
  967. if (p_preset->get("custom_template/release") != "") {
  968. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  969. if (!rvalid) {
  970. err += TTR("Custom release template not found.") + "\n";
  971. }
  972. }
  973. valid = dvalid || rvalid;
  974. r_missing_templates = !valid;
  975. String identifier = p_preset->get("application/bundle_identifier");
  976. String pn_err;
  977. if (!is_package_name_valid(identifier, &pn_err)) {
  978. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  979. valid = false;
  980. }
  981. bool sign_enabled = p_preset->get("codesign/enable");
  982. bool noto_enabled = p_preset->get("notarization/enable");
  983. if (noto_enabled) {
  984. if (!sign_enabled) {
  985. err += TTR("Notarization: code signing required.") + "\n";
  986. valid = false;
  987. }
  988. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  989. if (!hr_enabled) {
  990. err += TTR("Notarization: hardened runtime required.") + "\n";
  991. valid = false;
  992. }
  993. if (p_preset->get("notarization/apple_id_name") == "") {
  994. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  995. valid = false;
  996. }
  997. if (p_preset->get("notarization/apple_id_password") == "") {
  998. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  999. valid = false;
  1000. }
  1001. }
  1002. if (!err.is_empty()) {
  1003. r_error = err;
  1004. }
  1005. return valid;
  1006. }
  1007. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  1008. Ref<Image> img = memnew(Image(_osx_logo));
  1009. logo.instantiate();
  1010. logo->create_from_image(img);
  1011. }
  1012. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  1013. }
  1014. void register_osx_exporter() {
  1015. Ref<EditorExportPlatformOSX> platform;
  1016. platform.instantiate();
  1017. EditorExport::get_singleton()->add_export_platform(platform);
  1018. }