From ec215a0006225f4279a7d1bfdaa1450856eb0eeb Mon Sep 17 00:00:00 2001 From: Sinai Date: Tue, 11 May 2021 19:16:19 +1000 Subject: [PATCH] Use SortedSet for type name cache, expose OnTypeLoaded event --- src/Core/Reflection/Il2CppReflection.cs | 8 +------- src/Core/Reflection/ReflectionUtility.cs | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Core/Reflection/Il2CppReflection.cs b/src/Core/Reflection/Il2CppReflection.cs index 41513e8..a897253 100644 --- a/src/Core/Reflection/Il2CppReflection.cs +++ b/src/Core/Reflection/Il2CppReflection.cs @@ -24,13 +24,7 @@ namespace UnityExplorer TryLoadGameModules(); BuildDeobfuscationCache(); - AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded; - } - - private void OnAssemblyLoaded(object sender, AssemblyLoadEventArgs args) - { - foreach (var type in args.LoadedAssembly.TryGetTypes()) - TryCacheDeobfuscatedType(type); + OnTypeLoaded += TryCacheDeobfuscatedType; } #region IL2CPP Extern and pointers diff --git a/src/Core/Reflection/ReflectionUtility.cs b/src/Core/Reflection/ReflectionUtility.cs index 7dbdf8e..e64f1cd 100644 --- a/src/Core/Reflection/ReflectionUtility.cs +++ b/src/Core/Reflection/ReflectionUtility.cs @@ -7,6 +7,7 @@ using System.Reflection; using BF = System.Reflection.BindingFlags; using UnityExplorer.Core.Runtime; using System.Text; +using UnityEngine; namespace UnityExplorer { @@ -32,27 +33,26 @@ namespace UnityExplorer #region Type cache + public static Action OnTypeLoaded; + /// Key: Type.FullName public static readonly SortedDictionary AllTypes = new SortedDictionary(StringComparer.OrdinalIgnoreCase); - private static readonly List allTypeNames = new List(); + private static readonly SortedSet allTypeNames = new SortedSet(StringComparer.OrdinalIgnoreCase); private static void SetupTypeCache() { foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) CacheTypes(asm); - allTypeNames.Sort(); - AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded; } private static void AssemblyLoaded(object sender, AssemblyLoadEventArgs args) { - if (args.LoadedAssembly == null) + if (args.LoadedAssembly == null || args.LoadedAssembly.GetName().Name == "completions") return; CacheTypes(args.LoadedAssembly); - allTypeNames.Sort(); } private static void CacheTypes(Assembly asm) @@ -67,6 +67,8 @@ namespace UnityExplorer allTypeNames.Add(type.FullName); } + OnTypeLoaded?.Invoke(type); + foreach (var key in typeInheritance.Keys) { try @@ -224,12 +226,12 @@ namespace UnityExplorer if (!typeInheritance.ContainsKey(key)) { var set = new HashSet(); - for (int i = 0; i < allTypeNames.Count; i++) + foreach (var name in allTypeNames) { - var type = AllTypes[allTypeNames[i]]; - //type = ReflectionProvider.Instance.GetDeobfuscatedType(type); try { + var type = AllTypes[name]; + if (set.Contains(type) || (type.IsAbstract && type.IsSealed) // ignore static classes || (!allowAbstract && type.IsAbstract) @@ -261,11 +263,12 @@ namespace UnityExplorer { var set = new HashSet(); - for (int i = 0; i < allTypeNames.Count; i++) + foreach (var name in allTypeNames) { - var type = AllTypes[allTypeNames[i]]; try { + var type = AllTypes[name]; + if (set.Contains(type) || (type.IsAbstract && type.IsSealed) // ignore static classes || (!allowAbstract && type.IsAbstract)