AttributeCache.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace Animancer.Editor
  9. {
  10. /// <summary>[Editor-Only] A cache to optimize repeated attribute access.</summary>
  11. /// <remarks>
  12. /// If <typeparamref name="TAttribute"/> implements <see cref="IInitializable{T}"/> for <see cref="MemberInfo"/>,
  13. /// its <see cref="IInitializable{T}.Initialize(T)"/> method will be called automatically.
  14. /// </remarks>
  15. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AttributeCache_1
  16. public static class AttributeCache<TAttribute>
  17. where TAttribute : class
  18. {
  19. /************************************************************************************************************************/
  20. private static readonly Dictionary<MemberInfo, TAttribute>
  21. MemberToAttribute = new();
  22. /************************************************************************************************************************/
  23. /// <summary>
  24. /// Returns the <typeparamref name="TAttribute"/> attribute on the specified `member` (if there is one).
  25. /// </summary>
  26. public static TAttribute GetAttribute(MemberInfo member)
  27. {
  28. if (!MemberToAttribute.TryGetValue(member, out var attribute))
  29. {
  30. try
  31. {
  32. attribute = member.GetAttribute<TAttribute>();
  33. if (attribute is IInitializable<MemberInfo> initializable)
  34. initializable.Initialize(member);
  35. }
  36. catch (Exception exception)
  37. {
  38. Debug.LogException(exception);
  39. attribute = null;
  40. }
  41. MemberToAttribute.Add(member, attribute);
  42. }
  43. return attribute;
  44. }
  45. /************************************************************************************************************************/
  46. /// <summary>
  47. /// Returns the <typeparamref name="TAttribute"/> attribute (if any)
  48. /// on the specified `type` or its <see cref="Type.BaseType"/> (recursively).
  49. /// </summary>
  50. public static TAttribute GetAttribute(Type type)
  51. {
  52. if (type == null)
  53. return null;
  54. var attribute = GetAttribute((MemberInfo)type);
  55. if (attribute != null)
  56. return attribute;
  57. return MemberToAttribute[type] = GetAttribute(type.BaseType);
  58. }
  59. /************************************************************************************************************************/
  60. /// <summary>
  61. /// Returns the <typeparamref name="TAttribute"/> attribute on the specified `field` or its
  62. /// <see cref="FieldInfo.FieldType"/> or <see cref="MemberInfo.DeclaringType"/>.
  63. /// </summary>
  64. public static TAttribute FindAttribute(FieldInfo field)
  65. {
  66. var attribute = GetAttribute(field);
  67. if (attribute != null)
  68. return attribute;
  69. attribute = GetAttribute(field.FieldType);
  70. if (attribute != null)
  71. return MemberToAttribute[field] = attribute;
  72. attribute = GetAttribute(field.DeclaringType);
  73. if (attribute != null)
  74. return MemberToAttribute[field] = attribute;
  75. return attribute;
  76. }
  77. /************************************************************************************************************************/
  78. /// <summary>[Editor-Only]
  79. /// Returns the <typeparamref name="TAttribute"/> attribute on the underlying field
  80. /// of the `property` or its <see cref="FieldInfo.FieldType"/> or
  81. /// <see cref="MemberInfo.DeclaringType"/> or any of the parent properties
  82. /// or the type of the <see cref="SerializedObject.targetObject"/>.
  83. /// </summary>
  84. public static TAttribute FindAttribute(SerializedProperty property)
  85. {
  86. var accessor = property.GetAccessor();
  87. while (accessor != null)
  88. {
  89. var field = accessor.GetField(property);
  90. var attribute = GetAttribute(field);
  91. if (attribute != null)
  92. return attribute;
  93. var value = accessor.GetValue(property);
  94. if (value != null)
  95. {
  96. attribute = GetAttribute(value.GetType());
  97. if (attribute != null)
  98. return attribute;
  99. }
  100. accessor = accessor.Parent;
  101. }
  102. // If none of the fields of types they are declared in have names, try the actual type of the target.
  103. {
  104. var attribute = GetAttribute(property.serializedObject.targetObject.GetType());
  105. if (attribute != null)
  106. return attribute;
  107. }
  108. return null;
  109. }
  110. /************************************************************************************************************************/
  111. }
  112. }
  113. #endif