#nullable enable using System; using System.Collections.Concurrent; using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; namespace ShrinkSDK.CodeGen.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public sealed class ShrinkCodeGenAnalyzer : DiagnosticAnalyzer { private static readonly DiagnosticDescriptor InvalidEventHandler = new( "SHRINK001", "Invalid event subscriber signature", "Method '{0}' must use void(TEvent), UniTask(TEvent), or UniTask(TEvent, CancellationToken)", "ShrinkSDK.CodeGen", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor DuplicateNetworkContract = new( "SHRINK002", "Duplicate network opcode and route", "Network opcode/route '{0}' is already declared by '{1}'", "ShrinkSDK.CodeGen", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor InvalidInstaller = new( "SHRINK003", "Invalid application installer", "Type '{0}' has ShrinkAppModuleInstaller but does not implement IShrinkAppModuleInstaller", "ShrinkSDK.CodeGen", DiagnosticSeverity.Error, true); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(InvalidEventHandler, DuplicateNetworkContract, InvalidInstaller); public override void Initialize(AnalysisContext context) { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.EnableConcurrentExecution(); context.RegisterCompilationStartAction(start => { var networkContracts = new ConcurrentDictionary(StringComparer.Ordinal); start.RegisterSymbolAction(symbolContext => AnalyzeMethod(symbolContext), SymbolKind.Method); start.RegisterSymbolAction(symbolContext => AnalyzeType(symbolContext, networkContracts), SymbolKind.NamedType); }); } private static void AnalyzeMethod(SymbolAnalysisContext context) { var method = (IMethodSymbol)context.Symbol; if (!HasAttribute(method, "ShrinkEventBus.ShrinkSubscribeAttribute")) return; var parametersValid = method.Parameters.Length == 1 || method.Parameters.Length == 2 && method.Parameters[1].Type.ToDisplayString() == "System.Threading.CancellationToken"; var returnName = method.ReturnType.ToDisplayString(); var returnValid = method.Parameters.Length == 1 && (method.ReturnsVoid || returnName == "Cysharp.Threading.Tasks.UniTask") || method.Parameters.Length == 2 && returnName == "Cysharp.Threading.Tasks.UniTask"; if (!parametersValid || !returnValid) context.ReportDiagnostic(Diagnostic.Create(InvalidEventHandler, method.Locations.FirstOrDefault(), method.Name)); } private static void AnalyzeType(SymbolAnalysisContext context, ConcurrentDictionary networkContracts) { var type = (INamedTypeSymbol)context.Symbol; if (HasAttribute(type, "ShrinkApp.ShrinkAppModuleInstallerAttribute") && !type.AllInterfaces.Any(item => item.ToDisplayString() == "ShrinkApp.IShrinkAppModuleInstaller")) context.ReportDiagnostic(Diagnostic.Create(InvalidInstaller, type.Locations.FirstOrDefault(), type.Name)); var network = type.GetAttributes().FirstOrDefault(attribute => attribute.AttributeClass?.ToDisplayString() == "ShrinkNetwork.ShrinkNetworkMessageAttribute"); if (network == null || network.ConstructorArguments.Length == 0) return; var opcode = network.ConstructorArguments[0].Value?.ToString() ?? string.Empty; var route = network.ConstructorArguments.Length > 1 ? network.ConstructorArguments[1].Value as string ?? string.Empty : string.Empty; var key = opcode + ":" + route; if (!networkContracts.TryAdd(key, type) && networkContracts.TryGetValue(key, out var previous)) context.ReportDiagnostic(Diagnostic.Create(DuplicateNetworkContract, type.Locations.FirstOrDefault(), key, previous.Name)); } private static bool HasAttribute(ISymbol symbol, string fullName) => symbol.GetAttributes().Any(attribute => attribute.AttributeClass?.ToDisplayString() == fullName); }