export.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export.h"
  31. #include "editor/editor_export.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "global_config.h"
  35. #include "io/marshalls.h"
  36. #include "io/resource_saver.h"
  37. #include "io/zip_io.h"
  38. #include "os/file_access.h"
  39. #include "os/os.h"
  40. #include "platform/osx/logo.h"
  41. #include "string.h"
  42. #include "version.h"
  43. class EditorExportPlatformOSX : public EditorExportPlatform {
  44. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  45. int version_code;
  46. Ref<ImageTexture> logo;
  47. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  48. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  49. protected:
  50. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  51. virtual void get_export_options(List<ExportOption> *r_options);
  52. public:
  53. virtual String get_name() const { return "Mac OSX"; }
  54. virtual Ref<Texture> get_logo() const { return logo; }
  55. virtual String get_binary_extension() const { return "zip"; }
  56. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  57. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  58. EditorExportPlatformOSX();
  59. ~EditorExportPlatformOSX();
  60. };
  61. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  62. // what does this need to do?
  63. }
  64. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  65. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  66. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  67. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name"), ""));
  68. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  69. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "png"), ""));
  70. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/identifier"), "org.godotengine.macgame"));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), "godotmacgame"));
  72. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  73. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  75. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/bits_mode", PROPERTY_HINT_ENUM, "Fat (32 & 64 bits),64 bits,32 bits"), 0));
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  77. }
  78. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  79. Ref<ImageTexture> it = memnew(ImageTexture);
  80. int size = 512;
  81. Vector<uint8_t> data;
  82. data.resize(8);
  83. data[0] = 'i';
  84. data[1] = 'c';
  85. data[2] = 'n';
  86. data[3] = 's';
  87. const char *name[] = { "ic09", "ic08", "ic07", "icp6", "icp5", "icp4" };
  88. int index = 0;
  89. while (size >= 16) {
  90. 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?
  91. copy->convert(Image::FORMAT_RGBA8);
  92. copy->resize(size, size);
  93. it->create_from_image(copy);
  94. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/icon.png";
  95. ResourceSaver::save(path, it);
  96. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  97. ERR_FAIL_COND(!f);
  98. int ofs = data.size();
  99. uint32_t len = f->get_len();
  100. data.resize(data.size() + len + 8);
  101. f->get_buffer(&data[ofs + 8], len);
  102. memdelete(f);
  103. len += 8;
  104. len = BSWAP32(len);
  105. copymem(&data[ofs], name[index], 4);
  106. encode_uint32(len, &data[ofs + 4]);
  107. index++;
  108. size /= 2;
  109. }
  110. uint32_t total_len = data.size();
  111. total_len = BSWAP32(total_len);
  112. encode_uint32(total_len, &data[4]);
  113. p_data = data;
  114. }
  115. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  116. String str;
  117. String strnew;
  118. str.parse_utf8((const char *)plist.ptr(), plist.size());
  119. Vector<String> lines = str.split("\n");
  120. for (int i = 0; i < lines.size(); i++) {
  121. if (lines[i].find("$binary") != -1) {
  122. strnew += lines[i].replace("$binary", p_binary) + "\n";
  123. } else if (lines[i].find("$name") != -1) {
  124. strnew += lines[i].replace("$name", p_binary) + "\n";
  125. } else if (lines[i].find("$info") != -1) {
  126. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  127. } else if (lines[i].find("$identifier") != -1) {
  128. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  129. } else if (lines[i].find("$short_version") != -1) {
  130. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  131. } else if (lines[i].find("$version") != -1) {
  132. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  133. } else if (lines[i].find("$signature") != -1) {
  134. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  135. } else if (lines[i].find("$copyright") != -1) {
  136. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  137. } else if (lines[i].find("$highres") != -1) {
  138. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  139. } else {
  140. strnew += lines[i] + "\n";
  141. }
  142. }
  143. CharString cs = strnew.utf8();
  144. plist.resize(cs.size());
  145. for (int i = 9; i < cs.size(); i++) {
  146. plist[i] = cs[i];
  147. }
  148. }
  149. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  150. String src_pkg;
  151. EditorProgress ep("export", "Exporting for OSX", 104);
  152. if (p_debug)
  153. src_pkg = p_preset->get("custom_package/debug");
  154. else
  155. src_pkg = p_preset->get("custom_package/release");
  156. if (src_pkg == "") {
  157. String err;
  158. src_pkg = find_export_template("osx.zip", &err);
  159. if (src_pkg == "") {
  160. EditorNode::add_io_error(err);
  161. return ERR_FILE_NOT_FOUND;
  162. }
  163. }
  164. FileAccess *src_f = NULL;
  165. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  166. ep.step("Creating app", 0);
  167. unzFile pkg = unzOpen2(src_pkg.utf8().get_data(), &io);
  168. if (!pkg) {
  169. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg);
  170. return ERR_FILE_NOT_FOUND;
  171. }
  172. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  173. int ret = unzGoToFirstFile(pkg);
  174. zlib_filefunc_def io2 = io;
  175. FileAccess *dst_f = NULL;
  176. io2.opaque = &dst_f;
  177. zipFile dpkg = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  178. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".";
  179. int bits_mode = p_preset->get("application/bits_mode");
  180. binary_to_use += String(bits_mode == 0 ? "fat" : bits_mode == 1 ? "64" : "32");
  181. print_line("binary: " + binary_to_use);
  182. String pkg_name;
  183. if (p_preset->get("application/name") != "")
  184. pkg_name = p_preset->get("application/name"); // app_name
  185. else if (String(GlobalConfig::get_singleton()->get("application/name")) != "")
  186. pkg_name = String(GlobalConfig::get_singleton()->get("application/name"));
  187. else
  188. pkg_name = "Unnamed";
  189. bool found_binary = false;
  190. while (ret == UNZ_OK) {
  191. //get filename
  192. unz_file_info info;
  193. char fname[16384];
  194. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  195. String file = fname;
  196. print_line("READ: " + file);
  197. Vector<uint8_t> data;
  198. data.resize(info.uncompressed_size);
  199. //read
  200. unzOpenCurrentFile(pkg);
  201. unzReadCurrentFile(pkg, data.ptr(), data.size());
  202. unzCloseCurrentFile(pkg);
  203. //write
  204. file = file.replace_first("osx_template.app/", "");
  205. if (file == "Contents/Info.plist") {
  206. print_line("parse plist");
  207. _fix_plist(p_preset, data, pkg_name);
  208. }
  209. if (file.begins_with("Contents/MacOS/godot_")) {
  210. if (file != "Contents/MacOS/" + binary_to_use) {
  211. ret = unzGoToNextFile(pkg);
  212. continue; //ignore!
  213. }
  214. found_binary = true;
  215. file = "Contents/MacOS/" + pkg_name;
  216. }
  217. if (file == "Contents/Resources/icon.icns") {
  218. //see if there is an icon
  219. String iconpath;
  220. if (p_preset->get("application/icon") != "")
  221. iconpath = p_preset->get("application/icon");
  222. else
  223. iconpath = GlobalConfig::get_singleton()->get("application/icon");
  224. print_line("icon? " + iconpath);
  225. if (iconpath != "") {
  226. Ref<Image> icon;
  227. icon.instance();
  228. icon->load(iconpath);
  229. if (!icon->empty()) {
  230. print_line("loaded?");
  231. _make_icon(icon, data);
  232. }
  233. }
  234. //bleh?
  235. }
  236. file = pkg_name + ".app/" + file;
  237. if (data.size() > 0) {
  238. print_line("ADDING: " + file + " size: " + itos(data.size()));
  239. zip_fileinfo fi;
  240. fi.tmz_date.tm_hour = info.tmu_date.tm_hour;
  241. fi.tmz_date.tm_min = info.tmu_date.tm_min;
  242. fi.tmz_date.tm_sec = info.tmu_date.tm_sec;
  243. fi.tmz_date.tm_mon = info.tmu_date.tm_mon;
  244. fi.tmz_date.tm_mday = info.tmu_date.tm_mday;
  245. fi.tmz_date.tm_year = info.tmu_date.tm_year;
  246. fi.dosDate = info.dosDate;
  247. fi.internal_fa = info.internal_fa;
  248. fi.external_fa = info.external_fa;
  249. int err = zipOpenNewFileInZip(dpkg,
  250. file.utf8().get_data(),
  251. &fi,
  252. NULL,
  253. 0,
  254. NULL,
  255. 0,
  256. NULL,
  257. Z_DEFLATED,
  258. Z_DEFAULT_COMPRESSION);
  259. print_line("OPEN ERR: " + itos(err));
  260. err = zipWriteInFileInZip(dpkg, data.ptr(), data.size());
  261. print_line("WRITE ERR: " + itos(err));
  262. zipCloseFileInZip(dpkg);
  263. }
  264. ret = unzGoToNextFile(pkg);
  265. }
  266. if (!found_binary) {
  267. ERR_PRINTS("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  268. zipClose(dpkg, NULL);
  269. unzClose(pkg);
  270. return ERR_FILE_NOT_FOUND;
  271. }
  272. ep.step("Making PKG", 1);
  273. String pack_path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/data.pck";
  274. Error err = save_pack(p_preset, pack_path);
  275. if (err) {
  276. zipClose(dpkg, NULL);
  277. unzClose(pkg);
  278. return err;
  279. }
  280. {
  281. //write datapack
  282. zipOpenNewFileInZip(dpkg,
  283. (pkg_name + ".app/Contents/Resources/data.pck").utf8().get_data(),
  284. NULL,
  285. NULL,
  286. 0,
  287. NULL,
  288. 0,
  289. NULL,
  290. Z_DEFLATED,
  291. Z_DEFAULT_COMPRESSION);
  292. FileAccess *pf = FileAccess::open(pack_path, FileAccess::READ);
  293. ERR_FAIL_COND_V(!pf, ERR_CANT_OPEN);
  294. const int BSIZE = 16384;
  295. uint8_t buf[BSIZE];
  296. while (true) {
  297. int r = pf->get_buffer(buf, BSIZE);
  298. if (r <= 0)
  299. break;
  300. zipWriteInFileInZip(dpkg, buf, r);
  301. }
  302. zipCloseFileInZip(dpkg);
  303. memdelete(pf);
  304. }
  305. zipClose(dpkg, NULL);
  306. unzClose(pkg);
  307. return OK;
  308. }
  309. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  310. bool valid = true;
  311. String err;
  312. if (!exists_export_template("osx.zip", &err)) {
  313. valid = false;
  314. }
  315. if (p_preset->get("custom_package/debug") != "" && !FileAccess::exists(p_preset->get("custom_package/debug"))) {
  316. valid = false;
  317. err += "Custom debug package not found.\n";
  318. }
  319. if (p_preset->get("custom_package/release") != "" && !FileAccess::exists(p_preset->get("custom_package/release"))) {
  320. valid = false;
  321. err += "Custom release package not found.\n";
  322. }
  323. if (!err.empty())
  324. r_error = err;
  325. return valid;
  326. }
  327. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  328. Ref<Image> img = memnew(Image(_osx_logo));
  329. logo.instance();
  330. logo->create_from_image(img);
  331. }
  332. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  333. }
  334. void register_osx_exporter() {
  335. Ref<EditorExportPlatformOSX> platform;
  336. platform.instance();
  337. EditorExport::get_singleton()->add_export_platform(platform);
  338. }