export.cpp 39 KB

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