Path: blob/master/core/extension/gdextension_library_loader.cpp
9903 views
/**************************************************************************/1/* gdextension_library_loader.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 "gdextension_library_loader.h"3132#include "core/config/project_settings.h"33#include "core/io/dir_access.h"34#include "core/version.h"35#include "gdextension.h"3637Vector<SharedObject> GDExtensionLibraryLoader::find_extension_dependencies(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature) {38Vector<SharedObject> dependencies_shared_objects;39if (p_config->has_section("dependencies")) {40Vector<String> config_dependencies = p_config->get_section_keys("dependencies");4142for (const String &dependency : config_dependencies) {43Vector<String> dependency_tags = dependency.split(".");44bool all_tags_met = true;45for (int i = 0; i < dependency_tags.size(); i++) {46String tag = dependency_tags[i].strip_edges();47if (!p_has_feature(tag)) {48all_tags_met = false;49break;50}51}5253if (all_tags_met) {54Dictionary dependency_value = p_config->get_value("dependencies", dependency);55for (const Variant *key = dependency_value.next(nullptr); key; key = dependency_value.next(key)) {56String dependency_path = *key;57String target_path = dependency_value[*key];58if (dependency_path.is_relative_path()) {59dependency_path = p_path.get_base_dir().path_join(dependency_path);60}61dependencies_shared_objects.push_back(SharedObject(dependency_path, dependency_tags, target_path));62}63break;64}65}66}6768return dependencies_shared_objects;69}7071String GDExtensionLibraryLoader::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {72// First, check the explicit libraries.73if (p_config->has_section("libraries")) {74Vector<String> libraries = p_config->get_section_keys("libraries");7576// Iterate the libraries, finding the best matching tags.77String best_library_path;78Vector<String> best_library_tags;79for (const String &E : libraries) {80Vector<String> tags = E.split(".");81bool all_tags_met = true;82for (int i = 0; i < tags.size(); i++) {83String tag = tags[i].strip_edges();84if (!p_has_feature(tag)) {85all_tags_met = false;86break;87}88}8990if (all_tags_met && tags.size() > best_library_tags.size()) {91best_library_path = p_config->get_value("libraries", E);92best_library_tags = tags;93}94}9596if (!best_library_path.is_empty()) {97if (best_library_path.is_relative_path()) {98best_library_path = p_path.get_base_dir().path_join(best_library_path);99}100if (r_tags != nullptr) {101r_tags->append_array(best_library_tags);102}103return best_library_path;104}105}106107// Second, try to autodetect.108String autodetect_library_prefix;109if (p_config->has_section_key("configuration", "autodetect_library_prefix")) {110autodetect_library_prefix = p_config->get_value("configuration", "autodetect_library_prefix");111}112if (!autodetect_library_prefix.is_empty()) {113String autodetect_path = autodetect_library_prefix;114if (autodetect_path.is_relative_path()) {115autodetect_path = p_path.get_base_dir().path_join(autodetect_path);116}117118// Find the folder and file parts of the prefix.119String folder;120String file_prefix;121if (DirAccess::dir_exists_absolute(autodetect_path)) {122folder = autodetect_path;123} else if (DirAccess::dir_exists_absolute(autodetect_path.get_base_dir())) {124folder = autodetect_path.get_base_dir();125file_prefix = autodetect_path.get_file();126} else {127ERR_FAIL_V_MSG(String(), vformat("Error in extension: %s. Could not find folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));128}129130// Open the folder.131Ref<DirAccess> dir = DirAccess::open(folder);132ERR_FAIL_COND_V_MSG(dir.is_null(), String(), vformat("Error in extension: %s. Could not open folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));133134// Iterate the files and check the prefixes, finding the best matching file.135String best_file;136Vector<String> best_file_tags;137dir->list_dir_begin();138String file_name = dir->_get_next();139while (file_name != "") {140if (!dir->current_is_dir() && file_name.begins_with(file_prefix)) {141// Check if the files matches all requested feature tags.142String tags_str = file_name.trim_prefix(file_prefix);143tags_str = tags_str.trim_suffix(tags_str.get_extension());144145Vector<String> tags = tags_str.split(".", false);146bool all_tags_met = true;147for (int i = 0; i < tags.size(); i++) {148String tag = tags[i].strip_edges();149if (!p_has_feature(tag)) {150all_tags_met = false;151break;152}153}154155// If all tags are found in the feature list, and we found more tags than before, use this file.156if (all_tags_met && tags.size() > best_file_tags.size()) {157best_file_tags = tags;158best_file = file_name;159}160}161file_name = dir->_get_next();162}163164if (!best_file.is_empty()) {165String library_path = folder.path_join(best_file);166if (r_tags != nullptr) {167r_tags->append_array(best_file_tags);168}169return library_path;170}171}172return String();173}174175Error GDExtensionLibraryLoader::open_library(const String &p_path) {176Error err = parse_gdextension_file(p_path);177if (err != OK) {178return err;179}180181String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);182183Vector<String> abs_dependencies_paths;184if (!library_dependencies.is_empty()) {185for (const SharedObject &dependency : library_dependencies) {186abs_dependencies_paths.push_back(ProjectSettings::get_singleton()->globalize_path(dependency.path));187}188}189190OS::GDExtensionData data = {191true, // also_set_library_path192&library_path, // r_resolved_path193Engine::get_singleton()->is_editor_hint(), // generate_temp_files194&abs_dependencies_paths, // library_dependencies195};196197err = OS::get_singleton()->open_dynamic_library(is_static_library ? String() : abs_path, library, &data);198if (err != OK) {199return err;200}201202return OK;203}204205Error GDExtensionLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {206#ifdef TOOLS_ENABLED207p_extension->set_reloadable(is_reloadable && Engine::get_singleton()->is_extension_reloading_enabled());208#endif209210for (const KeyValue<String, String> &icon : class_icon_paths) {211p_extension->class_icon_paths[icon.key] = icon.value;212}213214void *entry_funcptr = nullptr;215216Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, entry_symbol, entry_funcptr, false);217218if (err != OK) {219ERR_PRINT(vformat("GDExtension entry point '%s' not found in library %s.", entry_symbol, library_path));220return err;221}222223GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;224225GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);226227if (ret) {228return OK;229} else {230ERR_PRINT(vformat("GDExtension initialization function '%s' returned an error.", entry_symbol));231return FAILED;232}233}234235void GDExtensionLibraryLoader::close_library() {236OS::get_singleton()->close_dynamic_library(library);237library = nullptr;238}239240bool GDExtensionLibraryLoader::is_library_open() const {241return library != nullptr;242}243244bool GDExtensionLibraryLoader::has_library_changed() const {245#ifdef TOOLS_ENABLED246// Check only that the last modified time is different (rather than checking247// that it's newer) since some OS's (namely Windows) will preserve the modified248// time by default when copying files.249if (FileAccess::get_modified_time(resource_path) != resource_last_modified_time) {250return true;251}252if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {253return true;254}255#endif256return false;257}258259bool GDExtensionLibraryLoader::library_exists() const {260return FileAccess::exists(resource_path);261}262263Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {264resource_path = p_path;265266Ref<ConfigFile> config;267config.instantiate();268269Error err = config->load(p_path);270271if (err != OK) {272ERR_PRINT(vformat("Error loading GDExtension configuration file: '%s'.", p_path));273return err;274}275276if (!config->has_section_key("configuration", "entry_symbol")) {277ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: '%s'.", p_path));278return ERR_INVALID_DATA;279}280281entry_symbol = config->get_value("configuration", "entry_symbol");282283uint32_t compatibility_minimum[3] = { 0, 0, 0 };284if (config->has_section_key("configuration", "compatibility_minimum")) {285String compat_string = config->get_value("configuration", "compatibility_minimum");286Vector<int> parts = compat_string.split_ints(".");287for (int i = 0; i < parts.size(); i++) {288if (i >= 3) {289break;290}291if (parts[i] >= 0) {292compatibility_minimum[i] = parts[i];293}294}295} else {296ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: '%s'.", p_path));297return ERR_INVALID_DATA;298}299300if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {301ERR_PRINT(vformat("GDExtension's compatibility_minimum (%d.%d.%d) must be at least 4.1.0: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));302return ERR_INVALID_DATA;303}304305bool compatible = true;306// Check version lexicographically.307if (GODOT_VERSION_MAJOR != compatibility_minimum[0]) {308compatible = GODOT_VERSION_MAJOR > compatibility_minimum[0];309} else if (GODOT_VERSION_MINOR != compatibility_minimum[1]) {310compatible = GODOT_VERSION_MINOR > compatibility_minimum[1];311} else {312compatible = GODOT_VERSION_PATCH >= compatibility_minimum[2];313}314if (!compatible) {315ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s, but your Godot version is %d.%d.%d",316compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path,317GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));318return ERR_INVALID_DATA;319}320321// Optionally check maximum compatibility.322if (config->has_section_key("configuration", "compatibility_maximum")) {323uint32_t compatibility_maximum[3] = { 0, 0, 0 };324String compat_string = config->get_value("configuration", "compatibility_maximum");325Vector<int> parts = compat_string.split_ints(".");326for (int i = 0; i < 3; i++) {327if (i < parts.size() && parts[i] >= 0) {328compatibility_maximum[i] = parts[i];329} else {330// If a version part is missing, set the maximum to an arbitrary high value.331compatibility_maximum[i] = 9999;332}333}334335compatible = true;336if (GODOT_VERSION_MAJOR != compatibility_maximum[0]) {337compatible = GODOT_VERSION_MAJOR < compatibility_maximum[0];338} else if (GODOT_VERSION_MINOR != compatibility_maximum[1]) {339compatible = GODOT_VERSION_MINOR < compatibility_maximum[1];340}341#if GODOT_VERSION_PATCH342// #if check to avoid -Wtype-limits warning when 0.343else {344compatible = GODOT_VERSION_PATCH <= compatibility_maximum[2];345}346#endif347348if (!compatible) {349ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s, but your Godot version is %d.%d.%d",350compat_string, p_path, GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));351return ERR_INVALID_DATA;352}353}354355library_path = find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });356357if (library_path.is_empty()) {358const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();359ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));360return ERR_FILE_NOT_FOUND;361}362363is_static_library = library_path.ends_with(".a") || library_path.ends_with(".xcframework");364365if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {366library_path = p_path.get_base_dir().path_join(library_path);367}368369#ifdef TOOLS_ENABLED370is_reloadable = config->get_value("configuration", "reloadable", false);371372update_last_modified_time(373FileAccess::get_modified_time(resource_path),374FileAccess::get_modified_time(library_path));375#endif376377library_dependencies = find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });378379// Handle icons if any are specified.380if (config->has_section("icons")) {381Vector<String> keys = config->get_section_keys("icons");382for (const String &key : keys) {383String icon_path = config->get_value("icons", key);384if (icon_path.is_relative_path()) {385icon_path = p_path.get_base_dir().path_join(icon_path);386}387388class_icon_paths[key] = icon_path;389}390}391392return OK;393}394395396