Cleanup and fix Singleton search slightly

This commit is contained in:
sinaioutlander 2020-12-14 19:26:59 +11:00
parent 28181e2266
commit 82e52de557

View File

@ -248,6 +248,11 @@ namespace UnityExplorer.UI.Modules
UnityObjectSearch(); UnityObjectSearch();
RefreshResultList(); RefreshResultList();
if (m_results.Length > 0)
m_resultCountText.text = $"{m_results.Length} Results";
else
m_resultCountText.text = "No results...";
} }
internal void StaticClassSearch() internal void StaticClassSearch()
@ -274,6 +279,20 @@ namespace UnityExplorer.UI.Modules
m_results = list.ToArray(); m_results = list.ToArray();
} }
internal string[] s_instanceNames = new string[]
{
"m_instance",
"m_Instance",
"s_instance",
"s_Instance",
"_instance",
"_Instance",
"instance",
"Instance",
"<Instance>k__BackingField",
"<instance>k__BackingField",
};
private void SingletonSearch() private void SingletonSearch()
{ {
m_isStaticClassSearching = false; m_isStaticClassSearching = false;
@ -284,6 +303,8 @@ namespace UnityExplorer.UI.Modules
if (!string.IsNullOrEmpty(m_nameInput.text)) if (!string.IsNullOrEmpty(m_nameInput.text))
nameFilter = m_nameInput.text.ToLower(); nameFilter = m_nameInput.text.ToLower();
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{ {
// All non-static classes // All non-static classes
@ -293,31 +314,36 @@ namespace UnityExplorer.UI.Modules
{ {
if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ToLower().Contains(nameFilter)) if (!string.IsNullOrEmpty(nameFilter) && !type.FullName.ToLower().Contains(nameFilter))
continue; continue;
#if CPP
// First look for an "Instance" Property // Only look for Properties in IL2CPP, not for Mono.
if (type.GetProperty("Instance", ReflectionHelpers.CommonFlags) is PropertyInfo pi PropertyInfo pi;
&& pi.CanRead foreach (var name in s_instanceNames)
&& pi.GetGetMethod(true).IsStatic)
{ {
var instance = pi.GetValue(null, null); pi = type.GetProperty(name, flags);
if (instance != null) if (pi != null)
instances.Add(instance); {
var instance = pi.GetValue(null, null);
if (instance != null)
{
instances.Add(instance);
continue;
}
}
} }
else #endif
// Look for a typical Instance backing field.
FieldInfo fi;
foreach (var name in s_instanceNames)
{ {
// Otherwise, look for a typical Instance backing field. fi = type.GetField(name, flags);
FieldInfo fi; if (fi != null)
fi = type.GetField("m_instance", ReflectionHelpers.CommonFlags);
if (fi == null)
fi = type.GetField("s_instance", ReflectionHelpers.CommonFlags);
if (fi == null)
fi = type.GetField("_instance", ReflectionHelpers.CommonFlags);
if (fi == null)
fi = type.GetField("instance", ReflectionHelpers.CommonFlags);
if (fi != null && fi.IsStatic)
{ {
var instance = fi.GetValue(null); var instance = fi.GetValue(null);
if (instance != null)
{
instances.Add(instance);
continue;
}
} }
} }
} }
@ -444,11 +470,6 @@ namespace UnityExplorer.UI.Modules
} }
m_results = results.ToArray(); m_results = results.ToArray();
if (m_results.Length > 0)
m_resultCountText.text = $"{m_results.Length} Results";
else
m_resultCountText.text = "No results...";
} }
private void OnResultPageTurn() private void OnResultPageTurn()