Reflection cleanup, fix il2cpp struct and enum boxing

And temp removing il2cpp IDictionary / IEnumerable helpers, will see what is necessary after knah's rewrite.
This commit is contained in:
Sinai
2021-05-07 01:22:55 +10:00
parent 1ee10c2507
commit 8534c08f49
18 changed files with 716 additions and 960 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -96,10 +97,7 @@ namespace UnityExplorer.UI.CacheObject
public void SetUserValue(object value)
{
if (State == ValueState.String)
ReflectionProvider.Instance.BoxStringToType(ref value, FallbackType);
else
value = value.TryCast(FallbackType);
value = value.TryCast(FallbackType);
TrySetUserValue(value);
}
@ -147,16 +145,16 @@ namespace UnityExplorer.UI.CacheObject
State = ValueState.Boolean;
else if (type.IsPrimitive || type == typeof(decimal))
State = ValueState.Number;
else if (ReflectionProvider.Instance.IsString(Value))
else if (type == typeof(string))
State = ValueState.String;
else if (type.IsEnum)
State = ValueState.Enum;
// todo Color and ValueStruct
else if (type.IsDictionary())
else if (typeof(IDictionary).IsAssignableFrom(type))
State = ValueState.Dictionary;
else if (type.IsEnumerable())
else if (typeof(IEnumerable).IsAssignableFrom(type))
State = ValueState.Collection;
else
State = ValueState.Unsupported;
@ -174,12 +172,12 @@ namespace UnityExplorer.UI.CacheObject
case ValueState.NotEvaluated:
label = $"<i>{NOT_YET_EVAL} ({SignatureHighlighter.Parse(FallbackType, true)})</i>"; break;
case ValueState.Exception:
label = $"<i><color=red>{ReflectionUtility.ReflectionExToString(LastException)}</color></i>"; break;
label = $"<i><color=red>{LastException.ReflectionExToString()}</color></i>"; break;
case ValueState.Boolean:
case ValueState.Number:
label = null; break;
case ValueState.String:
string s = ReflectionProvider.Instance.UnboxString(Value);
string s = Value as string;
if (s.Length > 200)
s = $"{s.Substring(0, 200)}...";
label = $"\"{s}\""; break;

View File

@ -60,7 +60,7 @@ namespace UnityExplorer.UI.Utility
return CLASS_INSTANCE;
}
private static readonly StringBuilder syntaxBuilder = new StringBuilder(2156);
//private static readonly StringBuilder syntaxBuilder = new StringBuilder(2156);
private static bool GetNamespace(Type type, out string ns)
{
@ -73,7 +73,7 @@ namespace UnityExplorer.UI.Utility
if (type == null)
throw new ArgumentNullException("type");
syntaxBuilder.Clear();
var syntaxBuilder = new StringBuilder();
// Namespace
@ -105,12 +105,6 @@ namespace UnityExplorer.UI.Utility
{
syntaxBuilder.Append('.');
//string memColor = GetMemberInfoColor(memberInfo, out bool isStatic);
//if (isStatic)
// syntaxBuilder.Append(OPEN_ITALIC);
//syntaxBuilder.Append($"<color={memColor}>{memberInfo.Name}{CLOSE_COLOR}");
int start = syntaxBuilder.Length - 1;
syntaxBuilder.Append(OPEN_COLOR)
.Append(GetMemberInfoColor(memberInfo, out bool isStatic))
@ -128,7 +122,6 @@ namespace UnityExplorer.UI.Utility
{
var args = method.GetGenericArguments();
if (args.Length > 0)
//syntaxBuilder.Append($"<{ParseGenericArgs(args, true)}>");
syntaxBuilder.Append('<').Append(ParseGenericArgs(args, true)).Append('>');
}
}
@ -136,31 +129,6 @@ namespace UnityExplorer.UI.Utility
return syntaxBuilder.ToString();
}
//public static string ParseType(Type type, bool includeNamespace = false, bool includeDllName = false)
//{
// var sb = new StringBuilder();
//
// bool isGeneric = type.IsGenericParameter || (type.HasElementType && type.GetElementType().IsGenericParameter);
//
// if (!isGeneric && includeNamespace && GetNamespace(type, out string ns))
// //sb.Append($"<color={NAMESPACE}>{ns}{CLOSE_COLOR}.");
// sb.Append(OPEN_COLOR).Append(NAMESPACE).Append('>').Append(ns).Append(CLOSE_COLOR).Append('.');
//
// sb.Append(HighlightType(type));
//
// if (includeDllName)
// {
// if (!string.IsNullOrEmpty(type.Assembly.Location))
// //sb.Append($" ({Path.GetFileName(type.Assembly.Location)})");
// sb.Append(' ').Append('(').Append(Path.GetFileName(type.Assembly.Location)).Append(')');
// else
// //sb.Append($" ({type.Assembly.GetName().Name})");
// sb.Append(' ').Append('(').Append(type.Assembly.GetName().Name).Append(')');
// }
//
// return sb.ToString();
//}
private static readonly Dictionary<string, string> typeToRichType = new Dictionary<string, string>();
private static bool EndsWith(this StringBuilder sb, string _string)
@ -198,7 +166,6 @@ namespace UnityExplorer.UI.Utility
if (type.IsGenericParameter || (type.HasElementType && type.GetElementType().IsGenericParameter))
{
//typeName = $"<color={CONST}>{typeName}</color>";
sb.Insert(0, $"<color={CONST}>");
sb.Append(CLOSE_COLOR);
}
@ -216,26 +183,22 @@ namespace UnityExplorer.UI.Utility
// make sure the typename actually has expected "`N" format.
if (sb[sb.Length - suffixLen] == '`')
//typeName = typeName.Substring(0, typeName.Length - suffixLen);
sb.Remove(sb.Length - suffixLen, suffixLen);
}
// highlight the base name itself
// do this after removing the `N suffix, so only the name itself is in the color tags.
//typeName = $"<color={GetClassColor(type)}>{typeName}</color>";
sb.Insert(0, $"{OPEN_COLOR}{GetClassColor(type)}>");
sb.Append(CLOSE_COLOR);
// parse the generic args, if any
if (args.Length > 0)
{
//typeName += $"<{ParseGenericArgs(args)}>";
sb.Append('<').Append(ParseGenericArgs(args)).Append('>');
}
}
if (isArray)
//typeName += "[]";
sb.Append('[').Append(']');
var ret = sb.ToString();
@ -248,24 +211,20 @@ namespace UnityExplorer.UI.Utility
{
if (args.Length < 1)
return string.Empty;
//string ret = "";
var sb = new StringBuilder();
for (int i = 0; i < args.Length; i++)
{
if (i > 0)
//ret += ", ";
sb.Append(',').Append(' ');
if (isGenericParams)
{
//ret += $"<color={CONST}>{args[i].Name}</color>";
sb.Append(OPEN_COLOR).Append(CONST).Append('>').Append(args[i].Name).Append(CLOSE_COLOR);
continue;
}
//ret += ParseType(args[i]);
sb.Append(HighlightType(args[i]));
}

View File

@ -70,7 +70,7 @@ namespace UnityExplorer.UI.Utility
{
var toString = ToString(value);
if (type.IsEnumerable())
if (typeof(IEnumerable).IsAssignableFrom(type))
{
if (value is IList iList)
_stringBuilder.Append($"[{iList.Count}] ");
@ -80,7 +80,7 @@ namespace UnityExplorer.UI.Utility
else
_stringBuilder.Append("[?] ");
}
else if (type.IsDictionary())
else if (typeof(IDictionary).IsAssignableFrom(type))
{
if (value is IDictionary iDict)
_stringBuilder.Append($"[{iDict.Count}] ");
@ -167,14 +167,14 @@ namespace UnityExplorer.UI.Utility
}
string _ = null;
toString = ReflectionProvider.Instance.ProcessTypeFullNameInString(type, toString, ref _);
toString = ReflectionUtility.ProcessTypeInString(type, toString, ref _);
#if CPP
if (value is Il2CppSystem.Type cppType)
{
var monoType = Core.Runtime.Il2Cpp.Il2CppReflection.GetMonoType(cppType);
var monoType = Il2CppReflection.GetUnhollowedType(cppType);
if (monoType != null)
toString = ReflectionProvider.Instance.ProcessTypeFullNameInString(monoType, toString, ref _);
toString = ReflectionUtility.ProcessTypeInString(monoType, toString, ref _);
}
#endif