| 1 |
using System.Collections.Generic; |
| 2 |
using System.Linq; |
| 3 |
using System.Text; |
| 4 |
using Microsoft.CodeAnalysis; |
| 5 |
using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 |
using Microsoft.CodeAnalysis.Text; |
| 7 |
|
| 8 |
namespace Godot.SourceGenerators |
| 9 |
{ |
| 10 |
[Generator] |
| 11 |
public class ScriptMethodsGenerator : ISourceGenerator |
| 12 |
{ |
| 13 |
public void Initialize(GeneratorInitializationContext context) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
public void Execute(GeneratorExecutionContext context) |
| 18 |
{ |
| 19 |
if (context.IsGodotSourceGeneratorDisabled("ScriptMethods")) |
| 20 |
return; |
| 21 |
|
| 22 |
INamedTypeSymbol[] godotClasses = context |
| 23 |
.Compilation.SyntaxTrees |
| 24 |
.SelectMany(tree => |
| 25 |
tree.GetRoot().DescendantNodes() |
| 26 |
.OfType<ClassDeclarationSyntax>() |
| 27 |
.SelectGodotScriptClasses(context.Compilation) |
| 28 |
|
| 29 |
.Where(x => |
| 30 |
{ |
| 31 |
if (x.cds.IsPartial()) |
| 32 |
{ |
| 33 |
if (x.cds.IsNested() && !x.cds.AreAllOuterTypesPartial(out _)) |
| 34 |
{ |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
return false; |
| 41 |
}) |
| 42 |
.Select(x => x.symbol) |
| 43 |
) |
| 44 |
.Distinct<INamedTypeSymbol>(SymbolEqualityComparer.Default) |
| 45 |
.ToArray(); |
| 46 |
|
| 47 |
if (godotClasses.Length > 0) |
| 48 |
{ |
| 49 |
var typeCache = new MarshalUtils.TypeCache(context.Compilation); |
| 50 |
|
| 51 |
foreach (var godotClass in godotClasses) |
| 52 |
{ |
| 53 |
VisitGodotScriptClass(context, typeCache, godotClass); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private class MethodOverloadEqualityComparer : IEqualityComparer<GodotMethodData> |
| 59 |
{ |
| 60 |
public bool Equals(GodotMethodData x, GodotMethodData y) |
| 61 |
=> x.ParamTypes.Length == y.ParamTypes.Length && x.Method.Name == y.Method.Name; |
| 62 |
|
| 63 |
public int GetHashCode(GodotMethodData obj) |
| 64 |
{ |
| 65 |
unchecked |
| 66 |
{ |
| 67 |
return (obj.ParamTypes.Length.GetHashCode() * 397) ^ obj.Method.Name.GetHashCode(); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private static void VisitGodotScriptClass( |
| 73 |
GeneratorExecutionContext context, |
| 74 |
MarshalUtils.TypeCache typeCache, |
| 75 |
INamedTypeSymbol symbol |
| 76 |
) |
| 77 |
{ |
| 78 |
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace; |
| 79 |
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ? |
| 80 |
namespaceSymbol.FullQualifiedNameOmitGlobal() : |
| 81 |
string.Empty; |
| 82 |
bool hasNamespace = classNs.Length != 0; |
| 83 |
|
| 84 |
bool isInnerClass = symbol.ContainingType != null; |
| 85 |
|
| 86 |
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint() |
| 87 |
+ "_ScriptMethods.generated"; |
| 88 |
|
| 89 |
var source = new StringBuilder(); |
| 90 |
|
| 91 |
source.Append("using Godot;\n"); |
| 92 |
source.Append("using Godot.NativeInterop;\n"); |
| 93 |
source.Append("\n"); |
| 94 |
|
| 95 |
if (hasNamespace) |
| 96 |
{ |
| 97 |
source.Append("namespace "); |
| 98 |
source.Append(classNs); |
| 99 |
source.Append(" {\n\n"); |
| 100 |
} |
| 101 |
|
| 102 |
if (isInnerClass) |
| 103 |
{ |
| 104 |
var containingType = symbol.ContainingType; |
| 105 |
AppendPartialContainingTypeDeclarations(containingType); |
| 106 |
|
| 107 |
void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType) |
| 108 |
{ |
| 109 |
if (containingType == null) |
| 110 |
return; |
| 111 |
|
| 112 |
AppendPartialContainingTypeDeclarations(containingType.ContainingType); |
| 113 |
|
| 114 |
source.Append("partial "); |
| 115 |
source.Append(containingType.GetDeclarationKeyword()); |
| 116 |
source.Append(" "); |
| 117 |
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); |
| 118 |
source.Append("\n{\n"); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
source.Append("partial class "); |
| 123 |
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); |
| 124 |
source.Append("\n{\n"); |
| 125 |
|
| 126 |
var members = symbol.GetMembers(); |
| 127 |
|
| 128 |
var methodSymbols = members |
| 129 |
.Where(s => s.Kind == SymbolKind.Method && !s.IsImplicitlyDeclared) |
| 130 |
.Cast<IMethodSymbol>() |
| 131 |
.Where(m => m.MethodKind == MethodKind.Ordinary); |
| 132 |
|
| 133 |
var godotClassMethods = methodSymbols.WhereHasGodotCompatibleSignature(typeCache) |
| 134 |
.Distinct(new MethodOverloadEqualityComparer()) |
| 135 |
.ToArray(); |
| 136 |
|
| 137 |
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); |
| 138 |
|
| 139 |
source.Append(" /// <summary>\n") |
| 140 |
.Append(" /// Cached StringNames for the methods contained in this class, for fast lookup.\n") |
| 141 |
.Append(" /// </summary>\n"); |
| 142 |
|
| 143 |
source.Append( |
| 144 |
$" public new class MethodName : {symbol.BaseType!.FullQualifiedNameIncludeGlobal()}.MethodName {{\n"); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
var distinctMethodNames = godotClassMethods |
| 149 |
.Select(m => m.Method.Name) |
| 150 |
.Distinct() |
| 151 |
.ToArray(); |
| 152 |
|
| 153 |
foreach (string methodName in distinctMethodNames) |
| 154 |
{ |
| 155 |
source.Append(" /// <summary>\n") |
| 156 |
.Append(" /// Cached name for the '") |
| 157 |
.Append(methodName) |
| 158 |
.Append("' method.\n") |
| 159 |
.Append(" /// </summary>\n"); |
| 160 |
|
| 161 |
source.Append(" public new static readonly global::Godot.StringName @"); |
| 162 |
source.Append(methodName); |
| 163 |
source.Append(" = \""); |
| 164 |
source.Append(methodName); |
| 165 |
source.Append("\";\n"); |
| 166 |
} |
| 167 |
|
| 168 |
source.Append(" }\n"); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if (godotClassMethods.Length > 0) |
| 173 |
{ |
| 174 |
const string ListType = "global::System.Collections.Generic.List<global::Godot.Bridge.MethodInfo>"; |
| 175 |
|
| 176 |
source.Append(" /// <summary>\n") |
| 177 |
.Append(" /// Get the method information for all the methods declared in this class.\n") |
| 178 |
.Append(" /// This method is used by Godot to register the available methods in the editor.\n") |
| 179 |
.Append(" /// Do not call this method.\n") |
| 180 |
.Append(" /// </summary>\n"); |
| 181 |
|
| 182 |
source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n"); |
| 183 |
|
| 184 |
source.Append(" internal new static ") |
| 185 |
.Append(ListType) |
| 186 |
.Append(" GetGodotMethodList()\n {\n"); |
| 187 |
|
| 188 |
source.Append(" var methods = new ") |
| 189 |
.Append(ListType) |
| 190 |
.Append("(") |
| 191 |
.Append(godotClassMethods.Length) |
| 192 |
.Append(");\n"); |
| 193 |
|
| 194 |
foreach (var method in godotClassMethods) |
| 195 |
{ |
| 196 |
var methodInfo = DetermineMethodInfo(method); |
| 197 |
AppendMethodInfo(source, methodInfo); |
| 198 |
} |
| 199 |
|
| 200 |
source.Append(" return methods;\n"); |
| 201 |
source.Append(" }\n"); |
| 202 |
} |
| 203 |
|
| 204 |
source.Append("#pragma warning restore CS0109\n"); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if (godotClassMethods.Length > 0) |
| 209 |
{ |
| 210 |
source.Append(" /// <inheritdoc/>\n"); |
| 211 |
source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n"); |
| 212 |
source.Append(" protected override bool InvokeGodotClassMethod(in godot_string_name method, "); |
| 213 |
source.Append("NativeVariantPtrArgs args, out godot_variant ret)\n {\n"); |
| 214 |
|
| 215 |
foreach (var method in godotClassMethods) |
| 216 |
{ |
| 217 |
GenerateMethodInvoker(method, source); |
| 218 |
} |
| 219 |
|
| 220 |
source.Append(" return base.InvokeGodotClassMethod(method, args, out ret);\n"); |
| 221 |
|
| 222 |
source.Append(" }\n"); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
var godotClassStaticMethods = godotClassMethods.Where(m => m.Method.IsStatic).ToArray(); |
| 228 |
|
| 229 |
if (godotClassStaticMethods.Length > 0) |
| 230 |
{ |
| 231 |
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); |
| 232 |
source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n"); |
| 233 |
source.Append(" internal new static bool InvokeGodotClassStaticMethod(in godot_string_name method, "); |
| 234 |
source.Append("NativeVariantPtrArgs args, out godot_variant ret)\n {\n"); |
| 235 |
|
| 236 |
foreach (var method in godotClassStaticMethods) |
| 237 |
{ |
| 238 |
GenerateMethodInvoker(method, source); |
| 239 |
} |
| 240 |
|
| 241 |
source.Append(" ret = default;\n"); |
| 242 |
source.Append(" return false;\n"); |
| 243 |
source.Append(" }\n"); |
| 244 |
|
| 245 |
source.Append("#pragma warning restore CS0109\n"); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (distinctMethodNames.Length > 0) |
| 251 |
{ |
| 252 |
source.Append(" /// <inheritdoc/>\n"); |
| 253 |
source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n"); |
| 254 |
source.Append(" protected override bool HasGodotClassMethod(in godot_string_name method)\n {\n"); |
| 255 |
|
| 256 |
foreach (string methodName in distinctMethodNames) |
| 257 |
{ |
| 258 |
GenerateHasMethodEntry(methodName, source); |
| 259 |
} |
| 260 |
|
| 261 |
source.Append(" return base.HasGodotClassMethod(method);\n"); |
| 262 |
|
| 263 |
source.Append(" }\n"); |
| 264 |
} |
| 265 |
|
| 266 |
source.Append("}\n"); |
| 267 |
|
| 268 |
if (isInnerClass) |
| 269 |
{ |
| 270 |
var containingType = symbol.ContainingType; |
| 271 |
|
| 272 |
while (containingType != null) |
| 273 |
{ |
| 274 |
source.Append("}\n"); |
| 275 |
|
| 276 |
containingType = containingType.ContainingType; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if (hasNamespace) |
| 281 |
{ |
| 282 |
source.Append("\n}\n"); |
| 283 |
} |
| 284 |
|
| 285 |
context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8)); |
| 286 |
} |
| 287 |
|
| 288 |
private static void AppendMethodInfo(StringBuilder source, MethodInfo methodInfo) |
| 289 |
{ |
| 290 |
source.Append(" methods.Add(new(name: MethodName.@") |
| 291 |
.Append(methodInfo.Name) |
| 292 |
.Append(", returnVal: "); |
| 293 |
|
| 294 |
AppendPropertyInfo(source, methodInfo.ReturnVal); |
| 295 |
|
| 296 |
source.Append(", flags: (global::Godot.MethodFlags)") |
| 297 |
.Append((int)methodInfo.Flags) |
| 298 |
.Append(", arguments: "); |
| 299 |
|
| 300 |
if (methodInfo.Arguments is { Count: > 0 }) |
| 301 |
{ |
| 302 |
source.Append("new() { "); |
| 303 |
|
| 304 |
foreach (var param in methodInfo.Arguments) |
| 305 |
{ |
| 306 |
AppendPropertyInfo(source, param); |
| 307 |
|
| 308 |
|
| 309 |
source.Append(", "); |
| 310 |
} |
| 311 |
|
| 312 |
source.Append(" }"); |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
source.Append("null"); |
| 317 |
} |
| 318 |
|
| 319 |
source.Append(", defaultArguments: null));\n"); |
| 320 |
} |
| 321 |
|
| 322 |
private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo) |
| 323 |
{ |
| 324 |
source.Append("new(type: (global::Godot.Variant.Type)") |
| 325 |
.Append((int)propertyInfo.Type) |
| 326 |
.Append(", name: \"") |
| 327 |
.Append(propertyInfo.Name) |
| 328 |
.Append("\", hint: (global::Godot.PropertyHint)") |
| 329 |
.Append((int)propertyInfo.Hint) |
| 330 |
.Append(", hintString: \"") |
| 331 |
.Append(propertyInfo.HintString) |
| 332 |
.Append("\", usage: (global::Godot.PropertyUsageFlags)") |
| 333 |
.Append((int)propertyInfo.Usage) |
| 334 |
.Append(", exported: ") |
| 335 |
.Append(propertyInfo.Exported ? "true" : "false"); |
| 336 |
if (propertyInfo.ClassName != null) |
| 337 |
{ |
| 338 |
source.Append(", className: new global::Godot.StringName(\"") |
| 339 |
.Append(propertyInfo.ClassName) |
| 340 |
.Append("\")"); |
| 341 |
} |
| 342 |
source.Append(")"); |
| 343 |
} |
| 344 |
|
| 345 |
private static MethodInfo DetermineMethodInfo(GodotMethodData method) |
| 346 |
{ |
| 347 |
PropertyInfo returnVal; |
| 348 |
|
| 349 |
if (method.RetType != null) |
| 350 |
{ |
| 351 |
returnVal = DeterminePropertyInfo(method.RetType.Value.MarshalType, |
| 352 |
method.RetType.Value.TypeSymbol, |
| 353 |
name: string.Empty); |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
returnVal = new PropertyInfo(VariantType.Nil, string.Empty, PropertyHint.None, |
| 358 |
hintString: null, PropertyUsageFlags.Default, exported: false); |
| 359 |
} |
| 360 |
|
| 361 |
int paramCount = method.ParamTypes.Length; |
| 362 |
|
| 363 |
List<PropertyInfo>? arguments; |
| 364 |
|
| 365 |
if (paramCount > 0) |
| 366 |
{ |
| 367 |
arguments = new(capacity: paramCount); |
| 368 |
|
| 369 |
for (int i = 0; i < paramCount; i++) |
| 370 |
{ |
| 371 |
arguments.Add(DeterminePropertyInfo(method.ParamTypes[i], |
| 372 |
method.Method.Parameters[i].Type, |
| 373 |
name: method.Method.Parameters[i].Name)); |
| 374 |
} |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
arguments = null; |
| 379 |
} |
| 380 |
|
| 381 |
MethodFlags flags = MethodFlags.Default; |
| 382 |
|
| 383 |
if (method.Method.IsStatic) |
| 384 |
{ |
| 385 |
flags |= MethodFlags.Static; |
| 386 |
} |
| 387 |
|
| 388 |
return new MethodInfo(method.Method.Name, returnVal, flags, arguments, |
| 389 |
defaultArguments: null); |
| 390 |
} |
| 391 |
|
| 392 |
private static PropertyInfo DeterminePropertyInfo(MarshalType marshalType, ITypeSymbol typeSymbol, string name) |
| 393 |
{ |
| 394 |
var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value; |
| 395 |
|
| 396 |
var propUsage = PropertyUsageFlags.Default; |
| 397 |
|
| 398 |
if (memberVariantType == VariantType.Nil) |
| 399 |
propUsage |= PropertyUsageFlags.NilIsVariant; |
| 400 |
|
| 401 |
string? className = null; |
| 402 |
if (memberVariantType == VariantType.Object && typeSymbol is INamedTypeSymbol namedTypeSymbol) |
| 403 |
{ |
| 404 |
className = namedTypeSymbol.GetGodotScriptNativeClassName(); |
| 405 |
} |
| 406 |
|
| 407 |
return new PropertyInfo(memberVariantType, name, |
| 408 |
PropertyHint.None, string.Empty, propUsage, className, exported: false); |
| 409 |
} |
| 410 |
|
| 411 |
private static void GenerateHasMethodEntry( |
| 412 |
string methodName, |
| 413 |
StringBuilder source |
| 414 |
) |
| 415 |
{ |
| 416 |
source.Append(" "); |
| 417 |
source.Append("if (method == MethodName.@"); |
| 418 |
source.Append(methodName); |
| 419 |
source.Append(") {\n return true;\n }\n"); |
| 420 |
} |
| 421 |
|
| 422 |
private static void GenerateMethodInvoker( |
| 423 |
GodotMethodData method, |
| 424 |
StringBuilder source |
| 425 |
) |
| 426 |
{ |
| 427 |
string methodName = method.Method.Name; |
| 428 |
|
| 429 |
source.Append(" if (method == MethodName.@"); |
| 430 |
source.Append(methodName); |
| 431 |
source.Append(" && args.Count == "); |
| 432 |
source.Append(method.ParamTypes.Length); |
| 433 |
source.Append(") {\n"); |
| 434 |
|
| 435 |
if (method.RetType != null) |
| 436 |
source.Append(" var callRet = "); |
| 437 |
else |
| 438 |
source.Append(" "); |
| 439 |
|
| 440 |
source.Append("@"); |
| 441 |
source.Append(methodName); |
| 442 |
source.Append("("); |
| 443 |
|
| 444 |
for (int i = 0; i < method.ParamTypes.Length; i++) |
| 445 |
{ |
| 446 |
if (i != 0) |
| 447 |
source.Append(", "); |
| 448 |
|
| 449 |
source.AppendNativeVariantToManagedExpr(string.Concat("args[", i.ToString(), "]"), |
| 450 |
method.ParamTypeSymbols[i], method.ParamTypes[i]); |
| 451 |
} |
| 452 |
|
| 453 |
source.Append(");\n"); |
| 454 |
|
| 455 |
if (method.RetType != null) |
| 456 |
{ |
| 457 |
source.Append(" ret = "); |
| 458 |
|
| 459 |
source.AppendManagedToNativeVariantExpr("callRet", |
| 460 |
method.RetType.Value.TypeSymbol, method.RetType.Value.MarshalType); |
| 461 |
source.Append(";\n"); |
| 462 |
|
| 463 |
source.Append(" return true;\n"); |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
source.Append(" ret = default;\n"); |
| 468 |
source.Append(" return true;\n"); |
| 469 |
} |
| 470 |
|
| 471 |
source.Append(" }\n"); |
| 472 |
} |
| 473 |
} |
| 474 |
} |