Path: blob/master/core/extension/gdextension_library_loader.cpp
20936 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};196197// Apple has a complex lookup system which goes beyond looking up the filename, so we try that first.198err = OS::get_singleton()->open_dynamic_library(abs_path, library, &data);199if (err != OK) {200err = OS::get_singleton()->open_dynamic_library(String(), library, &data);201if (err != OK) {202return err;203}204}205206return OK;207}208209Error GDExtensionLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {210#ifdef TOOLS_ENABLED211p_extension->set_reloadable(is_reloadable && Engine::get_singleton()->is_extension_reloading_enabled());212#endif213214for (const KeyValue<String, String> &icon : class_icon_paths) {215p_extension->class_icon_paths[icon.key] = icon.value;216}217218void *entry_funcptr = nullptr;219220Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, entry_symbol, entry_funcptr, false);221222if (err != OK) {223ERR_PRINT(vformat("GDExtension entry point '%s' not found in library %s.", entry_symbol, library_path));224return err;225}226227GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;228229GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);230231if (ret) {232return OK;233} else {234ERR_PRINT(vformat("GDExtension initialization function '%s' returned an error.", entry_symbol));235return FAILED;236}237}238239void GDExtensionLibraryLoader::close_library() {240OS::get_singleton()->close_dynamic_library(library);241library = nullptr;242}243244bool GDExtensionLibraryLoader::is_library_open() const {245return library != nullptr;246}247248bool GDExtensionLibraryLoader::has_library_changed() const {249#ifdef TOOLS_ENABLED250// Check only that the last modified time is different (rather than checking251// that it's newer) since some OS's (namely Windows) will preserve the modified252// time by default when copying files.253if (FileAccess::get_modified_time(resource_path) != resource_last_modified_time) {254return true;255}256if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {257return true;258}259#endif260return false;261}262263bool GDExtensionLibraryLoader::library_exists() const {264return FileAccess::exists(resource_path);265}266267Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {268resource_path = p_path;269270Ref<ConfigFile> config;271config.instantiate();272273Error err = config->load(p_path);274275if (err != OK) {276ERR_PRINT(vformat("Error loading GDExtension configuration file: '%s'.", p_path));277return err;278}279280if (!config->has_section_key("configuration", "entry_symbol")) {281ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: '%s'.", p_path));282return ERR_INVALID_DATA;283}284285entry_symbol = config->get_value("configuration", "entry_symbol");286287uint32_t compatibility_minimum[3] = { 0, 0, 0 };288if (config->has_section_key("configuration", "compatibility_minimum")) {289String compat_string = config->get_value("configuration", "compatibility_minimum");290Vector<int> parts = compat_string.split_ints(".");291for (int i = 0; i < parts.size(); i++) {292if (i >= 3) {293break;294}295if (parts[i] >= 0) {296compatibility_minimum[i] = parts[i];297}298}299} else {300ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: '%s'.", p_path));301return ERR_INVALID_DATA;302}303304if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {305ERR_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));306return ERR_INVALID_DATA;307}308309bool compatible = true;310// Check version lexicographically.311if (GODOT_VERSION_MAJOR != compatibility_minimum[0]) {312compatible = GODOT_VERSION_MAJOR > compatibility_minimum[0];313} else if (GODOT_VERSION_MINOR != compatibility_minimum[1]) {314compatible = GODOT_VERSION_MINOR > compatibility_minimum[1];315} else {316compatible = GODOT_VERSION_PATCH >= compatibility_minimum[2];317}318if (!compatible) {319ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s, but your Godot version is %d.%d.%d",320compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path,321GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));322return ERR_INVALID_DATA;323}324325// Optionally check maximum compatibility.326if (config->has_section_key("configuration", "compatibility_maximum")) {327uint32_t compatibility_maximum[3] = { 0, 0, 0 };328String compat_string = config->get_value("configuration", "compatibility_maximum");329Vector<int> parts = compat_string.split_ints(".");330for (int i = 0; i < 3; i++) {331if (i < parts.size() && parts[i] >= 0) {332compatibility_maximum[i] = parts[i];333} else {334// If a version part is missing, set the maximum to an arbitrary high value.335compatibility_maximum[i] = 9999;336}337}338339compatible = true;340if (GODOT_VERSION_MAJOR != compatibility_maximum[0]) {341compatible = GODOT_VERSION_MAJOR < compatibility_maximum[0];342} else if (GODOT_VERSION_MINOR != compatibility_maximum[1]) {343compatible = GODOT_VERSION_MINOR < compatibility_maximum[1];344}345#if GODOT_VERSION_PATCH346// #if check to avoid -Wtype-limits warning when 0.347else {348compatible = GODOT_VERSION_PATCH <= compatibility_maximum[2];349}350#endif351352if (!compatible) {353ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s, but your Godot version is %d.%d.%d",354compat_string, p_path, GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));355return ERR_INVALID_DATA;356}357}358359library_path = find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });360361if (library_path.is_empty()) {362const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();363ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));364return ERR_FILE_NOT_FOUND;365}366367if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {368library_path = p_path.get_base_dir().path_join(library_path);369}370371#ifdef TOOLS_ENABLED372is_reloadable = config->get_value("configuration", "reloadable", false);373374update_last_modified_time(375FileAccess::get_modified_time(resource_path),376FileAccess::get_modified_time(library_path));377#endif378379library_dependencies = find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });380381// Handle icons if any are specified.382if (config->has_section("icons")) {383Vector<String> keys = config->get_section_keys("icons");384for (const String &key : keys) {385String icon_path = config->get_value("icons", key);386if (icon_path.is_relative_path()) {387icon_path = p_path.get_base_dir().path_join(icon_path);388}389390class_icon_paths[key] = icon_path;391}392}393394return OK;395}396397398