export.cpp 23 KB

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