2020-08-29 21:15:54 +10:00
|
|
|
|
using System;
|
2020-09-06 21:33:09 +10:00
|
|
|
|
using System.Collections;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MelonLoader;
|
|
|
|
|
using UnityEngine;
|
2020-09-06 21:33:09 +10:00
|
|
|
|
using System.Reflection;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
|
|
|
|
namespace Explorer
|
|
|
|
|
{
|
2020-09-07 17:05:37 +10:00
|
|
|
|
public class CacheDictionary : CacheObjectBase, IExpandHeight
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-09-06 21:33:09 +10:00
|
|
|
|
public bool IsExpanded { get; set; }
|
2020-09-07 17:05:37 +10:00
|
|
|
|
public float WhiteSpace { get; set; } = 215f;
|
|
|
|
|
public float ButtonWidthOffset { get; set; } = 290f;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-07 17:05:37 +10:00
|
|
|
|
public PageHelper Pages = new PageHelper();
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-06 21:33:09 +10:00
|
|
|
|
private CacheObjectBase[] m_cachedKeys;
|
|
|
|
|
private CacheObjectBase[] m_cachedValues;
|
|
|
|
|
|
|
|
|
|
public Type TypeOfKeys
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (m_keysType == null) GetGenericArguments();
|
|
|
|
|
return m_keysType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private Type m_keysType;
|
|
|
|
|
|
|
|
|
|
public Type TypeOfValues
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (m_valuesType == null) GetGenericArguments();
|
|
|
|
|
return m_valuesType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private Type m_valuesType;
|
|
|
|
|
|
|
|
|
|
public IDictionary IDict
|
|
|
|
|
{
|
|
|
|
|
get => m_iDictionary ?? (m_iDictionary = Value as IDictionary) ?? Il2CppDictionaryToMono();
|
|
|
|
|
set => m_iDictionary = value;
|
|
|
|
|
}
|
|
|
|
|
private IDictionary m_iDictionary;
|
|
|
|
|
|
2020-09-07 17:05:37 +10:00
|
|
|
|
// ========== Methods ==========
|
|
|
|
|
|
2020-09-06 21:33:09 +10:00
|
|
|
|
// This is a bit janky due to Il2Cpp Dictionary not implementing IDictionary.
|
|
|
|
|
private IDictionary Il2CppDictionaryToMono()
|
|
|
|
|
{
|
|
|
|
|
// note: "ValueType" is the Dictionary itself, TypeOfValues is the 'Dictionary.Values' type.
|
|
|
|
|
|
|
|
|
|
// make generic dictionary from key and value type
|
|
|
|
|
var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>)
|
2020-09-07 03:25:43 +10:00
|
|
|
|
.MakeGenericType(TypeOfKeys, TypeOfValues));
|
2020-09-06 21:33:09 +10:00
|
|
|
|
|
|
|
|
|
// get keys and values
|
|
|
|
|
var keys = ValueType.GetProperty("Keys") .GetValue(Value);
|
|
|
|
|
var values = ValueType.GetProperty("Values").GetValue(Value);
|
|
|
|
|
|
2020-09-07 03:25:43 +10:00
|
|
|
|
// create lists to hold them
|
|
|
|
|
var keyList = new List<object>();
|
2020-09-06 21:33:09 +10:00
|
|
|
|
var valueList = new List<object>();
|
|
|
|
|
|
|
|
|
|
// get keys enumerator and store keys
|
|
|
|
|
var keyEnumerator = keys.GetType().GetMethod("GetEnumerator").Invoke(keys, null);
|
|
|
|
|
var keyCollectionType = keyEnumerator.GetType();
|
|
|
|
|
var keyMoveNext = keyCollectionType.GetMethod("MoveNext");
|
|
|
|
|
var keyCurrent = keyCollectionType.GetProperty("Current");
|
|
|
|
|
while ((bool)keyMoveNext.Invoke(keyEnumerator, null))
|
|
|
|
|
{
|
|
|
|
|
keyList.Add(keyCurrent.GetValue(keyEnumerator));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get values enumerator and store values
|
|
|
|
|
var valueEnumerator = values.GetType().GetMethod("GetEnumerator").Invoke(values, null);
|
|
|
|
|
var valueCollectionType = valueEnumerator.GetType();
|
|
|
|
|
var valueMoveNext = valueCollectionType.GetMethod("MoveNext");
|
|
|
|
|
var valueCurrent = valueCollectionType.GetProperty("Current");
|
|
|
|
|
while ((bool)valueMoveNext.Invoke(valueEnumerator, null))
|
|
|
|
|
{
|
|
|
|
|
valueList.Add(valueCurrent.GetValue(valueEnumerator));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// finally iterate into actual dictionary
|
|
|
|
|
for (int i = 0; i < keyList.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
dict.Add(keyList[i], valueList[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetGenericArguments()
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-09-07 17:05:37 +10:00
|
|
|
|
if (this.MemInfo != null)
|
2020-09-06 21:33:09 +10:00
|
|
|
|
{
|
2020-09-07 17:05:37 +10:00
|
|
|
|
Type memberType = null;
|
|
|
|
|
switch (this.MemInfo.MemberType)
|
2020-09-06 21:33:09 +10:00
|
|
|
|
{
|
2020-09-07 17:05:37 +10:00
|
|
|
|
case MemberTypes.Field:
|
|
|
|
|
memberType = (MemInfo as FieldInfo).FieldType;
|
|
|
|
|
break;
|
|
|
|
|
case MemberTypes.Property:
|
|
|
|
|
memberType = (MemInfo as PropertyInfo).PropertyType;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-07 17:05:37 +10:00
|
|
|
|
if (memberType != null && memberType.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
m_keysType = memberType.GetGenericArguments()[0];
|
|
|
|
|
m_valuesType = memberType.GetGenericArguments()[1];
|
2020-09-06 21:33:09 +10:00
|
|
|
|
}
|
2020-09-07 17:05:37 +10:00
|
|
|
|
}
|
|
|
|
|
else if (Value != null)
|
|
|
|
|
{
|
|
|
|
|
var type = Value.GetType();
|
|
|
|
|
if (type.IsGenericType)
|
2020-09-06 21:33:09 +10:00
|
|
|
|
{
|
2020-09-07 17:05:37 +10:00
|
|
|
|
m_keysType = type.GetGenericArguments()[0];
|
|
|
|
|
m_valuesType = type.GetGenericArguments()[1];
|
2020-09-06 21:33:09 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void UpdateValue()
|
|
|
|
|
{
|
2020-09-06 21:33:09 +10:00
|
|
|
|
base.UpdateValue();
|
|
|
|
|
|
|
|
|
|
// reset
|
|
|
|
|
IDict = null;
|
|
|
|
|
|
|
|
|
|
if (Value == null || IDict == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-06 21:33:09 +10:00
|
|
|
|
var keys = new List<CacheObjectBase>();
|
|
|
|
|
foreach (var key in IDict.Keys)
|
|
|
|
|
{
|
|
|
|
|
var cache = GetCacheObject(key, TypeOfKeys);
|
|
|
|
|
cache.UpdateValue();
|
|
|
|
|
keys.Add(cache);
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-06 21:33:09 +10:00
|
|
|
|
var values = new List<CacheObjectBase>();
|
|
|
|
|
foreach (var val in IDict.Values)
|
|
|
|
|
{
|
|
|
|
|
var cache = GetCacheObject(val, TypeOfValues);
|
|
|
|
|
cache.UpdateValue();
|
|
|
|
|
values.Add(cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_cachedKeys = keys.ToArray();
|
|
|
|
|
m_cachedValues = values.ToArray();
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 21:33:09 +10:00
|
|
|
|
// ============= GUI Draw =============
|
|
|
|
|
|
2020-08-29 21:15:54 +10:00
|
|
|
|
public override void DrawValue(Rect window, float width)
|
|
|
|
|
{
|
2020-09-06 21:33:09 +10:00
|
|
|
|
if (m_cachedKeys == null || m_cachedValues == null)
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Label("Cached keys or values is null!", null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int count = m_cachedKeys.Length;
|
|
|
|
|
|
|
|
|
|
if (!IsExpanded)
|
|
|
|
|
{
|
|
|
|
|
if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) }))
|
|
|
|
|
{
|
|
|
|
|
IsExpanded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) }))
|
|
|
|
|
{
|
|
|
|
|
IsExpanded = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
|
|
|
|
|
string btnLabel = $"<color=yellow>[{count}] Dictionary<{TypeOfKeys.FullName}, {TypeOfValues.FullName}></color>";
|
|
|
|
|
if (GUILayout.Button(btnLabel, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - ButtonWidthOffset) }))
|
|
|
|
|
{
|
|
|
|
|
WindowManager.InspectObject(Value, out bool _);
|
|
|
|
|
}
|
|
|
|
|
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
|
|
|
|
|
|
|
|
|
|
GUILayout.Space(5);
|
|
|
|
|
|
|
|
|
|
if (IsExpanded)
|
|
|
|
|
{
|
|
|
|
|
float whitespace = WhiteSpace;
|
|
|
|
|
if (whitespace > 0)
|
|
|
|
|
{
|
|
|
|
|
ClampLabelWidth(window, ref whitespace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pages.ItemCount = count;
|
|
|
|
|
|
|
|
|
|
if (count > Pages.ItemsPerPage)
|
|
|
|
|
{
|
|
|
|
|
GUILayout.EndHorizontal();
|
|
|
|
|
GUILayout.BeginHorizontal(null);
|
|
|
|
|
|
|
|
|
|
GUILayout.Space(whitespace);
|
|
|
|
|
|
|
|
|
|
Pages.CurrentPageLabel();
|
|
|
|
|
|
|
|
|
|
// prev/next page buttons
|
|
|
|
|
if (GUILayout.Button("< Prev", new GUILayoutOption[] { GUILayout.Width(60) }))
|
|
|
|
|
{
|
|
|
|
|
Pages.TurnPage(Turn.Left);
|
|
|
|
|
}
|
|
|
|
|
if (GUILayout.Button("Next >", new GUILayoutOption[] { GUILayout.Width(60) }))
|
|
|
|
|
{
|
|
|
|
|
Pages.TurnPage(Turn.Right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pages.DrawLimitInputArea();
|
|
|
|
|
|
|
|
|
|
GUILayout.Space(5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int offset = Pages.CalculateOffsetIndex();
|
|
|
|
|
|
|
|
|
|
for (int i = offset; i < offset + Pages.ItemsPerPage && i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var key = m_cachedKeys[i];
|
|
|
|
|
var val = m_cachedValues[i];
|
|
|
|
|
|
|
|
|
|
//collapsing the BeginHorizontal called from ReflectionWindow.WindowFunction or previous array entry
|
|
|
|
|
GUILayout.EndHorizontal();
|
|
|
|
|
GUILayout.BeginHorizontal(null);
|
|
|
|
|
|
|
|
|
|
//GUILayout.Space(whitespace);
|
|
|
|
|
|
|
|
|
|
if (key == null || val == null)
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Label($"[{i}] <i><color=grey>(null)</color></i>", null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
|
|
|
|
|
GUILayout.Label($"[{i}]", new GUILayoutOption[] { GUILayout.Width(30) });
|
|
|
|
|
|
|
|
|
|
GUILayout.Label("Key:", new GUILayoutOption[] { GUILayout.Width(40) });
|
|
|
|
|
key.DrawValue(window, (window.width / 2) - 30f);
|
|
|
|
|
|
|
|
|
|
GUILayout.Label("Value:", new GUILayoutOption[] { GUILayout.Width(40) });
|
|
|
|
|
val.DrawValue(window, (window.width / 2) - 30f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GUI.skin.label.alignment = TextAnchor.UpperLeft;
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|