From d97a36d8f7a302d9f570408add538939b9cff659 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sat, 16 Nov 2019 23:31:59 +0100 Subject: [PATCH] Model: Cache instances of CustomAttributeData --- Il2CppInspector/Il2CppModel.cs | 3 +++ Il2CppInspector/Reflection/CustomAttributeData.cs | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Il2CppInspector/Il2CppModel.cs b/Il2CppInspector/Il2CppModel.cs index c18e227..7cf518e 100644 --- a/Il2CppInspector/Il2CppModel.cs +++ b/Il2CppInspector/Il2CppModel.cs @@ -31,6 +31,9 @@ namespace Il2CppInspector.Reflection // List of all methods ordered by their MethodDefinitionIndex public MethodBase[] MethodsByDefinitionIndex { get; } + // List of all generated CustomAttributeData objects by their index into AttributeTypeIndices + public Dictionary AttributesByIndices { get; } = new Dictionary(); + public Il2CppModel(Il2CppInspector package) { Package = package; TypesByDefinitionIndex = new TypeInfo[package.TypeDefinitions.Length]; diff --git a/Il2CppInspector/Reflection/CustomAttributeData.cs b/Il2CppInspector/Reflection/CustomAttributeData.cs index 9b70f19..3486e9f 100644 --- a/Il2CppInspector/Reflection/CustomAttributeData.cs +++ b/Il2CppInspector/Reflection/CustomAttributeData.cs @@ -15,7 +15,7 @@ namespace Il2CppInspector.Reflection public class CustomAttributeData { // IL2CPP-specific data - private Il2CppInspector package => AttributeType.Assembly.Model.Package; + public Il2CppModel Model => AttributeType.Assembly.Model; public int Index { get; set; } // The type of the attribute @@ -23,7 +23,7 @@ namespace Il2CppInspector.Reflection public (ulong Start, ulong End)? VirtualAddress => // The last one will be wrong but there is no way to calculate it - (package.CustomAttributeGenerators[Index], package.CustomAttributeGenerators[Math.Min(Index + 1, package.CustomAttributeGenerators.Length - 1)]); + (Model.Package.CustomAttributeGenerators[Index], Model.Package.CustomAttributeGenerators[Math.Min(Index + 1, Model.Package.CustomAttributeGenerators.Length - 1)]); public override string ToString() => "[" + AttributeType.FullName + "]"; @@ -41,7 +41,16 @@ namespace Il2CppInspector.Reflection var range = pkg.AttributeTypeRanges[customAttributeIndex]; for (var i = range.start; i < range.start + range.count; i++) { var typeIndex = pkg.AttributeTypeIndices[i]; - yield return new CustomAttributeData { Index = customAttributeIndex, AttributeType = asm.Model.GetTypeFromUsage(typeIndex) }; + + if (asm.Model.AttributesByIndices.TryGetValue(i, out var attribute)) { + yield return attribute; + continue; + } + + attribute = new CustomAttributeData { Index = customAttributeIndex, AttributeType = asm.Model.GetTypeFromUsage(typeIndex) }; + + asm.Model.AttributesByIndices.Add(i, attribute); + yield return attribute; } }