export.cpp 21 KB

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