GDExtension: Prevent breaking compatibility for unexposed classes that can only be created once

(cherry picked from commit 2c707a911f)
This commit is contained in:
David Snopek 2025-09-30 14:34:46 -05:00 committed by Thaddeus Crews
parent c834443ef1
commit 2cb6d30dd1
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
4 changed files with 33 additions and 5 deletions

View file

@ -267,6 +267,7 @@ void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library
};
const ClassCreationDeprecatedInfo legacy = {
false,
p_extension_funcs->notification_func, // GDExtensionClassNotification notification_func;
p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func;
@ -281,7 +282,7 @@ void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_librar
const GDExtensionClassCreationInfo5 class_info5 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
true, // GDExtensionBool is_exposed;
p_extension_funcs->is_exposed, // GDExtensionBool is_exposed;
false, // GDExtensionBool is_runtime;
nullptr, // GDExtensionConstStringPtr icon_path;
p_extension_funcs->set_func, // GDExtensionClassSet set_func;
@ -305,6 +306,7 @@ void GDExtension::_register_extension_class2(GDExtensionClassLibraryPtr p_librar
};
const ClassCreationDeprecatedInfo legacy = {
!p_extension_funcs->is_exposed, // bool legacy_unexposed_class;
nullptr, // GDExtensionClassNotification notification_func;
p_extension_funcs->free_property_list_func, // GDExtensionClassFreePropertyList free_property_list_func;
p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func;
@ -319,7 +321,7 @@ void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_librar
const GDExtensionClassCreationInfo5 class_info5 = {
p_extension_funcs->is_virtual, // GDExtensionBool is_virtual;
p_extension_funcs->is_abstract, // GDExtensionBool is_abstract;
true, // GDExtensionBool is_exposed;
p_extension_funcs->is_exposed, // GDExtensionBool is_exposed;
p_extension_funcs->is_runtime, // GDExtensionBool is_runtime;
nullptr, // GDExtensionConstStringPtr icon_path;
p_extension_funcs->set_func, // GDExtensionClassSet set_func;
@ -343,6 +345,7 @@ void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_librar
};
const ClassCreationDeprecatedInfo legacy = {
!p_extension_funcs->is_exposed, // bool legacy_unexposed_class;
nullptr, // GDExtensionClassNotification notification_func;
nullptr, // GDExtensionClassFreePropertyList free_property_list_func;
p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance2 create_instance_func;
@ -355,9 +358,16 @@ void GDExtension::_register_extension_class3(GDExtensionClassLibraryPtr p_librar
void GDExtension::_register_extension_class4(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs) {
GDExtensionClassCreationInfo5 class_info5 = *p_extension_funcs;
// Force classes to be exposed, because the behavior of unexposed classes changed in an incompatible (albeit, minor) way.
class_info5.is_exposed = true;
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5);
const ClassCreationDeprecatedInfo legacy = {
!p_extension_funcs->is_exposed, // bool legacy_unexposed_class;
nullptr, // GDExtensionClassNotification notification_func;
nullptr, // GDExtensionClassFreePropertyList free_property_list_func;
nullptr, // GDExtensionClassCreateInstance2 create_instance_func;
nullptr, // GDExtensionClassGetRID get_rid;
nullptr, // GDExtensionClassGetVirtual get_virtual_func;
nullptr, // GDExtensionClassGetVirtual get_virtual_func;
};
_register_extension_class_internal(p_library, p_class_name, p_parent_class_name, &class_info5, &legacy);
}
#endif // DISABLE_DEPRECATED
@ -447,6 +457,7 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
extension->gdextension.validate_property = p_extension_funcs->validate_property_func;
#ifndef DISABLE_DEPRECATED
if (p_deprecated_funcs) {
extension->gdextension.legacy_unexposed_class = p_deprecated_funcs->legacy_unexposed_class;
extension->gdextension.notification = p_deprecated_funcs->notification_func;
extension->gdextension.free_property_list = p_deprecated_funcs->free_property_list_func;
extension->gdextension.create_instance = p_deprecated_funcs->create_instance_func;

View file

@ -67,6 +67,7 @@ class GDExtension : public Resource {
struct ClassCreationDeprecatedInfo {
#ifndef DISABLE_DEPRECATED
bool legacy_unexposed_class = false;
GDExtensionClassNotification notification_func = nullptr;
GDExtensionClassFreePropertyList free_property_list_func = nullptr;
GDExtensionClassCreateInstance create_instance_func = nullptr;

View file

@ -545,6 +545,12 @@ Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require
}
ERR_FAIL_NULL_V_MSG(ti, nullptr, vformat("Cannot get class '%s'.", String(p_class)));
ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, vformat("Class '%s' is disabled.", String(p_class)));
#ifndef DISABLE_DEPRECATED
// Force legacy unexposed classes to skip the exposed check to preserve backcompat.
if (ti->gdextension && ti->gdextension->legacy_unexposed_class) {
p_exposed_only = false;
}
#endif // DISABLE_DEPRECATED
if (p_exposed_only) {
ERR_FAIL_COND_V_MSG(!ti->exposed, nullptr, vformat("Class '%s' isn't exposed.", String(p_class)));
}
@ -604,6 +610,13 @@ bool ClassDB::_can_instantiate(ClassInfo *p_class_info, bool p_exposed_only) {
return false;
}
#ifndef DISABLE_DEPRECATED
// Force legacy unexposed classes to skip the exposed check to preserve backcompat.
if (p_class_info->gdextension && p_class_info->gdextension->legacy_unexposed_class) {
p_exposed_only = false;
}
#endif // DISABLE_DEPRECATED
if (p_exposed_only && !p_class_info->exposed) {
return false;
}

View file

@ -334,6 +334,9 @@ struct ObjectGDExtension {
bool is_runtime = false;
bool is_placeholder = false;
#endif
#ifndef DISABLE_DEPRECATED
bool legacy_unexposed_class = false;
#endif // DISABLE_DEPRECATED
GDExtensionClassSet set;
GDExtensionClassGet get;
GDExtensionClassGetPropertyList get_property_list;