Merge pull request #82554 from dsnopek/gdextension-placeholders

Allow registering "runtime classes" from GDExtension
This commit is contained in:
Rémi Verschelde 2024-02-20 17:13:24 +01:00
commit b652a81da7
No known key found for this signature in database
GPG key ID: C3336907360768E1
9 changed files with 401 additions and 38 deletions

View file

@ -205,6 +205,7 @@ 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(!valid, Variant(), vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder(), Variant(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name));
#endif
Variant ret;
GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
@ -218,6 +219,7 @@ public:
virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
#ifdef TOOLS_ENABLED
ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name));
#endif
ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have validated call support. This is most likely an engine bug.");
GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
@ -249,6 +251,7 @@ public:
virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
#ifdef TOOLS_ENABLED
ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder(), vformat("Cannot call GDExtension method bind '%s' on placeholder instance.", name));
#endif
ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have ptrcall support. This is most likely an engine bug.");
GDExtensionClassInstancePtr extension_instance = p_object->_get_extension_instance();
@ -341,10 +344,11 @@ public:
#ifndef DISABLE_DEPRECATED
void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs) {
const GDExtensionClassCreationInfo2 class_info2 = {
const GDExtensionClassCreationInfo3 class_info3 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
true, // GDExtensionBool is_exposed;
false, // GDExtensionBool is_runtime;
p_extension_funcs->set_func, // GDExtensionClassSet set_func;
p_extension_funcs->get_func, // GDExtensionClassGet get_func;
p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func;
@ -369,15 +373,45 @@ void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library
const ClassCreationDeprecatedInfo legacy = {
p_extension_funcs->notification_func,
};
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info2, &legacy);
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info3, &legacy);
}
void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs) {
const GDExtensionClassCreationInfo3 class_info3 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
p_extension_funcs->is_exposed, // GDExtensionBool is_exposed;
false, // GDExtensionBool is_runtime;
p_extension_funcs->set_func, // GDExtensionClassSet set_func;
p_extension_funcs->get_func, // GDExtensionClassGet get_func;
p_extension_funcs->get_property_list_func, // GDExtensionClassGetPropertyList get_property_list_func;
p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
p_extension_funcs->property_can_revert_func, // GDExtensionClassPropertyCanRevert property_can_revert_func;
p_extension_funcs->property_get_revert_func, // GDExtensionClassPropertyGetRevert property_get_revert_func;
p_extension_funcs->validate_property_func, // GDExtensionClassValidateProperty validate_property_func;
p_extension_funcs->notification_func, // GDExtensionClassNotification2 notification_func;
p_extension_funcs->to_string_func, // GDExtensionClassToString to_string_func;
p_extension_funcs->reference_func, // GDExtensionClassReference reference_func;
p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
p_extension_funcs->recreate_instance_func, // GDExtensionClassRecreateInstance recreate_instance_func;
p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
p_extension_funcs->get_virtual_call_data_func, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
p_extension_funcs->call_virtual_with_data_func, // GDExtensionClassCallVirtualWithData call_virtual_func;
p_extension_funcs->get_rid_func, // GDExtensionClassGetRID get_rid;
p_extension_funcs->class_userdata, // void *class_userdata;
};
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info3);
}
#endif // DISABLE_DEPRECATED
void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs) {
void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs) {
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, p_extension_funcs);
}
void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs) {
void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs) {
GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
@ -402,10 +436,15 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
#ifdef TOOLS_ENABLED
Extension *extension = nullptr;
bool is_runtime = (bool)p_extension_funcs->is_runtime;
if (self->is_reloading && self->extension_classes.has(class_name)) {
extension = &self->extension_classes[class_name];
if (!parent_extension && parent_class_name != extension->gdextension.parent_class_name) {
ERR_FAIL_MSG(vformat("GDExtension class '%s' attempt to change parent type from '%s' to '%s' on hot reload. Restart Godot for this change to take effect.", class_name, extension->gdextension.parent_class_name, parent_class_name));
ERR_FAIL_MSG(vformat("GDExtension class '%s' cannot change parent type from '%s' to '%s' on hot reload. Restart Godot for this change to take effect.", class_name, extension->gdextension.parent_class_name, parent_class_name));
}
if (extension->gdextension.is_runtime != is_runtime) {
ERR_PRINT(vformat("GDExtension class '%s' cannot change to/from runtime class on hot reload. Restart Godot for this change to take effect.", class_name));
is_runtime = extension->gdextension.is_runtime;
}
extension->is_reloading = false;
} else {
@ -434,6 +473,9 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
extension->gdextension.is_virtual = p_extension_funcs->is_virtual;
extension->gdextension.is_abstract = p_extension_funcs->is_abstract;
extension->gdextension.is_exposed = p_extension_funcs->is_exposed;
#ifdef TOOLS_ENABLED
extension->gdextension.is_runtime = is_runtime;
#endif
extension->gdextension.set = p_extension_funcs->set_func;
extension->gdextension.get = p_extension_funcs->get_func;
extension->gdextension.get_property_list = p_extension_funcs->get_property_list_func;
@ -836,8 +878,9 @@ void GDExtension::initialize_gdextensions() {
#ifndef DISABLE_DEPRECATED
register_interface_function("classdb_register_extension_class", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class);
#endif // DISABLE_DEPRECATED
register_interface_function("classdb_register_extension_class2", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class2);
#endif // DISABLE_DEPRECATED
register_interface_function("classdb_register_extension_class3", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class3);
register_interface_function("classdb_register_extension_class_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_method);
register_interface_function("classdb_register_extension_class_virtual_method", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_virtual_method);
register_interface_function("classdb_register_extension_class_integer_constant", (GDExtensionInterfaceFunctionPtr)&GDExtension::_register_extension_class_integer_constant);
@ -1089,7 +1132,10 @@ void GDExtension::prepare_reload() {
state.push_back(Pair<String, Variant>(P.name, value));
}
E.value.instance_state[obj_id] = state;
E.value.instance_state[obj_id] = {
state, // List<Pair<String, Variant>> properties;
obj->is_extension_placeholder(), // bool is_placeholder;
};
}
}
}
@ -1164,25 +1210,29 @@ void GDExtension::finish_reload() {
for (KeyValue<StringName, Extension> &E : extension_classes) {
// Loop over 'instance_state' rather than 'instance' because new instances
// may have been created when re-initializing the extension.
for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) {
Object *obj = ObjectDB::get_instance(S.key);
if (!obj) {
continue;
}
obj->reset_internal_extension(&E.value.gdextension);
if (S.value.is_placeholder) {
obj->reset_internal_extension(ClassDB::get_placeholder_extension(E.value.gdextension.class_name));
} else {
obj->reset_internal_extension(&E.value.gdextension);
}
}
}
// Now that all the classes are back, restore the state.
for (KeyValue<StringName, Extension> &E : extension_classes) {
for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) {
Object *obj = ObjectDB::get_instance(S.key);
if (!obj) {
continue;
}
for (const Pair<String, Variant> &state : S.value) {
for (const Pair<String, Variant> &state : S.value.properties) {
obj->set(state.first, state.second);
}
}
@ -1190,7 +1240,7 @@ void GDExtension::finish_reload() {
// Finally, let the objects know that we are done reloading them.
for (KeyValue<StringName, Extension> &E : extension_classes) {
for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
for (const KeyValue<ObjectID, Extension::InstanceState> &S : E.value.instance_state) {
Object *obj = ObjectDB::get_instance(S.key);
if (!obj) {
continue;

View file

@ -59,7 +59,12 @@ class GDExtension : public Resource {
bool is_reloading = false;
HashMap<StringName, GDExtensionMethodBind *> methods;
HashSet<ObjectID> instances;
HashMap<ObjectID, List<Pair<String, Variant>>> instance_state;
struct InstanceState {
List<Pair<String, Variant>> properties;
bool is_placeholder = false;
};
HashMap<ObjectID, InstanceState> instance_state;
#endif
};
@ -73,9 +78,10 @@ class GDExtension : public Resource {
#ifndef DISABLE_DEPRECATED
static void _register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs);
#endif // DISABLE_DEPRECATED
static void _register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs);
static void _register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs = nullptr);
#endif // DISABLE_DEPRECATED
static void _register_extension_class3(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs);
static void _register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs = nullptr);
static void _register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info);
static void _register_extension_class_virtual_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassVirtualMethodInfo *p_method_info);
static void _register_extension_class_integer_constant(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_enum_name, GDExtensionConstStringNamePtr p_constant_name, GDExtensionInt p_constant_value, GDExtensionBool p_is_bitfield);

View file

@ -1379,7 +1379,7 @@ static GDExtensionMethodBindPtr gdextension_classdb_get_method_bind(GDExtensionC
static GDExtensionObjectPtr gdextension_classdb_construct_object(GDExtensionConstStringNamePtr p_classname) {
const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
return (GDExtensionObjectPtr)ClassDB::instantiate(classname);
return (GDExtensionObjectPtr)ClassDB::instantiate_no_placeholders(classname);
}
static void *gdextension_classdb_get_class_tag(GDExtensionConstStringNamePtr p_classname) {

View file

@ -290,7 +290,7 @@ typedef struct {
GDExtensionClassGetVirtual get_virtual_func; // Queries a virtual function by name and returns a callback to invoke the requested virtual function.
GDExtensionClassGetRID get_rid_func;
void *class_userdata; // Per-class user data, later accessible in instance bindings.
} GDExtensionClassCreationInfo; // Deprecated. Use GDExtensionClassCreationInfo2 instead.
} GDExtensionClassCreationInfo; // Deprecated. Use GDExtensionClassCreationInfo3 instead.
typedef struct {
GDExtensionBool is_virtual;
@ -323,7 +323,41 @@ typedef struct {
GDExtensionClassCallVirtualWithData call_virtual_with_data_func;
GDExtensionClassGetRID get_rid_func;
void *class_userdata; // Per-class user data, later accessible in instance bindings.
} GDExtensionClassCreationInfo2;
} GDExtensionClassCreationInfo2; // Deprecated. Use GDExtensionClassCreationInfo3 instead.
typedef struct {
GDExtensionBool is_virtual;
GDExtensionBool is_abstract;
GDExtensionBool is_exposed;
GDExtensionBool is_runtime;
GDExtensionClassSet set_func;
GDExtensionClassGet get_func;
GDExtensionClassGetPropertyList get_property_list_func;
GDExtensionClassFreePropertyList free_property_list_func;
GDExtensionClassPropertyCanRevert property_can_revert_func;
GDExtensionClassPropertyGetRevert property_get_revert_func;
GDExtensionClassValidateProperty validate_property_func;
GDExtensionClassNotification2 notification_func;
GDExtensionClassToString to_string_func;
GDExtensionClassReference reference_func;
GDExtensionClassUnreference unreference_func;
GDExtensionClassCreateInstance create_instance_func; // (Default) constructor; mandatory. If the class is not instantiable, consider making it virtual or abstract.
GDExtensionClassFreeInstance free_instance_func; // Destructor; mandatory.
GDExtensionClassRecreateInstance recreate_instance_func;
// Queries a virtual function by name and returns a callback to invoke the requested virtual function.
GDExtensionClassGetVirtual get_virtual_func;
// Paired with `call_virtual_with_data_func`, this is an alternative to `get_virtual_func` for extensions that
// need or benefit from extra data when calling virtual functions.
// Returns user data that will be passed to `call_virtual_with_data_func`.
// Returning `NULL` from this function signals to Godot that the virtual function is not overridden.
// Data returned from this function should be managed by the extension and must be valid until the extension is deinitialized.
// You should supply either `get_virtual_func`, or `get_virtual_call_data_func` with `call_virtual_with_data_func`.
GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
// Used to call virtual functions when `get_virtual_call_data_func` is not null.
GDExtensionClassCallVirtualWithData call_virtual_with_data_func;
GDExtensionClassGetRID get_rid_func;
void *class_userdata; // Per-class user data, later accessible in instance bindings.
} GDExtensionClassCreationInfo3;
typedef void *GDExtensionClassLibraryPtr;
@ -2240,6 +2274,9 @@ typedef void (*GDExtensionInterfaceObjectSetInstance)(GDExtensionObjectPtr p_o,
*
* Gets the class name of an Object.
*
* If the GDExtension wraps the Godot object in an abstraction specific to its class, this is the
* function that should be used to determine which wrapper to use.
*
* @param p_object A pointer to the Object.
* @param p_library A pointer the library received by the GDExtension's entry point function.
* @param r_class_name A pointer to a String to receive the class name.
@ -2486,7 +2523,7 @@ typedef void *(*GDExtensionInterfaceClassdbGetClassTag)(GDExtensionConstStringNa
/**
* @name classdb_register_extension_class
* @since 4.1
* @deprecated in Godot 4.2. Use `classdb_register_extension_class2` instead.
* @deprecated in Godot 4.2. Use `classdb_register_extension_class3` instead.
*
* Registers an extension class in the ClassDB.
*
@ -2502,6 +2539,7 @@ typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass)(GDExtensionCla
/**
* @name classdb_register_extension_class2
* @since 4.2
* @deprecated in Godot 4.3. Use `classdb_register_extension_class3` instead.
*
* Registers an extension class in the ClassDB.
*
@ -2514,6 +2552,21 @@ typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass)(GDExtensionCla
*/
typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass2)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs);
/**
* @name classdb_register_extension_class3
* @since 4.3
*
* Registers an extension class in the ClassDB.
*
* Provided struct can be safely freed once the function returns.
*
* @param p_library A pointer the library received by the GDExtension's entry point function.
* @param p_class_name A pointer to a StringName with the class name.
* @param p_parent_class_name A pointer to a StringName with the parent class name.
* @param p_extension_funcs A pointer to a GDExtensionClassCreationInfo2 struct.
*/
typedef void (*GDExtensionInterfaceClassdbRegisterExtensionClass3)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs);
/**
* @name classdb_register_extension_class_method
* @since 4.1

View file

@ -70,6 +70,138 @@ 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;
}
// 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 +478,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,10 +499,17 @@ 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->gdextension->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;
@ -379,6 +518,82 @@ Object *ClassDB::instantiate(const StringName &p_class) {
}
}
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.");
ERR_FAIL_NULL_V_MSG(ti->gdextension, nullptr, "Class '" + String(p_class) + "' has no native extension.");
}
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;
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;
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
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 +1931,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;
}
@ -1826,6 +2041,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;

View file

@ -149,6 +149,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 +182,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>
@ -253,6 +259,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 +271,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);

View file

@ -492,14 +492,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;
@ -1792,6 +1800,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 +1937,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.

View file

@ -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;
@ -755,6 +759,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 +982,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();

View file

@ -95,7 +95,7 @@ Variant GDScriptNativeClass::_new() {
}
Object *GDScriptNativeClass::instantiate() {
return ClassDB::instantiate(name);
return ClassDB::instantiate_no_placeholders(name);
}
Variant GDScriptNativeClass::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {