2021-03-05 23:12:42 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
|
|
|
|
namespace Godot.SourceGenerators
|
|
|
|
{
|
|
|
|
[Generator]
|
|
|
|
public class ScriptPathAttributeGenerator : ISourceGenerator
|
|
|
|
{
|
|
|
|
public void Execute(GeneratorExecutionContext context)
|
|
|
|
{
|
2023-01-08 01:04:15 +00:00
|
|
|
if (context.IsGodotSourceGeneratorDisabled("ScriptPathAttribute"))
|
2021-03-05 23:12:42 +00:00
|
|
|
return;
|
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
if (context.IsGodotToolsProject())
|
|
|
|
return;
|
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
// NOTE: NotNullWhen diagnostics don't work on projects targeting .NET Standard 2.0
|
2021-03-05 23:12:42 +00:00
|
|
|
// ReSharper disable once ReplaceWithStringIsNullOrEmpty
|
2023-03-03 17:17:17 +00:00
|
|
|
if (!context.TryGetGlobalAnalyzerProperty("GodotProjectDirBase64", out string? godotProjectDir) || godotProjectDir!.Length == 0)
|
2021-03-05 23:12:42 +00:00
|
|
|
{
|
2023-03-03 17:17:17 +00:00
|
|
|
if (!context.TryGetGlobalAnalyzerProperty("GodotProjectDir", out godotProjectDir) || godotProjectDir!.Length == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Property 'GodotProjectDir' is null or empty.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Workaround for https://github.com/dotnet/roslyn/issues/51692
|
|
|
|
godotProjectDir = Encoding.UTF8.GetString(Convert.FromBase64String(godotProjectDir));
|
2021-03-05 23:12:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
Dictionary<INamedTypeSymbol, IEnumerable<ClassDeclarationSyntax>> godotClasses = context
|
|
|
|
.Compilation.SyntaxTrees
|
2021-03-05 23:12:42 +00:00
|
|
|
.SelectMany(tree =>
|
|
|
|
tree.GetRoot().DescendantNodes()
|
|
|
|
.OfType<ClassDeclarationSyntax>()
|
|
|
|
// Ignore inner classes
|
2021-12-28 22:25:16 +00:00
|
|
|
.Where(cds => !cds.IsNested())
|
2021-03-05 23:12:42 +00:00
|
|
|
.SelectGodotScriptClasses(context.Compilation)
|
|
|
|
// Report and skip non-partial classes
|
|
|
|
.Where(x =>
|
|
|
|
{
|
2021-12-28 22:25:16 +00:00
|
|
|
if (x.cds.IsPartial())
|
2021-03-05 23:12:42 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
)
|
2022-12-24 02:34:02 +00:00
|
|
|
.Where(x =>
|
|
|
|
// Ignore classes whose name is not the same as the file name
|
2024-02-05 01:49:57 +00:00
|
|
|
Path.GetFileNameWithoutExtension(x.cds.SyntaxTree.FilePath) == x.symbol.Name)
|
2023-08-05 17:45:22 +00:00
|
|
|
.GroupBy<(ClassDeclarationSyntax cds, INamedTypeSymbol symbol), INamedTypeSymbol>(x => x.symbol, SymbolEqualityComparer.Default)
|
|
|
|
.ToDictionary<IGrouping<INamedTypeSymbol, (ClassDeclarationSyntax cds, INamedTypeSymbol symbol)>, INamedTypeSymbol, IEnumerable<ClassDeclarationSyntax>>(g => g.Key, g => g.Select(x => x.cds), SymbolEqualityComparer.Default);
|
2021-03-05 23:12:42 +00:00
|
|
|
|
2024-02-15 17:18:33 +00:00
|
|
|
var usedPaths = new HashSet<string>();
|
2021-03-05 23:12:42 +00:00
|
|
|
foreach (var godotClass in godotClasses)
|
|
|
|
{
|
2024-02-15 17:18:33 +00:00
|
|
|
VisitGodotScriptClass(context, godotProjectDir, usedPaths,
|
2021-03-05 23:12:42 +00:00
|
|
|
symbol: godotClass.Key,
|
|
|
|
classDeclarations: godotClass.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (godotClasses.Count <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddScriptTypesAssemblyAttr(context, godotClasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void VisitGodotScriptClass(
|
|
|
|
GeneratorExecutionContext context,
|
|
|
|
string godotProjectDir,
|
2024-02-15 17:18:33 +00:00
|
|
|
HashSet<string> usedPaths,
|
2021-03-05 23:12:42 +00:00
|
|
|
INamedTypeSymbol symbol,
|
|
|
|
IEnumerable<ClassDeclarationSyntax> classDeclarations
|
|
|
|
)
|
|
|
|
{
|
2021-03-13 00:04:55 +00:00
|
|
|
var attributes = new StringBuilder();
|
2021-03-05 23:12:42 +00:00
|
|
|
|
|
|
|
// Remember syntax trees for which we already added an attribute, to prevent unnecessary duplicates.
|
|
|
|
var attributedTrees = new List<SyntaxTree>();
|
|
|
|
|
|
|
|
foreach (var cds in classDeclarations)
|
|
|
|
{
|
|
|
|
if (attributedTrees.Contains(cds.SyntaxTree))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attributedTrees.Add(cds.SyntaxTree);
|
|
|
|
|
2021-03-13 00:04:55 +00:00
|
|
|
if (attributes.Length != 0)
|
|
|
|
attributes.Append("\n");
|
2021-03-05 23:12:42 +00:00
|
|
|
|
2024-02-15 17:18:33 +00:00
|
|
|
string scriptPath = RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir);
|
|
|
|
if (!usedPaths.Add(scriptPath))
|
|
|
|
{
|
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.MultipleClassesInGodotScriptRule,
|
|
|
|
cds.Identifier.GetLocation(),
|
|
|
|
symbol.Name
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:04:55 +00:00
|
|
|
attributes.Append(@"[ScriptPathAttribute(""res://");
|
2024-02-15 17:18:33 +00:00
|
|
|
attributes.Append(scriptPath);
|
2021-03-13 00:04:55 +00:00
|
|
|
attributes.Append(@""")]");
|
2021-03-05 23:12:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 00:04:55 +00:00
|
|
|
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
|
|
|
|
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
|
2022-11-24 00:04:15 +00:00
|
|
|
namespaceSymbol.FullQualifiedNameOmitGlobal() :
|
2021-03-13 00:04:55 +00:00
|
|
|
string.Empty;
|
|
|
|
bool hasNamespace = classNs.Length != 0;
|
|
|
|
|
2022-11-24 00:04:15 +00:00
|
|
|
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint()
|
2022-10-22 21:13:52 +00:00
|
|
|
+ "_ScriptPath.generated";
|
2021-03-13 00:04:55 +00:00
|
|
|
|
|
|
|
var source = new StringBuilder();
|
|
|
|
|
|
|
|
// using Godot;
|
|
|
|
// namespace {classNs} {
|
|
|
|
// {attributesBuilder}
|
|
|
|
// partial class {className} { }
|
|
|
|
// }
|
|
|
|
|
|
|
|
source.Append("using Godot;\n");
|
|
|
|
|
|
|
|
if (hasNamespace)
|
|
|
|
{
|
|
|
|
source.Append("namespace ");
|
|
|
|
source.Append(classNs);
|
|
|
|
source.Append(" {\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
source.Append(attributes);
|
2022-02-27 20:57:30 +00:00
|
|
|
source.Append("\npartial class ");
|
|
|
|
source.Append(symbol.NameWithTypeParameters());
|
2021-03-13 00:04:55 +00:00
|
|
|
source.Append("\n{\n}\n");
|
|
|
|
|
|
|
|
if (hasNamespace)
|
|
|
|
{
|
|
|
|
source.Append("\n}\n");
|
|
|
|
}
|
|
|
|
|
2022-10-22 21:13:52 +00:00
|
|
|
context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8));
|
2021-03-05 23:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void AddScriptTypesAssemblyAttr(GeneratorExecutionContext context,
|
|
|
|
Dictionary<INamedTypeSymbol, IEnumerable<ClassDeclarationSyntax>> godotClasses)
|
|
|
|
{
|
|
|
|
var sourceBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
sourceBuilder.Append("[assembly:");
|
|
|
|
sourceBuilder.Append(GodotClasses.AssemblyHasScriptsAttr);
|
|
|
|
sourceBuilder.Append("(new System.Type[] {");
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
foreach (var godotClass in godotClasses)
|
|
|
|
{
|
|
|
|
var qualifiedName = godotClass.Key.ToDisplayString(
|
2021-12-03 17:40:32 +00:00
|
|
|
NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat
|
|
|
|
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
|
2021-03-05 23:12:42 +00:00
|
|
|
if (!first)
|
|
|
|
sourceBuilder.Append(", ");
|
|
|
|
first = false;
|
|
|
|
sourceBuilder.Append("typeof(");
|
|
|
|
sourceBuilder.Append(qualifiedName);
|
2024-02-05 01:49:57 +00:00
|
|
|
if (godotClass.Key.IsGenericType)
|
|
|
|
sourceBuilder.Append($"<{new string(',', godotClass.Key.TypeParameters.Count() - 1)}>");
|
2021-03-05 23:12:42 +00:00
|
|
|
sourceBuilder.Append(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceBuilder.Append("})]\n");
|
|
|
|
|
2022-10-22 21:13:52 +00:00
|
|
|
context.AddSource("AssemblyScriptTypes.generated",
|
2021-03-05 23:12:42 +00:00
|
|
|
SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Initialize(GeneratorInitializationContext context)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string RelativeToDir(string path, string dir)
|
|
|
|
{
|
|
|
|
// Make sure the directory ends with a path separator
|
|
|
|
dir = Path.Combine(dir, " ").TrimEnd();
|
|
|
|
|
|
|
|
if (Path.DirectorySeparatorChar == '\\')
|
|
|
|
dir = dir.Replace("/", "\\") + "\\";
|
|
|
|
|
|
|
|
var fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute);
|
|
|
|
var relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute);
|
|
|
|
|
|
|
|
// MakeRelativeUri converts spaces to %20, hence why we need UnescapeDataString
|
|
|
|
return Uri.UnescapeDataString(relRoot.MakeRelativeUri(fullPath).ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|