export.cpp 28 KB

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