export.cpp 28 KB

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