Path: blob/master/editor/export/plugin_config_apple_embedded.cpp
9902 views
/**************************************************************************/1/* plugin_config_apple_embedded.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 "plugin_config_apple_embedded.h"3132#include "core/config/project_settings.h"33#include "core/io/dir_access.h"34#include "core/io/file_access.h"3536String PluginConfigAppleEmbedded::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {37String absolute_path;3839if (dependency_path.is_empty()) {40return absolute_path;41}4243if (dependency_path.is_absolute_path()) {44return dependency_path;45}4647String res_path = ProjectSettings::get_singleton()->globalize_path("res://");48absolute_path = plugin_config_dir.path_join(dependency_path);4950return absolute_path.replace(res_path, "res://");51}5253String PluginConfigAppleEmbedded::resolve_system_dependency_path(String dependency_path) {54String absolute_path;5556if (dependency_path.is_empty()) {57return absolute_path;58}5960if (dependency_path.is_absolute_path()) {61return dependency_path;62}6364String system_path = "/System/Library/Frameworks";6566return system_path.path_join(dependency_path);67}6869Vector<String> PluginConfigAppleEmbedded::resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) {70Vector<String> paths;7172for (int i = 0; i < p_paths.size(); i++) {73String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]);7475if (path.is_empty()) {76continue;77}7879paths.push_back(path);80}8182return paths;83}8485Vector<String> PluginConfigAppleEmbedded::resolve_system_dependencies(Vector<String> p_paths) {86Vector<String> paths;8788for (int i = 0; i < p_paths.size(); i++) {89String path = resolve_system_dependency_path(p_paths[i]);9091if (path.is_empty()) {92continue;93}9495paths.push_back(path);96}9798return paths;99}100101bool PluginConfigAppleEmbedded::validate_plugin(PluginConfigAppleEmbedded &plugin_config) {102bool valid_name = !plugin_config.name.is_empty();103bool valid_binary_name = !plugin_config.binary.is_empty();104bool valid_initialize = !plugin_config.initialization_method.is_empty();105bool valid_deinitialize = !plugin_config.deinitialization_method.is_empty();106107bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize;108109if (!fields_value) {110return false;111}112113String plugin_extension = plugin_config.binary.get_extension().to_lower();114115if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) ||116(plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) {117plugin_config.valid_config = true;118plugin_config.supports_targets = false;119} else {120String file_path = plugin_config.binary.get_base_dir();121String file_name = plugin_config.binary.get_basename().get_file();122String file_extension = plugin_config.binary.get_extension();123String release_file_name = file_path.path_join(file_name + ".release." + file_extension);124String debug_file_name = file_path.path_join(file_name + ".debug." + file_extension);125126if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) ||127(plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) {128plugin_config.valid_config = true;129plugin_config.supports_targets = true;130}131}132133return plugin_config.valid_config;134}135136String PluginConfigAppleEmbedded::get_plugin_main_binary(PluginConfigAppleEmbedded &plugin_config, bool p_debug) {137if (!plugin_config.supports_targets) {138return plugin_config.binary;139}140141String plugin_binary_dir = plugin_config.binary.get_base_dir();142String plugin_name_prefix = plugin_config.binary.get_basename().get_file();143String plugin_extension = plugin_config.binary.get_extension();144String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + "." + plugin_extension;145146return plugin_binary_dir.path_join(plugin_file);147}148149uint64_t PluginConfigAppleEmbedded::get_plugin_modification_time(const PluginConfigAppleEmbedded &plugin_config, const String &config_path) {150uint64_t last_updated = FileAccess::get_modified_time(config_path);151152if (!plugin_config.supports_targets) {153last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));154} else {155String file_path = plugin_config.binary.get_base_dir();156String file_name = plugin_config.binary.get_basename().get_file();157String plugin_extension = plugin_config.binary.get_extension();158String release_file_name = file_path.path_join(file_name + ".release." + plugin_extension);159String debug_file_name = file_path.path_join(file_name + ".debug." + plugin_extension);160161last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name));162last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name));163}164165return last_updated;166}167168PluginConfigAppleEmbedded PluginConfigAppleEmbedded::load_plugin_config(Ref<ConfigFile> config_file, const String &path) {169PluginConfigAppleEmbedded plugin_config = {};170171if (config_file.is_null()) {172return plugin_config;173}174175config_file->clear();176177Error err = config_file->load(path);178179if (err != OK) {180return plugin_config;181}182183String config_base_dir = path.get_base_dir();184185plugin_config.name = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_NAME_KEY, String());186plugin_config.use_swift_runtime = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_USE_SWIFT_KEY, false);187plugin_config.initialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_INITIALIZE_KEY, String());188plugin_config.deinitialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_DEINITIALIZE_KEY, String());189190String binary_path = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_BINARY_KEY, String());191plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);192193if (config_file->has_section(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION)) {194Vector<String> linked_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKED_KEY, Vector<String>());195Vector<String> embedded_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());196Vector<String> system_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_SYSTEM_KEY, Vector<String>());197Vector<String> files = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_FILES_KEY, Vector<String>());198199plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);200plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);201plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);202203plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);204205plugin_config.capabilities = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());206207plugin_config.linker_flags = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKER_FLAGS, Vector<String>());208}209210if (config_file->has_section(PluginConfigAppleEmbedded::PLIST_SECTION)) {211Vector<String> keys = config_file->get_section_keys(PluginConfigAppleEmbedded::PLIST_SECTION);212213for (const String &key : keys) {214Vector<String> key_components = key.split(":");215216String key_value = "";217PluginConfigAppleEmbedded::PlistItemType key_type = PluginConfigAppleEmbedded::PlistItemType::UNKNOWN;218219if (key_components.size() == 1) {220key_value = key_components[0];221key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;222} else if (key_components.size() == 2) {223key_value = key_components[0];224225if (key_components[1].to_lower() == "string") {226key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;227} else if (key_components[1].to_lower() == "integer") {228key_type = PluginConfigAppleEmbedded::PlistItemType::INTEGER;229} else if (key_components[1].to_lower() == "boolean") {230key_type = PluginConfigAppleEmbedded::PlistItemType::BOOLEAN;231} else if (key_components[1].to_lower() == "raw") {232key_type = PluginConfigAppleEmbedded::PlistItemType::RAW;233} else if (key_components[1].to_lower() == "string_input") {234key_type = PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT;235}236}237238if (key_value.is_empty() || key_type == PluginConfigAppleEmbedded::PlistItemType::UNKNOWN) {239continue;240}241242String value;243244switch (key_type) {245case PluginConfigAppleEmbedded::PlistItemType::STRING: {246String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());247value = "<string>" + raw_value + "</string>";248} break;249case PluginConfigAppleEmbedded::PlistItemType::INTEGER: {250int raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, 0);251Dictionary value_dictionary;252String value_format = "<integer>$value</integer>";253value_dictionary["value"] = raw_value;254value = value_format.format(value_dictionary, "$_");255} break;256case PluginConfigAppleEmbedded::PlistItemType::BOOLEAN:257if (config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, false)) {258value = "<true/>";259} else {260value = "<false/>";261}262break;263case PluginConfigAppleEmbedded::PlistItemType::RAW: {264String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());265value = raw_value;266} break;267case PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT: {268String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());269value = raw_value;270} break;271default:272continue;273}274275plugin_config.plist[key_value] = PluginConfigAppleEmbedded::PlistItem{ key_type, value };276}277}278279if (validate_plugin(plugin_config)) {280plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);281}282283return plugin_config;284}285286287