SRMonoBehaviourEx.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // ReSharper disable once RedundantUsingDirective
  2. using System.Linq;
  3. namespace SRF
  4. {
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using Helpers;
  9. using Service;
  10. using UnityEngine;
  11. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field)]
  12. public sealed class RequiredFieldAttribute : Attribute
  13. {
  14. private bool _autoCreate;
  15. private bool _autoSearch;
  16. private bool _editorOnly = true;
  17. public RequiredFieldAttribute(bool autoSearch)
  18. {
  19. AutoSearch = autoSearch;
  20. }
  21. public RequiredFieldAttribute() {}
  22. public bool AutoSearch
  23. {
  24. get { return _autoSearch; }
  25. set { _autoSearch = value; }
  26. }
  27. public bool AutoCreate
  28. {
  29. get { return _autoCreate; }
  30. set { _autoCreate = value; }
  31. }
  32. [Obsolete]
  33. public bool EditorOnly
  34. {
  35. get { return _editorOnly; }
  36. set { _editorOnly = value; }
  37. }
  38. }
  39. /// <summary>
  40. /// Add to a field to attempt to use SRServiceManager to get an instance of the field type
  41. /// </summary>
  42. [AttributeUsage(AttributeTargets.Field)]
  43. public class ImportAttribute : Attribute
  44. {
  45. public readonly Type Service;
  46. public ImportAttribute() {}
  47. public ImportAttribute(Type serviceType)
  48. {
  49. Service = serviceType;
  50. }
  51. }
  52. public abstract class SRMonoBehaviourEx : SRMonoBehaviour
  53. {
  54. private static Dictionary<Type, IList<FieldInfo>> _checkedFields;
  55. private static void CheckFields(SRMonoBehaviourEx instance, bool justSet = false)
  56. {
  57. if (_checkedFields == null)
  58. {
  59. _checkedFields = new Dictionary<Type, IList<FieldInfo>>();
  60. }
  61. var t = instance.GetType();
  62. IList<FieldInfo> cache;
  63. if (!_checkedFields.TryGetValue(instance.GetType(), out cache))
  64. {
  65. cache = ScanType(t);
  66. _checkedFields.Add(t, cache);
  67. }
  68. PopulateObject(cache, instance, justSet);
  69. }
  70. private static void PopulateObject(IList<FieldInfo> cache, SRMonoBehaviourEx instance, bool justSet)
  71. {
  72. for (var i = 0; i < cache.Count; i++)
  73. {
  74. var f = cache[i];
  75. if (!EqualityComparer<object>.Default.Equals(f.Field.GetValue(instance), null))
  76. {
  77. continue;
  78. }
  79. // If import is enabled, use SRServiceManager to import the reference
  80. if (f.Import)
  81. {
  82. var t = f.ImportType ?? f.Field.FieldType;
  83. var service = SRServiceManager.GetService(t);
  84. if (service == null)
  85. {
  86. Debug.LogWarning("Field {0} import failed (Type {1})".Fmt(f.Field.Name, t));
  87. continue;
  88. }
  89. f.Field.SetValue(instance, service);
  90. continue;
  91. }
  92. // If autoset is enabled on field, try and find the component on the GameObject
  93. if (f.AutoSet)
  94. {
  95. var newValue = instance.GetComponent(f.Field.FieldType);
  96. if (!EqualityComparer<object>.Default.Equals(newValue, null))
  97. {
  98. f.Field.SetValue(instance, newValue);
  99. continue;
  100. }
  101. }
  102. if (justSet)
  103. {
  104. continue;
  105. }
  106. if (f.AutoCreate)
  107. {
  108. var newValue = instance.CachedGameObject.AddComponent(f.Field.FieldType);
  109. f.Field.SetValue(instance, newValue);
  110. }
  111. throw new UnassignedReferenceException(
  112. "Field {0} is unassigned, but marked with RequiredFieldAttribute".Fmt(f.Field.Name));
  113. }
  114. }
  115. private static List<FieldInfo> ScanType(Type t)
  116. {
  117. var cache = new List<FieldInfo>();
  118. // Check for attribute added to the class
  119. var globalAttr = SRReflection.GetAttribute<RequiredFieldAttribute>(t);
  120. #if NETFX_CORE
  121. var fields = t.GetTypeInfo().DeclaredFields.Where(f => !f.IsStatic);
  122. #else
  123. // Check each field for the attribute
  124. var fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  125. #endif
  126. foreach (var f in fields)
  127. {
  128. var requiredFieldAttribute = SRReflection.GetAttribute<RequiredFieldAttribute>(f);
  129. var importAttribute = SRReflection.GetAttribute<ImportAttribute>(f);
  130. if (globalAttr == null && requiredFieldAttribute == null && importAttribute == null)
  131. {
  132. continue; // Early out if no attributes found.
  133. }
  134. var info = new FieldInfo();
  135. info.Field = f;
  136. if (importAttribute != null)
  137. {
  138. info.Import = true;
  139. info.ImportType = importAttribute.Service;
  140. }
  141. else if (requiredFieldAttribute != null)
  142. {
  143. info.AutoSet = requiredFieldAttribute.AutoSearch;
  144. info.AutoCreate = requiredFieldAttribute.AutoCreate;
  145. }
  146. else
  147. {
  148. info.AutoSet = globalAttr.AutoSearch;
  149. info.AutoCreate = globalAttr.AutoCreate;
  150. }
  151. cache.Add(info);
  152. }
  153. return cache;
  154. }
  155. protected virtual void Awake()
  156. {
  157. CheckFields(this);
  158. }
  159. protected virtual void Start() {}
  160. protected virtual void Update() {}
  161. protected virtual void FixedUpdate() {}
  162. protected virtual void OnEnable() {}
  163. protected virtual void OnDisable() {}
  164. protected virtual void OnDestroy() {}
  165. private struct FieldInfo
  166. {
  167. public bool AutoCreate;
  168. public bool AutoSet;
  169. public System.Reflection.FieldInfo Field;
  170. public bool Import;
  171. public Type ImportType;
  172. }
  173. }
  174. }