| 1 | /**************************************************************************/ |
| 2 | /* gdextension.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 "gdextension.h" |
| 32 | #include "gdextension.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/object/class_db.h" |
| 36 | #include "core/object/method_bind.h" |
| 37 | #include "gdextension_library_loader.h" |
| 38 | #include "gdextension_manager.h" |
| 39 | |
| 40 | extern void gdextension_setup_interface(); |
| 41 | extern GDExtensionInterfaceFunctionPtr gdextension_get_proc_address(const char *p_name); |
| 42 | |
| 43 | typedef GDExtensionBool (*GDExtensionLegacyInitializationFunction)(void *p_interface, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization); |
| 44 | |
| 45 | String GDExtension::get_extension_list_config_file() { |
| 46 | return ProjectSettings::get_singleton()->get_project_data_path().path_join("extension_list.cfg"); |
| 47 | } |
| 48 | |
| 49 | class GDExtensionMethodBind : public MethodBind { |
| 50 | GDExtensionClassMethodCall call_func; |
| 51 | GDExtensionClassMethodValidatedCall validated_call_func; |
| 52 | GDExtensionClassMethodPtrCall ptrcall_func; |
| 53 | void *method_userdata; |
| 54 | bool vararg; |
| 55 | uint32_t argument_count; |
| 56 | PropertyInfo return_value_info; |
| 57 | GodotTypeInfo::Metadata return_value_metadata; |
| 58 | List<PropertyInfo> arguments_info; |
| 59 | List<GodotTypeInfo::Metadata> arguments_metadata; |
| 60 | |
| 61 | #ifdef TOOLS_ENABLED |
| 62 | friend class GDExtension; |
| 63 | |
| 64 | StringName name; |
| 65 | bool is_reloading = false; |
| 66 | bool valid = true; |
| 67 | #endif |
| 68 | |
| 69 | protected: |
| 70 | virtual Variant::Type _gen_argument_type(int p_arg) const override { |
| 71 | if (p_arg < 0) { |
| 72 | return return_value_info.type; |
| 73 | } else { |
| 74 | return arguments_info.get(p_arg).type; |
| 75 | } |
| 76 | } |
| 77 | virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { |
| 78 | if (p_arg < 0) { |
| 79 | return return_value_info; |
| 80 | } else { |
| 81 | return arguments_info.get(p_arg); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public: |
| 86 | #ifdef TOOLS_ENABLED |
| 87 | virtual bool is_valid() const override { return valid; } |
| 88 | #endif |
| 89 | |
| 90 | #ifdef DEBUG_ENABLED |
| 91 | virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override { |
| 92 | if (p_arg < 0) { |
| 93 | return return_value_metadata; |
| 94 | } else { |
| 95 | return arguments_metadata.get(p_arg); |
| 96 | } |
| 97 | } |
| 98 | #endif // DEBUG_ENABLED |
| 99 | |
| 100 | virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { |
| 101 | #ifdef TOOLS_ENABLED |
| 102 | ERR_FAIL_COND_V_MSG(!valid, Variant(), vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name)); |
| 103 | ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name)); |
| 104 | #endif |
| 105 | Variant ret; |
| 106 | GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance(); |
| 107 | GDExtensionCallError ce{ GDEXTENSION_CALL_OK, 0, 0 }; |
| 108 | call_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count, (GDExtensionVariantPtr)&ret, &ce); |
| 109 | r_error.error = Callable::CallError::Error(ce.error); |
| 110 | r_error.argument = ce.argument; |
| 111 | r_error.expected = ce.expected; |
| 112 | return ret; |
| 113 | } |
| 114 | virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override { |
| 115 | #ifdef TOOLS_ENABLED |
| 116 | ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name)); |
| 117 | ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name)); |
| 118 | #endif |
| 119 | ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have validated call support. This is most likely an engine bug."); |
| 120 | GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance(); |
| 121 | |
| 122 | if (validated_call_func) { |
| 123 | // This is added here, but it's unlikely to be provided by most extensions. |
| 124 | validated_call_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), (GDExtensionVariantPtr)r_ret); |
| 125 | } else { |
| 126 | // If not provided, go via ptrcall, which is faster than resorting to regular call. |
| 127 | const void **argptrs = (const void **)alloca(argument_count * sizeof(void *)); |
| 128 | for (uint32_t i = 0; i < argument_count; i++) { |
| 129 | argptrs[i] = VariantInternal::get_opaque_pointer(p_args[i]); |
| 130 | } |
| 131 | |
| 132 | void *ret_opaque = nullptr; |
| 133 | if (r_ret) { |
| 134 | VariantInternal::initialize(r_ret, return_value_info.type); |
| 135 | ret_opaque = r_ret->get_type() == Variant::NIL ? r_ret : VariantInternal::get_opaque_pointer(r_ret); |
| 136 | } |
| 137 | |
| 138 | ptrcall_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstTypePtr *>(argptrs), (GDExtensionTypePtr)ret_opaque); |
| 139 | |
| 140 | if (r_ret && r_ret->get_type() == Variant::OBJECT) { |
| 141 | VariantInternal::update_object_id(r_ret); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override { |
| 147 | #ifdef TOOLS_ENABLED |
| 148 | ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name)); |
| 149 | ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name)); |
| 150 | #endif |
| 151 | ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have ptrcall support. This is most likely an engine bug."); |
| 152 | GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance(); |
| 153 | ptrcall_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstTypePtr *>(p_args), (GDExtensionTypePtr)r_ret); |
| 154 | } |
| 155 | |
| 156 | virtual bool is_vararg() const override { |
| 157 | return vararg; |
| 158 | } |
| 159 | |
| 160 | #ifdef TOOLS_ENABLED |
| 161 | bool try_update(const GDExtensionClassMethodInfo *p_method_info) { |
| 162 | if (is_static() != (bool)(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_STATIC)) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if (vararg != (bool)(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_VARARG)) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | if (has_return() != (bool)p_method_info->has_return_value) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (has_return() && return_value_info.type != (Variant::Type)p_method_info->return_value_info->type) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if (argument_count != p_method_info->argument_count) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | List<PropertyInfo>::ConstIterator itr = arguments_info.begin(); |
| 183 | for (uint32_t i = 0; i < p_method_info->argument_count; ++itr, ++i) { |
| 184 | if (itr->type != (Variant::Type)p_method_info->arguments_info[i].type) { |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | update(p_method_info); |
| 190 | return true; |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | void update(const GDExtensionClassMethodInfo *p_method_info) { |
| 195 | #ifdef TOOLS_ENABLED |
| 196 | name = *reinterpret_cast<StringName *>(p_method_info->name); |
| 197 | #endif |
| 198 | method_userdata = p_method_info->method_userdata; |
| 199 | call_func = p_method_info->call_func; |
| 200 | validated_call_func = nullptr; |
| 201 | ptrcall_func = p_method_info->ptrcall_func; |
| 202 | set_name(*reinterpret_cast<StringName *>(p_method_info->name)); |
| 203 | |
| 204 | if (p_method_info->has_return_value) { |
| 205 | return_value_info = PropertyInfo(*p_method_info->return_value_info); |
| 206 | return_value_metadata = GodotTypeInfo::Metadata(p_method_info->return_value_metadata); |
| 207 | } |
| 208 | |
| 209 | arguments_info.clear(); |
| 210 | arguments_metadata.clear(); |
| 211 | for (uint32_t i = 0; i < p_method_info->argument_count; i++) { |
| 212 | arguments_info.push_back(PropertyInfo(p_method_info->arguments_info[i])); |
| 213 | arguments_metadata.push_back(GodotTypeInfo::Metadata(p_method_info->arguments_metadata[i])); |
| 214 | } |
| 215 | |
| 216 | set_hint_flags(p_method_info->method_flags); |
| 217 | argument_count = p_method_info->argument_count; |
| 218 | vararg = p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_VARARG; |
| 219 | _set_returns(p_method_info->has_return_value); |
| 220 | _set_const(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_CONST); |
| 221 | _set_static(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_STATIC); |
| 222 | #ifdef DEBUG_ENABLED |
| 223 | _generate_argument_types(p_method_info->argument_count); |
| 224 | #endif // DEBUG_ENABLED |
| 225 | set_argument_count(p_method_info->argument_count); |
| 226 | |
| 227 | Vector<Variant> defargs; |
| 228 | defargs.resize(p_method_info->default_argument_count); |
| 229 | for (uint32_t i = 0; i < p_method_info->default_argument_count; i++) { |
| 230 | defargs.write[i] = *static_cast<Variant *>(p_method_info->default_arguments[i]); |
| 231 | } |
| 232 | |
| 233 | set_default_arguments(defargs); |
| 234 | } |
| 235 | |
| 236 | explicit GDExtensionMethodBind(const GDExtensionClassMethodInfo *p_method_info) { |
| 237 | update(p_method_info); |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | #ifndef DISABLE_DEPRECATED |
| 242 | void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs) { |
| 243 | const GDExtensionClassCreationInfo5 class_info5 = { |
| 244 | p_extension_funcs->is_virtual, // GDExtensionBool is_virtual; |
| 245 | p_extension_funcs->is_abstract, // GDExtensionBool is_abstract; |
| 246 | true, // GDExtensionBool is_exposed; |
| 247 | false, // GDExtensionBool is_runtime; |
| 248 | nullptr, // GDExtensionConstStringPtr icon_path; |
| 249 | p_extension_funcs->set_func, // GDExtensionClassSet set_func; |
| 250 | p_extension_funcs->get_func, // GDExtensionClassGet get_func; |
| 251 | p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func; |
| 252 | nullptr, // GDExtensionClassFreePropertyList2 free_property_list_func; |
| 253 | p_extension_funcs->property_can_revert_func, // GDExtensionClassPropertyCanRevert property_can_revert_func; |
| 254 | p_extension_funcs->property_get_revert_func, // GDExtensionClassPropertyGetRevert property_get_revert_func; |
| 255 | nullptr, // GDExtensionClassValidateProperty validate_property_func; |
| 256 | nullptr, // GDExtensionClassNotification2 notification_func; |
| 257 | p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func; |
| 258 | p_extension_funcs->reference_func, // GDExtensionClassReference reference_func; |
| 259 | p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func; |
| 260 | nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */ |
| 261 | p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ |
| 262 | nullptr, // GDExtensionClassRecreateInstance recreate_instance_func; |
| 263 | nullptr, // GDExtensionClassGetVirtual get_virtual_func; |
| 264 | nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func; |
| 265 | nullptr, // GDExtensionClassCallVirtualWithData call_virtual_func; |
| 266 | p_extension_funcs->class_userdata, // void *class_userdata; |
| 267 | }; |
| 268 | |
| 269 | const ClassCreationDeprecatedInfo legacy = { |
| 270 | false, |
| 271 | p_extension_funcs->notification_func, // GDExtensionClassNotification notification_func; |
| 272 | p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func; |
| 273 | p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; |
| 274 | p_extension_funcs->get_rid_func, // GDExtensionClassGetRID get_rid; |
| 275 | p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func; |
| 276 | nullptr, |
| 277 | }; |
| 278 | _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5, &legacy); |
| 279 | } |
| 280 | |
| 281 | void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs) { |
| 282 | const GDExtensionClassCreationInfo5 class_info5 = { |
| 283 | p_extension_funcs->is_virtual, // GDExtensionBool is_virtual; |
| 284 | p_extension_funcs->is_abstract, // GDExtensionBool is_abstract; |
| 285 | p_extension_funcs->is_exposed, // GDExtensionBool is_exposed; |
| 286 | false, // GDExtensionBool is_runtime; |
| 287 | nullptr, // GDExtensionConstStringPtr icon_path; |
| 288 | p_extension_funcs->set_func, // GDExtensionClassSet set_func; |
| 289 | p_extension_funcs->get_func, // GDExtensionClassGet get_func; |
| 290 | p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func; |
| 291 | nullptr, // GDExtensionClassFreePropertyList2 free_property_list_func; |
| 292 | p_extension_funcs->property_can_revert_func, // GDExtensionClassPropertyCanRevert property_can_revert_func; |
| 293 | p_extension_funcs->property_get_revert_func, // GDExtensionClassPropertyGetRevert property_get_revert_func; |
| 294 | p_extension_funcs->validate_property_func, // GDExtensionClassValidateProperty validate_property_func; |
| 295 | p_extension_funcs->notification_func, // GDExtensionClassNotification2 notification_func; |
| 296 | p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func; |
| 297 | p_extension_funcs->reference_func, // GDExtensionClassReference reference_func; |
| 298 | p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func; |
| 299 | nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */ |
| 300 | p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ |
| 301 | p_extension_funcs->recreate_instance_func, // GDExtensionClassRecreateInstance recreate_instance_func; |
| 302 | nullptr, // GDExtensionClassGetVirtual get_virtual_func; |
| 303 | nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func; |
| 304 | p_extension_funcs->call_virtual_with_data_func, // GDExtensionClassCallVirtualWithData call_virtual_func; |
| 305 | p_extension_funcs->class_userdata, // void *class_userdata; |
| 306 | }; |
| 307 | |
| 308 | const ClassCreationDeprecatedInfo legacy = { |
| 309 | !p_extension_funcs->is_exposed, // bool legacy_unexposed_class; |
| 310 | nullptr, // GDExtensionClassNotification notification_func; |
| 311 | p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func; |
| 312 | p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; |
| 313 | p_extension_funcs->get_rid_func, // GDExtensionClassGetRID get_rid; |
| 314 | p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func; |
| 315 | p_extension_funcs->get_virtual_call_data_func, // GDExtensionClassGetVirtual get_virtual_func; |
| 316 | }; |
| 317 | _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5, &legacy); |
| 318 | } |
| 319 | |
| 320 | void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs) { |
| 321 | const GDExtensionClassCreationInfo5 class_info5 = { |
| 322 | p_extension_funcs->is_virtual, // GDExtensionBool is_virtual; |
| 323 | p_extension_funcs->is_abstract, // GDExtensionBool is_abstract; |
| 324 | p_extension_funcs->is_exposed, // GDExtensionBool is_exposed; |
| 325 | p_extension_funcs->is_runtime, // GDExtensionBool is_runtime; |
| 326 | nullptr, // GDExtensionConstStringPtr icon_path; |
| 327 | p_extension_funcs->set_func, // GDExtensionClassSet set_func; |
| 328 | p_extension_funcs->get_func, // GDExtensionClassGet get_func; |
| 329 | p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func; |
| 330 | p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func; |
| 331 | p_extension_funcs->property_can_revert_func, // GDExtensionClassPropertyCanRevert property_can_revert_func; |
| 332 | p_extension_funcs->property_get_revert_func, // GDExtensionClassPropertyGetRevert property_get_revert_func; |
| 333 | p_extension_funcs->validate_property_func, // GDExtensionClassValidateProperty validate_property_func; |
| 334 | p_extension_funcs->notification_func, // GDExtensionClassNotification2 notification_func; |
| 335 | p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func; |
| 336 | p_extension_funcs->reference_func, // GDExtensionClassReference reference_func; |
| 337 | p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func; |
| 338 | nullptr, // GDExtensionClassCreateInstance2 create_instance_func; /* this one is mandatory */ |
| 339 | p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ |
| 340 | p_extension_funcs->recreate_instance_func, // GDExtensionClassRecreateInstance recreate_instance_func; |
| 341 | nullptr, // GDExtensionClassGetVirtual get_virtual_func; |
| 342 | nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func; |
| 343 | p_extension_funcs->call_virtual_with_data_func, // GDExtensionClassCallVirtualWithData call_virtual_func; |
| 344 | p_extension_funcs->class_userdata, // void *class_userdata; |
| 345 | }; |
| 346 | |
| 347 | const ClassCreationDeprecatedInfo legacy = { |
| 348 | !p_extension_funcs->is_exposed, // bool legacy_unexposed_class; |
| 349 | nullptr, // GDExtensionClassNotification notification_func; |
| 350 | nullptr, // GDExtensionClassFreePropertyList free_property_list_func; |
| 351 | p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance2 create_instance_func; |
| 352 | p_extension_funcs->get_rid_func, // GDExtensionClassGetRID get_rid; |
| 353 | p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func; |
| 354 | p_extension_funcs->get_virtual_call_data_func, // GDExtensionClassGetVirtual get_virtual_func; |
| 355 | }; |
| 356 | _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5, &legacy); |
| 357 | } |
| 358 | |
| 359 | void GDExtension::_register_extension_class4(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs) { |
| 360 | GDExtensionClassCreationInfo5 class_info5 = *p_extension_funcs; |
| 361 | const ClassCreationDeprecatedInfo legacy = { |
| 362 | !p_extension_funcs->is_exposed, // bool legacy_unexposed_class; |
| 363 | nullptr, // GDExtensionClassNotification notification_func; |
| 364 | nullptr, // GDExtensionClassFreePropertyList free_property_list_func; |
| 365 | nullptr, // GDExtensionClassCreateInstance2 create_instance_func; |
| 366 | nullptr, // GDExtensionClassGetRID get_rid; |
| 367 | nullptr, // GDExtensionClassGetVirtual get_virtual_func; |
| 368 | nullptr, // GDExtensionClassGetVirtual get_virtual_func; |
| 369 | }; |
| 370 | _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5, &legacy); |
| 371 | } |
| 372 | #endif // DISABLE_DEPRECATED |
| 373 | |
| 374 | void GDExtension::_register_extension_class5(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo5 *p_extension_funcs) { |
| 375 | _register_extension_class_internal(p_library, p_class_name, p_parent_class_name, p_extension_funcs); |
| 376 | } |
| 377 | |
| 378 | void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo5 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs) { |
| 379 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 380 | |
| 381 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 382 | StringName parent_class_name = *reinterpret_cast<const StringName *>(p_parent_class_name); |
| 383 | ERR_FAIL_COND_MSG(!String(class_name).is_valid_unicode_identifier(), vformat("Attempt to register extension class '%s', which is not a valid class identifier.", class_name)); |
| 384 | ERR_FAIL_COND_MSG(ClassDB::class_exists(class_name), vformat("Attempt to register extension class '%s', which appears to be already registered.", class_name)); |
| 385 | |
| 386 | Extension *parent_extension = nullptr; |
| 387 | |
| 388 | if (self->extension_classes.has(parent_class_name)) { |
| 389 | parent_extension = &self->extension_classes[parent_class_name]; |
| 390 | } else if (ClassDB::class_exists(parent_class_name)) { |
| 391 | if (ClassDB::get_api_type(parent_class_name) == ClassDB::API_EXTENSION || ClassDB::get_api_type(parent_class_name) == ClassDB::API_EDITOR_EXTENSION) { |
| 392 | ERR_PRINT("Unimplemented yet"); |
| 393 | //inheriting from another extension |
| 394 | } else { |
| 395 | //inheriting from engine class |
| 396 | } |
| 397 | } else { |
| 398 | ERR_FAIL_MSG(vformat("Attempt to register an extension class '%s' using non-existing parent class '%s'.", String(class_name), String(parent_class_name))); |
| 399 | } |
| 400 | |
| 401 | #ifdef TOOLS_ENABLED |
| 402 | Extension *extension = nullptr; |
| 403 | bool is_runtime = (bool)p_extension_funcs->is_runtime; |
| 404 | if (self->is_reloading && self->extension_classes.has(class_name)) { |
| 405 | extension = &self->extension_classes[class_name]; |
| 406 | if (!parent_extension && parent_class_name != extension->gdextension.parent_class_name) { |
| 407 | ERR_FAIL_MSG(vformat("GDExtension class '%s' cannot change parent type from '%s' to '%s' on hot reload. Restart Godot for this change to take effect.", class_name, extension->gdextension.parent_class_name, parent_class_name)); |
| 408 | } |
| 409 | if (extension->gdextension.is_runtime != is_runtime) { |
| 410 | ERR_PRINT(vformat("GDExtension class '%s' cannot change to/from runtime class on hot reload. Restart Godot for this change to take effect.", class_name)); |
| 411 | is_runtime = extension->gdextension.is_runtime; |
| 412 | } |
| 413 | extension->is_reloading = false; |
| 414 | } else { |
| 415 | self->extension_classes[class_name] = Extension(); |
| 416 | extension = &self->extension_classes[class_name]; |
| 417 | } |
| 418 | #else |
| 419 | self->extension_classes[class_name] = Extension(); |
| 420 | Extension *extension = &self->extension_classes[class_name]; |
| 421 | #endif |
| 422 | |
| 423 | if (parent_extension) { |
| 424 | extension->gdextension.parent = &parent_extension->gdextension; |
| 425 | parent_extension->gdextension.children.push_back(&extension->gdextension); |
| 426 | } |
| 427 | |
| 428 | if (self->reloadable && p_extension_funcs->recreate_instance_func == nullptr) { |
| 429 | bool can_create_class = (bool)p_extension_funcs->create_instance_func; |
| 430 | #ifndef DISABLE_DEPRECATED |
| 431 | if (!can_create_class && p_deprecated_funcs) { |
| 432 | can_create_class = (bool)p_deprecated_funcs->create_instance_func; |
| 433 | } |
| 434 | #endif |
| 435 | if (can_create_class) { |
| 436 | ERR_PRINT(vformat("Extension marked as reloadable, but attempted to register class '%s' which doesn't support reloading. Perhaps your language binding don't support it? Reloading disabled for this extension.", class_name)); |
| 437 | self->reloadable = false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | extension->gdextension.library = self; |
| 442 | extension->gdextension.parent_class_name = parent_class_name; |
| 443 | extension->gdextension.class_name = class_name; |
| 444 | extension->gdextension.editor_class = self->level_initialized == INITIALIZATION_LEVEL_EDITOR; |
| 445 | extension->gdextension.is_virtual = p_extension_funcs->is_virtual; |
| 446 | extension->gdextension.is_abstract = p_extension_funcs->is_abstract; |
| 447 | extension->gdextension.is_exposed = p_extension_funcs->is_exposed; |
| 448 | #ifdef TOOLS_ENABLED |
| 449 | extension->gdextension.is_runtime = is_runtime; |
| 450 | #endif |
| 451 | extension->gdextension.set = p_extension_funcs->set_func; |
| 452 | extension->gdextension.get = p_extension_funcs->get_func; |
| 453 | extension->gdextension.get_property_list = p_extension_funcs->get_property_list_func; |
| 454 | extension->gdextension.free_property_list2 = p_extension_funcs->free_property_list_func; |
| 455 | extension->gdextension.property_can_revert = p_extension_funcs->property_can_revert_func; |
| 456 | extension->gdextension.property_get_revert = p_extension_funcs->property_get_revert_func; |
| 457 | extension->gdextension.validate_property = p_extension_funcs->validate_property_func; |
| 458 | #ifndef DISABLE_DEPRECATED |
| 459 | if (p_deprecated_funcs) { |
| 460 | extension->gdextension.legacy_unexposed_class = p_deprecated_funcs->legacy_unexposed_class; |
| 461 | extension->gdextension.notification = p_deprecated_funcs->notification_func; |
| 462 | extension->gdextension.free_property_list = p_deprecated_funcs->free_property_list_func; |
| 463 | extension->gdextension.create_instance = p_deprecated_funcs->create_instance_func; |
| 464 | extension->gdextension.get_rid = p_deprecated_funcs->get_rid_func; |
| 465 | extension->gdextension.get_virtual = p_deprecated_funcs->get_virtual_func; |
| 466 | extension->gdextension.get_virtual_call_data = p_deprecated_funcs->get_virtual_call_data_func; |
| 467 | } |
| 468 | #endif // DISABLE_DEPRECATED |
| 469 | extension->gdextension.notification2 = p_extension_funcs->notification_func; |
| 470 | extension->gdextension.to_string = p_extension_funcs->to_string_func; |
| 471 | extension->gdextension.reference = p_extension_funcs->reference_func; |
| 472 | extension->gdextension.unreference = p_extension_funcs->unreference_func; |
| 473 | extension->gdextension.class_userdata = p_extension_funcs->class_userdata; |
| 474 | extension->gdextension.create_instance2 = p_extension_funcs->create_instance_func; |
| 475 | extension->gdextension.free_instance = p_extension_funcs->free_instance_func; |
| 476 | extension->gdextension.recreate_instance = p_extension_funcs->recreate_instance_func; |
| 477 | extension->gdextension.get_virtual2 = p_extension_funcs->get_virtual_func; |
| 478 | extension->gdextension.get_virtual_call_data2 = p_extension_funcs->get_virtual_call_data_func; |
| 479 | extension->gdextension.call_virtual_with_data = p_extension_funcs->call_virtual_with_data_func; |
| 480 | |
| 481 | extension->gdextension.reloadable = self->reloadable; |
| 482 | #ifdef TOOLS_ENABLED |
| 483 | if (extension->gdextension.reloadable) { |
| 484 | extension->gdextension.tracking_userdata = extension; |
| 485 | extension->gdextension.track_instance = &GDExtension::_track_instance; |
| 486 | extension->gdextension.untrack_instance = &GDExtension::_untrack_instance; |
| 487 | } else { |
| 488 | extension->gdextension.tracking_userdata = nullptr; |
| 489 | extension->gdextension.track_instance = nullptr; |
| 490 | extension->gdextension.untrack_instance = nullptr; |
| 491 | } |
| 492 | #endif |
| 493 | |
| 494 | extension->gdextension.create_gdtype(); |
| 495 | |
| 496 | ClassDB::register_extension_class(&extension->gdextension); |
| 497 | |
| 498 | if (p_extension_funcs->icon_path != nullptr) { |
| 499 | const String icon_path = *reinterpret_cast<const String *>(p_extension_funcs->icon_path); |
| 500 | if (!icon_path.is_empty()) { |
| 501 | self->class_icon_paths[class_name] = icon_path; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void GDExtension::_register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info) { |
| 507 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 508 | |
| 509 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 510 | StringName method_name = *reinterpret_cast<const StringName *>(p_method_info->name); |
| 511 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension method '%s' for unexisting class '%s'.", String(method_name), class_name)); |
| 512 | |
| 513 | #ifdef TOOLS_ENABLED |
| 514 | Extension *extension = &self->extension_classes[class_name]; |
| 515 | GDExtensionMethodBind *method = nullptr; |
| 516 | |
| 517 | // If the extension is still marked as reloading, that means it failed to register again. |
| 518 | if (extension->is_reloading) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (self->is_reloading && extension->methods.has(method_name)) { |
| 523 | method = extension->methods[method_name]; |
| 524 | |
| 525 | // Try to update the method bind. If it doesn't work (because it's incompatible) then |
| 526 | // mark as invalid and create a new one. |
| 527 | if (!method->is_reloading || !method->try_update(p_method_info)) { |
| 528 | method->valid = false; |
| 529 | self->invalid_methods.push_back(method); |
| 530 | |
| 531 | method = nullptr; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (method == nullptr) { |
| 536 | method = memnew(GDExtensionMethodBind(p_method_info)); |
| 537 | method->set_instance_class(class_name); |
| 538 | extension->methods[method_name] = method; |
| 539 | } else { |
| 540 | method->is_reloading = false; |
| 541 | } |
| 542 | #else |
| 543 | GDExtensionMethodBind *method = memnew(GDExtensionMethodBind(p_method_info)); |
| 544 | method->set_instance_class(class_name); |
| 545 | #endif |
| 546 | |
| 547 | ClassDB::bind_method_custom(class_name, method); |
| 548 | } |
| 549 | |
| 550 | void GDExtension::_register_extension_class_virtual_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassVirtualMethodInfo *p_method_info) { |
| 551 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 552 | ClassDB::add_extension_class_virtual_method(class_name, p_method_info); |
| 553 | } |
| 554 | |
| 555 | 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) { |
| 556 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 557 | |
| 558 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 559 | StringName enum_name = *reinterpret_cast<const StringName *>(p_enum_name); |
| 560 | StringName constant_name = *reinterpret_cast<const StringName *>(p_constant_name); |
| 561 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension constant '%s' for unexisting class '%s'.", constant_name, class_name)); |
| 562 | |
| 563 | #ifdef TOOLS_ENABLED |
| 564 | // If the extension is still marked as reloading, that means it failed to register again. |
| 565 | Extension *extension = &self->extension_classes[class_name]; |
| 566 | if (extension->is_reloading) { |
| 567 | return; |
| 568 | } |
| 569 | #endif |
| 570 | |
| 571 | ClassDB::bind_integer_constant(class_name, enum_name, constant_name, p_constant_value, p_is_bitfield); |
| 572 | } |
| 573 | |
| 574 | void GDExtension::_register_extension_class_property(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter) { |
| 575 | _register_extension_class_property_indexed(p_library, p_class_name, p_info, p_setter, p_getter, -1); |
| 576 | } |
| 577 | |
| 578 | void GDExtension::_register_extension_class_property_indexed(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter, GDExtensionInt p_index) { |
| 579 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 580 | |
| 581 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 582 | StringName setter = *reinterpret_cast<const StringName *>(p_setter); |
| 583 | StringName getter = *reinterpret_cast<const StringName *>(p_getter); |
| 584 | String property_name = *reinterpret_cast<const StringName *>(p_info->name); |
| 585 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension class property '%s' for unexisting class '%s'.", property_name, class_name)); |
| 586 | |
| 587 | #ifdef TOOLS_ENABLED |
| 588 | // If the extension is still marked as reloading, that means it failed to register again. |
| 589 | Extension *extension = &self->extension_classes[class_name]; |
| 590 | if (extension->is_reloading) { |
| 591 | return; |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | PropertyInfo pinfo(*p_info); |
| 596 | |
| 597 | ClassDB::add_property(class_name, pinfo, setter, getter, p_index); |
| 598 | } |
| 599 | |
| 600 | void GDExtension::_register_extension_class_property_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_group_name, GDExtensionConstStringPtr p_prefix) { |
| 601 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 602 | |
| 603 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 604 | String group_name = *reinterpret_cast<const String *>(p_group_name); |
| 605 | String prefix = *reinterpret_cast<const String *>(p_prefix); |
| 606 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension class property group '%s' for unexisting class '%s'.", group_name, class_name)); |
| 607 | |
| 608 | #ifdef TOOLS_ENABLED |
| 609 | // If the extension is still marked as reloading, that means it failed to register again. |
| 610 | Extension *extension = &self->extension_classes[class_name]; |
| 611 | if (extension->is_reloading) { |
| 612 | return; |
| 613 | } |
| 614 | #endif |
| 615 | |
| 616 | ClassDB::add_property_group(class_name, group_name, prefix); |
| 617 | } |
| 618 | |
| 619 | void GDExtension::_register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_subgroup_name, GDExtensionConstStringPtr p_prefix) { |
| 620 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 621 | |
| 622 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 623 | String subgroup_name = *reinterpret_cast<const String *>(p_subgroup_name); |
| 624 | String prefix = *reinterpret_cast<const String *>(p_prefix); |
| 625 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension class property subgroup '%s' for unexisting class '%s'.", subgroup_name, class_name)); |
| 626 | |
| 627 | #ifdef TOOLS_ENABLED |
| 628 | // If the extension is still marked as reloading, that means it failed to register again. |
| 629 | Extension *extension = &self->extension_classes[class_name]; |
| 630 | if (extension->is_reloading) { |
| 631 | return; |
| 632 | } |
| 633 | #endif |
| 634 | |
| 635 | ClassDB::add_property_subgroup(class_name, subgroup_name, prefix); |
| 636 | } |
| 637 | |
| 638 | 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) { |
| 639 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 640 | |
| 641 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 642 | StringName signal_name = *reinterpret_cast<const StringName *>(p_signal_name); |
| 643 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to register extension class signal '%s' for unexisting class '%s'.", signal_name, class_name)); |
| 644 | |
| 645 | #ifdef TOOLS_ENABLED |
| 646 | // If the extension is still marked as reloading, that means it failed to register again. |
| 647 | Extension *extension = &self->extension_classes[class_name]; |
| 648 | if (extension->is_reloading) { |
| 649 | return; |
| 650 | } |
| 651 | #endif |
| 652 | |
| 653 | MethodInfo s; |
| 654 | s.name = signal_name; |
| 655 | for (int i = 0; i < p_argument_count; i++) { |
| 656 | PropertyInfo arg(p_argument_info[i]); |
| 657 | s.arguments.push_back(arg); |
| 658 | } |
| 659 | ClassDB::add_signal(class_name, s); |
| 660 | } |
| 661 | |
| 662 | void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name) { |
| 663 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 664 | |
| 665 | StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); |
| 666 | ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), vformat("Attempt to unregister unexisting extension class '%s'.", class_name)); |
| 667 | |
| 668 | Extension *ext = &self->extension_classes[class_name]; |
| 669 | #ifdef TOOLS_ENABLED |
| 670 | if (ext->is_reloading) { |
| 671 | self->_clear_extension(ext); |
| 672 | } |
| 673 | #endif |
| 674 | ERR_FAIL_COND_MSG(ext->gdextension.children.size(), vformat("Attempt to unregister class '%s' while other extension classes inherit from it.", class_name)); |
| 675 | |
| 676 | #ifdef TOOLS_ENABLED |
| 677 | ClassDB::unregister_extension_class(class_name, !ext->is_reloading); |
| 678 | #else |
| 679 | ClassDB::unregister_extension_class(class_name); |
| 680 | #endif |
| 681 | |
| 682 | if (ext->gdextension.parent != nullptr) { |
| 683 | ext->gdextension.parent->children.erase(&ext->gdextension); |
| 684 | } |
| 685 | |
| 686 | #ifdef TOOLS_ENABLED |
| 687 | if (!ext->is_reloading) { |
| 688 | self->extension_classes.erase(class_name); |
| 689 | } |
| 690 | |
| 691 | GDExtensionEditorHelp::remove_class(class_name); |
| 692 | #else |
| 693 | self->extension_classes.erase(class_name); |
| 694 | #endif |
| 695 | } |
| 696 | |
| 697 | void GDExtension::_get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringPtr r_path) { |
| 698 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 699 | |
| 700 | Ref<GDExtensionLibraryLoader> library_loader = self->loader; |
| 701 | String library_path; |
| 702 | if (library_loader.is_valid()) { |
| 703 | library_path = library_loader->library_path; |
| 704 | } |
| 705 | |
| 706 | memnew_placement(r_path, String(library_path)); |
| 707 | } |
| 708 | |
| 709 | void GDExtension::_register_get_classes_used_callback(GDExtensionClassLibraryPtr p_library, GDExtensionEditorGetClassesUsedCallback p_callback) { |
| 710 | #ifdef TOOLS_ENABLED |
| 711 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 712 | self->get_classes_used_callback = p_callback; |
| 713 | #endif |
| 714 | } |
| 715 | |
| 716 | void GDExtension::_register_main_loop_callbacks(GDExtensionClassLibraryPtr p_library, const GDExtensionMainLoopCallbacks *p_callbacks) { |
| 717 | GDExtension *self = reinterpret_cast<GDExtension *>(p_library); |
| 718 | self->startup_callback = p_callbacks->startup_func; |
| 719 | self->shutdown_callback = p_callbacks->shutdown_func; |
| 720 | self->frame_callback = p_callbacks->frame_func; |
| 721 | } |
| 722 | |
| 723 | void GDExtension::register_interface_function(const StringName &p_function_name, GDExtensionInterfaceFunctionPtr p_function_pointer) { |
| 724 | ERR_FAIL_COND_MSG(gdextension_interface_functions.has(p_function_name), vformat("Attempt to register interface function '%s', which appears to be already registered.", p_function_name)); |
| 725 | gdextension_interface_functions.insert(p_function_name, p_function_pointer); |
| 726 | } |
| 727 | |
| 728 | GDExtensionInterfaceFunctionPtr GDExtension::get_interface_function(const StringName &p_function_name) { |
| 729 | GDExtensionInterfaceFunctionPtr *function = gdextension_interface_functions.getptr(p_function_name); |
| 730 | ERR_FAIL_NULL_V_MSG(function, nullptr, vformat("Attempt to get non-existent interface function: '%s'.", String(p_function_name))); |
| 731 | return *function; |
| 732 | } |
| 733 | |
| 734 | Error GDExtension::open_library(const String &p_path, const Ref<GDExtensionLoader> &p_loader) { |
| 735 | ERR_FAIL_COND_V_MSG(p_loader.is_null(), FAILED, "Can't open GDExtension without a loader."); |
| 736 | loader = p_loader; |
| 737 | |
| 738 | Error err = loader->open_library(p_path); |
| 739 | |
| 740 | ERR_FAIL_COND_V_MSG(err == ERR_FILE_NOT_FOUND, err, vformat("GDExtension dynamic library not found: '%s'.", p_path)); |
| 741 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Can't open GDExtension dynamic library: '%s'.", p_path)); |
| 742 | |
| 743 | err = loader->initialize(&gdextension_get_proc_address, this, &initialization); |
| 744 | |
| 745 | if (err != OK) { |
| 746 | // Errors already logged in initialize(). |
| 747 | loader->close_library(); |
| 748 | return err; |
| 749 | } |
| 750 | |
| 751 | level_initialized = -1; |
| 752 | |
| 753 | return OK; |
| 754 | } |
| 755 | |
| 756 | void GDExtension::close_library() { |
| 757 | ERR_FAIL_COND(!is_library_open()); |
| 758 | loader->close_library(); |
| 759 | |
| 760 | class_icon_paths.clear(); |
| 761 | |
| 762 | #ifdef TOOLS_ENABLED |
| 763 | instance_bindings.clear(); |
| 764 | #endif |
| 765 | } |
| 766 | |
| 767 | bool GDExtension::is_library_open() const { |
| 768 | return loader.is_valid() && loader->is_library_open(); |
| 769 | } |
| 770 | |
| 771 | GDExtension::InitializationLevel GDExtension::get_minimum_library_initialization_level() const { |
| 772 | ERR_FAIL_COND_V(!is_library_open(), INITIALIZATION_LEVEL_CORE); |
| 773 | return InitializationLevel(initialization.minimum_initialization_level); |
| 774 | } |
| 775 | |
| 776 | void GDExtension::initialize_library(InitializationLevel p_level) { |
| 777 | ERR_FAIL_COND(!is_library_open()); |
| 778 | 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)); |
| 779 | |
| 780 | level_initialized = int32_t(p_level); |
| 781 | |
| 782 | ERR_FAIL_NULL(initialization.initialize); |
| 783 | |
| 784 | initialization.initialize(initialization.userdata, GDExtensionInitializationLevel(p_level)); |
| 785 | } |
| 786 | void GDExtension::deinitialize_library(InitializationLevel p_level) { |
| 787 | ERR_FAIL_COND(!is_library_open()); |
| 788 | ERR_FAIL_COND(p_level > int32_t(level_initialized)); |
| 789 | |
| 790 | level_initialized = int32_t(p_level) - 1; |
| 791 | |
| 792 | ERR_FAIL_NULL(initialization.deinitialize); |
| 793 | |
| 794 | initialization.deinitialize(initialization.userdata, GDExtensionInitializationLevel(p_level)); |
| 795 | } |
| 796 | |
| 797 | void GDExtension::_bind_methods() { |
| 798 | ClassDB::bind_method(D_METHOD("is_library_open"), &GDExtension::is_library_open); |
| 799 | ClassDB::bind_method(D_METHOD("get_minimum_library_initialization_level"), &GDExtension::get_minimum_library_initialization_level); |
| 800 | |
| 801 | BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_CORE); |
| 802 | BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_SERVERS); |
| 803 | BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_SCENE); |
| 804 | BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_EDITOR); |
| 805 | } |
| 806 | |
| 807 | GDExtension::~GDExtension() { |
| 808 | if (is_library_open()) { |
| 809 | close_library(); |
| 810 | } |
| 811 | #ifdef TOOLS_ENABLED |
| 812 | // If we have any invalid method binds still laying around, we can finally free them! |
| 813 | for (GDExtensionMethodBind *E : invalid_methods) { |
| 814 | memdelete(E); |
| 815 | } |
| 816 | #endif |
| 817 | } |
| 818 | |
| 819 | void GDExtension::initialize_gdextensions() { |
| 820 | gdextension_setup_interface(); |
| 821 | |
| 822 | #ifndef DISABLE_DEPRECATED |
| 823 | register_interface_function("classdb_register_extension_class", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class); |
| 824 | register_interface_function("classdb_register_extension_class2", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class2); |
| 825 | register_interface_function("classdb_register_extension_class3", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class3); |
| 826 | register_interface_function("classdb_register_extension_class4", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class4); |
| 827 | #endif // DISABLE_DEPRECATED |
| 828 | register_interface_function("classdb_register_extension_class5", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class5); |
| 829 | register_interface_function("classdb_register_extension_class_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_method); |
| 830 | register_interface_function("classdb_register_extension_class_virtual_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_virtual_method); |
| 831 | register_interface_function("classdb_register_extension_class_integer_constant", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_integer_constant); |
| 832 | register_interface_function("classdb_register_extension_class_property", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_property); |
| 833 | register_interface_function("classdb_register_extension_class_property_indexed", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_property_indexed); |
| 834 | register_interface_function("classdb_register_extension_class_property_group", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_property_group); |
| 835 | register_interface_function("classdb_register_extension_class_property_subgroup", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_property_subgroup); |
| 836 | register_interface_function("classdb_register_extension_class_signal", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_signal); |
| 837 | register_interface_function("classdb_unregister_extension_class", (GDExtensionInterfaceFunctionPtr)&GDExtension::_unregister_extension_class); |
| 838 | register_interface_function("get_library_path", (GDExtensionInterfaceFunctionPtr)&GDExtension::_get_library_path); |
| 839 | register_interface_function("editor_register_get_classes_used_callback", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_get_classes_used_callback); |
| 840 | register_interface_function("register_main_loop_callbacks", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_main_loop_callbacks); |
| 841 | } |
| 842 | |
| 843 | void GDExtension::finalize_gdextensions() { |
| 844 | gdextension_interface_functions.clear(); |
| 845 | } |
| 846 | |
| 847 | Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension) { |
| 848 | 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."); |
| 849 | |
| 850 | GDExtensionManager *extension_manager = GDExtensionManager::get_singleton(); |
| 851 | |
| 852 | GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path); |
| 853 | if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) { |
| 854 | // Errors already logged in load_extension(). |
| 855 | return FAILED; |
| 856 | } |
| 857 | |
| 858 | p_extension = extension_manager->get_extension(p_path); |
| 859 | return OK; |
| 860 | } |
| 861 | |
| 862 | 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) { |
| 863 | // We can't have two GDExtension resource object representing the same library, because |
| 864 | // loading (or unloading) a GDExtension affects global data. So, we need reuse the same |
| 865 | // object if one has already been loaded (even if caching is disabled at the resource |
| 866 | // loader level). |
| 867 | GDExtensionManager *manager = GDExtensionManager::get_singleton(); |
| 868 | if (manager->is_extension_loaded(p_path)) { |
| 869 | return manager->get_extension(p_path); |
| 870 | } |
| 871 | |
| 872 | Ref<GDExtension> lib; |
| 873 | Error err = load_gdextension_resource(p_path, lib); |
| 874 | if (err != OK && r_error) { |
| 875 | // Errors already logged in load_gdextension_resource(). |
| 876 | *r_error = err; |
| 877 | } |
| 878 | return lib; |
| 879 | } |
| 880 | |
| 881 | void GDExtensionResourceLoader::get_recognized_extensions(List<String> *p_extensions) const { |
| 882 | p_extensions->push_back("gdextension"); |
| 883 | } |
| 884 | |
| 885 | bool GDExtensionResourceLoader::handles_type(const String &p_type) const { |
| 886 | return p_type == "GDExtension"; |
| 887 | } |
| 888 | |
| 889 | String GDExtensionResourceLoader::get_resource_type(const String &p_path) const { |
| 890 | if (p_path.has_extension("gdextension")) { |
| 891 | return "GDExtension"; |
| 892 | } |
| 893 | return ""; |
| 894 | } |
| 895 | |
| 896 | #ifdef TOOLS_ENABLED |
| 897 | void GDExtensionResourceLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) { |
| 898 | Ref<GDExtension> gdext = ResourceLoader::load(p_path); |
| 899 | if (gdext.is_null()) { |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | for (const StringName class_name : gdext->get_classes_used()) { |
| 904 | if (ClassDB::class_exists(class_name)) { |
| 905 | r_classes->insert(class_name); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | bool GDExtension::has_library_changed() const { |
| 911 | return loader->has_library_changed(); |
| 912 | } |
| 913 | |
| 914 | void GDExtension::prepare_reload() { |
| 915 | is_reloading = true; |
| 916 | |
| 917 | for (KeyValue<StringName, Extension> &E : extension_classes) { |
| 918 | E.value.is_reloading = true; |
| 919 | |
| 920 | for (KeyValue<StringName, GDExtensionMethodBind *> &M : E.value.methods) { |
| 921 | M.value->is_reloading = true; |
| 922 | } |
| 923 | |
| 924 | for (const ObjectID &obj_id : E.value.instances) { |
| 925 | Object *obj = ObjectDB::get_instance(obj_id); |
| 926 | if (!obj) { |
| 927 | continue; |
| 928 | } |
| 929 | |
| 930 | // Store instance state so it can be restored after reload. |
| 931 | List<Pair<String, Variant>> state; |
| 932 | List<PropertyInfo> prop_list; |
| 933 | obj->get_property_list(&prop_list); |
| 934 | for (const PropertyInfo &P : prop_list) { |
| 935 | if (!(P.usage & PROPERTY_USAGE_STORAGE)) { |
| 936 | continue; |
| 937 | } |
| 938 | |
| 939 | Variant value = obj->get(P.name); |
| 940 | Variant default_value = ClassDB::class_get_default_property_value(obj->get_class_name(), P.name); |
| 941 | |
| 942 | if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) { |
| 943 | continue; |
| 944 | } |
| 945 | |
| 946 | if (P.type == Variant::OBJECT && value.is_zero() && !(P.usage & PROPERTY_USAGE_STORE_IF_NULL)) { |
| 947 | continue; |
| 948 | } |
| 949 | |
| 950 | state.push_back(Pair<String, Variant>(P.name, value)); |
| 951 | } |
| 952 | E.value.instance_state[obj_id] = { |
| 953 | state, // List<Pair<String, Variant>> properties; |
| 954 | obj->is_extension_placeholder(), // bool is_placeholder; |
| 955 | }; |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | void GDExtension::_clear_extension(Extension *p_extension) { |
| 961 | // Clear out hierarchy information because it may change. |
| 962 | p_extension->gdextension.parent = nullptr; |
| 963 | p_extension->gdextension.children.clear(); |
| 964 | |
| 965 | // Clear all objects of any GDExtension data. It will become its native parent class |
| 966 | // until the reload can reset the object with the new GDExtension data. |
| 967 | for (const ObjectID &obj_id : p_extension->instances) { |
| 968 | Object *obj = ObjectDB::get_instance(obj_id); |
| 969 | if (!obj) { |
| 970 | continue; |
| 971 | } |
| 972 | |
| 973 | obj->clear_internal_extension(); |
| 974 | } |
| 975 | |
| 976 | p_extension->gdextension.destroy_gdtype(); |
| 977 | } |
| 978 | |
| 979 | void GDExtension::track_instance_binding(Object *p_object) { |
| 980 | instance_bindings.push_back(p_object->get_instance_id()); |
| 981 | } |
| 982 | |
| 983 | void GDExtension::untrack_instance_binding(Object *p_object) { |
| 984 | instance_bindings.erase(p_object->get_instance_id()); |
| 985 | } |
| 986 | |
| 987 | void GDExtension::clear_instance_bindings() { |
| 988 | for (ObjectID obj_id : instance_bindings) { |
| 989 | Object *obj = ObjectDB::get_instance(obj_id); |
| 990 | if (!obj) { |
| 991 | continue; |
| 992 | } |
| 993 | |
| 994 | obj->free_instance_binding(this); |
| 995 | } |
| 996 | instance_bindings.clear(); |
| 997 | } |
| 998 | |
| 999 | void GDExtension::finish_reload() { |
| 1000 | is_reloading = false; |
| 1001 | |
| 1002 | // Clean up any classes or methods that didn't get re-added. |
| 1003 | Vector<StringName> classes_to_remove; |
| 1004 | for (KeyValue<StringName, Extension> &E : extension_classes) { |
| 1005 | if (E.value.is_reloading) { |
| 1006 | E.value.is_reloading = false; |
| 1007 | classes_to_remove.push_back(E.key); |
| 1008 | } |
| 1009 | |
| 1010 | Vector<StringName> methods_to_remove; |
| 1011 | for (KeyValue<StringName, GDExtensionMethodBind *> &M : E.value.methods) { |
| 1012 | if (M.value->is_reloading) { |
| 1013 | M.value->valid = false; |
| 1014 | invalid_methods.push_back(M.value); |
| 1015 | |
| 1016 | M.value->is_reloading = false; |
| 1017 | methods_to_remove.push_back(M.key); |
| 1018 | } |
| 1019 | } |
| 1020 | for (const StringName &method_name : methods_to_remove) { |
| 1021 | E.value.methods.erase(method_name); |
| 1022 | } |
| 1023 | } |
| 1024 | for (const StringName &class_name : classes_to_remove) { |
| 1025 | extension_classes.erase(class_name); |
| 1026 | } |
| 1027 | |
| 1028 | // Reset any the extension on instances made from the classes that remain. |
| 1029 | for (KeyValue<StringName, Extension> &E : extension_classes) { |
| 1030 | // Loop over 'instance_state' rather than 'instance' because new instances |
| 1031 | // may have been created when re-initializing the extension. |
| 1032 | for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) { |
| 1033 | Object *obj = ObjectDB::get_instance(S.key); |
| 1034 | if (!obj) { |
| 1035 | continue; |
| 1036 | } |
| 1037 | |
| 1038 | if (S.value.is_placeholder) { |
| 1039 | obj->reset_internal_extension(ClassDB::get_placeholder_extension(E.value.gdextension.class_name)); |
| 1040 | } else { |
| 1041 | obj->reset_internal_extension(&E.value.gdextension); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | // Now that all the classes are back, restore the state. |
| 1047 | for (KeyValue<StringName, Extension> &E : extension_classes) { |
| 1048 | for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) { |
| 1049 | Object *obj = ObjectDB::get_instance(S.key); |
| 1050 | if (!obj) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | for (const Pair<String, Variant> &state : S.value.properties) { |
| 1055 | obj->set(state.first, state.second); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // Finally, let the objects know that we are done reloading them. |
| 1061 | for (KeyValue<StringName, Extension> &E : extension_classes) { |
| 1062 | for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) { |
| 1063 | Object *obj = ObjectDB::get_instance(S.key); |
| 1064 | if (!obj) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | obj->notification(NOTIFICATION_EXTENSION_RELOADED); |
| 1069 | } |
| 1070 | |
| 1071 | // Clear the instance state, we're done looping. |
| 1072 | E.value.instance_state.clear(); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void GDExtension::_track_instance(void *p_user_data, void *p_instance) { |
| 1077 | Extension *extension = reinterpret_cast<Extension *>(p_user_data); |
| 1078 | Object *obj = reinterpret_cast<Object *>(p_instance); |
| 1079 | |
| 1080 | extension->instances.insert(obj->get_instance_id()); |
| 1081 | } |
| 1082 | |
| 1083 | void GDExtension::_untrack_instance(void *p_user_data, void *p_instance) { |
| 1084 | Extension *extension = reinterpret_cast<Extension *>(p_user_data); |
| 1085 | Object *obj = reinterpret_cast<Object *>(p_instance); |
| 1086 | |
| 1087 | extension->instances.erase(obj->get_instance_id()); |
| 1088 | } |
| 1089 | |
| 1090 | PackedStringArray GDExtension::get_classes_used() const { |
| 1091 | PackedStringArray ret; |
| 1092 | if (get_classes_used_callback) { |
| 1093 | get_classes_used_callback((GDExtensionTypePtr)&ret); |
| 1094 | } |
| 1095 | return ret; |
| 1096 | } |
| 1097 | |
| 1098 | void GDExtensionEditorPlugins::add_extension_class(const StringName &p_class_name) { |
| 1099 | if (editor_node_add_plugin) { |
| 1100 | callable_mp_static(editor_node_add_plugin).call_deferred(p_class_name); |
| 1101 | } else { |
| 1102 | extension_classes.push_back(p_class_name); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void GDExtensionEditorPlugins::remove_extension_class(const StringName &p_class_name) { |
| 1107 | if (editor_node_remove_plugin) { |
| 1108 | editor_node_remove_plugin(p_class_name); |
| 1109 | } else { |
| 1110 | extension_classes.erase(p_class_name); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | void GDExtensionEditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) { |
| 1115 | ERR_FAIL_NULL(editor_help_load_xml_buffer); |
| 1116 | editor_help_load_xml_buffer(p_buffer, p_size); |
| 1117 | } |
| 1118 | |
| 1119 | void GDExtensionEditorHelp::remove_class(const String &p_class) { |
| 1120 | ERR_FAIL_NULL(editor_help_remove_class); |
| 1121 | editor_help_remove_class(p_class); |
| 1122 | } |
| 1123 | #endif // TOOLS_ENABLED |