Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

C#: Fix accessing GodotInternal trampoline getter on generic types

Ignacio Roldán Etcheverryf2b68d7523b9
diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/TrampolineCollectorDispatchGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/TrampolineCollectorDispatchGenerator.cs
index 74ea328b237..ad3d2d5e115 100644
--- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/TrampolineCollectorDispatchGenerator.cs
+++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/TrampolineCollectorDispatchGenerator.cs
@@ -153,11 +153,23 @@ public class TrampolineCollectorDispatchGenerator : ISourceGenerator
}
- source
- .Append(" }\n");
+ source.Append(" }\n");
source.Append(" }\n"); // partial class GodotInternal
+ // Generate GetGodotClassGodotInternalType
+ // When GodotSharp calls `GetNestedType("GodotInternal")`, that method returns a generic type definition,
+ // even though we're calling it on a constructed generic type. This is an AOT-compatible workaround.
+ // This workaround should no longer be needed once assemblies and generated code are trim-compatible.
+ if (symbol.IsGenericType)
+ {
+ source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
+ source.Append(" private new static global::System.Type GetGodotClassGodotInternalType()\n {\n");
+ source.Append(" return typeof(GodotInternal);\n");
+ source.Append(" }\n");
+ source.Append("#pragma warning restore CS0109\n");
+ }
+
source.Append("#pragma warning restore CS0109\n");
source.Append("#pragma warning restore CS0628\n");
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs
index 19f11687643..21d83f1547c 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs
@@ -1095,13 +1095,33 @@ namespace Godot.Bridge
private static void GetGodotClassTrampolinesForType(Type type, TrampolineCollectorPool collectorPool)
{
- var getGodotClassTrampolines = type.GetNestedType("GodotInternal",
+ Type? godotInternalType;
+
+ if (type.IsGenericType)
+ {
+ var getGodotClassGodotInternalType = type.GetMethod("GetGodotClassGodotInternalType",
BindingFlags.DeclaredOnly | BindingFlags.Static |
- BindingFlags.NonPublic | BindingFlags.Public)
- ?.GetMethod(
- "GetGodotClassTrampolines",
+ BindingFlags.NonPublic | BindingFlags.Public);
+
+ if (getGodotClassGodotInternalType == null)
+ return;
+
+ godotInternalType = (Type?)getGodotClassGodotInternalType.Invoke(null, null);
+ }
+ else
+ {
+ godotInternalType = type.GetNestedType("GodotInternal",
BindingFlags.DeclaredOnly | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
+ }
+
+ if (godotInternalType == null)
+ return;
+
+ var getGodotClassTrampolines = godotInternalType.GetMethod(
+ "GetGodotClassTrampolines",
+ BindingFlags.DeclaredOnly | BindingFlags.Static |
+ BindingFlags.NonPublic | BindingFlags.Public);
if (getGodotClassTrampolines == null)
return;