Skip to content

Commit f7ffc4f

Browse files
committed
Automatically register only engine classes whose header has been included
(cherry picked from commit b507b3e)
1 parent 62cb5ea commit f7ffc4f

File tree

8 files changed

+122
-78
lines changed

8 files changed

+122
-78
lines changed

binding_generator.py

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
130130
if sources:
131131
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
132132
files.append(str(utility_functions_source_path.as_posix()))
133-
register_engine_classes_source_path = source_gen_folder / "register_engine_classes.cpp"
134-
files.append(str(register_engine_classes_source_path.as_posix()))
135133

136134
return files
137135

@@ -1170,10 +1168,6 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
11701168
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
11711169
)
11721170

1173-
register_engine_classes_filename = Path(output_dir) / "src" / "register_engine_classes.cpp"
1174-
with register_engine_classes_filename.open("w+", encoding="utf-8") as source_file:
1175-
source_file.write(generate_register_engine_classes_source(api))
1176-
11771171
for native_struct in api["native_structures"]:
11781172
struct_name = native_struct["name"]
11791173
snake_struct_name = camel_to_snake(struct_name)
@@ -1248,7 +1242,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
12481242
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
12491243

12501244
if class_name == "EditorPlugin":
1251-
result.append("#include <godot_cpp/templates/vector.hpp>")
1245+
result.append("#include <godot_cpp/classes/editor_plugin_registration.hpp>")
12521246

12531247
if len(fully_used_classes) > 0:
12541248
result.append("")
@@ -1400,30 +1394,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
14001394
result.append("};")
14011395
result.append("")
14021396

1403-
if class_name == "EditorPlugin":
1404-
result.append("class EditorPlugins {")
1405-
result.append("private:")
1406-
result.append("\tstatic Vector<StringName> plugin_classes;")
1407-
result.append("")
1408-
result.append("public:")
1409-
result.append("\tstatic void add_plugin_class(const StringName &p_class_name);")
1410-
result.append("\tstatic void remove_plugin_class(const StringName &p_class_name);")
1411-
result.append("\tstatic void deinitialize(GDExtensionInitializationLevel p_level);")
1412-
result.append("")
1413-
1414-
result.append("\ttemplate <class T>")
1415-
result.append("\tstatic void add_by_type() {")
1416-
result.append("\t\tadd_plugin_class(T::get_class_static());")
1417-
result.append("\t}")
1418-
1419-
result.append("\ttemplate <class T>")
1420-
result.append("\tstatic void remove_by_type() {")
1421-
result.append("\t\tremove_plugin_class(T::get_class_static());")
1422-
result.append("\t}")
1423-
1424-
result.append("};")
1425-
result.append("")
1426-
14271397
result.append("} // namespace godot")
14281398
result.append("")
14291399

@@ -1648,38 +1618,6 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
16481618
return "\n".join(result)
16491619

16501620

1651-
def generate_register_engine_classes_source(api):
1652-
includes = []
1653-
registrations = []
1654-
1655-
for class_api in api["classes"]:
1656-
if class_api["name"] == "ClassDB":
1657-
continue
1658-
1659-
class_name = class_api["name"]
1660-
snake_class_name = camel_to_snake(class_name)
1661-
1662-
includes.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
1663-
registrations.append(f"\tClassDB::register_engine_class<{class_name}>();")
1664-
1665-
result = []
1666-
add_header(f"register_engine_classes.cpp", result)
1667-
1668-
result.append("#include <godot_cpp/godot.hpp>")
1669-
result.append("")
1670-
result = result + includes
1671-
result.append("")
1672-
result.append("namespace godot {")
1673-
result.append("")
1674-
result.append("void GDExtensionBinding::register_engine_classes() {")
1675-
result = result + registrations
1676-
result.append("}")
1677-
result.append("")
1678-
result.append("} // namespace godot ")
1679-
1680-
return "\n".join(result)
1681-
1682-
16831621
def generate_global_constants(api, output_dir):
16841622
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
16851623
source_gen_folder = Path(output_dir) / "src" / "classes"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**************************************************************************/
2+
/* editor_plugin_registration.hpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
32+
#define GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
33+
34+
#include <godot_cpp/templates/vector.hpp>
35+
36+
namespace godot {
37+
38+
class EditorPlugin;
39+
class StringName;
40+
41+
class EditorPlugins {
42+
private:
43+
static Vector<StringName> plugin_classes;
44+
45+
public:
46+
static void add_plugin_class(const StringName &p_class_name);
47+
static void remove_plugin_class(const StringName &p_class_name);
48+
static void deinitialize(GDExtensionInitializationLevel p_level);
49+
50+
template <class T>
51+
static void add_by_type() {
52+
add_plugin_class(T::get_class_static());
53+
}
54+
template <class T>
55+
static void remove_by_type() {
56+
remove_plugin_class(T::get_class_static());
57+
}
58+
};
59+
60+
} // namespace godot
61+
62+
#endif // GODOT_EDITOR_PLUGIN_REGISTRATION_HPP

include/godot_cpp/classes/wrapped.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ class Wrapped {
9595
GodotObject *_owner = nullptr;
9696
};
9797

98+
namespace internal {
99+
100+
typedef void (*EngineClassRegistrationCallback)();
101+
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
102+
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
103+
void register_engine_classes();
104+
105+
template <class T>
106+
struct EngineClassRegistration {
107+
EngineClassRegistration() {
108+
add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
109+
}
110+
111+
static void callback() {
112+
register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
113+
}
114+
};
115+
116+
} // namespace internal
117+
98118
} // namespace godot
99119

100120
#define GDCLASS(m_class, m_inherits) \
@@ -308,6 +328,7 @@ public:
308328
// Don't use this for your classes, use GDCLASS() instead.
309329
#define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) \
310330
private: \
331+
inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
311332
void operator=(const m_class &p_rval) {} \
312333
\
313334
protected: \

include/godot_cpp/core/class_db.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ class ClassDB {
122122
static void register_class(bool p_virtual = false);
123123
template <class T>
124124
static void register_abstract_class();
125-
template <class T>
126-
static void register_engine_class();
125+
126+
_FORCE_INLINE_ static void _register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
127+
instance_binding_callbacks[p_name] = p_callbacks;
128+
}
127129

128130
template <class N, class M, typename... VarArgs>
129131
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
@@ -226,11 +228,6 @@ void ClassDB::register_abstract_class() {
226228
ClassDB::_register_class<T, true>();
227229
}
228230

229-
template <class T>
230-
void ClassDB::register_engine_class() {
231-
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
232-
}
233-
234231
template <class N, class M, typename... VarArgs>
235232
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
236233
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.

include/godot_cpp/godot.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ enum ModuleInitializationLevel {
191191
};
192192

193193
class GDExtensionBinding {
194-
private:
195-
static void register_engine_classes();
196-
197194
public:
198195
using Callback = void (*)(ModuleInitializationLevel p_level);
199196

src/classes/editor_plugin.cpp renamed to src/classes/editor_plugin_registration.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**************************************************************************/
2-
/* editor_plugin.cpp */
2+
/* editor_plugin_registration.cpp */
33
/**************************************************************************/
44
/* This file is part of: */
55
/* GODOT ENGINE */
@@ -28,9 +28,9 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
#include <godot_cpp/classes/editor_plugin.hpp>
31+
#include <godot_cpp/classes/editor_plugin_registration.hpp>
3232

33-
#include <godot_cpp/variant/string_name.hpp>
33+
#include <godot_cpp/variant/variant.hpp>
3434

3535
namespace godot {
3636

src/classes/wrapped.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31+
#include <vector>
32+
3133
#include <godot_cpp/classes/wrapped.hpp>
3234

3335
#include <godot_cpp/variant/builtin_types.hpp>
3436

3537
#include <godot_cpp/classes/object.hpp>
3638

39+
#include <godot_cpp/core/class_db.hpp>
40+
3741
namespace godot {
3842

3943
const StringName *Wrapped::_get_extension_class_name() const {
@@ -60,4 +64,29 @@ void postinitialize_handler(Wrapped *p_wrapped) {
6064
p_wrapped->_postinitialize();
6165
}
6266

67+
namespace internal {
68+
69+
std::vector<EngineClassRegistrationCallback> &get_engine_class_registration_callbacks() {
70+
static std::vector<EngineClassRegistrationCallback> engine_class_registration_callbacks;
71+
return engine_class_registration_callbacks;
72+
}
73+
74+
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback) {
75+
get_engine_class_registration_callbacks().push_back(p_callback);
76+
}
77+
78+
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
79+
ClassDB::_register_engine_class(p_name, p_callbacks);
80+
}
81+
82+
void register_engine_classes() {
83+
std::vector<EngineClassRegistrationCallback> &engine_class_registration_callbacks = get_engine_class_registration_callbacks();
84+
for (EngineClassRegistrationCallback cb : engine_class_registration_callbacks) {
85+
cb();
86+
}
87+
engine_class_registration_callbacks.clear();
88+
}
89+
90+
} // namespace internal
91+
6392
} // namespace godot

src/godot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include <godot_cpp/godot.hpp>
3232

33-
#include <godot_cpp/classes/editor_plugin.hpp>
33+
#include <godot_cpp/classes/editor_plugin_registration.hpp>
3434
#include <godot_cpp/classes/wrapped.hpp>
3535
#include <godot_cpp/core/class_db.hpp>
3636
#include <godot_cpp/core/memory.hpp>
@@ -406,7 +406,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
406406
ERR_FAIL_NULL_V_MSG(init_callback, false, "Initialization callback must be defined.");
407407

408408
Variant::init_bindings();
409-
register_engine_classes();
409+
godot::internal::register_engine_classes();
410410

411411
return true;
412412
}

0 commit comments

Comments
 (0)