Path: blob/master/editor/export/plugin_config_apple_embedded.cpp
20843 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.initialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_INITIALIZE_KEY, String());187plugin_config.deinitialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_DEINITIALIZE_KEY, String());188189String binary_path = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_BINARY_KEY, String());190plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);191192if (config_file->has_section(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION)) {193Vector<String> linked_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKED_KEY, Vector<String>());194Vector<String> embedded_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());195Vector<String> system_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_SYSTEM_KEY, Vector<String>());196Vector<String> files = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_FILES_KEY, Vector<String>());197198plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);199plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);200plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);201202plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);203204plugin_config.capabilities = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());205206plugin_config.linker_flags = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKER_FLAGS, Vector<String>());207}208209if (config_file->has_section(PluginConfigAppleEmbedded::PLIST_SECTION)) {210Vector<String> keys = config_file->get_section_keys(PluginConfigAppleEmbedded::PLIST_SECTION);211212for (const String &key : keys) {213Vector<String> key_components = key.split(":");214215String key_value = "";216PluginConfigAppleEmbedded::PlistItemType key_type = PluginConfigAppleEmbedded::PlistItemType::UNKNOWN;217218if (key_components.size() == 1) {219key_value = key_components[0];220key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;221} else if (key_components.size() == 2) {222key_value = key_components[0];223224if (key_components[1].to_lower() == "string") {225key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;226} else if (key_components[1].to_lower() == "integer") {227key_type = PluginConfigAppleEmbedded::PlistItemType::INTEGER;228} else if (key_components[1].to_lower() == "boolean") {229key_type = PluginConfigAppleEmbedded::PlistItemType::BOOLEAN;230} else if (key_components[1].to_lower() == "raw") {231key_type = PluginConfigAppleEmbedded::PlistItemType::RAW;232} else if (key_components[1].to_lower() == "string_input") {233key_type = PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT;234}235}236237if (key_value.is_empty() || key_type == PluginConfigAppleEmbedded::PlistItemType::UNKNOWN) {238continue;239}240241String value;242243switch (key_type) {244case PluginConfigAppleEmbedded::PlistItemType::STRING: {245String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());246value = "<string>" + raw_value + "</string>";247} break;248case PluginConfigAppleEmbedded::PlistItemType::INTEGER: {249int raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, 0);250Dictionary value_dictionary;251String value_format = "<integer>$value</integer>";252value_dictionary["value"] = raw_value;253value = value_format.format(value_dictionary, "$_");254} break;255case PluginConfigAppleEmbedded::PlistItemType::BOOLEAN:256if (config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, false)) {257value = "<true/>";258} else {259value = "<false/>";260}261break;262case PluginConfigAppleEmbedded::PlistItemType::RAW: {263String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());264value = raw_value;265} break;266case PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT: {267String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());268value = raw_value;269} break;270default:271continue;272}273274plugin_config.plist[key_value] = PluginConfigAppleEmbedded::PlistItem{ key_type, value };275}276}277278if (validate_plugin(plugin_config)) {279plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);280}281282return plugin_config;283}284285286