export.cpp 37 KB

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