export.cpp 26 KB

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