export_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**************************************************************************/
  2. /* export_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "logo_svg.gen.h"
  32. #include "run_icon_svg.gen.h"
  33. Vector<String> EditorExportPlatformIOS::device_types({ "iPhone", "iPad" });
  34. EditorExportPlatformIOS::EditorExportPlatformIOS() :
  35. EditorExportPlatformAppleEmbedded(_ios_logo_svg, _ios_run_icon_svg) {
  36. #ifdef MACOS_ENABLED
  37. _start_remote_device_poller_thread();
  38. #endif
  39. }
  40. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  41. }
  42. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
  43. EditorExportPlatformAppleEmbedded::get_export_options(r_options);
  44. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  45. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), get_minimum_deployment_target()));
  46. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "storyboard/image_scale_mode", PROPERTY_HINT_ENUM, "Same as Logo,Center,Scale to Fit,Scale to Fill,Scale"), 0));
  47. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
  48. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
  49. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  50. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  51. }
  52. bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  53. bool valid = EditorExportPlatformAppleEmbedded::has_valid_export_configuration(p_preset, r_error, r_missing_templates, p_debug);
  54. String err;
  55. String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
  56. String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver." + get_platform_name());
  57. if ((rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "metal") {
  58. float version = p_preset->get("application/min_ios_version").operator String().to_float();
  59. if (version < 14.0) {
  60. err += TTR("Metal renderer require iOS 14+.") + "\n";
  61. }
  62. }
  63. if (!err.is_empty()) {
  64. if (!r_error.is_empty()) {
  65. r_error += err;
  66. } else {
  67. r_error = err;
  68. }
  69. }
  70. return valid;
  71. }
  72. HashMap<String, Variant> EditorExportPlatformIOS::get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const {
  73. HashMap<String, Variant> settings;
  74. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  75. String value;
  76. switch (image_scale_mode) {
  77. case 0: {
  78. String logo_path = get_project_setting(p_preset, "application/boot_splash/image");
  79. bool is_on = get_project_setting(p_preset, "application/boot_splash/fullsize");
  80. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  81. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  82. } break;
  83. default: {
  84. value = storyboard_image_scale_mode[image_scale_mode - 1];
  85. }
  86. }
  87. settings["ios/launch_screen_image_mode"] = value;
  88. return settings;
  89. }
  90. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  91. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  92. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  93. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  94. String image_path = p_dest_dir.path_join("[email protected]");
  95. Error err = OK;
  96. Ref<Image> image = _load_icon_or_splash_image(custom_launch_image_2x, &err);
  97. if (err != OK || image.is_null() || image->is_empty()) {
  98. return err;
  99. }
  100. if (image->save_png(image_path) != OK) {
  101. return ERR_FILE_CANT_WRITE;
  102. }
  103. image_path = p_dest_dir.path_join("[email protected]");
  104. image = _load_icon_or_splash_image(custom_launch_image_3x, &err);
  105. if (err != OK || image.is_null() || image->is_empty()) {
  106. return err;
  107. }
  108. if (image->save_png(image_path) != OK) {
  109. return ERR_FILE_CANT_WRITE;
  110. }
  111. } else {
  112. Error err = OK;
  113. Ref<Image> splash;
  114. const String splash_path = get_project_setting(p_preset, "application/boot_splash/image");
  115. if (!splash_path.is_empty()) {
  116. splash = _load_icon_or_splash_image(splash_path, &err);
  117. }
  118. if (err != OK || splash.is_null() || splash->is_empty()) {
  119. splash.instantiate(boot_splash_png);
  120. }
  121. // Using same image for both @2x and @3x
  122. // because Godot's own boot logo uses single image for all resolutions.
  123. // Also not using @1x image, because devices using this image variant
  124. // are not supported by iOS 9, which is minimal target.
  125. const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
  126. const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");
  127. if (splash->save_png(splash_png_path_2x) != OK) {
  128. return ERR_FILE_CANT_WRITE;
  129. }
  130. if (splash->save_png(splash_png_path_3x) != OK) {
  131. return ERR_FILE_CANT_WRITE;
  132. }
  133. }
  134. return OK;
  135. }
  136. Vector<EditorExportPlatformAppleEmbedded::IconInfo> EditorExportPlatformIOS::get_icon_infos() const {
  137. Vector<EditorExportPlatformAppleEmbedded::IconInfo> icon_infos;
  138. return {
  139. // Settings on iPhone, iPad Pro, iPad, iPad mini
  140. { PNAME("icons/settings_58x58"), "universal", "Icon-58", "58", "2x", "29x29", false },
  141. { PNAME("icons/settings_87x87"), "universal", "Icon-87", "87", "3x", "29x29", false },
  142. // Notifications on iPhone, iPad Pro, iPad, iPad mini
  143. { PNAME("icons/notification_40x40"), "universal", "Icon-40", "40", "2x", "20x20", false },
  144. { PNAME("icons/notification_60x60"), "universal", "Icon-60", "60", "3x", "20x20", false },
  145. { PNAME("icons/notification_76x76"), "universal", "Icon-76", "76", "2x", "38x38", false },
  146. { PNAME("icons/notification_114x114"), "universal", "Icon-114", "114", "3x", "38x38", false },
  147. // Spotlight on iPhone, iPad Pro, iPad, iPad mini
  148. { PNAME("icons/spotlight_80x80"), "universal", "Icon-80", "80", "2x", "40x40", false },
  149. { PNAME("icons/spotlight_120x120"), "universal", "Icon-120", "120", "3x", "40x40", false },
  150. // Home Screen on iPhone
  151. { PNAME("icons/iphone_120x120"), "universal", "Icon-120-1", "120", "2x", "60x60", false },
  152. { PNAME("icons/iphone_180x180"), "universal", "Icon-180", "180", "3x", "60x60", false },
  153. // Home Screen on iPad Pro
  154. { PNAME("icons/ipad_167x167"), "universal", "Icon-167", "167", "2x", "83.5x83.5", false },
  155. // Home Screen on iPad, iPad mini
  156. { PNAME("icons/ipad_152x152"), "universal", "Icon-152", "152", "2x", "76x76", false },
  157. { PNAME("icons/ios_128x128"), "universal", "Icon-128", "128", "2x", "64x64", false },
  158. { PNAME("icons/ios_192x192"), "universal", "Icon-192", "192", "3x", "64x64", false },
  159. { PNAME("icons/ios_136x136"), "universal", "Icon-136", "136", "2x", "68x68", false },
  160. // App Store
  161. { PNAME("icons/app_store_1024x1024"), "universal", "Icon-1024", "1024", "1x", "1024x1024", true },
  162. };
  163. }
  164. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  165. String json_description = "{\"images\":[";
  166. String sizes;
  167. Ref<DirAccess> da = DirAccess::open(p_iconset_dir);
  168. if (da.is_null()) {
  169. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));
  170. return ERR_CANT_OPEN;
  171. }
  172. Color boot_bg_color = get_project_setting(p_preset, "application/boot_splash/bg_color");
  173. enum IconColorMode {
  174. ICON_NORMAL,
  175. ICON_DARK,
  176. ICON_TINTED,
  177. ICON_MAX,
  178. };
  179. Vector<IconInfo> icon_infos = get_icon_infos();
  180. bool first_icon = true;
  181. for (int i = 0; i < icon_infos.size(); ++i) {
  182. for (int color_mode = ICON_NORMAL; color_mode < ICON_MAX; color_mode++) {
  183. IconInfo info = icon_infos[i];
  184. int side_size = String(info.actual_size_side).to_int();
  185. String key = info.preset_key;
  186. String exp_name = info.export_name;
  187. if (color_mode == ICON_DARK) {
  188. key += "_dark";
  189. exp_name += "_dark";
  190. } else if (color_mode == ICON_TINTED) {
  191. key += "_tinted";
  192. exp_name += "_tinted";
  193. }
  194. exp_name += ".png";
  195. String icon_path = p_preset->get(key);
  196. bool resize_waning = true;
  197. if (icon_path.is_empty()) {
  198. // Load and resize base icon.
  199. key = "icons/icon_1024x1024";
  200. if (color_mode == ICON_DARK) {
  201. key += "_dark";
  202. } else if (color_mode == ICON_TINTED) {
  203. key += "_tinted";
  204. }
  205. icon_path = p_preset->get(key);
  206. resize_waning = false;
  207. }
  208. if (icon_path.is_empty()) {
  209. if (color_mode != ICON_NORMAL) {
  210. continue;
  211. }
  212. // Resize main app icon.
  213. icon_path = get_project_setting(p_preset, "application/config/icon");
  214. Error err = OK;
  215. Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);
  216. if (err != OK || img.is_null() || img->is_empty()) {
  217. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  218. return ERR_UNCONFIGURED;
  219. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  220. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  221. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  222. new_img->fill(boot_bg_color);
  223. _blend_and_rotate(new_img, img, false);
  224. err = new_img->save_png(p_iconset_dir + exp_name);
  225. } else {
  226. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  227. err = img->save_png(p_iconset_dir + exp_name);
  228. }
  229. if (err) {
  230. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  231. return err;
  232. }
  233. } else {
  234. // Load custom icon and resize if required.
  235. Error err = OK;
  236. Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);
  237. if (err != OK || img.is_null() || img->is_empty()) {
  238. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  239. return ERR_UNCONFIGURED;
  240. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  241. if (resize_waning) {
  242. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  243. }
  244. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  245. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  246. new_img->fill(boot_bg_color);
  247. _blend_and_rotate(new_img, img, false);
  248. err = new_img->save_png(p_iconset_dir + exp_name);
  249. } else if (img->get_width() != side_size || img->get_height() != side_size) {
  250. if (resize_waning) {
  251. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s): '%s' has incorrect size %s and was automatically resized to %s.", info.preset_key, icon_path, img->get_size(), Vector2i(side_size, side_size)));
  252. }
  253. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  254. err = img->save_png(p_iconset_dir + exp_name);
  255. } else if (!icon_path.ends_with(".png")) {
  256. err = img->save_png(p_iconset_dir + exp_name);
  257. } else {
  258. err = da->copy(icon_path, p_iconset_dir + exp_name);
  259. }
  260. if (err) {
  261. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  262. return err;
  263. }
  264. }
  265. sizes += String(info.actual_size_side) + "\n";
  266. if (first_icon) {
  267. first_icon = false;
  268. } else {
  269. json_description += ",";
  270. }
  271. json_description += String("{");
  272. if (color_mode != ICON_NORMAL) {
  273. json_description += String("\"appearances\":[{");
  274. json_description += String("\"appearance\":\"luminosity\",");
  275. if (color_mode == ICON_DARK) {
  276. json_description += String("\"value\":\"dark\"");
  277. } else if (color_mode == ICON_TINTED) {
  278. json_description += String("\"value\":\"tinted\"");
  279. }
  280. json_description += String("}],");
  281. }
  282. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  283. json_description += String("\"platform\":\"" + get_platform_name() + "\",");
  284. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  285. if (String(info.scale) != "1x") {
  286. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  287. }
  288. json_description += String("\"filename\":") + "\"" + exp_name + "\"";
  289. json_description += String("}");
  290. }
  291. }
  292. json_description += "],\"info\":{\"author\":\"xcode\",\"version\":1}}";
  293. Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  294. if (json_file.is_null()) {
  295. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));
  296. return ERR_CANT_CREATE;
  297. }
  298. CharString json_utf8 = json_description.utf8();
  299. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  300. Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  301. if (sizes_file.is_null()) {
  302. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));
  303. return ERR_CANT_CREATE;
  304. }
  305. CharString sizes_utf8 = sizes.utf8();
  306. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  307. return OK;
  308. }