export_plugin.cpp 46 KB

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