export.cpp 36 KB

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