From d0574749739a616083c4e424d61fdd9d2e639a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=20=C3=86nema=20=28Hamid=2EMemar=29?= <94260384+TheAenema@users.noreply.github.com> Date: Mon, 22 Dec 2025 19:43:02 +0000 Subject: [PATCH] Add API Type Override Method to `ClassDB` + `ClassDB` Binding Enhancements --- core/core_bind.cpp | 18 +++++++++++++++++- core/core_bind.h | 4 ++++ core/object/class_db.cpp | 11 +++++++++++ core/object/class_db.h | 1 + doc/classes/ClassDB.xml | 23 +++++++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/core/core_bind.cpp b/core/core_bind.cpp index c3c18625c2a..7831509711c 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1642,6 +1642,19 @@ ClassDB::APIType ClassDB::class_get_api_type(const StringName &p_class) const { return (APIType)api_type; } +Error ClassDB::class_override_api_type(const StringName &p_class, ClassDB::APIType p_api) const { + return ::ClassDB::override_api_type(p_class, (::ClassDB::APIType)p_api); +} + +ClassDB::APIType ClassDB::get_current_api() const { + ::ClassDB::APIType api_type = ::ClassDB::get_current_api(); + return (APIType)api_type; +} + +void ClassDB::set_current_api(ClassDB::APIType p_api) const { + ::ClassDB::set_current_api((::ClassDB::APIType)p_api); +} + bool ClassDB::class_has_signal(const StringName &p_class, const StringName &p_signal) const { return ::ClassDB::has_signal(p_class, p_signal); } @@ -1838,7 +1851,7 @@ void ClassDB::get_argument_options(const StringName &p_function, int p_idx, List pf == "class_has_method" || pf == "class_get_method_list" || pf == "class_get_integer_constant_list" || pf == "class_has_integer_constant" || pf == "class_get_integer_constant" || pf == "class_has_enum" || pf == "class_get_enum_list" || pf == "class_get_enum_constants" || pf == "class_get_integer_constant_enum" || - pf == "is_class_enabled" || pf == "is_class_enum_bitfield" || pf == "class_get_api_type"); + pf == "is_class_enabled" || pf == "is_class_enum_bitfield" || pf == "class_get_api_type" || pf == "get_current_api"); } if (first_argument_is_class || pf == "is_parent_class") { for (const String &E : get_class_list()) { @@ -1860,6 +1873,9 @@ void ClassDB::_bind_methods() { ::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate); ::ClassDB::bind_method(D_METHOD("class_get_api_type", "class"), &ClassDB::class_get_api_type); + ::ClassDB::bind_method(D_METHOD("class_override_api_type", "class", "api"), &ClassDB::class_override_api_type); + ::ClassDB::bind_method(D_METHOD("get_current_api"), &ClassDB::get_current_api); + ::ClassDB::bind_method(D_METHOD("set_current_api", "api"), &ClassDB::set_current_api); ::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::class_has_signal); ::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::class_get_signal); diff --git a/core/core_bind.h b/core/core_bind.h index 838e6e85f6c..f6ddbeccec6 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -525,6 +525,10 @@ class ClassDB : public Object { Variant instantiate(const StringName &p_class) const; APIType class_get_api_type(const StringName &p_class) const; + Error class_override_api_type(const StringName &p_class, APIType p_api) const; + APIType get_current_api() const; + void set_current_api(APIType p_api) const; + bool class_has_signal(const StringName &p_class, const StringName &p_signal) const; Dictionary class_get_signal(const StringName &p_class, const StringName &p_signal) const; TypedArray class_get_signal_list(const StringName &p_class, bool p_no_inheritance = false) const; diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index cb8cc09ba93..aff2195459a 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -369,6 +369,17 @@ ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) { return ti->api; } +Error ClassDB::override_api_type(const StringName &p_class, ClassDB::APIType p_api) { + Locker::Lock lock(Locker::STATE_WRITE); + + ClassInfo *ti = classes.getptr(p_class); + + ERR_FAIL_NULL_V_MSG(ti, Error::ERR_DOES_NOT_EXIST, vformat("Cannot get class '%s'.", String(p_class))); + ti->api = p_api; + + return Error::OK; +} + uint32_t ClassDB::get_api_hash(APIType p_api) { #ifdef DEBUG_ENABLED Locker::Lock lock(Locker::STATE_WRITE); diff --git a/core/object/class_db.h b/core/object/class_db.h index 3702f1a1ad9..47659fbf6f7 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -336,6 +336,7 @@ class ClassDB { 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); + static Error override_api_type(const StringName &p_class, APIType p_api); static uint32_t get_api_hash(APIType p_api); diff --git a/doc/classes/ClassDB.xml b/doc/classes/ClassDB.xml index f8ec7086479..4e3caf225b8 100644 --- a/doc/classes/ClassDB.xml +++ b/doc/classes/ClassDB.xml @@ -189,6 +189,15 @@ Returns whether [param class] or its ancestry has a signal called [param signal] or not. + + + + + + Overrides the API type of [param class] to [param api]. See [enum ClassDB.APIType]. + [b]Note:[/b] This should only be used with a thorough understanding of its implications. + + @@ -205,6 +214,12 @@ [b]Note:[/b] Script-defined classes with [code]class_name[/code] are not included in this list. Use [method ProjectSettings.get_global_class_list] to get a list of script-defined classes instead. + + + + Returns the currently active API type. See [enum ClassDB.APIType]. + + @@ -250,6 +265,14 @@ Returns whether [param inherits] is an ancestor of [param class] or not. + + + + + Sets the globally active API type to [param api]. See [enum ClassDB.APIType]. + [b]Note:[/b] This should only be used with a thorough understanding of its implications. + +