From cd7ddf193a12822e2030e7b7feb27d90ba0580eb Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sun, 10 Jan 2021 08:37:49 +0100 Subject: [PATCH] Model: Implement MetadataToken property --- Il2CppInspector.Common/Outputs/AssemblyShims.cs | 12 ++++++------ Il2CppInspector.Common/Reflection/Assembly.cs | 4 ++++ .../Reflection/CustomAttributeData.cs | 16 ++++++++-------- Il2CppInspector.Common/Reflection/EventInfo.cs | 1 + Il2CppInspector.Common/Reflection/FieldInfo.cs | 1 + Il2CppInspector.Common/Reflection/MemberInfo.cs | 3 +++ Il2CppInspector.Common/Reflection/MethodBase.cs | 1 + .../Reflection/ParameterInfo.cs | 4 ++++ .../Reflection/PropertyInfo.cs | 1 + Il2CppInspector.Common/Reflection/TypeInfo.cs | 1 + Il2CppInspector.Common/Reflection/TypeModel.cs | 4 ++-- 11 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Il2CppInspector.Common/Outputs/AssemblyShims.cs b/Il2CppInspector.Common/Outputs/AssemblyShims.cs index 3bd8d51..77c0e33 100644 --- a/Il2CppInspector.Common/Outputs/AssemblyShims.cs +++ b/Il2CppInspector.Common/Outputs/AssemblyShims.cs @@ -186,7 +186,7 @@ namespace Il2CppInspector.Outputs // Add token attribute if (type.Definition != null) - mType.AddAttribute(module, tokenAttribute, ("Token", $"0x{type.Definition.token:X8}")); + mType.AddAttribute(module, tokenAttribute, ("Token", $"0x{type.MetadataToken:X8}")); // Add custom attribute attributes foreach (var ca in type.CustomAttributes) @@ -222,7 +222,7 @@ namespace Il2CppInspector.Outputs mField.AddAttribute(module, fieldOffsetAttribute, ("Offset", $"0x{field.Offset:X}")); // Add token attribute - mField.AddAttribute(module, tokenAttribute, ("Token", $"0x{field.Definition.token:X8}")); + mField.AddAttribute(module, tokenAttribute, ("Token", $"0x{field.MetadataToken:X8}")); // Add custom attribute attributes foreach (var ca in field.CustomAttributes) @@ -250,7 +250,7 @@ namespace Il2CppInspector.Outputs // Add token attribute // Generic properties and constructed properties (from disperate get/set methods) have no definition if (prop.Definition != null) - mProp.AddAttribute(module, tokenAttribute, ("Token", $"0x{prop.Definition.token:X8}")); + mProp.AddAttribute(module, tokenAttribute, ("Token", $"0x{prop.MetadataToken:X8}")); // Add custom attribute attributes foreach (var ca in prop.CustomAttributes) @@ -270,7 +270,7 @@ namespace Il2CppInspector.Outputs mEvent.InvokeMethod = AddMethod(module, mType, evt.RaiseMethod); // Add token attribute - mEvent.AddAttribute(module, tokenAttribute, ("Token", $"0x{evt.Definition.token:X8}")); + mEvent.AddAttribute(module, tokenAttribute, ("Token", $"0x{evt.MetadataToken:X8}")); // Add custom attribute attributes foreach (var ca in evt.CustomAttributes) @@ -366,7 +366,7 @@ namespace Il2CppInspector.Outputs } // Add token attribute - mMethod.AddAttribute(module, tokenAttribute, ("Token", $"0x{method.Definition.token:X8}")); + mMethod.AddAttribute(module, tokenAttribute, ("Token", $"0x{method.MetadataToken:X8}")); // Add method pointer attribute if (method.VirtualAddress.HasValue) { @@ -489,7 +489,7 @@ namespace Il2CppInspector.Outputs // Add token attributes module.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.ImageDefinition.token:X8}")); - module.Assembly.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.AssemblyDefinition.token:X8}")); + module.Assembly.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.MetadataToken:X8}")); } // Add custom attribute attributes (must do this after all assemblies are created due to type referencing) diff --git a/Il2CppInspector.Common/Reflection/Assembly.cs b/Il2CppInspector.Common/Reflection/Assembly.cs index 312e8d6..a84a6e4 100644 --- a/Il2CppInspector.Common/Reflection/Assembly.cs +++ b/Il2CppInspector.Common/Reflection/Assembly.cs @@ -27,6 +27,9 @@ namespace Il2CppInspector.Reflection { // Display name of the assembly public string ShortName { get; } + // Metadata token of the assembly + public int MetadataToken { get; } + // Entry point method for the assembly public MethodInfo EntryPoint => throw new NotImplementedException(); @@ -45,6 +48,7 @@ namespace Il2CppInspector.Reflection { if (AssemblyDefinition.imageIndex != imageIndex) throw new InvalidOperationException("Assembly/image index mismatch"); + MetadataToken = (int) AssemblyDefinition.token; Index = ImageDefinition.assemblyIndex; ShortName = Model.Package.Strings[ImageDefinition.nameIndex]; diff --git a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs index 39ef449..cfeb275 100644 --- a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs +++ b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs @@ -64,16 +64,16 @@ namespace Il2CppInspector.Reflection } } - private static IList getCustomAttributes(Assembly asm, uint token, int customAttributeIndex) => + private static IList getCustomAttributes(Assembly asm, int token, int customAttributeIndex) => getCustomAttributes(asm, asm.Model.GetCustomAttributeIndex(asm, token, customAttributeIndex)).ToList(); - public static IList GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.AssemblyDefinition.token, asm.AssemblyDefinition.customAttributeIndex); - public static IList GetCustomAttributes(EventInfo evt) => getCustomAttributes(evt.Assembly, evt.Definition.token, evt.Definition.customAttributeIndex); - public static IList GetCustomAttributes(FieldInfo field) => getCustomAttributes(field.Assembly, field.Definition.token, field.Definition.customAttributeIndex); - public static IList GetCustomAttributes(MethodBase method) => getCustomAttributes(method.Assembly, method.Definition.token, method.Definition.customAttributeIndex); - public static IList GetCustomAttributes(ParameterInfo param) => getCustomAttributes(param.DeclaringMethod.Assembly, param.Definition.token, param.Definition.customAttributeIndex); + public static IList GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.MetadataToken, asm.AssemblyDefinition.customAttributeIndex); + public static IList GetCustomAttributes(EventInfo evt) => getCustomAttributes(evt.Assembly, evt.MetadataToken, evt.Definition.customAttributeIndex); + public static IList GetCustomAttributes(FieldInfo field) => getCustomAttributes(field.Assembly, field.MetadataToken, field.Definition.customAttributeIndex); + public static IList GetCustomAttributes(MethodBase method) => getCustomAttributes(method.Assembly, method.MetadataToken, method.Definition.customAttributeIndex); + public static IList GetCustomAttributes(ParameterInfo param) => getCustomAttributes(param.DeclaringMethod.Assembly, param.MetadataToken, param.Definition.customAttributeIndex); public static IList GetCustomAttributes(PropertyInfo prop) - => prop.Definition != null ? getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex) : new List(); - public static IList GetCustomAttributes(TypeInfo type) => type.Definition != null? getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex) : new List(); + => prop.Definition != null ? getCustomAttributes(prop.Assembly, prop.MetadataToken, prop.Definition.customAttributeIndex) : new List(); + public static IList GetCustomAttributes(TypeInfo type) => type.Definition != null? getCustomAttributes(type.Assembly, type.MetadataToken, type.Definition.customAttributeIndex) : new List(); } } diff --git a/Il2CppInspector.Common/Reflection/EventInfo.cs b/Il2CppInspector.Common/Reflection/EventInfo.cs index 3c1bd96..9f5a998 100644 --- a/Il2CppInspector.Common/Reflection/EventInfo.cs +++ b/Il2CppInspector.Common/Reflection/EventInfo.cs @@ -41,6 +41,7 @@ namespace Il2CppInspector.Reflection public EventInfo(Il2CppInspector pkg, int eventIndex, TypeInfo declaringType) : base(declaringType) { Definition = pkg.Events[eventIndex]; + MetadataToken = (int) Definition.token; Index = eventIndex; Name = pkg.Strings[Definition.nameIndex]; rootDefinition = this; diff --git a/Il2CppInspector.Common/Reflection/FieldInfo.cs b/Il2CppInspector.Common/Reflection/FieldInfo.cs index e428b3c..3a37411 100644 --- a/Il2CppInspector.Common/Reflection/FieldInfo.cs +++ b/Il2CppInspector.Common/Reflection/FieldInfo.cs @@ -91,6 +91,7 @@ namespace Il2CppInspector.Reflection { public FieldInfo(Il2CppInspector pkg, int fieldIndex, TypeInfo declaringType) : base(declaringType) { Definition = pkg.Fields[fieldIndex]; + MetadataToken = (int) Definition.token; Index = fieldIndex; Name = pkg.Strings[Definition.nameIndex]; diff --git a/Il2CppInspector.Common/Reflection/MemberInfo.cs b/Il2CppInspector.Common/Reflection/MemberInfo.cs index 71ac6fb..ebc7895 100644 --- a/Il2CppInspector.Common/Reflection/MemberInfo.cs +++ b/Il2CppInspector.Common/Reflection/MemberInfo.cs @@ -25,6 +25,9 @@ namespace Il2CppInspector.Reflection { // What sort of member this is, eg. method, field etc. public abstract MemberTypes MemberType { get; } + // Metadata token of the member + public int MetadataToken { get; protected set; } + // Name of the member public virtual string Name { get; set; } diff --git a/Il2CppInspector.Common/Reflection/MethodBase.cs b/Il2CppInspector.Common/Reflection/MethodBase.cs index 4614c29..44b9ab7 100644 --- a/Il2CppInspector.Common/Reflection/MethodBase.cs +++ b/Il2CppInspector.Common/Reflection/MethodBase.cs @@ -117,6 +117,7 @@ namespace Il2CppInspector.Reflection // Initialize a method from a method definition (MethodDef) protected MethodBase(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(declaringType) { Definition = pkg.Methods[methodIndex]; + MetadataToken = (int) Definition.token; Index = methodIndex; Name = pkg.Strings[Definition.nameIndex]; diff --git a/Il2CppInspector.Common/Reflection/ParameterInfo.cs b/Il2CppInspector.Common/Reflection/ParameterInfo.cs index 482cc2e..f4eceb4 100644 --- a/Il2CppInspector.Common/Reflection/ParameterInfo.cs +++ b/Il2CppInspector.Common/Reflection/ParameterInfo.cs @@ -41,6 +41,9 @@ namespace Il2CppInspector.Reflection // The method in which the parameter is defined public MethodBase DeclaringMethod { get; } + // Metadata token of the parameter + public int MetadataToken { get; } + // Name of parameter public string Name { get; } public string CSharpName => Constants.Keywords.Contains(Name) ? "@" + Name : Name.ToCIdentifier(); @@ -65,6 +68,7 @@ namespace Il2CppInspector.Reflection } Definition = pkg.Params[Index]; + MetadataToken = (int) Definition.token; Name = pkg.Strings[Definition.nameIndex]; rootDefinition = this; diff --git a/Il2CppInspector.Common/Reflection/PropertyInfo.cs b/Il2CppInspector.Common/Reflection/PropertyInfo.cs index 2c12f58..440a562 100644 --- a/Il2CppInspector.Common/Reflection/PropertyInfo.cs +++ b/Il2CppInspector.Common/Reflection/PropertyInfo.cs @@ -52,6 +52,7 @@ namespace Il2CppInspector.Reflection { base(declaringType) { Index = propIndex; Definition = pkg.Properties[propIndex]; + MetadataToken = (int) Definition.token; Name = pkg.Strings[Definition.nameIndex]; rootDefinition = this; diff --git a/Il2CppInspector.Common/Reflection/TypeInfo.cs b/Il2CppInspector.Common/Reflection/TypeInfo.cs index ea9a564..16e578b 100644 --- a/Il2CppInspector.Common/Reflection/TypeInfo.cs +++ b/Il2CppInspector.Common/Reflection/TypeInfo.cs @@ -739,6 +739,7 @@ namespace Il2CppInspector.Reflection var pkg = Assembly.Model.Package; Definition = pkg.TypeDefinitions[typeIndex]; + MetadataToken = (int) Definition.token; Index = typeIndex; Namespace = Regex.Replace(pkg.Strings[Definition.namespaceIndex], @"[^A-Za-z0-9_\-\.<>{}]", ""); Name = pkg.Strings[Definition.nameIndex]; diff --git a/Il2CppInspector.Common/Reflection/TypeModel.cs b/Il2CppInspector.Common/Reflection/TypeModel.cs index a8d9d75..8114788 100644 --- a/Il2CppInspector.Common/Reflection/TypeModel.cs +++ b/Il2CppInspector.Common/Reflection/TypeModel.cs @@ -293,13 +293,13 @@ namespace Il2CppInspector.Reflection } // The attribute index is an index into AttributeTypeRanges, each of which is a start-end range index into AttributeTypeIndices, each of which is a TypeIndex - public int GetCustomAttributeIndex(Assembly asm, uint token, int customAttributeIndex) { + public int GetCustomAttributeIndex(Assembly asm, int token, int customAttributeIndex) { // Prior to v24.1, Type, Field, Parameter, Method, Event, Property, Assembly definitions had their own customAttributeIndex field if (Package.Version <= 24.0) return customAttributeIndex; // From v24.1 onwards, token was added to Il2CppCustomAttributeTypeRange and each Il2CppImageDefinition noted the CustomAttributeTypeRanges for the image - if (!Package.AttributeIndicesByToken[asm.ImageDefinition.customAttributeStart].TryGetValue(token, out var index)) + if (!Package.AttributeIndicesByToken[asm.ImageDefinition.customAttributeStart].TryGetValue((uint) token, out var index)) return -1; return index; }