Skip to content

Commit 45dd2bd

Browse files
committed
Merge pull request #88683 from dsnopek/runtime-classes-in-modules
Allow registering "runtime classes" in modules (not just GDExtension)
2 parents b82855b + 4d0b989 commit 45dd2bd

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

core/object/class_db.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class PlaceholderExtensionInstance {
178178
while (native_parent->gdextension) {
179179
native_parent = native_parent->inherits_ptr;
180180
}
181+
ERR_FAIL_NULL_V(native_parent->creation_func, nullptr);
181182

182183
// Construct a placeholder.
183184
Object *obj = native_parent->creation_func();
@@ -501,7 +502,7 @@ Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require
501502
if (ti->gdextension && ti->gdextension->create_instance) {
502503
ObjectGDExtension *extension = ti->gdextension;
503504
#ifdef TOOLS_ENABLED
504-
if (!p_require_real_class && ti->gdextension->is_runtime && Engine::get_singleton()->is_editor_hint()) {
505+
if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
505506
extension = get_placeholder_extension(ti->name);
506507
}
507508
#endif
@@ -514,6 +515,17 @@ Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require
514515
#endif
515516
return obj;
516517
} else {
518+
#ifdef TOOLS_ENABLED
519+
if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
520+
if (!ti->inherits_ptr || !ti->inherits_ptr->creation_func) {
521+
ERR_PRINT_ONCE(vformat("Cannot make a placeholder instance of runtime class %s because its parent cannot be constructed.", ti->name));
522+
} else {
523+
ObjectGDExtension *extension = get_placeholder_extension(ti->name);
524+
return (Object *)extension->create_instance(extension->class_userdata);
525+
}
526+
}
527+
#endif
528+
517529
return ti->creation_func();
518530
}
519531
}
@@ -544,26 +556,42 @@ ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class)
544556
}
545557
ERR_FAIL_NULL_V_MSG(ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
546558
ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
547-
ERR_FAIL_NULL_V_MSG(ti->gdextension, nullptr, "Class '" + String(p_class) + "' has no native extension.");
548559
}
549560

561+
// Make a "fake" extension to act as a placeholder.
550562
placeholder_extensions[p_class] = ObjectGDExtension();
551563
placeholder_extension = placeholder_extensions.getptr(p_class);
552564

553-
// Make a "fake" extension to act as a placeholder.
554-
placeholder_extension->library = ti->gdextension->library;
555-
placeholder_extension->parent = ti->gdextension->parent;
556-
placeholder_extension->children = ti->gdextension->children;
557-
placeholder_extension->parent_class_name = ti->gdextension->parent_class_name;
558-
placeholder_extension->class_name = ti->gdextension->class_name;
559-
placeholder_extension->editor_class = ti->gdextension->editor_class;
560-
placeholder_extension->reloadable = ti->gdextension->reloadable;
561-
placeholder_extension->is_virtual = ti->gdextension->is_virtual;
562-
placeholder_extension->is_abstract = ti->gdextension->is_abstract;
563-
placeholder_extension->is_exposed = ti->gdextension->is_exposed;
564565
placeholder_extension->is_runtime = true;
565566
placeholder_extension->is_placeholder = true;
566567

568+
if (ti->gdextension) {
569+
placeholder_extension->library = ti->gdextension->library;
570+
placeholder_extension->parent = ti->gdextension->parent;
571+
placeholder_extension->children = ti->gdextension->children;
572+
placeholder_extension->parent_class_name = ti->gdextension->parent_class_name;
573+
placeholder_extension->class_name = ti->gdextension->class_name;
574+
placeholder_extension->editor_class = ti->gdextension->editor_class;
575+
placeholder_extension->reloadable = ti->gdextension->reloadable;
576+
placeholder_extension->is_virtual = ti->gdextension->is_virtual;
577+
placeholder_extension->is_abstract = ti->gdextension->is_abstract;
578+
placeholder_extension->is_exposed = ti->gdextension->is_exposed;
579+
580+
placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata;
581+
placeholder_extension->track_instance = ti->gdextension->track_instance;
582+
placeholder_extension->untrack_instance = ti->gdextension->untrack_instance;
583+
} else {
584+
placeholder_extension->library = nullptr;
585+
placeholder_extension->parent = nullptr;
586+
placeholder_extension->parent_class_name = ti->inherits;
587+
placeholder_extension->class_name = ti->name;
588+
placeholder_extension->editor_class = ti->api == API_EDITOR;
589+
placeholder_extension->reloadable = false;
590+
placeholder_extension->is_virtual = ti->is_virtual;
591+
placeholder_extension->is_abstract = false;
592+
placeholder_extension->is_exposed = ti->exposed;
593+
}
594+
567595
placeholder_extension->set = &PlaceholderExtensionInstance::placeholder_instance_set;
568596
placeholder_extension->get = &PlaceholderExtensionInstance::placeholder_instance_get;
569597
placeholder_extension->get_property_list = &PlaceholderExtensionInstance::placeholder_instance_get_property_list;
@@ -586,10 +614,6 @@ ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class)
586614
placeholder_extension->call_virtual_with_data = nullptr;
587615
placeholder_extension->recreate_instance = &PlaceholderExtensionInstance::placeholder_class_recreate_instance;
588616

589-
placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata;
590-
placeholder_extension->track_instance = ti->gdextension->track_instance;
591-
placeholder_extension->untrack_instance = ti->gdextension->untrack_instance;
592-
593617
return placeholder_extension;
594618
}
595619
#endif
@@ -2028,6 +2052,9 @@ void ClassDB::register_extension_class(ObjectGDExtension *p_extension) {
20282052
}
20292053
}
20302054
c.reloadable = p_extension->reloadable;
2055+
#ifdef TOOLS_ENABLED
2056+
c.is_runtime = p_extension->is_runtime;
2057+
#endif
20312058

20322059
classes[p_extension->class_name] = c;
20332060
}

core/object/class_db.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class ClassDB {
133133
bool exposed = false;
134134
bool reloadable = false;
135135
bool is_virtual = false;
136+
bool is_runtime = false;
136137
Object *(*creation_func)() = nullptr;
137138

138139
ClassInfo() {}
@@ -234,6 +235,23 @@ class ClassDB {
234235
T::register_custom_data_to_otdb();
235236
}
236237

238+
template <class T>
239+
static void register_runtime_class() {
240+
GLOBAL_LOCK_FUNCTION;
241+
static_assert(types_are_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
242+
T::initialize_class();
243+
ClassInfo *t = classes.getptr(T::get_class_static());
244+
ERR_FAIL_NULL(t);
245+
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()));
246+
t->creation_func = &creator<T>;
247+
t->exposed = true;
248+
t->is_virtual = false;
249+
t->is_runtime = true;
250+
t->class_ptr = T::get_class_ptr_static();
251+
t->api = current_api;
252+
T::register_custom_data_to_otdb();
253+
}
254+
237255
static void register_extension_class(ObjectGDExtension *p_extension);
238256
static void unregister_extension_class(const StringName &p_class, bool p_free_method_binds = true);
239257

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

539+
#define GDREGISTER_RUNTIME_CLASS(m_class) \
540+
if (m_class::_class_is_enabled) { \
541+
::ClassDB::register_runtime_class<m_class>(); \
542+
}
543+
521544
#define GDREGISTER_NATIVE_STRUCT(m_class, m_code) ClassDB::register_native_struct(#m_class, m_code, sizeof(m_class))
522545

523546
#include "core/disabled_classes.gen.h"

core/object/method_bind.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class MethodBindVarArgT : public MethodBindVarArgBase<MethodBindVarArgT<T>, T, v
225225

226226
public:
227227
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
228+
#ifdef TOOLS_ENABLED
229+
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()));
230+
#endif
228231
(static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>::method)(p_args, p_arg_count, r_error);
229232
return {};
230233
}
@@ -261,6 +264,9 @@ class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>,
261264
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
262265
#endif
263266
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
267+
#ifdef TOOLS_ENABLED
268+
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()));
269+
#endif
264270
return (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error);
265271
}
266272

@@ -329,6 +335,9 @@ class MethodBindT : public MethodBind {
329335

330336
#endif
331337
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
338+
#ifdef TOOLS_ENABLED
339+
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()));
340+
#endif
332341
#ifdef TYPED_METHOD_BIND
333342
call_with_variant_args_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
334343
#else
@@ -338,6 +347,9 @@ class MethodBindT : public MethodBind {
338347
}
339348

340349
virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
350+
#ifdef TOOLS_ENABLED
351+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
352+
#endif
341353
#ifdef TYPED_METHOD_BIND
342354
call_with_validated_object_instance_args(static_cast<T *>(p_object), method, p_args);
343355
#else
@@ -346,6 +358,9 @@ class MethodBindT : public MethodBind {
346358
}
347359

348360
virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
361+
#ifdef TOOLS_ENABLED
362+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
363+
#endif
349364
#ifdef TYPED_METHOD_BIND
350365
call_with_ptr_args<T, P...>(static_cast<T *>(p_object), method, p_args);
351366
#else
@@ -404,6 +419,9 @@ class MethodBindTC : public MethodBind {
404419

405420
#endif
406421
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
422+
#ifdef TOOLS_ENABLED
423+
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()));
424+
#endif
407425
#ifdef TYPED_METHOD_BIND
408426
call_with_variant_argsc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
409427
#else
@@ -413,6 +431,9 @@ class MethodBindTC : public MethodBind {
413431
}
414432

415433
virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
434+
#ifdef TOOLS_ENABLED
435+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
436+
#endif
416437
#ifdef TYPED_METHOD_BIND
417438
call_with_validated_object_instance_argsc(static_cast<T *>(p_object), method, p_args);
418439
#else
@@ -421,6 +442,9 @@ class MethodBindTC : public MethodBind {
421442
}
422443

423444
virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
445+
#ifdef TOOLS_ENABLED
446+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
447+
#endif
424448
#ifdef TYPED_METHOD_BIND
425449
call_with_ptr_argsc<T, P...>(static_cast<T *>(p_object), method, p_args);
426450
#else
@@ -490,6 +514,9 @@ class MethodBindTR : public MethodBind {
490514

491515
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
492516
Variant ret;
517+
#ifdef TOOLS_ENABLED
518+
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()));
519+
#endif
493520
#ifdef TYPED_METHOD_BIND
494521
call_with_variant_args_ret_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
495522
#else
@@ -499,6 +526,9 @@ class MethodBindTR : public MethodBind {
499526
}
500527

501528
virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
529+
#ifdef TOOLS_ENABLED
530+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
531+
#endif
502532
#ifdef TYPED_METHOD_BIND
503533
call_with_validated_object_instance_args_ret(static_cast<T *>(p_object), method, p_args, r_ret);
504534
#else
@@ -507,6 +537,9 @@ class MethodBindTR : public MethodBind {
507537
}
508538

509539
virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
540+
#ifdef TOOLS_ENABLED
541+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
542+
#endif
510543
#ifdef TYPED_METHOD_BIND
511544
call_with_ptr_args_ret<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
512545
#else
@@ -577,6 +610,9 @@ class MethodBindTRC : public MethodBind {
577610

578611
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
579612
Variant ret;
613+
#ifdef TOOLS_ENABLED
614+
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()));
615+
#endif
580616
#ifdef TYPED_METHOD_BIND
581617
call_with_variant_args_retc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
582618
#else
@@ -586,6 +622,9 @@ class MethodBindTRC : public MethodBind {
586622
}
587623

588624
virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
625+
#ifdef TOOLS_ENABLED
626+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
627+
#endif
589628
#ifdef TYPED_METHOD_BIND
590629
call_with_validated_object_instance_args_retc(static_cast<T *>(p_object), method, p_args, r_ret);
591630
#else
@@ -594,6 +633,9 @@ class MethodBindTRC : public MethodBind {
594633
}
595634

596635
virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
636+
#ifdef TOOLS_ENABLED
637+
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
638+
#endif
597639
#ifdef TYPED_METHOD_BIND
598640
call_with_ptr_args_retc<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
599641
#else

core/object/object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ struct ObjectGDExtension {
358358

359359
#ifdef TOOLS_ENABLED
360360
void *tracking_userdata = nullptr;
361-
void (*track_instance)(void *p_userdata, void *p_instance);
362-
void (*untrack_instance)(void *p_userdata, void *p_instance);
361+
void (*track_instance)(void *p_userdata, void *p_instance) = nullptr;
362+
void (*untrack_instance)(void *p_userdata, void *p_instance) = nullptr;
363363
#endif
364364
};
365365

0 commit comments

Comments
 (0)