export.cpp 26 KB

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