export.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. int total_size = 0;
  476. Vector<String> dylibs_found;
  477. while (ret == UNZ_OK && err == OK) {
  478. bool is_execute = false;
  479. // Get filename.
  480. unz_file_info info;
  481. char fname[16384];
  482. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  483. String file = fname;
  484. Vector<uint8_t> data;
  485. data.resize(info.uncompressed_size);
  486. // Read.
  487. unzOpenCurrentFile(src_pkg_zip);
  488. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  489. unzCloseCurrentFile(src_pkg_zip);
  490. // Write.
  491. file = file.replace_first("osx_template.app/", "");
  492. if (file == "Contents/Info.plist") {
  493. _fix_plist(p_preset, data, pkg_name);
  494. }
  495. if (file.begins_with("Contents/MacOS/godot_")) {
  496. if (file != "Contents/MacOS/" + binary_to_use) {
  497. ret = unzGoToNextFile(src_pkg_zip);
  498. continue; // skip
  499. }
  500. found_binary = true;
  501. is_execute = true;
  502. file = "Contents/MacOS/" + pkg_name;
  503. }
  504. if (file == "Contents/Resources/icon.icns") {
  505. // See if there is an icon.
  506. String iconpath;
  507. if (p_preset->get("application/icon") != "")
  508. iconpath = p_preset->get("application/icon");
  509. else
  510. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  511. if (iconpath != "") {
  512. if (iconpath.get_extension() == "icns") {
  513. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  514. if (icon) {
  515. data.resize(icon->get_len());
  516. icon->get_buffer(&data.write[0], icon->get_len());
  517. icon->close();
  518. memdelete(icon);
  519. }
  520. } else {
  521. Ref<Image> icon;
  522. icon.instance();
  523. icon->load(iconpath);
  524. if (!icon->empty()) {
  525. _make_icon(icon, data);
  526. }
  527. }
  528. }
  529. }
  530. if (data.size() > 0) {
  531. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  532. if (!p_debug) {
  533. ret = unzGoToNextFile(src_pkg_zip);
  534. continue; // skip
  535. }
  536. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  537. }
  538. if (file.find("/data.mono.osx.64.release/") != -1) {
  539. if (p_debug) {
  540. ret = unzGoToNextFile(src_pkg_zip);
  541. continue; // skip
  542. }
  543. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  544. }
  545. if (file.ends_with(".dylib")) {
  546. dylibs_found.push_back(file);
  547. }
  548. print_line("ADDING: " + file + " size: " + itos(data.size()));
  549. total_size += data.size();
  550. // Write it into our application bundle.
  551. file = tmp_app_path_name.plus_file(file);
  552. if (err == OK) {
  553. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  554. }
  555. if (err == OK) {
  556. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  557. if (f) {
  558. f->store_buffer(data.ptr(), data.size());
  559. f->close();
  560. if (is_execute) {
  561. // chmod with 0755 if the file is executable.
  562. FileAccess::set_unix_permissions(file, 0755);
  563. }
  564. memdelete(f);
  565. } else {
  566. err = ERR_CANT_CREATE;
  567. }
  568. }
  569. }
  570. ret = unzGoToNextFile(src_pkg_zip);
  571. }
  572. // We're done with our source zip.
  573. unzClose(src_pkg_zip);
  574. if (!found_binary) {
  575. ERR_PRINTS("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  576. err = ERR_FILE_NOT_FOUND;
  577. }
  578. if (err == OK) {
  579. if (ep.step("Making PKG", 1)) {
  580. return ERR_SKIP;
  581. }
  582. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  583. Vector<SharedObject> shared_objects;
  584. err = save_pack(p_preset, pack_path, &shared_objects);
  585. // See if we can code sign our new package.
  586. bool sign_enabled = p_preset->get("codesign/enable");
  587. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  588. if (sign_enabled && (ent_path == "")) {
  589. ent_path = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  590. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  591. if (ent_f) {
  592. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  593. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  594. ent_f->store_line("<plist version=\"1.0\">");
  595. ent_f->store_line("<dict>");
  596. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  597. // These entitlements are required to run managed code, and are always enabled in Mono builds.
  598. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  599. ent_f->store_line("<true/>");
  600. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  601. ent_f->store_line("<true/>");
  602. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  603. ent_f->store_line("<true/>");
  604. } else {
  605. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  606. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  607. ent_f->store_line("<true/>");
  608. }
  609. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  610. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  611. ent_f->store_line("<true/>");
  612. }
  613. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  614. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  615. ent_f->store_line("<true/>");
  616. }
  617. }
  618. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  619. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  620. ent_f->store_line("<true/>");
  621. }
  622. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  623. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  624. ent_f->store_line("<true/>");
  625. }
  626. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  627. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  628. ent_f->store_line("<true/>");
  629. }
  630. if ((bool)p_preset->get("codesign/entitlements/location")) {
  631. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  632. ent_f->store_line("<true/>");
  633. }
  634. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  635. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  636. ent_f->store_line("<true/>");
  637. }
  638. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  639. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  640. ent_f->store_line("<true/>");
  641. }
  642. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  643. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  644. ent_f->store_line("<true/>");
  645. }
  646. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  647. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  648. ent_f->store_line("<true/>");
  649. }
  650. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  651. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  652. ent_f->store_line("<true/>");
  653. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  654. ent_f->store_line("<key>com.apple.security.network.server</key>");
  655. ent_f->store_line("<true/>");
  656. }
  657. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  658. ent_f->store_line("<key>com.apple.security.network.client</key>");
  659. ent_f->store_line("<true/>");
  660. }
  661. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  662. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  663. ent_f->store_line("<true/>");
  664. }
  665. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  666. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  667. ent_f->store_line("<true/>");
  668. }
  669. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  670. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  671. ent_f->store_line("<true/>");
  672. }
  673. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  674. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  675. ent_f->store_line("<true/>");
  676. }
  677. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  678. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  679. ent_f->store_line("<true/>");
  680. }
  681. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  682. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  683. ent_f->store_line("<true/>");
  684. }
  685. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  686. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  687. ent_f->store_line("<true/>");
  688. }
  689. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  690. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  691. ent_f->store_line("<true/>");
  692. }
  693. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  694. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  695. ent_f->store_line("<true/>");
  696. }
  697. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  698. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  699. ent_f->store_line("<true/>");
  700. }
  701. }
  702. ent_f->store_line("</dict>");
  703. ent_f->store_line("</plist>");
  704. ent_f->close();
  705. memdelete(ent_f);
  706. } else {
  707. err = ERR_CANT_CREATE;
  708. }
  709. }
  710. if (err == OK) {
  711. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  712. for (int i = 0; i < shared_objects.size(); i++) {
  713. err = da->copy(shared_objects[i].path, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  714. if (err == OK && sign_enabled) {
  715. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file(), ent_path);
  716. }
  717. }
  718. memdelete(da);
  719. }
  720. if (sign_enabled) {
  721. for (int i = 0; i < dylibs_found.size(); i++) {
  722. if (err == OK) {
  723. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  724. }
  725. }
  726. }
  727. if (err == OK && sign_enabled) {
  728. if (ep.step("Code signing bundle", 2)) {
  729. return ERR_SKIP;
  730. }
  731. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  732. }
  733. if (export_format == "dmg") {
  734. // Create a DMG.
  735. if (err == OK) {
  736. if (ep.step("Making DMG", 3)) {
  737. return ERR_SKIP;
  738. }
  739. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  740. }
  741. // Sign DMG.
  742. if (err == OK && sign_enabled) {
  743. if (ep.step("Code signing DMG", 3)) {
  744. return ERR_SKIP;
  745. }
  746. err = _code_sign(p_preset, p_path, ent_path);
  747. }
  748. } else {
  749. // Create ZIP.
  750. if (err == OK) {
  751. if (ep.step("Making ZIP", 3)) {
  752. return ERR_SKIP;
  753. }
  754. if (FileAccess::exists(p_path)) {
  755. OS::get_singleton()->move_to_trash(p_path);
  756. }
  757. FileAccess *dst_f = nullptr;
  758. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  759. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  760. _zip_folder_recursive(zip, EditorSettings::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  761. zipClose(zip, nullptr);
  762. }
  763. }
  764. // Clean up temporary .app dir.
  765. tmp_app_dir->change_dir(tmp_app_path_name);
  766. tmp_app_dir->erase_contents_recursive();
  767. tmp_app_dir->change_dir("..");
  768. tmp_app_dir->remove(tmp_app_dir_name);
  769. }
  770. return err;
  771. }
  772. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  773. String dir = p_root_path.plus_file(p_folder);
  774. DirAccess *da = DirAccess::open(dir);
  775. da->list_dir_begin();
  776. String f;
  777. while ((f = da->get_next()) != "") {
  778. if (f == "." || f == "..") {
  779. continue;
  780. }
  781. if (da->current_is_dir()) {
  782. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  783. } else {
  784. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name));
  785. OS::Time time = OS::get_singleton()->get_time();
  786. OS::Date date = OS::get_singleton()->get_date();
  787. zip_fileinfo zipfi;
  788. zipfi.tmz_date.tm_hour = time.hour;
  789. zipfi.tmz_date.tm_mday = date.day;
  790. zipfi.tmz_date.tm_min = time.min;
  791. 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/
  792. zipfi.tmz_date.tm_sec = time.sec;
  793. zipfi.tmz_date.tm_year = date.year;
  794. zipfi.dosDate = 0;
  795. // 0100000: regular file type
  796. // 0000755: permissions rwxr-xr-x
  797. // 0000644: permissions rw-r--r--
  798. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  799. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  800. zipfi.internal_fa = 0;
  801. zipOpenNewFileInZip4(p_zip,
  802. p_folder.plus_file(f).utf8().get_data(),
  803. &zipfi,
  804. nullptr,
  805. 0,
  806. nullptr,
  807. 0,
  808. nullptr,
  809. Z_DEFLATED,
  810. Z_DEFAULT_COMPRESSION,
  811. 0,
  812. -MAX_WBITS,
  813. DEF_MEM_LEVEL,
  814. Z_DEFAULT_STRATEGY,
  815. nullptr,
  816. 0,
  817. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  818. 0);
  819. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  820. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  821. zipCloseFileInZip(p_zip);
  822. }
  823. }
  824. da->list_dir_end();
  825. memdelete(da);
  826. }
  827. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  828. String err;
  829. bool valid = false;
  830. // Look for export templates (first official, and if defined custom templates).
  831. bool dvalid = exists_export_template("osx.zip", &err);
  832. bool rvalid = dvalid; // Both in the same ZIP.
  833. if (p_preset->get("custom_template/debug") != "") {
  834. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  835. if (!dvalid) {
  836. err += TTR("Custom debug template not found.") + "\n";
  837. }
  838. }
  839. if (p_preset->get("custom_template/release") != "") {
  840. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  841. if (!rvalid) {
  842. err += TTR("Custom release template not found.") + "\n";
  843. }
  844. }
  845. valid = dvalid || rvalid;
  846. r_missing_templates = !valid;
  847. if (!err.empty())
  848. r_error = err;
  849. return valid;
  850. }
  851. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  852. Ref<Image> img = memnew(Image(_osx_logo));
  853. logo.instance();
  854. logo->create_from_image(img);
  855. }
  856. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  857. }
  858. void register_osx_exporter() {
  859. Ref<EditorExportPlatformOSX> platform;
  860. platform.instance();
  861. EditorExport::get_singleton()->add_export_platform(platform);
  862. }