@@ -309,6 +309,10 @@ def generate_bindings(api_filepath, use_template_get_node, bits="64", precision=
309
309
generate_utility_functions (api , target_dir )
310
310
311
311
312
+ CLASS_ALIASES = {
313
+ "ClassDB" : "ClassDBSingleton" ,
314
+ }
315
+
312
316
builtin_classes = []
313
317
314
318
# Key is class name, value is boolean where True means the class is refcounted.
@@ -1256,9 +1260,9 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
1256
1260
# First create map of classes and singletons.
1257
1261
for class_api in api ["classes" ]:
1258
1262
# Generate code for the ClassDB singleton under a different name.
1259
- if class_api ["name" ] == "ClassDB" :
1260
- class_api ["name " ] = "ClassDBSingleton"
1261
- class_api ["alias_for " ] = "ClassDB"
1263
+ if class_api ["name" ] in CLASS_ALIASES :
1264
+ class_api ["alias_for " ] = class_api [ "name" ]
1265
+ class_api ["name " ] = CLASS_ALIASES [ class_api [ "alias_for" ]]
1262
1266
engine_classes [class_api ["name" ]] = class_api ["is_refcounted" ]
1263
1267
for native_struct in api ["native_structures" ]:
1264
1268
if native_struct ["name" ] == "ObjectID" :
@@ -1268,9 +1272,9 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
1268
1272
1269
1273
for singleton in api ["singletons" ]:
1270
1274
# Generate code for the ClassDB singleton under a different name.
1271
- if singleton ["name" ] == "ClassDB" :
1272
- singleton ["name " ] = "ClassDBSingleton"
1273
- singleton ["alias_for " ] = "ClassDB"
1275
+ if singleton ["name" ] in CLASS_ALIASES :
1276
+ singleton ["alias_for " ] = singleton [ "name" ]
1277
+ singleton ["name " ] = CLASS_ALIASES [ singleton [ "name" ]]
1274
1278
singletons .append (singleton ["name" ])
1275
1279
1276
1280
for class_api in api ["classes" ]:
@@ -1475,6 +1479,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1475
1479
result .append ("#include <type_traits>" )
1476
1480
result .append ("" )
1477
1481
1482
+ if class_name == "ClassDBSingleton" :
1483
+ result .append ("#include <godot_cpp/core/binder_common.hpp>" )
1484
+ result .append ("" )
1485
+
1478
1486
result .append ("namespace godot {" )
1479
1487
result .append ("" )
1480
1488
@@ -1633,6 +1641,19 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1633
1641
1634
1642
if class_name == "ClassDBSingleton" :
1635
1643
result .append ("#define CLASSDB_SINGLETON_FORWARD_METHODS \\ " )
1644
+
1645
+ if "enums" in class_api :
1646
+ for enum_api in class_api ["enums" ]:
1647
+ if enum_api ["is_bitfield" ]:
1648
+ result .append (f'\t enum { enum_api ["name" ]} : uint64_t {{ \\ ' )
1649
+ else :
1650
+ result .append (f'\t enum { enum_api ["name" ]} {{ \\ ' )
1651
+
1652
+ for value in enum_api ["values" ]:
1653
+ result .append (f'\t \t { value ["name" ]} = { value ["value" ]} , \\ ' )
1654
+ result .append ("\t }; \\ " )
1655
+ result .append ("\t \\ " )
1656
+
1636
1657
for method in class_api ["methods" ]:
1637
1658
# ClassDBSingleton shouldn't have any static or vararg methods, but if some appear later, lets skip them.
1638
1659
if vararg :
@@ -1641,12 +1662,17 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1641
1662
continue
1642
1663
1643
1664
method_signature = "\t static "
1665
+ return_type = None
1644
1666
if "return_type" in method :
1645
- method_signature += f' { correct_type (method ["return_type" ]) } '
1667
+ return_type = correct_type (method ["return_type" ]. replace ( "ClassDBSingleton" , "ClassDB" ), None , False )
1646
1668
elif "return_value" in method :
1647
- method_signature += (
1648
- correct_type (method ["return_value" ]["type" ], method ["return_value" ].get ("meta" , None )) + " "
1669
+ return_type = correct_type (
1670
+ method ["return_value" ]["type" ].replace ("ClassDBSingleton" , "ClassDB" ),
1671
+ method ["return_value" ].get ("meta" , None ),
1672
+ False ,
1649
1673
)
1674
+ if return_type is not None :
1675
+ method_signature += return_type + " "
1650
1676
else :
1651
1677
method_signature += "void "
1652
1678
@@ -1665,8 +1691,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1665
1691
result .append (method_signature )
1666
1692
1667
1693
method_body = "\t \t "
1668
- if " return_type" in method or "return_value" in method :
1694
+ if return_type is not None :
1669
1695
method_body += "return "
1696
+ if "alias_for" in class_api and return_type .startswith (class_api ["alias_for" ] + "::" ):
1697
+ method_body += f"({ return_type } )"
1670
1698
method_body += f'ClassDBSingleton::get_singleton()->{ method ["name" ]} ('
1671
1699
method_body += ", " .join (map (lambda x : escape_identifier (x ["name" ]), method_arguments ))
1672
1700
method_body += "); \\ "
@@ -1676,6 +1704,18 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
1676
1704
result .append ("\t ;" )
1677
1705
result .append ("" )
1678
1706
1707
+ result .append ("#define CLASSDB_SINGLETON_VARIANT_CAST \\ " )
1708
+
1709
+ if "enums" in class_api :
1710
+ for enum_api in class_api ["enums" ]:
1711
+ if enum_api ["is_bitfield" ]:
1712
+ result .append (f'\t VARIANT_BITFIELD_CAST({ class_api ["alias_for" ]} ::{ enum_api ["name" ]} ); \\ ' )
1713
+ else :
1714
+ result .append (f'\t VARIANT_ENUM_CAST({ class_api ["alias_for" ]} ::{ enum_api ["name" ]} ); \\ ' )
1715
+
1716
+ result .append ("\t ;" )
1717
+ result .append ("" )
1718
+
1679
1719
result .append (f"#endif // ! { header_guard } " )
1680
1720
1681
1721
return "\n " .join (result )
@@ -2513,7 +2553,7 @@ def correct_typed_array(type_name):
2513
2553
return type_name
2514
2554
2515
2555
2516
- def correct_type (type_name , meta = None ):
2556
+ def correct_type (type_name , meta = None , use_alias = True ):
2517
2557
type_conversion = {"float" : "double" , "int" : "int64_t" , "Nil" : "Variant" }
2518
2558
if meta != None :
2519
2559
if "int" in meta :
@@ -2529,11 +2569,15 @@ def correct_type(type_name, meta=None):
2529
2569
if is_enum (type_name ):
2530
2570
if is_bitfield (type_name ):
2531
2571
base_class = get_enum_class (type_name )
2572
+ if use_alias and base_class in CLASS_ALIASES :
2573
+ base_class = CLASS_ALIASES [base_class ]
2532
2574
if base_class == "GlobalConstants" :
2533
2575
return f"BitField<{ get_enum_name (type_name )} >"
2534
2576
return f"BitField<{ base_class } ::{ get_enum_name (type_name )} >"
2535
2577
else :
2536
2578
base_class = get_enum_class (type_name )
2579
+ if use_alias and base_class in CLASS_ALIASES :
2580
+ base_class = CLASS_ALIASES [base_class ]
2537
2581
if base_class == "GlobalConstants" :
2538
2582
return f"{ get_enum_name (type_name )} "
2539
2583
return f"{ base_class } ::{ get_enum_name (type_name )} "
0 commit comments