Skip to content

Commit 65aaceb

Browse files
committed
Add static methods to ClassDB for the methods bound to the ClassDB singleton
1 parent 80986f8 commit 65aaceb

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

binding_generator.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
9797
files.append(str(source_filename.as_posix()))
9898

9999
for engine_class in api["classes"]:
100-
# TODO: Properly setup this singleton since it conflicts with ClassDB in the bindings.
100+
# Generate code for the ClassDB singleton under a different name.
101101
if engine_class["name"] == "ClassDB":
102-
continue
102+
engine_class["name"] = "ClassDBSingleton"
103103
header_filename = include_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".hpp")
104104
source_filename = source_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".cpp")
105105
if headers:
@@ -1036,21 +1036,21 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
10361036

10371037
# First create map of classes and singletons.
10381038
for class_api in api["classes"]:
1039-
# TODO: Properly setup this singleton since it conflicts with ClassDB in the bindings.
1039+
# Generate code for the ClassDB singleton under a different name.
10401040
if class_api["name"] == "ClassDB":
1041-
continue
1041+
class_api["name"] = "ClassDBSingleton"
10421042
engine_classes[class_api["name"]] = class_api["is_refcounted"]
10431043
for native_struct in api["native_structures"]:
10441044
engine_classes[native_struct["name"]] = False
10451045
native_structures.append(native_struct["name"])
10461046

10471047
for singleton in api["singletons"]:
1048+
# Generate code for the ClassDB singleton under a different name.
1049+
if singleton["name"] == "ClassDB":
1050+
singleton["name"] = "ClassDBSingleton"
10481051
singletons.append(singleton["name"])
10491052

10501053
for class_api in api["classes"]:
1051-
# TODO: Properly setup this singleton since it conflicts with ClassDB in the bindings.
1052-
if class_api["name"] == "ClassDB":
1053-
continue
10541054
# Check used classes for header include.
10551055
used_classes = set()
10561056
fully_used_classes = set()
@@ -1242,7 +1242,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
12421242
if len(fully_used_classes) > 0:
12431243
result.append("")
12441244

1245-
if class_name != "Object":
1245+
if class_name != "Object" and class_name != "ClassDBSingleton":
12461246
result.append("#include <godot_cpp/core/class_db.hpp>")
12471247
result.append("")
12481248
result.append("#include <type_traits>")
@@ -1421,6 +1421,47 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
14211421
result.append(f'VARIANT_ENUM_CAST({class_name}::{enum_api["name"]});')
14221422
result.append("")
14231423

1424+
if class_name == "ClassDBSingleton":
1425+
result.append("#define CLASSDB_SINGLETON_FORWARD_METHODS \\")
1426+
for method in class_api["methods"]:
1427+
# ClassDBSingleton shouldn't have any static or vararg methods, but if some appear later, lets skip them.
1428+
if vararg:
1429+
continue
1430+
if "is_static" in method and method["is_static"]:
1431+
continue
1432+
1433+
method_signature = "\tstatic "
1434+
if "return_type" in method:
1435+
method_signature += f'{correct_type(method["return_type"])} '
1436+
else:
1437+
method_signature += "void "
1438+
1439+
method_signature += f'{method["name"]}('
1440+
1441+
method_arguments = []
1442+
if "arguments" in method:
1443+
method_arguments = method["arguments"]
1444+
1445+
method_signature += make_function_parameters(
1446+
method_arguments, include_default=True, for_builtin=True, is_vararg=False
1447+
)
1448+
1449+
method_signature += ") { \\"
1450+
1451+
result.append(method_signature)
1452+
1453+
method_body = "\t\t"
1454+
if "return_type" in method:
1455+
method_body += "return "
1456+
method_body += f'ClassDBSingleton::get_singleton()->{method["name"]}('
1457+
method_body += ", ".join(map(lambda x: escape_identifier(x["name"]), method_arguments))
1458+
method_body += "); \\"
1459+
1460+
result.append(method_body)
1461+
result.append("\t} \\")
1462+
result.append("\t;")
1463+
result.append("")
1464+
14241465
result.append(f"#endif // ! {header_guard}")
14251466

14261467
return "\n".join(result)
@@ -2285,6 +2326,7 @@ def escape_identifier(id):
22852326
"operator": "_operator",
22862327
"typeof": "type_of",
22872328
"typename": "type_name",
2329+
"enum": "_enum",
22882330
}
22892331
if id in cpp_keywords_map:
22902332
return cpp_keywords_map[id]

include/godot_cpp/core/class_db.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <godot_cpp/core/method_bind.hpp>
3939
#include <godot_cpp/core/object.hpp>
4040

41+
#include <godot_cpp/classes/class_db_singleton.hpp>
42+
4143
#include <list>
4244
#include <set>
4345
#include <string>
@@ -146,6 +148,8 @@ class ClassDB {
146148

147149
static void initialize(GDExtensionInitializationLevel p_level);
148150
static void deinitialize(GDExtensionInitializationLevel p_level);
151+
152+
CLASSDB_SINGLETON_FORWARD_METHODS;
149153
};
150154

151155
#define BIND_CONSTANT(m_constant) \

0 commit comments

Comments
 (0)