2
0

export.cpp 23 KB

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