2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* gdextension.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
# include "gdextension.h"
2021-07-29 13:53:05 +00:00
# include "core/config/project_settings.h"
2022-10-26 16:23:09 +00:00
# include "core/io/dir_access.h"
2021-06-19 15:58:49 +00:00
# include "core/object/class_db.h"
# include "core/object/method_bind.h"
# include "core/os/os.h"
2022-12-07 11:11:28 +00:00
String GDExtension : : get_extension_list_config_file ( ) {
2022-08-30 00:34:01 +00:00
return ProjectSettings : : get_singleton ( ) - > get_project_data_path ( ) . path_join ( " extension_list.cfg " ) ;
2021-09-10 15:32:29 +00:00
}
2021-08-20 18:32:56 +00:00
2022-12-07 11:11:28 +00:00
String GDExtension : : find_extension_library ( const String & p_path , Ref < ConfigFile > p_config , std : : function < bool ( String ) > p_has_feature , PackedStringArray * r_tags ) {
2022-10-26 16:23:09 +00:00
// 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 ( ) ;
}
2022-12-07 11:11:28 +00:00
class GDExtensionMethodBind : public MethodBind {
GDExtensionClassMethodCall call_func ;
GDExtensionClassMethodPtrCall ptrcall_func ;
2021-06-19 15:58:49 +00:00
void * method_userdata ;
bool vararg ;
2022-10-23 08:43:09 +00:00
PropertyInfo return_value_info ;
GodotTypeInfo : : Metadata return_value_metadata ;
List < PropertyInfo > arguments_info ;
List < GodotTypeInfo : : Metadata > arguments_metadata ;
2021-06-19 15:58:49 +00:00
protected :
2022-04-07 10:23:40 +00:00
virtual Variant : : Type _gen_argument_type ( int p_arg ) const override {
2022-10-23 08:43:09 +00:00
if ( p_arg < 0 ) {
return return_value_info . type ;
} else {
return arguments_info [ p_arg ] . type ;
}
2021-06-19 15:58:49 +00:00
}
2022-04-07 10:23:40 +00:00
virtual PropertyInfo _gen_argument_type_info ( int p_arg ) const override {
2022-10-23 08:43:09 +00:00
if ( p_arg < 0 ) {
return return_value_info ;
} else {
return arguments_info [ p_arg ] ;
}
2021-06-19 15:58:49 +00:00
}
public :
2022-04-07 10:23:40 +00:00
# ifdef DEBUG_METHODS_ENABLED
virtual GodotTypeInfo : : Metadata get_argument_meta ( int p_arg ) const override {
2022-10-23 08:43:09 +00:00
if ( p_arg < 0 ) {
return return_value_metadata ;
} else {
return arguments_metadata [ p_arg ] ;
}
2021-06-19 15:58:49 +00:00
}
2022-04-07 10:23:40 +00:00
# endif
2021-06-19 15:58:49 +00:00
2022-10-22 13:18:33 +00:00
virtual Variant call ( Object * p_object , const Variant * * p_args , int p_arg_count , Callable : : CallError & r_error ) const override {
2021-06-19 15:58:49 +00:00
Variant ret ;
2022-05-06 06:44:13 +00:00
GDExtensionClassInstancePtr extension_instance = is_static ( ) ? nullptr : p_object - > _get_extension_instance ( ) ;
2022-12-07 11:11:28 +00:00
GDExtensionCallError ce { GDEXTENSION_CALL_OK , 0 , 0 } ;
call_func ( method_userdata , extension_instance , reinterpret_cast < GDExtensionConstVariantPtr * > ( p_args ) , p_arg_count , ( GDExtensionVariantPtr ) & ret , & ce ) ;
2021-06-19 15:58:49 +00:00
r_error . error = Callable : : CallError : : Error ( ce . error ) ;
r_error . argument = ce . argument ;
r_error . expected = ce . expected ;
return ret ;
}
2022-10-22 13:18:33 +00:00
virtual void ptrcall ( Object * p_object , const void * * p_args , void * r_ret ) const override {
2021-06-19 15:58:49 +00:00
ERR_FAIL_COND_MSG ( vararg , " Vararg methods don't have ptrcall support. This is most likely an engine bug. " ) ;
GDExtensionClassInstancePtr extension_instance = p_object - > _get_extension_instance ( ) ;
2022-12-07 11:11:28 +00:00
ptrcall_func ( method_userdata , extension_instance , reinterpret_cast < GDExtensionConstTypePtr * > ( p_args ) , ( GDExtensionTypePtr ) r_ret ) ;
2021-06-19 15:58:49 +00:00
}
2022-04-07 10:23:40 +00:00
virtual bool is_vararg ( ) const override {
2021-06-19 15:58:49 +00:00
return false ;
}
2022-05-06 06:44:13 +00:00
2022-12-07 11:11:28 +00:00
explicit GDExtensionMethodBind ( const GDExtensionClassMethodInfo * p_method_info ) {
2021-06-19 15:58:49 +00:00
method_userdata = p_method_info - > method_userdata ;
call_func = p_method_info - > call_func ;
ptrcall_func = p_method_info - > ptrcall_func ;
2022-10-23 08:43:09 +00:00
set_name ( * reinterpret_cast < StringName * > ( p_method_info - > name ) ) ;
if ( p_method_info - > has_return_value ) {
return_value_info = PropertyInfo ( * p_method_info - > return_value_info ) ;
return_value_metadata = GodotTypeInfo : : Metadata ( p_method_info - > return_value_metadata ) ;
}
for ( uint32_t i = 0 ; i < p_method_info - > argument_count ; i + + ) {
2022-11-15 23:51:48 +00:00
arguments_info . push_back ( PropertyInfo ( p_method_info - > arguments_info [ i ] ) ) ;
arguments_metadata . push_back ( GodotTypeInfo : : Metadata ( p_method_info - > arguments_metadata [ i ] ) ) ;
2022-10-23 08:43:09 +00:00
}
2021-06-19 15:58:49 +00:00
2022-05-06 06:44:13 +00:00
set_hint_flags ( p_method_info - > method_flags ) ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
vararg = p_method_info - > method_flags & GDEXTENSION_METHOD_FLAG_VARARG ;
2021-06-19 15:58:49 +00:00
_set_returns ( p_method_info - > has_return_value ) ;
2022-12-07 11:11:28 +00:00
_set_const ( p_method_info - > method_flags & GDEXTENSION_METHOD_FLAG_CONST ) ;
_set_static ( p_method_info - > method_flags & GDEXTENSION_METHOD_FLAG_STATIC ) ;
2021-06-19 15:58:49 +00:00
# ifdef DEBUG_METHODS_ENABLED
_generate_argument_types ( p_method_info - > argument_count ) ;
# endif
set_argument_count ( p_method_info - > argument_count ) ;
2022-05-06 06:44:13 +00:00
Vector < Variant > defargs ;
defargs . resize ( p_method_info - > default_argument_count ) ;
for ( uint32_t i = 0 ; i < p_method_info - > default_argument_count ; i + + ) {
defargs . write [ i ] = * static_cast < Variant * > ( p_method_info - > default_arguments [ i ] ) ;
}
set_default_arguments ( defargs ) ;
2021-06-19 15:58:49 +00:00
}
} ;
2022-12-07 11:11:28 +00:00
static GDExtensionInterface gdextension_interface ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , GDExtensionConstStringNamePtr p_parent_class_name , const GDExtensionClassCreationInfo * p_extension_funcs ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-22 13:18:33 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
StringName parent_class_name = * reinterpret_cast < const StringName * > ( p_parent_class_name ) ;
2021-07-29 13:53:05 +00:00
ERR_FAIL_COND_MSG ( ! String ( class_name ) . is_valid_identifier ( ) , " Attempt to register extension class ' " + class_name + " ', which is not a valid class identifier. " ) ;
2021-06-19 15:58:49 +00:00
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 ;
if ( self - > extension_classes . has ( parent_class_name ) ) {
parent_extension = & self - > extension_classes [ parent_class_name ] ;
} else if ( ClassDB : : class_exists ( parent_class_name ) ) {
if ( ClassDB : : get_api_type ( parent_class_name ) = = ClassDB : : API_EXTENSION | | ClassDB : : get_api_type ( parent_class_name ) = = ClassDB : : API_EDITOR_EXTENSION ) {
ERR_PRINT ( " Unimplemented yet " ) ;
//inheriting from another extension
} else {
//inheriting from engine class
}
} else {
ERR_FAIL_MSG ( " Attempt to register an extension class ' " + String ( class_name ) + " ' using non-existing parent class ' " + String ( parent_class_name ) + " ' " ) ;
}
self - > extension_classes [ class_name ] = Extension ( ) ;
Extension * extension = & self - > extension_classes [ class_name ] ;
if ( parent_extension ) {
2022-12-07 11:11:28 +00:00
extension - > gdextension . parent = & parent_extension - > gdextension ;
parent_extension - > gdextension . children . push_back ( & extension - > gdextension ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
extension - > gdextension . parent_class_name = parent_class_name ;
extension - > gdextension . class_name = class_name ;
extension - > gdextension . editor_class = self - > level_initialized = = INITIALIZATION_LEVEL_EDITOR ;
extension - > gdextension . is_virtual = p_extension_funcs - > is_virtual ;
extension - > gdextension . is_abstract = p_extension_funcs - > is_abstract ;
extension - > gdextension . set = p_extension_funcs - > set_func ;
extension - > gdextension . get = p_extension_funcs - > get_func ;
extension - > gdextension . get_property_list = p_extension_funcs - > get_property_list_func ;
extension - > gdextension . free_property_list = p_extension_funcs - > free_property_list_func ;
extension - > gdextension . property_can_revert = p_extension_funcs - > property_can_revert_func ;
extension - > gdextension . property_get_revert = p_extension_funcs - > property_get_revert_func ;
extension - > gdextension . notification = p_extension_funcs - > notification_func ;
extension - > gdextension . to_string = p_extension_funcs - > to_string_func ;
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 . free_instance = p_extension_funcs - > free_instance_func ;
extension - > gdextension . get_virtual = p_extension_funcs - > get_virtual_func ;
extension - > gdextension . get_rid = p_extension_funcs - > get_rid_func ;
ClassDB : : register_extension_class ( & extension - > gdextension ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_method ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , const GDExtensionClassMethodInfo * p_method_info ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
StringName method_name = * reinterpret_cast < const StringName * > ( p_method_info - > name ) ;
2021-07-29 13:53:05 +00:00
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension method ' " + String ( method_name ) + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-06-19 15:58:49 +00:00
//Extension *extension = &self->extension_classes[class_name];
2022-12-07 11:11:28 +00:00
GDExtensionMethodBind * method = memnew ( GDExtensionMethodBind ( p_method_info ) ) ;
2021-07-29 13:53:05 +00:00
method - > set_instance_class ( class_name ) ;
2021-06-19 15:58:49 +00:00
ClassDB : : bind_method_custom ( class_name , method ) ;
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_integer_constant ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , GDExtensionConstStringNamePtr p_enum_name , GDExtensionConstStringNamePtr p_constant_name , GDExtensionInt p_constant_value , GDExtensionBool p_is_bitfield ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
StringName enum_name = * reinterpret_cast < const StringName * > ( p_enum_name ) ;
StringName constant_name = * reinterpret_cast < const StringName * > ( p_constant_name ) ;
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension constant ' " + constant_name + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
ClassDB : : bind_integer_constant ( class_name , enum_name , constant_name , p_constant_value , p_is_bitfield ) ;
2021-06-19 15:58:49 +00:00
}
2021-09-16 02:37:43 +00:00
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_property ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , const GDExtensionPropertyInfo * p_info , GDExtensionConstStringNamePtr p_setter , GDExtensionConstStringNamePtr p_getter ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
StringName setter = * reinterpret_cast < const StringName * > ( p_setter ) ;
StringName getter = * reinterpret_cast < const StringName * > ( p_getter ) ;
String property_name = * reinterpret_cast < const StringName * > ( p_info - > name ) ;
2022-06-06 20:24:13 +00:00
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension class property ' " + property_name + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-06-19 15:58:49 +00:00
//Extension *extension = &self->extension_classes[class_name];
2022-06-06 20:24:13 +00:00
PropertyInfo pinfo ( * p_info ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
ClassDB : : add_property ( class_name , pinfo , setter , getter ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_property_group ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , GDExtensionConstStringPtr p_group_name , GDExtensionConstStringPtr p_prefix ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-09-16 02:37:43 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
String group_name = * reinterpret_cast < const String * > ( p_group_name ) ;
String prefix = * reinterpret_cast < const String * > ( p_prefix ) ;
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension class property group ' " + group_name + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-09-16 02:37:43 +00:00
2022-10-23 08:43:09 +00:00
ClassDB : : add_property_group ( class_name , group_name , prefix ) ;
2021-09-16 02:37:43 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_property_subgroup ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , GDExtensionConstStringPtr p_subgroup_name , GDExtensionConstStringPtr p_prefix ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-09-16 02:37:43 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
String subgroup_name = * reinterpret_cast < const String * > ( p_subgroup_name ) ;
String prefix = * reinterpret_cast < const String * > ( p_prefix ) ;
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension class property subgroup ' " + subgroup_name + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-09-16 02:37:43 +00:00
2022-10-23 08:43:09 +00:00
ClassDB : : add_property_subgroup ( class_name , subgroup_name , prefix ) ;
2021-09-16 02:37:43 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _register_extension_class_signal ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name , GDExtensionConstStringNamePtr p_signal_name , const GDExtensionPropertyInfo * p_argument_info , GDExtensionInt p_argument_count ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
StringName signal_name = * reinterpret_cast < const StringName * > ( p_signal_name ) ;
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to register extension class signal ' " + signal_name + " ' for unexisting class ' " + class_name + " '. " ) ;
2021-06-19 15:58:49 +00:00
MethodInfo s ;
2022-10-23 08:43:09 +00:00
s . name = signal_name ;
2021-06-19 15:58:49 +00:00
for ( int i = 0 ; i < p_argument_count ; i + + ) {
2022-06-06 20:24:13 +00:00
PropertyInfo arg ( p_argument_info [ i ] ) ;
2021-06-19 15:58:49 +00:00
s . arguments . push_back ( arg ) ;
}
ClassDB : : add_signal ( class_name , s ) ;
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _unregister_extension_class ( GDExtensionClassLibraryPtr p_library , GDExtensionConstStringNamePtr p_class_name ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2021-06-19 15:58:49 +00:00
2022-10-23 08:43:09 +00:00
StringName class_name = * reinterpret_cast < const StringName * > ( p_class_name ) ;
2021-07-29 13:53:05 +00:00
ERR_FAIL_COND_MSG ( ! self - > extension_classes . has ( class_name ) , " Attempt to unregister unexisting extension class ' " + class_name + " '. " ) ;
2021-06-19 15:58:49 +00:00
Extension * ext = & self - > extension_classes [ class_name ] ;
2022-12-07 11:11:28 +00:00
ERR_FAIL_COND_MSG ( ext - > gdextension . children . size ( ) , " Attempt to unregister class ' " + class_name + " ' while other extension classes inherit from it. " ) ;
2021-06-19 15:58:49 +00:00
ClassDB : : unregister_extension_class ( class_name ) ;
2022-12-07 11:11:28 +00:00
if ( ext - > gdextension . parent ! = nullptr ) {
ext - > gdextension . parent - > children . erase ( & ext - > gdextension ) ;
2021-06-19 15:58:49 +00:00
}
self - > extension_classes . erase ( class_name ) ;
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _get_library_path ( GDExtensionClassLibraryPtr p_library , GDExtensionStringPtr r_path ) {
GDExtension * self = reinterpret_cast < GDExtension * > ( p_library ) ;
2022-04-28 22:51:04 +00:00
* ( String * ) r_path = self - > library_path ;
}
2022-12-07 11:11:28 +00:00
Error GDExtension : : open_library ( const String & p_path , const String & p_entry_symbol ) {
2022-04-28 22:51:04 +00:00
Error err = OS : : get_singleton ( ) - > open_dynamic_library ( p_path , library , true , & library_path ) ;
2021-06-19 15:58:49 +00:00
if ( err ! = OK ) {
2022-06-19 08:14:41 +00:00
ERR_PRINT ( " GDExtension dynamic library not found: " + p_path ) ;
2021-06-19 15:58:49 +00:00
return err ;
}
void * entry_funcptr = nullptr ;
err = OS : : get_singleton ( ) - > get_dynamic_library_symbol_handle ( library , p_entry_symbol , entry_funcptr , false ) ;
if ( err ! = OK ) {
2022-06-19 08:14:41 +00:00
ERR_PRINT ( " GDExtension entry point ' " + p_entry_symbol + " ' not found in library " + p_path ) ;
2021-06-19 15:58:49 +00:00
OS : : get_singleton ( ) - > close_dynamic_library ( library ) ;
return err ;
}
2022-12-07 11:11:28 +00:00
GDExtensionInitializationFunction initialization_function = ( GDExtensionInitializationFunction ) entry_funcptr ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
if ( initialization_function ( & gdextension_interface , this , & initialization ) ) {
2022-06-11 22:18:08 +00:00
level_initialized = - 1 ;
return OK ;
} else {
2022-06-19 08:14:41 +00:00
ERR_PRINT ( " GDExtension initialization function ' " + p_entry_symbol + " ' returned an error. " ) ;
2022-06-11 22:18:08 +00:00
return FAILED ;
}
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : close_library ( ) {
2021-06-19 15:58:49 +00:00
ERR_FAIL_COND ( library = = nullptr ) ;
OS : : get_singleton ( ) - > close_dynamic_library ( library ) ;
library = nullptr ;
}
2022-12-07 11:11:28 +00:00
bool GDExtension : : is_library_open ( ) const {
2021-06-19 15:58:49 +00:00
return library ! = nullptr ;
}
2022-12-07 11:11:28 +00:00
GDExtension : : InitializationLevel GDExtension : : get_minimum_library_initialization_level ( ) const {
2021-06-19 15:58:49 +00:00
ERR_FAIL_COND_V ( library = = nullptr , INITIALIZATION_LEVEL_CORE ) ;
return InitializationLevel ( initialization . minimum_initialization_level ) ;
}
2022-02-16 17:02:38 +00:00
2022-12-07 11:11:28 +00:00
void GDExtension : : initialize_library ( InitializationLevel p_level ) {
2021-06-19 15:58:49 +00:00
ERR_FAIL_COND ( library = = nullptr ) ;
2022-02-16 17:02:38 +00:00
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 ) ) ;
2021-06-19 15:58:49 +00:00
level_initialized = int32_t ( p_level ) ;
ERR_FAIL_COND ( initialization . initialize = = nullptr ) ;
2022-12-07 11:11:28 +00:00
initialization . initialize ( initialization . userdata , GDExtensionInitializationLevel ( p_level ) ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : deinitialize_library ( InitializationLevel p_level ) {
2021-06-19 15:58:49 +00:00
ERR_FAIL_COND ( library = = nullptr ) ;
ERR_FAIL_COND ( p_level > int32_t ( level_initialized ) ) ;
level_initialized = int32_t ( p_level ) - 1 ;
2022-12-07 11:11:28 +00:00
initialization . deinitialize ( initialization . userdata , GDExtensionInitializationLevel ( p_level ) ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
void GDExtension : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " open_library " , " path " , " entry_symbol " ) , & GDExtension : : open_library ) ;
ClassDB : : bind_method ( D_METHOD ( " close_library " ) , & GDExtension : : close_library ) ;
ClassDB : : bind_method ( D_METHOD ( " is_library_open " ) , & GDExtension : : is_library_open ) ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_minimum_library_initialization_level " ) , & GDExtension : : get_minimum_library_initialization_level ) ;
ClassDB : : bind_method ( D_METHOD ( " initialize_library " , " level " ) , & GDExtension : : initialize_library ) ;
2021-06-19 15:58:49 +00:00
BIND_ENUM_CONSTANT ( INITIALIZATION_LEVEL_CORE ) ;
BIND_ENUM_CONSTANT ( INITIALIZATION_LEVEL_SERVERS ) ;
BIND_ENUM_CONSTANT ( INITIALIZATION_LEVEL_SCENE ) ;
BIND_ENUM_CONSTANT ( INITIALIZATION_LEVEL_EDITOR ) ;
}
2022-12-07 11:11:28 +00:00
GDExtension : : GDExtension ( ) {
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
GDExtension : : ~ GDExtension ( ) {
2021-06-19 15:58:49 +00:00
if ( library ! = nullptr ) {
close_library ( ) ;
}
}
2022-12-07 11:11:28 +00:00
extern void gdextension_setup_interface ( GDExtensionInterface * p_interface ) ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
void GDExtension : : initialize_gdextensions ( ) {
gdextension_setup_interface ( & gdextension_interface ) ;
2021-06-19 15:58:49 +00:00
2022-12-07 11:11:28 +00:00
gdextension_interface . classdb_register_extension_class = _register_extension_class ;
gdextension_interface . classdb_register_extension_class_method = _register_extension_class_method ;
gdextension_interface . classdb_register_extension_class_integer_constant = _register_extension_class_integer_constant ;
gdextension_interface . classdb_register_extension_class_property = _register_extension_class_property ;
gdextension_interface . classdb_register_extension_class_property_group = _register_extension_class_property_group ;
gdextension_interface . classdb_register_extension_class_property_subgroup = _register_extension_class_property_subgroup ;
gdextension_interface . classdb_register_extension_class_signal = _register_extension_class_signal ;
gdextension_interface . classdb_unregister_extension_class = _unregister_extension_class ;
gdextension_interface . get_library_path = _get_library_path ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
Ref < Resource > GDExtensionResourceLoader : : load ( const String & p_path , const String & p_original_path , Error * r_error , bool p_use_sub_threads , float * r_progress , CacheMode p_cache_mode ) {
2021-06-19 15:58:49 +00:00
Ref < ConfigFile > config ;
config . instantiate ( ) ;
Error err = config - > load ( p_path ) ;
if ( r_error ) {
* r_error = err ;
}
if ( err ! = OK ) {
2022-07-20 17:05:49 +00:00
ERR_PRINT ( " Error loading GDExtension configuration file: " + p_path ) ;
2022-05-02 23:43:50 +00:00
return Ref < Resource > ( ) ;
2021-06-19 15:58:49 +00:00
}
if ( ! config - > has_section_key ( " configuration " , " entry_symbol " ) ) {
if ( r_error ) {
* r_error = ERR_INVALID_DATA ;
}
2022-07-20 17:05:49 +00:00
ERR_PRINT ( " GDExtension configuration file must contain a \" configuration/entry_symbol \" key: " + p_path ) ;
2022-05-02 23:43:50 +00:00
return Ref < Resource > ( ) ;
2021-06-19 15:58:49 +00:00
}
String entry_symbol = config - > get_value ( " configuration " , " entry_symbol " ) ;
2022-12-07 11:11:28 +00:00
String library_path = GDExtension : : find_extension_library ( p_path , config , [ ] ( String p_feature ) { return OS : : get_singleton ( ) - > has_feature ( p_feature ) ; } ) ;
2021-06-19 15:58:49 +00:00
2021-12-09 09:42:46 +00:00
if ( library_path . is_empty ( ) ) {
2021-06-19 15:58:49 +00:00
if ( r_error ) {
* r_error = ERR_FILE_NOT_FOUND ;
}
2022-07-20 17:05:49 +00:00
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 ) ) ;
2022-05-02 23:43:50 +00:00
return Ref < Resource > ( ) ;
2021-06-19 15:58:49 +00:00
}
2021-12-12 21:11:28 +00:00
if ( ! library_path . is_resource_file ( ) & & ! library_path . is_absolute_path ( ) ) {
2022-08-30 00:34:01 +00:00
library_path = p_path . get_base_dir ( ) . path_join ( library_path ) ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
Ref < GDExtension > lib ;
2021-06-19 15:58:49 +00:00
lib . instantiate ( ) ;
2021-07-29 13:53:05 +00:00
String abs_path = ProjectSettings : : get_singleton ( ) - > globalize_path ( library_path ) ;
err = lib - > open_library ( abs_path , entry_symbol ) ;
2021-06-19 15:58:49 +00:00
if ( r_error ) {
* r_error = err ;
}
if ( err ! = OK ) {
2022-06-19 08:14:41 +00:00
// Errors already logged in open_library()
2022-05-02 23:43:50 +00:00
return Ref < Resource > ( ) ;
2021-06-19 15:58:49 +00:00
}
2023-03-31 19:17:59 +00:00
// 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 ) {
lib - > class_icon_paths [ key ] = config - > get_value ( " icons " , key ) ;
}
}
2021-06-19 15:58:49 +00:00
return lib ;
}
2022-12-07 11:11:28 +00:00
void GDExtensionResourceLoader : : get_recognized_extensions ( List < String > * p_extensions ) const {
2021-06-19 15:58:49 +00:00
p_extensions - > push_back ( " gdextension " ) ;
}
2022-12-07 11:11:28 +00:00
bool GDExtensionResourceLoader : : handles_type ( const String & p_type ) const {
return p_type = = " GDExtension " ;
2021-06-19 15:58:49 +00:00
}
2022-12-07 11:11:28 +00:00
String GDExtensionResourceLoader : : get_resource_type ( const String & p_path ) const {
2021-06-19 15:58:49 +00:00
String el = p_path . get_extension ( ) . to_lower ( ) ;
if ( el = = " gdextension " ) {
2022-12-07 11:11:28 +00:00
return " GDExtension " ;
2021-06-19 15:58:49 +00:00
}
return " " ;
}