Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

17.3 KB
Raw Blame
/**************************************************************************/
/*  export_plugin.cpp                                                     */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "export_plugin.h"

#include "logo_svg.gen.h"
#include "run_icon_svg.gen.h"

#include "editor/editor_node.h"
#include "main/splash.gen.h"

Vector<String> EditorExportPlatformIOS::device_types({ "iPhone", "iPad" });

void EditorExportPlatformIOS::initialize() {
	if (EditorNode::get_singleton()) {
		EditorExportPlatformAppleEmbedded::_initialize(_ios_logo_svg, _ios_run_icon_svg);
#ifdef MACOS_ENABLED
		_start_remote_device_poller_thread();
#endif
	}
}

EditorExportPlatformIOS::~EditorExportPlatformIOS() {
#ifdef MACOS_ENABLED
	_stop_remote_device_poller_thread();
#endif
}

void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
	EditorExportPlatformAppleEmbedded::get_export_options(r_options);

	r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), get_minimum_deployment_target()));

	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));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
	r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));

	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_gaming_tier"), false));
	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/performance_a12"), false));
}

bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
	bool valid = EditorExportPlatformAppleEmbedded::has_valid_export_configuration(p_preset, r_error, r_missing_templates, p_debug);

	String err;
	String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
	String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver." + get_platform_name());
	if ((rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "metal") {
		float version = p_preset->get("application/min_ios_version").operator String().to_float();
		if (version < 14.0) {
			err += TTR("Metal renderer require iOS 14+.") + "\n";
		}
	}

	if (!err.is_empty()) {
		if (!r_error.is_empty()) {
			r_error += err;
		} else {
			r_error = err;
		}
	}

	return valid;
}

bool EditorExportPlatformIOS::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
	if (p_preset) {
		if (p_option == "capabilities/performance_a12") {
			String rendering_method = get_project_setting(Ref<EditorExportPreset>(p_preset), "rendering/renderer/rendering_method.mobile");
			return !(rendering_method == "forward_plus" || rendering_method == "mobile");
		}
	}
	return EditorExportPlatformAppleEmbedded::get_export_option_visibility(p_preset, p_option);
}

HashMap<String, Variant> EditorExportPlatformIOS::get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const {
	HashMap<String, Variant> settings;

	int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
	String value;

	switch (image_scale_mode) {
		case 0: {
			String logo_path = get_project_setting(p_preset, "application/boot_splash/image");
			RSE::SplashStretchMode stretch_mode = get_project_setting(p_preset, "application/boot_splash/stretch_mode");
			// If custom logo is not specified, Godot does not scale default one, so we should do the same.
			if (logo_path.is_empty()) {
				value = "center";
			} else {
				switch (stretch_mode) {
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_DISABLED: {
						value = "center";
					} break;
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP: {
						value = "scaleAspectFit";
					} break;
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_WIDTH: {
						value = "scaleAspectFit";
					} break;
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_HEIGHT: {
						value = "scaleAspectFit";
					} break;
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_COVER: {
						value = "scaleAspectFill";
					} break;
					case RSE::SplashStretchMode::SPLASH_STRETCH_MODE_IGNORE: {
						value = "scaleToFill";
					} break;
				}
			}
		} break;
		default: {
			value = storyboard_image_scale_mode[image_scale_mode - 1];
		}
	}
	settings["ios/launch_screen_image_mode"] = value;
	return settings;
}

Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
	const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
	const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");

	if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
		String image_path = p_dest_dir.path_join("[email protected]");
		Error err = OK;
		Ref<Image> image = _load_icon_or_splash_image(custom_launch_image_2x, &err);

		if (err != OK || image.is_null() || image->is_empty()) {
			return err;
		}

		if (image->save_png(image_path) != OK) {
			return ERR_FILE_CANT_WRITE;
		}

		image_path = p_dest_dir.path_join("[email protected]");
		image = _load_icon_or_splash_image(custom_launch_image_3x, &err);

		if (err != OK || image.is_null() || image->is_empty()) {
			return err;
		}

		if (image->save_png(image_path) != OK) {
			return ERR_FILE_CANT_WRITE;
		}
	} else {
		Error err = OK;
		Ref<Image> splash;

		const String splash_path = get_project_setting(p_preset, "application/boot_splash/image");

		if (!splash_path.is_empty()) {
			splash = _load_icon_or_splash_image(splash_path, &err);
		}

		if (err != OK || splash.is_null() || splash->is_empty()) {
			splash.instantiate(boot_splash_png, sizeof(boot_splash_png));
		}

		// Using same image for both @2x and @3x
		// because Godot's own boot logo uses single image for all resolutions.
		// Also not using @1x image, because devices using this image variant
		// are not supported by iOS 9, which is minimal target.
		const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
		const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");

		if (splash->save_png(splash_png_path_2x) != OK) {
			return ERR_FILE_CANT_WRITE;
		}

		if (splash->save_png(splash_png_path_3x) != OK) {
			return ERR_FILE_CANT_WRITE;
		}
	}

	return OK;
}

Vector<EditorExportPlatformAppleEmbedded::IconInfo> EditorExportPlatformIOS::get_icon_infos() const {
	return {
		// Settings on iPhone, iPad Pro, iPad, iPad mini
		{ PNAME("icons/settings_58x58"), "universal", "Icon-58", "58", "2x", "29x29", false },
		{ PNAME("icons/settings_87x87"), "universal", "Icon-87", "87", "3x", "29x29", false },

		// Notifications on iPhone, iPad Pro, iPad, iPad mini
		{ PNAME("icons/notification_40x40"), "universal", "Icon-40", "40", "2x", "20x20", false },
		{ PNAME("icons/notification_60x60"), "universal", "Icon-60", "60", "3x", "20x20", false },
		{ PNAME("icons/notification_76x76"), "universal", "Icon-76", "76", "2x", "38x38", false },
		{ PNAME("icons/notification_114x114"), "universal", "Icon-114", "114", "3x", "38x38", false },

		// Spotlight on iPhone, iPad Pro, iPad, iPad mini
		{ PNAME("icons/spotlight_80x80"), "universal", "Icon-80", "80", "2x", "40x40", false },
		{ PNAME("icons/spotlight_120x120"), "universal", "Icon-120", "120", "3x", "40x40", false },

		// Home Screen on iPhone
		{ PNAME("icons/iphone_120x120"), "universal", "Icon-120-1", "120", "2x", "60x60", false },
		{ PNAME("icons/iphone_180x180"), "universal", "Icon-180", "180", "3x", "60x60", false },

		// Home Screen on iPad Pro
		{ PNAME("icons/ipad_167x167"), "universal", "Icon-167", "167", "2x", "83.5x83.5", false },

		// Home Screen on iPad, iPad mini
		{ PNAME("icons/ipad_152x152"), "universal", "Icon-152", "152", "2x", "76x76", false },

		{ PNAME("icons/ios_128x128"), "universal", "Icon-128", "128", "2x", "64x64", false },
		{ PNAME("icons/ios_192x192"), "universal", "Icon-192", "192", "3x", "64x64", false },

		{ PNAME("icons/ios_136x136"), "universal", "Icon-136", "136", "2x", "68x68", false },

		// App Store
		{ PNAME("icons/app_store_1024x1024"), "universal", "Icon-1024", "1024", "1x", "1024x1024", true },
	};
}

void EditorExportPlatformIOS::get_required_device_capabilities(const Ref<EditorExportPreset> &p_preset, Vector<String> &r_capabilities) const {
	EditorExportPlatformAppleEmbedded::get_required_device_capabilities(p_preset, r_capabilities);
	String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
	if ((bool)p_preset->get("capabilities/performance_gaming_tier") && !r_capabilities.has("iphone-performance-gaming-tier")) {
		r_capabilities.push_back("iphone-performance-gaming-tier");
	}
	if (((bool)p_preset->get("capabilities/performance_a12") || rendering_method == "forward_plus" || rendering_method == "mobile") && !r_capabilities.has("iphone-ipad-minimum-performance-a12")) {
		r_capabilities.push_back("iphone-ipad-minimum-performance-a12");
	}
}

String 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) {
	// Do iOS specific processing first, and call super implementation if there are no matches

	String strnew;

	// Supported Destinations
	if (p_line.contains("$targeted_device_family")) {
		String xcode_value;
		switch ((int)p_preset->get("application/targeted_device_family")) {
			case 0: // iPhone
				xcode_value = "1";
				break;
			case 1: // iPad
				xcode_value = "2";
				break;
			case 2: // iPhone & iPad
				xcode_value = "1,2";
				break;
		}
		strnew += p_line.replace("$targeted_device_family", xcode_value) + "\n";

		// MoltenVK Framework
	} else if (p_line.contains("$moltenvk_buildfile")) {
		String value = p_config.has_moltenvk ? "9039D3BE24C093AC0020482C /* MoltenVK.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9039D3BD24C093AC0020482C /* MoltenVK.xcframework */; };" : "";
		strnew += p_line.replace("$moltenvk_buildfile", value) + "\n";
	} else if (p_line.contains("$moltenvk_fileref")) {
		String value = p_config.has_moltenvk ? "9039D3BD24C093AC0020482C /* MoltenVK.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = MoltenVK; path = MoltenVK.xcframework; sourceTree = \"<group>\"; };" : "";
		strnew += p_line.replace("$moltenvk_fileref", value) + "\n";
	} else if (p_line.contains("$moltenvk_buildphase")) {
		String value = p_config.has_moltenvk ? "9039D3BE24C093AC0020482C /* MoltenVK.xcframework in Frameworks */," : "";
		strnew += p_line.replace("$moltenvk_buildphase", value) + "\n";
	} else if (p_line.contains("$moltenvk_buildgrp")) {
		String value = p_config.has_moltenvk ? "9039D3BD24C093AC0020482C /* MoltenVK.xcframework */," : "";
		strnew += p_line.replace("$moltenvk_buildgrp", value) + "\n";

		// Launch Storyboard
	} else if (p_line.contains("$plist_launch_screen_name")) {
		String value = "<key>UILaunchStoryboardName</key>\n<string>Launch Screen</string>";
		strnew += p_line.replace("$plist_launch_screen_name", value) + "\n";
	} else if (p_line.contains("$pbx_launch_screen_file_reference")) {
		String value = "90DD2D9D24B36E8000717FE1 = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = \"Launch Screen.storyboard\"; sourceTree = \"<group>\"; };";
		strnew += p_line.replace("$pbx_launch_screen_file_reference", value) + "\n";
	} else if (p_line.contains("$pbx_launch_screen_copy_files")) {
		String value = "90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */,";
		strnew += p_line.replace("$pbx_launch_screen_copy_files", value) + "\n";
	} else if (p_line.contains("$pbx_launch_screen_build_phase")) {
		String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */,";
		strnew += p_line.replace("$pbx_launch_screen_build_phase", value) + "\n";
	} else if (p_line.contains("$pbx_launch_screen_build_reference")) {
		String value = "90DD2D9E24B36E8000717FE1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 90DD2D9D24B36E8000717FE1 /* Launch Screen.storyboard */; };";
		strnew += p_line.replace("$pbx_launch_screen_build_reference", value) + "\n";

		// Launch Storyboard customization
	} else if (p_line.contains("$launch_screen_image_mode")) {
		String value = get_custom_project_settings(p_preset)["ios/launch_screen_image_mode"];
		strnew += p_line.replace("$launch_screen_image_mode", value).replace("$launch_screen_image_file_name", launch_screen_image_file_name) + "\n";
	} else if (p_line.contains("$launch_screen_image_file_name")) {
		strnew += p_line.replace("$launch_screen_image_file_name", launch_screen_image_file_name) + "\n";
	} else if (p_line.contains("$launch_screen_background_color")) {
		bool use_custom = p_preset->get("storyboard/use_custom_bg_color");
		Color color = use_custom ? p_preset->get("storyboard/custom_bg_color") : get_project_setting(p_preset, "application/boot_splash/bg_color");
		const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";

		Dictionary value_dictionary;
		value_dictionary["red"] = color.r;
		value_dictionary["green"] = color.g;
		value_dictionary["blue"] = color.b;
		value_dictionary["alpha"] = color.a;
		String value = value_format.format(value_dictionary, "$_");

		strnew += p_line.replace("$launch_screen_background_color", value) + "\n";

		// OS Deployment Target
	} else if (p_line.contains("$os_deployment_target")) {
		String min_version = p_preset->get("application/min_" + get_platform_name() + "_version");
		String value = "IPHONEOS_DEPLOYMENT_TARGET = " + min_version + ";";
		strnew += p_line.replace("$os_deployment_target", value) + "\n";

		// Valid Archs
	} else if (p_line.contains("$valid_archs")) {
		strnew += p_line.replace("$valid_archs", "arm64 x86_64") + "\n";

		// Application Scene Manifest - Default Session Role
	} else if (p_line.contains("$application_scene_manifest_default_session_role")) {
		strnew += p_line.replace("$application_scene_manifest_default_session_role", "") + "\n";

		// Application Scene Manifest - Immersive Configuration
	} else if (p_line.contains("$application_scene_manifest_immersive_configuration")) {
		strnew += p_line.replace("$application_scene_manifest_immersive_configuration", "") + "\n";

		// Info.plist NSHandsTrackingUsageDescription
	} else if (p_line.contains("$hand_tracking_usage_description")) {
		strnew += p_line.replace("$hand_tracking_usage_description", "") + "\n";

		// Info.plist NSAccessoryTrackingUsageDescription
	} else if (p_line.contains("$accessory_tracking_usage_description")) {
		strnew += p_line.replace("$accessory_tracking_usage_description", "") + "\n";

		// Apple Embedded common
	} else {
		strnew += EditorExportPlatformAppleEmbedded::_process_config_file_line(p_preset, p_line, p_config, p_debug, p_code_signing);
	}

	return strnew;
}