|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +namespace Explorer |
| 6 | +{ |
| 7 | + public static class CacheFactory |
| 8 | + { |
| 9 | + public static CacheObjectBase GetTypeAndCacheObject(object obj) |
| 10 | + => GetTypeAndCacheObject(obj, null, null); |
| 11 | + |
| 12 | + public static CacheObjectBase GetTypeAndCacheObject(MemberInfo memberInfo, object declarer) |
| 13 | + => GetTypeAndCacheObject(null, memberInfo, declarer); |
| 14 | + |
| 15 | + public static CacheObjectBase GetTypeAndCacheObject(object obj, MemberInfo memberInfo, object declarer) |
| 16 | + { |
| 17 | + Type type = null; |
| 18 | + |
| 19 | + if (memberInfo != null) |
| 20 | + { |
| 21 | + if (memberInfo is FieldInfo fi) |
| 22 | + { |
| 23 | + type = fi.FieldType; |
| 24 | + } |
| 25 | + else if (memberInfo is PropertyInfo pi) |
| 26 | + { |
| 27 | + type = pi.PropertyType; |
| 28 | + } |
| 29 | + else if (memberInfo is MethodInfo mi) |
| 30 | + { |
| 31 | + type = mi.ReturnType; |
| 32 | + } |
| 33 | + } |
| 34 | + else if (obj != null) |
| 35 | + { |
| 36 | + type = ReflectionHelpers.GetActualType(obj); |
| 37 | + } |
| 38 | + |
| 39 | + if (type == null) |
| 40 | + { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + return GetCacheObject(obj, memberInfo, declarer, type); |
| 45 | + } |
| 46 | + |
| 47 | + public static CacheObjectBase GetCacheObject(object obj, Type valueType) |
| 48 | + => GetCacheObject(obj, null, null, valueType); |
| 49 | + |
| 50 | + private static CacheObjectBase GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance, Type valueType) |
| 51 | + { |
| 52 | + CacheObjectBase cached; |
| 53 | + |
| 54 | + var pi = memberInfo as PropertyInfo; |
| 55 | + var mi = memberInfo as MethodInfo; |
| 56 | + |
| 57 | + // Check if can process args |
| 58 | + if ((pi != null && !CanProcessArgs(pi.GetIndexParameters())) |
| 59 | + || (mi != null && !CanProcessArgs(mi.GetParameters()))) |
| 60 | + { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + if (mi != null) |
| 65 | + { |
| 66 | + cached = new CacheMethod(); |
| 67 | + } |
| 68 | + else if (valueType == typeof(GameObject) || valueType == typeof(Transform)) |
| 69 | + { |
| 70 | + cached = new CacheGameObject(); |
| 71 | + } |
| 72 | + else if (valueType.IsPrimitive || valueType == typeof(string)) |
| 73 | + { |
| 74 | + cached = new CachePrimitive(); |
| 75 | + } |
| 76 | + else if (valueType.IsEnum) |
| 77 | + { |
| 78 | + if (valueType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] attributes && attributes.Length > 0) |
| 79 | + { |
| 80 | + cached = new CacheEnumFlags(); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + cached = new CacheEnum(); |
| 85 | + } |
| 86 | + } |
| 87 | + else if (valueType == typeof(Vector2) || valueType == typeof(Vector3) || valueType == typeof(Vector4)) |
| 88 | + { |
| 89 | + cached = new CacheVector(); |
| 90 | + } |
| 91 | + else if (valueType == typeof(Quaternion)) |
| 92 | + { |
| 93 | + cached = new CacheQuaternion(); |
| 94 | + } |
| 95 | + else if (valueType == typeof(Color)) |
| 96 | + { |
| 97 | + cached = new CacheColor(); |
| 98 | + } |
| 99 | + else if (valueType == typeof(Rect)) |
| 100 | + { |
| 101 | + cached = new CacheRect(); |
| 102 | + } |
| 103 | + // must check this before IsEnumerable |
| 104 | + else if (ReflectionHelpers.IsDictionary(valueType)) |
| 105 | + { |
| 106 | + cached = new CacheDictionary(); |
| 107 | + } |
| 108 | + else if (ReflectionHelpers.IsEnumerable(valueType)) |
| 109 | + { |
| 110 | + cached = new CacheList(); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + cached = new CacheOther(); |
| 115 | + } |
| 116 | + |
| 117 | + cached.Value = obj; |
| 118 | + cached.ValueType = valueType; |
| 119 | + |
| 120 | + if (memberInfo != null) |
| 121 | + { |
| 122 | + cached.MemInfo = memberInfo; |
| 123 | + cached.DeclaringType = memberInfo.DeclaringType; |
| 124 | + cached.DeclaringInstance = declaringInstance; |
| 125 | + } |
| 126 | + |
| 127 | + if (pi != null) |
| 128 | + { |
| 129 | + cached.m_arguments = pi.GetIndexParameters(); |
| 130 | + } |
| 131 | + else if (mi != null) |
| 132 | + { |
| 133 | + cached.m_arguments = mi.GetParameters(); |
| 134 | + } |
| 135 | + |
| 136 | + cached.m_argumentInput = new string[cached.m_arguments.Length]; |
| 137 | + |
| 138 | + cached.UpdateValue(); |
| 139 | + |
| 140 | + cached.Init(); |
| 141 | + |
| 142 | + return cached; |
| 143 | + } |
| 144 | + |
| 145 | + public static bool CanProcessArgs(ParameterInfo[] parameters) |
| 146 | + { |
| 147 | + foreach (var param in parameters) |
| 148 | + { |
| 149 | + var pType = param.ParameterType; |
| 150 | + |
| 151 | + if (pType.IsByRef && pType.HasElementType) |
| 152 | + { |
| 153 | + pType = pType.GetElementType(); |
| 154 | + } |
| 155 | + |
| 156 | + if (pType.IsPrimitive || pType == typeof(string)) |
| 157 | + { |
| 158 | + continue; |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + return false; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return true; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments