export_plugin.cpp 15 KB

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