export.cpp 42 KB

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