Use SortedSet for type name cache, expose OnTypeLoaded event

This commit is contained in:
Sinai 2021-05-11 19:16:19 +10:00
parent 6e9bb83099
commit ec215a0006
2 changed files with 14 additions and 17 deletions

View File

@ -24,13 +24,7 @@ namespace UnityExplorer
TryLoadGameModules(); TryLoadGameModules();
BuildDeobfuscationCache(); BuildDeobfuscationCache();
AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded; OnTypeLoaded += TryCacheDeobfuscatedType;
}
private void OnAssemblyLoaded(object sender, AssemblyLoadEventArgs args)
{
foreach (var type in args.LoadedAssembly.TryGetTypes())
TryCacheDeobfuscatedType(type);
} }
#region IL2CPP Extern and pointers #region IL2CPP Extern and pointers

View File

@ -7,6 +7,7 @@ using System.Reflection;
using BF = System.Reflection.BindingFlags; using BF = System.Reflection.BindingFlags;
using UnityExplorer.Core.Runtime; using UnityExplorer.Core.Runtime;
using System.Text; using System.Text;
using UnityEngine;
namespace UnityExplorer namespace UnityExplorer
{ {
@ -32,27 +33,26 @@ namespace UnityExplorer
#region Type cache #region Type cache
public static Action<Type> OnTypeLoaded;
/// <summary>Key: Type.FullName</summary> /// <summary>Key: Type.FullName</summary>
public static readonly SortedDictionary<string, Type> AllTypes = new SortedDictionary<string, Type>(StringComparer.OrdinalIgnoreCase); public static readonly SortedDictionary<string, Type> AllTypes = new SortedDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
private static readonly List<string> allTypeNames = new List<string>(); private static readonly SortedSet<string> allTypeNames = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
private static void SetupTypeCache() private static void SetupTypeCache()
{ {
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
CacheTypes(asm); CacheTypes(asm);
allTypeNames.Sort();
AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded; AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded;
} }
private static void AssemblyLoaded(object sender, AssemblyLoadEventArgs args) private static void AssemblyLoaded(object sender, AssemblyLoadEventArgs args)
{ {
if (args.LoadedAssembly == null) if (args.LoadedAssembly == null || args.LoadedAssembly.GetName().Name == "completions")
return; return;
CacheTypes(args.LoadedAssembly); CacheTypes(args.LoadedAssembly);
allTypeNames.Sort();
} }
private static void CacheTypes(Assembly asm) private static void CacheTypes(Assembly asm)
@ -67,6 +67,8 @@ namespace UnityExplorer
allTypeNames.Add(type.FullName); allTypeNames.Add(type.FullName);
} }
OnTypeLoaded?.Invoke(type);
foreach (var key in typeInheritance.Keys) foreach (var key in typeInheritance.Keys)
{ {
try try
@ -224,12 +226,12 @@ namespace UnityExplorer
if (!typeInheritance.ContainsKey(key)) if (!typeInheritance.ContainsKey(key))
{ {
var set = new HashSet<Type>(); var set = new HashSet<Type>();
for (int i = 0; i < allTypeNames.Count; i++) foreach (var name in allTypeNames)
{ {
var type = AllTypes[allTypeNames[i]];
//type = ReflectionProvider.Instance.GetDeobfuscatedType(type);
try try
{ {
var type = AllTypes[name];
if (set.Contains(type) if (set.Contains(type)
|| (type.IsAbstract && type.IsSealed) // ignore static classes || (type.IsAbstract && type.IsSealed) // ignore static classes
|| (!allowAbstract && type.IsAbstract) || (!allowAbstract && type.IsAbstract)
@ -261,11 +263,12 @@ namespace UnityExplorer
{ {
var set = new HashSet<Type>(); var set = new HashSet<Type>();
for (int i = 0; i < allTypeNames.Count; i++) foreach (var name in allTypeNames)
{ {
var type = AllTypes[allTypeNames[i]];
try try
{ {
var type = AllTypes[name];
if (set.Contains(type) if (set.Contains(type)
|| (type.IsAbstract && type.IsSealed) // ignore static classes || (type.IsAbstract && type.IsSealed) // ignore static classes
|| (!allowAbstract && type.IsAbstract) || (!allowAbstract && type.IsAbstract)