Skip to content

Commit b507b3e

Browse files
committed
Automatically register only engine classes whose header has been included
1 parent ef2f63a commit b507b3e

8 files changed

+114
-78
lines changed

binding_generator.py

+1-63
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
131131
if sources:
132132
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
133133
files.append(str(utility_functions_source_path.as_posix()))
134-
register_engine_classes_source_path = source_gen_folder / "register_engine_classes.cpp"
135-
files.append(str(register_engine_classes_source_path.as_posix()))
136134

137135
return files
138136

@@ -1207,10 +1205,6 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
12071205
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
12081206
)
12091207

1210-
register_engine_classes_filename = Path(output_dir) / "src" / "register_engine_classes.cpp"
1211-
with register_engine_classes_filename.open("w+", encoding="utf-8") as source_file:
1212-
source_file.write(generate_register_engine_classes_source(api))
1213-
12141208
for native_struct in api["native_structures"]:
12151209
struct_name = native_struct["name"]
12161210
snake_struct_name = camel_to_snake(struct_name)
@@ -1285,7 +1279,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
12851279
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
12861280

12871281
if class_name == "EditorPlugin":
1288-
result.append("#include <godot_cpp/templates/vector.hpp>")
1282+
result.append("#include <godot_cpp/classes/editor_plugin_registration.hpp>")
12891283

12901284
if len(fully_used_classes) > 0:
12911285
result.append("")
@@ -1437,30 +1431,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
14371431
result.append("};")
14381432
result.append("")
14391433

1440-
if class_name == "EditorPlugin":
1441-
result.append("class EditorPlugins {")
1442-
result.append("private:")
1443-
result.append("\tstatic Vector<StringName> plugin_classes;")
1444-
result.append("")
1445-
result.append("public:")
1446-
result.append("\tstatic void add_plugin_class(const StringName &p_class_name);")
1447-
result.append("\tstatic void remove_plugin_class(const StringName &p_class_name);")
1448-
result.append("\tstatic void deinitialize(GDExtensionInitializationLevel p_level);")
1449-
result.append("")
1450-
1451-
result.append("\ttemplate <class T>")
1452-
result.append("\tstatic void add_by_type() {")
1453-
result.append("\t\tadd_plugin_class(T::get_class_static());")
1454-
result.append("\t}")
1455-
1456-
result.append("\ttemplate <class T>")
1457-
result.append("\tstatic void remove_by_type() {")
1458-
result.append("\t\tremove_plugin_class(T::get_class_static());")
1459-
result.append("\t}")
1460-
1461-
result.append("};")
1462-
result.append("")
1463-
14641434
result.append("} // namespace godot")
14651435
result.append("")
14661436

@@ -1685,38 +1655,6 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
16851655
return "\n".join(result)
16861656

16871657

1688-
def generate_register_engine_classes_source(api):
1689-
includes = []
1690-
registrations = []
1691-
1692-
for class_api in api["classes"]:
1693-
if class_api["name"] == "ClassDB":
1694-
continue
1695-
1696-
class_name = class_api["name"]
1697-
snake_class_name = camel_to_snake(class_name)
1698-
1699-
includes.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
1700-
registrations.append(f"\tClassDB::register_engine_class<{class_name}>();")
1701-
1702-
result = []
1703-
add_header(f"register_engine_classes.cpp", result)
1704-
1705-
result.append("#include <godot_cpp/godot.hpp>")
1706-
result.append("")
1707-
result = result + includes
1708-
result.append("")
1709-
result.append("namespace godot {")
1710-
result.append("")
1711-
result.append("void GDExtensionBinding::register_engine_classes() {")
1712-
result = result + registrations
1713-
result.append("}")
1714-
result.append("")
1715-
result.append("} // namespace godot ")
1716-
1717-
return "\n".join(result)
1718-
1719-
17201658
def generate_global_constants(api, output_dir):
17211659
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
17221660
source_gen_folder = Path(output_dir) / "src" / "classes"
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

+17
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ namespace internal {
111111
GDExtensionPropertyInfo *create_c_property_list(const ::godot::List<::godot::PropertyInfo> &plist_cpp, uint32_t *r_size);
112112
void free_c_property_list(GDExtensionPropertyInfo *plist);
113113

114+
typedef void (*EngineClassRegistrationCallback)();
115+
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
116+
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
117+
void register_engine_classes();
118+
119+
template <class T>
120+
struct EngineClassRegistration {
121+
EngineClassRegistration() {
122+
add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
123+
}
124+
125+
static void callback() {
126+
register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
127+
}
128+
};
129+
114130
} // namespace internal
115131

116132
} // namespace godot
@@ -352,6 +368,7 @@ public:
352368
// Don't use this for your classes, use GDCLASS() instead.
353369
#define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) \
354370
private: \
371+
inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
355372
void operator=(const m_class &p_rval) {} \
356373
\
357374
protected: \

include/godot_cpp/core/class_db.hpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ class ClassDB {
119119
static void register_abstract_class();
120120
template <class T>
121121
static void register_internal_class();
122-
template <class T>
123-
static void register_engine_class();
122+
123+
_FORCE_INLINE_ static void _register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
124+
instance_binding_callbacks[p_name] = p_callbacks;
125+
}
124126

125127
template <class N, class M, typename... VarArgs>
126128
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
@@ -233,11 +235,6 @@ void ClassDB::register_internal_class() {
233235
ClassDB::_register_class<T, false>(false, false);
234236
}
235237

236-
template <class T>
237-
void ClassDB::register_engine_class() {
238-
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
239-
}
240-
241238
template <class N, class M, typename... VarArgs>
242239
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
243240
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.

include/godot_cpp/godot.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ enum ModuleInitializationLevel {
196196
};
197197

198198
class GDExtensionBinding {
199-
private:
200-
static void register_engine_classes();
201-
202199
public:
203200
using Callback = void (*)(ModuleInitializationLevel p_level);
204201

src/classes/editor_plugin.cpp src/classes/editor_plugin_registration.cpp

+3-3
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

+25
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 {
@@ -81,6 +85,11 @@ void postinitialize_handler(Wrapped *p_wrapped) {
8185

8286
namespace internal {
8387

88+
std::vector<EngineClassRegistrationCallback> &get_engine_class_registration_callbacks() {
89+
static std::vector<EngineClassRegistrationCallback> engine_class_registration_callbacks;
90+
return engine_class_registration_callbacks;
91+
}
92+
8493
GDExtensionPropertyInfo *create_c_property_list(const ::godot::List<::godot::PropertyInfo> &plist_cpp, uint32_t *r_size) {
8594
GDExtensionPropertyInfo *plist = nullptr;
8695
// Linked list size can be expensive to get so we cache it
@@ -106,6 +115,22 @@ void free_c_property_list(GDExtensionPropertyInfo *plist) {
106115
memfree(plist);
107116
}
108117

118+
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback) {
119+
get_engine_class_registration_callbacks().push_back(p_callback);
120+
}
121+
122+
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
123+
ClassDB::_register_engine_class(p_name, p_callbacks);
124+
}
125+
126+
void register_engine_classes() {
127+
std::vector<EngineClassRegistrationCallback> &engine_class_registration_callbacks = get_engine_class_registration_callbacks();
128+
for (EngineClassRegistrationCallback cb : engine_class_registration_callbacks) {
129+
cb();
130+
}
131+
engine_class_registration_callbacks.clear();
132+
}
133+
109134
} // namespace internal
110135

111136
} // namespace godot

src/godot.cpp

+2-2
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>
@@ -416,7 +416,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
416416
ERR_FAIL_NULL_V_MSG(init_callback, false, "Initialization callback must be defined.");
417417

418418
Variant::init_bindings();
419-
register_engine_classes();
419+
godot::internal::register_engine_classes();
420420

421421
return true;
422422
}

0 commit comments

Comments
 (0)