PropertyResolver.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. namespace UnityTest
  8. {
  9. [Serializable]
  10. public class PropertyResolver
  11. {
  12. public string[] ExcludedFieldNames { get; set; }
  13. public Type[] ExcludedTypes { get; set; }
  14. public Type[] AllowedTypes { get; set; }
  15. public PropertyResolver ()
  16. {
  17. ExcludedFieldNames = new string[] {};
  18. ExcludedTypes = new Type[] {};
  19. AllowedTypes = new Type[] {};
  20. }
  21. public IList<string> GetFieldsAndPropertiesUnderPath(GameObject go, string propertPath)
  22. {
  23. propertPath = propertPath.Trim ();
  24. if (!PropertyPathIsValid (propertPath))
  25. {
  26. throw new ArgumentException("Incorrent property path: " + propertPath);
  27. }
  28. var idx = propertPath.LastIndexOf('.');
  29. if (idx < 0)
  30. {
  31. var components = GetFieldsAndPropertiesFromGameObject(go, 2, null);
  32. return components;
  33. }
  34. var propertyToSearch = propertPath;
  35. Type type = null;
  36. try
  37. {
  38. type = GetPropertyTypeFromString(go, propertyToSearch);
  39. idx = propertPath.Length-1;
  40. }
  41. catch(ArgumentException)
  42. {
  43. try
  44. {
  45. propertyToSearch = propertPath.Substring(0, idx);
  46. type = GetPropertyTypeFromString(go, propertyToSearch);
  47. }
  48. catch (NullReferenceException)
  49. {
  50. var components = GetFieldsAndPropertiesFromGameObject(go, 2, null);
  51. return components.Where(s => s.StartsWith(propertPath.Substring(idx + 1))).ToArray();
  52. }
  53. }
  54. var resultList = new List<string>();
  55. var path = "";
  56. if(propertyToSearch.EndsWith("."))
  57. propertyToSearch = propertyToSearch.Substring(0, propertyToSearch.Length-1);
  58. foreach(var c in propertyToSearch)
  59. {
  60. if(c == '.')
  61. resultList.Add(path);
  62. path += c;
  63. }
  64. resultList.Add(path);
  65. foreach (var prop in type.GetProperties())
  66. {
  67. if (prop.Name.StartsWith(propertPath.Substring(idx + 1)))
  68. resultList.Add(propertyToSearch + "." + prop.Name);
  69. }
  70. foreach (var prop in type.GetFields())
  71. {
  72. if (prop.Name.StartsWith(propertPath.Substring(idx + 1)))
  73. resultList.Add(propertyToSearch + "." + prop.Name);
  74. }
  75. return resultList.ToArray();
  76. }
  77. private bool PropertyPathIsValid ( string propertPath )
  78. {
  79. if (propertPath.StartsWith ("."))
  80. return false;
  81. if (propertPath.IndexOf ("..") >= 0)
  82. return false;
  83. if (Regex.IsMatch (propertPath,
  84. @"\s"))
  85. return false;
  86. return true;
  87. }
  88. public IList<string> GetFieldsAndPropertiesFromGameObject ( GameObject gameObject, int depthOfSearch, string extendPath )
  89. {
  90. if(depthOfSearch<1) throw new ArgumentOutOfRangeException("depthOfSearch need to be greater than 0");
  91. var goVals = GetPropertiesAndFieldsFromType(typeof(GameObject),
  92. depthOfSearch - 1).Select(s => "gameObject." + s);
  93. var result = new List<string>();
  94. if (AllowedTypes == null || !AllowedTypes.Any() || AllowedTypes.Contains(typeof(GameObject)))
  95. result.Add("gameObject");
  96. result.AddRange (goVals);
  97. foreach (var componentType in GetAllComponents(gameObject))
  98. {
  99. if (AllowedTypes == null || !AllowedTypes.Any() || AllowedTypes.Contains(componentType))
  100. result.Add(componentType.Name);
  101. if (depthOfSearch > 1)
  102. {
  103. var vals = GetPropertiesAndFieldsFromType (componentType,
  104. depthOfSearch - 1 );
  105. var valsFullName = vals.Select (s => componentType.Name + "." + s);
  106. result.AddRange (valsFullName);
  107. }
  108. }
  109. if (!string.IsNullOrEmpty (extendPath))
  110. {
  111. var pathType = GetPropertyTypeFromString (gameObject,
  112. extendPath);
  113. var vals = GetPropertiesAndFieldsFromType (pathType,
  114. depthOfSearch - 1);
  115. var valsFullName = vals.Select (s => extendPath + "." + s);
  116. result.AddRange (valsFullName);
  117. }
  118. return result;
  119. }
  120. private string[] GetPropertiesAndFieldsFromType ( Type type, int level )
  121. {
  122. level--;
  123. var result = new List<string>();
  124. var fields = new List<MemberInfo>();
  125. fields.AddRange(type.GetFields());
  126. fields.AddRange(type.GetProperties());
  127. foreach (var member in fields)
  128. {
  129. var memberType = GetMemberFieldType(member);
  130. var memberTypeName = memberType.Name;
  131. if (AllowedTypes == null
  132. ||!AllowedTypes.Any()
  133. || (AllowedTypes.Contains(memberType) && !ExcludedFieldNames.Contains(memberTypeName))
  134. )
  135. {
  136. result.Add(member.Name);
  137. }
  138. if (level > 0 && IsTypeOrNameNotExcluded(memberType, memberTypeName))
  139. {
  140. var vals = GetPropertiesAndFieldsFromType(memberType,
  141. level);
  142. var valsFullName = vals.Select(s => member.Name + "." + s);
  143. result.AddRange(valsFullName);
  144. }
  145. }
  146. return result.ToArray();
  147. }
  148. private Type GetMemberFieldType ( MemberInfo info )
  149. {
  150. if (info.MemberType == MemberTypes.Property)
  151. return (info as PropertyInfo).PropertyType;
  152. if (info.MemberType == MemberTypes.Field)
  153. return (info as FieldInfo).FieldType;
  154. throw new Exception("Only properties and fields are allowed");
  155. }
  156. private Type[] GetAllComponents ( GameObject gameObject )
  157. {
  158. var result = new List<Type>();
  159. var components = gameObject.GetComponents(typeof(Component));
  160. foreach (var component in components)
  161. {
  162. var componentType = component.GetType();
  163. if (IsTypeOrNameNotExcluded(componentType, null))
  164. {
  165. result.Add(componentType);
  166. }
  167. }
  168. return result.ToArray();
  169. }
  170. private bool IsTypeOrNameNotExcluded(Type memberType, string memberTypeName)
  171. {
  172. return !ExcludedTypes.Contains(memberType)
  173. && !ExcludedFieldNames.Contains(memberTypeName);
  174. }
  175. #region Static helpers
  176. public static object GetPropertyValueFromString(GameObject gameObj, string propertyPath)
  177. {
  178. if (propertyPath == "")
  179. return gameObj;
  180. var propsQueue = new Queue<string>(propertyPath.Split('.').Where(s => !string.IsNullOrEmpty(s)));
  181. if (propsQueue == null) throw new ArgumentException("Incorrent property path");
  182. object result;
  183. if (char.IsLower(propsQueue.Peek()[0]))
  184. {
  185. result = gameObj;
  186. }
  187. else
  188. {
  189. result = gameObj.GetComponent(propsQueue.Dequeue());
  190. }
  191. Type type = result.GetType();
  192. while (propsQueue.Count != 0)
  193. {
  194. var nameToFind = propsQueue.Dequeue();
  195. var property = type.GetProperty(nameToFind);
  196. if (property != null)
  197. {
  198. result = property.GetGetMethod().Invoke(result,
  199. null);
  200. }
  201. else
  202. {
  203. var field = type.GetField(nameToFind);
  204. result = field.GetValue(result);
  205. }
  206. type = result.GetType();
  207. }
  208. return result;
  209. }
  210. private static Type GetPropertyTypeFromString(GameObject gameObj, string propertyPath)
  211. {
  212. if (propertyPath == "")
  213. return gameObj.GetType();
  214. var propsQueue = new Queue<string>(propertyPath.Split('.').Where(s => !string.IsNullOrEmpty(s)));
  215. if (propsQueue == null) throw new ArgumentException("Incorrent property path");
  216. Type result;
  217. if (char.IsLower(propsQueue.Peek()[0]))
  218. {
  219. result = gameObj.GetType();
  220. }
  221. else
  222. {
  223. var component = gameObj.GetComponent(propsQueue.Dequeue());
  224. if (component == null) throw new ArgumentException("Incorrent property path");
  225. result = component.GetType();
  226. }
  227. while (propsQueue.Count != 0)
  228. {
  229. var nameToFind = propsQueue.Dequeue();
  230. var property = result.GetProperty(nameToFind);
  231. if (property != null)
  232. {
  233. result = property.PropertyType;
  234. }
  235. else
  236. {
  237. var field = result.GetField(nameToFind);
  238. if (field == null) throw new ArgumentException("Incorrent property path");
  239. result = field.FieldType;
  240. }
  241. }
  242. return result;
  243. }
  244. #endregion
  245. }
  246. }