Compare commits

...

9 Commits
1.3.5 ... 1.4.1

Author SHA1 Message Date
6bafab785b 1.4.1
* Cleanup some small bugs introduced in 1.4.0
* Added better exception handling for failed Reflection, and the ability to hide failed reflection members in the Reflection window, as well as see the error type.
* Reflection window members now display the full name instead of just the member name (eg. "Camera.main" instead of just "main").
2020-08-22 17:17:11 +10:00
62b1688d53 Update README.md 2020-08-22 01:42:29 +10:00
4d015cbe93 Update README.md 2020-08-22 01:08:09 +10:00
0da8f4faea Update README.md 2020-08-22 01:02:04 +10:00
b264151c46 1.4.0
- Wrote the CacheObject class to replace MemberInfoHolder, resulting code is better perfomance and much easier to read.
- Added pages to Object Reflection window, now limited to 20 members per page to improve performance further.
2020-08-22 00:16:05 +10:00
3d2bc7cd4b Update README.md 2020-08-20 18:59:59 +10:00
85c26e6af7 Update README.md 2020-08-20 18:56:07 +10:00
b149efa234 Update README.md 2020-08-20 18:53:56 +10:00
72c222d59a 1.3.5
- Project structure cleanup
- Code refactoring
- Adding some extension methods for cleaner looking code
- Performance improvements on Object Reflection window
2020-08-18 17:38:09 +10:00
20 changed files with 976 additions and 730 deletions

View File

@ -1,9 +1,20 @@
# CppExplorer
# CppExplorer [![Version](https://img.shields.io/badge/MelonLoader-0.2.6-green.svg)]()
[![Version](https://img.shields.io/badge/MelonLoader-0.2.6-green.svg)]()
<p align="center">
<img align="center" src="https://i.imgur.com/1ZoZemW.png">
</p>
An in-game explorer and a suite of debugging tools for [IL2CPP](https://docs.unity3d.com/Manual/IL2CPP.html) Unity games, using [MelonLoader](https://github.com/HerpDerpinstine/MelonLoader).
<p align="center">
An in-game explorer and a suite of debugging tools for <a href="https://docs.unity3d.com/Manual/IL2CPP.html">IL2CPP</a> Unity games, using <a href="https://github.com/HerpDerpinstine/MelonLoader">MelonLoader</a>.<br><br>
<a href="../../releases/latest">
<img src="https://img.shields.io/github/release/sinai-dev/CppExplorer.svg" />
</a>
<img src="https://img.shields.io/github/downloads/sinai-dev/CppExplorer/total.svg" />
</p>
### Note
Most games running on Unity 2017 to 2019 should be supported. If you find that the GUI does not display properly and you get errors in the MelonLoader console about it, then this is likely due to a bug with Il2CppAssemblyUnhollower's unstripping. This bug is known by the developer of the tool and they will fix it as soon as they are able to.
## Features
@ -39,6 +50,8 @@ In order to fix this problem, you can:
## Images
<i>Note: images may be slightly outdated, taken from version 1.2</i>.
Scene explorer, and inspection of a MonoBehaviour object:
[![](https://i.imgur.com/Yxizwcz.png)](https://i.imgur.com/Yxizwcz.png)

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnityEngine;
namespace Explorer
{
public class CacheEnum : CacheObject
{
private readonly Type m_enumType;
private readonly string[] m_names;
public CacheEnum(object obj)
{
if (obj != null)
{
m_enumType = obj.GetType();
m_names = Enum.GetNames(m_enumType);
}
}
public override void DrawValue(Rect window, float width)
{
if (MemberInfo != null)
{
if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.Width(25) }))
{
SetEnum(ref Value, -1);
SetValue();
}
if (GUILayout.Button(">", new GUILayoutOption[] { GUILayout.Width(25) }))
{
SetEnum(ref Value, 1);
SetValue();
}
}
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);
}
public void SetEnum(ref object value, int change)
{
var names = m_names.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]);
}
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnityEngine;
namespace Explorer
{
public class CacheGameObject : CacheObject
{
private GameObject m_gameObject;
public CacheGameObject(object obj)
{
if (obj != null)
m_gameObject = GetGameObject(obj);
}
private GameObject GetGameObject(object obj)
{
if (obj is Il2CppSystem.Object ilObj)
{
var ilType = ilObj.GetIl2CppType();
if (ilType == ReflectionHelpers.GameObjectType || ilType == ReflectionHelpers.TransformType)
{
return ilObj.TryCast<GameObject>() ?? ilObj.TryCast<Transform>()?.gameObject;
}
}
return null;
}
public override void DrawValue(Rect window, float width)
{
UIHelpers.GameobjButton(m_gameObject, null, false, width);
}
public override void SetValue()
{
throw new NotImplementedException("TODO");
}
public override void UpdateValue()
{
base.UpdateValue();
m_gameObject = GetGameObject(Value);
}
}
}

View File

@ -0,0 +1,170 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Mono.CSharp;
using UnityEngine;
namespace Explorer
{
public partial class CacheList : CacheObject
{
public bool IsExpanded { get; set; }
public int ArrayOffset { get; set; }
public Type EntryType
{
get
{
if (m_entryType == null)
{
m_entryType = Value?.GetType().GetGenericArguments()[0];
}
return m_entryType;
}
set
{
m_entryType = value;
}
}
private Type m_entryType;
public IEnumerable Enumerable
{
get
{
if (m_enumerable == null && Value != null)
{
m_enumerable = Value as IEnumerable ?? CppListToEnumerable(Value);
}
return m_enumerable;
}
}
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;
return (IEnumerable)typeof(Il2CppSystem.Collections.Generic.List<>)
.MakeGenericType(new Type[] { EntryType })
.GetMethod("ToArray")
.Invoke(list, new object[0]);
}
public override void DrawValue(Rect window, float width)
{
int count = m_cachedEntries.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 + "] " + EntryType + "</color>";
if (GUILayout.Button(btnLabel, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - 260) }))
{
WindowManager.InspectObject(Value, out bool _);
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
if (IsExpanded)
{
if (count > CppExplorer.ArrayLimit)
{
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Space(190);
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)CppExplorer.ArrayLimit)) - 1;
GUILayout.Label($"Page {ArrayOffset + 1}/{maxOffset + 1}", new GUILayoutOption[] { GUILayout.Width(80) });
// prev/next page buttons
if (GUILayout.Button("< Prev", null))
{
if (ArrayOffset > 0) ArrayOffset--;
}
if (GUILayout.Button("Next >", null))
{
if (ArrayOffset < maxOffset) ArrayOffset++;
}
}
int offset = ArrayOffset * CppExplorer.ArrayLimit;
if (offset >= count) offset = 0;
for (int i = offset; i < offset + CppExplorer.ArrayLimit && i < count; i++)
{
var entry = m_cachedEntries[i];
//collapsing the BeginHorizontal called from ReflectionWindow.WindowFunction or previous array entry
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Space(190);
if (entry.Value == null)
{
GUILayout.Label("<i><color=grey>null</color></i>", null);
}
else
{
GUILayout.Label(i.ToString(), new GUILayoutOption[] { GUILayout.Width(30) });
entry.DrawValue(window, window.width - 250);
}
}
}
}
public override void SetValue()
{
throw new NotImplementedException("TODO");
}
/// <summary>
/// Called when the user presses the "Update" button, or if AutoUpdate is on.
/// </summary>
public override void UpdateValue()
{
base.UpdateValue();
if (Value == null) return;
var enumerator = Enumerable?.GetEnumerator();
if (enumerator == null) return;
var list = new List<CacheObject>();
while (enumerator.MoveNext())
{
list.Add(GetCacheObject(enumerator.Current));
}
m_cachedEntries = list.ToArray();
}
}
}

View File

@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnhollowerBaseLib;
using UnityEngine;
namespace Explorer
{
public abstract class CacheObject
{
public object Value;
public string ValueType;
// Reflection window only
public MemberInfo MemberInfo { 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;
// methods
public abstract void DrawValue(Rect window, float width);
public abstract void SetValue();
public static CacheObject GetCacheObject(object obj)
{
return GetCacheObject(obj, null, null);
}
/// <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>
/// <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 ((obj is Il2CppSystem.Object || typeof(Il2CppSystem.Object).IsAssignableFrom(type))
&& (type.FullName.Contains("UnityEngine.GameObject") || type.FullName.Contains("UnityEngine.Transform")))
{
holder = new CacheGameObject(obj);
}
else if (type.IsPrimitive || type == typeof(string))
{
holder = new CachePrimitive(obj);
}
else if (type.IsEnum)
{
holder = new CacheEnum(obj);
}
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type) || ReflectionHelpers.IsList(type))
{
holder = new CacheList(obj);
}
else
{
holder = new CacheOther();
}
if (memberInfo != null)
{
holder.MemberInfo = memberInfo;
holder.DeclaringType = memberInfo.DeclaringType;
holder.DeclaringInstance = declaringInstance;
//if (declaringInstance is Il2CppSystem.Object ilInstance && ilInstance.GetType() != memberInfo.DeclaringType)
//{
// try
// {
// holder.DeclaringInstance = ilInstance.Il2CppCast(holder.DeclaringType);
// }
// catch (Exception e)
// {
// holder.ReflectionException = ReflectionHelpers.ExceptionToString(e);
// holder.DeclaringInstance = declaringInstance;
// }
//}
//else
//{
// 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;
return holder;
}
public void Draw(Rect window, float labelWidth = 180f)
{
if (MemberInfo != null)
{
GUILayout.Label("<color=cyan>" + FullName + ":</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
}
else
{
GUILayout.Space(labelWidth);
}
if (!string.IsNullOrEmpty(ReflectionException))
{
GUILayout.Label("<color=red>Reflection failed!</color> (" + ReflectionException + ")", null);
}
else if (Value == null)
{
GUILayout.Label("<i>null (" + this.ValueType + ")</i>", null);
}
else
{
DrawValue(window, window.width - labelWidth - 90);
}
}
public virtual void UpdateValue()
{
if (MemberInfo == null || !string.IsNullOrEmpty(ReflectionException))
{
return;
}
try
{
if (MemberInfo.MemberType == MemberTypes.Field)
{
var fi = MemberInfo as FieldInfo;
Value = fi.GetValue(fi.IsStatic ? null : DeclaringInstance);
}
else if (MemberInfo.MemberType == MemberTypes.Property)
{
var pi = MemberInfo as PropertyInfo;
bool isStatic = pi.GetAccessors()[0].IsStatic;
var target = isStatic ? null : DeclaringInstance;
Value = pi.GetValue(target, null);
}
//ReflectionException = null;
}
catch (Exception e)
{
ReflectionException = ReflectionHelpers.ExceptionToString(e);
}
}
public void SetValue(object value, MemberInfo memberInfo, object declaringInstance)
{
try
{
if (memberInfo.MemberType == MemberTypes.Field)
{
var fi = memberInfo as FieldInfo;
if (!(fi.IsLiteral && !fi.IsInitOnly))
{
fi.SetValue(fi.IsStatic ? null : declaringInstance, value);
}
}
else if (memberInfo.MemberType == MemberTypes.Property)
{
var pi = memberInfo as PropertyInfo;
if (pi.CanWrite)
{
pi.SetValue(pi.GetAccessors()[0].IsStatic ? null : declaringInstance, value);
}
}
}
catch (Exception e)
{
MelonLogger.LogWarning($"Error setting value: {e.GetType()}, {e.Message}");
}
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using UnityEngine;
namespace Explorer
{
public class CacheOther : CacheObject
{
private MethodInfo m_toStringMethod;
private bool m_triedToGetMethod;
public MethodInfo ToStringMethod
{
get
{
if (m_toStringMethod == null && !m_triedToGetMethod)
{
if (Value == null) return null;
m_triedToGetMethod = true;
try
{
var methods = ReflectionHelpers.GetActualType(Value)
.GetMethods(ReflectionHelpers.CommonFlags)
.Where(x => x.Name == "ToString")
.GetEnumerator();
while (methods.MoveNext())
{
// just get the first (top-most level) method, then break.
m_toStringMethod = methods.Current;
break;
}
}
catch { }
}
return m_toStringMethod;
}
}
public override void DrawValue(Rect window, float width)
{
string label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
if (!label.Contains(ValueType))
{
label += $" ({ValueType})";
}
if (Value is UnityEngine.Object unityObj && !label.Contains(unityObj.name))
{
label = unityObj.name + " | " + label;
}
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
if (GUILayout.Button("<color=yellow>" + label + "</color>", new GUILayoutOption[] { GUILayout.MaxWidth(width) }))
{
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

@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnityEngine;
namespace Explorer
{
public class CachePrimitive : CacheObject
{
public enum PrimitiveType
{
Bool,
Double,
Float,
Int,
String
}
private readonly PrimitiveType m_primitiveType;
public CachePrimitive(object obj)
{
if (obj == null) return;
if (obj is bool)
{
m_primitiveType = PrimitiveType.Bool;
}
else if (obj is double)
{
m_primitiveType = PrimitiveType.Double;
}
else if (obj is float)
{
m_primitiveType = PrimitiveType.Float;
}
else if (obj is int || obj is IntPtr || obj is uint)
{
m_primitiveType = PrimitiveType.Int;
}
else if (obj is string)
{
m_primitiveType = PrimitiveType.String;
}
}
public override void DrawValue(Rect window, float width)
{
if (m_primitiveType == PrimitiveType.Bool && Value is bool b)
{
var color = "<color=" + (b ? "lime>" : "red>");
Value = GUILayout.Toggle((bool)Value, color + Value.ToString() + "</color>", null);
if (b != (bool)Value)
{
SetValue();
}
}
else
{
var toString = Value.ToString();
if (toString.Length > 37)
{
Value = GUILayout.TextArea(toString, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - 260) });
}
else
{
Value = GUILayout.TextField(toString, new GUILayoutOption[] { GUILayout.MaxWidth(window.width - 260) });
}
if (MemberInfo != null)
{
if (GUILayout.Button("<color=#00FF00>Apply</color>", new GUILayoutOption[] { GUILayout.Width(60) }))
{
SetValue();
}
}
}
}
public override void SetValue()
{
if (MemberInfo == null)
{
MelonLogger.Log("Trying to SetValue but the MemberInfo is null!");
return;
}
switch (m_primitiveType)
{
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;
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using MelonLoader;
using UnhollowerBaseLib;
namespace Explorer
{
@ -12,10 +13,10 @@ namespace Explorer
// consts
public const string ID = "com.sinai.cppexplorer";
public const string VERSION = "1.3.5";
public const string VERSION = "1.4.1";
public const string AUTHOR = "Sinai";
public const string NAME = "IL2CPP Runtime Explorer"
public const string NAME = "CppExplorer"
#if Release_Unity2018
+ " (Unity 2018)"
#endif

View File

@ -136,14 +136,19 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CachedObjects\CacheEnum.cs" />
<Compile Include="CachedObjects\CacheGameObject.cs" />
<Compile Include="CachedObjects\CacheList.cs" />
<Compile Include="CachedObjects\CachePrimitive.cs" />
<Compile Include="CachedObjects\CacheOther.cs" />
<Compile Include="CppExplorer.cs" />
<Compile Include="Extensions\ReflectionExtensions.cs" />
<Compile Include="Extensions\UnityExtensions.cs" />
<Compile Include="Helpers\ReflectionHelpers.cs" />
<Compile Include="Helpers\UIHelpers.cs" />
<Compile Include="Helpers\UnityHelpers.cs" />
<Compile Include="MainMenu\InspectUnderMouse.cs" />
<Compile Include="Windows\Reflection\FieldInfoHolder.cs" />
<Compile Include="Windows\Reflection\MemberInfoHolder.cs" />
<Compile Include="Windows\Reflection\PropertyInfoHolder.cs" />
<Compile Include="CachedObjects\CacheObject.cs" />
<Compile Include="Windows\UIWindow.cs" />
<Compile Include="MainMenu\Pages\ConsolePage.cs" />
<Compile Include="MainMenu\Pages\Console\REPL.cs" />

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Explorer
{
public static class ReflectionExtensions
{
public static object Il2CppCast(this object obj, Type castTo)
{
return ReflectionHelpers.Il2CppCast(obj, castTo);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Explorer
{
public static class UnityExtensions
{
public static string GetGameObjectPath(this Transform _transform)
{
return GetGameObjectPath(_transform, true);
}
public static string GetGameObjectPath(this Transform _transform, bool _includeThisName)
{
string path = _includeThisName ? ("/" + _transform.name) : "";
GameObject gameObject = _transform.gameObject;
while (gameObject.transform.parent != null)
{
gameObject = gameObject.transform.parent.gameObject;
path = "/" + gameObject.name + path;
}
return path;
}
}
}

View File

@ -9,6 +9,7 @@ using UnhollowerRuntimeLib;
using UnityEngine;
using BF = System.Reflection.BindingFlags;
using ILBF = Il2CppSystem.Reflection.BindingFlags;
using MelonLoader;
namespace Explorer
{
@ -26,10 +27,53 @@ namespace Explorer
public static object Il2CppCast(object obj, Type castTo)
{
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) return obj;
var generic = m_tryCastMethodInfo.MakeGenericMethod(castTo);
return generic.Invoke(obj, null);
}
public static string ExceptionToString(Exception e)
{
if (IsFailedGeneric(e))
{
return "Unable to initialize this type.";
}
else if (IsObjectCollected(e))
{
return "Garbage collected in Il2Cpp.";
}
return e.GetType() + ", " + e.Message;
}
public static bool IsFailedGeneric(Exception e)
{
return IsExceptionOfType(e, typeof(TargetInvocationException)) && IsExceptionOfType(e, typeof(TypeLoadException));
}
public static bool IsObjectCollected(Exception e)
{
return IsExceptionOfType(e, typeof(ObjectCollectedException));
}
public static bool IsExceptionOfType(Exception e, Type t, bool strict = true, bool checkInner = true)
{
bool isType;
if (strict)
isType = e.GetType() == t;
else
isType = t.IsAssignableFrom(e.GetType());
if (isType) return true;
if (e.InnerException != null && checkInner)
return IsExceptionOfType(e.InnerException, t, strict);
else
return false;
}
public static bool IsList(Type t)
{
return t.IsGenericType
@ -56,6 +100,8 @@ namespace Explorer
public static Type GetActualType(object m_object)
{
if (m_object == null) return null;
if (m_object is Il2CppSystem.Object ilObject)
{
var iltype = ilObject.GetIl2CppType();

View File

@ -5,7 +5,6 @@ using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Object = UnityEngine.Object;
using System.Reflection;
namespace Explorer
{
@ -103,311 +102,5 @@ namespace Explorer
GUILayout.EndHorizontal();
}
public static void DrawMember(ref object value, ref bool isExpanded, ref int arrayOffset, MemberInfo memberInfo, Rect rect, object setTarget = null, Action<object> setAction = null, float labelWidth = 180, bool autoSet = false)
{
GUILayout.Label("<color=cyan>" + memberInfo.Name + ":</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
string valueType = "";
bool canWrite = true;
if (memberInfo is FieldInfo fi)
{
valueType = fi.FieldType.Name;
canWrite = !(fi.IsLiteral && !fi.IsInitOnly);
}
else if (memberInfo is PropertyInfo pi)
{
valueType = pi.PropertyType.Name;
canWrite = pi.CanWrite;
}
DrawValue(ref value, ref isExpanded, ref arrayOffset, rect, valueType, (canWrite ? setTarget : null), (canWrite ? setAction : null), autoSet);
}
public static void DrawValue(ref object value, ref bool isExpanded, ref int arrayOffset, Rect rect, string nullValueType = null, object setTarget = null, Action<object> setAction = null, bool autoSet = false)
{
if (value == null)
{
GUILayout.Label("<i>null (" + nullValueType + ")</i>", null);
return;
}
var valueType = value.GetType();
Il2CppSystem.Type ilType = null;
if (value is Il2CppSystem.Object ilObject)
{
ilType = ilObject.GetIl2CppType();
}
if (valueType.IsPrimitive || value.GetType() == typeof(string))
{
DrawPrimitive(ref value, rect, setTarget, setAction);
}
else if (ilType != null && ilType == ReflectionHelpers.GameObjectType || ReflectionHelpers.TransformType.IsAssignableFrom(ilType))
{
GameObject go;
var ilObj = value as Il2CppSystem.Object;
if (ilType == ReflectionHelpers.GameObjectType)
{
go = ilObj.TryCast<GameObject>();
}
else
{
go = ilObj.TryCast<Transform>().gameObject;
}
GameobjButton(go, null, false, rect.width - 250);
}
else if (valueType.IsEnum)
{
if (setAction != null)
{
if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.Width(25) }))
{
SetEnum(ref value, -1);
setAction.Invoke(setTarget);
}
if (GUILayout.Button(">", new GUILayoutOption[] { GUILayout.Width(25) }))
{
SetEnum(ref value, 1);
setAction.Invoke(setTarget);
}
}
GUILayout.Label(value.ToString(), null);
}
else if (value is System.Collections.IEnumerable || ReflectionHelpers.IsList(valueType))
{
System.Collections.IEnumerable enumerable;
if (value is System.Collections.IEnumerable isEnumerable)
{
enumerable = isEnumerable;
}
else
{
var listValueType = value.GetType().GetGenericArguments()[0];
var listType = typeof(Il2CppSystem.Collections.Generic.List<>).MakeGenericType(new Type[] { listValueType });
var method = listType.GetMethod("ToArray");
enumerable = (System.Collections.IEnumerable)method.Invoke(value, new object[0]);
}
int count = enumerable.Cast<object>().Count();
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 + "] " + valueType + "</color>";
if (GUILayout.Button(btnLabel, new GUILayoutOption[] { GUILayout.MaxWidth(rect.width - 260) }))
{
WindowManager.InspectObject(value, out bool _);
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
if (isExpanded)
{
if (count > CppExplorer.ArrayLimit)
{
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Space(190);
int maxOffset = (int)Mathf.Ceil(count / CppExplorer.ArrayLimit);
GUILayout.Label($"Page {arrayOffset + 1}/{maxOffset + 1}", new GUILayoutOption[] { GUILayout.Width(80) });
// prev/next page buttons
if (GUILayout.Button("< Prev", null))
{
if (arrayOffset > 0) arrayOffset--;
}
if (GUILayout.Button("Next >", null))
{
if (arrayOffset < maxOffset) arrayOffset++;
}
}
int offset = arrayOffset * CppExplorer.ArrayLimit;
if (offset >= count) offset = 0;
var enumerator = enumerable.GetEnumerator();
if (enumerator != null)
{
int i = 0;
int preiterated = 0;
while (enumerator.MoveNext())
{
if (offset > 0 && preiterated < offset)
{
preiterated++;
continue;
}
var obj = enumerator.Current;
//collapsing the BeginHorizontal called from ReflectionWindow.WindowFunction or previous array entry
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Space(190);
if (i > CppExplorer.ArrayLimit - 1)
{
//GUILayout.Label($"<i><color=red>{count - CppExplorer.ArrayLimit} results omitted, array is too long!</color></i>", null);
break;
}
if (obj == null)
{
GUILayout.Label("<i><color=grey>null</color></i>", null);
}
else
{
var type = obj.GetType();
var lbl = (i + offset) + ": <color=cyan>" + obj.ToString() + "</color>";
if (type.IsPrimitive || typeof(string).IsAssignableFrom(type))
{
GUILayout.Label(lbl, null);
}
else
{
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
if (GUILayout.Button(lbl, null))
{
WindowManager.InspectObject(obj, out _);
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
}
//var type = obj.GetType();
//DrawMember(ref obj, type.ToString(), i.ToString(), rect, setTarget, setAction, 25, true);
}
i++;
}
}
}
}
else
{
var label = value.ToString();
if (valueType == typeof(Object))
{
label = (value as Object).name;
}
else if (value is Vector4 vec4)
{
label = vec4.ToString();
}
else if (value is Vector3 vec3)
{
label = vec3.ToString();
}
else if (value is Vector2 vec2)
{
label = vec2.ToString();
}
else if (value is Rect rec)
{
label = rec.ToString();
}
else if (value is Matrix4x4 matrix)
{
label = matrix.ToString();
}
else if (value is Color col)
{
label = col.ToString();
}
string typeLabel;
if (ilType != null)
{
typeLabel = ilType.FullName;
}
else
{
typeLabel = value.GetType().FullName;
}
if (!label.Contains(typeLabel))
{
label += $" ({typeLabel})";
}
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
if (GUILayout.Button("<color=yellow>" + label + "</color>", new GUILayoutOption[] { GUILayout.MaxWidth(rect.width - 230) }))
{
WindowManager.InspectObject(value, out bool _);
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
}
}
// Helper for drawing primitive values (with Apply button)
public static void DrawPrimitive(ref object value, Rect m_rect, object setTarget = null, Action<object> setAction = null, bool autoSet = false)
{
bool allowSet = setAction != null;
if (value.GetType() == typeof(bool))
{
bool b = (bool)value;
var color = "<color=" + (b ? "lime>" : "red>");
if (allowSet)
{
value = GUILayout.Toggle((bool)value, color + value.ToString() + "</color>", null);
if (b != (bool)value)
{
setAction.Invoke(setTarget);
}
}
}
else
{
if (value.ToString().Length > 37)
{
value = GUILayout.TextArea(value.ToString(), new GUILayoutOption[] { GUILayout.MaxWidth(m_rect.width - 260) });
}
else
{
value = GUILayout.TextField(value.ToString(), new GUILayoutOption[] { GUILayout.MaxWidth(m_rect.width - 260) });
}
if (autoSet || (allowSet && GUILayout.Button("<color=#00FF00>Apply</color>", new GUILayoutOption[] { GUILayout.Width(60) })))
{
setAction.Invoke(setTarget);
}
}
}
// Helper for setting an enum
public static void SetEnum(ref object value, int change)
{
var type = value.GetType();
var names = Enum.GetNames(type).ToList();
int newindex = names.IndexOf(value.ToString()) + change;
if ((change < 0 && newindex >= 0) || (change > 0 && newindex < names.Count))
{
value = Enum.Parse(type, names[newindex]);
}
}
}
}

View File

@ -7,7 +7,7 @@ using UnityEngine;
namespace Explorer
{
public static class UnityHelpers
public class UnityHelpers
{
private static Camera m_mainCamera;
@ -30,22 +30,5 @@ namespace Explorer
return UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
}
}
public static string GetGameObjectPath(this Transform _transform)
{
return GetGameObjectPath(_transform, true);
}
public static string GetGameObjectPath(this Transform _transform, bool _includeThisName)
{
string path = _includeThisName ? ("/" + _transform.name) : "";
GameObject gameObject = _transform.gameObject;
while (gameObject.transform.parent != null)
{
gameObject = gameObject.transform.parent.gameObject;
path = "/" + gameObject.name + path;
}
return path;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -22,9 +23,11 @@ namespace Explorer
private string m_typeInput = "";
private int m_limit = 20;
private int m_pageOffset = 0;
private List<object> m_searchResults = new List<object>();
//private List<object> m_searchResults = new List<object>();
private Vector2 resultsScroll = Vector2.zero;
private List<CacheObject> m_searchResults = new List<CacheObject>();
public SceneFilter SceneMode = SceneFilter.Any;
public TypeFilter TypeMode = TypeFilter.Object;
@ -59,6 +62,24 @@ namespace Explorer
{
}
private void CacheResults(IEnumerable results)
{
m_searchResults = new List<CacheObject>();
foreach (var obj in results)
{
var toCache = obj;
if (toCache is Il2CppSystem.Object ilObject)
{
toCache = ilObject.TryCast<GameObject>() ?? ilObject.TryCast<Transform>()?.gameObject ?? ilObject;
}
var cache = CacheObject.GetCacheObject(toCache);
m_searchResults.Add(cache);
}
}
public override void DrawWindow()
{
try
@ -68,7 +89,8 @@ namespace Explorer
GUILayout.Label("<b><color=orange>Helpers</color></b>", new GUILayoutOption[] { GUILayout.Width(70) });
if (GUILayout.Button("Find Static Instances", new GUILayoutOption[] { GUILayout.Width(180) }))
{
m_searchResults = GetInstanceClassScanner().ToList();
//m_searchResults = GetInstanceClassScanner().ToList();
CacheResults(GetInstanceClassScanner());
m_pageOffset = 0;
}
GUILayout.EndHorizontal();
@ -89,7 +111,7 @@ namespace Explorer
{
// prev/next page buttons
GUILayout.BeginHorizontal(null);
int maxOffset = (int)Mathf.Ceil(count / this.m_limit);
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)m_limit)) - 1;
if (GUILayout.Button("< Prev", null))
{
if (m_pageOffset > 0) m_pageOffset--;
@ -111,28 +133,12 @@ namespace Explorer
if (m_searchResults.Count > 0)
{
int offset = m_pageOffset * this.m_limit;
int preiterated = 0;
if (offset >= count) m_pageOffset = 0;
for (int i = 0; i < m_searchResults.Count; i++)
for (int i = offset; i < offset + CppExplorer.ArrayLimit && i < count; i++)
{
if (offset > 0 && preiterated < offset)
{
preiterated++;
continue;
}
if (i - offset > this.m_limit - 1)
{
break;
}
var obj = m_searchResults[i];
bool _ = false;
int __ = 0;
UIHelpers.DrawValue(ref obj, ref _, ref __, _temprect);
m_searchResults[i].Draw(MainMenu.MainRect, 0f);
//m_searchResults[i].DrawValue(MainMenu.MainRect);
}
}
else
@ -141,7 +147,6 @@ namespace Explorer
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
}
catch
@ -252,7 +257,7 @@ namespace Explorer
private void Search()
{
m_pageOffset = 0;
m_searchResults = FindAllObjectsOfType(m_searchInput, m_typeInput);
CacheResults(FindAllObjectsOfType(m_searchInput, m_typeInput));
}
private List<object> FindAllObjectsOfType(string _search, string _type)
@ -265,6 +270,7 @@ namespace Explorer
{
var findType = ReflectionHelpers.GetTypeByName(_type);
searchType = Il2CppSystem.Type.GetType(findType.AssemblyQualifiedName);
//MelonLogger.Log("Search type: " + findType.AssemblyQualifiedName);
}
catch (Exception e)
{
@ -294,8 +300,13 @@ namespace Explorer
var allObjectsOfType = Resources.FindObjectsOfTypeAll(searchType);
//MelonLogger.Log("Found count: " + allObjectsOfType.Length);
int i = 0;
foreach (var obj in allObjectsOfType)
{
if (i >= 2000) break;
if (_search != "" && !obj.name.ToLower().Contains(_search.ToLower()))
{
continue;
@ -317,6 +328,8 @@ namespace Explorer
{
matches.Add(obj);
}
i++;
}
return matches;

View File

@ -1,120 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnhollowerBaseLib;
namespace Explorer
{
public class FieldInfoHolder : MemberInfoHolder
{
public FieldInfo fieldInfo;
public object m_value;
public FieldInfoHolder(Type _type, FieldInfo _fieldInfo)
{
classType = _type;
fieldInfo = _fieldInfo;
}
public override void UpdateValue(object obj)
{
try
{
if (obj is Il2CppSystem.Object ilObject)
{
var declaringType = this.fieldInfo.DeclaringType;
var cast = ReflectionHelpers.Il2CppCast(obj, declaringType);
m_value = this.fieldInfo.GetValue(fieldInfo.IsStatic ? null : cast);
}
else
{
m_value = this.fieldInfo.GetValue(fieldInfo.IsStatic ? null : obj);
}
}
catch (Exception e)
{
MelonLogger.Log($"Error updating FieldInfoHolder | {e.GetType()}: {e.Message}\r\n{e.StackTrace}");
}
}
public override void Draw(ReflectionWindow window)
{
UIHelpers.DrawMember(ref m_value, ref this.IsExpanded, ref this.arrayOffset, this.fieldInfo, window.m_rect, window.Target, SetValue);
}
public override void SetValue(object obj)
{
try
{
if (fieldInfo.FieldType.IsEnum)
{
if (Enum.Parse(fieldInfo.FieldType, m_value.ToString()) is object enumValue && enumValue != null)
{
m_value = enumValue;
}
}
else if (fieldInfo.FieldType.IsPrimitive)
{
if (fieldInfo.FieldType == typeof(float))
{
if (float.TryParse(m_value.ToString(), out float f))
{
m_value = f;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a float!");
}
}
else if (fieldInfo.FieldType == typeof(double))
{
if (double.TryParse(m_value.ToString(), out double d))
{
m_value = d;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a double!");
}
}
else if (fieldInfo.FieldType != typeof(bool))
{
if (int.TryParse(m_value.ToString(), out int i))
{
m_value = i;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to an integer! type: " + fieldInfo.FieldType);
}
}
else
{
MelonLogger.Log("Unsupported primitive field type: " + fieldInfo.FieldType.FullName);
}
}
if (obj is Il2CppSystem.Object ilObject)
{
var declaringType = this.fieldInfo.DeclaringType;
var cast = ReflectionHelpers.Il2CppCast(obj, declaringType);
fieldInfo.SetValue(fieldInfo.IsStatic ? null : cast, m_value);
}
else
{
fieldInfo.SetValue(fieldInfo.IsStatic ? null : obj, m_value);
}
}
catch (Exception e)
{
MelonLogger.Log($"Error setting FieldInfoHolder | {e.GetType()}: {e.Message}\r\n{e.StackTrace}");
}
}
}
}

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Explorer
{
public abstract class MemberInfoHolder
{
public Type classType;
public bool IsExpanded = false;
public int arrayOffset = 0;
public abstract void Draw(ReflectionWindow window);
public abstract void UpdateValue(object obj);
public abstract void SetValue(object obj);
}
}

View File

@ -1,127 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MelonLoader;
using UnhollowerBaseLib;
namespace Explorer
{
public class PropertyInfoHolder : MemberInfoHolder
{
public PropertyInfo propInfo;
public object m_value;
public PropertyInfoHolder(Type _type, PropertyInfo _propInfo)
{
classType = _type;
propInfo = _propInfo;
}
public override void Draw(ReflectionWindow window)
{
UIHelpers.DrawMember(ref m_value, ref this.IsExpanded, ref this.arrayOffset, this.propInfo, window.m_rect, window.Target, SetValue);
}
public override void UpdateValue(object obj)
{
try
{
if (obj is Il2CppSystem.Object ilObject)
{
var declaringType = this.propInfo.DeclaringType;
if (declaringType == typeof(Il2CppObjectBase))
{
m_value = ilObject.Pointer;
}
else
{
var cast = ReflectionHelpers.Il2CppCast(obj, declaringType);
m_value = this.propInfo.GetValue(this.propInfo.GetAccessors()[0].IsStatic ? null : cast, null);
}
}
else
{
m_value = this.propInfo.GetValue(obj, null);
}
}
catch (Exception e)
{
MelonLogger.Log("Exception on PropertyInfoHolder.UpdateValue, Name: " + this.propInfo.Name);
MelonLogger.Log(e.GetType() + ", " + e.Message);
var inner = e.InnerException;
while (inner != null)
{
MelonLogger.Log("inner: " + inner.GetType() + ", " + inner.Message);
inner = inner.InnerException;
}
m_value = null;
}
}
public override void SetValue(object obj)
{
try
{
if (propInfo.PropertyType.IsEnum)
{
if (Enum.Parse(propInfo.PropertyType, m_value.ToString()) is object enumValue && enumValue != null)
{
m_value = enumValue;
}
}
else if (propInfo.PropertyType.IsPrimitive)
{
if (propInfo.PropertyType == typeof(float))
{
if (float.TryParse(m_value.ToString(), out float f))
{
m_value = f;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a float!");
}
}
else if (propInfo.PropertyType == typeof(double))
{
if (double.TryParse(m_value.ToString(), out double d))
{
m_value = d;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a double!");
}
}
else if (propInfo.PropertyType != typeof(bool))
{
if (int.TryParse(m_value.ToString(), out int i))
{
m_value = i;
}
else
{
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to an integer! type: " + propInfo.PropertyType);
}
}
}
var declaring = propInfo.DeclaringType;
var cast = ReflectionHelpers.Il2CppCast(obj, declaring);
propInfo.SetValue(propInfo.GetAccessors()[0].IsStatic ? null : cast, m_value, null);
}
catch
{
//MelonLogger.Log("Exception trying to set property " + this.propInfo.Name);
}
}
}
}

View File

@ -16,20 +16,23 @@ namespace Explorer
public override string Name { get => "Object Reflection"; set => Name = value; }
public Type ObjectType;
//public object Target;
private FieldInfoHolder[] m_FieldInfos;
private PropertyInfoHolder[] m_PropertyInfos;
private CacheObject[] m_cachedMembers;
private CacheObject[] m_cachedMemberFiltered;
private int m_pageOffset;
private int m_limitPerPage = 20;
private bool m_autoUpdate = false;
private string m_search = "";
public MemberFilter m_filter = MemberFilter.Property;
public MemberInfoType m_filter = MemberInfoType.Property;
private bool m_hideFailedReflection = true;
public enum MemberFilter
public enum MemberInfoType
{
Both,
Field,
Property,
Field
Method,
All
}
public override void Init()
@ -44,121 +47,106 @@ namespace Explorer
ObjectType = type;
var types = ReflectionHelpers.GetAllBaseTypes(Target);
CacheMembers(types);
CacheFields(types);
CacheProperties(types);
MelonLogger.Log("Cached properties: " + m_PropertyInfos.Length);
UpdateValues(true);
m_filter = MemberInfoType.All;
m_cachedMemberFiltered = m_cachedMembers.Where(x => ShouldProcessMember(x)).ToArray();
UpdateValues();
m_filter = MemberInfoType.Property;
}
public override void Update()
{
m_cachedMemberFiltered = m_cachedMembers.Where(x => ShouldProcessMember(x)).ToArray();
if (m_autoUpdate)
{
UpdateValues();
}
}
private void UpdateValues(bool forceAll = false)
private void UpdateValues()
{
UpdateMemberList(forceAll, this.m_FieldInfos, MemberFilter.Field);
UpdateMemberList(forceAll, this.m_PropertyInfos, MemberFilter.Property);
UpdateMembers();
}
private void UpdateMemberList(bool forceAll, MemberInfoHolder[] list, MemberFilter filter)
private void UpdateMembers()
{
if (forceAll || m_filter == MemberFilter.Both || m_filter == filter)
foreach (var member in m_cachedMemberFiltered)
{
foreach (var holder in list)
{
if (forceAll || ShouldUpdateMemberInfo(holder))
{
holder.UpdateValue(Target);
}
}
member.UpdateValue();
}
}
private bool ShouldUpdateMemberInfo(MemberInfoHolder holder)
private bool ShouldProcessMember(CacheObject holder)
{
var memberName = holder is FieldInfoHolder ?
(holder as FieldInfoHolder).fieldInfo.Name :
(holder as PropertyInfoHolder).propInfo.Name;
if (m_filter != MemberInfoType.All && m_filter != holder.MemberInfoType) return false;
return m_search == "" || memberName.ToLower().Contains(m_search.ToLower());
if (!string.IsNullOrEmpty(holder.ReflectionException) && m_hideFailedReflection) return false;
if (m_search == "" || holder.MemberInfo == null) return true;
return holder.FullName
.ToLower()
.Contains(m_search.ToLower());
}
private void CacheProperties(Type[] types, List<string> names = null)
private void CacheMembers(Type[] types)
{
if (names == null)
{
names = new List<string>();
}
var list = new List<CacheObject>();
var list = new List<PropertyInfoHolder>();
var names = new List<string>();
foreach (var type in types)
foreach (var declaringType in types)
{
PropertyInfo[] propInfos = new PropertyInfo[0];
if (declaringType == typeof(Il2CppObjectBase)) continue;
MemberInfo[] infos;
string exception = null;
try
{
propInfos = type.GetProperties(ReflectionHelpers.CommonFlags);
infos = declaringType.GetMembers(ReflectionHelpers.CommonFlags);
}
catch (TypeLoadException)
catch
{
MelonLogger.Log($"Couldn't get Properties for Type '{type.Name}', it may not support Il2Cpp Reflection at the moment.");
MelonLogger.Log("Exception getting members for type: " + declaringType.Name);
continue;
}
foreach (var pi in propInfos)
//object value = null;
object target = Target;
if (target is Il2CppSystem.Object ilObject)
{
// this member causes a crash when inspected, so just skipping it for now.
if (pi.Name == "Il2CppType")
try
{
continue;
target = ilObject.Il2CppCast(declaringType);
}
if (names.Contains(pi.Name))
catch (Exception e)
{
continue;
exception = ReflectionHelpers.ExceptionToString(e);
}
names.Add(pi.Name);
}
var piHolder = new PropertyInfoHolder(type, pi);
list.Add(piHolder);
foreach (var member in infos)
{
if (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property)
{
if (member.Name == "Il2CppType") continue;
var name = member.DeclaringType.Name + "." + member.Name;
if (names.Contains(name)) continue;
names.Add(name);
var cached = CacheObject.GetCacheObject(null, member, target);
list.Add(cached);
cached.ReflectionException = exception;
}
}
}
m_PropertyInfos = list.ToArray();
}
private void CacheFields(Type[] types, List<string> names = null)
{
if (names == null)
{
names = new List<string>();
}
var list = new List<FieldInfoHolder>();
foreach (var type in types)
{
foreach (var fi in type.GetFields(ReflectionHelpers.CommonFlags))
{
if (names.Contains(fi.Name))
{
continue;
}
names.Add(fi.Name);
var fiHolder = new FieldInfoHolder(type, fi);
list.Add(fiHolder);
}
}
m_FieldInfos = list.ToArray();
m_cachedMembers = list.ToArray();
}
// =========== GUI DRAW =========== //
@ -209,13 +197,20 @@ namespace Explorer
GUILayout.BeginHorizontal(null);
GUILayout.Label("<b>Search:</b>", new GUILayoutOption[] { GUILayout.Width(75) });
m_search = GUILayout.TextField(m_search, null);
GUILayout.Label("<b>Limit per page:</b>", new GUILayoutOption[] { GUILayout.Width(125) });
var limitString = m_limitPerPage.ToString();
limitString = GUILayout.TextField(limitString, new GUILayoutOption[] { GUILayout.Width(60) });
if (int.TryParse(limitString, out int i))
{
m_limitPerPage = i;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
GUILayout.Label("<b>Filter:</b>", new GUILayoutOption[] { GUILayout.Width(75) });
FilterToggle(MemberFilter.Both, "Both");
FilterToggle(MemberFilter.Property, "Properties");
FilterToggle(MemberFilter.Field, "Fields");
FilterToggle(MemberInfoType.All, "All");
FilterToggle(MemberInfoType.Property, "Properties");
FilterToggle(MemberInfoType.Field, "Fields");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(null);
@ -226,24 +221,39 @@ namespace Explorer
}
GUI.color = m_autoUpdate ? Color.green : Color.red;
m_autoUpdate = GUILayout.Toggle(m_autoUpdate, "Auto-update?", new GUILayoutOption[] { GUILayout.Width(100) });
GUI.color = m_hideFailedReflection ? Color.green : Color.red;
m_hideFailedReflection = GUILayout.Toggle(m_hideFailedReflection, "Hide failed Reflection?", new GUILayoutOption[] { GUILayout.Width(150) });
GUI.color = Color.white;
GUILayout.EndHorizontal();
GUILayout.Space(10);
int count = m_cachedMemberFiltered.Length;
if (count > m_limitPerPage)
{
// prev/next page buttons
GUILayout.BeginHorizontal(null);
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)m_limitPerPage)) - 1;
if (GUILayout.Button("< Prev", null))
{
if (m_pageOffset > 0) m_pageOffset--;
}
GUILayout.Label($"Page {m_pageOffset + 1}/{maxOffset + 1}", new GUILayoutOption[] { GUILayout.Width(80) });
if (GUILayout.Button("Next >", null))
{
if (m_pageOffset < maxOffset) m_pageOffset++;
}
GUILayout.EndHorizontal();
}
scroll = GUILayout.BeginScrollView(scroll, GUI.skin.scrollView);
GUILayout.Space(10);
if (m_filter == MemberFilter.Both || m_filter == MemberFilter.Field)
{
DrawMembers(this.m_FieldInfos, "Fields");
}
if (m_filter == MemberFilter.Both || m_filter == MemberFilter.Property)
{
DrawMembers(this.m_PropertyInfos, "Properties");
}
DrawMembers(this.m_cachedMemberFiltered);
GUILayout.EndScrollView();
@ -259,28 +269,59 @@ namespace Explorer
}
}
private void DrawMembers(MemberInfoHolder[] members, string title)
private void DrawMembers(CacheObject[] members)
{
// todo pre-cache list based on current search, otherwise this doesnt work.
int i = 0;
DrawMembersInternal("Properties", MemberInfoType.Property, members, ref i);
DrawMembersInternal("Fields", MemberInfoType.Field, members, ref i);
}
private void DrawMembersInternal(string title, MemberInfoType filter, CacheObject[] members, ref int index)
{
if (m_filter != filter && m_filter != MemberInfoType.All)
{
return;
}
UIStyles.HorizontalLine(Color.grey);
GUILayout.Label($"<size=18><b><color=gold>{title}</color></b></size>", null);
foreach (var holder in members)
{
var memberName = (holder as FieldInfoHolder)?.fieldInfo.Name ?? (holder as PropertyInfoHolder)?.propInfo.Name;
int offset = (m_pageOffset * m_limitPerPage) + index;
if (m_search != "" && !memberName.ToLower().Contains(m_search.ToLower()))
{
continue;
}
if (offset >= m_cachedMemberFiltered.Length)
{
m_pageOffset = 0;
offset = 0;
}
for (int j = offset; j < offset + m_limitPerPage && j < members.Length; j++)
{
var holder = members[j];
if (holder.MemberInfoType != filter || !ShouldProcessMember(holder)) continue;
GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Height(25) });
holder.Draw(this);
try
{
holder.Draw(this.m_rect, 180f);
}
catch // (Exception e)
{
//MelonLogger.Log("Exception drawing member " + holder.MemberInfo.Name);
//MelonLogger.Log(e.GetType() + ", " + e.Message);
//MelonLogger.Log(e.StackTrace);
}
GUILayout.EndHorizontal();
index++;
if (index >= m_limitPerPage) break;
}
}
private void FilterToggle(MemberFilter mode, string label)
private void FilterToggle(MemberInfoType mode, string label)
{
if (m_filter == mode)
{

View File

@ -23,9 +23,12 @@ namespace Explorer
public Vector2 scroll = Vector2.zero;
public abstract void Init();
public abstract void WindowFunction(int windowID);
public abstract void Update();
public static UIWindow CreateWindow<T>(object target) where T : UIWindow
{
//var component = (UIWindow)AddToGameObject<T>(Instance.gameObject);
var window = Activator.CreateInstance<T>();
window.Target = target;
@ -50,13 +53,8 @@ namespace Explorer
MelonLogger.Log("Exception removing Window from WindowManager.Windows list!");
MelonLogger.Log($"{e.GetType()} : {e.Message}\r\n{e.StackTrace}");
}
//Destroy(this);
}
public abstract void Init();
public abstract void WindowFunction(int windowID);
public abstract void Update();
public void OnGUI()
{
if (CppExplorer.ShowMenu)