export.cpp 22 KB

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