Path: blob/master/platform/ios/export/export_plugin.cpp
10278 views
/**************************************************************************/1/* export_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "export_plugin.h"3132#include "logo_svg.gen.h"33#include "run_icon_svg.gen.h"3435Vector<String> EditorExportPlatformIOS::device_types({ "iPhone", "iPad" });3637EditorExportPlatformIOS::EditorExportPlatformIOS() :38EditorExportPlatformAppleEmbedded(_ios_logo_svg, _ios_run_icon_svg) {39#ifdef MACOS_ENABLED40_start_remote_device_poller_thread();41#endif42}4344EditorExportPlatformIOS::~EditorExportPlatformIOS() {45}4647void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {48EditorExportPlatformAppleEmbedded::get_export_options(r_options);4950r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));51r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), get_minimum_deployment_target()));5253r_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));54r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));55r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));56r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));57r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));58}5960bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {61bool valid = EditorExportPlatformAppleEmbedded::has_valid_export_configuration(p_preset, r_error, r_missing_templates, p_debug);6263String err;64String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");65String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver." + get_platform_name());66if ((rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "metal") {67float version = p_preset->get("application/min_ios_version").operator String().to_float();68if (version < 14.0) {69err += TTR("Metal renderer require iOS 14+.") + "\n";70}71}7273if (!err.is_empty()) {74if (!r_error.is_empty()) {75r_error += err;76} else {77r_error = err;78}79}8081return valid;82}8384HashMap<String, Variant> EditorExportPlatformIOS::get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const {85HashMap<String, Variant> settings;8687int image_scale_mode = p_preset->get("storyboard/image_scale_mode");88String value;8990switch (image_scale_mode) {91case 0: {92String logo_path = get_project_setting(p_preset, "application/boot_splash/image");93bool is_on = get_project_setting(p_preset, "application/boot_splash/fullsize");94// If custom logo is not specified, Godot does not scale default one, so we should do the same.95value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";96} break;97default: {98value = storyboard_image_scale_mode[image_scale_mode - 1];99}100}101settings["ios/launch_screen_image_mode"] = value;102return settings;103}104105Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {106const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");107const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");108109if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {110String image_path = p_dest_dir.path_join("[email protected]");111Error err = OK;112Ref<Image> image = _load_icon_or_splash_image(custom_launch_image_2x, &err);113114if (err != OK || image.is_null() || image->is_empty()) {115return err;116}117118if (image->save_png(image_path) != OK) {119return ERR_FILE_CANT_WRITE;120}121122image_path = p_dest_dir.path_join("[email protected]");123image = _load_icon_or_splash_image(custom_launch_image_3x, &err);124125if (err != OK || image.is_null() || image->is_empty()) {126return err;127}128129if (image->save_png(image_path) != OK) {130return ERR_FILE_CANT_WRITE;131}132} else {133Error err = OK;134Ref<Image> splash;135136const String splash_path = get_project_setting(p_preset, "application/boot_splash/image");137138if (!splash_path.is_empty()) {139splash = _load_icon_or_splash_image(splash_path, &err);140}141142if (err != OK || splash.is_null() || splash->is_empty()) {143splash.instantiate(boot_splash_png);144}145146// Using same image for both @2x and @3x147// because Godot's own boot logo uses single image for all resolutions.148// Also not using @1x image, because devices using this image variant149// are not supported by iOS 9, which is minimal target.150const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");151const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");152153if (splash->save_png(splash_png_path_2x) != OK) {154return ERR_FILE_CANT_WRITE;155}156157if (splash->save_png(splash_png_path_3x) != OK) {158return ERR_FILE_CANT_WRITE;159}160}161162return OK;163}164165Vector<EditorExportPlatformAppleEmbedded::IconInfo> EditorExportPlatformIOS::get_icon_infos() const {166Vector<EditorExportPlatformAppleEmbedded::IconInfo> icon_infos;167return {168// Settings on iPhone, iPad Pro, iPad, iPad mini169{ PNAME("icons/settings_58x58"), "universal", "Icon-58", "58", "2x", "29x29", false },170{ PNAME("icons/settings_87x87"), "universal", "Icon-87", "87", "3x", "29x29", false },171172// Notifications on iPhone, iPad Pro, iPad, iPad mini173{ PNAME("icons/notification_40x40"), "universal", "Icon-40", "40", "2x", "20x20", false },174{ PNAME("icons/notification_60x60"), "universal", "Icon-60", "60", "3x", "20x20", false },175{ PNAME("icons/notification_76x76"), "universal", "Icon-76", "76", "2x", "38x38", false },176{ PNAME("icons/notification_114x114"), "universal", "Icon-114", "114", "3x", "38x38", false },177178// Spotlight on iPhone, iPad Pro, iPad, iPad mini179{ PNAME("icons/spotlight_80x80"), "universal", "Icon-80", "80", "2x", "40x40", false },180{ PNAME("icons/spotlight_120x120"), "universal", "Icon-120", "120", "3x", "40x40", false },181182// Home Screen on iPhone183{ PNAME("icons/iphone_120x120"), "universal", "Icon-120-1", "120", "2x", "60x60", false },184{ PNAME("icons/iphone_180x180"), "universal", "Icon-180", "180", "3x", "60x60", false },185186// Home Screen on iPad Pro187{ PNAME("icons/ipad_167x167"), "universal", "Icon-167", "167", "2x", "83.5x83.5", false },188189// Home Screen on iPad, iPad mini190{ PNAME("icons/ipad_152x152"), "universal", "Icon-152", "152", "2x", "76x76", false },191192{ PNAME("icons/ios_128x128"), "universal", "Icon-128", "128", "2x", "64x64", false },193{ PNAME("icons/ios_192x192"), "universal", "Icon-192", "192", "3x", "64x64", false },194195{ PNAME("icons/ios_136x136"), "universal", "Icon-136", "136", "2x", "68x68", false },196197// App Store198{ PNAME("icons/app_store_1024x1024"), "universal", "Icon-1024", "1024", "1x", "1024x1024", true },199};200}201202Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {203String json_description = "{\"images\":[";204String sizes;205206Ref<DirAccess> da = DirAccess::open(p_iconset_dir);207if (da.is_null()) {208add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));209return ERR_CANT_OPEN;210}211212Color boot_bg_color = get_project_setting(p_preset, "application/boot_splash/bg_color");213214enum IconColorMode {215ICON_NORMAL,216ICON_DARK,217ICON_TINTED,218ICON_MAX,219};220221Vector<IconInfo> icon_infos = get_icon_infos();222bool first_icon = true;223for (int i = 0; i < icon_infos.size(); ++i) {224for (int color_mode = ICON_NORMAL; color_mode < ICON_MAX; color_mode++) {225IconInfo info = icon_infos[i];226int side_size = String(info.actual_size_side).to_int();227String key = info.preset_key;228String exp_name = info.export_name;229if (color_mode == ICON_DARK) {230key += "_dark";231exp_name += "_dark";232} else if (color_mode == ICON_TINTED) {233key += "_tinted";234exp_name += "_tinted";235}236exp_name += ".png";237String icon_path = p_preset->get(key);238bool resize_waning = true;239if (icon_path.is_empty()) {240// Load and resize base icon.241key = "icons/icon_1024x1024";242if (color_mode == ICON_DARK) {243key += "_dark";244} else if (color_mode == ICON_TINTED) {245key += "_tinted";246}247icon_path = p_preset->get(key);248resize_waning = false;249}250if (icon_path.is_empty()) {251if (color_mode != ICON_NORMAL) {252continue;253}254// Resize main app icon.255icon_path = get_project_setting(p_preset, "application/config/icon");256Error err = OK;257Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);258if (err != OK || img.is_null() || img->is_empty()) {259add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));260return ERR_UNCONFIGURED;261} else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {262img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));263Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);264new_img->fill(boot_bg_color);265_blend_and_rotate(new_img, img, false);266err = new_img->save_png(p_iconset_dir + exp_name);267} else {268img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));269err = img->save_png(p_iconset_dir + exp_name);270}271if (err) {272add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));273return err;274}275} else {276// Load custom icon and resize if required.277Error err = OK;278Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);279if (err != OK || img.is_null() || img->is_empty()) {280add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));281return ERR_UNCONFIGURED;282} else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {283if (resize_waning) {284add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));285}286img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));287Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);288new_img->fill(boot_bg_color);289_blend_and_rotate(new_img, img, false);290err = new_img->save_png(p_iconset_dir + exp_name);291} else if (img->get_width() != side_size || img->get_height() != side_size) {292if (resize_waning) {293add_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)));294}295img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));296err = img->save_png(p_iconset_dir + exp_name);297} else if (!icon_path.ends_with(".png")) {298err = img->save_png(p_iconset_dir + exp_name);299} else {300err = da->copy(icon_path, p_iconset_dir + exp_name);301}302303if (err) {304add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));305return err;306}307}308sizes += String(info.actual_size_side) + "\n";309if (first_icon) {310first_icon = false;311} else {312json_description += ",";313}314json_description += String("{");315if (color_mode != ICON_NORMAL) {316json_description += String("\"appearances\":[{");317json_description += String("\"appearance\":\"luminosity\",");318if (color_mode == ICON_DARK) {319json_description += String("\"value\":\"dark\"");320} else if (color_mode == ICON_TINTED) {321json_description += String("\"value\":\"tinted\"");322}323json_description += String("}],");324}325json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";326json_description += String("\"platform\":\"" + get_platform_name() + "\",");327json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";328if (String(info.scale) != "1x") {329json_description += String("\"scale\":") + "\"" + info.scale + "\",";330}331json_description += String("\"filename\":") + "\"" + exp_name + "\"";332json_description += String("}");333}334}335json_description += "],\"info\":{\"author\":\"xcode\",\"version\":1}}";336337Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);338if (json_file.is_null()) {339add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));340return ERR_CANT_CREATE;341}342343CharString json_utf8 = json_description.utf8();344json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());345346Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);347if (sizes_file.is_null()) {348add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));349return ERR_CANT_CREATE;350}351352CharString sizes_utf8 = sizes.utf8();353sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());354355return OK;356}357358359