| 1 | /**************************************************************************/ |
| 2 | /* godot_plugin_config.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "godot_plugin_config.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/dir_access.h" |
| 35 | #include "core/io/file_access.h" |
| 36 | |
| 37 | String PluginConfigIOS::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) { |
| 38 | String absolute_path; |
| 39 | |
| 40 | if (dependency_path.is_empty()) { |
| 41 | return absolute_path; |
| 42 | } |
| 43 | |
| 44 | if (dependency_path.is_absolute_path()) { |
| 45 | return dependency_path; |
| 46 | } |
| 47 | |
| 48 | String res_path = ProjectSettings::get_singleton()->globalize_path("res://" ); |
| 49 | absolute_path = plugin_config_dir.path_join(dependency_path); |
| 50 | |
| 51 | return absolute_path.replace(res_path, "res://" ); |
| 52 | } |
| 53 | |
| 54 | String PluginConfigIOS::resolve_system_dependency_path(String dependency_path) { |
| 55 | String absolute_path; |
| 56 | |
| 57 | if (dependency_path.is_empty()) { |
| 58 | return absolute_path; |
| 59 | } |
| 60 | |
| 61 | if (dependency_path.is_absolute_path()) { |
| 62 | return dependency_path; |
| 63 | } |
| 64 | |
| 65 | String system_path = "/System/Library/Frameworks" ; |
| 66 | |
| 67 | return system_path.path_join(dependency_path); |
| 68 | } |
| 69 | |
| 70 | Vector<String> PluginConfigIOS::resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) { |
| 71 | Vector<String> paths; |
| 72 | |
| 73 | for (int i = 0; i < p_paths.size(); i++) { |
| 74 | String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]); |
| 75 | |
| 76 | if (path.is_empty()) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | paths.push_back(path); |
| 81 | } |
| 82 | |
| 83 | return paths; |
| 84 | } |
| 85 | |
| 86 | Vector<String> PluginConfigIOS::resolve_system_dependencies(Vector<String> p_paths) { |
| 87 | Vector<String> paths; |
| 88 | |
| 89 | for (int i = 0; i < p_paths.size(); i++) { |
| 90 | String path = resolve_system_dependency_path(p_paths[i]); |
| 91 | |
| 92 | if (path.is_empty()) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | paths.push_back(path); |
| 97 | } |
| 98 | |
| 99 | return paths; |
| 100 | } |
| 101 | |
| 102 | bool PluginConfigIOS::validate_plugin(PluginConfigIOS &plugin_config) { |
| 103 | bool valid_name = !plugin_config.name.is_empty(); |
| 104 | bool valid_binary_name = !plugin_config.binary.is_empty(); |
| 105 | bool valid_initialize = !plugin_config.initialization_method.is_empty(); |
| 106 | bool valid_deinitialize = !plugin_config.deinitialization_method.is_empty(); |
| 107 | |
| 108 | bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize; |
| 109 | |
| 110 | if (!fields_value) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | String plugin_extension = plugin_config.binary.get_extension().to_lower(); |
| 115 | |
| 116 | if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) || |
| 117 | (plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) { |
| 118 | plugin_config.valid_config = true; |
| 119 | plugin_config.supports_targets = false; |
| 120 | } else { |
| 121 | String file_path = plugin_config.binary.get_base_dir(); |
| 122 | String file_name = plugin_config.binary.get_basename().get_file(); |
| 123 | String file_extension = plugin_config.binary.get_extension(); |
| 124 | String release_file_name = file_path.path_join(file_name + ".release." + file_extension); |
| 125 | String debug_file_name = file_path.path_join(file_name + ".debug." + file_extension); |
| 126 | |
| 127 | if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) || |
| 128 | (plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) { |
| 129 | plugin_config.valid_config = true; |
| 130 | plugin_config.supports_targets = true; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return plugin_config.valid_config; |
| 135 | } |
| 136 | |
| 137 | String PluginConfigIOS::get_plugin_main_binary(PluginConfigIOS &plugin_config, bool p_debug) { |
| 138 | if (!plugin_config.supports_targets) { |
| 139 | return plugin_config.binary; |
| 140 | } |
| 141 | |
| 142 | String plugin_binary_dir = plugin_config.binary.get_base_dir(); |
| 143 | String plugin_name_prefix = plugin_config.binary.get_basename().get_file(); |
| 144 | String plugin_extension = plugin_config.binary.get_extension(); |
| 145 | String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release" ) + "." + plugin_extension; |
| 146 | |
| 147 | return plugin_binary_dir.path_join(plugin_file); |
| 148 | } |
| 149 | |
| 150 | uint64_t PluginConfigIOS::get_plugin_modification_time(const PluginConfigIOS &plugin_config, const String &config_path) { |
| 151 | uint64_t last_updated = FileAccess::get_modified_time(config_path); |
| 152 | |
| 153 | if (!plugin_config.supports_targets) { |
| 154 | last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary)); |
| 155 | } else { |
| 156 | String file_path = plugin_config.binary.get_base_dir(); |
| 157 | String file_name = plugin_config.binary.get_basename().get_file(); |
| 158 | String plugin_extension = plugin_config.binary.get_extension(); |
| 159 | String release_file_name = file_path.path_join(file_name + ".release." + plugin_extension); |
| 160 | String debug_file_name = file_path.path_join(file_name + ".debug." + plugin_extension); |
| 161 | |
| 162 | last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name)); |
| 163 | last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name)); |
| 164 | } |
| 165 | |
| 166 | return last_updated; |
| 167 | } |
| 168 | |
| 169 | PluginConfigIOS PluginConfigIOS::load_plugin_config(Ref<ConfigFile> config_file, const String &path) { |
| 170 | PluginConfigIOS plugin_config = {}; |
| 171 | |
| 172 | if (!config_file.is_valid()) { |
| 173 | return plugin_config; |
| 174 | } |
| 175 | |
| 176 | config_file->clear(); |
| 177 | |
| 178 | Error err = config_file->load(path); |
| 179 | |
| 180 | if (err != OK) { |
| 181 | return plugin_config; |
| 182 | } |
| 183 | |
| 184 | String config_base_dir = path.get_base_dir(); |
| 185 | |
| 186 | plugin_config.name = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_NAME_KEY, String()); |
| 187 | plugin_config.use_swift_runtime = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_USE_SWIFT_KEY, false); |
| 188 | plugin_config.initialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_INITIALIZE_KEY, String()); |
| 189 | plugin_config.deinitialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_DEINITIALIZE_KEY, String()); |
| 190 | |
| 191 | String binary_path = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_BINARY_KEY, String()); |
| 192 | plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path); |
| 193 | |
| 194 | if (config_file->has_section(PluginConfigIOS::DEPENDENCIES_SECTION)) { |
| 195 | Vector<String> linked_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKED_KEY, Vector<String>()); |
| 196 | Vector<String> embedded_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_EMBEDDED_KEY, Vector<String>()); |
| 197 | Vector<String> system_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_SYSTEM_KEY, Vector<String>()); |
| 198 | Vector<String> files = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_FILES_KEY, Vector<String>()); |
| 199 | |
| 200 | plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies); |
| 201 | plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies); |
| 202 | plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies); |
| 203 | |
| 204 | plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files); |
| 205 | |
| 206 | plugin_config.capabilities = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>()); |
| 207 | |
| 208 | plugin_config.linker_flags = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKER_FLAGS, Vector<String>()); |
| 209 | } |
| 210 | |
| 211 | if (config_file->has_section(PluginConfigIOS::PLIST_SECTION)) { |
| 212 | List<String> keys; |
| 213 | config_file->get_section_keys(PluginConfigIOS::PLIST_SECTION, &keys); |
| 214 | |
| 215 | for (int i = 0; i < keys.size(); i++) { |
| 216 | Vector<String> key_components = keys[i].split(":" ); |
| 217 | |
| 218 | String key_value = "" ; |
| 219 | PluginConfigIOS::PlistItemType key_type = PluginConfigIOS::PlistItemType::UNKNOWN; |
| 220 | |
| 221 | if (key_components.size() == 1) { |
| 222 | key_value = key_components[0]; |
| 223 | key_type = PluginConfigIOS::PlistItemType::STRING; |
| 224 | } else if (key_components.size() == 2) { |
| 225 | key_value = key_components[0]; |
| 226 | |
| 227 | if (key_components[1].to_lower() == "string" ) { |
| 228 | key_type = PluginConfigIOS::PlistItemType::STRING; |
| 229 | } else if (key_components[1].to_lower() == "integer" ) { |
| 230 | key_type = PluginConfigIOS::PlistItemType::INTEGER; |
| 231 | } else if (key_components[1].to_lower() == "boolean" ) { |
| 232 | key_type = PluginConfigIOS::PlistItemType::BOOLEAN; |
| 233 | } else if (key_components[1].to_lower() == "raw" ) { |
| 234 | key_type = PluginConfigIOS::PlistItemType::RAW; |
| 235 | } else if (key_components[1].to_lower() == "string_input" ) { |
| 236 | key_type = PluginConfigIOS::PlistItemType::STRING_INPUT; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (key_value.is_empty() || key_type == PluginConfigIOS::PlistItemType::UNKNOWN) { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | String value; |
| 245 | |
| 246 | switch (key_type) { |
| 247 | case PluginConfigIOS::PlistItemType::STRING: { |
| 248 | String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], String()); |
| 249 | value = "<string>" + raw_value + "</string>" ; |
| 250 | } break; |
| 251 | case PluginConfigIOS::PlistItemType::INTEGER: { |
| 252 | int raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], 0); |
| 253 | Dictionary value_dictionary; |
| 254 | String value_format = "<integer>$value</integer>" ; |
| 255 | value_dictionary["value" ] = raw_value; |
| 256 | value = value_format.format(value_dictionary, "$_" ); |
| 257 | } break; |
| 258 | case PluginConfigIOS::PlistItemType::BOOLEAN: |
| 259 | if (config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], false)) { |
| 260 | value = "<true/>" ; |
| 261 | } else { |
| 262 | value = "<false/>" ; |
| 263 | } |
| 264 | break; |
| 265 | case PluginConfigIOS::PlistItemType::RAW: { |
| 266 | String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], String()); |
| 267 | value = raw_value; |
| 268 | } break; |
| 269 | case PluginConfigIOS::PlistItemType::STRING_INPUT: { |
| 270 | String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], String()); |
| 271 | value = raw_value; |
| 272 | } break; |
| 273 | default: |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | plugin_config.plist[key_value] = PluginConfigIOS::PlistItem{ key_type, value }; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (validate_plugin(plugin_config)) { |
| 282 | plugin_config.last_updated = get_plugin_modification_time(plugin_config, path); |
| 283 | } |
| 284 | |
| 285 | return plugin_config; |
| 286 | } |
| 287 | |