2022-07-28 17:41:47 +02:00
|
|
|
using System.Collections.Generic;
|
2021-12-28 23:25:16 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
|
|
|
|
namespace Godot.SourceGenerators
|
|
|
|
{
|
|
|
|
[Generator]
|
2022-07-28 17:41:48 +02:00
|
|
|
public class ScriptMethodsGenerator : ISourceGenerator
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
public void Initialize(GeneratorInitializationContext context)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
public void Execute(GeneratorExecutionContext context)
|
|
|
|
{
|
|
|
|
if (context.AreGodotSourceGeneratorsDisabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
INamedTypeSymbol[] godotClasses = context
|
|
|
|
.Compilation.SyntaxTrees
|
|
|
|
.SelectMany(tree =>
|
|
|
|
tree.GetRoot().DescendantNodes()
|
|
|
|
.OfType<ClassDeclarationSyntax>()
|
|
|
|
.SelectGodotScriptClasses(context.Compilation)
|
|
|
|
// Report and skip non-partial classes
|
|
|
|
.Where(x =>
|
|
|
|
{
|
|
|
|
if (x.cds.IsPartial())
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
if (x.cds.IsNested() && !x.cds.AreAllOuterTypesPartial(out var typeMissingPartial))
|
|
|
|
{
|
|
|
|
Common.ReportNonPartialGodotScriptOuterClass(context, typeMissingPartial!);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
return true;
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
Common.ReportNonPartialGodotScriptClass(context, x.cds, x.symbol);
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.Select(x => x.symbol)
|
|
|
|
)
|
|
|
|
.Distinct<INamedTypeSymbol>(SymbolEqualityComparer.Default)
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
if (godotClasses.Length > 0)
|
|
|
|
{
|
2022-08-15 05:57:52 +02:00
|
|
|
var typeCache = new MarshalUtils.TypeCache(context.Compilation);
|
2021-12-28 23:25:16 +01:00
|
|
|
|
|
|
|
foreach (var godotClass in godotClasses)
|
|
|
|
{
|
|
|
|
VisitGodotScriptClass(context, typeCache, godotClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
private class MethodOverloadEqualityComparer : IEqualityComparer<GodotMethodData>
|
|
|
|
{
|
|
|
|
public bool Equals(GodotMethodData x, GodotMethodData y)
|
|
|
|
=> x.ParamTypes.Length == y.ParamTypes.Length && x.Method.Name == y.Method.Name;
|
|
|
|
|
|
|
|
public int GetHashCode(GodotMethodData obj)
|
|
|
|
{
|
|
|
|
unchecked
|
|
|
|
{
|
|
|
|
return (obj.ParamTypes.Length.GetHashCode() * 397) ^ obj.Method.Name.GetHashCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
private static void VisitGodotScriptClass(
|
|
|
|
GeneratorExecutionContext context,
|
|
|
|
MarshalUtils.TypeCache typeCache,
|
|
|
|
INamedTypeSymbol symbol
|
|
|
|
)
|
|
|
|
{
|
|
|
|
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
|
|
|
|
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
|
|
|
|
namespaceSymbol.FullQualifiedName() :
|
|
|
|
string.Empty;
|
|
|
|
bool hasNamespace = classNs.Length != 0;
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
bool isInnerClass = symbol.ContainingType != null;
|
|
|
|
|
2022-02-27 21:57:30 +01:00
|
|
|
string uniqueHint = symbol.FullQualifiedName().SanitizeQualifiedNameForUniqueHint()
|
2022-07-28 17:41:48 +02:00
|
|
|
+ "_ScriptMethods_Generated";
|
2021-12-28 23:25:16 +01:00
|
|
|
|
|
|
|
var source = new StringBuilder();
|
|
|
|
|
|
|
|
source.Append("using Godot;\n");
|
|
|
|
source.Append("using Godot.NativeInterop;\n");
|
|
|
|
source.Append("\n");
|
|
|
|
|
|
|
|
if (hasNamespace)
|
|
|
|
{
|
|
|
|
source.Append("namespace ");
|
|
|
|
source.Append(classNs);
|
|
|
|
source.Append(" {\n\n");
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if (isInnerClass)
|
|
|
|
{
|
|
|
|
var containingType = symbol.ContainingType;
|
|
|
|
|
|
|
|
while (containingType != null)
|
|
|
|
{
|
|
|
|
source.Append("partial ");
|
|
|
|
source.Append(containingType.GetDeclarationKeyword());
|
|
|
|
source.Append(" ");
|
2022-02-27 21:57:30 +01:00
|
|
|
source.Append(containingType.NameWithTypeParameters());
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append("\n{\n");
|
|
|
|
|
|
|
|
containingType = containingType.ContainingType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append("partial class ");
|
2022-02-27 21:57:30 +01:00
|
|
|
source.Append(symbol.NameWithTypeParameters());
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append("\n{\n");
|
|
|
|
|
|
|
|
var members = symbol.GetMembers();
|
|
|
|
|
|
|
|
var methodSymbols = members
|
2022-02-27 21:57:30 +01:00
|
|
|
.Where(s => !s.IsStatic && s.Kind == SymbolKind.Method && !s.IsImplicitlyDeclared)
|
2021-12-28 23:25:16 +01:00
|
|
|
.Cast<IMethodSymbol>()
|
2022-02-27 21:57:30 +01:00
|
|
|
.Where(m => m.MethodKind == MethodKind.Ordinary);
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
var godotClassMethods = methodSymbols.WhereHasGodotCompatibleSignature(typeCache)
|
|
|
|
.Distinct(new MethodOverloadEqualityComparer())
|
|
|
|
.ToArray();
|
2022-07-28 17:41:47 +02:00
|
|
|
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
|
|
|
|
|
|
|
|
source.Append($" public new class MethodName : {symbol.BaseType.FullQualifiedName()}.MethodName {{\n");
|
2021-12-28 23:25:16 +01:00
|
|
|
|
|
|
|
// Generate cached StringNames for methods and properties, for fast lookup
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
var distinctMethodNames = godotClassMethods
|
|
|
|
.Select(m => m.Method.Name)
|
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
2022-02-27 21:57:30 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
foreach (string methodName in distinctMethodNames)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append(" public new static readonly StringName ");
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append(methodName);
|
|
|
|
source.Append(" = \"");
|
|
|
|
source.Append(methodName);
|
|
|
|
source.Append("\";\n");
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append(" }\n"); // class GodotInternal
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
// Generate GetGodotMethodList
|
|
|
|
|
|
|
|
if (godotClassMethods.Length > 0)
|
|
|
|
{
|
|
|
|
const string listType = "System.Collections.Generic.List<global::Godot.Bridge.MethodInfo>";
|
|
|
|
|
|
|
|
source.Append(" internal new static ")
|
|
|
|
.Append(listType)
|
|
|
|
.Append(" GetGodotMethodList()\n {\n");
|
|
|
|
|
|
|
|
source.Append(" var methods = new ")
|
|
|
|
.Append(listType)
|
|
|
|
.Append("(")
|
|
|
|
.Append(godotClassMethods.Length)
|
|
|
|
.Append(");\n");
|
|
|
|
|
|
|
|
foreach (var method in godotClassMethods)
|
|
|
|
{
|
|
|
|
var methodInfo = DetermineMethodInfo(method);
|
|
|
|
AppendMethodInfo(source, methodInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(" return methods;\n");
|
|
|
|
source.Append(" }\n");
|
|
|
|
}
|
|
|
|
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append("#pragma warning restore CS0109\n");
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
// Generate InvokeGodotClassMethod
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if (godotClassMethods.Length > 0)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
source.Append(" protected override bool InvokeGodotClassMethod(in godot_string_name method, ");
|
|
|
|
source.Append("NativeVariantPtrArgs args, int argCount, out godot_variant ret)\n {\n");
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
foreach (var method in godotClassMethods)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
GenerateMethodInvoker(method, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(" return base.InvokeGodotClassMethod(method, args, argCount, out ret);\n");
|
|
|
|
|
|
|
|
source.Append(" }\n");
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:47 +02:00
|
|
|
// Generate HasGodotClassMethod
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
if (distinctMethodNames.Length > 0)
|
2022-07-28 17:41:47 +02:00
|
|
|
{
|
|
|
|
source.Append(" protected override bool HasGodotClassMethod(in godot_string_name method)\n {\n");
|
|
|
|
|
|
|
|
bool isFirstEntry = true;
|
2022-07-28 17:41:48 +02:00
|
|
|
foreach (string methodName in distinctMethodNames)
|
2022-07-28 17:41:47 +02:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
GenerateHasMethodEntry(methodName, source, isFirstEntry);
|
2022-07-28 17:41:47 +02:00
|
|
|
isFirstEntry = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(" return base.HasGodotClassMethod(method);\n");
|
|
|
|
|
|
|
|
source.Append(" }\n");
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append("}\n"); // partial class
|
2022-07-28 17:41:47 +02:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
if (isInnerClass)
|
2022-07-28 17:41:47 +02:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
var containingType = symbol.ContainingType;
|
2022-07-28 17:41:47 +02:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
while (containingType != null)
|
2022-07-28 17:41:47 +02:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append("}\n"); // outer class
|
2022-07-28 17:41:47 +02:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
containingType = containingType.ContainingType;
|
|
|
|
}
|
2022-07-28 17:41:47 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
if (hasNamespace)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append("\n}\n");
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8));
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
private static void AppendMethodInfo(StringBuilder source, MethodInfo methodInfo)
|
|
|
|
{
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append(" methods.Add(new(name: MethodName.")
|
2022-07-28 17:41:48 +02:00
|
|
|
.Append(methodInfo.Name)
|
|
|
|
.Append(", returnVal: ");
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
AppendPropertyInfo(source, methodInfo.ReturnVal);
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append(", flags: (Godot.MethodFlags)")
|
|
|
|
.Append((int)methodInfo.Flags)
|
|
|
|
.Append(", arguments: ");
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
if (methodInfo.Arguments is { Count: > 0 })
|
|
|
|
{
|
|
|
|
source.Append("new() { ");
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
foreach (var param in methodInfo.Arguments)
|
|
|
|
{
|
|
|
|
AppendPropertyInfo(source, param);
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
// C# allows colon after the last element
|
|
|
|
source.Append(", ");
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append(" }");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source.Append("null");
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append(", defaultArguments: null));\n");
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
|
|
|
|
{
|
|
|
|
source.Append("new(type: (Godot.Variant.Type)")
|
|
|
|
.Append((int)propertyInfo.Type)
|
|
|
|
.Append(", name: \"")
|
|
|
|
.Append(propertyInfo.Name)
|
|
|
|
.Append("\", hint: (Godot.PropertyHint)")
|
|
|
|
.Append((int)propertyInfo.Hint)
|
|
|
|
.Append(", hintString: \"")
|
|
|
|
.Append(propertyInfo.HintString)
|
|
|
|
.Append("\", usage: (Godot.PropertyUsageFlags)")
|
|
|
|
.Append((int)propertyInfo.Usage)
|
|
|
|
.Append(", exported: ")
|
|
|
|
.Append(propertyInfo.Exported ? "true" : "false")
|
|
|
|
.Append(")");
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
private static MethodInfo DetermineMethodInfo(GodotMethodData method)
|
|
|
|
{
|
|
|
|
PropertyInfo returnVal;
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
if (method.RetType != null)
|
|
|
|
{
|
|
|
|
returnVal = DeterminePropertyInfo(method.RetType.Value, name: string.Empty);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
returnVal = new PropertyInfo(VariantType.Nil, string.Empty, PropertyHint.None,
|
|
|
|
hintString: null, PropertyUsageFlags.Default, exported: false);
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
int paramCount = method.ParamTypes.Length;
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
List<PropertyInfo>? arguments;
|
|
|
|
|
|
|
|
if (paramCount > 0)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
arguments = new(capacity: paramCount);
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
for (int i = 0; i < paramCount; i++)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
arguments.Add(DeterminePropertyInfo(method.ParamTypes[i],
|
|
|
|
name: method.Method.Parameters[i].Name));
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 17:41:48 +02:00
|
|
|
else
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-07-28 17:41:48 +02:00
|
|
|
arguments = null;
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:41:48 +02:00
|
|
|
return new MethodInfo(method.Method.Name, returnVal, MethodFlags.Default, arguments,
|
|
|
|
defaultArguments: null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, string name)
|
|
|
|
{
|
|
|
|
var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value;
|
|
|
|
|
|
|
|
var propUsage = PropertyUsageFlags.Default;
|
|
|
|
|
|
|
|
if (memberVariantType == VariantType.Nil)
|
|
|
|
propUsage |= PropertyUsageFlags.NilIsVariant;
|
|
|
|
|
|
|
|
return new PropertyInfo(memberVariantType, name,
|
|
|
|
PropertyHint.None, string.Empty, propUsage, exported: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void GenerateHasMethodEntry(
|
|
|
|
string methodName,
|
|
|
|
StringBuilder source,
|
|
|
|
bool isFirstEntry
|
|
|
|
)
|
|
|
|
{
|
|
|
|
source.Append(" ");
|
|
|
|
if (!isFirstEntry)
|
|
|
|
source.Append("else ");
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append("if (method == MethodName.");
|
2022-07-28 17:41:48 +02:00
|
|
|
source.Append(methodName);
|
|
|
|
source.Append(") {\n return true;\n }\n");
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void GenerateMethodInvoker(
|
2022-02-27 21:57:30 +01:00
|
|
|
GodotMethodData method,
|
2021-12-28 23:25:16 +01:00
|
|
|
StringBuilder source
|
|
|
|
)
|
|
|
|
{
|
|
|
|
string methodName = method.Method.Name;
|
|
|
|
|
2022-09-06 14:43:40 +02:00
|
|
|
source.Append(" if (method == MethodName.");
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append(methodName);
|
|
|
|
source.Append(" && argCount == ");
|
|
|
|
source.Append(method.ParamTypes.Length);
|
|
|
|
source.Append(") {\n");
|
|
|
|
|
|
|
|
if (method.RetType != null)
|
2022-02-27 21:57:46 +01:00
|
|
|
source.Append(" var callRet = ");
|
2021-12-28 23:25:16 +01:00
|
|
|
else
|
|
|
|
source.Append(" ");
|
|
|
|
|
|
|
|
source.Append(methodName);
|
|
|
|
source.Append("(");
|
|
|
|
|
|
|
|
for (int i = 0; i < method.ParamTypes.Length; i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
source.Append(", ");
|
|
|
|
|
2022-07-28 17:41:50 +02:00
|
|
|
source.AppendNativeVariantToManagedExpr(string.Concat("args[", i.ToString(), "]"),
|
2022-02-27 21:57:46 +01:00
|
|
|
method.ParamTypeSymbols[i], method.ParamTypes[i]);
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(");\n");
|
|
|
|
|
|
|
|
if (method.RetType != null)
|
|
|
|
{
|
2022-02-27 21:57:46 +01:00
|
|
|
source.Append(" ret = ");
|
|
|
|
|
2022-07-28 17:41:50 +02:00
|
|
|
source.AppendManagedToNativeVariantExpr("callRet", method.RetType.Value);
|
2022-02-27 21:57:46 +01:00
|
|
|
source.Append(";\n");
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
source.Append(" return true;\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source.Append(" ret = default;\n");
|
|
|
|
source.Append(" return true;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(" }\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|