export.cpp 36 KB

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