summaryrefslogtreecommitdiffstats
path: root/core/extension/gdextension.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/extension/gdextension.cpp')
-rw-r--r--core/extension/gdextension.cpp432
1 files changed, 84 insertions, 348 deletions
diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp
index 8e2366fc95..e764b9c112 100644
--- a/core/extension/gdextension.cpp
+++ b/core/extension/gdextension.cpp
@@ -32,11 +32,9 @@
#include "gdextension.compat.inc"
#include "core/config/project_settings.h"
-#include "core/io/dir_access.h"
#include "core/object/class_db.h"
#include "core/object/method_bind.h"
-#include "core/os/os.h"
-#include "core/version.h"
+#include "gdextension_library_loader.h"
#include "gdextension_manager.h"
extern void gdextension_setup_interface();
@@ -48,146 +46,6 @@ String GDExtension::get_extension_list_config_file() {
return ProjectSettings::get_singleton()->get_project_data_path().path_join("extension_list.cfg");
}
-Vector<SharedObject> GDExtension::find_extension_dependencies(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature) {
- Vector<SharedObject> dependencies_shared_objects;
- if (p_config->has_section("dependencies")) {
- List<String> config_dependencies;
- p_config->get_section_keys("dependencies", &config_dependencies);
-
- for (const String &dependency : config_dependencies) {
- Vector<String> dependency_tags = dependency.split(".");
- bool all_tags_met = true;
- for (int i = 0; i < dependency_tags.size(); i++) {
- String tag = dependency_tags[i].strip_edges();
- if (!p_has_feature(tag)) {
- all_tags_met = false;
- break;
- }
- }
-
- if (all_tags_met) {
- Dictionary dependency_value = p_config->get_value("dependencies", dependency);
- for (const Variant *key = dependency_value.next(nullptr); key; key = dependency_value.next(key)) {
- String dependency_path = *key;
- String target_path = dependency_value[*key];
- if (dependency_path.is_relative_path()) {
- dependency_path = p_path.get_base_dir().path_join(dependency_path);
- }
- dependencies_shared_objects.push_back(SharedObject(dependency_path, dependency_tags, target_path));
- }
- break;
- }
- }
- }
-
- return dependencies_shared_objects;
-}
-
-String GDExtension::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {
- // First, check the explicit libraries.
- if (p_config->has_section("libraries")) {
- List<String> libraries;
- p_config->get_section_keys("libraries", &libraries);
-
- // Iterate the libraries, finding the best matching tags.
- String best_library_path;
- Vector<String> best_library_tags;
- for (const String &E : libraries) {
- Vector<String> tags = E.split(".");
- bool all_tags_met = true;
- for (int i = 0; i < tags.size(); i++) {
- String tag = tags[i].strip_edges();
- if (!p_has_feature(tag)) {
- all_tags_met = false;
- break;
- }
- }
-
- if (all_tags_met && tags.size() > best_library_tags.size()) {
- best_library_path = p_config->get_value("libraries", E);
- best_library_tags = tags;
- }
- }
-
- if (!best_library_path.is_empty()) {
- if (best_library_path.is_relative_path()) {
- best_library_path = p_path.get_base_dir().path_join(best_library_path);
- }
- if (r_tags != nullptr) {
- r_tags->append_array(best_library_tags);
- }
- return best_library_path;
- }
- }
-
- // Second, try to autodetect
- String autodetect_library_prefix;
- if (p_config->has_section_key("configuration", "autodetect_library_prefix")) {
- autodetect_library_prefix = p_config->get_value("configuration", "autodetect_library_prefix");
- }
- if (!autodetect_library_prefix.is_empty()) {
- String autodetect_path = autodetect_library_prefix;
- if (autodetect_path.is_relative_path()) {
- autodetect_path = p_path.get_base_dir().path_join(autodetect_path);
- }
-
- // Find the folder and file parts of the prefix.
- String folder;
- String file_prefix;
- if (DirAccess::dir_exists_absolute(autodetect_path)) {
- folder = autodetect_path;
- } else if (DirAccess::dir_exists_absolute(autodetect_path.get_base_dir())) {
- folder = autodetect_path.get_base_dir();
- file_prefix = autodetect_path.get_file();
- } else {
- ERR_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));
- }
-
- // Open the folder.
- Ref<DirAccess> dir = DirAccess::open(folder);
- ERR_FAIL_COND_V_MSG(!dir.is_valid(), 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));
-
- // Iterate the files and check the prefixes, finding the best matching file.
- String best_file;
- Vector<String> best_file_tags;
- dir->list_dir_begin();
- String file_name = dir->_get_next();
- while (file_name != "") {
- if (!dir->current_is_dir() && file_name.begins_with(file_prefix)) {
- // Check if the files matches all requested feature tags.
- String tags_str = file_name.trim_prefix(file_prefix);
- tags_str = tags_str.trim_suffix(tags_str.get_extension());
-
- Vector<String> tags = tags_str.split(".", false);
- bool all_tags_met = true;
- for (int i = 0; i < tags.size(); i++) {
- String tag = tags[i].strip_edges();
- if (!p_has_feature(tag)) {
- all_tags_met = false;
- break;
- }
- }
-
- // If all tags are found in the feature list, and we found more tags than before, use this file.
- if (all_tags_met && tags.size() > best_file_tags.size()) {
- best_file_tags = tags;
- best_file = file_name;
- }
- }
- file_name = dir->_get_next();
- }
-
- if (!best_file.is_empty()) {
- String library_path = folder.path_join(best_file);
- if (r_tags != nullptr) {
- r_tags->append_array(best_file_tags);
- }
- return library_path;
- }
- }
- return String();
-}
-
class GDExtensionMethodBind : public MethodBind {
GDExtensionClassMethodCall call_func;
GDExtensionClassMethodValidatedCall validated_call_func;
@@ -382,7 +240,7 @@ public:
#ifndef DISABLE_DEPRECATED
void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs) {
- const GDExtensionClassCreationInfo3 class_info3 = {
+ const GDExtensionClassCreationInfo4 class_info4 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
true, // GDExtensionBool is_exposed;
@@ -398,7 +256,7 @@ void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library
p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func;
p_extension_funcs->reference_func, // GDExtensionClassReference reference_func;
p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
- p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
+ nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */
p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
nullptr, // GDExtensionClassRecreateInstance recreate_instance_func;
p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
@@ -411,12 +269,13 @@ void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library
const ClassCreationDeprecatedInfo legacy = {
p_extension_funcs->notification_func, // GDExtensionClassNotification notification_func;
p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
+ p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func;
};
- _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info3, &legacy);
+ _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info4, &legacy);
}
void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs) {
- const GDExtensionClassCreationInfo3 class_info3 = {
+ const GDExtensionClassCreationInfo4 class_info4 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
p_extension_funcs->is_exposed, // GDExtensionBool is_exposed;
@@ -432,7 +291,7 @@ void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_librar
p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func;
p_extension_funcs->reference_func, // GDExtensionClassReference reference_func;
p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
- p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
+ nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */
p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
p_extension_funcs->recreate_instance_func, // GDExtensionClassRecreateInstance recreate_instance_func;
p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
@@ -445,21 +304,58 @@ void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_librar
const ClassCreationDeprecatedInfo legacy = {
nullptr, // GDExtensionClassNotification notification_func;
p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
+ p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func;
};
- _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info3, &legacy);
+ _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info4, &legacy);
}
-#endif // DISABLE_DEPRECATED
void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs) {
+ const GDExtensionClassCreationInfo4 class_info4 = {
+ p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
+ p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
+ p_extension_funcs->is_exposed, // GDExtensionBool is_exposed;
+ p_extension_funcs->is_runtime, // GDExtensionBool is_runtime;
+ p_extension_funcs->set_func, // GDExtensionClassSet set_func;
+ p_extension_funcs->get_func, // GDExtensionClassGet get_func;
+ p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func;
+ p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
+ p_extension_funcs->property_can_revert_func, // GDExtensionClassPropertyCanRevert property_can_revert_func;
+ p_extension_funcs->property_get_revert_func, // GDExtensionClassPropertyGetRevert property_get_revert_func;
+ p_extension_funcs->validate_property_func, // GDExtensionClassValidateProperty validate_property_func;
+ p_extension_funcs->notification_func, // GDExtensionClassNotification2 notification_func;
+ p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func;
+ p_extension_funcs->reference_func, // GDExtensionClassReference reference_func;
+ p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
+ nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */
+ p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
+ p_extension_funcs->recreate_instance_func, // GDExtensionClassRecreateInstance recreate_instance_func;
+ p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
+ p_extension_funcs->get_virtual_call_data_func, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
+ p_extension_funcs->call_virtual_with_data_func, // GDExtensionClassCallVirtualWithData call_virtual_func;
+ p_extension_funcs->get_rid_func, // GDExtensionClassGetRID get_rid;
+ p_extension_funcs->class_userdata, // void *class_userdata;
+ };
+
+ const ClassCreationDeprecatedInfo legacy = {
+ nullptr, // GDExtensionClassNotification notification_func;
+ nullptr, // GDExtensionClassFreePropertyList free_property_list_func;
+ p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance2 create_instance_func;
+ };
+ _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info4, &legacy);
+}
+
+#endif // DISABLE_DEPRECATED
+
+void GDExtension::_register_extension_class4(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs) {
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, p_extension_funcs);
}
-void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs) {
+void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs) {
GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
StringName parent_class_name = *reinterpret_cast<const StringName *>(p_parent_class_name);
- ERR_FAIL_COND_MSG(!String(class_name).is_valid_identifier(), "Attempt to register extension class '" + class_name + "', which is not a valid class identifier.");
+ ERR_FAIL_COND_MSG(!String(class_name).is_valid_ascii_identifier(), "Attempt to register extension class '" + class_name + "', which is not a valid class identifier.");
ERR_FAIL_COND_MSG(ClassDB::class_exists(class_name), "Attempt to register extension class '" + class_name + "', which appears to be already registered.");
Extension *parent_extension = nullptr;
@@ -530,6 +426,7 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
if (p_deprecated_funcs) {
extension->gdextension.notification = p_deprecated_funcs->notification_func;
extension->gdextension.free_property_list = p_deprecated_funcs->free_property_list_func;
+ extension->gdextension.create_instance = p_deprecated_funcs->create_instance_func;
}
#endif // DISABLE_DEPRECATED
extension->gdextension.notification2 = p_extension_funcs->notification_func;
@@ -537,7 +434,7 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
extension->gdextension.reference = p_extension_funcs->reference_func;
extension->gdextension.unreference = p_extension_funcs->unreference_func;
extension->gdextension.class_userdata = p_extension_funcs->class_userdata;
- extension->gdextension.create_instance = p_extension_funcs->create_instance_func;
+ extension->gdextension.create_instance2 = p_extension_funcs->create_instance_func;
extension->gdextension.free_instance = p_extension_funcs->free_instance_func;
extension->gdextension.recreate_instance = p_extension_funcs->recreate_instance_func;
extension->gdextension.get_virtual = p_extension_funcs->get_virtual_func;
@@ -755,7 +652,13 @@ void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_libra
void GDExtension::_get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringPtr r_path) {
GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
- memnew_placement(r_path, String(self->library_path));
+ Ref<GDExtensionLibraryLoader> library_loader = self->loader;
+ String library_path;
+ if (library_loader.is_valid()) {
+ library_path = library_loader->library_path;
+ }
+
+ memnew_placement(r_path, String(library_path));
}
HashMap<StringName, GDExtensionInterfaceFunctionPtr> GDExtension::gdextension_interface_functions;
@@ -771,64 +674,32 @@ GDExtensionInterfaceFunctionPtr GDExtension::get_interface_function(const String
return *function;
}
-Error GDExtension::open_library(const String &p_path, const String &p_entry_symbol, Vector<SharedObject> *p_dependencies) {
- String abs_path = ProjectSettings::get_singleton()->globalize_path(p_path);
+Error GDExtension::open_library(const String &p_path, const Ref<GDExtensionLoader> &p_loader) {
+ ERR_FAIL_NULL_V_MSG(p_loader, FAILED, "Can't open GDExtension without a loader.");
+ loader = p_loader;
- Vector<String> abs_dependencies_paths;
- if (p_dependencies != nullptr && !p_dependencies->is_empty()) {
- for (const SharedObject &dependency : *p_dependencies) {
- abs_dependencies_paths.push_back(ProjectSettings::get_singleton()->globalize_path(dependency.path));
- }
- }
+ Error err = loader->open_library(p_path);
- String actual_lib_path;
- OS::GDExtensionData data = {
- true, // also_set_library_path
- &actual_lib_path, // r_resolved_path
- Engine::get_singleton()->is_editor_hint(), // generate_temp_files
- &abs_dependencies_paths, // library_dependencies
- };
- Error err = OS::get_singleton()->open_dynamic_library(abs_path, library, &data);
+ ERR_FAIL_COND_V_MSG(err == ERR_FILE_NOT_FOUND, err, "GDExtension dynamic library not found: " + p_path);
+ ERR_FAIL_COND_V_MSG(err != OK, err, "Can't open GDExtension dynamic library: " + p_path);
- if (actual_lib_path.get_file() != abs_path.get_file()) {
- // If temporary files are generated, let's change the library path to point at the original,
- // because that's what we want to check to see if it's changed.
- library_path = actual_lib_path.get_base_dir().path_join(p_path.get_file());
- } else {
- library_path = actual_lib_path;
- }
-
- ERR_FAIL_COND_V_MSG(err == ERR_FILE_NOT_FOUND, err, "GDExtension dynamic library not found: " + abs_path);
- ERR_FAIL_COND_V_MSG(err != OK, err, "Can't open GDExtension dynamic library: " + abs_path);
-
- void *entry_funcptr = nullptr;
-
- err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, p_entry_symbol, entry_funcptr, false);
+ err = loader->initialize(&gdextension_get_proc_address, this, &initialization);
if (err != OK) {
- ERR_PRINT("GDExtension entry point '" + p_entry_symbol + "' not found in library " + abs_path);
- OS::get_singleton()->close_dynamic_library(library);
+ // Errors already logged in initialize().
+ loader->close_library();
return err;
}
- GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
- GDExtensionBool ret = initialization_function(&gdextension_get_proc_address, this, &initialization);
+ level_initialized = -1;
- if (ret) {
- level_initialized = -1;
- return OK;
- } else {
- ERR_PRINT("GDExtension initialization function '" + p_entry_symbol + "' returned an error.");
- OS::get_singleton()->close_dynamic_library(library);
- return FAILED;
- }
+ return OK;
}
void GDExtension::close_library() {
- ERR_FAIL_NULL(library);
- OS::get_singleton()->close_dynamic_library(library);
+ ERR_FAIL_COND(!is_library_open());
+ loader->close_library();
- library = nullptr;
class_icon_paths.clear();
#ifdef TOOLS_ENABLED
@@ -837,16 +708,16 @@ void GDExtension::close_library() {
}
bool GDExtension::is_library_open() const {
- return library != nullptr;
+ return loader.is_valid() && loader->is_library_open();
}
GDExtension::InitializationLevel GDExtension::get_minimum_library_initialization_level() const {
- ERR_FAIL_NULL_V(library, INITIALIZATION_LEVEL_CORE);
+ ERR_FAIL_COND_V(!is_library_open(), INITIALIZATION_LEVEL_CORE);
return InitializationLevel(initialization.minimum_initialization_level);
}
void GDExtension::initialize_library(InitializationLevel p_level) {
- ERR_FAIL_NULL(library);
+ ERR_FAIL_COND(!is_library_open());
ERR_FAIL_COND_MSG(p_level <= int32_t(level_initialized), vformat("Level '%d' must be higher than the current level '%d'", p_level, level_initialized));
level_initialized = int32_t(p_level);
@@ -856,7 +727,7 @@ void GDExtension::initialize_library(InitializationLevel p_level) {
initialization.initialize(initialization.userdata, GDExtensionInitializationLevel(p_level));
}
void GDExtension::deinitialize_library(InitializationLevel p_level) {
- ERR_FAIL_NULL(library);
+ ERR_FAIL_COND(!is_library_open());
ERR_FAIL_COND(p_level > int32_t(level_initialized));
level_initialized = int32_t(p_level) - 1;
@@ -880,7 +751,7 @@ GDExtension::GDExtension() {
}
GDExtension::~GDExtension() {
- if (library != nullptr) {
+ if (is_library_open()) {
close_library();
}
#ifdef TOOLS_ENABLED
@@ -897,8 +768,9 @@ void GDExtension::initialize_gdextensions() {
#ifndef DISABLE_DEPRECATED
register_interface_function("classdb_register_extension_class", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class);
register_interface_function("classdb_register_extension_class2", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class2);
-#endif // DISABLE_DEPRECATED
register_interface_function("classdb_register_extension_class3", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class3);
+#endif // DISABLE_DEPRECATED
+ register_interface_function("classdb_register_extension_class4", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class4);
register_interface_function("classdb_register_extension_class_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_method);
register_interface_function("classdb_register_extension_class_virtual_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_virtual_method);
register_interface_function("classdb_register_extension_class_integer_constant", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_integer_constant);
@@ -918,142 +790,15 @@ void GDExtension::finalize_gdextensions() {
Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension) {
ERR_FAIL_COND_V_MSG(p_extension.is_valid() && p_extension->is_library_open(), ERR_ALREADY_IN_USE, "Cannot load GDExtension resource into already opened library.");
- Ref<ConfigFile> config;
- config.instantiate();
-
- Error err = config->load(p_path);
-
- if (err != OK) {
- ERR_PRINT("Error loading GDExtension configuration file: " + p_path);
- return err;
- }
-
- if (!config->has_section_key("configuration", "entry_symbol")) {
- ERR_PRINT("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: " + p_path);
- return ERR_INVALID_DATA;
- }
-
- String entry_symbol = config->get_value("configuration", "entry_symbol");
-
- uint32_t compatibility_minimum[3] = { 0, 0, 0 };
- if (config->has_section_key("configuration", "compatibility_minimum")) {
- String compat_string = config->get_value("configuration", "compatibility_minimum");
- Vector<int> parts = compat_string.split_ints(".");
- for (int i = 0; i < parts.size(); i++) {
- if (i >= 3) {
- break;
- }
- if (parts[i] >= 0) {
- compatibility_minimum[i] = parts[i];
- }
- }
- } else {
- ERR_PRINT("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: " + p_path);
- return ERR_INVALID_DATA;
- }
-
- if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
- ERR_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));
- return ERR_INVALID_DATA;
- }
-
- bool compatible = true;
- // Check version lexicographically.
- if (VERSION_MAJOR != compatibility_minimum[0]) {
- compatible = VERSION_MAJOR > compatibility_minimum[0];
- } else if (VERSION_MINOR != compatibility_minimum[1]) {
- compatible = VERSION_MINOR > compatibility_minimum[1];
- } else {
- compatible = VERSION_PATCH >= compatibility_minimum[2];
- }
- if (!compatible) {
- ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
- return ERR_INVALID_DATA;
- }
-
- // Optionally check maximum compatibility.
- if (config->has_section_key("configuration", "compatibility_maximum")) {
- uint32_t compatibility_maximum[3] = { 0, 0, 0 };
- String compat_string = config->get_value("configuration", "compatibility_maximum");
- Vector<int> parts = compat_string.split_ints(".");
- for (int i = 0; i < 3; i++) {
- if (i < parts.size() && parts[i] >= 0) {
- compatibility_maximum[i] = parts[i];
- } else {
- // If a version part is missing, set the maximum to an arbitrary high value.
- compatibility_maximum[i] = 9999;
- }
- }
-
- compatible = true;
- if (VERSION_MAJOR != compatibility_maximum[0]) {
- compatible = VERSION_MAJOR < compatibility_maximum[0];
- } else if (VERSION_MINOR != compatibility_maximum[1]) {
- compatible = VERSION_MINOR < compatibility_maximum[1];
- }
-#if VERSION_PATCH
- // #if check to avoid -Wtype-limits warning when 0.
- else {
- compatible = VERSION_PATCH <= compatibility_maximum[2];
- }
-#endif
-
- if (!compatible) {
- ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s", compat_string, p_path));
- return ERR_INVALID_DATA;
- }
- }
-
- String library_path = GDExtension::find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
-
- if (library_path.is_empty()) {
- const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
- ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
- return ERR_FILE_NOT_FOUND;
- }
-
- bool is_static_library = library_path.ends_with(".a") || library_path.ends_with(".xcframework");
-
- if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
- library_path = p_path.get_base_dir().path_join(library_path);
- }
-
- if (p_extension.is_null()) {
- p_extension.instantiate();
- }
+ GDExtensionManager *extension_manager = GDExtensionManager::get_singleton();
-#ifdef TOOLS_ENABLED
- p_extension->set_reloadable(config->get_value("configuration", "reloadable", false) && Engine::get_singleton()->is_extension_reloading_enabled());
-
- p_extension->update_last_modified_time(
- FileAccess::get_modified_time(p_path),
- FileAccess::get_modified_time(library_path));
-#endif
-
- Vector<SharedObject> library_dependencies = GDExtension::find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
- err = p_extension->open_library(is_static_library ? String() : library_path, entry_symbol, &library_dependencies);
- if (err != OK) {
- // Unreference the extension so that this loading can be considered a failure.
- p_extension.unref();
-
- // Errors already logged in open_library()
- return err;
- }
-
- // Handle icons if any are specified.
- if (config->has_section("icons")) {
- List<String> keys;
- config->get_section_keys("icons", &keys);
- for (const String &key : keys) {
- String icon_path = config->get_value("icons", key);
- if (icon_path.is_relative_path()) {
- icon_path = p_path.get_base_dir().path_join(icon_path);
- }
-
- p_extension->class_icon_paths[key] = icon_path;
- }
+ GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path);
+ if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) {
+ // Errors already logged in load_extension().
+ return FAILED;
}
+ p_extension = extension_manager->get_extension(p_path);
return OK;
}
@@ -1094,16 +839,7 @@ String GDExtensionResourceLoader::get_resource_type(const String &p_path) const
#ifdef TOOLS_ENABLED
bool GDExtension::has_library_changed() const {
- // Check only that the last modified time is different (rather than checking
- // that it's newer) since some OS's (namely Windows) will preserve the modified
- // time by default when copying files.
- if (FileAccess::get_modified_time(get_path()) != resource_last_modified_time) {
- return true;
- }
- if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {
- return true;
- }
- return false;
+ return loader->has_library_changed();
}
void GDExtension::prepare_reload() {