export.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.gen.h"
  41. #include "string.h"
  42. #include "version.h"
  43. #include <sys/stat.h>
  44. class EditorExportPlatformIOS : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformIOS, EditorExportPlatform);
  46. int version_code;
  47. Ref<ImageTexture> logo;
  48. void _fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const String &p_name, const String &p_binary);
  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 "iOS"; }
  54. virtual Ref<Texture> get_logo() const { return logo; }
  55. virtual String get_binary_extension() const { return "xcodeproj"; }
  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. EditorExportPlatformIOS();
  59. ~EditorExportPlatformIOS();
  60. };
  61. void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  62. // what does this need to do?
  63. }
  64. void EditorExportPlatformIOS::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.iosgame"));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), "godotiosgame"));
  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"), 1));
  76. /* probably need some more info */
  77. }
  78. void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const String &p_name, const String &p_binary) {
  79. String str;
  80. String strnew;
  81. str.parse_utf8((const char *)pfile.ptr(), pfile.size());
  82. Vector<String> lines = str.split("\n");
  83. for (int i = 0; i < lines.size(); i++) {
  84. if (lines[i].find("$binary") != -1) {
  85. strnew += lines[i].replace("$binary", p_binary) + "\n";
  86. } else if (lines[i].find("$name") != -1) {
  87. strnew += lines[i].replace("$name", p_name) + "\n";
  88. } else if (lines[i].find("$info") != -1) {
  89. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  90. } else if (lines[i].find("$identifier") != -1) {
  91. strnew += lines[i].replace("$identifier", p_preset->get("application/identifier")) + "\n";
  92. } else if (lines[i].find("$short_version") != -1) {
  93. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  94. } else if (lines[i].find("$version") != -1) {
  95. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  96. } else if (lines[i].find("$signature") != -1) {
  97. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  98. } else if (lines[i].find("$copyright") != -1) {
  99. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  100. } else {
  101. strnew += lines[i] + "\n";
  102. }
  103. }
  104. // !BAS! I'm assuming the 9 in the original code was a typo. I've added -1 or else it seems to also be adding our terminating zero...
  105. // should apply the same fix in our OSX export.
  106. CharString cs = strnew.utf8();
  107. pfile.resize(cs.size() - 1);
  108. for (int i = 0; i < cs.size() - 1; i++) {
  109. pfile[i] = cs[i];
  110. }
  111. }
  112. Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  113. String src_pkg_name;
  114. String dest_dir = p_path.get_base_dir() + "/";
  115. String binary_name = p_path.get_file().get_basename();
  116. EditorProgress ep("export", "Exporting for iOS", 3);
  117. if (p_debug)
  118. src_pkg_name = p_preset->get("custom_package/debug");
  119. else
  120. src_pkg_name = p_preset->get("custom_package/release");
  121. if (src_pkg_name == "") {
  122. String err;
  123. src_pkg_name = find_export_template("iphone.zip", &err);
  124. if (src_pkg_name == "") {
  125. EditorNode::add_io_error(err);
  126. return ERR_FILE_NOT_FOUND;
  127. }
  128. }
  129. FileAccess *src_f = NULL;
  130. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  131. ep.step("Creating app", 0);
  132. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  133. if (!src_pkg_zip) {
  134. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  135. return ERR_FILE_NOT_FOUND;
  136. }
  137. ERR_FAIL_COND_V(!src_pkg_zip, ERR_CANT_OPEN);
  138. int ret = unzGoToFirstFile(src_pkg_zip);
  139. String binary_to_use = "godot.iphone." + String(p_debug ? "debug" : "release") + ".";
  140. int bits_mode = p_preset->get("application/bits_mode");
  141. binary_to_use += String(bits_mode == 0 ? "fat" : bits_mode == 1 ? "arm64" : "armv7");
  142. print_line("binary: " + binary_to_use);
  143. String pkg_name;
  144. if (p_preset->get("application/name") != "")
  145. pkg_name = p_preset->get("application/name"); // app_name
  146. else if (String(GlobalConfig::get_singleton()->get("application/config/name")) != "")
  147. pkg_name = String(GlobalConfig::get_singleton()->get("application/config/name"));
  148. else
  149. pkg_name = "Unnamed";
  150. DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir);
  151. ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE)
  152. /* Now process our template */
  153. bool found_binary = false;
  154. int total_size = 0;
  155. while (ret == UNZ_OK) {
  156. bool is_execute = false;
  157. //get filename
  158. unz_file_info info;
  159. char fname[16384];
  160. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, NULL, 0, NULL, 0);
  161. String file = fname;
  162. print_line("READ: " + file);
  163. Vector<uint8_t> data;
  164. data.resize(info.uncompressed_size);
  165. //read
  166. unzOpenCurrentFile(src_pkg_zip);
  167. unzReadCurrentFile(src_pkg_zip, data.ptr(), data.size());
  168. unzCloseCurrentFile(src_pkg_zip);
  169. //write
  170. file = file.replace_first("iphone/", "");
  171. if (file == "godot_ios.xcodeproj/project.pbxproj") {
  172. print_line("parse pbxproj");
  173. _fix_config_file(p_preset, data, pkg_name, binary_name);
  174. } else if (file == "godot_ios/godot_ios-Info.plist") {
  175. print_line("parse plist");
  176. _fix_config_file(p_preset, data, pkg_name, binary_name);
  177. } else if (file.begins_with("godot.iphone")) {
  178. if (file != binary_to_use) {
  179. ret = unzGoToNextFile(src_pkg_zip);
  180. continue; //ignore!
  181. }
  182. found_binary = true;
  183. is_execute = true;
  184. file = "godot_ios.iphone";
  185. }
  186. ///@TODO need to parse logo files
  187. if (data.size() > 0) {
  188. file = file.replace("godot_ios", binary_name);
  189. print_line("ADDING: " + file + " size: " + itos(data.size()));
  190. total_size += data.size();
  191. /* write it into our folder structure */
  192. file = dest_dir + file;
  193. /* make sure this folder exists */
  194. String dir_name = file.get_base_dir();
  195. if (!tmp_app_path->dir_exists(dir_name)) {
  196. print_line("Creating " + dir_name);
  197. Error dir_err = tmp_app_path->make_dir_recursive(dir_name);
  198. if (dir_err) {
  199. ERR_PRINTS("Can't create '" + dir_name + "'.");
  200. unzClose(src_pkg_zip);
  201. return ERR_CANT_CREATE;
  202. }
  203. }
  204. /* write the file */
  205. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  206. if (!f) {
  207. ERR_PRINTS("Can't write '" + file + "'.");
  208. unzClose(src_pkg_zip);
  209. return ERR_CANT_CREATE;
  210. };
  211. f->store_buffer(data.ptr(), data.size());
  212. f->close();
  213. memdelete(f);
  214. #ifdef OSX_ENABLED
  215. if (is_execute) {
  216. // we need execute rights on this file
  217. chmod(file.utf8().get_data(), 0755);
  218. }
  219. #endif
  220. }
  221. ret = unzGoToNextFile(src_pkg_zip);
  222. }
  223. /* we're done with our source zip */
  224. unzClose(src_pkg_zip);
  225. if (!found_binary) {
  226. ERR_PRINTS("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  227. unzClose(src_pkg_zip);
  228. return ERR_FILE_NOT_FOUND;
  229. }
  230. ep.step("Making PKG", 1);
  231. String pack_path = dest_dir + binary_name + ".pck";
  232. Error err = save_pack(p_preset, pack_path);
  233. if (err) {
  234. return err;
  235. }
  236. #ifdef OSX_ENABLED
  237. /* and open up xcode with our new project.... */
  238. List<String> args;
  239. args.push_back(p_path);
  240. err = OS::get_singleton()->execute("/usr/bin/open", args, false);
  241. ERR_FAIL_COND_V(err, err);
  242. #endif
  243. return OK;
  244. }
  245. bool EditorExportPlatformIOS::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  246. bool valid = true;
  247. String err;
  248. if (!exists_export_template("iphone.zip", &err)) {
  249. valid = false;
  250. }
  251. if (p_preset->get("custom_package/debug") != "" && !FileAccess::exists(p_preset->get("custom_package/debug"))) {
  252. valid = false;
  253. err += "Custom debug package not found.\n";
  254. }
  255. if (p_preset->get("custom_package/release") != "" && !FileAccess::exists(p_preset->get("custom_package/release"))) {
  256. valid = false;
  257. err += "Custom release package not found.\n";
  258. }
  259. if (!err.empty())
  260. r_error = err;
  261. return valid;
  262. }
  263. EditorExportPlatformIOS::EditorExportPlatformIOS() {
  264. ///@TODO need to create the correct logo
  265. // Ref<Image> img = memnew(Image(_iphone_logo));
  266. Ref<Image> img = memnew(Image(_osx_logo));
  267. logo.instance();
  268. logo->create_from_image(img);
  269. }
  270. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  271. }
  272. void register_iphone_exporter() {
  273. Ref<EditorExportPlatformIOS> platform;
  274. platform.instance();
  275. EditorExport::get_singleton()->add_export_platform(platform);
  276. }