Skip to content

Commit

Permalink
Register classes with the correct base class name
Browse files Browse the repository at this point in the history
The base class is supposed to be the actual base type (including custom types).
  • Loading branch information
raulsntos committed Jun 9, 2024
1 parent e341d55 commit 998dac1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Godot.Bindings/Bridge/ClassDB/ClassDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,26 @@ private unsafe static void RegisterClassCore<T>(bool isVirtual, bool isAbstract,
class_userdata = (void*)GCHandle.ToIntPtr(context.GCHandle),
};

StringName? baseClassName = GodotObject.GetGodotNativeName(typeof(T));
StringName? godotNativeName = GodotObject.GetGodotNativeName(typeof(T));

// The 'baseType' will never be null becase T has a constraint that
// The 'BaseType' will never be null becase T has a constraint that
// it must derive from GodotObject, but we assert this anyway so the
// null analysis doesn't complain about it being null.
Debug.Assert(baseClassName is not null, $"Type '{typeof(T)}' must derive from a Godot type.");
Debug.Assert(godotNativeName is not null, $"Type '{typeof(T)}' must derive from a Godot type.");

StringName baseClassName;
if (typeof(T).BaseType?.Assembly != typeof(GodotObject).Assembly)
{
// If the base type is not a built-in Godot type,
// construct the name from the type name.
baseClassName = new StringName(typeof(T).BaseType!.Name);
}
else
{
// Otherwise, use the retrieved Godot native name
// which may be different from the type name.
baseClassName = godotNativeName;
}

NativeGodotStringName classNameNative = className.NativeValue.DangerousSelfRef;
NativeGodotStringName baseClassNameNative = baseClassName.NativeValue.DangerousSelfRef;
Expand All @@ -136,7 +150,7 @@ private unsafe static void RegisterClassCore<T>(bool isVirtual, bool isAbstract,

configure(context);

if (InteropUtils.RegisterVirtualOverridesHelpers.TryGetValue(baseClassName, out var registerVirtualOverrides))
if (InteropUtils.RegisterVirtualOverridesHelpers.TryGetValue(godotNativeName, out var registerVirtualOverrides))
{
registerVirtualOverrides(context);
}
Expand Down

0 comments on commit 998dac1

Please sign in to comment.