Skip to content

Commit 2474962

Browse files
committed
Ensure GDExtension class is the correct type for the Godot engine class
1 parent a5c6ca5 commit 2474962

File tree

12 files changed

+124
-18
lines changed

12 files changed

+124
-18
lines changed

binding_generator.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
128128
if sources:
129129
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
130130
files.append(str(utility_functions_source_path.as_posix()))
131+
register_engine_classes_source_path = source_gen_folder / "register_engine_classes.cpp"
132+
files.append(str(register_engine_classes_source_path.as_posix()))
131133

132134
return files
133135

@@ -1157,6 +1159,10 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
11571159
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
11581160
)
11591161

1162+
register_engine_classes_filename = Path(output_dir) / "src" / "register_engine_classes.cpp"
1163+
with register_engine_classes_filename.open("w+") as source_file:
1164+
source_file.write(generate_register_engine_classes_source(api))
1165+
11601166
for native_struct in api["native_structures"]:
11611167
struct_name = native_struct["name"]
11621168
snake_struct_name = camel_to_snake(struct_name)
@@ -1551,6 +1557,38 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
15511557
return "\n".join(result)
15521558

15531559

1560+
def generate_register_engine_classes_source(api):
1561+
includes = []
1562+
registrations = []
1563+
1564+
for class_api in api["classes"]:
1565+
if class_api["name"] == "ClassDB":
1566+
continue
1567+
1568+
class_name = class_api["name"]
1569+
snake_class_name = camel_to_snake(class_name)
1570+
1571+
includes.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
1572+
registrations.append(f"\tClassDB::register_engine_class<{class_name}>();")
1573+
1574+
result = []
1575+
add_header(f"register_engine_classes.cpp", result)
1576+
1577+
result.append("#include <godot_cpp/godot.hpp>")
1578+
result.append("")
1579+
result = result + includes
1580+
result.append("")
1581+
result.append("namespace godot {")
1582+
result.append("")
1583+
result.append("void GDExtensionBinding::register_engine_classes() {")
1584+
result = result + registrations
1585+
result.append("}")
1586+
result.append("")
1587+
result.append("} // namespace godot ")
1588+
1589+
return "\n".join(result)
1590+
1591+
15541592
def generate_global_constants(api, output_dir):
15551593
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
15561594
source_gen_folder = Path(output_dir) / "src" / "classes"

gdextension/gdextension_interface.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,19 @@ typedef void (*GDExtensionInterfaceObjectSetInstanceBinding)(GDExtensionObjectPt
18791879
*/
18801880
typedef void (*GDExtensionInterfaceObjectSetInstance)(GDExtensionObjectPtr p_o, GDExtensionConstStringNamePtr p_classname, GDExtensionClassInstancePtr p_instance); /* p_classname should be a registered extension class and should extend the p_o object's class. */
18811881

1882+
/**
1883+
* @name object_get_class_name
1884+
*
1885+
* Gets the class name of an Object.
1886+
*
1887+
* @param p_object A pointer to the Object.
1888+
* @param p_library A pointer the library received by the GDExtension's entry point function.
1889+
* @param r_class_name A pointer to a String to receive the class name.
1890+
*
1891+
* @return true if successful in getting the class name; otherwise false.
1892+
*/
1893+
typedef GDExtensionBool (*GDExtensionInterfaceObjectGetClassName)(GDExtensionConstObjectPtr p_object, GDExtensionClassLibraryPtr p_library, GDExtensionStringNamePtr r_class_name);
1894+
18821895
/**
18831896
* @name object_cast_to
18841897
*

include/godot_cpp/classes/ref.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ struct PtrToArg<Ref<T>> {
242242
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
243243
// Important: p_ptr is T*, not Ref<T>*, since Object* is what engine gives to ptrcall.
244244
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
245-
return Ref<T>(reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding(
246-
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
247-
godot::internal::token, &T::___binding_callbacks)));
245+
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))));
248246
}
249247

250248
typedef Ref<T> EncodeT;
@@ -267,9 +265,7 @@ struct PtrToArg<const Ref<T> &> {
267265

268266
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
269267
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
270-
return Ref<T>(reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding(
271-
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
272-
godot::internal::token, &T::___binding_callbacks)));
268+
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))));
273269
}
274270
};
275271

include/godot_cpp/core/class_db.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class ClassDB {
104104
private:
105105
// This may only contain custom classes, not Godot classes
106106
static std::unordered_map<StringName, ClassInfo> classes;
107+
static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
107108

108109
static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
109110
static void initialize_class(const ClassInfo &cl);
@@ -117,6 +118,8 @@ class ClassDB {
117118
static void register_class(bool p_virtual = false);
118119
template <class T>
119120
static void register_abstract_class();
121+
template <class T>
122+
static void register_engine_class();
120123

121124
template <class N, class M, typename... VarArgs>
122125
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
@@ -137,6 +140,7 @@ class ClassDB {
137140
static MethodBind *get_method(const StringName &p_class, const StringName &p_method);
138141

139142
static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name);
143+
static const GDExtensionInstanceBindingCallbacks *get_instance_binding_callbacks(const StringName &p_class);
140144

141145
static void initialize(GDExtensionInitializationLevel p_level);
142146
static void deinitialize(GDExtensionInitializationLevel p_level);
@@ -161,6 +165,8 @@ class ClassDB {
161165

162166
template <class T, bool is_abstract>
163167
void ClassDB::_register_class(bool p_virtual) {
168+
instance_binding_callbacks[T::get_class_static()] = &T::___binding_callbacks;
169+
164170
// Register this class within our plugin
165171
ClassInfo cl;
166172
cl.name = T::get_class_static();
@@ -213,6 +219,11 @@ void ClassDB::register_abstract_class() {
213219
ClassDB::_register_class<T, true>();
214220
}
215221

222+
template <class T>
223+
void ClassDB::register_engine_class() {
224+
instance_binding_callbacks[T::get_class_static()] = &T::___binding_callbacks;
225+
}
226+
216227
template <class N, class M, typename... VarArgs>
217228
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
218229
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.

include/godot_cpp/core/engine_ptrcall.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ O *_call_native_mb_ret_obj(const GDExtensionMethodBindPtr mb, void *instance, co
5151
if (ret == nullptr) {
5252
return nullptr;
5353
}
54-
return reinterpret_cast<O *>(internal::gdextension_interface_object_get_instance_binding(ret, internal::token, &O::___binding_callbacks));
54+
return reinterpret_cast<O *>(internal::get_object_instance_binding(ret));
5555
}
5656

5757
template <class R, class... Args>
@@ -81,7 +81,7 @@ Object *_call_utility_ret_obj(const GDExtensionPtrUtilityFunction func, void *in
8181
GodotObject *ret = nullptr;
8282
std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
8383
func(&ret, mb_args.data(), mb_args.size());
84-
return (Object *)internal::gdextension_interface_object_get_instance_binding(ret, internal::token, &Object::___binding_callbacks);
84+
return (Object *)internal::get_object_instance_binding(ret);
8585
}
8686

8787
template <class... Args>

include/godot_cpp/core/method_ptrcall.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <godot_cpp/core/defs.hpp>
3535

36+
#include <godot_cpp/core/object.hpp>
3637
#include <godot_cpp/godot.hpp>
3738
#include <godot_cpp/variant/variant.hpp>
3839

@@ -168,9 +169,7 @@ MAKE_PTRARG_BY_REFERENCE(Variant);
168169
template <class T>
169170
struct PtrToArg<T *> {
170171
_FORCE_INLINE_ static T *convert(const void *p_ptr) {
171-
return reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding(
172-
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
173-
godot::internal::token, &T::___binding_callbacks));
172+
return reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))));
174173
}
175174
typedef Object *EncodeT;
176175
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
@@ -181,9 +180,7 @@ struct PtrToArg<T *> {
181180
template <class T>
182181
struct PtrToArg<const T *> {
183182
_FORCE_INLINE_ static const T *convert(const void *p_ptr) {
184-
return reinterpret_cast<const T *>(godot::internal::gdextension_interface_object_get_instance_binding(
185-
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
186-
godot::internal::token, &T::___binding_callbacks));
183+
return reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))));
187184
}
188185
typedef const Object *EncodeT;
189186
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {

include/godot_cpp/core/object.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

5353
namespace godot {
5454

55+
namespace internal {
56+
57+
Object *get_object_instance_binding(GodotObject *);
58+
59+
} // namespace internal
60+
5561
struct MethodInfo {
5662
StringName name;
5763
PropertyInfo return_val;
@@ -128,7 +134,7 @@ class ObjectDB {
128134
if (obj == nullptr) {
129135
return nullptr;
130136
}
131-
return reinterpret_cast<Object *>(internal::gdextension_interface_object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks));
137+
return internal::get_object_instance_binding(obj);
132138
}
133139
};
134140

@@ -142,7 +148,7 @@ T *Object::cast_to(Object *p_object) {
142148
if (casted == nullptr) {
143149
return nullptr;
144150
}
145-
return reinterpret_cast<T *>(internal::gdextension_interface_object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
151+
return dynamic_cast<T *>(internal::get_object_instance_binding(casted));
146152
}
147153

148154
template <class T>
@@ -155,7 +161,7 @@ const T *Object::cast_to(const Object *p_object) {
155161
if (casted == nullptr) {
156162
return nullptr;
157163
}
158-
return reinterpret_cast<const T *>(internal::gdextension_interface_object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
164+
return dynamic_cast<const T *>(internal::get_object_instance_binding(casted));
159165
}
160166

161167
} // namespace godot

include/godot_cpp/godot.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extern "C" GDExtensionInterfaceGlobalGetSingleton gdextension_interface_global_g
159159
extern "C" GDExtensionInterfaceObjectGetInstanceBinding gdextension_interface_object_get_instance_binding;
160160
extern "C" GDExtensionInterfaceObjectSetInstanceBinding gdextension_interface_object_set_instance_binding;
161161
extern "C" GDExtensionInterfaceObjectSetInstance gdextension_interface_object_set_instance;
162+
extern "C" GDExtensionInterfaceObjectGetClassName gdextension_interface_object_get_class_name;
162163
extern "C" GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to;
163164
extern "C" GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_instance_from_id;
164165
extern "C" GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id;
@@ -188,6 +189,9 @@ enum ModuleInitializationLevel {
188189
};
189190

190191
class GDExtensionBinding {
192+
private:
193+
static void register_engine_classes();
194+
191195
public:
192196
using Callback = void (*)(ModuleInitializationLevel p_level);
193197

src/core/class_db.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
namespace godot {
4141

4242
std::unordered_map<StringName, ClassDB::ClassInfo> ClassDB::classes;
43+
std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> ClassDB::instance_binding_callbacks;
4344
GDExtensionInitializationLevel ClassDB::current_level = GDEXTENSION_INITIALIZATION_CORE;
4445

4546
MethodDefinition D_METHOD(StringName p_name) {
@@ -314,6 +315,12 @@ GDExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, GDExtens
314315
return nullptr;
315316
}
316317

318+
const GDExtensionInstanceBindingCallbacks *ClassDB::get_instance_binding_callbacks(const StringName &p_class) {
319+
std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *>::iterator callbacks_it = instance_binding_callbacks.find(p_class);
320+
ERR_FAIL_COND_V_MSG(callbacks_it == instance_binding_callbacks.end(), nullptr, String("Cannot find instance binding callbacks for class '{0}'.").format(Array::make(p_class)));
321+
return callbacks_it->second;
322+
}
323+
317324
void ClassDB::bind_virtual_method(const StringName &p_class, const StringName &p_method, GDExtensionClassCallVirtual p_call) {
318325
std::unordered_map<StringName, ClassInfo>::iterator type_it = classes.find(p_class);
319326
ERR_FAIL_COND_MSG(type_it == classes.end(), String("Class '{0}' doesn't exist.").format(Array::make(p_class)));

src/core/object.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,38 @@
3030

3131
#include <godot_cpp/core/object.hpp>
3232

33+
#include <godot_cpp/core/class_db.hpp>
34+
3335
namespace godot {
3436

37+
namespace internal {
38+
39+
Object *get_object_instance_binding(GodotObject *p_engine_object) {
40+
if (p_engine_object == nullptr) {
41+
return nullptr;
42+
}
43+
44+
// Get existing instance binding, if one already exists.
45+
GDExtensionObjectPtr instance = gdextension_interface_object_get_instance_binding(p_engine_object, token, nullptr);
46+
if (instance != nullptr) {
47+
return reinterpret_cast<Object *>(instance);
48+
}
49+
50+
// Otherwise, try to look up the correct binding callbacks.
51+
const GDExtensionInstanceBindingCallbacks *binding_callbacks = nullptr;
52+
StringName class_name;
53+
if (gdextension_interface_object_get_class_name(p_engine_object, library, reinterpret_cast<GDExtensionStringNamePtr>(class_name._native_ptr()))) {
54+
binding_callbacks = ClassDB::get_instance_binding_callbacks(class_name);
55+
}
56+
if (binding_callbacks == nullptr) {
57+
binding_callbacks = &Object::___binding_callbacks;
58+
}
59+
60+
return reinterpret_cast<Object *>(gdextension_interface_object_get_instance_binding(p_engine_object, token, binding_callbacks));
61+
}
62+
63+
} // namespace internal
64+
3565
MethodInfo::MethodInfo() :
3666
flags(GDEXTENSION_METHOD_FLAG_NORMAL) {}
3767

0 commit comments

Comments
 (0)