export.cpp 44 KB

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