Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override default behaviour of methods if not explicitly overridden #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/Godot.Bindings/Bridge/ClassDB/ClassDB.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Godot.NativeInterop;
Expand All @@ -23,7 +24,7 @@ public static class ClassDB
/// </summary>
/// <typeparam name="T">The type of the class.</typeparam>
/// <param name="configure">The configuration function.</param>
public static void RegisterClass<T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
public static void RegisterClass<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
RegisterClassCore<T>(isVirtual: false, isAbstract: false, isExposed: true, isRuntime: false, configure);
}
Expand All @@ -35,7 +36,7 @@ public static void RegisterClass<T>(Action<ClassDBRegistrationContext> configure
/// </summary>
/// <typeparam name="T">The type of the class.</typeparam>
/// <param name="configure">The configuration function.</param>
public static void RegisterRuntimeClass<T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
public static void RegisterRuntimeClass<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
RegisterClassCore<T>(isVirtual: false, isAbstract: false, isExposed: true, isRuntime: true, configure);
}
Expand All @@ -47,7 +48,7 @@ public static void RegisterRuntimeClass<T>(Action<ClassDBRegistrationContext> co
/// </summary>
/// <typeparam name="T">The type of the class.</typeparam>
/// <param name="configure">The configuration function.</param>
public static void RegisterVirtualClass<T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
public static void RegisterVirtualClass<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
RegisterClassCore<T>(isVirtual: true, isAbstract: false, isExposed: true, isRuntime: false, configure);
}
Expand All @@ -59,7 +60,7 @@ public static void RegisterVirtualClass<T>(Action<ClassDBRegistrationContext> co
/// </summary>
/// <typeparam name="T">The type of the class.</typeparam>
/// <param name="configure">The configuration function.</param>
public static void RegisterAbstractClass<T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
public static void RegisterAbstractClass<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
RegisterClassCore<T>(isVirtual: false, isAbstract: true, isExposed: true, isRuntime: false, configure);
}
Expand All @@ -70,12 +71,12 @@ public static void RegisterAbstractClass<T>(Action<ClassDBRegistrationContext> c
/// </summary>
/// <typeparam name="T">The type of the class.</typeparam>
/// <param name="configure">The configuration function.</param>
public static void RegisterInternalClass<T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
public static void RegisterInternalClass<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
RegisterClassCore<T>(isVirtual: false, isAbstract: false, isExposed: false, isRuntime: false, configure);
}

private unsafe static void RegisterClassCore<T>(bool isVirtual, bool isAbstract, bool isExposed, bool isRuntime, Action<ClassDBRegistrationContext> configure) where T : GodotObject
private unsafe static void RegisterClassCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicMethods)] T>(bool isVirtual, bool isAbstract, bool isExposed, bool isRuntime, Action<ClassDBRegistrationContext> configure) where T : GodotObject
{
if (typeof(T).IsAbstract && !isAbstract)
{
Expand Down Expand Up @@ -152,7 +153,7 @@ private unsafe static void RegisterClassCore<T>(bool isVirtual, bool isAbstract,

if (InteropUtils.RegisterVirtualOverridesHelpers.TryGetValue(godotNativeName, out var registerVirtualOverrides))
{
registerVirtualOverrides(context);
registerVirtualOverrides(typeof(T), context);
}
}

Expand Down Expand Up @@ -412,7 +413,9 @@ private unsafe static void Free_Native(void* userData, void* instance)

if (!context.RegisteredVirtualMethodOverrides.TryGetValue(methodNameStr, out var virtualMethodInfo))
{
throw new InvalidOperationException($"Virtual method '{methodNameStr}' has not been registered in class '{context.ClassName}'.");
// Virtual method not registered, it likely means it wasn't overridden.
// Returning null so it falls back to the default implementation.
return null;
Beliar83 marked this conversation as resolved.
Show resolved Hide resolved
}

return (delegate* unmanaged[Cdecl]<void*, void**, void*, void>)Marshal.GetFunctionPointerForDelegate(virtualMethodInfo.Invoker.CallVirtualWithPtrArgs);
Expand Down
4 changes: 3 additions & 1 deletion src/Godot.Bindings/NativeInterop/InteropUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;
using Godot.Bridge;

namespace Godot.NativeInterop;
Expand All @@ -8,7 +9,8 @@ internal static partial class InteropUtils
{
internal static FrozenDictionary<StringName, Func<nint, GodotObject>> CreateHelpers { get; private set; }

internal static FrozenDictionary<StringName, Action<ClassDBRegistrationContext>> RegisterVirtualOverridesHelpers { get; private set; }
internal delegate void RegisterVirtualOverrideHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type type, ClassDBRegistrationContext context);
internal static FrozenDictionary<StringName, RegisterVirtualOverrideHelper> RegisterVirtualOverridesHelpers { get; private set; }

static InteropUtils()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override void Populate(BindingsData.CollectionContext context)
Body = MethodBody.Create(writer =>
{
writer.WriteLine($"var createHelpers = new global::System.Collections.Generic.Dictionary<StringName, global::System.Func<nint, global::Godot.GodotObject>>(capacity: {context.Api.Classes.Length});");
writer.WriteLine($"var registerVirtualOverridesHelpers = new global::System.Collections.Generic.Dictionary<StringName, global::System.Action<global::Godot.Bridge.ClassDBRegistrationContext>>(capacity: {context.Api.Classes.Length});");
writer.WriteLine($"var registerVirtualOverridesHelpers = new global::System.Collections.Generic.Dictionary<StringName, RegisterVirtualOverrideHelper>(capacity: {context.Api.Classes.Length});");

foreach (var engineClass in context.Api.Classes)
{
Expand Down Expand Up @@ -303,6 +303,7 @@ private void PopulateEngineClassMethods(BindingsData.CollectionContext context,
IsNew = engineClass.Name != "Object",
Parameters =
{
new ParameterInfo("type", new TypeInfo("Type", "System")) { Attributes = ["[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods)]"]},
new ParameterInfo("context", new TypeInfo("ClassDBRegistrationContext", "Godot.Bridge")),
},
Body = new RegisterVirtualOverrides(type, virtualMethods),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static void GenerateCore(GodotApi api, string outputDirectoryPath, Bindi
using var streamWriter = new StreamWriter(stream);
using var writer = new IndentedTextWriter(streamWriter);


writer.WriteLine($"namespace {type.Namespace};");
writer.WriteLine();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using Godot.BindingsGenerator.ApiDump;
using Godot.BindingsGenerator.Reflection;

Expand All @@ -23,10 +24,27 @@ public override void Write(MethodBase owner, IndentedTextWriter writer)
{
if (_type.BaseType is not null)
{
writer.WriteLine($"{_type.BaseType.FullNameWithGlobal}.RegisterVirtualOverrides(context);");
writer.WriteLine($"{_type.BaseType.FullNameWithGlobal}.RegisterVirtualOverrides(type, context);");
}
foreach (var (method, engineMethod) in _virtualMethods)
{
string types = string.Join(", ", method.Parameters.Select(p => $"typeof({p.Type.FullNameWithGlobal})"));
writer.OpenBlock();

writer.WriteLine($"global::System.Reflection.MethodInfo? methodInfo = type.GetMethod(nameof({_type.Name}.{method.Name}), global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.NonPublic, [{types}]);");
writer.WriteLine("var isOverride = false;");
writer.WriteLine("if (methodInfo is not null)");
writer.OpenBlock();
writer.WriteLine($"bool isMethodDeclaredInDerivedType = methodInfo.DeclaringType != typeof({_type.Name});");
writer.WriteLine("global::System.Reflection.MethodInfo baseDefinition = methodInfo.GetBaseDefinition();");
writer.WriteLine("bool isMethodDeclaredAsOverride = baseDefinition != methodInfo;");
writer.WriteLine($"bool isBaseDefinitionFromCorrectBaseType = baseDefinition.DeclaringType == typeof({_type.Name});");
writer.WriteLine("isOverride = isMethodDeclaredInDerivedType && isMethodDeclaredAsOverride && isBaseDefinitionFromCorrectBaseType;");
writer.CloseBlock();
writer.WriteLine("");
writer.WriteLine("if (isOverride)");
writer.OpenBlock();

writer.Write($"context.BindVirtualMethodOverride(MethodName.{method.Name}, ");
writer.Write($"static ({_type.FullNameWithGlobal} __instance");
if (method.Parameters.Count > 0)
Expand Down Expand Up @@ -84,6 +102,8 @@ public override void Write(MethodBase owner, IndentedTextWriter writer)

writer.Indent--;
writer.WriteLine("});");
writer.CloseBlock();
writer.CloseBlock();
}
}
}