Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registering "runtime classes" in modules (not just GDExtension) #88683

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions core/object/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down Expand Up @@ -234,6 +235,23 @@ class ClassDB {
T::register_custom_data_to_otdb();
}

template <class T>
static void register_runtime_class() {
GLOBAL_LOCK_FUNCTION;
static_assert(types_are_same_v<typename T::self_type, T>, "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>;
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);

Expand Down Expand Up @@ -518,6 +536,11 @@ _FORCE_INLINE_ Vector<Error> errarray(P... p_args) {
::ClassDB::register_internal_class<m_class>(); \
}

#define GDREGISTER_RUNTIME_CLASS(m_class) \
if (m_class::_class_is_enabled) { \
::ClassDB::register_runtime_class<m_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"
Expand Down
42 changes: 42 additions & 0 deletions core/object/method_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class MethodBindVarArgT : public MethodBindVarArgBase<MethodBindVarArgT<T>, 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<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>::method)(p_args, p_arg_count, r_error);
return {};
}
Expand Down Expand Up @@ -261,6 +264,9 @@ class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>,
#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<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error);
}

Expand Down Expand Up @@ -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<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
#else
Expand All @@ -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<T *>(p_object), method, p_args);
#else
Expand All @@ -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<T, P...>(static_cast<T *>(p_object), method, p_args);
#else
Expand Down Expand Up @@ -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<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
#else
Expand All @@ -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<T *>(p_object), method, p_args);
#else
Expand All @@ -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<T, P...>(static_cast<T *>(p_object), method, p_args);
#else
Expand Down Expand Up @@ -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<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
#else
Expand All @@ -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<T *>(p_object), method, p_args, r_ret);
#else
Expand All @@ -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<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
#else
Expand Down Expand Up @@ -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<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
#else
Expand All @@ -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<T *>(p_object), method, p_args, r_ret);
#else
Expand All @@ -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<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
#else
Expand Down
4 changes: 2 additions & 2 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
Loading