export.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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/config/project_settings.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/io/zip_io.h"
  35. #include "core/os/dir_access.h"
  36. #include "core/os/file_access.h"
  37. #include "core/os/os.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 = 0;
  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 _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  51. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  52. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  53. void _zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name);
  54. #ifdef OSX_ENABLED
  55. bool use_codesign() const { return true; }
  56. bool use_dmg() const { return true; }
  57. #else
  58. bool use_codesign() const { return false; }
  59. bool use_dmg() const { return false; }
  60. #endif
  61. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  62. String pname = p_package;
  63. if (pname.length() == 0) {
  64. if (r_error) {
  65. *r_error = TTR("Identifier is missing.");
  66. }
  67. return false;
  68. }
  69. for (int i = 0; i < pname.length(); i++) {
  70. char32_t c = pname[i];
  71. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  72. if (r_error) {
  73. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  74. }
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. protected:
  81. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) override;
  82. virtual void get_export_options(List<ExportOption> *r_options) override;
  83. public:
  84. virtual String get_name() const override { return "macOS"; }
  85. virtual String get_os_name() const override { return "macOS"; }
  86. virtual Ref<Texture2D> get_logo() const override { return logo; }
  87. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override {
  88. List<String> list;
  89. if (use_dmg()) {
  90. list.push_back("dmg");
  91. }
  92. list.push_back("zip");
  93. return list;
  94. }
  95. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  96. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override;
  97. virtual void get_platform_features(List<String> *r_features) override {
  98. r_features->push_back("pc");
  99. r_features->push_back("s3tc");
  100. r_features->push_back("macOS");
  101. }
  102. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) override {
  103. }
  104. EditorExportPlatformOSX();
  105. ~EditorExportPlatformOSX();
  106. };
  107. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  108. if (p_preset->get("texture_format/s3tc")) {
  109. r_features->push_back("s3tc");
  110. }
  111. if (p_preset->get("texture_format/etc")) {
  112. r_features->push_back("etc");
  113. }
  114. if (p_preset->get("texture_format/etc2")) {
  115. r_features->push_back("etc2");
  116. }
  117. r_features->push_back("64");
  118. }
  119. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  131. 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"), ""));
  132. 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"), ""));
  133. #ifdef OSX_ENABLED
  134. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  141. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  142. 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"), ""));
  143. 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"), ""));
  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, Vector<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.ptr()[i * 4 + p_ch];
  159. if (i < src_len - 2) {
  160. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(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.ptr()[(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. Vector<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.ptr()[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("$bundle_identifier") != -1) {
  304. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_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::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  338. #ifdef OSX_ENABLED
  339. List<String> args;
  340. args.push_back("altool");
  341. args.push_back("--notarize-app");
  342. args.push_back("--primary-bundle-id");
  343. args.push_back(p_preset->get("application/bundle_identifier"));
  344. args.push_back("--username");
  345. args.push_back(p_preset->get("notarization/apple_id_name"));
  346. args.push_back("--password");
  347. args.push_back(p_preset->get("notarization/apple_id_password"));
  348. args.push_back("--type");
  349. args.push_back("osx");
  350. if (p_preset->get("notarization/apple_team_id")) {
  351. args.push_back("--asc-provider");
  352. args.push_back(p_preset->get("notarization/apple_team_id"));
  353. }
  354. args.push_back("--file");
  355. args.push_back(p_path);
  356. String str;
  357. Error err = OS::get_singleton()->execute("xcrun", args, &str, nullptr, true);
  358. ERR_FAIL_COND_V(err != OK, err);
  359. print_line("altool (" + p_path + "):\n" + str);
  360. if (str.find("RequestUUID") == -1) {
  361. EditorNode::add_io_error("altool: " + str);
  362. return FAILED;
  363. } else {
  364. print_line("Note: The notarization process generally takes less than an hour. When the process is completed, you'll receive an email.");
  365. print_line(" You can check progress manually by opening a Terminal and running the following command:");
  366. print_line(" \"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  367. }
  368. #endif
  369. return OK;
  370. }
  371. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  372. #ifdef OSX_ENABLED
  373. List<String> args;
  374. if (p_preset->get("codesign/timestamp")) {
  375. args.push_back("--timestamp");
  376. }
  377. if (p_preset->get("codesign/hardened_runtime")) {
  378. args.push_back("--options");
  379. args.push_back("runtime");
  380. }
  381. if ((p_preset->get("codesign/entitlements") != "") && (p_path.get_extension() != "dmg")) {
  382. args.push_back("--entitlements");
  383. args.push_back(p_preset->get("codesign/entitlements"));
  384. }
  385. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  386. for (int i = 0; i < user_args.size(); i++) {
  387. String user_arg = user_args[i].strip_edges();
  388. if (!user_arg.is_empty()) {
  389. args.push_back(user_arg);
  390. }
  391. }
  392. args.push_back("-s");
  393. args.push_back(p_preset->get("codesign/identity"));
  394. args.push_back("-v"); /* provide some more feedback */
  395. args.push_back(p_path);
  396. String str;
  397. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  398. ERR_FAIL_COND_V(err != OK, err);
  399. print_line("codesign (" + p_path + "):\n" + str);
  400. if (str.find("no identity found") != -1) {
  401. EditorNode::add_io_error("codesign: no identity found");
  402. return FAILED;
  403. }
  404. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  405. EditorNode::add_io_error("codesign: invalid entitlements file");
  406. return FAILED;
  407. }
  408. #endif
  409. return OK;
  410. }
  411. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  412. List<String> args;
  413. if (FileAccess::exists(p_dmg_path)) {
  414. OS::get_singleton()->move_to_trash(p_dmg_path);
  415. }
  416. args.push_back("create");
  417. args.push_back(p_dmg_path);
  418. args.push_back("-volname");
  419. args.push_back(p_pkg_name);
  420. args.push_back("-fs");
  421. args.push_back("HFS+");
  422. args.push_back("-srcfolder");
  423. args.push_back(p_app_path_name);
  424. String str;
  425. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  426. ERR_FAIL_COND_V(err != OK, err);
  427. print_line("hdiutil returned: " + str);
  428. if (str.find("create failed") != -1) {
  429. if (str.find("File exists") != -1) {
  430. EditorNode::add_io_error("hdiutil: create failed - file exists");
  431. } else {
  432. EditorNode::add_io_error("hdiutil: create failed");
  433. }
  434. return FAILED;
  435. }
  436. return OK;
  437. }
  438. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  439. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  440. String src_pkg_name;
  441. EditorProgress ep("export", "Exporting for OSX", 3, true);
  442. if (p_debug) {
  443. src_pkg_name = p_preset->get("custom_template/debug");
  444. } else {
  445. src_pkg_name = p_preset->get("custom_template/release");
  446. }
  447. if (src_pkg_name == "") {
  448. String err;
  449. src_pkg_name = find_export_template("osx.zip", &err);
  450. if (src_pkg_name == "") {
  451. EditorNode::add_io_error(err);
  452. return ERR_FILE_NOT_FOUND;
  453. }
  454. }
  455. if (!DirAccess::exists(p_path.get_base_dir())) {
  456. return ERR_FILE_BAD_PATH;
  457. }
  458. FileAccess *src_f = nullptr;
  459. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  460. if (ep.step("Creating app", 0)) {
  461. return ERR_SKIP;
  462. }
  463. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  464. if (!src_pkg_zip) {
  465. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  466. return ERR_FILE_NOT_FOUND;
  467. }
  468. int ret = unzGoToFirstFile(src_pkg_zip);
  469. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  470. String pkg_name;
  471. if (p_preset->get("application/name") != "") {
  472. pkg_name = p_preset->get("application/name"); // app_name
  473. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  474. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  475. } else {
  476. pkg_name = "Unnamed";
  477. }
  478. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  479. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  480. // Create our application bundle.
  481. String tmp_app_dir_name = pkg_name + ".app";
  482. String tmp_app_path_name = EditorSettings::get_singleton()->get_cache_dir().plus_file(tmp_app_dir_name);
  483. print_line("Exporting to " + tmp_app_path_name);
  484. Error err = OK;
  485. DirAccessRef tmp_app_dir = DirAccess::create_for_path(tmp_app_path_name);
  486. if (!tmp_app_dir) {
  487. err = ERR_CANT_CREATE;
  488. }
  489. // Create our folder structure.
  490. if (err == OK) {
  491. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  492. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  493. }
  494. if (err == OK) {
  495. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  496. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  497. }
  498. if (err == OK) {
  499. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  500. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  501. }
  502. // Now process our template.
  503. bool found_binary = false;
  504. int total_size = 0;
  505. while (ret == UNZ_OK && err == OK) {
  506. bool is_execute = false;
  507. // Get filename.
  508. unz_file_info info;
  509. char fname[16384];
  510. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  511. String file = fname;
  512. Vector<uint8_t> data;
  513. data.resize(info.uncompressed_size);
  514. // Read.
  515. unzOpenCurrentFile(src_pkg_zip);
  516. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  517. unzCloseCurrentFile(src_pkg_zip);
  518. // Write.
  519. file = file.replace_first("osx_template.app/", "");
  520. if (file == "Contents/Info.plist") {
  521. _fix_plist(p_preset, data, pkg_name);
  522. }
  523. if (file.begins_with("Contents/MacOS/godot_")) {
  524. if (file != "Contents/MacOS/" + binary_to_use) {
  525. ret = unzGoToNextFile(src_pkg_zip);
  526. continue; // skip
  527. }
  528. found_binary = true;
  529. is_execute = true;
  530. file = "Contents/MacOS/" + pkg_name;
  531. }
  532. if (file == "Contents/Resources/icon.icns") {
  533. // See if there is an icon.
  534. String iconpath;
  535. if (p_preset->get("application/icon") != "") {
  536. iconpath = p_preset->get("application/icon");
  537. } else {
  538. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  539. }
  540. if (iconpath != "") {
  541. if (iconpath.get_extension() == "icns") {
  542. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  543. if (icon) {
  544. data.resize(icon->get_len());
  545. icon->get_buffer(&data.write[0], icon->get_len());
  546. icon->close();
  547. memdelete(icon);
  548. }
  549. } else {
  550. Ref<Image> icon;
  551. icon.instance();
  552. icon->load(iconpath);
  553. if (!icon->is_empty()) {
  554. _make_icon(icon, data);
  555. }
  556. }
  557. }
  558. }
  559. if (data.size() > 0) {
  560. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  561. if (!p_debug) {
  562. ret = unzGoToNextFile(src_pkg_zip);
  563. continue; // skip
  564. }
  565. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  566. }
  567. if (file.find("/data.mono.osx.64.release/") != -1) {
  568. if (p_debug) {
  569. ret = unzGoToNextFile(src_pkg_zip);
  570. continue; // skip
  571. }
  572. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  573. }
  574. print_line("ADDING: " + file + " size: " + itos(data.size()));
  575. total_size += data.size();
  576. // Write it into our application bundle.
  577. file = tmp_app_path_name.plus_file(file);
  578. if (err == OK) {
  579. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  580. }
  581. if (err == OK) {
  582. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  583. if (f) {
  584. f->store_buffer(data.ptr(), data.size());
  585. f->close();
  586. if (is_execute) {
  587. // chmod with 0755 if the file is executable.
  588. FileAccess::set_unix_permissions(file, 0755);
  589. }
  590. memdelete(f);
  591. } else {
  592. err = ERR_CANT_CREATE;
  593. }
  594. }
  595. }
  596. ret = unzGoToNextFile(src_pkg_zip);
  597. }
  598. // We're done with our source zip.
  599. unzClose(src_pkg_zip);
  600. if (!found_binary) {
  601. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  602. err = ERR_FILE_NOT_FOUND;
  603. }
  604. if (err == OK) {
  605. if (ep.step("Making PKG", 1)) {
  606. return ERR_SKIP;
  607. }
  608. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  609. Vector<SharedObject> shared_objects;
  610. err = save_pack(p_preset, pack_path, &shared_objects);
  611. // See if we can code sign our new package.
  612. bool sign_enabled = p_preset->get("codesign/enable");
  613. if (err == OK) {
  614. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  615. for (int i = 0; i < shared_objects.size(); i++) {
  616. err = da->copy(shared_objects[i].path, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  617. if (err == OK && sign_enabled) {
  618. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  619. }
  620. }
  621. memdelete(da);
  622. }
  623. if (err == OK && sign_enabled) {
  624. if (ep.step("Code signing bundle", 2)) {
  625. return ERR_SKIP;
  626. }
  627. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name);
  628. }
  629. if (export_format == "dmg") {
  630. // Create a DMG.
  631. if (err == OK) {
  632. if (ep.step("Making DMG", 3)) {
  633. return ERR_SKIP;
  634. }
  635. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  636. }
  637. // Sign DMG.
  638. if (err == OK && sign_enabled) {
  639. if (ep.step("Code signing DMG", 3)) {
  640. return ERR_SKIP;
  641. }
  642. err = _code_sign(p_preset, p_path);
  643. }
  644. } else {
  645. // Create ZIP.
  646. if (err == OK) {
  647. if (ep.step("Making ZIP", 3)) {
  648. return ERR_SKIP;
  649. }
  650. if (FileAccess::exists(p_path)) {
  651. OS::get_singleton()->move_to_trash(p_path);
  652. }
  653. FileAccess *dst_f = nullptr;
  654. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  655. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  656. _zip_folder_recursive(zip, EditorSettings::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  657. zipClose(zip, nullptr);
  658. }
  659. }
  660. bool noto_enabled = p_preset->get("notarization/enable");
  661. if (err == OK && noto_enabled) {
  662. if (ep.step("Sending archive for notarization", 4)) {
  663. return ERR_SKIP;
  664. }
  665. err = _notarize(p_preset, p_path);
  666. }
  667. // Clean up temporary .app dir.
  668. tmp_app_dir->change_dir(tmp_app_path_name);
  669. tmp_app_dir->erase_contents_recursive();
  670. tmp_app_dir->change_dir("..");
  671. tmp_app_dir->remove(tmp_app_dir_name);
  672. }
  673. return err;
  674. }
  675. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  676. String dir = p_root_path.plus_file(p_folder);
  677. DirAccess *da = DirAccess::open(dir);
  678. da->list_dir_begin();
  679. String f;
  680. while ((f = da->get_next()) != "") {
  681. if (f == "." || f == "..") {
  682. continue;
  683. }
  684. if (da->current_is_dir()) {
  685. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  686. } else {
  687. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name));
  688. OS::Time time = OS::get_singleton()->get_time();
  689. OS::Date date = OS::get_singleton()->get_date();
  690. zip_fileinfo zipfi;
  691. zipfi.tmz_date.tm_hour = time.hour;
  692. zipfi.tmz_date.tm_mday = date.day;
  693. zipfi.tmz_date.tm_min = time.min;
  694. 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/
  695. zipfi.tmz_date.tm_sec = time.sec;
  696. zipfi.tmz_date.tm_year = date.year;
  697. zipfi.dosDate = 0;
  698. // 0100000: regular file type
  699. // 0000755: permissions rwxr-xr-x
  700. // 0000644: permissions rw-r--r--
  701. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  702. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  703. zipfi.internal_fa = 0;
  704. zipOpenNewFileInZip4(p_zip,
  705. p_folder.plus_file(f).utf8().get_data(),
  706. &zipfi,
  707. nullptr,
  708. 0,
  709. nullptr,
  710. 0,
  711. nullptr,
  712. Z_DEFLATED,
  713. Z_DEFAULT_COMPRESSION,
  714. 0,
  715. -MAX_WBITS,
  716. DEF_MEM_LEVEL,
  717. Z_DEFAULT_STRATEGY,
  718. nullptr,
  719. 0,
  720. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  721. 0);
  722. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  723. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  724. zipCloseFileInZip(p_zip);
  725. }
  726. }
  727. da->list_dir_end();
  728. memdelete(da);
  729. }
  730. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  731. String err;
  732. bool valid = false;
  733. // Look for export templates (first official, and if defined custom templates).
  734. bool dvalid = exists_export_template("osx.zip", &err);
  735. bool rvalid = dvalid; // Both in the same ZIP.
  736. if (p_preset->get("custom_template/debug") != "") {
  737. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  738. if (!dvalid) {
  739. err += TTR("Custom debug template not found.") + "\n";
  740. }
  741. }
  742. if (p_preset->get("custom_template/release") != "") {
  743. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  744. if (!rvalid) {
  745. err += TTR("Custom release template not found.") + "\n";
  746. }
  747. }
  748. valid = dvalid || rvalid;
  749. r_missing_templates = !valid;
  750. String identifier = p_preset->get("application/bundle_identifier");
  751. String pn_err;
  752. if (!is_package_name_valid(identifier, &pn_err)) {
  753. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  754. valid = false;
  755. }
  756. bool sign_enabled = p_preset->get("codesign/enable");
  757. if (sign_enabled) {
  758. if (p_preset->get("codesign/identity") == "") {
  759. err += TTR("Codesign: identity not specified.") + "\n";
  760. valid = false;
  761. }
  762. }
  763. bool noto_enabled = p_preset->get("notarization/enable");
  764. if (noto_enabled) {
  765. if (!sign_enabled) {
  766. err += TTR("Notarization: code signing required.") + "\n";
  767. valid = false;
  768. }
  769. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  770. if (!hr_enabled) {
  771. err += TTR("Notarization: hardened runtime required.") + "\n";
  772. valid = false;
  773. }
  774. if (p_preset->get("notarization/apple_id_name") == "") {
  775. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  776. valid = false;
  777. }
  778. if (p_preset->get("notarization/apple_id_password") == "") {
  779. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  780. valid = false;
  781. }
  782. }
  783. if (!err.is_empty()) {
  784. r_error = err;
  785. }
  786. return valid;
  787. }
  788. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  789. Ref<Image> img = memnew(Image(_osx_logo));
  790. logo.instance();
  791. logo->create_from_image(img);
  792. }
  793. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  794. }
  795. void register_osx_exporter() {
  796. Ref<EditorExportPlatformOSX> platform;
  797. platform.instance();
  798. EditorExport::get_singleton()->add_export_platform(platform);
  799. }