Merge pull request #88683 from dsnopek/runtime-classes-in-modules
Allow registering "runtime classes" in modules (not just GDExtension)
This commit is contained in:
commit
45dd2bd31e
|
@ -178,6 +178,7 @@ public:
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -133,6 +133,7 @@ public:
|
|||
bool exposed = false;
|
||||
bool reloadable = false;
|
||||
bool is_virtual = false;
|
||||
bool is_runtime = false;
|
||||
Object *(*creation_func)() = nullptr;
|
||||
|
||||
ClassInfo() {}
|
||||
|
@ -234,6 +235,23 @@ public:
|
|||
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);
|
||||
|
||||
|
@ -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"
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
@ -261,6 +264,9 @@ public:
|
|||
#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);
|
||||
}
|
||||
|
||||
|
@ -329,6 +335,9 @@ public:
|
|||
|
||||
#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
|
||||
|
@ -338,6 +347,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -346,6 +358,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -404,6 +419,9 @@ public:
|
|||
|
||||
#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
|
||||
|
@ -413,6 +431,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -421,6 +442,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -490,6 +514,9 @@ public:
|
|||
|
||||
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
|
||||
|
@ -499,6 +526,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -507,6 +537,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -577,6 +610,9 @@ public:
|
|||
|
||||
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
|
||||
|
@ -586,6 +622,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -594,6 +633,9 @@ public:
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue