2020-08-22 00:16:05 +10:00
|
|
|
|
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; }
|
2020-08-22 17:17:11 +10:00
|
|
|
|
public string FullName => $"{MemberInfo.DeclaringType.Name}.{MemberInfo.Name}";
|
|
|
|
|
public string ReflectionException;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
2020-08-22 17:17:11 +10:00
|
|
|
|
// methods
|
2020-08-22 00:16:05 +10:00
|
|
|
|
public abstract void DrawValue(Rect window, float width);
|
|
|
|
|
public abstract void SetValue();
|
|
|
|
|
|
|
|
|
|
public static CacheObject GetCacheObject(object obj)
|
|
|
|
|
{
|
|
|
|
|
return GetCacheObject(obj, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 17:17:11 +10:00
|
|
|
|
/// <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>
|
2020-08-22 00:16:05 +10:00
|
|
|
|
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;
|
|
|
|
|
|
2020-08-22 17:17:11 +10:00
|
|
|
|
if ((obj is Il2CppSystem.Object || typeof(Il2CppSystem.Object).IsAssignableFrom(type))
|
|
|
|
|
&& (type.FullName.Contains("UnityEngine.GameObject") || type.FullName.Contains("UnityEngine.Transform")))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
holder = new CacheGameObject(obj);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-22 17:17:11 +10:00
|
|
|
|
else if (type.IsPrimitive || type == typeof(string))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
holder = new CachePrimitive(obj);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-22 17:17:11 +10:00
|
|
|
|
else if (type.IsEnum)
|
|
|
|
|
{
|
|
|
|
|
holder = new CacheEnum(obj);
|
|
|
|
|
}
|
|
|
|
|
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type) || ReflectionHelpers.IsList(type))
|
|
|
|
|
{
|
|
|
|
|
holder = new CacheList(obj);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
holder = new CacheOther();
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (memberInfo != null)
|
|
|
|
|
{
|
|
|
|
|
holder.MemberInfo = memberInfo;
|
|
|
|
|
holder.DeclaringType = memberInfo.DeclaringType;
|
2020-08-22 17:17:11 +10:00
|
|
|
|
holder.DeclaringInstance = declaringInstance;
|
|
|
|
|
|
2020-08-22 00:16:05 +10:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
GUILayout.Label("<color=cyan>" + FullName + ":</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Space(labelWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 17:17:11 +10:00
|
|
|
|
if (!string.IsNullOrEmpty(ReflectionException))
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Label("<color=red>Reflection failed!</color> (" + ReflectionException + ")", null);
|
|
|
|
|
}
|
|
|
|
|
else if (Value == null)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
GUILayout.Label("<i>null (" + this.ValueType + ")</i>", null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DrawValue(window, window.width - labelWidth - 90);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 17:17:11 +10:00
|
|
|
|
public virtual void UpdateValue()
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
if (MemberInfo == null || !string.IsNullOrEmpty(ReflectionException))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
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;
|
2020-08-22 17:17:11 +10:00
|
|
|
|
bool isStatic = pi.GetAccessors()[0].IsStatic;
|
|
|
|
|
var target = isStatic ? null : DeclaringInstance;
|
|
|
|
|
Value = pi.GetValue(target, null);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-22 17:17:11 +10:00
|
|
|
|
//ReflectionException = null;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-22 17:17:11 +10:00
|
|
|
|
catch (Exception e)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-22 17:17:11 +10:00
|
|
|
|
ReflectionException = ReflectionHelpers.ExceptionToString(e);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|