Преглед изворни кода

Merge pull request #78349 from raulsntos/dotnet/fix-method-info

C#: Set `PropertyInfo.class_name` for method parameters
Rémi Verschelde пре 2 година
родитељ
комит
19e0135acb

+ 6 - 0
modules/mono/csharp_script.cpp

@@ -2320,6 +2320,9 @@ void CSharpScript::update_script_class_info(Ref<CSharpScript> p_script) {
 			Variant::Type param_type = (Variant::Type)(int)param["type"];
 			PropertyInfo arg_info = PropertyInfo(param_type, (String)param["name"]);
 			arg_info.usage = (uint32_t)param["usage"];
+			if (param.has("class_name")) {
+				arg_info.class_name = (StringName)param["class_name"];
+			}
 			mi.arguments.push_back(arg_info);
 		}
 
@@ -2350,6 +2353,9 @@ void CSharpScript::update_script_class_info(Ref<CSharpScript> p_script) {
 			Variant::Type param_type = (Variant::Type)(int)param["type"];
 			PropertyInfo arg_info = PropertyInfo(param_type, (String)param["name"]);
 			arg_info.usage = (uint32_t)param["usage"];
+			if (param.has("class_name")) {
+				arg_info.class_name = (StringName)param["class_name"];
+			}
 			mi.arguments.push_back(arg_info);
 		}
 

+ 6 - 0
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/PropertyInfo.cs

@@ -4,12 +4,17 @@ namespace Godot.SourceGenerators
     {
         public PropertyInfo(VariantType type, string name, PropertyHint hint,
             string? hintString, PropertyUsageFlags usage, bool exported)
+            : this(type, name, hint, hintString, usage, className: null, exported) { }
+
+        public PropertyInfo(VariantType type, string name, PropertyHint hint,
+            string? hintString, PropertyUsageFlags usage, string? className, bool exported)
         {
             Type = type;
             Name = name;
             Hint = hint;
             HintString = hintString;
             Usage = usage;
+            ClassName = className;
             Exported = exported;
         }
 
@@ -18,6 +23,7 @@ namespace Godot.SourceGenerators
         public PropertyHint Hint { get; }
         public string? HintString { get; }
         public PropertyUsageFlags Usage { get; }
+        public string? ClassName { get; }
         public bool Exported { get; }
     }
 }

+ 20 - 5
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs

@@ -288,8 +288,14 @@ namespace Godot.SourceGenerators
                 .Append("\", usage: (global::Godot.PropertyUsageFlags)")
                 .Append((int)propertyInfo.Usage)
                 .Append(", exported: ")
-                .Append(propertyInfo.Exported ? "true" : "false")
-                .Append(")");
+                .Append(propertyInfo.Exported ? "true" : "false");
+            if (propertyInfo.ClassName != null)
+            {
+                source.Append(", className: new global::Godot.StringName(\"")
+                    .Append(propertyInfo.ClassName)
+                    .Append("\")");
+            }
+            source.Append(")");
         }
 
         private static MethodInfo DetermineMethodInfo(GodotMethodData method)
@@ -298,7 +304,9 @@ namespace Godot.SourceGenerators
 
             if (method.RetType != null)
             {
-                returnVal = DeterminePropertyInfo(method.RetType.Value.MarshalType, name: string.Empty);
+                returnVal = DeterminePropertyInfo(method.RetType.Value.MarshalType,
+                    method.RetType.Value.TypeSymbol,
+                    name: string.Empty);
             }
             else
             {
@@ -317,6 +325,7 @@ namespace Godot.SourceGenerators
                 for (int i = 0; i < paramCount; i++)
                 {
                     arguments.Add(DeterminePropertyInfo(method.ParamTypes[i],
+                        method.Method.Parameters[i].Type,
                         name: method.Method.Parameters[i].Name));
                 }
             }
@@ -329,7 +338,7 @@ namespace Godot.SourceGenerators
                 defaultArguments: null);
         }
 
-        private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, string name)
+        private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, ITypeSymbol typeSymbol, string name)
         {
             var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value;
 
@@ -338,8 +347,14 @@ namespace Godot.SourceGenerators
             if (memberVariantType == VariantType.Nil)
                 propUsage |= PropertyUsageFlags.NilIsVariant;
 
+            string? className = null;
+            if (memberVariantType == VariantType.Object && typeSymbol is INamedTypeSymbol namedTypeSymbol)
+            {
+                className = namedTypeSymbol.GetGodotScriptNativeClassName();
+            }
+
             return new PropertyInfo(memberVariantType, name,
-                PropertyHint.None, string.Empty, propUsage, exported: false);
+                PropertyHint.None, string.Empty, propUsage, className, exported: false);
         }
 
         private static void GenerateHasMethodEntry(

+ 20 - 5
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs

@@ -360,8 +360,14 @@ namespace Godot.SourceGenerators
                 .Append("\", usage: (global::Godot.PropertyUsageFlags)")
                 .Append((int)propertyInfo.Usage)
                 .Append(", exported: ")
-                .Append(propertyInfo.Exported ? "true" : "false")
-                .Append(")");
+                .Append(propertyInfo.Exported ? "true" : "false");
+            if (propertyInfo.ClassName != null)
+            {
+                source.Append(", className: new global::Godot.StringName(\"")
+                    .Append(propertyInfo.ClassName)
+                    .Append("\")");
+            }
+            source.Append(")");
         }
 
         private static MethodInfo DetermineMethodInfo(GodotSignalDelegateData signalDelegateData)
@@ -372,7 +378,9 @@ namespace Godot.SourceGenerators
 
             if (invokeMethodData.RetType != null)
             {
-                returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value.MarshalType, name: string.Empty);
+                returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value.MarshalType,
+                    invokeMethodData.RetType.Value.TypeSymbol,
+                    name: string.Empty);
             }
             else
             {
@@ -391,6 +399,7 @@ namespace Godot.SourceGenerators
                 for (int i = 0; i < paramCount; i++)
                 {
                     arguments.Add(DeterminePropertyInfo(invokeMethodData.ParamTypes[i],
+                        invokeMethodData.Method.Parameters[i].Type,
                         name: invokeMethodData.Method.Parameters[i].Name));
                 }
             }
@@ -403,7 +412,7 @@ namespace Godot.SourceGenerators
                 defaultArguments: null);
         }
 
-        private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, string name)
+        private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, ITypeSymbol typeSymbol, string name)
         {
             var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value;
 
@@ -412,8 +421,14 @@ namespace Godot.SourceGenerators
             if (memberVariantType == VariantType.Nil)
                 propUsage |= PropertyUsageFlags.NilIsVariant;
 
+            string? className = null;
+            if (memberVariantType == VariantType.Object && typeSymbol is INamedTypeSymbol namedTypeSymbol)
+            {
+                className = namedTypeSymbol.GetGodotScriptNativeClassName();
+            }
+
             return new PropertyInfo(memberVariantType, name,
-                PropertyHint.None, string.Empty, propUsage, exported: false);
+                PropertyHint.None, string.Empty, propUsage, className, exported: false);
         }
 
         private static void GenerateHasSignalEntry(

+ 6 - 0
modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/PropertyInfo.cs

@@ -9,16 +9,22 @@ public readonly struct PropertyInfo
     public PropertyHint Hint { get; init; }
     public string HintString { get; init; }
     public PropertyUsageFlags Usage { get; init; }
+    public StringName? ClassName { get; init; }
     public bool Exported { get; init; }
 
     public PropertyInfo(Variant.Type type, StringName name, PropertyHint hint, string hintString,
         PropertyUsageFlags usage, bool exported)
+        : this(type, name, hint, hintString, usage, className: null, exported) { }
+
+    public PropertyInfo(Variant.Type type, StringName name, PropertyHint hint, string hintString,
+        PropertyUsageFlags usage, StringName? className, bool exported)
     {
         Type = type;
         Name = name;
         Hint = hint;
         HintString = hintString;
         Usage = usage;
+        ClassName = className;
         Exported = exported;
     }
 }

+ 16 - 4
modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs

@@ -655,12 +655,18 @@ namespace Godot.Bridge
                             {
                                 foreach (var param in method.Arguments)
                                 {
-                                    methodParams.Add(new Collections.Dictionary()
+                                    var pinfo = new Collections.Dictionary()
                                     {
                                         { "name", param.Name },
                                         { "type", (int)param.Type },
                                         { "usage", (int)param.Usage }
-                                    });
+                                    };
+                                    if (param.ClassName != null)
+                                    {
+                                        pinfo["class_name"] = param.ClassName;
+                                    }
+
+                                    methodParams.Add(pinfo);
                                 }
                             }
 
@@ -743,12 +749,18 @@ namespace Godot.Bridge
                             {
                                 foreach (var param in signal.Arguments)
                                 {
-                                    signalParams.Add(new Collections.Dictionary()
+                                    var pinfo = new Collections.Dictionary()
                                     {
                                         { "name", param.Name },
                                         { "type", (int)param.Type },
                                         { "usage", (int)param.Usage }
-                                    });
+                                    };
+                                    if (param.ClassName != null)
+                                    {
+                                        pinfo["class_name"] = param.ClassName;
+                                    }
+
+                                    signalParams.Add(pinfo);
                                 }
                             }