export.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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/file_access.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "core/version.h"
  38. #include "editor/editor_export.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "platform/osx/logo.gen.h"
  42. #include "string.h"
  43. #include <sys/stat.h>
  44. class EditorExportPlatformOSX : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  46. int version_code;
  47. Ref<ImageTexture> logo;
  48. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  49. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  50. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  51. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  52. #ifdef OSX_ENABLED
  53. bool use_codesign() const { return true; }
  54. bool use_dmg() const { return true; }
  55. #else
  56. bool use_codesign() const { return false; }
  57. bool use_dmg() const { return false; }
  58. #endif
  59. protected:
  60. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  61. virtual void get_export_options(List<ExportOption> *r_options);
  62. public:
  63. virtual String get_name() const { return "Mac OSX"; }
  64. virtual String get_os_name() const { return "OSX"; }
  65. virtual Ref<Texture> get_logo() const { return logo; }
  66. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  67. List<String> list;
  68. if (use_dmg()) {
  69. list.push_back("dmg");
  70. }
  71. list.push_back("zip");
  72. return list;
  73. }
  74. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  75. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  76. virtual void get_platform_features(List<String> *r_features) {
  77. r_features->push_back("pc");
  78. r_features->push_back("s3tc");
  79. r_features->push_back("OSX");
  80. }
  81. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  82. }
  83. EditorExportPlatformOSX();
  84. ~EditorExportPlatformOSX();
  85. };
  86. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  87. if (p_preset->get("texture_format/s3tc")) {
  88. r_features->push_back("s3tc");
  89. }
  90. if (p_preset->get("texture_format/etc")) {
  91. r_features->push_back("etc");
  92. }
  93. if (p_preset->get("texture_format/etc2")) {
  94. r_features->push_back("etc2");
  95. }
  96. r_features->push_back("64");
  97. }
  98. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png"), ""));
  104. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  105. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  106. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  107. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  108. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  109. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  110. #ifdef OSX_ENABLED
  111. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity"), ""));
  112. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements"), ""));
  113. #endif
  114. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  115. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  117. }
  118. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  119. Ref<ImageTexture> it = memnew(ImageTexture);
  120. int size = 512;
  121. Vector<uint8_t> data;
  122. data.resize(8);
  123. data.write[0] = 'i';
  124. data.write[1] = 'c';
  125. data.write[2] = 'n';
  126. data.write[3] = 's';
  127. const char *name[] = { "ic09", "ic08", "ic07", "icp6", "icp5", "icp4" };
  128. int index = 0;
  129. while (size >= 16) {
  130. 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?
  131. copy->convert(Image::FORMAT_RGBA8);
  132. copy->resize(size, size);
  133. it->create_from_image(copy);
  134. String path = EditorSettings::get_singleton()->get_cache_dir().plus_file("icon.png");
  135. ResourceSaver::save(path, it);
  136. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  137. ERR_FAIL_COND(!f);
  138. int ofs = data.size();
  139. uint32_t len = f->get_len();
  140. data.resize(data.size() + len + 8);
  141. f->get_buffer(&data.write[ofs + 8], len);
  142. memdelete(f);
  143. len += 8;
  144. len = BSWAP32(len);
  145. copymem(&data.write[ofs], name[index], 4);
  146. encode_uint32(len, &data.write[ofs + 4]);
  147. index++;
  148. size /= 2;
  149. }
  150. uint32_t total_len = data.size();
  151. total_len = BSWAP32(total_len);
  152. encode_uint32(total_len, &data.write[4]);
  153. p_data = data;
  154. }
  155. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  156. String str;
  157. String strnew;
  158. str.parse_utf8((const char *)plist.ptr(), plist.size());
  159. Vector<String> lines = str.split("\n");
  160. for (int i = 0; i < lines.size(); i++) {
  161. if (lines[i].find("$binary") != -1) {
  162. strnew += lines[i].replace("$binary", p_binary) + "\n";
  163. } else if (lines[i].find("$name") != -1) {
  164. strnew += lines[i].replace("$name", p_binary) + "\n";
  165. } else if (lines[i].find("$info") != -1) {
  166. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  167. } else if (lines[i].find("$identifier") != -1) {
  168. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  169. } else if (lines[i].find("$short_version") != -1) {
  170. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  171. } else if (lines[i].find("$version") != -1) {
  172. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  173. } else if (lines[i].find("$signature") != -1) {
  174. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  175. } else if (lines[i].find("$copyright") != -1) {
  176. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  177. } else if (lines[i].find("$highres") != -1) {
  178. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  179. } else {
  180. strnew += lines[i] + "\n";
  181. }
  182. }
  183. CharString cs = strnew.utf8();
  184. plist.resize(cs.size() - 1);
  185. for (int i = 0; i < cs.size() - 1; i++) {
  186. plist.write[i] = cs[i];
  187. }
  188. }
  189. /**
  190. If we're running the OSX version of the Godot editor we'll:
  191. - export our application bundle to a temporary folder
  192. - attempt to code sign it
  193. - and then wrap it up in a DMG
  194. **/
  195. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  196. List<String> args;
  197. if (p_preset->get("codesign/entitlements") != "") {
  198. /* this should point to our entitlements.plist file that sandboxes our application, I don't know if this should also be placed in our app bundle */
  199. args.push_back("-entitlements");
  200. args.push_back(p_preset->get("codesign/entitlements"));
  201. }
  202. args.push_back("-s");
  203. args.push_back(p_preset->get("codesign/identity"));
  204. args.push_back("-v"); /* provide some more feedback */
  205. args.push_back(p_path);
  206. String str;
  207. Error err = OS::get_singleton()->execute("codesign", args, true, NULL, &str, NULL, true);
  208. ERR_FAIL_COND_V(err != OK, err);
  209. print_line("codesign: " + str);
  210. if (str.find("no identity found") != -1) {
  211. EditorNode::add_io_error("codesign: no identity found");
  212. return FAILED;
  213. }
  214. return OK;
  215. }
  216. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  217. List<String> args;
  218. OS::get_singleton()->move_to_trash(p_dmg_path);
  219. args.push_back("create");
  220. args.push_back(p_dmg_path);
  221. args.push_back("-volname");
  222. args.push_back(p_pkg_name);
  223. args.push_back("-fs");
  224. args.push_back("HFS+");
  225. args.push_back("-srcfolder");
  226. args.push_back(p_app_path_name);
  227. String str;
  228. Error err = OS::get_singleton()->execute("hdiutil", args, true, NULL, &str, NULL, true);
  229. ERR_FAIL_COND_V(err != OK, err);
  230. print_line("hdiutil returned: " + str);
  231. if (str.find("create failed") != -1) {
  232. if (str.find("File exists") != -1) {
  233. EditorNode::add_io_error("hdiutil: create failed - file exists");
  234. } else {
  235. EditorNode::add_io_error("hdiutil: create failed");
  236. }
  237. return FAILED;
  238. }
  239. return OK;
  240. }
  241. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  242. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  243. String src_pkg_name;
  244. EditorProgress ep("export", "Exporting for OSX", 3);
  245. if (p_debug)
  246. src_pkg_name = p_preset->get("custom_package/debug");
  247. else
  248. src_pkg_name = p_preset->get("custom_package/release");
  249. if (src_pkg_name == "") {
  250. String err;
  251. src_pkg_name = find_export_template("osx.zip", &err);
  252. if (src_pkg_name == "") {
  253. EditorNode::add_io_error(err);
  254. return ERR_FILE_NOT_FOUND;
  255. }
  256. }
  257. FileAccess *src_f = NULL;
  258. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  259. ep.step("Creating app", 0);
  260. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  261. if (!src_pkg_zip) {
  262. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  263. return ERR_FILE_NOT_FOUND;
  264. }
  265. ERR_FAIL_COND_V(!src_pkg_zip, ERR_CANT_OPEN);
  266. int ret = unzGoToFirstFile(src_pkg_zip);
  267. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  268. String pkg_name;
  269. if (p_preset->get("application/name") != "")
  270. pkg_name = p_preset->get("application/name"); // app_name
  271. else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "")
  272. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  273. else
  274. pkg_name = "Unnamed";
  275. Error err = OK;
  276. String tmp_app_path_name = "";
  277. zlib_filefunc_def io2 = io;
  278. FileAccess *dst_f = NULL;
  279. io2.opaque = &dst_f;
  280. zipFile dst_pkg_zip = NULL;
  281. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  282. if (export_format == "dmg") {
  283. // We're on OSX so we can export to DMG, but first we create our application bundle
  284. tmp_app_path_name = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".app");
  285. print_line("Exporting to " + tmp_app_path_name);
  286. DirAccess *tmp_app_path = DirAccess::create_for_path(tmp_app_path_name);
  287. if (!tmp_app_path) {
  288. err = ERR_CANT_CREATE;
  289. }
  290. // Create our folder structure or rely on unzip?
  291. if (err == OK) {
  292. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  293. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  294. }
  295. if (err == OK) {
  296. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  297. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  298. }
  299. if (err == OK) {
  300. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  301. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  302. }
  303. } else {
  304. // Open our destination zip file
  305. dst_pkg_zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  306. if (!dst_pkg_zip) {
  307. err = ERR_CANT_CREATE;
  308. }
  309. }
  310. // Now process our template
  311. bool found_binary = false;
  312. int total_size = 0;
  313. while (ret == UNZ_OK && err == OK) {
  314. bool is_execute = false;
  315. //get filename
  316. unz_file_info info;
  317. char fname[16384];
  318. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  319. String file = fname;
  320. Vector<uint8_t> data;
  321. data.resize(info.uncompressed_size);
  322. //read
  323. unzOpenCurrentFile(src_pkg_zip);
  324. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  325. unzCloseCurrentFile(src_pkg_zip);
  326. //write
  327. file = file.replace_first("osx_template.app/", "");
  328. if (file == "Contents/Info.plist") {
  329. _fix_plist(p_preset, data, pkg_name);
  330. }
  331. if (file.begins_with("Contents/MacOS/godot_")) {
  332. if (file != "Contents/MacOS/" + binary_to_use) {
  333. ret = unzGoToNextFile(src_pkg_zip);
  334. continue; //ignore!
  335. }
  336. found_binary = true;
  337. is_execute = true;
  338. file = "Contents/MacOS/" + pkg_name;
  339. }
  340. if (file == "Contents/Resources/icon.icns") {
  341. //see if there is an icon
  342. String iconpath;
  343. if (p_preset->get("application/icon") != "")
  344. iconpath = p_preset->get("application/icon");
  345. else
  346. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  347. if (iconpath != "") {
  348. Ref<Image> icon;
  349. icon.instance();
  350. icon->load(iconpath);
  351. if (!icon->empty()) {
  352. _make_icon(icon, data);
  353. }
  354. }
  355. //bleh?
  356. }
  357. if (data.size() > 0) {
  358. print_line("ADDING: " + file + " size: " + itos(data.size()));
  359. total_size += data.size();
  360. if (export_format == "dmg") {
  361. // write it into our application bundle
  362. file = tmp_app_path_name + "/" + file;
  363. // write the file, need to add chmod
  364. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  365. if (f) {
  366. f->store_buffer(data.ptr(), data.size());
  367. f->close();
  368. if (is_execute) {
  369. // Chmod with 0755 if the file is executable
  370. f->_chmod(file, 0755);
  371. }
  372. memdelete(f);
  373. } else {
  374. err = ERR_CANT_CREATE;
  375. }
  376. } else {
  377. // add it to our zip file
  378. file = pkg_name + ".app/" + file;
  379. zip_fileinfo fi;
  380. fi.tmz_date.tm_hour = info.tmu_date.tm_hour;
  381. fi.tmz_date.tm_min = info.tmu_date.tm_min;
  382. fi.tmz_date.tm_sec = info.tmu_date.tm_sec;
  383. fi.tmz_date.tm_mon = info.tmu_date.tm_mon;
  384. fi.tmz_date.tm_mday = info.tmu_date.tm_mday;
  385. fi.tmz_date.tm_year = info.tmu_date.tm_year;
  386. fi.dosDate = info.dosDate;
  387. fi.internal_fa = info.internal_fa;
  388. fi.external_fa = info.external_fa;
  389. zipOpenNewFileInZip(dst_pkg_zip,
  390. file.utf8().get_data(),
  391. &fi,
  392. NULL,
  393. 0,
  394. NULL,
  395. 0,
  396. NULL,
  397. Z_DEFLATED,
  398. Z_DEFAULT_COMPRESSION);
  399. zipWriteInFileInZip(dst_pkg_zip, data.ptr(), data.size());
  400. zipCloseFileInZip(dst_pkg_zip);
  401. }
  402. }
  403. ret = unzGoToNextFile(src_pkg_zip);
  404. }
  405. // we're done with our source zip
  406. unzClose(src_pkg_zip);
  407. if (!found_binary) {
  408. ERR_PRINTS("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  409. err = ERR_FILE_NOT_FOUND;
  410. }
  411. if (err == OK) {
  412. ep.step("Making PKG", 1);
  413. if (export_format == "dmg") {
  414. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  415. Vector<SharedObject> shared_objects;
  416. err = save_pack(p_preset, pack_path, &shared_objects);
  417. // see if we can code sign our new package
  418. String identity = p_preset->get("codesign/identity");
  419. if (err == OK) {
  420. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  421. for (int i = 0; i < shared_objects.size(); i++) {
  422. err = da->copy(shared_objects[i].path, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  423. if (err == OK && identity != "") {
  424. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  425. }
  426. }
  427. memdelete(da);
  428. }
  429. if (err == OK && identity != "") {
  430. ep.step("Code signing bundle", 2);
  431. // the order in which we code sign is important, this is a bit of a shame or we could do this in our loop that extracts the files from our ZIP
  432. // start with our application
  433. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name);
  434. ///@TODO we should check the contents of /Contents/Frameworks for frameworks to sign
  435. }
  436. if (err == OK && identity != "") {
  437. // we should probably loop through all resources and sign them?
  438. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Resources/icon.icns");
  439. }
  440. if (err == OK && identity != "") {
  441. err = _code_sign(p_preset, pack_path);
  442. }
  443. if (err == OK && identity != "") {
  444. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Info.plist");
  445. }
  446. // and finally create a DMG
  447. if (err == OK) {
  448. ep.step("Making DMG", 3);
  449. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  450. }
  451. // Clean up temporary .app dir
  452. OS::get_singleton()->move_to_trash(tmp_app_path_name);
  453. } else {
  454. String pack_path = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".pck");
  455. Vector<SharedObject> shared_objects;
  456. err = save_pack(p_preset, pack_path, &shared_objects);
  457. if (err == OK) {
  458. zipOpenNewFileInZip(dst_pkg_zip,
  459. (pkg_name + ".app/Contents/Resources/" + pkg_name + ".pck").utf8().get_data(),
  460. NULL,
  461. NULL,
  462. 0,
  463. NULL,
  464. 0,
  465. NULL,
  466. Z_DEFLATED,
  467. Z_DEFAULT_COMPRESSION);
  468. FileAccess *pf = FileAccess::open(pack_path, FileAccess::READ);
  469. if (pf) {
  470. const int BSIZE = 16384;
  471. uint8_t buf[BSIZE];
  472. while (true) {
  473. int r = pf->get_buffer(buf, BSIZE);
  474. if (r <= 0)
  475. break;
  476. zipWriteInFileInZip(dst_pkg_zip, buf, r);
  477. }
  478. zipCloseFileInZip(dst_pkg_zip);
  479. memdelete(pf);
  480. } else {
  481. err = ERR_CANT_OPEN;
  482. }
  483. }
  484. if (err == OK) {
  485. //add shared objects
  486. for (int i = 0; i < shared_objects.size(); i++) {
  487. Vector<uint8_t> file = FileAccess::get_file_as_array(shared_objects[i].path);
  488. ERR_CONTINUE(file.empty());
  489. zipOpenNewFileInZip(dst_pkg_zip,
  490. (pkg_name + ".app/Contents/Frameworks/").plus_file(shared_objects[i].path.get_file()).utf8().get_data(),
  491. NULL,
  492. NULL,
  493. 0,
  494. NULL,
  495. 0,
  496. NULL,
  497. Z_DEFLATED,
  498. Z_DEFAULT_COMPRESSION);
  499. zipWriteInFileInZip(dst_pkg_zip, file.ptr(), file.size());
  500. zipCloseFileInZip(dst_pkg_zip);
  501. }
  502. }
  503. }
  504. }
  505. if (dst_pkg_zip) {
  506. zipClose(dst_pkg_zip, NULL);
  507. }
  508. return err;
  509. }
  510. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  511. bool valid = false;
  512. String err;
  513. if (exists_export_template("osx.zip", &err)) {
  514. valid = true;
  515. }
  516. if (p_preset->get("custom_package/debug") != "") {
  517. if (FileAccess::exists(p_preset->get("custom_package/debug"))) {
  518. valid = true;
  519. } else {
  520. err += "Custom debug package not found.\n";
  521. }
  522. }
  523. if (p_preset->get("custom_package/release") != "") {
  524. if (FileAccess::exists(p_preset->get("custom_package/release"))) {
  525. valid = true;
  526. } else {
  527. err += "Custom release package not found.\n";
  528. }
  529. }
  530. if (!err.empty())
  531. r_error = err;
  532. r_missing_templates = !valid;
  533. return valid;
  534. }
  535. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  536. Ref<Image> img = memnew(Image(_osx_logo));
  537. logo.instance();
  538. logo->create_from_image(img);
  539. }
  540. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  541. }
  542. void register_osx_exporter() {
  543. Ref<EditorExportPlatformOSX> platform;
  544. platform.instance();
  545. EditorExport::get_singleton()->add_export_platform(platform);
  546. }