MethodMetadata.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.CodeAnalysis;
  2. namespace Lua.SourceGenerator;
  3. internal class MethodMetadata
  4. {
  5. public IMethodSymbol Symbol { get; }
  6. public bool IsStatic { get; }
  7. public bool IsAsync { get; }
  8. public bool HasReturnValue { get; }
  9. public bool HasMemberAttribute { get; }
  10. public bool HasMetamethodAttribute { get; }
  11. public string LuaMemberName { get; }
  12. public LuaObjectMetamethod Metamethod { get; }
  13. public MethodMetadata(IMethodSymbol symbol, SymbolReferences references)
  14. {
  15. Symbol = symbol;
  16. IsStatic = symbol.IsStatic;
  17. var returnType = symbol.ReturnType;
  18. var fullName = (returnType.ContainingNamespace.IsGlobalNamespace ? "" : (returnType.ContainingNamespace + ".")) + returnType.Name;
  19. IsAsync = fullName is "System.Threading.Tasks.Task"
  20. or "System.Threading.Tasks.ValueTask"
  21. or "Cysharp.Threading.Tasks.UniTask"
  22. or "UnityEngine.Awaitable";
  23. HasReturnValue = !symbol.ReturnsVoid && !(IsAsync && returnType is INamedTypeSymbol n && !n.IsGenericType);
  24. LuaMemberName = symbol.Name;
  25. var memberAttribute = symbol.GetAttribute(references.LuaMemberAttribute);
  26. HasMemberAttribute = memberAttribute != null;
  27. if (memberAttribute != null)
  28. {
  29. if (memberAttribute.ConstructorArguments.Length > 0)
  30. {
  31. var value = memberAttribute.ConstructorArguments[0].Value;
  32. if (value is string str)
  33. {
  34. LuaMemberName = str;
  35. }
  36. }
  37. }
  38. var metamethodAttribute = symbol.GetAttribute(references.LuaMetamethodAttribute);
  39. HasMetamethodAttribute = metamethodAttribute != null;
  40. if (metamethodAttribute != null)
  41. {
  42. Metamethod = (LuaObjectMetamethod)Enum.Parse(typeof(LuaObjectMetamethod), metamethodAttribute.ConstructorArguments[0].Value!.ToString());
  43. }
  44. }
  45. }