From b4632e317d06acac62b2d5f4af02ce4dbdd57687 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 21 Sep 2021 04:01:28 +0200 Subject: [PATCH] Fix creation (and godot-side deletion) of extended objects. Proper initialization for godot-cpp classes with memnew. Extension classes (i.e. the `GDCLASS` macro) behave differently from regular wrapped classes, and requires Godot to initialize them during object construction. This commit update the GDCLASS macro to not create/destroy the instance during the bindings callback, but during the extension callbacks. When setting the object instance, the bindings instance is set to the pointer of the extension instance so that it can later be retrieved normally via `object_get_instance_bindings`. --- godot-headers-temp/godot/gdnative_interface.h | 4 +++- include/godot_cpp/classes/wrapped.hpp | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/godot-headers-temp/godot/gdnative_interface.h b/godot-headers-temp/godot/gdnative_interface.h index 13aa273b..04c03632 100644 --- a/godot-headers-temp/godot/gdnative_interface.h +++ b/godot-headers-temp/godot/gdnative_interface.h @@ -137,6 +137,7 @@ typedef void *GDNativeStringNamePtr; typedef void *GDNativeStringPtr; typedef void *GDNativeObjectPtr; typedef void *GDNativeTypePtr; +typedef void *GDNativeExtensionPtr; typedef void *GDNativeMethodBindPtr; typedef int64_t GDNativeInt; typedef uint8_t GDNativeBool; @@ -428,7 +429,8 @@ typedef struct { /* CLASSDB */ - GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname); + GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname, GDNativeExtensionPtr *r_extension); + GDNativeObjectPtr (*classdb_construct_object)(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension); GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); void *(*classdb_get_class_tag)(const char *p_classname); diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 2a0422ae..0fb0a893 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -137,20 +137,22 @@ public: } \ \ static void free(void *data, GDExtensionClassInstancePtr ptr) { \ - Memory::free_static(reinterpret_cast(ptr)); \ + if (ptr) { \ + m_class *cls = reinterpret_cast(ptr); \ + cls->~m_class(); \ + ::godot::Memory::free_static(cls); \ + } \ } \ \ static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \ - reinterpret_cast(p_instance)->_owner = reinterpret_cast(p_object_instance); \ + godot::internal::interface->object_set_instance_binding(p_object_instance, godot::internal::token, p_instance, &m_class::___binding_callbacks); \ + reinterpret_cast(p_instance)->_owner = reinterpret_cast(p_object_instance); \ } \ \ static void *___binding_create_callback(void *p_token, void *p_instance) { \ - m_class *result = new ("") m_class; \ - result->_owner = reinterpret_cast(p_instance); \ - return result; \ + return nullptr; \ } \ static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \ - Memory::free_static(reinterpret_cast(p_binding)); \ } \ static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \ return true; \ @@ -162,9 +164,10 @@ public: }; \ \ static m_class *_new() { \ - static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class); \ + static GDNativeExtensionPtr ___extension = nullptr; \ + static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class, &___extension); \ CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ - GDNativeObjectPtr obj = ___constructor(); \ + GDNativeObjectPtr obj = godot::internal::interface->classdb_construct_object(___constructor, ___extension); \ return reinterpret_cast(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \ } \ \ @@ -205,7 +208,7 @@ public: ___binding_reference_callback, \ }; \ static m_class *_new() { \ - static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class); \ + static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class, nullptr); \ CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ GDNativeObjectPtr obj = ___constructor(); \ return reinterpret_cast(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \