export.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "string.h"
  44. #include <sys/stat.h>
  45. class EditorExportPlatformOSX : public EditorExportPlatform {
  46. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  47. int version_code;
  48. Ref<ImageTexture> logo;
  49. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  50. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  51. Error _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  52. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  53. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  54. void _zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name);
  55. #ifdef OSX_ENABLED
  56. bool use_codesign() const { return true; }
  57. bool use_dmg() const { return true; }
  58. #else
  59. bool use_codesign() const { return false; }
  60. bool use_dmg() const { return false; }
  61. #endif
  62. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  63. String pname = p_package;
  64. if (pname.length() == 0) {
  65. if (r_error) {
  66. *r_error = TTR("Identifier is missing.");
  67. }
  68. return false;
  69. }
  70. for (int i = 0; i < pname.length(); i++) {
  71. CharType c = pname[i];
  72. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  73. if (r_error) {
  74. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  75. }
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. protected:
  82. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  83. virtual void get_export_options(List<ExportOption> *r_options);
  84. public:
  85. virtual String get_name() const { return "Mac OSX"; }
  86. virtual String get_os_name() const { return "OSX"; }
  87. virtual Ref<Texture2D> get_logo() const { return logo; }
  88. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  89. List<String> list;
  90. if (use_dmg()) {
  91. list.push_back("dmg");
  92. }
  93. list.push_back("zip");
  94. return list;
  95. }
  96. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  97. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  98. virtual void get_platform_features(List<String> *r_features) {
  99. r_features->push_back("pc");
  100. r_features->push_back("s3tc");
  101. r_features->push_back("OSX");
  102. }
  103. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  104. }
  105. EditorExportPlatformOSX();
  106. ~EditorExportPlatformOSX();
  107. };
  108. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  109. if (p_preset->get("texture_format/s3tc")) {
  110. r_features->push_back("s3tc");
  111. }
  112. if (p_preset->get("texture_format/etc")) {
  113. r_features->push_back("etc");
  114. }
  115. if (p_preset->get("texture_format/etc2")) {
  116. r_features->push_back("etc2");
  117. }
  118. r_features->push_back("64");
  119. }
  120. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  132. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/camera_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the camera"), ""));
  133. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/microphone_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the microphone"), ""));
  134. #ifdef OSX_ENABLED
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  141. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  142. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  143. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_password", PROPERTY_HINT_PLACEHOLDER_TEXT, "Enable two-factor authentication and provide app-specific password"), ""));
  144. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_team_id", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide team ID if your Apple ID belongs to multiple teams"), ""));
  145. #endif
  146. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  147. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  148. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  149. }
  150. void _rgba8_to_packbits_encode(int p_ch, int p_size, Vector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  151. int src_len = p_size * p_size;
  152. Vector<uint8_t> result;
  153. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  154. int res_size = 0;
  155. uint8_t buf[128];
  156. int buf_size = 0;
  157. int i = 0;
  158. while (i < src_len) {
  159. uint8_t cur = p_source.ptr()[i * 4 + p_ch];
  160. if (i < src_len - 2) {
  161. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(i + 2) * 4 + p_ch] == cur)) {
  162. if (buf_size > 0) {
  163. result.write[res_size++] = (uint8_t)(buf_size - 1);
  164. copymem(&result.write[res_size], &buf, buf_size);
  165. res_size += buf_size;
  166. buf_size = 0;
  167. }
  168. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  169. bool hit_lim = true;
  170. for (int j = 3; j <= lim; j++) {
  171. if (p_source.ptr()[(i + j) * 4 + p_ch] != cur) {
  172. hit_lim = false;
  173. i = i + j - 1;
  174. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  175. result.write[res_size++] = cur;
  176. break;
  177. }
  178. }
  179. if (hit_lim) {
  180. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  181. result.write[res_size++] = cur;
  182. i = i + lim;
  183. }
  184. } else {
  185. buf[buf_size++] = cur;
  186. if (buf_size == 128) {
  187. result.write[res_size++] = (uint8_t)(buf_size - 1);
  188. copymem(&result.write[res_size], &buf, buf_size);
  189. res_size += buf_size;
  190. buf_size = 0;
  191. }
  192. }
  193. } else {
  194. buf[buf_size++] = cur;
  195. result.write[res_size++] = (uint8_t)(buf_size - 1);
  196. copymem(&result.write[res_size], &buf, buf_size);
  197. res_size += buf_size;
  198. buf_size = 0;
  199. }
  200. i++;
  201. }
  202. int ofs = p_dest.size();
  203. p_dest.resize(p_dest.size() + res_size);
  204. copymem(&p_dest.write[ofs], result.ptr(), res_size);
  205. }
  206. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  207. Ref<ImageTexture> it = memnew(ImageTexture);
  208. Vector<uint8_t> data;
  209. data.resize(8);
  210. data.write[0] = 'i';
  211. data.write[1] = 'c';
  212. data.write[2] = 'n';
  213. data.write[3] = 's';
  214. struct MacOSIconInfo {
  215. const char *name;
  216. const char *mask_name;
  217. bool is_png;
  218. int size;
  219. };
  220. static const MacOSIconInfo icon_infos[] = {
  221. { "ic10", "", true, 1024 }, //1024x1024 32-bit PNG and 512x512@2x 32-bit "retina" PNG
  222. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  223. { "ic14", "", true, 512 }, //256x256@2x 32-bit "retina" PNG
  224. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  225. { "ic13", "", true, 256 }, //128x128@2x 32-bit "retina" PNG
  226. { "ic07", "", true, 128 }, //128x128 32-bit PNG
  227. { "ic12", "", true, 64 }, //32x32@2x 32-bit "retina" PNG
  228. { "ic11", "", true, 32 }, //16x16@2x 32-bit "retina" PNG
  229. { "il32", "l8mk", false, 32 }, //32x32 24-bit RLE + 8-bit uncompressed mask
  230. { "is32", "s8mk", false, 16 } //16x16 24-bit RLE + 8-bit uncompressed mask
  231. };
  232. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  233. 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?
  234. copy->convert(Image::FORMAT_RGBA8);
  235. copy->resize(icon_infos[i].size, icon_infos[i].size);
  236. if (icon_infos[i].is_png) {
  237. // Encode PNG icon.
  238. it->create_from_image(copy);
  239. String path = EditorSettings::get_singleton()->get_cache_dir().plus_file("icon.png");
  240. ResourceSaver::save(path, it);
  241. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  242. if (!f) {
  243. // Clean up generated file.
  244. DirAccess::remove_file_or_error(path);
  245. ERR_FAIL();
  246. }
  247. int ofs = data.size();
  248. uint32_t len = f->get_len();
  249. data.resize(data.size() + len + 8);
  250. f->get_buffer(&data.write[ofs + 8], len);
  251. memdelete(f);
  252. len += 8;
  253. len = BSWAP32(len);
  254. copymem(&data.write[ofs], icon_infos[i].name, 4);
  255. encode_uint32(len, &data.write[ofs + 4]);
  256. // Clean up generated file.
  257. DirAccess::remove_file_or_error(path);
  258. } else {
  259. Vector<uint8_t> src_data = copy->get_data();
  260. //encode 24bit RGB RLE icon
  261. {
  262. int ofs = data.size();
  263. data.resize(data.size() + 8);
  264. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  265. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  266. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  267. int len = data.size() - ofs;
  268. len = BSWAP32(len);
  269. copymem(&data.write[ofs], icon_infos[i].name, 4);
  270. encode_uint32(len, &data.write[ofs + 4]);
  271. }
  272. //encode 8bit mask uncompressed icon
  273. {
  274. int ofs = data.size();
  275. int len = copy->get_width() * copy->get_height();
  276. data.resize(data.size() + len + 8);
  277. for (int j = 0; j < len; j++) {
  278. data.write[ofs + 8 + j] = src_data.ptr()[j * 4 + 3];
  279. }
  280. len += 8;
  281. len = BSWAP32(len);
  282. copymem(&data.write[ofs], icon_infos[i].mask_name, 4);
  283. encode_uint32(len, &data.write[ofs + 4]);
  284. }
  285. }
  286. }
  287. uint32_t total_len = data.size();
  288. total_len = BSWAP32(total_len);
  289. encode_uint32(total_len, &data.write[4]);
  290. p_data = data;
  291. }
  292. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  293. String str;
  294. String strnew;
  295. str.parse_utf8((const char *)plist.ptr(), plist.size());
  296. Vector<String> lines = str.split("\n");
  297. for (int i = 0; i < lines.size(); i++) {
  298. if (lines[i].find("$binary") != -1) {
  299. strnew += lines[i].replace("$binary", p_binary) + "\n";
  300. } else if (lines[i].find("$name") != -1) {
  301. strnew += lines[i].replace("$name", p_binary) + "\n";
  302. } else if (lines[i].find("$info") != -1) {
  303. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  304. } else if (lines[i].find("$bundle_identifier") != -1) {
  305. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  306. } else if (lines[i].find("$short_version") != -1) {
  307. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  308. } else if (lines[i].find("$version") != -1) {
  309. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  310. } else if (lines[i].find("$signature") != -1) {
  311. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  312. } else if (lines[i].find("$copyright") != -1) {
  313. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  314. } else if (lines[i].find("$highres") != -1) {
  315. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  316. } else if (lines[i].find("$camera_usage_description") != -1) {
  317. String description = p_preset->get("privacy/camera_usage_description");
  318. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  319. } else if (lines[i].find("$microphone_usage_description") != -1) {
  320. String description = p_preset->get("privacy/microphone_usage_description");
  321. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  322. } else {
  323. strnew += lines[i] + "\n";
  324. }
  325. }
  326. CharString cs = strnew.utf8();
  327. plist.resize(cs.size() - 1);
  328. for (int i = 0; i < cs.size() - 1; i++) {
  329. plist.write[i] = cs[i];
  330. }
  331. }
  332. /**
  333. If we're running the OSX version of the Godot editor we'll:
  334. - export our application bundle to a temporary folder
  335. - attempt to code sign it
  336. - and then wrap it up in a DMG
  337. **/
  338. Error EditorExportPlatformOSX::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  339. #ifdef OSX_ENABLED
  340. List<String> args;
  341. args.push_back("altool");
  342. args.push_back("--notarize-app");
  343. args.push_back("--primary-bundle-id");
  344. args.push_back(p_preset->get("application/bundle_identifier"));
  345. args.push_back("--username");
  346. args.push_back(p_preset->get("notarization/apple_id_name"));
  347. args.push_back("--password");
  348. args.push_back(p_preset->get("notarization/apple_id_password"));
  349. args.push_back("--type");
  350. args.push_back("osx");
  351. if (p_preset->get("notarization/apple_team_id")) {
  352. args.push_back("--asc-provider");
  353. args.push_back(p_preset->get("notarization/apple_team_id"));
  354. }
  355. args.push_back("--file");
  356. args.push_back(p_path);
  357. String str;
  358. Error err = OS::get_singleton()->execute("xcrun", args, true, nullptr, &str, nullptr, true);
  359. ERR_FAIL_COND_V(err != OK, err);
  360. print_line("altool (" + p_path + "):\n" + str);
  361. if (str.find("RequestUUID") == -1) {
  362. EditorNode::add_io_error("altool: " + str);
  363. return FAILED;
  364. } else {
  365. print_line("Note: The notarization process generally takes less than an hour. When the process is completed, you'll receive an email.");
  366. print_line(" You can check progress manually by opening a Terminal and running the following command:");
  367. print_line(" \"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  368. }
  369. #endif
  370. return OK;
  371. }
  372. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  373. #ifdef OSX_ENABLED
  374. List<String> args;
  375. if (p_preset->get("codesign/timestamp")) {
  376. args.push_back("--timestamp");
  377. }
  378. if (p_preset->get("codesign/hardened_runtime")) {
  379. args.push_back("--options");
  380. args.push_back("runtime");
  381. }
  382. if ((p_preset->get("codesign/entitlements") != "") && (p_path.get_extension() != "dmg")) {
  383. args.push_back("--entitlements");
  384. args.push_back(p_preset->get("codesign/entitlements"));
  385. }
  386. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  387. for (int i = 0; i < user_args.size(); i++) {
  388. String user_arg = user_args[i].strip_edges();
  389. if (!user_arg.empty()) {
  390. args.push_back(user_arg);
  391. }
  392. }
  393. args.push_back("-s");
  394. args.push_back(p_preset->get("codesign/identity"));
  395. args.push_back("-v"); /* provide some more feedback */
  396. args.push_back(p_path);
  397. String str;
  398. Error err = OS::get_singleton()->execute("codesign", args, true, nullptr, &str, nullptr, true);
  399. ERR_FAIL_COND_V(err != OK, err);
  400. print_line("codesign (" + p_path + "):\n" + str);
  401. if (str.find("no identity found") != -1) {
  402. EditorNode::add_io_error("codesign: no identity found");
  403. return FAILED;
  404. }
  405. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  406. EditorNode::add_io_error("codesign: invalid entitlements file");
  407. return FAILED;
  408. }
  409. #endif
  410. return OK;
  411. }
  412. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  413. List<String> args;
  414. if (FileAccess::exists(p_dmg_path)) {
  415. OS::get_singleton()->move_to_trash(p_dmg_path);
  416. }
  417. args.push_back("create");
  418. args.push_back(p_dmg_path);
  419. args.push_back("-volname");
  420. args.push_back(p_pkg_name);
  421. args.push_back("-fs");
  422. args.push_back("HFS+");
  423. args.push_back("-srcfolder");
  424. args.push_back(p_app_path_name);
  425. String str;
  426. Error err = OS::get_singleton()->execute("hdiutil", args, true, nullptr, &str, nullptr, true);
  427. ERR_FAIL_COND_V(err != OK, err);
  428. print_line("hdiutil returned: " + str);
  429. if (str.find("create failed") != -1) {
  430. if (str.find("File exists") != -1) {
  431. EditorNode::add_io_error("hdiutil: create failed - file exists");
  432. } else {
  433. EditorNode::add_io_error("hdiutil: create failed");
  434. }
  435. return FAILED;
  436. }
  437. return OK;
  438. }
  439. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  440. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  441. String src_pkg_name;
  442. EditorProgress ep("export", "Exporting for OSX", 3, true);
  443. if (p_debug) {
  444. src_pkg_name = p_preset->get("custom_template/debug");
  445. } else {
  446. src_pkg_name = p_preset->get("custom_template/release");
  447. }
  448. if (src_pkg_name == "") {
  449. String err;
  450. src_pkg_name = find_export_template("osx.zip", &err);
  451. if (src_pkg_name == "") {
  452. EditorNode::add_io_error(err);
  453. return ERR_FILE_NOT_FOUND;
  454. }
  455. }
  456. if (!DirAccess::exists(p_path.get_base_dir())) {
  457. return ERR_FILE_BAD_PATH;
  458. }
  459. FileAccess *src_f = nullptr;
  460. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  461. if (ep.step("Creating app", 0)) {
  462. return ERR_SKIP;
  463. }
  464. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  465. if (!src_pkg_zip) {
  466. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  467. return ERR_FILE_NOT_FOUND;
  468. }
  469. int ret = unzGoToFirstFile(src_pkg_zip);
  470. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  471. String pkg_name;
  472. if (p_preset->get("application/name") != "") {
  473. pkg_name = p_preset->get("application/name"); // app_name
  474. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  475. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  476. } else {
  477. pkg_name = "Unnamed";
  478. }
  479. String pkg_name_safe = OS::get_singleton()->get_safe_dir_name(pkg_name);
  480. Error err = OK;
  481. String tmp_app_path_name = "";
  482. DirAccess *tmp_app_path = nullptr;
  483. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  484. // Create our application bundle.
  485. tmp_app_path_name = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".app");
  486. print_line("Exporting to " + tmp_app_path_name);
  487. tmp_app_path = DirAccess::create_for_path(tmp_app_path_name);
  488. if (!tmp_app_path) {
  489. err = ERR_CANT_CREATE;
  490. }
  491. // Create our folder structure.
  492. if (err == OK) {
  493. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  494. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  495. }
  496. if (err == OK) {
  497. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  498. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  499. }
  500. if (err == OK) {
  501. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  502. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  503. }
  504. // Now process our template.
  505. bool found_binary = false;
  506. int total_size = 0;
  507. while (ret == UNZ_OK && err == OK) {
  508. bool is_execute = false;
  509. // Get filename.
  510. unz_file_info info;
  511. char fname[16384];
  512. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  513. String file = fname;
  514. Vector<uint8_t> data;
  515. data.resize(info.uncompressed_size);
  516. // Read.
  517. unzOpenCurrentFile(src_pkg_zip);
  518. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  519. unzCloseCurrentFile(src_pkg_zip);
  520. // Write.
  521. file = file.replace_first("osx_template.app/", "");
  522. if (file == "Contents/Info.plist") {
  523. _fix_plist(p_preset, data, pkg_name);
  524. }
  525. if (file.begins_with("Contents/MacOS/godot_")) {
  526. if (file != "Contents/MacOS/" + binary_to_use) {
  527. ret = unzGoToNextFile(src_pkg_zip);
  528. continue; // skip
  529. }
  530. found_binary = true;
  531. is_execute = true;
  532. file = "Contents/MacOS/" + pkg_name;
  533. }
  534. if (file == "Contents/Resources/icon.icns") {
  535. // See if there is an icon.
  536. String iconpath;
  537. if (p_preset->get("application/icon") != "") {
  538. iconpath = p_preset->get("application/icon");
  539. } else {
  540. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  541. }
  542. if (iconpath != "") {
  543. if (iconpath.get_extension() == "icns") {
  544. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  545. if (icon) {
  546. data.resize(icon->get_len());
  547. icon->get_buffer(&data.write[0], icon->get_len());
  548. icon->close();
  549. memdelete(icon);
  550. }
  551. } else {
  552. Ref<Image> icon;
  553. icon.instance();
  554. icon->load(iconpath);
  555. if (!icon->empty()) {
  556. _make_icon(icon, data);
  557. }
  558. }
  559. }
  560. }
  561. if (data.size() > 0) {
  562. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  563. if (!p_debug) {
  564. ret = unzGoToNextFile(src_pkg_zip);
  565. continue; // skip
  566. }
  567. file = file.replace("/data.mono.osx.64.release_debug/", "/data_" + pkg_name_safe + "/");
  568. }
  569. if (file.find("/data.mono.osx.64.release/") != -1) {
  570. if (p_debug) {
  571. ret = unzGoToNextFile(src_pkg_zip);
  572. continue; // skip
  573. }
  574. file = file.replace("/data.mono.osx.64.release/", "/data_" + pkg_name_safe + "/");
  575. }
  576. print_line("ADDING: " + file + " size: " + itos(data.size()));
  577. total_size += data.size();
  578. // Write it into our application bundle.
  579. file = tmp_app_path_name.plus_file(file);
  580. if (err == OK) {
  581. err = tmp_app_path->make_dir_recursive(file.get_base_dir());
  582. }
  583. if (err == OK) {
  584. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  585. if (f) {
  586. f->store_buffer(data.ptr(), data.size());
  587. f->close();
  588. if (is_execute) {
  589. // chmod with 0755 if the file is executable.
  590. FileAccess::set_unix_permissions(file, 0755);
  591. }
  592. memdelete(f);
  593. } else {
  594. err = ERR_CANT_CREATE;
  595. }
  596. }
  597. }
  598. ret = unzGoToNextFile(src_pkg_zip);
  599. }
  600. // We're done with our source zip.
  601. unzClose(src_pkg_zip);
  602. if (!found_binary) {
  603. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  604. err = ERR_FILE_NOT_FOUND;
  605. }
  606. if (err == OK) {
  607. if (ep.step("Making PKG", 1)) {
  608. return ERR_SKIP;
  609. }
  610. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  611. Vector<SharedObject> shared_objects;
  612. err = save_pack(p_preset, pack_path, &shared_objects);
  613. // See if we can code sign our new package.
  614. bool sign_enabled = p_preset->get("codesign/enable");
  615. if (err == OK) {
  616. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  617. for (int i = 0; i < shared_objects.size(); i++) {
  618. err = da->copy(shared_objects[i].path, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  619. if (err == OK && sign_enabled) {
  620. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  621. }
  622. }
  623. memdelete(da);
  624. }
  625. if (err == OK && sign_enabled) {
  626. if (ep.step("Code signing bundle", 2)) {
  627. return ERR_SKIP;
  628. }
  629. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name);
  630. }
  631. if (export_format == "dmg") {
  632. // Create a DMG.
  633. if (err == OK) {
  634. if (ep.step("Making DMG", 3)) {
  635. return ERR_SKIP;
  636. }
  637. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  638. }
  639. // Sign DMG.
  640. if (err == OK && sign_enabled) {
  641. if (ep.step("Code signing DMG", 3)) {
  642. return ERR_SKIP;
  643. }
  644. err = _code_sign(p_preset, p_path);
  645. }
  646. } else {
  647. // Create ZIP.
  648. if (err == OK) {
  649. if (ep.step("Making ZIP", 3)) {
  650. return ERR_SKIP;
  651. }
  652. if (FileAccess::exists(p_path)) {
  653. OS::get_singleton()->move_to_trash(p_path);
  654. }
  655. FileAccess *dst_f = nullptr;
  656. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  657. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  658. _zip_folder_recursive(zip, EditorSettings::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  659. zipClose(zip, nullptr);
  660. }
  661. }
  662. bool noto_enabled = p_preset->get("notarization/enable");
  663. if (err == OK && noto_enabled) {
  664. if (ep.step("Sending archive for notarization", 4)) {
  665. return ERR_SKIP;
  666. }
  667. err = _notarize(p_preset, p_path);
  668. }
  669. // Clean up temporary .app dir.
  670. OS::get_singleton()->move_to_trash(tmp_app_path_name);
  671. }
  672. return err;
  673. }
  674. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  675. String dir = p_root_path.plus_file(p_folder);
  676. DirAccess *da = DirAccess::open(dir);
  677. da->list_dir_begin();
  678. String f;
  679. while ((f = da->get_next()) != "") {
  680. if (f == "." || f == "..") {
  681. continue;
  682. }
  683. if (da->current_is_dir()) {
  684. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  685. } else {
  686. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name));
  687. OS::Time time = OS::get_singleton()->get_time();
  688. OS::Date date = OS::get_singleton()->get_date();
  689. zip_fileinfo zipfi;
  690. zipfi.tmz_date.tm_hour = time.hour;
  691. zipfi.tmz_date.tm_mday = date.day;
  692. zipfi.tmz_date.tm_min = time.min;
  693. zipfi.tmz_date.tm_mon = date.month;
  694. zipfi.tmz_date.tm_sec = time.sec;
  695. zipfi.tmz_date.tm_year = date.year;
  696. zipfi.dosDate = 0;
  697. zipfi.external_fa = (is_executable ? 0755 : 0644) << 16L;
  698. zipfi.internal_fa = 0;
  699. zipOpenNewFileInZip4(p_zip,
  700. p_folder.plus_file(f).utf8().get_data(),
  701. &zipfi,
  702. nullptr,
  703. 0,
  704. nullptr,
  705. 0,
  706. nullptr,
  707. Z_DEFLATED,
  708. Z_DEFAULT_COMPRESSION,
  709. 0,
  710. -MAX_WBITS,
  711. DEF_MEM_LEVEL,
  712. Z_DEFAULT_STRATEGY,
  713. nullptr,
  714. 0,
  715. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  716. 0);
  717. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  718. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  719. zipCloseFileInZip(p_zip);
  720. }
  721. }
  722. da->list_dir_end();
  723. memdelete(da);
  724. }
  725. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  726. String err;
  727. bool valid = false;
  728. // Look for export templates (first official, and if defined custom templates).
  729. bool dvalid = exists_export_template("osx.zip", &err);
  730. bool rvalid = dvalid; // Both in the same ZIP.
  731. if (p_preset->get("custom_template/debug") != "") {
  732. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  733. if (!dvalid) {
  734. err += TTR("Custom debug template not found.") + "\n";
  735. }
  736. }
  737. if (p_preset->get("custom_template/release") != "") {
  738. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  739. if (!rvalid) {
  740. err += TTR("Custom release template not found.") + "\n";
  741. }
  742. }
  743. valid = dvalid || rvalid;
  744. r_missing_templates = !valid;
  745. String identifier = p_preset->get("application/bundle_identifier");
  746. String pn_err;
  747. if (!is_package_name_valid(identifier, &pn_err)) {
  748. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  749. valid = false;
  750. }
  751. bool sign_enabled = p_preset->get("codesign/enable");
  752. if (sign_enabled) {
  753. if (p_preset->get("codesign/identity") == "") {
  754. err += TTR("Codesign: identity not specified.") + "\n";
  755. valid = false;
  756. }
  757. }
  758. bool noto_enabled = p_preset->get("notarization/enable");
  759. if (noto_enabled) {
  760. if (!sign_enabled) {
  761. err += TTR("Notarization: code signing required.") + "\n";
  762. valid = false;
  763. }
  764. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  765. if (!hr_enabled) {
  766. err += TTR("Notarization: hardened runtime required.") + "\n";
  767. valid = false;
  768. }
  769. if (p_preset->get("notarization/apple_id_name") == "") {
  770. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  771. valid = false;
  772. }
  773. if (p_preset->get("notarization/apple_id_password") == "") {
  774. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  775. valid = false;
  776. }
  777. }
  778. if (!err.empty()) {
  779. r_error = err;
  780. }
  781. return valid;
  782. }
  783. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  784. Ref<Image> img = memnew(Image(_osx_logo));
  785. logo.instance();
  786. logo->create_from_image(img);
  787. }
  788. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  789. }
  790. void register_osx_exporter() {
  791. Ref<EditorExportPlatformOSX> platform;
  792. platform.instance();
  793. EditorExport::get_singleton()->add_export_platform(platform);
  794. }