Skip to content

Commit f904387

Browse files
committed
Fix only add "Enum" suffix if necessary, matching what Godot generates
1 parent e9b9b7d commit f904387

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

addons/csharp_gdextension_bindgen/csharp_gdextension_bindgen.gd

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static func _generate_enum(cls_name: StringName, enum_name: StringName) -> Strin
302302
flags = "[Flags]" if ClassDB.is_class_enum_bitfield(cls_name, enum_name) else "",
303303
enum_name = enum_name,
304304
constants = "\n".join(constants).indent("\t"),
305-
maybe_enum_suffix = "" if enum_name.ends_with("Flags") else "Enum",
305+
maybe_enum_suffix = "Enum" if _needs_enum_suffix(cls_name, enum_name) else "",
306306
}).strip_edges()
307307

308308

@@ -316,15 +316,15 @@ static func _generate_integer_constant(cls_name: StringName, constant_name: Stri
316316
static func _generate_property(cls_name: StringName, property: Dictionary) -> String:
317317
var property_name = property["name"]
318318
var csharp_property_name = property_name.to_pascal_case()
319-
var property_type = _get_property_type(property)
319+
var property_type = _get_property_type(cls_name, property)
320320

321321
var getset = PackedStringArray()
322322

323323
var getter = ClassDB.class_get_property_getter(cls_name, property_name)
324324
if getter:
325325
if _is_extension_class(cls_name):
326326
getset.append("get => {get_cast}_object.Get(PropertyName.{csharp_property_name});".format({
327-
get_cast = _property_get_cast(property),
327+
get_cast = _property_get_cast(cls_name, property),
328328
csharp_property_name = csharp_property_name,
329329
}))
330330
else:
@@ -359,15 +359,15 @@ static func _generate_property(cls_name: StringName, property: Dictionary) -> St
359359
static func _generate_method(cls_name: StringName, method: Dictionary) -> String:
360360
var method_name = method["name"]
361361
var csharp_method_name = method_name.to_pascal_case()
362-
var return_type = _get_method_return_type(method_name, method["return"])
362+
var return_type = _get_method_return_type(cls_name, method_name, method["return"])
363363
var is_static = method["flags"] & METHOD_FLAG_STATIC
364364

365365
var arg_types = PackedStringArray()
366366
var arg_names = PackedStringArray()
367367

368368
var args = PackedStringArray()
369369
for argument in method["args"]:
370-
var arg_type = _get_property_type(argument)
370+
var arg_type = _get_property_type(cls_name, argument)
371371
var arg_name = "@" + argument["name"]
372372
# hardcode type that cannot be known from reflection in GDScript
373373
if method["name"] == "connect" and arg_name == "@flags":
@@ -453,12 +453,12 @@ static func _generate_method(cls_name: StringName, method: Dictionary) -> String
453453
if is_static:
454454
implementation.append("{maybe_return}ClassDB.ClassCallStatic(NativeName, {arg_names});".format({
455455
arg_names = ", ".join(arg_names),
456-
maybe_return = "return " + _property_get_cast(method["return"]) if return_type != "void" else "",
456+
maybe_return = "return " + _property_get_cast(cls_name, method["return"]) if return_type != "void" else "",
457457
}))
458458
else:
459459
implementation.append("{maybe_return}_object.Call({arg_names});".format({
460460
arg_names = ", ".join(arg_names),
461-
maybe_return = "return " + _property_get_cast(method["return"]) if return_type != "void" else "",
461+
maybe_return = "return " + _property_get_cast(cls_name, method["return"]) if return_type != "void" else "",
462462
}))
463463
else:
464464
if is_static:
@@ -493,11 +493,11 @@ static func _generate_method(cls_name: StringName, method: Dictionary) -> String
493493
static func _generate_signal(cls_name: StringName, sig: Dictionary):
494494
var signal_name = sig["name"]
495495
var csharp_signal_name = signal_name.to_pascal_case()
496-
var return_type = _get_method_return_type(signal_name, sig["return"])
496+
var return_type = _get_method_return_type(cls_name, signal_name, sig["return"])
497497

498498
var arg_types = PackedStringArray()
499499
for argument in sig["args"]:
500-
var arg_type = _get_property_type(argument)
500+
var arg_type = _get_property_type(cls_name, argument)
501501
arg_types.append(arg_type)
502502

503503
var delegate_type
@@ -536,7 +536,7 @@ static func _property_is_enum(property: Dictionary) -> bool:
536536
return property["usage"] & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)
537537

538538

539-
static func _get_property_type(property: Dictionary) -> String:
539+
static func _get_property_type(cls_name: StringName, property: Dictionary) -> String:
540540
match property["type"]:
541541
TYPE_NIL:
542542
return "Variant"
@@ -547,7 +547,11 @@ static func _get_property_type(property: Dictionary) -> String:
547547
var enum_name = property["class_name"]
548548
if enum_name == "Error":
549549
return "Godot.Error"
550-
return enum_name + "Enum"
550+
var split = enum_name.split(".")
551+
if split.size() == 1:
552+
return enum_name + ("Enum" if _needs_enum_suffix(cls_name, enum_name) else "")
553+
else:
554+
return enum_name + ("Enum" if _needs_enum_suffix(split[0], split[1]) else "")
551555
return "int"
552556
TYPE_FLOAT:
553557
return "double" if OS.has_feature("double") else "float"
@@ -632,8 +636,8 @@ static func _get_mapped_variant_type(variant_type_name: String) -> String:
632636
return _type_map.get(variant_type_name, "Godot." + variant_type_name)
633637

634638

635-
static func _property_get_cast(property: Dictionary):
636-
var property_type = _get_property_type(property)
639+
static func _property_get_cast(cls_name: StringName, property: Dictionary):
640+
var property_type = _get_property_type(cls_name, property)
637641
if _property_is_enum(property):
638642
return "(%s)(int)" % property_type
639643
else:
@@ -664,7 +668,7 @@ static func _first_non_extension_parent(cls_name: StringName) -> StringName:
664668
return cls_name
665669

666670

667-
static func _get_method_return_type(method_name: StringName, method_return: Dictionary) -> String:
671+
static func _get_method_return_type(cls_name: StringName, method_name: StringName, method_return: Dictionary) -> String:
668672
# hardcode type that cannot be known from reflection in GDScript
669673
if method_name == "get_instance_id":
670674
return "ulong"
@@ -675,7 +679,7 @@ static func _get_method_return_type(method_name: StringName, method_return: Dict
675679
else:
676680
return "void"
677681
else:
678-
return _get_property_type(method_return)
682+
return _get_property_type(cls_name, method_return)
679683

680684

681685
static func _get_parent_classes(cls_name: StringName) -> Array[StringName]:
@@ -735,3 +739,16 @@ static func _get_class_from_class_name(cls_name: String) -> String:
735739
while not ClassDB.is_parent_class(test_cls, parent_classes[0]):
736740
parent_classes.pop_front()
737741
return parent_classes[0]
742+
743+
744+
static func _needs_enum_suffix(cls_name: StringName, enum_name: String) -> bool:
745+
var snake_case_enum_name = enum_name.to_snake_case()
746+
if ClassDB.class_has_method(cls_name, snake_case_enum_name):
747+
return true
748+
if ClassDB.class_has_signal(cls_name, snake_case_enum_name):
749+
return true
750+
var properties = ClassDB.class_get_property_list(cls_name)
751+
for property in properties:
752+
if snake_case_enum_name == property["name"]:
753+
return true
754+
return false

0 commit comments

Comments
 (0)