Path: blob/master/platform/ios/export/export_plugin.cpp
20938 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"3435#include "editor/editor_node.h"3637Vector<String> EditorExportPlatformIOS::device_types({ "iPhone", "iPad" });3839void EditorExportPlatformIOS::initialize() {40if (EditorNode::get_singleton()) {41EditorExportPlatformAppleEmbedded::_initialize(_ios_logo_svg, _ios_run_icon_svg);42#ifdef MACOS_ENABLED43_start_remote_device_poller_thread();44#endif45}46}4748EditorExportPlatformIOS::~EditorExportPlatformIOS() {49#ifdef MACOS_ENABLED50_stop_remote_device_poller_thread();51#endif52}5354void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {55EditorExportPlatformAppleEmbedded::get_export_options(r_options);5657r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));58r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), get_minimum_deployment_target()));5960r_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));61r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));62r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));63r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));64r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));65}6667bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {68bool valid = EditorExportPlatformAppleEmbedded::has_valid_export_configuration(p_preset, r_error, r_missing_templates, p_debug);6970String err;71String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");72String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver." + get_platform_name());73if ((rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "metal") {74float version = p_preset->get("application/min_ios_version").operator String().to_float();75if (version < 14.0) {76err += TTR("Metal renderer require iOS 14+.") + "\n";77}78}7980if (!err.is_empty()) {81if (!r_error.is_empty()) {82r_error += err;83} else {84r_error = err;85}86}8788return valid;89}9091HashMap<String, Variant> EditorExportPlatformIOS::get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const {92HashMap<String, Variant> settings;9394int image_scale_mode = p_preset->get("storyboard/image_scale_mode");95String value;9697switch (image_scale_mode) {98case 0: {99String logo_path = get_project_setting(p_preset, "application/boot_splash/image");100RenderingServer::SplashStretchMode stretch_mode = get_project_setting(p_preset, "application/boot_splash/stretch_mode");101// If custom logo is not specified, Godot does not scale default one, so we should do the same.102if (logo_path.is_empty()) {103value = "center";104} else {105switch (stretch_mode) {106case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_DISABLED: {107value = "center";108} break;109case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP: {110value = "scaleAspectFit";111} break;112case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_WIDTH: {113value = "scaleAspectFit";114} break;115case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_HEIGHT: {116value = "scaleAspectFit";117} break;118case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_COVER: {119value = "scaleAspectFill";120} break;121case RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_IGNORE: {122value = "scaleToFill";123} break;124}125}126} break;127default: {128value = storyboard_image_scale_mode[image_scale_mode - 1];129}130}131settings["ios/launch_screen_image_mode"] = value;132return settings;133}134135Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {136const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");137const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");138139if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {140String image_path = p_dest_dir.path_join("[email protected]");141Error err = OK;142Ref<Image> image = _load_icon_or_splash_image(custom_launch_image_2x, &err);143144if (err != OK || image.is_null() || image->is_empty()) {145return err;146}147148if (image->save_png(image_path) != OK) {149return ERR_FILE_CANT_WRITE;150}151152image_path = p_dest_dir.path_join("[email protected]");153image = _load_icon_or_splash_image(custom_launch_image_3x, &err);154155if (err != OK || image.is_null() || image->is_empty()) {156return err;157}158159if (image->save_png(image_path) != OK) {160return ERR_FILE_CANT_WRITE;161}162} else {163Error err = OK;164Ref<Image> splash;165166const String splash_path = get_project_setting(p_preset, "application/boot_splash/image");167168if (!splash_path.is_empty()) {169splash = _load_icon_or_splash_image(splash_path, &err);170}171172if (err != OK || splash.is_null() || splash->is_empty()) {173splash.instantiate(boot_splash_png);174}175176// Using same image for both @2x and @3x177// because Godot's own boot logo uses single image for all resolutions.178// Also not using @1x image, because devices using this image variant179// are not supported by iOS 9, which is minimal target.180const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");181const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");182183if (splash->save_png(splash_png_path_2x) != OK) {184return ERR_FILE_CANT_WRITE;185}186187if (splash->save_png(splash_png_path_3x) != OK) {188return ERR_FILE_CANT_WRITE;189}190}191192return OK;193}194195Vector<EditorExportPlatformAppleEmbedded::IconInfo> EditorExportPlatformIOS::get_icon_infos() const {196Vector<EditorExportPlatformAppleEmbedded::IconInfo> icon_infos;197return {198// Settings on iPhone, iPad Pro, iPad, iPad mini199{ PNAME("icons/settings_58x58"), "universal", "Icon-58", "58", "2x", "29x29", false },200{ PNAME("icons/settings_87x87"), "universal", "Icon-87", "87", "3x", "29x29", false },201202// Notifications on iPhone, iPad Pro, iPad, iPad mini203{ PNAME("icons/notification_40x40"), "universal", "Icon-40", "40", "2x", "20x20", false },204{ PNAME("icons/notification_60x60"), "universal", "Icon-60", "60", "3x", "20x20", false },205{ PNAME("icons/notification_76x76"), "universal", "Icon-76", "76", "2x", "38x38", false },206{ PNAME("icons/notification_114x114"), "universal", "Icon-114", "114", "3x", "38x38", false },207208// Spotlight on iPhone, iPad Pro, iPad, iPad mini209{ PNAME("icons/spotlight_80x80"), "universal", "Icon-80", "80", "2x", "40x40", false },210{ PNAME("icons/spotlight_120x120"), "universal", "Icon-120", "120", "3x", "40x40", false },211212// Home Screen on iPhone213{ PNAME("icons/iphone_120x120"), "universal", "Icon-120-1", "120", "2x", "60x60", false },214{ PNAME("icons/iphone_180x180"), "universal", "Icon-180", "180", "3x", "60x60", false },215216// Home Screen on iPad Pro217{ PNAME("icons/ipad_167x167"), "universal", "Icon-167", "167", "2x", "83.5x83.5", false },218219// Home Screen on iPad, iPad mini220{ PNAME("icons/ipad_152x152"), "universal", "Icon-152", "152", "2x", "76x76", false },221222{ PNAME("icons/ios_128x128"), "universal", "Icon-128", "128", "2x", "64x64", false },223{ PNAME("icons/ios_192x192"), "universal", "Icon-192", "192", "3x", "64x64", false },224225{ PNAME("icons/ios_136x136"), "universal", "Icon-136", "136", "2x", "68x68", false },226227// App Store228{ PNAME("icons/app_store_1024x1024"), "universal", "Icon-1024", "1024", "1x", "1024x1024", true },229};230}231232Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {233String json_description = "{\"images\":[";234String sizes;235236Ref<DirAccess> da = DirAccess::open(p_iconset_dir);237if (da.is_null()) {238add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));239return ERR_CANT_OPEN;240}241242Color boot_bg_color = get_project_setting(p_preset, "application/boot_splash/bg_color");243244enum IconColorMode {245ICON_NORMAL,246ICON_DARK,247ICON_TINTED,248ICON_MAX,249};250251Vector<IconInfo> icon_infos = get_icon_infos();252bool first_icon = true;253for (int i = 0; i < icon_infos.size(); ++i) {254for (int color_mode = ICON_NORMAL; color_mode < ICON_MAX; color_mode++) {255IconInfo info = icon_infos[i];256int side_size = String(info.actual_size_side).to_int();257String key = info.preset_key;258String exp_name = info.export_name;259if (color_mode == ICON_DARK) {260key += "_dark";261exp_name += "_dark";262} else if (color_mode == ICON_TINTED) {263key += "_tinted";264exp_name += "_tinted";265}266exp_name += ".png";267String icon_path = p_preset->get(key);268bool resize_waning = true;269if (icon_path.is_empty()) {270// Load and resize base icon.271key = "icons/icon_1024x1024";272if (color_mode == ICON_DARK) {273key += "_dark";274} else if (color_mode == ICON_TINTED) {275key += "_tinted";276}277icon_path = p_preset->get(key);278resize_waning = false;279}280if (icon_path.is_empty()) {281if (color_mode != ICON_NORMAL) {282continue;283}284// Resize main app icon.285icon_path = get_project_setting(p_preset, "application/config/icon");286Error err = OK;287Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);288if (err != OK || img.is_null() || img->is_empty()) {289add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));290return ERR_UNCONFIGURED;291} else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {292img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));293Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);294new_img->fill(boot_bg_color);295_blend_and_rotate(new_img, img, false);296err = new_img->save_png(p_iconset_dir + exp_name);297} else {298img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));299err = img->save_png(p_iconset_dir + exp_name);300}301if (err) {302add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));303return err;304}305} else {306// Load custom icon and resize if required.307Error err = OK;308Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);309if (err != OK || img.is_null() || img->is_empty()) {310add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));311return ERR_UNCONFIGURED;312} else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {313if (resize_waning) {314add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));315}316img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));317Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);318new_img->fill(boot_bg_color);319_blend_and_rotate(new_img, img, false);320err = new_img->save_png(p_iconset_dir + exp_name);321} else if (img->get_width() != side_size || img->get_height() != side_size) {322if (resize_waning) {323add_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)));324}325img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));326err = img->save_png(p_iconset_dir + exp_name);327} else if (!icon_path.ends_with(".png")) {328err = img->save_png(p_iconset_dir + exp_name);329} else {330err = da->copy(icon_path, p_iconset_dir + exp_name);331}332333if (err) {334add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));335return err;336}337}338sizes += String(info.actual_size_side) + "\n";339if (first_icon) {340first_icon = false;341} else {342json_description += ",";343}344json_description += String("{");345if (color_mode != ICON_NORMAL) {346json_description += String("\"appearances\":[{");347json_description += String("\"appearance\":\"luminosity\",");348if (color_mode == ICON_DARK) {349json_description += String("\"value\":\"dark\"");350} else if (color_mode == ICON_TINTED) {351json_description += String("\"value\":\"tinted\"");352}353json_description += String("}],");354}355json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";356json_description += String("\"platform\":\"" + get_platform_name() + "\",");357json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";358if (String(info.scale) != "1x") {359json_description += String("\"scale\":") + "\"" + info.scale + "\",";360}361json_description += String("\"filename\":") + "\"" + exp_name + "\"";362json_description += String("}");363}364}365json_description += "],\"info\":{\"author\":\"xcode\",\"version\":1}}";366367Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);368if (json_file.is_null()) {369add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));370return ERR_CANT_CREATE;371}372373CharString json_utf8 = json_description.utf8();374json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());375376Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);377if (sizes_file.is_null()) {378add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));379return ERR_CANT_CREATE;380}381382CharString sizes_utf8 = sizes.utf8();383sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());384385return OK;386}387388String EditorExportPlatformIOS::_process_config_file_line(const Ref<EditorExportPreset> &p_preset, const String &p_line, const AppleEmbeddedConfigData &p_config, bool p_debug, const CodeSigningDetails &p_code_signing) {389// Do iOS specific processing first, and call super implementation if there are no matches390391String strnew;392393// Supported Destinations394if (p_line.contains("$targeted_device_family")) {395String xcode_value;396switch ((int)p_preset->get("application/targeted_device_family")) {397case 0: // iPhone398xcode_value = "1";399break;400case 1: // iPad401xcode_value = "2";402break;403case 2: // iPhone & iPad404xcode_value = "1,2";405break;406}407strnew += p_line.replace("$targeted_device_family", xcode_value) + "\n";408409// MoltenVK Framework410} else if (p_line.contains("$moltenvk_buildfile")) {411String value = "9039D3BE24C093AC0020482C /* MoltenVK.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9039D3BD24C093AC0020482C /* MoltenVK.xcframework */; };";412strnew += p_line.replace("$moltenvk_buildfile", value) + "\n";413} else if (p_line.contains("$moltenvk_fileref")) {414String value = "9039D3BD24C093AC0020482C /* MoltenVK.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = MoltenVK; path = MoltenVK.xcframework; sourceTree = \"<group>\"; };";415strnew += p_line.replace("$moltenvk_fileref", value) + "\n";416} else if (p_line.contains("$moltenvk_buildphase")) {417String value = "9039D3BE24C093AC0020482C /* MoltenVK.xcframework in Frameworks */,";418strnew += p_line.replace("$moltenvk_buildphase", value) + "\n";419} else if (p_line.contains("$moltenvk_buildgrp")) {420String value = "9039D3BD24C093AC0020482C /* MoltenVK.xcframework */,";421strnew += p_line.replace("$moltenvk_buildgrp", value) + "\n";422423// Launch Storyboard424} else if (p_line.contains("$plist_launch_screen_name")) {425String value = "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>";426strnew += p_line.replace("$plist_launch_screen_name", value) + "\n";427} else if (p_line.contains("$pbx_launch_screen_file_reference")) {428String value = "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };";429strnew += p_line.replace("$pbx_launch_screen_file_reference", value) + "\n";430} else if (p_line.contains("$pbx_launch_screen_copy_files")) {431String value = "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */,";432strnew += p_line.replace("$pbx_launch_screen_copy_files", value) + "\n";433} else if (p_line.contains("$pbx_launch_screen_build_phase")) {434String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */,";435strnew += p_line.replace("$pbx_launch_screen_build_phase", value) + "\n";436} else if (p_line.contains("$pbx_launch_screen_build_reference")) {437String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };";438strnew += p_line.replace("$pbx_launch_screen_build_reference", value) + "\n";439440// Launch Storyboard customization441} else if (p_line.contains("$launch_screen_image_mode")) {442int image_scale_mode = p_preset->get("storyboard/image_scale_mode");443String value;444445switch (image_scale_mode) {446case 0: {447String logo_path = get_project_setting(p_preset, "application/boot_splash/image");448bool is_on = get_project_setting(p_preset, "application/boot_splash/fullsize");449// If custom logo is not specified, Godot does not scale default one, so we should do the same.450value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";451} break;452default: {453value = storyboard_image_scale_mode[image_scale_mode - 1];454}455}456457strnew += p_line.replace("$launch_screen_image_mode", value) + "\n";458} else if (p_line.contains("$launch_screen_background_color")) {459bool use_custom = p_preset->get("storyboard/use_custom_bg_color");460Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : get_project_setting(p_preset, "application/boot_splash/bg_color");461const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";462463Dictionary value_dictionary;464value_dictionary["red"] = color.r;465value_dictionary["green"] = color.g;466value_dictionary["blue"] = color.b;467value_dictionary["alpha"] = color.a;468String value = value_format.format(value_dictionary, "$_");469470strnew += p_line.replace("$launch_screen_background_color", value) + "\n";471472// OS Deployment Target473} else if (p_line.contains("$os_deployment_target")) {474String min_version = p_preset->get("application/min_" + get_platform_name() + "_version");475String value = "IPHONEOS_DEPLOYMENT_TARGET = " + min_version + ";";476strnew += p_line.replace("$os_deployment_target", value) + "\n";477478// Valid Archs479} else if (p_line.contains("$valid_archs")) {480strnew += p_line.replace("$valid_archs", "arm64 x86_64") + "\n";481482// Apple Embedded common483} else {484strnew += EditorExportPlatformAppleEmbedded::_process_config_file_line(p_preset, p_line, p_config, p_debug, p_code_signing);485}486487return strnew;488}489490491