|
1 | 1 | using System;
|
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic;
|
3 | 4 | using System.Linq;
|
4 | 5 | using System.Text;
|
5 | 6 | using System.Threading.Tasks;
|
6 | 7 | using MelonLoader;
|
7 | 8 | using UnityEngine;
|
| 9 | +using System.Reflection; |
8 | 10 |
|
9 | 11 | namespace Explorer
|
10 | 12 | {
|
11 | 13 | public class CacheDictionary : CacheObjectBase
|
12 | 14 | {
|
| 15 | + public bool IsExpanded { get; set; } |
| 16 | + public PageHelper Pages = new PageHelper(); |
13 | 17 |
|
| 18 | + public float WhiteSpace = 215f; |
| 19 | + public float ButtonWidthOffset = 290f; |
14 | 20 |
|
15 |
| - public override void Init() |
| 21 | + private CacheObjectBase[] m_cachedKeys; |
| 22 | + private CacheObjectBase[] m_cachedValues; |
| 23 | + |
| 24 | + public Type TypeOfKeys |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + if (m_keysType == null) GetGenericArguments(); |
| 29 | + return m_keysType; |
| 30 | + } |
| 31 | + } |
| 32 | + private Type m_keysType; |
| 33 | + |
| 34 | + public Type TypeOfValues |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + if (m_valuesType == null) GetGenericArguments(); |
| 39 | + return m_valuesType; |
| 40 | + } |
| 41 | + } |
| 42 | + private Type m_valuesType; |
| 43 | + |
| 44 | + public IDictionary IDict |
| 45 | + { |
| 46 | + get => m_iDictionary ?? (m_iDictionary = Value as IDictionary) ?? Il2CppDictionaryToMono(); |
| 47 | + set => m_iDictionary = value; |
| 48 | + } |
| 49 | + private IDictionary m_iDictionary; |
| 50 | + |
| 51 | + // This is a bit janky due to Il2Cpp Dictionary not implementing IDictionary. |
| 52 | + private IDictionary Il2CppDictionaryToMono() |
| 53 | + { |
| 54 | + // note: "ValueType" is the Dictionary itself, TypeOfValues is the 'Dictionary.Values' type. |
| 55 | + |
| 56 | + // make generic dictionary from key and value type |
| 57 | + var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>) |
| 58 | + .MakeGenericType(TypeOfKeys, TypeOfValues)); |
| 59 | + |
| 60 | + // get keys and values |
| 61 | + var keys = ValueType.GetProperty("Keys") .GetValue(Value); |
| 62 | + var values = ValueType.GetProperty("Values").GetValue(Value); |
| 63 | + |
| 64 | + // create a list to hold them |
| 65 | + var keyList = new List<object>(); |
| 66 | + var valueList = new List<object>(); |
| 67 | + |
| 68 | + // get keys enumerator and store keys |
| 69 | + var keyEnumerator = keys.GetType().GetMethod("GetEnumerator").Invoke(keys, null); |
| 70 | + var keyCollectionType = keyEnumerator.GetType(); |
| 71 | + var keyMoveNext = keyCollectionType.GetMethod("MoveNext"); |
| 72 | + var keyCurrent = keyCollectionType.GetProperty("Current"); |
| 73 | + while ((bool)keyMoveNext.Invoke(keyEnumerator, null)) |
| 74 | + { |
| 75 | + keyList.Add(keyCurrent.GetValue(keyEnumerator)); |
| 76 | + } |
| 77 | + |
| 78 | + // get values enumerator and store values |
| 79 | + var valueEnumerator = values.GetType().GetMethod("GetEnumerator").Invoke(values, null); |
| 80 | + var valueCollectionType = valueEnumerator.GetType(); |
| 81 | + var valueMoveNext = valueCollectionType.GetMethod("MoveNext"); |
| 82 | + var valueCurrent = valueCollectionType.GetProperty("Current"); |
| 83 | + while ((bool)valueMoveNext.Invoke(valueEnumerator, null)) |
| 84 | + { |
| 85 | + valueList.Add(valueCurrent.GetValue(valueEnumerator)); |
| 86 | + } |
| 87 | + |
| 88 | + // finally iterate into actual dictionary |
| 89 | + for (int i = 0; i < keyList.Count; i++) |
| 90 | + { |
| 91 | + dict.Add(keyList[i], valueList[i]); |
| 92 | + } |
| 93 | + |
| 94 | + return dict; |
| 95 | + } |
| 96 | + |
| 97 | + // ========== Methods ========== |
| 98 | + |
| 99 | + private void GetGenericArguments() |
16 | 100 | {
|
17 |
| - //base.Init(); |
| 101 | + if (m_keysType == null || m_valuesType == null) |
| 102 | + { |
| 103 | + if (this.MemInfo != null) |
| 104 | + { |
| 105 | + Type memberType = null; |
| 106 | + switch (this.MemInfo.MemberType) |
| 107 | + { |
| 108 | + case MemberTypes.Field: |
| 109 | + memberType = (MemInfo as FieldInfo).FieldType; |
| 110 | + break; |
| 111 | + case MemberTypes.Property: |
| 112 | + memberType = (MemInfo as PropertyInfo).PropertyType; |
| 113 | + break; |
| 114 | + } |
18 | 115 |
|
19 |
| - Value = "Unsupported"; |
| 116 | + if (memberType != null && memberType.IsGenericType) |
| 117 | + { |
| 118 | + m_keysType = memberType.GetGenericArguments()[0]; |
| 119 | + m_valuesType = memberType.GetGenericArguments()[1]; |
| 120 | + } |
| 121 | + } |
| 122 | + else if (Value != null) |
| 123 | + { |
| 124 | + var type = Value.GetType(); |
| 125 | + if (type.IsGenericType) |
| 126 | + { |
| 127 | + m_keysType = type.GetGenericArguments()[0]; |
| 128 | + m_valuesType = type.GetGenericArguments()[1]; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return; |
20 | 134 | }
|
21 | 135 |
|
22 | 136 | public override void UpdateValue()
|
23 | 137 | {
|
24 |
| - //base.UpdateValue(); |
| 138 | + base.UpdateValue(); |
| 139 | + |
| 140 | + // reset |
| 141 | + IDict = null; |
| 142 | + |
| 143 | + if (Value == null || IDict == null) |
| 144 | + { |
| 145 | + return; |
| 146 | + } |
25 | 147 |
|
| 148 | + var keys = new List<CacheObjectBase>(); |
| 149 | + foreach (var key in IDict.Keys) |
| 150 | + { |
| 151 | + var cache = GetCacheObject(key, TypeOfKeys); |
| 152 | + cache.UpdateValue(); |
| 153 | + keys.Add(cache); |
| 154 | + } |
26 | 155 |
|
| 156 | + var values = new List<CacheObjectBase>(); |
| 157 | + foreach (var val in IDict.Values) |
| 158 | + { |
| 159 | + var cache = GetCacheObject(val, TypeOfValues); |
| 160 | + cache.UpdateValue(); |
| 161 | + values.Add(cache); |
| 162 | + } |
| 163 | + |
| 164 | + m_cachedKeys = keys.ToArray(); |
| 165 | + m_cachedValues = values.ToArray(); |
27 | 166 | }
|
28 | 167 |
|
| 168 | + // ============= GUI Draw ============= |
| 169 | + |
29 | 170 | public override void DrawValue(Rect window, float width)
|
30 | 171 | {
|
31 |
| - GUILayout.Label("<color=red>Dictionary (unsupported)</color>", null); |
| 172 | + if (m_cachedKeys == null || m_cachedValues == null) |
| 173 | + { |
| 174 | + GUILayout.Label("Cached keys or values is null!", null); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + int count = m_cachedKeys.Length; |
| 179 | + |
| 180 | + if (!IsExpanded) |
| 181 | + { |
| 182 | + if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) })) |
| 183 | + { |
| 184 | + IsExpanded = true; |
| 185 | + } |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) })) |
| 190 | + { |
| 191 | + IsExpanded = false; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + GUI.skin.button.alignment = TextAnchor.MiddleLeft; |
| 196 | + string btnLabel = $"<color=yellow>[{count}] Dictionary<{TypeOfKeys.FullName}, {TypeOfValues.FullName}></color>"; |
| 197 | + if (GUILayout.Button(btnLabel, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - ButtonWidthOffset) })) |
| 198 | + { |
| 199 | + WindowManager.InspectObject(Value, out bool _); |
| 200 | + } |
| 201 | + GUI.skin.button.alignment = TextAnchor.MiddleCenter; |
| 202 | + |
| 203 | + GUILayout.Space(5); |
| 204 | + |
| 205 | + if (IsExpanded) |
| 206 | + { |
| 207 | + float whitespace = WhiteSpace; |
| 208 | + if (whitespace > 0) |
| 209 | + { |
| 210 | + ClampLabelWidth(window, ref whitespace); |
| 211 | + } |
| 212 | + |
| 213 | + Pages.ItemCount = count; |
| 214 | + |
| 215 | + if (count > Pages.ItemsPerPage) |
| 216 | + { |
| 217 | + GUILayout.EndHorizontal(); |
| 218 | + GUILayout.BeginHorizontal(null); |
| 219 | + |
| 220 | + GUILayout.Space(whitespace); |
| 221 | + |
| 222 | + Pages.CurrentPageLabel(); |
| 223 | + |
| 224 | + // prev/next page buttons |
| 225 | + if (GUILayout.Button("< Prev", new GUILayoutOption[] { GUILayout.Width(60) })) |
| 226 | + { |
| 227 | + Pages.TurnPage(Turn.Left); |
| 228 | + } |
| 229 | + if (GUILayout.Button("Next >", new GUILayoutOption[] { GUILayout.Width(60) })) |
| 230 | + { |
| 231 | + Pages.TurnPage(Turn.Right); |
| 232 | + } |
| 233 | + |
| 234 | + Pages.DrawLimitInputArea(); |
| 235 | + |
| 236 | + GUILayout.Space(5); |
| 237 | + } |
| 238 | + |
| 239 | + int offset = Pages.CalculateOffsetIndex(); |
| 240 | + |
| 241 | + for (int i = offset; i < offset + Pages.ItemsPerPage && i < count; i++) |
| 242 | + { |
| 243 | + var key = m_cachedKeys[i]; |
| 244 | + var val = m_cachedValues[i]; |
| 245 | + |
| 246 | + //collapsing the BeginHorizontal called from ReflectionWindow.WindowFunction or previous array entry |
| 247 | + GUILayout.EndHorizontal(); |
| 248 | + GUILayout.BeginHorizontal(null); |
| 249 | + |
| 250 | + //GUILayout.Space(whitespace); |
| 251 | + |
| 252 | + if (key == null || val == null) |
| 253 | + { |
| 254 | + GUILayout.Label($"[{i}] <i><color=grey>(null)</color></i>", null); |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + GUI.skin.label.alignment = TextAnchor.MiddleCenter; |
| 259 | + GUILayout.Label($"[{i}]", new GUILayoutOption[] { GUILayout.Width(30) }); |
| 260 | + |
| 261 | + GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.MinWidth((window.width / 3) - 60f) }); |
| 262 | + GUILayout.Label("Key:", new GUILayoutOption[] { GUILayout.Width(40) }); |
| 263 | + key.DrawValue(window, (window.width / 2) - 30f); |
| 264 | + GUILayout.EndHorizontal(); |
| 265 | + |
| 266 | + GUILayout.BeginHorizontal(null); |
| 267 | + GUILayout.Label("Value:", new GUILayoutOption[] { GUILayout.Width(40) }); |
| 268 | + val.DrawValue(window, (window.width / 2) - 30f); |
| 269 | + GUILayout.EndHorizontal(); |
| 270 | + } |
| 271 | + |
| 272 | + } |
| 273 | + |
| 274 | + GUI.skin.label.alignment = TextAnchor.UpperLeft; |
| 275 | + } |
32 | 276 | }
|
33 | 277 | }
|
34 | 278 | }
|
0 commit comments