summaryrefslogtreecommitdiffstats
path: root/core/object
diff options
context:
space:
mode:
Diffstat (limited to 'core/object')
-rw-r--r--core/object/class_db.cpp257
-rw-r--r--core/object/class_db.h31
-rw-r--r--core/object/method_bind.h42
-rw-r--r--core/object/object.compat.inc40
-rw-r--r--core/object/object.cpp52
-rw-r--r--core/object/object.h14
6 files changed, 414 insertions, 22 deletions
diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp
index f2a9a68d08..63adc5b502 100644
--- a/core/object/class_db.cpp
+++ b/core/object/class_db.cpp
@@ -70,6 +70,139 @@ HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;
+#ifdef TOOLS_ENABLED
+HashMap<StringName, ObjectGDExtension> ClassDB::placeholder_extensions;
+
+class PlaceholderExtensionInstance {
+ StringName class_name;
+ HashMap<StringName, Variant> properties;
+
+public:
+ PlaceholderExtensionInstance(const StringName &p_class_name) {
+ class_name = p_class_name;
+ }
+
+ ~PlaceholderExtensionInstance() {}
+
+ void set(const StringName &p_name, const Variant &p_value) {
+ bool is_default_valid = false;
+ Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name, &is_default_valid);
+
+ // If there's a default value, then we know it's a valid property.
+ if (is_default_valid) {
+ properties[p_name] = p_value;
+ }
+ }
+
+ Variant get(const StringName &p_name) {
+ const Variant *value = properties.getptr(p_name);
+ Variant ret;
+
+ if (value) {
+ ret = *value;
+ } else {
+ bool is_default_valid = false;
+ Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name, &is_default_valid);
+ if (is_default_valid) {
+ ret = default_value;
+ }
+ }
+
+ return ret;
+ }
+
+ static GDExtensionBool placeholder_instance_set(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) {
+ PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
+ const StringName &name = *(StringName *)p_name;
+ const Variant &value = *(const Variant *)p_value;
+
+ self->set(name, value);
+
+ // We have to return true so Godot doesn't try to call the real setter function.
+ return true;
+ }
+
+ static GDExtensionBool placeholder_instance_get(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
+ PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
+ const StringName &name = *(StringName *)p_name;
+ Variant *value = (Variant *)r_ret;
+
+ *value = self->get(name);
+
+ // We have to return true so Godot doesn't try to call the real getter function.
+ return true;
+ }
+
+ static const GDExtensionPropertyInfo *placeholder_instance_get_property_list(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) {
+ *r_count = 0;
+ return nullptr;
+ }
+
+ static void placeholder_instance_free_property_list(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list) {
+ }
+
+ static GDExtensionBool placeholder_instance_property_can_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) {
+ return false;
+ }
+
+ static GDExtensionBool placeholder_instance_property_get_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
+ return false;
+ }
+
+ static GDExtensionBool placeholder_instance_validate_property(GDExtensionClassInstancePtr p_instance, GDExtensionPropertyInfo *p_property) {
+ return false;
+ }
+
+ static void placeholder_instance_notification(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {
+ }
+
+ static void placeholder_instance_to_string(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr p_out) {
+ *r_is_valid = true;
+ }
+
+ static void placeholder_instance_reference(GDExtensionClassInstancePtr p_instance) {
+ }
+
+ static void placeholder_instance_unreference(GDExtensionClassInstancePtr p_instance) {
+ }
+
+ static uint64_t placeholder_instance_get_rid(GDExtensionClassInstancePtr p_instance) {
+ return 0;
+ }
+
+ static GDExtensionObjectPtr placeholder_class_create_instance(void *p_class_userdata) {
+ ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
+
+ // Find the closest native parent.
+ ClassDB::ClassInfo *native_parent = ti->inherits_ptr;
+ 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();
+ obj->_extension = ClassDB::get_placeholder_extension(ti->name);
+ obj->_extension_instance = memnew(PlaceholderExtensionInstance(ti->name));
+ return obj;
+ }
+
+ static GDExtensionObjectPtr placeholder_class_recreate_instance(void *p_class_userdata, GDExtensionObjectPtr p_object) {
+ ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
+ return memnew(PlaceholderExtensionInstance(ti->name));
+ }
+
+ static void placeholder_class_free_instance(void *p_class_userdata, GDExtensionClassInstancePtr p_instance) {
+ PlaceholderExtensionInstance *instance = (PlaceholderExtensionInstance *)p_instance;
+ memdelete(instance);
+ }
+
+ static GDExtensionClassCallVirtual placeholder_class_get_virtual(void *p_class_userdata, GDExtensionConstStringNamePtr p_name) {
+ return nullptr;
+ }
+};
+#endif
+
bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
if (!classes.has(p_class)) {
return false;
@@ -346,7 +479,7 @@ StringName ClassDB::get_compatibility_class(const StringName &p_class) {
return StringName();
}
-Object *ClassDB::instantiate(const StringName &p_class) {
+Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require_real_class) {
ClassInfo *ti;
{
OBJTYPE_RLOCK;
@@ -367,18 +500,124 @@ Object *ClassDB::instantiate(const StringName &p_class) {
}
#endif
if (ti->gdextension && ti->gdextension->create_instance) {
- Object *obj = (Object *)ti->gdextension->create_instance(ti->gdextension->class_userdata);
+ ObjectGDExtension *extension = ti->gdextension;
#ifdef TOOLS_ENABLED
- if (ti->gdextension->track_instance) {
- ti->gdextension->track_instance(ti->gdextension->tracking_userdata, obj);
+ if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
+ extension = get_placeholder_extension(ti->name);
+ }
+#endif
+ Object *obj = (Object *)extension->create_instance(extension->class_userdata);
+
+#ifdef TOOLS_ENABLED
+ if (extension->track_instance) {
+ extension->track_instance(extension->tracking_userdata, obj);
}
#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();
}
}
+Object *ClassDB::instantiate(const StringName &p_class) {
+ return _instantiate_internal(p_class);
+}
+
+Object *ClassDB::instantiate_no_placeholders(const StringName &p_class) {
+ return _instantiate_internal(p_class, true);
+}
+
+#ifdef TOOLS_ENABLED
+ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class) {
+ ObjectGDExtension *placeholder_extension = placeholder_extensions.getptr(p_class);
+ if (placeholder_extension) {
+ return placeholder_extension;
+ }
+
+ ClassInfo *ti;
+ {
+ OBJTYPE_RLOCK;
+ ti = classes.getptr(p_class);
+ if (!ti || ti->disabled || !ti->creation_func || (ti->gdextension && !ti->gdextension->create_instance)) {
+ if (compat_classes.has(p_class)) {
+ ti = classes.getptr(compat_classes[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.");
+ }
+
+ // Make a "fake" extension to act as a placeholder.
+ placeholder_extensions[p_class] = ObjectGDExtension();
+ placeholder_extension = placeholder_extensions.getptr(p_class);
+
+ 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;
+ placeholder_extension->free_property_list = &PlaceholderExtensionInstance::placeholder_instance_free_property_list;
+ placeholder_extension->property_can_revert = &PlaceholderExtensionInstance::placeholder_instance_property_can_revert;
+ placeholder_extension->property_get_revert = &PlaceholderExtensionInstance::placeholder_instance_property_get_revert;
+ placeholder_extension->validate_property = &PlaceholderExtensionInstance::placeholder_instance_validate_property;
+ placeholder_extension->notification = nullptr;
+ placeholder_extension->notification2 = &PlaceholderExtensionInstance::placeholder_instance_notification;
+ placeholder_extension->to_string = &PlaceholderExtensionInstance::placeholder_instance_to_string;
+ placeholder_extension->reference = &PlaceholderExtensionInstance::placeholder_instance_reference;
+ placeholder_extension->unreference = &PlaceholderExtensionInstance::placeholder_instance_unreference;
+ placeholder_extension->get_rid = &PlaceholderExtensionInstance::placeholder_instance_get_rid;
+
+ placeholder_extension->class_userdata = ti;
+ placeholder_extension->create_instance = &PlaceholderExtensionInstance::placeholder_class_create_instance;
+ placeholder_extension->free_instance = &PlaceholderExtensionInstance::placeholder_class_free_instance;
+ placeholder_extension->get_virtual = &PlaceholderExtensionInstance::placeholder_class_get_virtual;
+ placeholder_extension->get_virtual_call_data = nullptr;
+ placeholder_extension->call_virtual_with_data = nullptr;
+ placeholder_extension->recreate_instance = &PlaceholderExtensionInstance::placeholder_class_recreate_instance;
+
+ return placeholder_extension;
+}
+#endif
+
void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) {
ERR_FAIL_NULL(p_object);
ClassInfo *ti;
@@ -1716,7 +1955,7 @@ Variant ClassDB::class_get_default_property_value(const StringName &p_class, con
c = Engine::get_singleton()->get_singleton_object(p_class);
cleanup_c = false;
} else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) { // Keep this condition in sync with doc_tools.cpp get_documentation_default_value.
- c = ClassDB::instantiate(p_class);
+ c = ClassDB::instantiate_no_placeholders(p_class);
cleanup_c = true;
}
@@ -1813,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;
}
@@ -1826,6 +2068,11 @@ void ClassDB::unregister_extension_class(const StringName &p_class, bool p_free_
}
}
classes.erase(p_class);
+ default_values_cached.erase(p_class);
+ default_values.erase(p_class);
+#ifdef TOOLS_ENABLED
+ placeholder_extensions.erase(p_class);
+#endif
}
HashMap<StringName, ClassDB::NativeStruct> ClassDB::native_structs;
diff --git a/core/object/class_db.h b/core/object/class_db.h
index c910b30d11..7f117b4a9b 100644
--- a/core/object/class_db.h
+++ b/core/object/class_db.h
@@ -133,6 +133,7 @@ public:
bool exposed = false;
bool reloadable = false;
bool is_virtual = false;
+ bool is_runtime = false;
Object *(*creation_func)() = nullptr;
ClassInfo() {}
@@ -149,6 +150,10 @@ public:
static HashMap<StringName, StringName> resource_base_extensions;
static HashMap<StringName, StringName> compat_classes;
+#ifdef TOOLS_ENABLED
+ static HashMap<StringName, ObjectGDExtension> placeholder_extensions;
+#endif
+
#ifdef DEBUG_METHODS_ENABLED
static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, bool p_compatibility, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount);
#else
@@ -178,6 +183,8 @@ private:
static MethodBind *_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector<Variant> &p_default_args, bool p_compatibility);
static void _bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility);
+ static Object *_instantiate_internal(const StringName &p_class, bool p_require_real_class = false);
+
public:
// DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
template <class T>
@@ -228,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);
@@ -253,6 +277,7 @@ public:
static void get_class_list(List<StringName> *p_classes);
#ifdef TOOLS_ENABLED
static void get_extensions_class_list(List<StringName> *p_classes);
+ static ObjectGDExtension *get_placeholder_extension(const StringName &p_class);
#endif
static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
static void get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
@@ -264,6 +289,7 @@ public:
static bool can_instantiate(const StringName &p_class);
static bool is_virtual(const StringName &p_class);
static Object *instantiate(const StringName &p_class);
+ static Object *instantiate_no_placeholders(const StringName &p_class);
static void set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance);
static APIType get_api_type(const StringName &p_class);
@@ -510,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"
diff --git a/core/object/method_bind.h b/core/object/method_bind.h
index d67fd003c8..a1723adb9a 100644
--- a/core/object/method_bind.h
+++ b/core/object/method_bind.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
diff --git a/core/object/object.compat.inc b/core/object/object.compat.inc
new file mode 100644
index 0000000000..bf1e99fc9b
--- /dev/null
+++ b/core/object/object.compat.inc
@@ -0,0 +1,40 @@
+/**************************************************************************/
+/* object.compat.inc */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef DISABLE_DEPRECATED
+
+#include "core/object/class_db.h"
+
+void Object::_bind_compatibility_methods() {
+ ClassDB::bind_compatibility_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(""));
+ ClassDB::bind_compatibility_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(""));
+}
+
+#endif
diff --git a/core/object/object.cpp b/core/object/object.cpp
index cc33d0ab8a..5db1d2534f 100644
--- a/core/object/object.cpp
+++ b/core/object/object.cpp
@@ -29,6 +29,7 @@
/**************************************************************************/
#include "object.h"
+#include "object.compat.inc"
#include "core/core_string_names.h"
#include "core/extension/gdextension_manager.h"
@@ -492,14 +493,22 @@ void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) cons
ClassDB::get_property_list(current_extension->class_name, p_list, true, this);
if (current_extension->get_property_list) {
- uint32_t pcount;
- const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
- for (uint32_t i = 0; i < pcount; i++) {
- p_list->push_back(PropertyInfo(pinfo[i]));
- }
- if (current_extension->free_property_list) {
- current_extension->free_property_list(_extension_instance, pinfo);
+#ifdef TOOLS_ENABLED
+ // If this is a placeholder, we can't call into the GDExtension on the parent class,
+ // because we don't have a real instance of the class to give it.
+ if (likely(!_extension->is_placeholder)) {
+#endif
+ uint32_t pcount;
+ const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
+ for (uint32_t i = 0; i < pcount; i++) {
+ p_list->push_back(PropertyInfo(pinfo[i]));
+ }
+ if (current_extension->free_property_list) {
+ current_extension->free_property_list(_extension_instance, pinfo);
+ }
+#ifdef TOOLS_ENABLED
}
+#endif
}
current_extension = current_extension->parent;
@@ -1464,6 +1473,7 @@ void Object::initialize_class() {
}
ClassDB::_add_class<Object>();
_bind_methods();
+ _bind_compatibility_methods();
initialized = true;
}
@@ -1639,8 +1649,8 @@ void Object::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
- ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(""));
- ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(""));
+ ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
+ ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);
@@ -1792,6 +1802,16 @@ uint32_t Object::get_edited_version() const {
#endif
StringName Object::get_class_name_for_extension(const GDExtension *p_library) const {
+#ifdef TOOLS_ENABLED
+ // If this is the library this extension comes from and it's a placeholder, we
+ // have to return the closest native parent's class name, so that it doesn't try to
+ // use this like the real object.
+ if (unlikely(_extension && _extension->library == p_library && _extension->is_placeholder)) {
+ const StringName *class_name = _get_class_namev();
+ return *class_name;
+ }
+#endif
+
// Only return the class name per the extension if it matches the given p_library.
if (_extension && _extension->library == p_library) {
return _extension->class_name;
@@ -1919,13 +1939,15 @@ void Object::clear_internal_extension() {
// Clear the instance bindings.
_instance_binding_mutex.lock();
- if (_instance_bindings[0].free_callback) {
- _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
+ if (_instance_bindings) {
+ if (_instance_bindings[0].free_callback) {
+ _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
+ }
+ _instance_bindings[0].binding = nullptr;
+ _instance_bindings[0].token = nullptr;
+ _instance_bindings[0].free_callback = nullptr;
+ _instance_bindings[0].reference_callback = nullptr;
}
- _instance_bindings[0].binding = nullptr;
- _instance_bindings[0].token = nullptr;
- _instance_bindings[0].free_callback = nullptr;
- _instance_bindings[0].reference_callback = nullptr;
_instance_binding_mutex.unlock();
// Clear the virtual methods.
diff --git a/core/object/object.h b/core/object/object.h
index 27f28b4aae..a062b8aa62 100644
--- a/core/object/object.h
+++ b/core/object/object.h
@@ -317,6 +317,10 @@ struct ObjectGDExtension {
bool is_virtual = false;
bool is_abstract = false;
bool is_exposed = true;
+#ifdef TOOLS_ENABLED
+ bool is_runtime = false;
+ bool is_placeholder = false;
+#endif
GDExtensionClassSet set;
GDExtensionClassGet get;
GDExtensionClassGetPropertyList get_property_list;
@@ -354,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
};
@@ -698,7 +702,11 @@ protected:
virtual void _notificationv(int p_notification, bool p_reversed) {}
static void _bind_methods();
+#ifndef DISABLE_DEPRECATED
+ static void _bind_compatibility_methods();
+#else
static void _bind_compatibility_methods() {}
+#endif
bool _set(const StringName &p_name, const Variant &p_property) { return false; };
bool _get(const StringName &p_name, Variant &r_property) const { return false; };
void _get_property_list(List<PropertyInfo> *p_list) const {};
@@ -755,6 +763,7 @@ protected:
void _clear_internal_resource_paths(const Variant &p_var);
friend class ClassDB;
+ friend class PlaceholderExtensionInstance;
bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
@@ -977,6 +986,7 @@ public:
#ifdef TOOLS_ENABLED
void clear_internal_extension();
void reset_internal_extension(ObjectGDExtension *p_extension);
+ bool is_extension_placeholder() const { return _extension && _extension->is_placeholder; }
#endif
void clear_internal_resource_paths();