From 4d0b989bb85b0cae95c4535611f755afbbd0bb42 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Wed, 21 Feb 2024 17:56:50 -0600 Subject: [PATCH] Allow registering "runtime classes" in modules (not just GDExtension) --- core/object/class_db.cpp | 61 ++++++++++++++++++++++++++++----------- core/object/class_db.h | 23 +++++++++++++++ core/object/method_bind.h | 42 +++++++++++++++++++++++++++ core/object/object.h | 4 +-- 4 files changed, 111 insertions(+), 19 deletions(-) diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 7671b7972ec9..63adc5b50212 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -178,6 +178,7 @@ class PlaceholderExtensionInstance { while (native_parent->gdextension) { native_parent = native_parent->inherits_ptr; } + ERR_FAIL_NULL_V(native_parent->creation_func, nullptr); // Construct a placeholder. Object *obj = native_parent->creation_func(); @@ -501,7 +502,7 @@ Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require if (ti->gdextension && ti->gdextension->create_instance) { ObjectGDExtension *extension = ti->gdextension; #ifdef TOOLS_ENABLED - if (!p_require_real_class && ti->gdextension->is_runtime && Engine::get_singleton()->is_editor_hint()) { + if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) { extension = get_placeholder_extension(ti->name); } #endif @@ -514,6 +515,17 @@ Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require #endif return obj; } else { +#ifdef TOOLS_ENABLED + if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) { + if (!ti->inherits_ptr || !ti->inherits_ptr->creation_func) { + ERR_PRINT_ONCE(vformat("Cannot make a placeholder instance of runtime class %s because its parent cannot be constructed.", ti->name)); + } else { + ObjectGDExtension *extension = get_placeholder_extension(ti->name); + return (Object *)extension->create_instance(extension->class_userdata); + } + } +#endif + return ti->creation_func(); } } @@ -544,26 +556,42 @@ ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class) } ERR_FAIL_NULL_V_MSG(ti, nullptr, "Cannot get class '" + String(p_class) + "'."); ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled."); - ERR_FAIL_NULL_V_MSG(ti->gdextension, nullptr, "Class '" + String(p_class) + "' has no native extension."); } + // Make a "fake" extension to act as a placeholder. placeholder_extensions[p_class] = ObjectGDExtension(); placeholder_extension = placeholder_extensions.getptr(p_class); - // Make a "fake" extension to act as a placeholder. - placeholder_extension->library = ti->gdextension->library; - placeholder_extension->parent = ti->gdextension->parent; - placeholder_extension->children = ti->gdextension->children; - placeholder_extension->parent_class_name = ti->gdextension->parent_class_name; - placeholder_extension->class_name = ti->gdextension->class_name; - placeholder_extension->editor_class = ti->gdextension->editor_class; - placeholder_extension->reloadable = ti->gdextension->reloadable; - placeholder_extension->is_virtual = ti->gdextension->is_virtual; - placeholder_extension->is_abstract = ti->gdextension->is_abstract; - placeholder_extension->is_exposed = ti->gdextension->is_exposed; placeholder_extension->is_runtime = true; placeholder_extension->is_placeholder = true; + if (ti->gdextension) { + placeholder_extension->library = ti->gdextension->library; + placeholder_extension->parent = ti->gdextension->parent; + placeholder_extension->children = ti->gdextension->children; + placeholder_extension->parent_class_name = ti->gdextension->parent_class_name; + placeholder_extension->class_name = ti->gdextension->class_name; + placeholder_extension->editor_class = ti->gdextension->editor_class; + placeholder_extension->reloadable = ti->gdextension->reloadable; + placeholder_extension->is_virtual = ti->gdextension->is_virtual; + placeholder_extension->is_abstract = ti->gdextension->is_abstract; + placeholder_extension->is_exposed = ti->gdextension->is_exposed; + + placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata; + placeholder_extension->track_instance = ti->gdextension->track_instance; + placeholder_extension->untrack_instance = ti->gdextension->untrack_instance; + } else { + placeholder_extension->library = nullptr; + placeholder_extension->parent = nullptr; + placeholder_extension->parent_class_name = ti->inherits; + placeholder_extension->class_name = ti->name; + placeholder_extension->editor_class = ti->api == API_EDITOR; + placeholder_extension->reloadable = false; + placeholder_extension->is_virtual = ti->is_virtual; + placeholder_extension->is_abstract = false; + placeholder_extension->is_exposed = ti->exposed; + } + placeholder_extension->set = &PlaceholderExtensionInstance::placeholder_instance_set; placeholder_extension->get = &PlaceholderExtensionInstance::placeholder_instance_get; placeholder_extension->get_property_list = &PlaceholderExtensionInstance::placeholder_instance_get_property_list; @@ -586,10 +614,6 @@ ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class) placeholder_extension->call_virtual_with_data = nullptr; placeholder_extension->recreate_instance = &PlaceholderExtensionInstance::placeholder_class_recreate_instance; - placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata; - placeholder_extension->track_instance = ti->gdextension->track_instance; - placeholder_extension->untrack_instance = ti->gdextension->untrack_instance; - return placeholder_extension; } #endif @@ -2028,6 +2052,9 @@ void ClassDB::register_extension_class(ObjectGDExtension *p_extension) { } } c.reloadable = p_extension->reloadable; +#ifdef TOOLS_ENABLED + c.is_runtime = p_extension->is_runtime; +#endif classes[p_extension->class_name] = c; } diff --git a/core/object/class_db.h b/core/object/class_db.h index 5b4f2188455d..7f117b4a9bb3 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -133,6 +133,7 @@ class ClassDB { bool exposed = false; bool reloadable = false; bool is_virtual = false; + bool is_runtime = false; Object *(*creation_func)() = nullptr; ClassInfo() {} @@ -234,6 +235,23 @@ class ClassDB { T::register_custom_data_to_otdb(); } + template + static void register_runtime_class() { + GLOBAL_LOCK_FUNCTION; + static_assert(types_are_same_v, "Class not declared properly, please use GDCLASS."); + T::initialize_class(); + ClassInfo *t = classes.getptr(T::get_class_static()); + ERR_FAIL_NULL(t); + ERR_FAIL_COND_MSG(t->inherits_ptr && !t->inherits_ptr->creation_func, vformat("Cannot register runtime class '%s' that descends from an abstract parent class.", T::get_class_static())); + t->creation_func = &creator; + t->exposed = true; + t->is_virtual = false; + t->is_runtime = true; + t->class_ptr = T::get_class_ptr_static(); + t->api = current_api; + T::register_custom_data_to_otdb(); + } + static void register_extension_class(ObjectGDExtension *p_extension); static void unregister_extension_class(const StringName &p_class, bool p_free_method_binds = true); @@ -518,6 +536,11 @@ _FORCE_INLINE_ Vector errarray(P... p_args) { ::ClassDB::register_internal_class(); \ } +#define GDREGISTER_RUNTIME_CLASS(m_class) \ + if (m_class::_class_is_enabled) { \ + ::ClassDB::register_runtime_class(); \ + } + #define GDREGISTER_NATIVE_STRUCT(m_class, m_code) ClassDB::register_native_struct(#m_class, m_code, sizeof(m_class)) #include "core/disabled_classes.gen.h" diff --git a/core/object/method_bind.h b/core/object/method_bind.h index d67fd003c883..a1723adb9a11 100644 --- a/core/object/method_bind.h +++ b/core/object/method_bind.h @@ -225,6 +225,9 @@ class MethodBindVarArgT : public MethodBindVarArgBase, T, v public: virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif (static_cast(p_object)->*MethodBindVarArgBase, T, void, false>::method)(p_args, p_arg_count, r_error); return {}; } @@ -261,6 +264,9 @@ class MethodBindVarArgTR : public MethodBindVarArgBase, #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif return (static_cast(p_object)->*MethodBindVarArgBase, T, R, true>::method)(p_args, p_arg_count, r_error); } @@ -329,6 +335,9 @@ class MethodBindT : public MethodBind { #endif virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_variant_args_dv(static_cast(p_object), method, p_args, p_arg_count, r_error, get_default_arguments()); #else @@ -338,6 +347,9 @@ class MethodBindT : public MethodBind { } virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_validated_object_instance_args(static_cast(p_object), method, p_args); #else @@ -346,6 +358,9 @@ class MethodBindT : public MethodBind { } virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_ptr_args(static_cast(p_object), method, p_args); #else @@ -404,6 +419,9 @@ class MethodBindTC : public MethodBind { #endif virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_variant_argsc_dv(static_cast(p_object), method, p_args, p_arg_count, r_error, get_default_arguments()); #else @@ -413,6 +431,9 @@ class MethodBindTC : public MethodBind { } virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_validated_object_instance_argsc(static_cast(p_object), method, p_args); #else @@ -421,6 +442,9 @@ class MethodBindTC : public MethodBind { } virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_ptr_argsc(static_cast(p_object), method, p_args); #else @@ -490,6 +514,9 @@ class MethodBindTR : public MethodBind { virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { Variant ret; +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), ret, vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_variant_args_ret_dv(static_cast(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments()); #else @@ -499,6 +526,9 @@ class MethodBindTR : public MethodBind { } virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_validated_object_instance_args_ret(static_cast(p_object), method, p_args, r_ret); #else @@ -507,6 +537,9 @@ class MethodBindTR : public MethodBind { } virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_ptr_args_ret(static_cast(p_object), method, p_args, r_ret); #else @@ -577,6 +610,9 @@ class MethodBindTRC : public MethodBind { virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override { Variant ret; +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), ret, vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_variant_args_retc_dv(static_cast(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments()); #else @@ -586,6 +622,9 @@ class MethodBindTRC : public MethodBind { } virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_validated_object_instance_args_retc(static_cast(p_object), method, p_args, r_ret); #else @@ -594,6 +633,9 @@ class MethodBindTRC : public MethodBind { } virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override { +#ifdef TOOLS_ENABLED + ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name())); +#endif #ifdef TYPED_METHOD_BIND call_with_ptr_args_retc(static_cast(p_object), method, p_args, r_ret); #else diff --git a/core/object/object.h b/core/object/object.h index 3974ef7295db..621b0f8a085f 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -358,8 +358,8 @@ struct ObjectGDExtension { #ifdef TOOLS_ENABLED void *tracking_userdata = nullptr; - void (*track_instance)(void *p_userdata, void *p_instance); - void (*untrack_instance)(void *p_userdata, void *p_instance); + void (*track_instance)(void *p_userdata, void *p_instance) = nullptr; + void (*untrack_instance)(void *p_userdata, void *p_instance) = nullptr; #endif };