HipcSyntaxReceiver.cs
1 using Microsoft.CodeAnalysis; 2 using Microsoft.CodeAnalysis.CSharp; 3 using Microsoft.CodeAnalysis.CSharp.Syntax; 4 using System.Collections.Generic; 5 using System.Linq; 6 7 namespace Ryujinx.Horizon.Generators.Hipc 8 { 9 class HipcSyntaxReceiver : ISyntaxReceiver 10 { 11 public List<CommandInterface> CommandInterfaces { get; } 12 13 public HipcSyntaxReceiver() 14 { 15 CommandInterfaces = new List<CommandInterface>(); 16 } 17 18 public void OnVisitSyntaxNode(SyntaxNode syntaxNode) 19 { 20 if (syntaxNode is ClassDeclarationSyntax classDeclaration) 21 { 22 if (!classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword) || classDeclaration.BaseList == null) 23 { 24 return; 25 } 26 27 CommandInterface commandInterface = new CommandInterface(classDeclaration); 28 29 foreach (var memberDeclaration in classDeclaration.Members) 30 { 31 if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) 32 { 33 VisitMethod(commandInterface, methodDeclaration); 34 } 35 } 36 37 CommandInterfaces.Add(commandInterface); 38 } 39 } 40 41 private void VisitMethod(CommandInterface commandInterface, MethodDeclarationSyntax methodDeclaration) 42 { 43 string attributeName = HipcGenerator.CommandAttributeName.Replace("Attribute", string.Empty); 44 45 if (methodDeclaration.AttributeLists.Count != 0) 46 { 47 foreach (var attributeList in methodDeclaration.AttributeLists) 48 { 49 if (attributeList.Attributes.Any(x => x.Name.ToString().Contains(attributeName))) 50 { 51 commandInterface.CommandImplementations.Add(methodDeclaration); 52 break; 53 } 54 } 55 } 56 } 57 } 58 }