mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-09-20 13:36:01 +08:00
Fix some issues in IL2CPP, improve type cache efficiency, reduce alloc
This commit is contained in:
@ -26,7 +26,7 @@ namespace UnityExplorer.UI.CacheObject
|
||||
{
|
||||
try
|
||||
{
|
||||
var ret = FieldInfo.GetValue(this.Owner.Target.TryCast(this.DeclaringType));
|
||||
var ret = FieldInfo.GetValue(DeclaringInstance);
|
||||
HadException = false;
|
||||
LastException = null;
|
||||
return ret;
|
||||
|
@ -42,7 +42,7 @@ namespace UnityExplorer.UI.CacheObject
|
||||
{
|
||||
KeyInputWanted = true;
|
||||
KeyInputText = key.ToString();
|
||||
KeyInputTypeText = SignatureHighlighter.ParseType(type, false);
|
||||
KeyInputTypeText = SignatureHighlighter.Parse(type, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -18,6 +18,8 @@ namespace UnityExplorer.UI.CacheObject
|
||||
|
||||
public abstract Type DeclaringType { get; }
|
||||
public string NameForFiltering { get; protected set; }
|
||||
public object DeclaringInstance => IsStatic ? null : (m_declaringInstance ?? (m_declaringInstance = Owner.Target.TryCast(DeclaringType)));
|
||||
private object m_declaringInstance;
|
||||
|
||||
public abstract bool IsStatic { get; }
|
||||
public override bool HasArguments => Arguments?.Length > 0 || GenericArguments.Length > 0;
|
||||
@ -29,7 +31,7 @@ namespace UnityExplorer.UI.CacheObject
|
||||
public virtual void SetInspectorOwner(ReflectionInspector inspector, MemberInfo member)
|
||||
{
|
||||
this.Owner = inspector;
|
||||
this.NameLabelText = SignatureHighlighter.ParseFullSyntax(member.DeclaringType, false, member);
|
||||
this.NameLabelText = SignatureHighlighter.Parse(member.DeclaringType, false, member);
|
||||
this.NameForFiltering = $"{member.DeclaringType.Name}.{member.Name}";
|
||||
}
|
||||
|
||||
|
@ -34,12 +34,10 @@ namespace UnityExplorer.UI.CacheObject
|
||||
if (methodInfo.IsGenericMethod)
|
||||
methodInfo = MethodInfo.MakeGenericMethod(Evaluator.TryParseGenericArguments());
|
||||
|
||||
var target = MethodInfo.IsStatic ? null : Owner.Target.TryCast(DeclaringType);
|
||||
|
||||
if (Arguments.Length > 0)
|
||||
return methodInfo.Invoke(target, Evaluator.TryParseArguments());
|
||||
return methodInfo.Invoke(DeclaringInstance, Evaluator.TryParseArguments());
|
||||
|
||||
var ret = methodInfo.Invoke(target, new object[0]);
|
||||
var ret = methodInfo.Invoke(DeclaringInstance, new object[0]);
|
||||
|
||||
HadException = false;
|
||||
LastException = null;
|
||||
|
@ -5,6 +5,7 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.Core.Runtime;
|
||||
using UnityExplorer.UI.CacheObject.Views;
|
||||
using UnityExplorer.UI.IValues;
|
||||
using UnityExplorer.UI.ObjectPool;
|
||||
@ -94,6 +95,8 @@ namespace UnityExplorer.UI.CacheObject
|
||||
|
||||
// Updating and applying values
|
||||
|
||||
public abstract void SetUserValue(object value);
|
||||
|
||||
public virtual void SetValueFromSource(object value)
|
||||
{
|
||||
this.Value = value;
|
||||
@ -118,8 +121,6 @@ namespace UnityExplorer.UI.CacheObject
|
||||
this.IValue.SetValue(Value);
|
||||
}
|
||||
|
||||
public abstract void SetUserValue(object value);
|
||||
|
||||
/// <summary>
|
||||
/// Process the CacheMember state when the value has been evaluated (or re-evaluated)
|
||||
/// </summary>
|
||||
@ -137,7 +138,7 @@ namespace UnityExplorer.UI.CacheObject
|
||||
State = ValueState.Boolean;
|
||||
else if (type.IsPrimitive || type == typeof(decimal))
|
||||
State = ValueState.Number;
|
||||
else if (type == typeof(string))
|
||||
else if (ReflectionProvider.Instance.IsString(Value))
|
||||
State = ValueState.String;
|
||||
else if (type.IsEnum)
|
||||
State = ValueState.Enum;
|
||||
@ -162,14 +163,14 @@ namespace UnityExplorer.UI.CacheObject
|
||||
switch (State)
|
||||
{
|
||||
case ValueState.NotEvaluated:
|
||||
label = $"<i>{NOT_YET_EVAL} ({SignatureHighlighter.ParseType(FallbackType, true)})</i>"; break;
|
||||
label = $"<i>{NOT_YET_EVAL} ({SignatureHighlighter.Parse(FallbackType, true)})</i>"; break;
|
||||
case ValueState.Exception:
|
||||
label = $"<i><color=red>{ReflectionUtility.ReflectionExToString(LastException)}</color></i>"; break;
|
||||
case ValueState.Boolean:
|
||||
case ValueState.Number:
|
||||
label = null; break;
|
||||
case ValueState.String:
|
||||
string s = Value as string;
|
||||
string s = ReflectionProvider.Instance.UnboxString(Value);
|
||||
if (s.Length > 200)
|
||||
s = $"{s.Substring(0, 200)}...";
|
||||
label = $"\"{s}\""; break;
|
||||
@ -252,7 +253,7 @@ namespace UnityExplorer.UI.CacheObject
|
||||
|
||||
cell.TypeLabel.gameObject.SetActive(args.typeLabelActive);
|
||||
if (args.typeLabelActive)
|
||||
cell.TypeLabel.text = SignatureHighlighter.ParseType(Value.GetActualType(), false);
|
||||
cell.TypeLabel.text = SignatureHighlighter.Parse(Value.GetActualType(), false);
|
||||
|
||||
cell.Toggle.gameObject.SetActive(args.toggleActive);
|
||||
if (args.toggleActive)
|
||||
|
@ -28,12 +28,10 @@ namespace UnityExplorer.UI.CacheObject
|
||||
{
|
||||
try
|
||||
{
|
||||
var target = IsStatic ? null : Owner.Target.TryCast(DeclaringType);
|
||||
|
||||
if (HasArguments)
|
||||
return PropertyInfo.GetValue(target, this.Evaluator.TryParseArguments());
|
||||
return PropertyInfo.GetValue(DeclaringInstance, this.Evaluator.TryParseArguments());
|
||||
|
||||
var ret = PropertyInfo.GetValue(target, null);
|
||||
var ret = PropertyInfo.GetValue(DeclaringInstance, null);
|
||||
HadException = false;
|
||||
LastException = null;
|
||||
return ret;
|
||||
|
@ -38,7 +38,7 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
|
||||
Image = root.AddComponent<Image>();
|
||||
|
||||
this.NameLayout.minWidth = 40;
|
||||
this.NameLayout.minWidth = 55;
|
||||
this.NameLayout.flexibleWidth = 50;
|
||||
this.NameLayout.minHeight = 30;
|
||||
this.NameLayout.flexibleHeight = 0;
|
||||
|
@ -31,6 +31,7 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
private GameObject genericArgHolder;
|
||||
private readonly List<GameObject> genericArgRows = new List<GameObject>();
|
||||
private readonly List<Text> genericArgLabels = new List<Text>();
|
||||
private readonly List<TypeCompleter> genericAutocompleters = new List<TypeCompleter>();
|
||||
|
||||
private readonly List<InputField> inputFieldCache = new List<InputField>();
|
||||
|
||||
@ -153,9 +154,12 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
|
||||
genericArgRows[i].SetActive(true);
|
||||
|
||||
var constraints = arg.GetGenericParameterConstraints();
|
||||
var autoCompleter = genericAutocompleters[i];
|
||||
autoCompleter.BaseType = arg;
|
||||
autoCompleter.CacheTypes();
|
||||
|
||||
// TODO show "class" constraints as they dont show up, "struct" does effectively.
|
||||
var constraints = arg.GetGenericParameterConstraints();
|
||||
autoCompleter.GenericConstraints = constraints;
|
||||
|
||||
var sb = new StringBuilder($"<color={SignatureHighlighter.CONST}>{arg.Name}</color>");
|
||||
|
||||
@ -164,7 +168,7 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
if (j == 0) sb.Append(' ').Append('(');
|
||||
else sb.Append(',').Append(' ');
|
||||
|
||||
sb.Append(SignatureHighlighter.ParseType(constraints[j]));
|
||||
sb.Append(SignatureHighlighter.Parse(constraints[j], false));
|
||||
|
||||
if (j + 1 == constraints.Length)
|
||||
sb.Append(')');
|
||||
@ -194,19 +198,19 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
AddArgRow(i, false);
|
||||
|
||||
argRows[i].SetActive(true);
|
||||
argLabels[i].text = $"{SignatureHighlighter.ParseType(arg.ParameterType)} <color={SignatureHighlighter.LOCAL_ARG}>{arg.Name}</color>";
|
||||
argLabels[i].text = $"{SignatureHighlighter.Parse(arg.ParameterType, false)} <color={SignatureHighlighter.LOCAL_ARG}>{arg.Name}</color>";
|
||||
}
|
||||
}
|
||||
|
||||
private void AddArgRow(int index, bool generic)
|
||||
{
|
||||
if (!generic)
|
||||
AddArgRow(index, argHolder, argRows, argLabels, argumentInput);//, false);
|
||||
AddArgRow(index, argHolder, argRows, argLabels, argumentInput, false);
|
||||
else
|
||||
AddArgRow(index, genericArgHolder, genericArgRows, genericArgLabels, genericInput);//, true);
|
||||
AddArgRow(index, genericArgHolder, genericArgRows, genericArgLabels, genericInput, true);
|
||||
}
|
||||
|
||||
private void AddArgRow(int index, GameObject parent, List<GameObject> objectList, List<Text> labelList, string[] inputArray)//, bool autocomplete)
|
||||
private void AddArgRow(int index, GameObject parent, List<GameObject> objectList, List<Text> labelList, string[] inputArray, bool autocomplete)
|
||||
{
|
||||
var horiGroup = UIFactory.CreateUIObject("ArgRow_" + index, parent);
|
||||
UIFactory.SetLayoutElement(horiGroup, minHeight: 25, flexibleHeight: 50, minWidth: 50, flexibleWidth: 9999);
|
||||
@ -225,6 +229,9 @@ namespace UnityExplorer.UI.CacheObject.Views
|
||||
inputObj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
inputField.onValueChanged.AddListener((string val) => { inputArray[index] = val; });
|
||||
inputFieldCache.Add(inputField);
|
||||
|
||||
if (autocomplete)
|
||||
genericAutocompleters.Add(new TypeCompleter(null, inputField));
|
||||
}
|
||||
|
||||
public GameObject CreateContent(GameObject parent)
|
||||
|
@ -86,7 +86,7 @@ namespace UnityExplorer.UI.IValues
|
||||
|
||||
CacheEntries(value);
|
||||
|
||||
TopLabel.text = $"[{cachedEntries.Count}] {SignatureHighlighter.ParseType(type, false)}";
|
||||
TopLabel.text = $"[{cachedEntries.Count}] {SignatureHighlighter.Parse(type, false)}";
|
||||
}
|
||||
|
||||
|
||||
|
@ -78,7 +78,7 @@ namespace UnityExplorer.UI.IValues
|
||||
|
||||
CacheEntries(value);
|
||||
|
||||
TopLabel.text = $"[{cachedEntries.Count}] {SignatureHighlighter.ParseType(type, false)}";
|
||||
TopLabel.text = $"[{cachedEntries.Count}] {SignatureHighlighter.Parse(type, false)}";
|
||||
}
|
||||
|
||||
//this.ScrollPoolLayout.minHeight = Math.Min(400f, 35f * values.Count);
|
||||
|
@ -160,7 +160,7 @@ namespace UnityExplorer.UI.Inspectors
|
||||
|
||||
if (!compToStringCache.ContainsKey(type.AssemblyQualifiedName))
|
||||
{
|
||||
compToStringCache.Add(type.AssemblyQualifiedName, SignatureHighlighter.ParseType(type, true));
|
||||
compToStringCache.Add(type.AssemblyQualifiedName, SignatureHighlighter.Parse(type, true));
|
||||
}
|
||||
|
||||
cell.Button.ButtonText.text = compToStringCache[type.AssemblyQualifiedName];
|
||||
|
@ -128,8 +128,8 @@ namespace UnityExplorer.UI.Inspectors
|
||||
}
|
||||
|
||||
// Setup main labels and tab text
|
||||
Tab.TabText.text = $"{prefix} {SignatureHighlighter.ParseType(TargetType)}";
|
||||
NameText.text = SignatureHighlighter.ParseFullSyntax(TargetType, true);
|
||||
Tab.TabText.text = $"{prefix} {SignatureHighlighter.Parse(TargetType, false)}";
|
||||
NameText.text = SignatureHighlighter.Parse(TargetType, true);
|
||||
|
||||
string asmText;
|
||||
if (TargetType.Assembly != null && !string.IsNullOrEmpty(TargetType.Assembly.Location))
|
||||
@ -230,6 +230,7 @@ namespace UnityExplorer.UI.Inspectors
|
||||
|
||||
private void UpdateDisplayedMembers()// bool onlyAutoUpdate)
|
||||
{
|
||||
ExplorerCore.Log("Updating values...");
|
||||
bool shouldRefresh = false;
|
||||
foreach (var cell in MemberScrollPool.CellPool)
|
||||
{
|
||||
@ -238,6 +239,7 @@ namespace UnityExplorer.UI.Inspectors
|
||||
var member = cell.MemberOccupant;
|
||||
if (member.ShouldAutoEvaluate) // && (!onlyAutoUpdate || member.AutoUpdateWanted))
|
||||
{
|
||||
ExplorerCore.Log("Evaluating cell " + cell.MemberOccupant.NameForFiltering);
|
||||
shouldRefresh = true;
|
||||
member.Evaluate();
|
||||
member.SetDataToCell(member.CellView);
|
||||
|
@ -79,9 +79,9 @@ namespace UnityExplorer.UI.Panels
|
||||
lastCheckedTypeInput = desiredTypeInput;
|
||||
|
||||
//var type = ReflectionUtility.GetTypeByName(desiredTypeInput);
|
||||
if (typeAutocompleter.AllTypes.TryGetValue(desiredTypeInput, out var cachedType))
|
||||
if (ReflectionUtility.AllTypes.TryGetValue(desiredTypeInput, out var cachedType))
|
||||
{
|
||||
var type = cachedType.Type;
|
||||
var type = cachedType;
|
||||
lastTypeCanHaveGO = typeof(Component).IsAssignableFrom(type) || type == typeof(GameObject);
|
||||
sceneFilterRow.SetActive(lastTypeCanHaveGO);
|
||||
childFilterRow.SetActive(lastTypeCanHaveGO);
|
||||
@ -134,7 +134,7 @@ namespace UnityExplorer.UI.Panels
|
||||
{
|
||||
string text;
|
||||
if (m_context == SearchContext.StaticClass)
|
||||
text = SignatureHighlighter.ParseType(currentResults[index] as Type, true, true);
|
||||
text = SignatureHighlighter.Parse(currentResults[index] as Type, true);
|
||||
else
|
||||
text = ToStringUtility.ToStringWithType(currentResults[index], currentResults[index]?.GetActualType());
|
||||
|
||||
|
@ -108,6 +108,8 @@ namespace UnityExplorer.UI
|
||||
if (!ShowMenu)
|
||||
return;
|
||||
|
||||
gcLabel.text = "GC : " + (GC.GetTotalMemory(false) / 1024 / 1024) + "MB";
|
||||
|
||||
if (InputManager.GetKeyDown(ConfigManager.Force_Unlock_Keybind.Value))
|
||||
CursorUnlocker.Unlock = !CursorUnlocker.Unlock;
|
||||
|
||||
@ -221,6 +223,8 @@ namespace UnityExplorer.UI
|
||||
//private static float lastTimeSpeed;
|
||||
//private static bool pausing;
|
||||
|
||||
private static Text gcLabel;
|
||||
|
||||
private static void CreateTopNavBar()
|
||||
{
|
||||
var navbarPanel = UIFactory.CreateUIObject("MainNavbar", CanvasRoot);
|
||||
@ -238,6 +242,11 @@ namespace UnityExplorer.UI
|
||||
var title = UIFactory.CreateLabel(navbarPanel, "Title", titleTxt, TextAnchor.MiddleLeft, default, true, 18);
|
||||
UIFactory.SetLayoutElement(title.gameObject, minWidth: 240, flexibleWidth: 0);
|
||||
|
||||
// temp debug
|
||||
|
||||
gcLabel = UIFactory.CreateLabel(navbarPanel, "GCLabel", "GC: ", TextAnchor.MiddleLeft);
|
||||
UIFactory.SetLayoutElement(gcLabel.gameObject, minWidth: 150, minHeight: 25, flexibleWidth: 0);
|
||||
|
||||
// TODO something nicer for this, maybe a 'Tools' dropout below the main navbar with a few helpers like this.
|
||||
|
||||
//var btn = UIFactory.CreateButton(navbarPanel, "Button", "pause", new Color(0.2f, 0.2f, 0.2f));
|
||||
|
@ -68,7 +68,7 @@ namespace UnityExplorer.UI.Utility
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string ParseFullSyntax(Type type, bool includeNamespace, MemberInfo memberInfo = null)
|
||||
public static string Parse(Type type, bool includeNamespace, MemberInfo memberInfo = null)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
@ -136,30 +136,30 @@ 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();
|
||||
}
|
||||
//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>();
|
||||
|
||||
@ -182,6 +182,7 @@ namespace UnityExplorer.UI.Utility
|
||||
private static string HighlightType(Type type)
|
||||
{
|
||||
string key = type.ToString();
|
||||
|
||||
if (typeToRichType.ContainsKey(key))
|
||||
return typeToRichType[key];
|
||||
|
||||
@ -265,7 +266,7 @@ namespace UnityExplorer.UI.Utility
|
||||
}
|
||||
|
||||
//ret += ParseType(args[i]);
|
||||
sb.Append(ParseType(args[i]));
|
||||
sb.Append(HighlightType(args[i]));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
|
@ -30,7 +30,7 @@ namespace UnityExplorer.UI.Utility
|
||||
|
||||
Type type = value?.GetActualType() ?? fallbackType;
|
||||
|
||||
string richType = SignatureHighlighter.ParseFullSyntax(type, includeNamespace);
|
||||
string richType = SignatureHighlighter.Parse(type, includeNamespace);
|
||||
|
||||
_stringBuilder.Clear();
|
||||
|
||||
|
@ -8,17 +8,13 @@ using UnityEngine.UI;
|
||||
using UnityExplorer.Core.Runtime;
|
||||
using UnityExplorer.UI.Models;
|
||||
using UnityExplorer.UI.Panels;
|
||||
using UnityExplorer.UI.Utility;
|
||||
|
||||
namespace UnityExplorer.UI.Widgets.AutoComplete
|
||||
{
|
||||
public class TypeCompleter : ISuggestionProvider
|
||||
{
|
||||
public class CachedType
|
||||
{
|
||||
public Type Type;
|
||||
public string FullNameValue;
|
||||
public string DisplayName;
|
||||
}
|
||||
internal static readonly Dictionary<string, string> sharedTypeToLabel = new Dictionary<string, string>(4096);
|
||||
|
||||
public event Action<Suggestion> SuggestionClicked;
|
||||
|
||||
@ -31,10 +27,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
|
||||
private readonly List<Suggestion> suggestions = new List<Suggestion>();
|
||||
private float timeOfLastCheck;
|
||||
|
||||
public Dictionary<string, CachedType> AllTypes = new Dictionary<string, CachedType>();
|
||||
|
||||
// cached type trees from all autocompleters
|
||||
private static readonly Dictionary<string, Dictionary<string, CachedType>> typeCache = new Dictionary<string, Dictionary<string, CachedType>>();
|
||||
private HashSet<Type> allowedTypes;
|
||||
|
||||
public TypeCompleter(Type baseType, InputField inputField)
|
||||
{
|
||||
@ -47,6 +40,11 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
|
||||
CacheTypes();
|
||||
}
|
||||
|
||||
public void CacheTypes()
|
||||
{
|
||||
allowedTypes = ReflectionUtility.GetImplementationsOf(BaseType, true, false);
|
||||
}
|
||||
|
||||
public void OnSuggestionClicked(Suggestion suggestion)
|
||||
{
|
||||
timeOfLastCheck = Time.realtimeSinceStartup;
|
||||
@ -84,61 +82,29 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
|
||||
{
|
||||
suggestions.Clear();
|
||||
|
||||
var added = new HashSet<string>();
|
||||
|
||||
// Check for exact match first
|
||||
if (AllTypes.TryGetValue(value, out CachedType cache))
|
||||
AddSuggestion(cache);
|
||||
|
||||
foreach (var entry in AllTypes.Values)
|
||||
AddSuggestion(entry);
|
||||
|
||||
void AddSuggestion(CachedType entry)
|
||||
if (BaseType == null)
|
||||
{
|
||||
if (entry.FullNameValue == null)
|
||||
entry.FullNameValue = ReflectionProvider.Instance.GetDeobfuscatedType(entry.Type).FullName;
|
||||
|
||||
if (added.Contains(entry.FullNameValue))
|
||||
return;
|
||||
added.Add(entry.FullNameValue);
|
||||
|
||||
if (entry.DisplayName == null)
|
||||
entry.DisplayName = Utility.SignatureHighlighter.ParseFullSyntax(entry.Type, true);
|
||||
|
||||
suggestions.Add(new Suggestion(entry.DisplayName, entry.FullNameValue));
|
||||
}
|
||||
}
|
||||
|
||||
public void CacheTypes()
|
||||
{
|
||||
var key = BaseType.AssemblyQualifiedName;
|
||||
|
||||
if (typeCache.ContainsKey(key))
|
||||
{
|
||||
AllTypes = typeCache[key];
|
||||
ExplorerCore.LogWarning("Autocompleter Base type is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
AllTypes = new Dictionary<string, CachedType>();
|
||||
// Check for exact match first
|
||||
if (ReflectionUtility.AllTypes.TryGetValue(value, out Type t) && allowedTypes.Contains(t))
|
||||
AddSuggestion(t);
|
||||
|
||||
var list = ReflectionUtility.GetImplementationsOf(BaseType, true, false)
|
||||
.Select(it => new CachedType()
|
||||
{
|
||||
Type = it,
|
||||
FullNameValue = ReflectionProvider.Instance.GetDeobfuscatedType(it).FullName
|
||||
})
|
||||
.ToList();
|
||||
|
||||
list.Sort((CachedType a, CachedType b) => a.FullNameValue.CompareTo(b.FullNameValue));
|
||||
|
||||
foreach (var cache in list)
|
||||
foreach (var entry in allowedTypes)
|
||||
{
|
||||
if (AllTypes.ContainsKey(cache.FullNameValue))
|
||||
continue;
|
||||
AllTypes.Add(cache.FullNameValue, cache);
|
||||
if (entry.FullName.ContainsIgnoreCase(value))
|
||||
AddSuggestion(entry);
|
||||
}
|
||||
}
|
||||
|
||||
typeCache.Add(key, AllTypes);
|
||||
void AddSuggestion(Type type)
|
||||
{
|
||||
if (!sharedTypeToLabel.ContainsKey(type.FullName))
|
||||
sharedTypeToLabel.Add(type.FullName, SignatureHighlighter.Parse(type, true));
|
||||
|
||||
suggestions.Add(new Suggestion(sharedTypeToLabel[type.FullName], type.FullName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,6 +40,19 @@ namespace UnityExplorer.UI.Widgets
|
||||
public float DefaultHeight => m_defaultHeight ?? (float)(m_defaultHeight = ScrollPool.PrototypeHeight);
|
||||
private float? m_defaultHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Lookup table for "which data index first appears at this position"<br/>
|
||||
/// Index: DefaultHeight * index from top of data<br/>
|
||||
/// Value: the first data index at this position<br/>
|
||||
/// </summary>
|
||||
private readonly List<int> rangeCache = new List<int>();
|
||||
|
||||
/// <summary>Same as GetRangeIndexOfPosition, except this rounds up to the next division if there was remainder from the previous cell.</summary>
|
||||
private int GetRangeCeilingOfPosition(float position) => (int)Math.Ceiling((decimal)position / (decimal)DefaultHeight);
|
||||
|
||||
/// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary>
|
||||
private int GetRangeFloorOfPosition(float position) => (int)Math.Floor((decimal)position / (decimal)DefaultHeight);
|
||||
|
||||
/// <summary>Get the data index at the specified position of the total height cache.</summary>
|
||||
public int GetFirstDataIndexAtPosition(float desiredHeight) => GetFirstDataIndexAtPosition(desiredHeight, out _);
|
||||
|
||||
@ -81,19 +94,6 @@ namespace UnityExplorer.UI.Widgets
|
||||
return dataIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lookup table for "which data index first appears at this position"<br/>
|
||||
/// Index: DefaultHeight * index from top of data<br/>
|
||||
/// Value: the first data index at this position<br/>
|
||||
/// </summary>
|
||||
private readonly List<int> rangeCache = new List<int>();
|
||||
|
||||
/// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary>
|
||||
private int GetRangeFloorOfPosition(float position) => (int)Math.Floor((decimal)position / (decimal)DefaultHeight);
|
||||
|
||||
/// <summary>Same as GetRangeIndexOfPosition, except this rounds up to the next division if there was remainder from the previous cell.</summary>
|
||||
private int GetRangeCeilingOfPosition(float position) => (int)Math.Ceiling((decimal)position / (decimal)DefaultHeight);
|
||||
|
||||
/// <summary>
|
||||
/// Get the spread of the height, starting from the start position.<br/><br/>
|
||||
/// The "spread" begins at the start of the next interval of the DefaultHeight, then increases for
|
||||
@ -116,6 +116,8 @@ namespace UnityExplorer.UI.Widgets
|
||||
/// <summary>Append a data index to the cache with the provided height value.</summary>
|
||||
public void Add(float value)
|
||||
{
|
||||
value = (float)Math.Floor(value);
|
||||
|
||||
int spread = GetRangeSpread(totalHeight, value);
|
||||
|
||||
heightCache.Add(new DataViewInfo()
|
||||
@ -150,6 +152,8 @@ namespace UnityExplorer.UI.Widgets
|
||||
/// <summary>Set a given data index with the specified value.</summary>
|
||||
public void SetIndex(int dataIndex, float height)
|
||||
{
|
||||
height = (float)Math.Floor(height);
|
||||
|
||||
// If the index being set is beyond the DataSource item count, prune and return.
|
||||
if (dataIndex >= ScrollPool.DataSource.ItemCount)
|
||||
{
|
||||
@ -189,20 +193,17 @@ namespace UnityExplorer.UI.Widgets
|
||||
int spread = GetRangeSpread(cache.startPosition, height);
|
||||
|
||||
// If the previous item in the range cache is not the previous data index, there is a gap.
|
||||
if (dataIndex > 0 && rangeCache.Count > rangeIndex && rangeCache[rangeIndex - 1] != (dataIndex - 1))
|
||||
if (dataIndex > 0 && rangeCache[rangeIndex - 1] != (dataIndex - 1))
|
||||
{
|
||||
// Recalculate start positions up to this index. The gap could be anywhere.
|
||||
RecalculateStartPositions(dataIndex);
|
||||
// Recalculate start positions up to this index. The gap could be anywhere before here.
|
||||
RecalculateStartPositions(dataIndex + 1);
|
||||
// Get the range index and spread again after rebuilding
|
||||
rangeIndex = GetRangeCeilingOfPosition(cache.startPosition);
|
||||
spread = GetRangeSpread(cache.startPosition, height);
|
||||
}
|
||||
|
||||
// Should never happen
|
||||
if (rangeCache.Count <= rangeIndex || rangeCache[rangeIndex] != dataIndex)
|
||||
throw new Exception($"Trying to set range index but cache is corrupt after rebuild!\r\n" +
|
||||
$"dataIndex: {dataIndex}, rangeIndex: {rangeIndex}, rangeCache.Count: {rangeCache.Count}, " +
|
||||
$"startPos: {cache.startPosition}/{TotalHeight}");
|
||||
throw new Exception("ScrollPool data height cache is corrupt or invalid, rebuild failed!");
|
||||
|
||||
if (spread != cache.normalizedSpread)
|
||||
{
|
||||
@ -217,13 +218,21 @@ namespace UnityExplorer.UI.Widgets
|
||||
{
|
||||
if (spreadDiff > 0)
|
||||
{
|
||||
for (int i = 0; i < spreadDiff; i++)
|
||||
while (rangeCache[rangeIndex] == dataIndex && spreadDiff > 0)
|
||||
{
|
||||
rangeCache.Insert(rangeIndex, dataIndex);
|
||||
spreadDiff--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < -spreadDiff; i++)
|
||||
while (rangeCache[rangeIndex] == dataIndex && spreadDiff < 0)
|
||||
{
|
||||
rangeCache.RemoveAt(rangeIndex);
|
||||
spreadDiff++;
|
||||
}
|
||||
//for (int i = 0; i < -spreadDiff; i++)
|
||||
// rangeCache.RemoveAt(rangeIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,20 +242,37 @@ namespace UnityExplorer.UI.Widgets
|
||||
return;
|
||||
|
||||
DataViewInfo cache;
|
||||
DataViewInfo prev = heightCache[0];
|
||||
for (int i = 1; i <= toIndex && i < heightCache.Count; i++)
|
||||
DataViewInfo prev = null;
|
||||
for (int i = 0; i <= toIndex && i < heightCache.Count; i++)
|
||||
{
|
||||
cache = heightCache[i];
|
||||
|
||||
cache.startPosition = prev.startPosition + prev.height;
|
||||
if (prev != null)
|
||||
cache.startPosition = prev.startPosition + prev.height;
|
||||
else
|
||||
cache.startPosition = 0;
|
||||
|
||||
var prevSpread = cache.normalizedSpread;
|
||||
var origSpread = cache.normalizedSpread;
|
||||
cache.normalizedSpread = GetRangeSpread(cache.startPosition, cache.height);
|
||||
if (cache.normalizedSpread != prevSpread)
|
||||
SetSpread(i, GetRangeCeilingOfPosition(cache.startPosition), cache.normalizedSpread - prevSpread);
|
||||
if (cache.normalizedSpread != origSpread)
|
||||
SetSpread(i, GetRangeCeilingOfPosition(cache.startPosition), cache.normalizedSpread - origSpread);
|
||||
|
||||
prev = cache;
|
||||
}
|
||||
}
|
||||
|
||||
//private void HardRebuildRanges()
|
||||
//{
|
||||
// var tempList = new List<float>();
|
||||
// for (int i = 0; i < heightCache.Count; i++)
|
||||
// tempList.Add(heightCache[i]);
|
||||
//
|
||||
// heightCache.Clear();
|
||||
// rangeCache.Clear();
|
||||
// totalHeight = 0;
|
||||
//
|
||||
// for (int i = 0; i < tempList.Count; i++)
|
||||
// SetIndex(i, tempList[i]);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user