export.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/io/zip_io.h"
  34. #include "core/os/file_access.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "core/version.h"
  38. #include "editor/editor_export.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "platform/osx/logo.gen.h"
  42. #include "string.h"
  43. #include <sys/stat.h>
  44. class EditorExportPlatformOSX : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  46. int version_code;
  47. Ref<ImageTexture> logo;
  48. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  49. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  50. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  51. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  52. #ifdef OSX_ENABLED
  53. bool use_codesign() const { return true; }
  54. bool use_dmg() const { return true; }
  55. #else
  56. bool use_codesign() const { return false; }
  57. bool use_dmg() const { return false; }
  58. #endif
  59. protected:
  60. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  61. virtual void get_export_options(List<ExportOption> *r_options);
  62. public:
  63. virtual String get_name() const { return "Mac OSX"; }
  64. virtual String get_os_name() const { return "OSX"; }
  65. virtual Ref<Texture> get_logo() const { return logo; }
  66. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  67. List<String> list;
  68. if (use_dmg()) {
  69. list.push_back("dmg");
  70. }
  71. list.push_back("zip");
  72. return list;
  73. }
  74. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  75. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  76. virtual void get_platform_features(List<String> *r_features) {
  77. r_features->push_back("pc");
  78. r_features->push_back("s3tc");
  79. r_features->push_back("OSX");
  80. }
  81. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  82. }
  83. EditorExportPlatformOSX();
  84. ~EditorExportPlatformOSX();
  85. };
  86. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  87. if (p_preset->get("texture_format/s3tc")) {
  88. r_features->push_back("s3tc");
  89. }
  90. if (p_preset->get("texture_format/etc")) {
  91. r_features->push_back("etc");
  92. }
  93. if (p_preset->get("texture_format/etc2")) {
  94. r_features->push_back("etc2");
  95. }
  96. r_features->push_back("64");
  97. }
  98. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png"), ""));
  104. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  105. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  106. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  107. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  108. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  109. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  110. #ifdef OSX_ENABLED
  111. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity"), ""));
  112. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements"), ""));
  113. #endif
  114. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  115. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  117. }
  118. void _rgba8_to_packbits_encode(int p_ch, int p_size, PoolVector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  119. int src_len = p_size * p_size;
  120. Vector<uint8_t> result;
  121. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  122. int res_size = 0;
  123. uint8_t buf[128];
  124. int buf_size = 0;
  125. int i = 0;
  126. while (i < src_len) {
  127. uint8_t cur = p_source.read()[i * 4 + p_ch];
  128. if (i < src_len - 2) {
  129. if ((p_source.read()[(i + 1) * 4 + p_ch] == cur) && (p_source.read()[(i + 2) * 4 + p_ch] == cur)) {
  130. if (buf_size > 0) {
  131. result.write[res_size++] = (uint8_t)(buf_size - 1);
  132. copymem(&result.write[res_size], &buf, buf_size);
  133. res_size += buf_size;
  134. buf_size = 0;
  135. }
  136. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  137. bool hit_lim = true;
  138. for (int j = 3; j <= lim; j++) {
  139. if (p_source.read()[(i + j) * 4 + p_ch] != cur) {
  140. hit_lim = false;
  141. i = i + j - 1;
  142. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  143. result.write[res_size++] = cur;
  144. break;
  145. }
  146. }
  147. if (hit_lim) {
  148. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  149. result.write[res_size++] = cur;
  150. i = i + lim;
  151. }
  152. } else {
  153. buf[buf_size++] = cur;
  154. if (buf_size == 128) {
  155. result.write[res_size++] = (uint8_t)(buf_size - 1);
  156. copymem(&result.write[res_size], &buf, buf_size);
  157. res_size += buf_size;
  158. buf_size = 0;
  159. }
  160. }
  161. } else {
  162. buf[buf_size++] = cur;
  163. result.write[res_size++] = (uint8_t)(buf_size - 1);
  164. copymem(&result.write[res_size], &buf, buf_size);
  165. res_size += buf_size;
  166. buf_size = 0;
  167. }
  168. i++;
  169. }
  170. int ofs = p_dest.size();
  171. p_dest.resize(p_dest.size() + res_size);
  172. copymem(&p_dest.write[ofs], result.ptr(), res_size);
  173. }
  174. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  175. Ref<ImageTexture> it = memnew(ImageTexture);
  176. Vector<uint8_t> data;
  177. data.resize(8);
  178. data.write[0] = 'i';
  179. data.write[1] = 'c';
  180. data.write[2] = 'n';
  181. data.write[3] = 's';
  182. struct MacOSIconInfo {
  183. const char *name;
  184. const char *mask_name;
  185. bool is_png;
  186. int size;
  187. };
  188. static const MacOSIconInfo icon_infos[] = {
  189. { "ic10", "", true, 1024 }, //1024x1024 32-bit PNG and 512x512@2x 32-bit "retina" PNG
  190. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  191. { "ic14", "", true, 512 }, //256x256@2x 32-bit "retina" PNG
  192. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  193. { "ic13", "", true, 256 }, //128x128@2x 32-bit "retina" PNG
  194. { "ic07", "", true, 128 }, //128x128 32-bit PNG
  195. { "ic12", "", true, 64 }, //32x32@2x 32-bit "retina" PNG
  196. { "ic11", "", true, 32 }, //16x16@2x 32-bit "retina" PNG
  197. { "il32", "l8mk", false, 32 }, //32x32 24-bit RLE + 8-bit uncompressed mask
  198. { "is32", "s8mk", false, 16 } //16x16 24-bit RLE + 8-bit uncompressed mask
  199. };
  200. for (unsigned int i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  201. 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?
  202. copy->convert(Image::FORMAT_RGBA8);
  203. copy->resize(icon_infos[i].size, icon_infos[i].size);
  204. if (icon_infos[i].is_png) {
  205. //encode png icon
  206. it->create_from_image(copy);
  207. String path = EditorSettings::get_singleton()->get_cache_dir().plus_file("icon.png");
  208. ResourceSaver::save(path, it);
  209. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  210. ERR_FAIL_COND(!f);
  211. int ofs = data.size();
  212. uint32_t len = f->get_len();
  213. data.resize(data.size() + len + 8);
  214. f->get_buffer(&data.write[ofs + 8], len);
  215. memdelete(f);
  216. len += 8;
  217. len = BSWAP32(len);
  218. copymem(&data.write[ofs], icon_infos[i].name, 4);
  219. encode_uint32(len, &data.write[ofs + 4]);
  220. } else {
  221. PoolVector<uint8_t> src_data = copy->get_data();
  222. //encode 24bit RGB RLE icon
  223. {
  224. int ofs = data.size();
  225. data.resize(data.size() + 8);
  226. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  227. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  228. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  229. int len = data.size() - ofs;
  230. len = BSWAP32(len);
  231. copymem(&data.write[ofs], icon_infos[i].name, 4);
  232. encode_uint32(len, &data.write[ofs + 4]);
  233. }
  234. //encode 8bit mask uncompressed icon
  235. {
  236. int ofs = data.size();
  237. int len = copy->get_width() * copy->get_height();
  238. data.resize(data.size() + len + 8);
  239. for (int j = 0; j < len; j++) {
  240. data.write[ofs + 8 + j] = src_data.read()[j * 4 + 3];
  241. }
  242. len += 8;
  243. len = BSWAP32(len);
  244. copymem(&data.write[ofs], icon_infos[i].mask_name, 4);
  245. encode_uint32(len, &data.write[ofs + 4]);
  246. }
  247. }
  248. }
  249. uint32_t total_len = data.size();
  250. total_len = BSWAP32(total_len);
  251. encode_uint32(total_len, &data.write[4]);
  252. p_data = data;
  253. }
  254. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  255. String str;
  256. String strnew;
  257. str.parse_utf8((const char *)plist.ptr(), plist.size());
  258. Vector<String> lines = str.split("\n");
  259. for (int i = 0; i < lines.size(); i++) {
  260. if (lines[i].find("$binary") != -1) {
  261. strnew += lines[i].replace("$binary", p_binary) + "\n";
  262. } else if (lines[i].find("$name") != -1) {
  263. strnew += lines[i].replace("$name", p_binary) + "\n";
  264. } else if (lines[i].find("$info") != -1) {
  265. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  266. } else if (lines[i].find("$identifier") != -1) {
  267. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  268. } else if (lines[i].find("$short_version") != -1) {
  269. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  270. } else if (lines[i].find("$version") != -1) {
  271. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  272. } else if (lines[i].find("$signature") != -1) {
  273. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  274. } else if (lines[i].find("$copyright") != -1) {
  275. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  276. } else if (lines[i].find("$highres") != -1) {
  277. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  278. } else {
  279. strnew += lines[i] + "\n";
  280. }
  281. }
  282. CharString cs = strnew.utf8();
  283. plist.resize(cs.size() - 1);
  284. for (int i = 0; i < cs.size() - 1; i++) {
  285. plist.write[i] = cs[i];
  286. }
  287. }
  288. /**
  289. If we're running the OSX version of the Godot editor we'll:
  290. - export our application bundle to a temporary folder
  291. - attempt to code sign it
  292. - and then wrap it up in a DMG
  293. **/
  294. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  295. List<String> args;
  296. if (p_preset->get("codesign/entitlements") != "") {
  297. /* 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 */
  298. args.push_back("-entitlements");
  299. args.push_back(p_preset->get("codesign/entitlements"));
  300. }
  301. args.push_back("-s");
  302. args.push_back(p_preset->get("codesign/identity"));
  303. args.push_back("-v"); /* provide some more feedback */
  304. args.push_back(p_path);
  305. String str;
  306. Error err = OS::get_singleton()->execute("codesign", args, true, NULL, &str, NULL, true);
  307. ERR_FAIL_COND_V(err != OK, err);
  308. print_line("codesign: " + str);
  309. if (str.find("no identity found") != -1) {
  310. EditorNode::add_io_error("codesign: no identity found");
  311. return FAILED;
  312. }
  313. return OK;
  314. }
  315. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  316. List<String> args;
  317. OS::get_singleton()->move_to_trash(p_dmg_path);
  318. args.push_back("create");
  319. args.push_back(p_dmg_path);
  320. args.push_back("-volname");
  321. args.push_back(p_pkg_name);
  322. args.push_back("-fs");
  323. args.push_back("HFS+");
  324. args.push_back("-srcfolder");
  325. args.push_back(p_app_path_name);
  326. String str;
  327. Error err = OS::get_singleton()->execute("hdiutil", args, true, NULL, &str, NULL, true);
  328. ERR_FAIL_COND_V(err != OK, err);
  329. print_line("hdiutil returned: " + str);
  330. if (str.find("create failed") != -1) {
  331. if (str.find("File exists") != -1) {
  332. EditorNode::add_io_error("hdiutil: create failed - file exists");
  333. } else {
  334. EditorNode::add_io_error("hdiutil: create failed");
  335. }
  336. return FAILED;
  337. }
  338. return OK;
  339. }
  340. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  341. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  342. String src_pkg_name;
  343. EditorProgress ep("export", "Exporting for OSX", 3);
  344. if (p_debug)
  345. src_pkg_name = p_preset->get("custom_package/debug");
  346. else
  347. src_pkg_name = p_preset->get("custom_package/release");
  348. if (src_pkg_name == "") {
  349. String err;
  350. src_pkg_name = find_export_template("osx.zip", &err);
  351. if (src_pkg_name == "") {
  352. EditorNode::add_io_error(err);
  353. return ERR_FILE_NOT_FOUND;
  354. }
  355. }
  356. FileAccess *src_f = NULL;
  357. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  358. ep.step("Creating app", 0);
  359. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  360. if (!src_pkg_zip) {
  361. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  362. return ERR_FILE_NOT_FOUND;
  363. }
  364. ERR_FAIL_COND_V(!src_pkg_zip, ERR_CANT_OPEN);
  365. int ret = unzGoToFirstFile(src_pkg_zip);
  366. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  367. String pkg_name;
  368. if (p_preset->get("application/name") != "")
  369. pkg_name = p_preset->get("application/name"); // app_name
  370. else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "")
  371. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  372. else
  373. pkg_name = "Unnamed";
  374. Error err = OK;
  375. String tmp_app_path_name = "";
  376. zlib_filefunc_def io2 = io;
  377. FileAccess *dst_f = NULL;
  378. io2.opaque = &dst_f;
  379. zipFile dst_pkg_zip = NULL;
  380. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  381. if (export_format == "dmg") {
  382. // We're on OSX so we can export to DMG, but first we create our application bundle
  383. tmp_app_path_name = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".app");
  384. print_line("Exporting to " + tmp_app_path_name);
  385. DirAccess *tmp_app_path = DirAccess::create_for_path(tmp_app_path_name);
  386. if (!tmp_app_path) {
  387. err = ERR_CANT_CREATE;
  388. }
  389. // Create our folder structure or rely on unzip?
  390. if (err == OK) {
  391. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  392. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  393. }
  394. if (err == OK) {
  395. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  396. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  397. }
  398. if (err == OK) {
  399. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  400. err = tmp_app_path->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  401. }
  402. } else {
  403. // Open our destination zip file
  404. dst_pkg_zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  405. if (!dst_pkg_zip) {
  406. err = ERR_CANT_CREATE;
  407. }
  408. }
  409. // Now process our template
  410. bool found_binary = false;
  411. int total_size = 0;
  412. while (ret == UNZ_OK && err == OK) {
  413. bool is_execute = false;
  414. //get filename
  415. unz_file_info info;
  416. char fname[16384];
  417. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  418. String file = fname;
  419. Vector<uint8_t> data;
  420. data.resize(info.uncompressed_size);
  421. //read
  422. unzOpenCurrentFile(src_pkg_zip);
  423. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  424. unzCloseCurrentFile(src_pkg_zip);
  425. //write
  426. file = file.replace_first("osx_template.app/", "");
  427. if (file == "Contents/Info.plist") {
  428. _fix_plist(p_preset, data, pkg_name);
  429. }
  430. if (file.begins_with("Contents/MacOS/godot_")) {
  431. if (file != "Contents/MacOS/" + binary_to_use) {
  432. ret = unzGoToNextFile(src_pkg_zip);
  433. continue; //ignore!
  434. }
  435. found_binary = true;
  436. is_execute = true;
  437. file = "Contents/MacOS/" + pkg_name;
  438. }
  439. if (file == "Contents/Resources/icon.icns") {
  440. //see if there is an icon
  441. String iconpath;
  442. if (p_preset->get("application/icon") != "")
  443. iconpath = p_preset->get("application/icon");
  444. else
  445. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  446. if (iconpath != "") {
  447. Ref<Image> icon;
  448. icon.instance();
  449. icon->load(iconpath);
  450. if (!icon->empty()) {
  451. _make_icon(icon, data);
  452. }
  453. }
  454. //bleh?
  455. }
  456. if (data.size() > 0) {
  457. print_line("ADDING: " + file + " size: " + itos(data.size()));
  458. total_size += data.size();
  459. if (export_format == "dmg") {
  460. // write it into our application bundle
  461. file = tmp_app_path_name + "/" + file;
  462. // write the file, need to add chmod
  463. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  464. if (f) {
  465. f->store_buffer(data.ptr(), data.size());
  466. f->close();
  467. if (is_execute) {
  468. // Chmod with 0755 if the file is executable
  469. f->_chmod(file, 0755);
  470. }
  471. memdelete(f);
  472. } else {
  473. err = ERR_CANT_CREATE;
  474. }
  475. } else {
  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. 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. zipWriteInFileInZip(dst_pkg_zip, data.ptr(), data.size());
  499. zipCloseFileInZip(dst_pkg_zip);
  500. }
  501. }
  502. ret = unzGoToNextFile(src_pkg_zip);
  503. }
  504. // we're done with our source zip
  505. unzClose(src_pkg_zip);
  506. if (!found_binary) {
  507. ERR_PRINTS("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  508. err = ERR_FILE_NOT_FOUND;
  509. }
  510. if (err == OK) {
  511. ep.step("Making PKG", 1);
  512. if (export_format == "dmg") {
  513. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  514. Vector<SharedObject> shared_objects;
  515. err = save_pack(p_preset, pack_path, &shared_objects);
  516. // see if we can code sign our new package
  517. String identity = p_preset->get("codesign/identity");
  518. if (err == OK) {
  519. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  520. for (int i = 0; i < shared_objects.size(); i++) {
  521. err = da->copy(shared_objects[i].path, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  522. if (err == OK && identity != "") {
  523. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + shared_objects[i].path.get_file());
  524. }
  525. }
  526. memdelete(da);
  527. }
  528. if (err == OK && identity != "") {
  529. ep.step("Code signing bundle", 2);
  530. // 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
  531. // start with our application
  532. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name);
  533. ///@TODO we should check the contents of /Contents/Frameworks for frameworks to sign
  534. }
  535. if (err == OK && identity != "") {
  536. // we should probably loop through all resources and sign them?
  537. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Resources/icon.icns");
  538. }
  539. if (err == OK && identity != "") {
  540. err = _code_sign(p_preset, pack_path);
  541. }
  542. if (err == OK && identity != "") {
  543. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Info.plist");
  544. }
  545. // and finally create a DMG
  546. if (err == OK) {
  547. ep.step("Making DMG", 3);
  548. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  549. }
  550. // Clean up temporary .app dir
  551. OS::get_singleton()->move_to_trash(tmp_app_path_name);
  552. } else {
  553. String pack_path = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".pck");
  554. Vector<SharedObject> shared_objects;
  555. err = save_pack(p_preset, pack_path, &shared_objects);
  556. if (err == OK) {
  557. zipOpenNewFileInZip(dst_pkg_zip,
  558. (pkg_name + ".app/Contents/Resources/" + pkg_name + ".pck").utf8().get_data(),
  559. NULL,
  560. NULL,
  561. 0,
  562. NULL,
  563. 0,
  564. NULL,
  565. Z_DEFLATED,
  566. Z_DEFAULT_COMPRESSION);
  567. FileAccess *pf = FileAccess::open(pack_path, FileAccess::READ);
  568. if (pf) {
  569. const int BSIZE = 16384;
  570. uint8_t buf[BSIZE];
  571. while (true) {
  572. int r = pf->get_buffer(buf, BSIZE);
  573. if (r <= 0)
  574. break;
  575. zipWriteInFileInZip(dst_pkg_zip, buf, r);
  576. }
  577. zipCloseFileInZip(dst_pkg_zip);
  578. memdelete(pf);
  579. } else {
  580. err = ERR_CANT_OPEN;
  581. }
  582. }
  583. if (err == OK) {
  584. //add shared objects
  585. for (int i = 0; i < shared_objects.size(); i++) {
  586. Vector<uint8_t> file = FileAccess::get_file_as_array(shared_objects[i].path);
  587. ERR_CONTINUE(file.empty());
  588. zipOpenNewFileInZip(dst_pkg_zip,
  589. (pkg_name + ".app/Contents/Frameworks/").plus_file(shared_objects[i].path.get_file()).utf8().get_data(),
  590. NULL,
  591. NULL,
  592. 0,
  593. NULL,
  594. 0,
  595. NULL,
  596. Z_DEFLATED,
  597. Z_DEFAULT_COMPRESSION);
  598. zipWriteInFileInZip(dst_pkg_zip, file.ptr(), file.size());
  599. zipCloseFileInZip(dst_pkg_zip);
  600. }
  601. }
  602. }
  603. }
  604. if (dst_pkg_zip) {
  605. zipClose(dst_pkg_zip, NULL);
  606. }
  607. return err;
  608. }
  609. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  610. bool valid = false;
  611. String err;
  612. if (exists_export_template("osx.zip", &err)) {
  613. valid = true;
  614. }
  615. if (p_preset->get("custom_package/debug") != "") {
  616. if (FileAccess::exists(p_preset->get("custom_package/debug"))) {
  617. valid = true;
  618. } else {
  619. err += "Custom debug package not found.\n";
  620. }
  621. }
  622. if (p_preset->get("custom_package/release") != "") {
  623. if (FileAccess::exists(p_preset->get("custom_package/release"))) {
  624. valid = true;
  625. } else {
  626. err += "Custom release package not found.\n";
  627. }
  628. }
  629. if (!err.empty())
  630. r_error = err;
  631. r_missing_templates = !valid;
  632. return valid;
  633. }
  634. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  635. Ref<Image> img = memnew(Image(_osx_logo));
  636. logo.instance();
  637. logo->create_from_image(img);
  638. }
  639. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  640. }
  641. void register_osx_exporter() {
  642. Ref<EditorExportPlatformOSX> platform;
  643. platform.instance();
  644. EditorExport::get_singleton()->add_export_platform(platform);
  645. }