HipcSyntaxReceiver.cs 2.0 KB

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