* Fixed a bug on the Reflection window which would prevent primitive values from being applied
* Improved some parts of the Scene Explorer and the Reflection Window interfaces
* Scene Explorer now has "page view" like other lists
* Various minor cleanups and refactorings
This commit is contained in:
sinaioutlander
2020-08-24 01:42:19 +10:00
parent e3d1add090
commit 45b5ce0ef8
15 changed files with 536 additions and 386 deletions

View File

@ -10,21 +10,18 @@ namespace Explorer
{
public class CacheEnum : CacheObject
{
private readonly Type m_enumType;
private readonly string[] m_names;
public Type EnumType;
public string[] EnumNames;
public CacheEnum(object obj)
public override void Init()
{
if (obj != null)
{
m_enumType = obj.GetType();
m_names = Enum.GetNames(m_enumType);
}
EnumType = Value.GetType();
EnumNames = Enum.GetNames(EnumType);
}
public override void DrawValue(Rect window, float width)
{
if (MemberInfo != null)
if (CanWrite)
{
if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.Width(25) }))
{
@ -38,34 +35,18 @@ namespace Explorer
}
}
GUILayout.Label(Value.ToString(), null);
}
public override void SetValue()
{
if (MemberInfo == null)
{
MelonLogger.Log("Trying to SetValue but the MemberInfo is null!");
return;
}
if (Enum.Parse(m_enumType, Value.ToString()) is object enumValue && enumValue != null)
{
Value = enumValue;
}
SetValue(Value, MemberInfo, DeclaringInstance);
GUILayout.Label(Value.ToString(), null);// + "<color=yellow><i> (" + ValueType + ")</i></color>", null);
}
public void SetEnum(ref object value, int change)
{
var names = m_names.ToList();
var names = EnumNames.ToList();
int newindex = names.IndexOf(value.ToString()) + change;
if ((change < 0 && newindex >= 0) || (change > 0 && newindex < names.Count))
{
value = Enum.Parse(m_enumType, names[newindex]);
value = Enum.Parse(EnumType, names[newindex]);
}
}
}

View File

@ -10,44 +10,37 @@ namespace Explorer
{
public class CacheGameObject : CacheObject
{
private GameObject m_gameObject;
public CacheGameObject(object obj)
private GameObject GameObj
{
if (obj != null)
m_gameObject = GetGameObject(obj);
}
private GameObject GetGameObject(object obj)
{
if (obj is Il2CppSystem.Object ilObj)
get
{
var ilType = ilObj.GetIl2CppType();
if (ilType == ReflectionHelpers.GameObjectType || ilType == ReflectionHelpers.TransformType)
if (m_gameObject == null)
{
return ilObj.TryCast<GameObject>() ?? ilObj.TryCast<Transform>()?.gameObject;
}
}
if (Value is Il2CppSystem.Object ilObj)
{
var ilType = ilObj.GetIl2CppType();
return null;
if (ilType == ReflectionHelpers.GameObjectType || ilType == ReflectionHelpers.TransformType)
{
m_gameObject = ilObj.TryCast<GameObject>() ?? ilObj.TryCast<Transform>()?.gameObject;
}
}
}
return m_gameObject;
}
}
private GameObject m_gameObject;
public override void DrawValue(Rect window, float width)
{
UIHelpers.GameobjButton(m_gameObject, null, false, width);
}
public override void SetValue()
{
throw new NotImplementedException("TODO");
UIHelpers.GameobjButton(GameObj, null, false, width);
}
public override void UpdateValue()
{
base.UpdateValue();
m_gameObject = GetGameObject(Value);
}
}
}

View File

@ -14,6 +14,7 @@ namespace Explorer
{
public bool IsExpanded { get; set; }
public int ArrayOffset { get; set; }
public int ArrayLimit { get; set; } = 20;
public Type EntryType
{
@ -47,15 +48,6 @@ namespace Explorer
private IEnumerable m_enumerable;
private CacheObject[] m_cachedEntries;
public CacheList(object obj)
{
if (obj != null)
{
Value = obj;
EntryType = obj.GetType().GetGenericArguments()[0];
}
}
private IEnumerable CppListToEnumerable(object list)
{
if (EntryType == null) return null;
@ -95,12 +87,12 @@ namespace Explorer
if (IsExpanded)
{
if (count > CppExplorer.ArrayLimit)
if (count > ArrayLimit)
{
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Space(190);
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)CppExplorer.ArrayLimit)) - 1;
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)ArrayLimit)) - 1;
GUILayout.Label($"Page {ArrayOffset + 1}/{maxOffset + 1}", new GUILayoutOption[] { GUILayout.Width(80) });
// prev/next page buttons
if (GUILayout.Button("< Prev", null))
@ -111,13 +103,20 @@ namespace Explorer
{
if (ArrayOffset < maxOffset) ArrayOffset++;
}
GUILayout.Label("Limit: ", new GUILayoutOption[] { GUILayout.Width(50) });
var limit = this.ArrayLimit.ToString();
limit = GUILayout.TextField(limit, new GUILayoutOption[] { GUILayout.Width(50) });
if (limit != ArrayLimit.ToString() && int.TryParse(limit, out int i))
{
ArrayLimit = i;
}
}
int offset = ArrayOffset * CppExplorer.ArrayLimit;
int offset = ArrayOffset * ArrayLimit;
if (offset >= count) offset = 0;
for (int i = offset; i < offset + CppExplorer.ArrayLimit && i < count; i++)
for (int i = offset; i < offset + ArrayLimit && i < count; i++)
{
var entry = m_cachedEntries[i];
@ -140,11 +139,6 @@ namespace Explorer
}
}
public override void SetValue()
{
throw new NotImplementedException("TODO");
}
/// <summary>
/// Called when the user presses the "Update" button, or if AutoUpdate is on.
/// </summary>
@ -161,7 +155,7 @@ namespace Explorer
var list = new List<CacheObject>();
while (enumerator.MoveNext())
{
list.Add(GetCacheObject(enumerator.Current));
list.Add(GetCacheObject(enumerator.Current, null, null, this.EntryType));
}
m_cachedEntries = list.ToArray();

View File

@ -17,15 +17,45 @@ namespace Explorer
// Reflection window only
public MemberInfo MemberInfo { get; set; }
public ReflectionWindow.MemberInfoType MemberInfoType { get; set; }
// public ReflectionWindow.MemberInfoType MemberInfoType { get; set; }
public Type DeclaringType { get; set; }
public object DeclaringInstance { get; set; }
public string FullName => $"{MemberInfo.DeclaringType.Name}.{MemberInfo.Name}";
public string ReflectionException;
public bool CanWrite
{
get
{
if (MemberInfo is FieldInfo fi)
{
return !(fi.IsLiteral && !fi.IsInitOnly);
}
else if (MemberInfo is PropertyInfo pi)
{
return pi.CanWrite;
}
else
{
return false;
}
}
}
public ReflectionWindow.MemberInfoType MemberInfoType
{
get
{
if (MemberInfo is FieldInfo) return ReflectionWindow.MemberInfoType.Field;
if (MemberInfo is PropertyInfo) return ReflectionWindow.MemberInfoType.Property;
if (MemberInfo is MethodInfo) return ReflectionWindow.MemberInfoType.Method;
return ReflectionWindow.MemberInfoType.All;
}
}
// methods
public virtual void Init() { }
public abstract void DrawValue(Rect window, float width);
public abstract void SetValue();
public static CacheObject GetCacheObject(object obj)
{
@ -41,63 +71,72 @@ namespace Explorer
/// <returns></returns>
public static CacheObject GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance)
{
CacheObject holder;
var type = ReflectionHelpers.GetActualType(obj) ?? (memberInfo as FieldInfo)?.FieldType ?? (memberInfo as PropertyInfo)?.PropertyType;
if (type == null)
{
MelonLogger.Log("Could not get type for object or memberinfo!");
return null;
}
return GetCacheObject(obj, memberInfo, declaringInstance, type);
}
/// <summary>
/// Gets the CacheObject subclass for an object or MemberInfo
/// </summary>
/// <param name="obj">The current value (can be null if memberInfo is not null)</param>
/// <param name="memberInfo">The MemberInfo (can be null if obj is not null)</param>
/// <param name="declaringInstance">If MemberInfo is not null, the declaring class instance. Can be null if static.</param>
/// <param name="type">The type of the object or MemberInfo value.</param>
/// <returns></returns>
public static CacheObject GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance, Type type)
{
CacheObject holder;
if ((obj is Il2CppSystem.Object || typeof(Il2CppSystem.Object).IsAssignableFrom(type))
&& (type.FullName.Contains("UnityEngine.GameObject") || type.FullName.Contains("UnityEngine.Transform")))
{
holder = new CacheGameObject(obj);
holder = new CacheGameObject();
}
else if (type.IsPrimitive || type == typeof(string))
{
holder = new CachePrimitive(obj);
holder = new CachePrimitive();
}
else if (type.IsEnum)
{
holder = new CacheEnum(obj);
holder = new CacheEnum();
}
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type) || ReflectionHelpers.IsList(type))
{
holder = new CacheList(obj);
holder = new CacheList();
}
else
{
holder = new CacheOther();
}
if (memberInfo != null)
{
holder.MemberInfo = memberInfo;
holder.DeclaringType = memberInfo.DeclaringType;
holder.DeclaringInstance = declaringInstance;
if (memberInfo.MemberType == MemberTypes.Field)
{
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Field;
}
else if (memberInfo.MemberType == MemberTypes.Property)
{
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Property;
}
else if (memberInfo.MemberType == MemberTypes.Method)
{
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Method;
}
}
holder.Value = obj;
holder.ValueType = type.FullName;
if (memberInfo != null)
{
holder.MemberInfo = memberInfo;
holder.DeclaringType = memberInfo.DeclaringType;
holder.DeclaringInstance = declaringInstance;
}
holder.UpdateValue();
holder.Init();
return holder;
}
public void Draw(Rect window, float labelWidth = 180f)
public void Draw(Rect window, float labelWidth = 215f)
{
if (MemberInfo != null)
{
GUILayout.Label("<color=cyan>" + FullName + ":</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
GUILayout.Label("<color=cyan>" + FullName + "</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
}
else
{
@ -110,7 +149,7 @@ namespace Explorer
}
else if (Value == null)
{
GUILayout.Label("<i>null (" + this.ValueType + ")</i>", null);
GUILayout.Label("<i>null (" + ValueType + ")</i>", null);
}
else
{
@ -147,25 +186,19 @@ namespace Explorer
}
}
public void SetValue(object value, MemberInfo memberInfo, object declaringInstance)
public void SetValue()
{
try
{
if (memberInfo.MemberType == MemberTypes.Field)
if (MemberInfo.MemberType == MemberTypes.Field)
{
var fi = memberInfo as FieldInfo;
if (!(fi.IsLiteral && !fi.IsInitOnly))
{
fi.SetValue(fi.IsStatic ? null : declaringInstance, value);
}
var fi = MemberInfo as FieldInfo;
fi.SetValue(fi.IsStatic ? null : DeclaringInstance, Value);
}
else if (memberInfo.MemberType == MemberTypes.Property)
else if (MemberInfo.MemberType == MemberTypes.Property)
{
var pi = memberInfo as PropertyInfo;
if (pi.CanWrite)
{
pi.SetValue(pi.GetAccessors()[0].IsStatic ? null : declaringInstance, value);
}
var pi = MemberInfo as PropertyInfo;
pi.SetValue(pi.GetAccessors()[0].IsStatic ? null : DeclaringInstance, Value);
}
}
catch (Exception e)

View File

@ -58,21 +58,11 @@ namespace Explorer
}
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
if (GUILayout.Button("<color=yellow>" + label + "</color>", new GUILayoutOption[] { GUILayout.MaxWidth(width) }))
if (GUILayout.Button("<color=yellow>" + label + "</color>", new GUILayoutOption[] { GUILayout.MaxWidth(width + 40) }))
{
WindowManager.InspectObject(Value, out bool _);
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
}
public override void SetValue()
{
throw new NotImplementedException("TODO");
}
//public override void UpdateValue(object obj)
//{
//}
}
}

View File

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using MelonLoader;
using UnityEngine;
@ -10,7 +7,7 @@ namespace Explorer
{
public class CachePrimitive : CacheObject
{
public enum PrimitiveType
public enum PrimitiveTypes
{
Bool,
Double,
@ -19,69 +16,122 @@ namespace Explorer
String
}
private readonly PrimitiveType m_primitiveType;
private string m_valueToString;
public CachePrimitive(object obj)
public PrimitiveTypes PrimitiveType;
public MethodInfo ParseMethod
{
if (obj == null) return;
get
{
if (m_parseMethod == null)
{
Type t = null;
switch (PrimitiveType)
{
case PrimitiveTypes.Bool:
t = typeof(bool); break;
case PrimitiveTypes.Double:
t = typeof(double); break;
case PrimitiveTypes.Float:
t = typeof(float); break;
case PrimitiveTypes.Int:
t = typeof(int); break;
case PrimitiveTypes.String:
t = typeof(string); break;
}
m_parseMethod = t.GetMethod("Parse", new Type[] { typeof(string) });
}
return m_parseMethod;
}
}
if (obj is bool)
private MethodInfo m_parseMethod;
public override void Init()
{
if (Value == null)
{
m_primitiveType = PrimitiveType.Bool;
// this must mean it is a string? no other primitive type should be nullable
PrimitiveType = PrimitiveTypes.String;
return;
}
else if (obj is double)
m_valueToString = Value.ToString();
var type = Value.GetType();
if (type == typeof(bool))
{
m_primitiveType = PrimitiveType.Double;
PrimitiveType = PrimitiveTypes.Bool;
}
else if (obj is float)
else if (type == typeof(double))
{
m_primitiveType = PrimitiveType.Float;
PrimitiveType = PrimitiveTypes.Double;
}
else if (obj is int || obj is IntPtr || obj is uint)
else if (type == typeof(float))
{
m_primitiveType = PrimitiveType.Int;
PrimitiveType = PrimitiveTypes.Float;
}
else if (obj is string)
else if (type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong) || type == typeof(IntPtr))
{
m_primitiveType = PrimitiveType.String;
PrimitiveType = PrimitiveTypes.Int;
}
else
{
if (type != typeof(string))
{
MelonLogger.Log("Unsupported primitive: " + type);
}
PrimitiveType = PrimitiveTypes.String;
}
}
public override void UpdateValue()
{
base.UpdateValue();
m_valueToString = Value?.ToString();
}
public override void DrawValue(Rect window, float width)
{
if (m_primitiveType == PrimitiveType.Bool && Value is bool b)
if (PrimitiveType == PrimitiveTypes.Bool)
{
var b = (bool)Value;
var color = "<color=" + (b ? "lime>" : "red>");
Value = GUILayout.Toggle((bool)Value, color + Value.ToString() + "</color>", null);
b = GUILayout.Toggle(b, color + b.ToString() + "</color>", null);
if (b != (bool)Value)
{
SetValue();
SetValue(m_valueToString);
}
}
else
{
var toString = Value.ToString();
if (toString.Length > 37)
GUILayout.Label("<color=yellow><i>" + PrimitiveType + "</i></color>", new GUILayoutOption[] { GUILayout.Width(50) });
var _width = window.width - 200;
if (m_valueToString.Length > 37)
{
Value = GUILayout.TextArea(toString, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - 260) });
m_valueToString = GUILayout.TextArea(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(_width) });
}
else
{
Value = GUILayout.TextField(toString, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - 260) });
m_valueToString = GUILayout.TextField(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(_width) });
}
if (MemberInfo != null)
if (CanWrite)
{
if (GUILayout.Button("<color=#00FF00>Apply</color>", new GUILayoutOption[] { GUILayout.Width(60) }))
{
SetValue();
SetValue(m_valueToString);
}
}
}
}
public override void SetValue()
public void SetValue(string value)
{
if (MemberInfo == null)
{
@ -89,19 +139,24 @@ namespace Explorer
return;
}
switch (m_primitiveType)
if (PrimitiveType == PrimitiveTypes.String)
{
case PrimitiveType.Bool:
SetValue(bool.Parse(Value.ToString()), MemberInfo, DeclaringInstance); return;
case PrimitiveType.Double:
SetValue(double.Parse(Value.ToString()), MemberInfo, DeclaringInstance); return;
case PrimitiveType.Float:
SetValue(float.Parse(Value.ToString()), MemberInfo, DeclaringInstance); return;
case PrimitiveType.Int:
SetValue(int.Parse(Value.ToString()), MemberInfo, DeclaringInstance); return;
case PrimitiveType.String:
SetValue(Value.ToString(), MemberInfo, DeclaringInstance); return;
Value = value;
}
else
{
try
{
var val = ParseMethod.Invoke(null, new object[] { value });
Value = val;
}
catch (Exception e)
{
MelonLogger.Log("Exception parsing value: " + e.GetType() + ", " + e.Message);
}
}
SetValue();
}
}
}