diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index e5d6b46..330d828 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -22,6 +22,9 @@ namespace Il2CppInspector // All method pointers sorted in ascending order (for finding the end of a method) private List sortedMethodPointers { get; } + // Attribute indexes (>=24.1) arranged by customAttributeStart and token + public Dictionary> AttributeIndicesByToken { get; } + // Shortcuts public double Version => Metadata.Version; @@ -158,6 +161,20 @@ namespace Il2CppInspector sortedMethodPointers = Binary.GlobalMethodPointers.OrderBy(m => m).ToList(); if (Version >= 24.2) sortedMethodPointers = Binary.ModuleMethodPointers.SelectMany(module => module.Value).OrderBy(m => m).ToList(); + + // Organize custom attribute indices + if (Version >= 24.1) { + AttributeIndicesByToken = new Dictionary>(); + foreach (var image in Images) { + var attsByToken = new Dictionary(); + for (int i = 0; i < image.customAttributeCount; i++) { + var index = image.customAttributeStart + i; + var token = AttributeTypeRanges[index].token; + attsByToken.Add(token, index); + } + AttributeIndicesByToken.Add(image.customAttributeStart, attsByToken); + } + } } public (ulong Start, ulong End)? GetMethodPointer(Il2CppCodeGenModule module, Il2CppMethodDefinition methodDef) { diff --git a/Il2CppInspector/Il2CppModel.cs b/Il2CppInspector/Il2CppModel.cs index 75e7301..c18e227 100644 --- a/Il2CppInspector/Il2CppModel.cs +++ b/Il2CppInspector/Il2CppModel.cs @@ -113,12 +113,10 @@ namespace Il2CppInspector.Reflection if (Package.Version <= 24.0) return customAttributeIndex; - var image = asm.ImageDefinition; - var imageRange = image.customAttributeStart..(int)(image.customAttributeStart + image.customAttributeCount); - // From v24.1 onwards, token was added to Il2CppCustomAttributeTypeRange and each Il2CppImageDefinition noted the CustomAttributeTypeRanges for the image - var index = Array.FindIndex(Package.AttributeTypeRanges[imageRange], x => x.token == token); - return index == -1 ? -1 : index + image.customAttributeStart; + if (!Package.AttributeIndicesByToken[asm.ImageDefinition.customAttributeStart].TryGetValue(token, out var index)) + return -1; + return index; } } } \ No newline at end of file