export.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/io/zip_io.h"
  37. #include "core/os/os.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 <sys/stat.h>
  44. class EditorExportPlatformOSX : public EditorExportPlatform {
  45. GDCLASS(EditorExportPlatformOSX, EditorExportPlatform);
  46. int version_code = 0;
  47. Ref<ImageTexture> logo;
  48. void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary);
  49. void _make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
  50. Error _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  51. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path);
  52. Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
  53. void _zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name);
  54. #ifdef OSX_ENABLED
  55. bool use_codesign() const { return true; }
  56. bool use_dmg() const { return true; }
  57. #else
  58. bool use_codesign() const { return false; }
  59. bool use_dmg() const { return false; }
  60. #endif
  61. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  62. String pname = p_package;
  63. if (pname.length() == 0) {
  64. if (r_error) {
  65. *r_error = TTR("Identifier is missing.");
  66. }
  67. return false;
  68. }
  69. for (int i = 0; i < pname.length(); i++) {
  70. char32_t c = pname[i];
  71. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.')) {
  72. if (r_error) {
  73. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  74. }
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. protected:
  81. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) override;
  82. virtual void get_export_options(List<ExportOption> *r_options) override;
  83. public:
  84. virtual String get_name() const override { return "macOS"; }
  85. virtual String get_os_name() const override { return "macOS"; }
  86. virtual Ref<Texture2D> get_logo() const override { return logo; }
  87. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override {
  88. List<String> list;
  89. if (use_dmg()) {
  90. list.push_back("dmg");
  91. }
  92. list.push_back("zip");
  93. return list;
  94. }
  95. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  96. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override;
  97. virtual void get_platform_features(List<String> *r_features) override {
  98. r_features->push_back("pc");
  99. r_features->push_back("s3tc");
  100. r_features->push_back("macOS");
  101. }
  102. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) override {
  103. }
  104. EditorExportPlatformOSX();
  105. ~EditorExportPlatformOSX();
  106. };
  107. void EditorExportPlatformOSX::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  108. if (p_preset->get("texture_format/s3tc")) {
  109. r_features->push_back("s3tc");
  110. }
  111. if (p_preset->get("texture_format/etc")) {
  112. r_features->push_back("etc");
  113. }
  114. if (p_preset->get("texture_format/etc2")) {
  115. r_features->push_back("etc2");
  116. }
  117. r_features->push_back("64");
  118. }
  119. void EditorExportPlatformOSX::get_export_options(List<ExportOption> *r_options) {
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/info"), "Made with Godot Engine"));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.png,*.icns"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/bundle_identifier", PROPERTY_HINT_PLACEHOLDER_TEXT, "com.example.game"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/signature"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/app_category", PROPERTY_HINT_ENUM, "Business,Developer-tools,Education,Entertainment,Finance,Games,Action-games,Adventure-games,Arcade-games,Board-games,Card-games,Casino-games,Dice-games,Educational-games,Family-games,Kids-games,Music-games,Puzzle-games,Racing-games,Role-playing-games,Simulation-games,Sports-games,Strategy-games,Trivia-games,Word-games,Graphics-design,Healthcare-fitness,Lifestyle,Medical,Music,News,Photography,Productivity,Reference,Social-networking,Sports,Travel,Utilities,Video,Weather"), "Games"));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/short_version"), "1.0"));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0"));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "display/high_res"), false));
  132. 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"), ""));
  133. 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"), ""));
  134. #ifdef OSX_ENABLED
  135. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), true));
  136. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_PLACEHOLDER_TEXT, "Type: Name (ID)"), ""));
  137. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  138. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/hardened_runtime"), true));
  139. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/replace_existing_signature"), true));
  140. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/entitlements/custom_file", PROPERTY_HINT_GLOBAL_FILE, "*.plist"), ""));
  141. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_jit_code_execution"), false));
  142. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_unsigned_executable_memory"), false));
  143. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/allow_dyld_environment_variables"), false));
  144. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/disable_library_validation"), false));
  145. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/audio_input"), false));
  146. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/camera"), false));
  147. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/location"), false));
  148. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/address_book"), false));
  149. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/calendars"), false));
  150. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/photos_library"), false));
  151. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/apple_events"), false));
  152. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/enabled"), false));
  153. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_server"), false));
  154. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/network_client"), false));
  155. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_usb"), false));
  156. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/entitlements/app_sandbox/device_bluetooth"), false));
  157. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_downloads", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  158. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_pictures", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  159. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_music", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  160. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/entitlements/app_sandbox/files_movies", PROPERTY_HINT_ENUM, "No,Read-only,Read-write"), 0));
  161. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  162. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "notarization/enable"), false));
  163. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Apple ID email"), ""));
  164. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_id_password", PROPERTY_HINT_PLACEHOLDER_TEXT, "Enable two-factor authentication and provide app-specific password"), ""));
  165. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "notarization/apple_team_id", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide team ID if your Apple ID belongs to multiple teams"), ""));
  166. #endif
  167. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  168. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  169. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  170. }
  171. void _rgba8_to_packbits_encode(int p_ch, int p_size, Vector<uint8_t> &p_source, Vector<uint8_t> &p_dest) {
  172. int src_len = p_size * p_size;
  173. Vector<uint8_t> result;
  174. result.resize(src_len * 1.25); //temp vector for rle encoded data, make it 25% larger for worst case scenario
  175. int res_size = 0;
  176. uint8_t buf[128];
  177. int buf_size = 0;
  178. int i = 0;
  179. while (i < src_len) {
  180. uint8_t cur = p_source.ptr()[i * 4 + p_ch];
  181. if (i < src_len - 2) {
  182. if ((p_source.ptr()[(i + 1) * 4 + p_ch] == cur) && (p_source.ptr()[(i + 2) * 4 + p_ch] == cur)) {
  183. if (buf_size > 0) {
  184. result.write[res_size++] = (uint8_t)(buf_size - 1);
  185. memcpy(&result.write[res_size], &buf, buf_size);
  186. res_size += buf_size;
  187. buf_size = 0;
  188. }
  189. uint8_t lim = i + 130 >= src_len ? src_len - i - 1 : 130;
  190. bool hit_lim = true;
  191. for (int j = 3; j <= lim; j++) {
  192. if (p_source.ptr()[(i + j) * 4 + p_ch] != cur) {
  193. hit_lim = false;
  194. i = i + j - 1;
  195. result.write[res_size++] = (uint8_t)(j - 3 + 0x80);
  196. result.write[res_size++] = cur;
  197. break;
  198. }
  199. }
  200. if (hit_lim) {
  201. result.write[res_size++] = (uint8_t)(lim - 3 + 0x80);
  202. result.write[res_size++] = cur;
  203. i = i + lim;
  204. }
  205. } else {
  206. buf[buf_size++] = cur;
  207. if (buf_size == 128) {
  208. result.write[res_size++] = (uint8_t)(buf_size - 1);
  209. memcpy(&result.write[res_size], &buf, buf_size);
  210. res_size += buf_size;
  211. buf_size = 0;
  212. }
  213. }
  214. } else {
  215. buf[buf_size++] = cur;
  216. result.write[res_size++] = (uint8_t)(buf_size - 1);
  217. memcpy(&result.write[res_size], &buf, buf_size);
  218. res_size += buf_size;
  219. buf_size = 0;
  220. }
  221. i++;
  222. }
  223. int ofs = p_dest.size();
  224. p_dest.resize(p_dest.size() + res_size);
  225. memcpy(&p_dest.write[ofs], result.ptr(), res_size);
  226. }
  227. void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_t> &p_data) {
  228. Ref<ImageTexture> it = memnew(ImageTexture);
  229. Vector<uint8_t> data;
  230. data.resize(8);
  231. data.write[0] = 'i';
  232. data.write[1] = 'c';
  233. data.write[2] = 'n';
  234. data.write[3] = 's';
  235. struct MacOSIconInfo {
  236. const char *name;
  237. const char *mask_name;
  238. bool is_png;
  239. int size;
  240. };
  241. static const MacOSIconInfo icon_infos[] = {
  242. { "ic10", "", true, 1024 }, //1024x1024 32-bit PNG and 512x512@2x 32-bit "retina" PNG
  243. { "ic09", "", true, 512 }, //512×512 32-bit PNG
  244. { "ic14", "", true, 512 }, //256x256@2x 32-bit "retina" PNG
  245. { "ic08", "", true, 256 }, //256×256 32-bit PNG
  246. { "ic13", "", true, 256 }, //128x128@2x 32-bit "retina" PNG
  247. { "ic07", "", true, 128 }, //128x128 32-bit PNG
  248. { "ic12", "", true, 64 }, //32x32@2x 32-bit "retina" PNG
  249. { "ic11", "", true, 32 }, //16x16@2x 32-bit "retina" PNG
  250. { "il32", "l8mk", false, 32 }, //32x32 24-bit RLE + 8-bit uncompressed mask
  251. { "is32", "s8mk", false, 16 } //16x16 24-bit RLE + 8-bit uncompressed mask
  252. };
  253. for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) {
  254. 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?
  255. copy->convert(Image::FORMAT_RGBA8);
  256. copy->resize(icon_infos[i].size, icon_infos[i].size);
  257. if (icon_infos[i].is_png) {
  258. // Encode PNG icon.
  259. it->create_from_image(copy);
  260. String path = EditorPaths::get_singleton()->get_cache_dir().plus_file("icon.png");
  261. ResourceSaver::save(path, it);
  262. FileAccess *f = FileAccess::open(path, FileAccess::READ);
  263. if (!f) {
  264. // Clean up generated file.
  265. DirAccess::remove_file_or_error(path);
  266. ERR_FAIL();
  267. }
  268. int ofs = data.size();
  269. uint64_t len = f->get_length();
  270. data.resize(data.size() + len + 8);
  271. f->get_buffer(&data.write[ofs + 8], len);
  272. memdelete(f);
  273. len += 8;
  274. len = BSWAP32(len);
  275. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  276. encode_uint32(len, &data.write[ofs + 4]);
  277. // Clean up generated file.
  278. DirAccess::remove_file_or_error(path);
  279. } else {
  280. Vector<uint8_t> src_data = copy->get_data();
  281. //encode 24bit RGB RLE icon
  282. {
  283. int ofs = data.size();
  284. data.resize(data.size() + 8);
  285. _rgba8_to_packbits_encode(0, icon_infos[i].size, src_data, data); // encode R
  286. _rgba8_to_packbits_encode(1, icon_infos[i].size, src_data, data); // encode G
  287. _rgba8_to_packbits_encode(2, icon_infos[i].size, src_data, data); // encode B
  288. int len = data.size() - ofs;
  289. len = BSWAP32(len);
  290. memcpy(&data.write[ofs], icon_infos[i].name, 4);
  291. encode_uint32(len, &data.write[ofs + 4]);
  292. }
  293. //encode 8bit mask uncompressed icon
  294. {
  295. int ofs = data.size();
  296. int len = copy->get_width() * copy->get_height();
  297. data.resize(data.size() + len + 8);
  298. for (int j = 0; j < len; j++) {
  299. data.write[ofs + 8 + j] = src_data.ptr()[j * 4 + 3];
  300. }
  301. len += 8;
  302. len = BSWAP32(len);
  303. memcpy(&data.write[ofs], icon_infos[i].mask_name, 4);
  304. encode_uint32(len, &data.write[ofs + 4]);
  305. }
  306. }
  307. }
  308. uint32_t total_len = data.size();
  309. total_len = BSWAP32(total_len);
  310. encode_uint32(total_len, &data.write[4]);
  311. p_data = data;
  312. }
  313. void EditorExportPlatformOSX::_fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary) {
  314. String str;
  315. String strnew;
  316. str.parse_utf8((const char *)plist.ptr(), plist.size());
  317. Vector<String> lines = str.split("\n");
  318. for (int i = 0; i < lines.size(); i++) {
  319. if (lines[i].find("$binary") != -1) {
  320. strnew += lines[i].replace("$binary", p_binary) + "\n";
  321. } else if (lines[i].find("$name") != -1) {
  322. strnew += lines[i].replace("$name", p_binary) + "\n";
  323. } else if (lines[i].find("$info") != -1) {
  324. strnew += lines[i].replace("$info", p_preset->get("application/info")) + "\n";
  325. } else if (lines[i].find("$bundle_identifier") != -1) {
  326. strnew += lines[i].replace("$bundle_identifier", p_preset->get("application/bundle_identifier")) + "\n";
  327. } else if (lines[i].find("$short_version") != -1) {
  328. strnew += lines[i].replace("$short_version", p_preset->get("application/short_version")) + "\n";
  329. } else if (lines[i].find("$version") != -1) {
  330. strnew += lines[i].replace("$version", p_preset->get("application/version")) + "\n";
  331. } else if (lines[i].find("$signature") != -1) {
  332. strnew += lines[i].replace("$signature", p_preset->get("application/signature")) + "\n";
  333. } else if (lines[i].find("$app_category") != -1) {
  334. String cat = p_preset->get("application/app_category");
  335. strnew += lines[i].replace("$app_category", cat.to_lower()) + "\n";
  336. } else if (lines[i].find("$copyright") != -1) {
  337. strnew += lines[i].replace("$copyright", p_preset->get("application/copyright")) + "\n";
  338. } else if (lines[i].find("$highres") != -1) {
  339. strnew += lines[i].replace("$highres", p_preset->get("display/high_res") ? "<true/>" : "<false/>") + "\n";
  340. } else if (lines[i].find("$camera_usage_description") != -1) {
  341. String description = p_preset->get("privacy/camera_usage_description");
  342. strnew += lines[i].replace("$camera_usage_description", description) + "\n";
  343. } else if (lines[i].find("$microphone_usage_description") != -1) {
  344. String description = p_preset->get("privacy/microphone_usage_description");
  345. strnew += lines[i].replace("$microphone_usage_description", description) + "\n";
  346. } else {
  347. strnew += lines[i] + "\n";
  348. }
  349. }
  350. CharString cs = strnew.utf8();
  351. plist.resize(cs.size() - 1);
  352. for (int i = 0; i < cs.size() - 1; i++) {
  353. plist.write[i] = cs[i];
  354. }
  355. }
  356. /**
  357. If we're running the OSX version of the Godot editor we'll:
  358. - export our application bundle to a temporary folder
  359. - attempt to code sign it
  360. - and then wrap it up in a DMG
  361. **/
  362. Error EditorExportPlatformOSX::_notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  363. #ifdef OSX_ENABLED
  364. List<String> args;
  365. args.push_back("altool");
  366. args.push_back("--notarize-app");
  367. args.push_back("--primary-bundle-id");
  368. args.push_back(p_preset->get("application/bundle_identifier"));
  369. args.push_back("--username");
  370. args.push_back(p_preset->get("notarization/apple_id_name"));
  371. args.push_back("--password");
  372. args.push_back(p_preset->get("notarization/apple_id_password"));
  373. args.push_back("--type");
  374. args.push_back("osx");
  375. if (p_preset->get("notarization/apple_team_id")) {
  376. args.push_back("--asc-provider");
  377. args.push_back(p_preset->get("notarization/apple_team_id"));
  378. }
  379. args.push_back("--file");
  380. args.push_back(p_path);
  381. String str;
  382. Error err = OS::get_singleton()->execute("xcrun", args, &str, nullptr, true);
  383. ERR_FAIL_COND_V(err != OK, err);
  384. print_line("altool (" + p_path + "):\n" + str);
  385. if (str.find("RequestUUID") == -1) {
  386. EditorNode::add_io_error("altool: " + str);
  387. return FAILED;
  388. } else {
  389. print_line("Note: The notarization process generally takes less than an hour. When the process is completed, you'll receive an email.");
  390. print_line(" You can check progress manually by opening a Terminal and running the following command:");
  391. print_line(" \"xcrun altool --notarization-history 0 -u <your email> -p <app-specific pwd>\"");
  392. }
  393. #endif
  394. return OK;
  395. }
  396. Error EditorExportPlatformOSX::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path) {
  397. #ifdef OSX_ENABLED
  398. List<String> args;
  399. if (p_preset->get("codesign/timestamp")) {
  400. args.push_back("--timestamp");
  401. }
  402. if (p_preset->get("codesign/hardened_runtime")) {
  403. args.push_back("--options");
  404. args.push_back("runtime");
  405. }
  406. if (p_path.get_extension() != "dmg") {
  407. args.push_back("--entitlements");
  408. args.push_back(p_ent_path);
  409. }
  410. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  411. for (int i = 0; i < user_args.size(); i++) {
  412. String user_arg = user_args[i].strip_edges();
  413. if (!user_arg.is_empty()) {
  414. args.push_back(user_arg);
  415. }
  416. }
  417. args.push_back("-s");
  418. if (p_preset->get("codesign/identity") == "") {
  419. args.push_back("-");
  420. } else {
  421. args.push_back(p_preset->get("codesign/identity"));
  422. }
  423. args.push_back("-v"); /* provide some more feedback */
  424. if (p_preset->get("codesign/replace_existing_signature")) {
  425. args.push_back("-f");
  426. }
  427. args.push_back(p_path);
  428. String str;
  429. Error err = OS::get_singleton()->execute("codesign", args, &str, nullptr, true);
  430. ERR_FAIL_COND_V(err != OK, err);
  431. print_line("codesign (" + p_path + "):\n" + str);
  432. if (str.find("no identity found") != -1) {
  433. EditorNode::add_io_error("codesign: no identity found");
  434. return FAILED;
  435. }
  436. if ((str.find("unrecognized blob type") != -1) || (str.find("cannot read entitlement data") != -1)) {
  437. EditorNode::add_io_error("codesign: invalid entitlements file");
  438. return FAILED;
  439. }
  440. #endif
  441. return OK;
  442. }
  443. Error EditorExportPlatformOSX::_create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name) {
  444. List<String> args;
  445. if (FileAccess::exists(p_dmg_path)) {
  446. OS::get_singleton()->move_to_trash(p_dmg_path);
  447. }
  448. args.push_back("create");
  449. args.push_back(p_dmg_path);
  450. args.push_back("-volname");
  451. args.push_back(p_pkg_name);
  452. args.push_back("-fs");
  453. args.push_back("HFS+");
  454. args.push_back("-srcfolder");
  455. args.push_back(p_app_path_name);
  456. String str;
  457. Error err = OS::get_singleton()->execute("hdiutil", args, &str, nullptr, true);
  458. ERR_FAIL_COND_V(err != OK, err);
  459. print_line("hdiutil returned: " + str);
  460. if (str.find("create failed") != -1) {
  461. if (str.find("File exists") != -1) {
  462. EditorNode::add_io_error("hdiutil: create failed - file exists");
  463. } else {
  464. EditorNode::add_io_error("hdiutil: create failed");
  465. }
  466. return FAILED;
  467. }
  468. return OK;
  469. }
  470. Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  471. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  472. String src_pkg_name;
  473. EditorProgress ep("export", "Exporting for OSX", 3, true);
  474. if (p_debug) {
  475. src_pkg_name = p_preset->get("custom_template/debug");
  476. } else {
  477. src_pkg_name = p_preset->get("custom_template/release");
  478. }
  479. if (src_pkg_name == "") {
  480. String err;
  481. src_pkg_name = find_export_template("osx.zip", &err);
  482. if (src_pkg_name == "") {
  483. EditorNode::add_io_error(err);
  484. return ERR_FILE_NOT_FOUND;
  485. }
  486. }
  487. if (!DirAccess::exists(p_path.get_base_dir())) {
  488. return ERR_FILE_BAD_PATH;
  489. }
  490. FileAccess *src_f = nullptr;
  491. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  492. if (ep.step("Creating app", 0)) {
  493. return ERR_SKIP;
  494. }
  495. unzFile src_pkg_zip = unzOpen2(src_pkg_name.utf8().get_data(), &io);
  496. if (!src_pkg_zip) {
  497. EditorNode::add_io_error("Could not find template app to export:\n" + src_pkg_name);
  498. return ERR_FILE_NOT_FOUND;
  499. }
  500. int ret = unzGoToFirstFile(src_pkg_zip);
  501. String binary_to_use = "godot_osx_" + String(p_debug ? "debug" : "release") + ".64";
  502. String pkg_name;
  503. if (p_preset->get("application/name") != "") {
  504. pkg_name = p_preset->get("application/name"); // app_name
  505. } else if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  506. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  507. } else {
  508. pkg_name = "Unnamed";
  509. }
  510. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  511. String export_format = use_dmg() && p_path.ends_with("dmg") ? "dmg" : "zip";
  512. // Create our application bundle.
  513. String tmp_app_dir_name = pkg_name + ".app";
  514. String tmp_app_path_name = EditorPaths::get_singleton()->get_cache_dir().plus_file(tmp_app_dir_name);
  515. print_line("Exporting to " + tmp_app_path_name);
  516. Error err = OK;
  517. DirAccessRef tmp_app_dir = DirAccess::create_for_path(tmp_app_path_name);
  518. if (!tmp_app_dir) {
  519. err = ERR_CANT_CREATE;
  520. }
  521. // Create our folder structure.
  522. if (err == OK) {
  523. print_line("Creating " + tmp_app_path_name + "/Contents/MacOS");
  524. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/MacOS");
  525. }
  526. if (err == OK) {
  527. print_line("Creating " + tmp_app_path_name + "/Contents/Frameworks");
  528. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks");
  529. }
  530. if (err == OK) {
  531. print_line("Creating " + tmp_app_path_name + "/Contents/Resources");
  532. err = tmp_app_dir->make_dir_recursive(tmp_app_path_name + "/Contents/Resources");
  533. }
  534. // Now process our template.
  535. bool found_binary = false;
  536. int total_size = 0;
  537. Vector<String> dylibs_found;
  538. while (ret == UNZ_OK && err == OK) {
  539. bool is_execute = false;
  540. // Get filename.
  541. unz_file_info info;
  542. char fname[16384];
  543. ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);
  544. String file = fname;
  545. Vector<uint8_t> data;
  546. data.resize(info.uncompressed_size);
  547. // Read.
  548. unzOpenCurrentFile(src_pkg_zip);
  549. unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());
  550. unzCloseCurrentFile(src_pkg_zip);
  551. // Write.
  552. file = file.replace_first("osx_template.app/", "");
  553. if (file == "Contents/Info.plist") {
  554. _fix_plist(p_preset, data, pkg_name);
  555. }
  556. if (file.begins_with("Contents/MacOS/godot_")) {
  557. if (file != "Contents/MacOS/" + binary_to_use) {
  558. ret = unzGoToNextFile(src_pkg_zip);
  559. continue; // skip
  560. }
  561. found_binary = true;
  562. is_execute = true;
  563. file = "Contents/MacOS/" + pkg_name;
  564. }
  565. if (file == "Contents/Resources/icon.icns") {
  566. // See if there is an icon.
  567. String iconpath;
  568. if (p_preset->get("application/icon") != "") {
  569. iconpath = p_preset->get("application/icon");
  570. } else {
  571. iconpath = ProjectSettings::get_singleton()->get("application/config/icon");
  572. }
  573. if (iconpath != "") {
  574. if (iconpath.get_extension() == "icns") {
  575. FileAccess *icon = FileAccess::open(iconpath, FileAccess::READ);
  576. if (icon) {
  577. data.resize(icon->get_length());
  578. icon->get_buffer(&data.write[0], icon->get_length());
  579. icon->close();
  580. memdelete(icon);
  581. }
  582. } else {
  583. Ref<Image> icon;
  584. icon.instance();
  585. icon->load(iconpath);
  586. if (!icon->is_empty()) {
  587. _make_icon(icon, data);
  588. }
  589. }
  590. }
  591. }
  592. if (data.size() > 0) {
  593. if (file.find("/data.mono.osx.64.release_debug/") != -1) {
  594. if (!p_debug) {
  595. ret = unzGoToNextFile(src_pkg_zip);
  596. continue; // skip
  597. }
  598. file = file.replace("/data.mono.osx.64.release_debug/", "/GodotSharp/");
  599. }
  600. if (file.find("/data.mono.osx.64.release/") != -1) {
  601. if (p_debug) {
  602. ret = unzGoToNextFile(src_pkg_zip);
  603. continue; // skip
  604. }
  605. file = file.replace("/data.mono.osx.64.release/", "/GodotSharp/");
  606. }
  607. if (file.ends_with(".dylib")) {
  608. dylibs_found.push_back(file);
  609. }
  610. print_line("ADDING: " + file + " size: " + itos(data.size()));
  611. total_size += data.size();
  612. // Write it into our application bundle.
  613. file = tmp_app_path_name.plus_file(file);
  614. if (err == OK) {
  615. err = tmp_app_dir->make_dir_recursive(file.get_base_dir());
  616. }
  617. if (err == OK) {
  618. FileAccess *f = FileAccess::open(file, FileAccess::WRITE);
  619. if (f) {
  620. f->store_buffer(data.ptr(), data.size());
  621. f->close();
  622. if (is_execute) {
  623. // chmod with 0755 if the file is executable.
  624. FileAccess::set_unix_permissions(file, 0755);
  625. }
  626. memdelete(f);
  627. } else {
  628. err = ERR_CANT_CREATE;
  629. }
  630. }
  631. }
  632. ret = unzGoToNextFile(src_pkg_zip);
  633. }
  634. // We're done with our source zip.
  635. unzClose(src_pkg_zip);
  636. if (!found_binary) {
  637. ERR_PRINT("Requested template binary '" + binary_to_use + "' not found. It might be missing from your template archive.");
  638. err = ERR_FILE_NOT_FOUND;
  639. }
  640. if (err == OK) {
  641. if (ep.step("Making PKG", 1)) {
  642. return ERR_SKIP;
  643. }
  644. String pack_path = tmp_app_path_name + "/Contents/Resources/" + pkg_name + ".pck";
  645. Vector<SharedObject> shared_objects;
  646. err = save_pack(p_preset, pack_path, &shared_objects);
  647. // See if we can code sign our new package.
  648. bool sign_enabled = p_preset->get("codesign/enable");
  649. String ent_path = p_preset->get("codesign/entitlements/custom_file");
  650. if (sign_enabled && (ent_path == "")) {
  651. ent_path = EditorPaths::get_singleton()->get_cache_dir().plus_file(pkg_name + ".entitlements");
  652. FileAccess *ent_f = FileAccess::open(ent_path, FileAccess::WRITE);
  653. if (ent_f) {
  654. ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  655. ent_f->store_line("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  656. ent_f->store_line("<plist version=\"1.0\">");
  657. ent_f->store_line("<dict>");
  658. if ((bool)p_preset->get("codesign/entitlements/allow_jit_code_execution")) {
  659. ent_f->store_line("<key>com.apple.security.cs.allow-jit</key>");
  660. ent_f->store_line("<true/>");
  661. }
  662. if ((bool)p_preset->get("codesign/entitlements/allow_unsigned_executable_memory")) {
  663. ent_f->store_line("<key>com.apple.security.cs.allow-unsigned-executable-memory</key>");
  664. ent_f->store_line("<true/>");
  665. }
  666. if ((bool)p_preset->get("codesign/entitlements/allow_dyld_environment_variables")) {
  667. ent_f->store_line("<key>com.apple.security.cs.allow-dyld-environment-variables</key>");
  668. ent_f->store_line("<true/>");
  669. }
  670. if ((bool)p_preset->get("codesign/entitlements/disable_library_validation")) {
  671. ent_f->store_line("<key>com.apple.security.cs.disable-library-validation</key>");
  672. ent_f->store_line("<true/>");
  673. }
  674. if ((bool)p_preset->get("codesign/entitlements/audio_input")) {
  675. ent_f->store_line("<key>com.apple.security.device.audio-input</key>");
  676. ent_f->store_line("<true/>");
  677. }
  678. if ((bool)p_preset->get("codesign/entitlements/camera")) {
  679. ent_f->store_line("<key>com.apple.security.device.camera</key>");
  680. ent_f->store_line("<true/>");
  681. }
  682. if ((bool)p_preset->get("codesign/entitlements/location")) {
  683. ent_f->store_line("<key>com.apple.security.personal-information.location</key>");
  684. ent_f->store_line("<true/>");
  685. }
  686. if ((bool)p_preset->get("codesign/entitlements/address_book")) {
  687. ent_f->store_line("<key>com.apple.security.personal-information.addressbook</key>");
  688. ent_f->store_line("<true/>");
  689. }
  690. if ((bool)p_preset->get("codesign/entitlements/calendars")) {
  691. ent_f->store_line("<key>com.apple.security.personal-information.calendars</key>");
  692. ent_f->store_line("<true/>");
  693. }
  694. if ((bool)p_preset->get("codesign/entitlements/photos_library")) {
  695. ent_f->store_line("<key>com.apple.security.personal-information.photos-library</key>");
  696. ent_f->store_line("<true/>");
  697. }
  698. if ((bool)p_preset->get("codesign/entitlements/apple_events")) {
  699. ent_f->store_line("<key>com.apple.security.automation.apple-events</key>");
  700. ent_f->store_line("<true/>");
  701. }
  702. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/enabled")) {
  703. ent_f->store_line("<key>com.apple.security.app-sandbox</key>");
  704. ent_f->store_line("<true/>");
  705. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_server")) {
  706. ent_f->store_line("<key>com.apple.security.network.server</key>");
  707. ent_f->store_line("<true/>");
  708. }
  709. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/network_client")) {
  710. ent_f->store_line("<key>com.apple.security.network.client</key>");
  711. ent_f->store_line("<true/>");
  712. }
  713. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_usb")) {
  714. ent_f->store_line("<key>com.apple.security.device.usb</key>");
  715. ent_f->store_line("<true/>");
  716. }
  717. if ((bool)p_preset->get("codesign/entitlements/app_sandbox/device_bluetooth")) {
  718. ent_f->store_line("<key>com.apple.security.device.bluetooth</key>");
  719. ent_f->store_line("<true/>");
  720. }
  721. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 1) {
  722. ent_f->store_line("<key>com.apple.security.files.downloads.read-only</key>");
  723. ent_f->store_line("<true/>");
  724. }
  725. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_downloads") == 2) {
  726. ent_f->store_line("<key>com.apple.security.files.downloads.read-write</key>");
  727. ent_f->store_line("<true/>");
  728. }
  729. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 1) {
  730. ent_f->store_line("<key>com.apple.security.files.pictures.read-only</key>");
  731. ent_f->store_line("<true/>");
  732. }
  733. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_pictures") == 2) {
  734. ent_f->store_line("<key>com.apple.security.files.pictures.read-write</key>");
  735. ent_f->store_line("<true/>");
  736. }
  737. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 1) {
  738. ent_f->store_line("<key>com.apple.security.files.music.read-only</key>");
  739. ent_f->store_line("<true/>");
  740. }
  741. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_music") == 2) {
  742. ent_f->store_line("<key>com.apple.security.files.music.read-write</key>");
  743. ent_f->store_line("<true/>");
  744. }
  745. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 1) {
  746. ent_f->store_line("<key>com.apple.security.files.movies.read-only</key>");
  747. ent_f->store_line("<true/>");
  748. }
  749. if ((int)p_preset->get("codesign/entitlements/app_sandbox/files_movies") == 2) {
  750. ent_f->store_line("<key>com.apple.security.files.movies.read-write</key>");
  751. ent_f->store_line("<true/>");
  752. }
  753. }
  754. ent_f->store_line("</dict>");
  755. ent_f->store_line("</plist>");
  756. ent_f->close();
  757. memdelete(ent_f);
  758. } else {
  759. err = ERR_CANT_CREATE;
  760. }
  761. }
  762. if (err == OK) {
  763. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  764. for (int i = 0; i < shared_objects.size(); i++) {
  765. String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
  766. if (da->dir_exists(src_path)) {
  767. #ifndef UNIX_ENABLED
  768. WARN_PRINT("Relative symlinks are not supported, exported " + src_path.get_file() + " might be broken!");
  769. #endif
  770. print_verbose("export framework: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  771. err = da->make_dir_recursive(tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  772. if (err == OK) {
  773. err = da->copy_dir(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), -1, true);
  774. }
  775. } else {
  776. print_verbose("export dylib: " + src_path + " -> " + tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  777. err = da->copy(src_path, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file());
  778. }
  779. if (err == OK && sign_enabled) {
  780. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file(), ent_path);
  781. }
  782. }
  783. memdelete(da);
  784. }
  785. if (sign_enabled) {
  786. for (int i = 0; i < dylibs_found.size(); i++) {
  787. if (err == OK) {
  788. err = _code_sign(p_preset, tmp_app_path_name + "/" + dylibs_found[i], ent_path);
  789. }
  790. }
  791. }
  792. if (err == OK && sign_enabled) {
  793. if (ep.step("Code signing bundle", 2)) {
  794. return ERR_SKIP;
  795. }
  796. err = _code_sign(p_preset, tmp_app_path_name + "/Contents/MacOS/" + pkg_name, ent_path);
  797. }
  798. if (export_format == "dmg") {
  799. // Create a DMG.
  800. if (err == OK) {
  801. if (ep.step("Making DMG", 3)) {
  802. return ERR_SKIP;
  803. }
  804. err = _create_dmg(p_path, pkg_name, tmp_app_path_name);
  805. }
  806. // Sign DMG.
  807. if (err == OK && sign_enabled) {
  808. if (ep.step("Code signing DMG", 3)) {
  809. return ERR_SKIP;
  810. }
  811. err = _code_sign(p_preset, p_path, ent_path);
  812. }
  813. } else {
  814. // Create ZIP.
  815. if (err == OK) {
  816. if (ep.step("Making ZIP", 3)) {
  817. return ERR_SKIP;
  818. }
  819. if (FileAccess::exists(p_path)) {
  820. OS::get_singleton()->move_to_trash(p_path);
  821. }
  822. FileAccess *dst_f = nullptr;
  823. zlib_filefunc_def io_dst = zipio_create_io_from_file(&dst_f);
  824. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  825. _zip_folder_recursive(zip, EditorPaths::get_singleton()->get_cache_dir(), pkg_name + ".app", pkg_name);
  826. zipClose(zip, nullptr);
  827. }
  828. }
  829. bool noto_enabled = p_preset->get("notarization/enable");
  830. if (err == OK && noto_enabled) {
  831. if (ep.step("Sending archive for notarization", 4)) {
  832. return ERR_SKIP;
  833. }
  834. err = _notarize(p_preset, p_path);
  835. }
  836. // Clean up temporary .app dir.
  837. tmp_app_dir->change_dir(tmp_app_path_name);
  838. tmp_app_dir->erase_contents_recursive();
  839. tmp_app_dir->change_dir("..");
  840. tmp_app_dir->remove(tmp_app_dir_name);
  841. }
  842. return err;
  843. }
  844. void EditorExportPlatformOSX::_zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  845. String dir = p_root_path.plus_file(p_folder);
  846. DirAccess *da = DirAccess::open(dir);
  847. da->list_dir_begin();
  848. String f;
  849. while ((f = da->get_next()) != "") {
  850. if (f == "." || f == "..") {
  851. continue;
  852. }
  853. if (da->is_link(f)) {
  854. OS::Time time = OS::get_singleton()->get_time();
  855. OS::Date date = OS::get_singleton()->get_date();
  856. zip_fileinfo zipfi;
  857. zipfi.tmz_date.tm_hour = time.hour;
  858. zipfi.tmz_date.tm_mday = date.day;
  859. zipfi.tmz_date.tm_min = time.minute;
  860. zipfi.tmz_date.tm_mon = date.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, http://www.cplusplus.com/reference/ctime/tm/
  861. zipfi.tmz_date.tm_sec = time.second;
  862. zipfi.tmz_date.tm_year = date.year;
  863. zipfi.dosDate = 0;
  864. // 0120000: symbolic link type
  865. // 0000755: permissions rwxr-xr-x
  866. // 0000644: permissions rw-r--r--
  867. uint32_t _mode = 0120644;
  868. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  869. zipfi.internal_fa = 0;
  870. zipOpenNewFileInZip4(p_zip,
  871. p_folder.plus_file(f).utf8().get_data(),
  872. &zipfi,
  873. nullptr,
  874. 0,
  875. nullptr,
  876. 0,
  877. nullptr,
  878. Z_DEFLATED,
  879. Z_DEFAULT_COMPRESSION,
  880. 0,
  881. -MAX_WBITS,
  882. DEF_MEM_LEVEL,
  883. Z_DEFAULT_STRATEGY,
  884. nullptr,
  885. 0,
  886. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  887. 0);
  888. String target = da->read_link(f);
  889. zipWriteInFileInZip(p_zip, target.utf8().get_data(), target.utf8().size());
  890. zipCloseFileInZip(p_zip);
  891. } else if (da->current_is_dir()) {
  892. _zip_folder_recursive(p_zip, p_root_path, p_folder.plus_file(f), p_pkg_name);
  893. } else {
  894. bool is_executable = (p_folder.ends_with("MacOS") && (f == p_pkg_name));
  895. OS::Time time = OS::get_singleton()->get_time();
  896. OS::Date date = OS::get_singleton()->get_date();
  897. zip_fileinfo zipfi;
  898. zipfi.tmz_date.tm_hour = time.hour;
  899. zipfi.tmz_date.tm_mday = date.day;
  900. zipfi.tmz_date.tm_min = time.minute;
  901. zipfi.tmz_date.tm_mon = date.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, http://www.cplusplus.com/reference/ctime/tm/
  902. zipfi.tmz_date.tm_sec = time.second;
  903. zipfi.tmz_date.tm_year = date.year;
  904. zipfi.dosDate = 0;
  905. // 0100000: regular file type
  906. // 0000755: permissions rwxr-xr-x
  907. // 0000644: permissions rw-r--r--
  908. uint32_t _mode = (is_executable ? 0100755 : 0100644);
  909. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  910. zipfi.internal_fa = 0;
  911. zipOpenNewFileInZip4(p_zip,
  912. p_folder.plus_file(f).utf8().get_data(),
  913. &zipfi,
  914. nullptr,
  915. 0,
  916. nullptr,
  917. 0,
  918. nullptr,
  919. Z_DEFLATED,
  920. Z_DEFAULT_COMPRESSION,
  921. 0,
  922. -MAX_WBITS,
  923. DEF_MEM_LEVEL,
  924. Z_DEFAULT_STRATEGY,
  925. nullptr,
  926. 0,
  927. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  928. 0);
  929. Vector<uint8_t> array = FileAccess::get_file_as_array(dir.plus_file(f));
  930. zipWriteInFileInZip(p_zip, array.ptr(), array.size());
  931. zipCloseFileInZip(p_zip);
  932. }
  933. }
  934. da->list_dir_end();
  935. memdelete(da);
  936. }
  937. bool EditorExportPlatformOSX::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  938. String err;
  939. bool valid = false;
  940. // Look for export templates (first official, and if defined custom templates).
  941. bool dvalid = exists_export_template("osx.zip", &err);
  942. bool rvalid = dvalid; // Both in the same ZIP.
  943. if (p_preset->get("custom_template/debug") != "") {
  944. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  945. if (!dvalid) {
  946. err += TTR("Custom debug template not found.") + "\n";
  947. }
  948. }
  949. if (p_preset->get("custom_template/release") != "") {
  950. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  951. if (!rvalid) {
  952. err += TTR("Custom release template not found.") + "\n";
  953. }
  954. }
  955. valid = dvalid || rvalid;
  956. r_missing_templates = !valid;
  957. String identifier = p_preset->get("application/bundle_identifier");
  958. String pn_err;
  959. if (!is_package_name_valid(identifier, &pn_err)) {
  960. err += TTR("Invalid bundle identifier:") + " " + pn_err + "\n";
  961. valid = false;
  962. }
  963. bool sign_enabled = p_preset->get("codesign/enable");
  964. bool noto_enabled = p_preset->get("notarization/enable");
  965. if (noto_enabled) {
  966. if (!sign_enabled) {
  967. err += TTR("Notarization: code signing required.") + "\n";
  968. valid = false;
  969. }
  970. bool hr_enabled = p_preset->get("codesign/hardened_runtime");
  971. if (!hr_enabled) {
  972. err += TTR("Notarization: hardened runtime required.") + "\n";
  973. valid = false;
  974. }
  975. if (p_preset->get("notarization/apple_id_name") == "") {
  976. err += TTR("Notarization: Apple ID name not specified.") + "\n";
  977. valid = false;
  978. }
  979. if (p_preset->get("notarization/apple_id_password") == "") {
  980. err += TTR("Notarization: Apple ID password not specified.") + "\n";
  981. valid = false;
  982. }
  983. }
  984. if (!err.is_empty()) {
  985. r_error = err;
  986. }
  987. return valid;
  988. }
  989. EditorExportPlatformOSX::EditorExportPlatformOSX() {
  990. Ref<Image> img = memnew(Image(_osx_logo));
  991. logo.instance();
  992. logo->create_from_image(img);
  993. }
  994. EditorExportPlatformOSX::~EditorExportPlatformOSX() {
  995. }
  996. void register_osx_exporter() {
  997. Ref<EditorExportPlatformOSX> platform;
  998. platform.instance();
  999. EditorExport::get_singleton()->add_export_platform(platform);
  1000. }