@@ -178,6 +178,10 @@ def generate_bindings(api_filepath, use_template_get_node, bits="64", precision=
178
178
generate_utility_functions (api , target_dir )
179
179
180
180
181
+ CLASS_ALIASES = {
182
+ "ClassDB" : "ClassDBSingleton" ,
183
+ }
184
+
181
185
builtin_classes = []
182
186
183
187
# Key is class name, value is boolean where True means the class is refcounted.
@@ -1079,19 +1083,19 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
1079
1083
# First create map of classes and singletons.
1080
1084
for class_api in api ["classes" ]:
1081
1085
# Generate code for the ClassDB singleton under a different name.
1082
- if class_api ["name" ] == "ClassDB" :
1083
- class_api ["name " ] = "ClassDBSingleton"
1084
- class_api ["alias_for " ] = "ClassDB"
1086
+ if class_api ["name" ] in CLASS_ALIASES :
1087
+ class_api ["alias_for " ] = class_api [ "name" ]
1088
+ class_api ["name " ] = CLASS_ALIASES [ class_api [ "alias_for" ]]
1085
1089
engine_classes [class_api ["name" ]] = class_api ["is_refcounted" ]
1086
1090
for native_struct in api ["native_structures" ]:
1087
1091
engine_classes [native_struct ["name" ]] = False
1088
1092
native_structures .append (native_struct ["name" ])
1089
1093
1090
1094
for singleton in api ["singletons" ]:
1091
1095
# Generate code for the ClassDB singleton under a different name.
1092
- if singleton ["name" ] == "ClassDB" :
1093
- singleton ["name " ] = "ClassDBSingleton"
1094
- singleton ["alias_for " ] = "ClassDB"
1096
+ if singleton ["name" ] in CLASS_ALIASES :
1097
+ singleton ["alias_for " ] = singleton [ "name" ]
1098
+ singleton ["name " ] = CLASS_ALIASES [ singleton [ "name" ]]
1095
1099
singletons .append (singleton ["name" ])
1096
1100
1097
1101
for class_api in api ["classes" ]:
@@ -1294,6 +1298,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1294
1298
result .append ("#include <type_traits>" )
1295
1299
result .append ("" )
1296
1300
1301
+ if class_name == "ClassDBSingleton" :
1302
+ result .append ("#include <godot_cpp/core/binder_common.hpp>" )
1303
+ result .append ("" )
1304
+
1297
1305
result .append ("namespace godot {" )
1298
1306
result .append ("" )
1299
1307
@@ -1448,6 +1456,19 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1448
1456
1449
1457
if class_name == "ClassDBSingleton" :
1450
1458
result .append ("#define CLASSDB_SINGLETON_FORWARD_METHODS \\ " )
1459
+
1460
+ if "enums" in class_api :
1461
+ for enum_api in class_api ["enums" ]:
1462
+ if enum_api ["is_bitfield" ]:
1463
+ result .append (f'\t enum { enum_api ["name" ]} : uint64_t {{ \\ ' )
1464
+ else :
1465
+ result .append (f'\t enum { enum_api ["name" ]} {{ \\ ' )
1466
+
1467
+ for value in enum_api ["values" ]:
1468
+ result .append (f'\t \t { value ["name" ]} = { value ["value" ]} , \\ ' )
1469
+ result .append ("\t }; \\ " )
1470
+ result .append ("\t \\ " )
1471
+
1451
1472
for method in class_api ["methods" ]:
1452
1473
# ClassDBSingleton shouldn't have any static or vararg methods, but if some appear later, lets skip them.
1453
1474
if vararg :
@@ -1456,12 +1477,17 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1456
1477
continue
1457
1478
1458
1479
method_signature = "\t static "
1480
+ return_type = None
1459
1481
if "return_type" in method :
1460
- method_signature += f' { correct_type (method ["return_type" ]) } '
1482
+ return_type = correct_type (method ["return_type" ]. replace ( "ClassDBSingleton" , "ClassDB" ), None , False )
1461
1483
elif "return_value" in method :
1462
- method_signature += (
1463
- correct_type (method ["return_value" ]["type" ], method ["return_value" ].get ("meta" , None )) + " "
1484
+ return_type = correct_type (
1485
+ method ["return_value" ]["type" ].replace ("ClassDBSingleton" , "ClassDB" ),
1486
+ method ["return_value" ].get ("meta" , None ),
1487
+ False ,
1464
1488
)
1489
+ if return_type is not None :
1490
+ method_signature += return_type + " "
1465
1491
else :
1466
1492
method_signature += "void "
1467
1493
@@ -1480,8 +1506,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1480
1506
result .append (method_signature )
1481
1507
1482
1508
method_body = "\t \t "
1483
- if " return_type" in method or "return_value" in method :
1509
+ if return_type is not None :
1484
1510
method_body += "return "
1511
+ if "alias_for" in class_api and return_type .startswith (class_api ["alias_for" ] + "::" ):
1512
+ method_body += f"({ return_type } )"
1485
1513
method_body += f'ClassDBSingleton::get_singleton()->{ method ["name" ]} ('
1486
1514
method_body += ", " .join (map (lambda x : escape_identifier (x ["name" ]), method_arguments ))
1487
1515
method_body += "); \\ "
@@ -1491,6 +1519,18 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1491
1519
result .append ("\t ;" )
1492
1520
result .append ("" )
1493
1521
1522
+ result .append ("#define CLASSDB_SINGLETON_VARIANT_CAST \\ " )
1523
+
1524
+ if "enums" in class_api :
1525
+ for enum_api in class_api ["enums" ]:
1526
+ if enum_api ["is_bitfield" ]:
1527
+ result .append (f'\t VARIANT_BITFIELD_CAST({ class_api ["alias_for" ]} ::{ enum_api ["name" ]} ); \\ ' )
1528
+ else :
1529
+ result .append (f'\t VARIANT_ENUM_CAST({ class_api ["alias_for" ]} ::{ enum_api ["name" ]} ); \\ ' )
1530
+
1531
+ result .append ("\t ;" )
1532
+ result .append ("" )
1533
+
1494
1534
result .append (f"#endif // ! { header_guard } " )
1495
1535
1496
1536
return "\n " .join (result )
@@ -2285,7 +2325,7 @@ def correct_typed_array(type_name):
2285
2325
return type_name
2286
2326
2287
2327
2288
- def correct_type (type_name , meta = None ):
2328
+ def correct_type (type_name , meta = None , use_alias = True ):
2289
2329
type_conversion = {"float" : "double" , "int" : "int64_t" , "Nil" : "Variant" }
2290
2330
if meta != None :
2291
2331
if "int" in meta :
@@ -2301,11 +2341,15 @@ def correct_type(type_name, meta=None):
2301
2341
if is_enum (type_name ):
2302
2342
if is_bitfield (type_name ):
2303
2343
base_class = get_enum_class (type_name )
2344
+ if use_alias and base_class in CLASS_ALIASES :
2345
+ base_class = CLASS_ALIASES [base_class ]
2304
2346
if base_class == "GlobalConstants" :
2305
2347
return f"BitField<{ get_enum_name (type_name )} >"
2306
2348
return f"BitField<{ base_class } ::{ get_enum_name (type_name )} >"
2307
2349
else :
2308
2350
base_class = get_enum_class (type_name )
2351
+ if use_alias and base_class in CLASS_ALIASES :
2352
+ base_class = CLASS_ALIASES [base_class ]
2309
2353
if base_class == "GlobalConstants" :
2310
2354
return f"{ get_enum_name (type_name )} "
2311
2355
return f"{ base_class } ::{ get_enum_name (type_name )} "
0 commit comments