diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index 5436a36..25765ad 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -32,8 +32,10 @@ namespace Il2CppInspector public Il2CppEventDefinition[] Events => Metadata.Events; public Il2CppGenericContainer[] GenericContainers => Metadata.GenericContainers; public Il2CppGenericParameter[] GenericParameters => Metadata.GenericParameters; + public Il2CppCustomAttributeTypeRange[] AttributeTypeRanges => Metadata.AttributeTypeRanges; public int[] InterfaceUsageIndices => Metadata.InterfaceUsageIndices; public int[] NestedTypeIndices => Metadata.NestedTypeIndices; + public int[] AttributeTypeIndices => Metadata.AttributeTypeIndices; public Dictionary FieldDefaultValue { get; } = new Dictionary(); public List FieldOffsets { get; } public List TypeUsages => Binary.Types; diff --git a/Il2CppInspector/Il2CppModel.cs b/Il2CppInspector/Il2CppModel.cs index 25aadc1..5ec1d1f 100644 --- a/Il2CppInspector/Il2CppModel.cs +++ b/Il2CppInspector/Il2CppModel.cs @@ -107,18 +107,17 @@ namespace Il2CppInspector.Reflection return newUsage; } - // Attribute management - private int getCustomAttributeIndex(Assembly asm, uint token, int customAttributeIndex) { + // 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) { + // Prior to v24.1, Type, Field, Parameter, Method, Event, Property, Assembly definitions had their own customAttributeIndex field + if (Package.Version <= 24.0) + return customAttributeIndex; + var image = asm.Definition; + var imageRange = image.customAttributeStart..(int)(image.customAttributeStart + image.customAttributeCount); - throw new NotImplementedException(); + // From v24.1 onwards, token was added to Il2CppCustomAttributeTypeRange and each Il2CppImageDefinition noted the CustomAttributeTypeRanges for the image + return Array.FindIndex(Package.AttributeTypeRanges[imageRange], x => x.token == token) + image.customAttributeStart; } - - public int GetCustomAttributeIndex(Assembly asm) => getCustomAttributeIndex(asm, asm.Definition.token, -1); - public int GetCustomAttributeIndex(EventInfo evt) => getCustomAttributeIndex(evt.Assembly, evt.Definition.token, evt.Definition.customAttributeIndex); - public int GetCustomAttributeIndex(FieldInfo field) => getCustomAttributeIndex(field.Assembly, field.Definition.token, field.Definition.customAttributeIndex); - public int GetCustomAttributeIndex(MethodBase method) => getCustomAttributeIndex(method.Assembly, method.Definition.token, method.Definition.customAttributeIndex); - public int GetCustomAttributeIndex(ParameterInfo param) => getCustomAttributeIndex(param.Member.Assembly, param.Definition.token, param.Definition.customAttributeIndex); - public int GetCustomAttributeIndex(PropertyInfo prop) => getCustomAttributeIndex(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex); } } \ No newline at end of file diff --git a/Il2CppInspector/Metadata.cs b/Il2CppInspector/Metadata.cs index d7af7af..0b43c6a 100644 --- a/Il2CppInspector/Metadata.cs +++ b/Il2CppInspector/Metadata.cs @@ -32,7 +32,7 @@ namespace Il2CppInspector public int[] InterfaceUsageIndices { get; } public int[] NestedTypeIndices { get; } - public int[] AttributeRangeIndices { get; } + public int[] AttributeTypeIndices { get; } public Dictionary Strings { get; } = new Dictionary(); @@ -106,7 +106,7 @@ namespace Il2CppInspector GenericParameters = ReadArray(Header.genericParametersOffset, Header.genericParametersCount / Sizeof(typeof(Il2CppGenericParameter))); if (Version >= 21) { - AttributeRangeIndices = ReadArray(Header.attributeTypesOffset, Header.attributeTypesCount / sizeof(int)); + AttributeTypeIndices = ReadArray(Header.attributeTypesOffset, Header.attributeTypesCount / sizeof(int)); AttributeTypeRanges = ReadArray(Header.attributesInfoOffset, Header.attributesInfoCount / Sizeof(typeof(Il2CppCustomAttributeTypeRange))); } // TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage diff --git a/Il2CppInspector/Reflection/Assembly.cs b/Il2CppInspector/Reflection/Assembly.cs index 3853947..ca43205 100644 --- a/Il2CppInspector/Reflection/Assembly.cs +++ b/Il2CppInspector/Reflection/Assembly.cs @@ -17,7 +17,8 @@ namespace Il2CppInspector.Reflection { public Il2CppCodeGenModule Module { get; } public int Index { get; } - // TODO: CustomAttributes + // Custom attributes for this assembly + public IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); // Name of the assembly public string FullName { get; } diff --git a/Il2CppInspector/Reflection/CustomAttributeData.cs b/Il2CppInspector/Reflection/CustomAttributeData.cs index bc10b04..db626cc 100644 --- a/Il2CppInspector/Reflection/CustomAttributeData.cs +++ b/Il2CppInspector/Reflection/CustomAttributeData.cs @@ -4,12 +4,44 @@ All rights reserved. */ +using System.Collections.Generic; + namespace Il2CppInspector.Reflection { // See: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=netframework-4.8 - public class CustomAttributeData { - // TODO: CustomAttributeData + // The type of the attribute + public TypeInfo AttributeType { get; set; } + + // TODO Constructor, ConstructorArguments, NamedArguments + + public override string ToString() => "[" + AttributeType.FullName + "]"; + + // Get all the custom attributes for a given assembly, type, member or parameter + private static IEnumerable getCustomAttributes(Assembly asm, int customAttributeIndex) { + var pkg = asm.Model.Package; + + // Attribute type ranges weren't included before v21 (customASttributeGenerators was though) + if (pkg.Version < 21) + yield break; + + 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 { AttributeType = asm.Model.GetTypeFromUsage(typeIndex) }; + } + } + + private static IList getCustomAttributes(Assembly asm, uint token, int customAttributeIndex) + => (IList) getCustomAttributes(asm, asm.Model.GetCustomAttributeIndex(asm, token, customAttributeIndex)); + + public static IList GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.Definition.token, -1); + 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.Member.Assembly, param.Definition.token, param.Definition.customAttributeIndex); + public static IList GetCustomAttributes(PropertyInfo prop) => getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex); + public static IList GetCustomAttributes(TypeInfo type) => getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex); } } diff --git a/Il2CppInspector/Reflection/EventInfo.cs b/Il2CppInspector/Reflection/EventInfo.cs index bcdb3a4..bf403e6 100644 --- a/Il2CppInspector/Reflection/EventInfo.cs +++ b/Il2CppInspector/Reflection/EventInfo.cs @@ -4,6 +4,7 @@ All rights reserved. */ +using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -18,6 +19,9 @@ namespace Il2CppInspector.Reflection // Information/flags about the event public EventAttributes Attributes { get; } + // Custom attributes for this member + public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); + // Methods for the event public MethodInfo AddMethod { get; } public MethodInfo RemoveMethod { get; } diff --git a/Il2CppInspector/Reflection/FieldInfo.cs b/Il2CppInspector/Reflection/FieldInfo.cs index 58a2719..83bb438 100644 --- a/Il2CppInspector/Reflection/FieldInfo.cs +++ b/Il2CppInspector/Reflection/FieldInfo.cs @@ -4,7 +4,7 @@ All rights reserved. */ -using System; +using System.Collections.Generic; using System.Reflection; namespace Il2CppInspector.Reflection { @@ -14,7 +14,10 @@ namespace Il2CppInspector.Reflection { public Il2CppFieldDefinition Definition { get; } public int Index { get; } public long Offset { get; } - + + // Custom attributes for this member + public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); + public bool HasDefaultValue { get; } public object DefaultValue { get; } diff --git a/Il2CppInspector/Reflection/MemberInfo.cs b/Il2CppInspector/Reflection/MemberInfo.cs index 3bad2c3..3960783 100644 --- a/Il2CppInspector/Reflection/MemberInfo.cs +++ b/Il2CppInspector/Reflection/MemberInfo.cs @@ -1,10 +1,9 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ -using System; using System.Collections.Generic; using System.Reflection; @@ -15,7 +14,7 @@ namespace Il2CppInspector.Reflection { public Assembly Assembly { get; protected set; } // Custom attributes for this member - public IEnumerable CustomAttributes => throw new NotImplementedException(); + public abstract IEnumerable CustomAttributes { get; } // Type that this type is declared in for nested types protected int declaringTypeDefinitionIndex { private get; set; } = -1; @@ -27,8 +26,6 @@ namespace Il2CppInspector.Reflection { // Name of the member public virtual string Name { get; protected set; } - // TODO: GetCustomAttributes etc. - // For top-level members in an assembly (ie. non-nested types) protected MemberInfo(Assembly asm) => Assembly = asm; diff --git a/Il2CppInspector/Reflection/MethodBase.cs b/Il2CppInspector/Reflection/MethodBase.cs index c68bafb..7c4be4a 100644 --- a/Il2CppInspector/Reflection/MethodBase.cs +++ b/Il2CppInspector/Reflection/MethodBase.cs @@ -25,8 +25,9 @@ namespace Il2CppInspector.Reflection // True if the method contains unresolved generic type parameters public bool ContainsGenericParameters { get; } - // TODO: Custom attribute stuff - + // Custom attributes for this member + public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); + public List GenericTypeParameters { get; } // System.Reflection.MethodInfo.GetGenericArguments() public List DeclaredParameters { get; } = new List(); diff --git a/Il2CppInspector/Reflection/ParameterInfo.cs b/Il2CppInspector/Reflection/ParameterInfo.cs index 2992c72..c6e1c6c 100644 --- a/Il2CppInspector/Reflection/ParameterInfo.cs +++ b/Il2CppInspector/Reflection/ParameterInfo.cs @@ -5,6 +5,7 @@ */ using System; +using System.Collections.Generic; using System.Reflection; namespace Il2CppInspector.Reflection @@ -18,7 +19,8 @@ namespace Il2CppInspector.Reflection // Information/flags about the parameter public ParameterAttributes Attributes { get; } - // TODO: CustomAttributes + // Custom attributes for this parameter + public IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); // True if the parameter has a default value public bool HasDefaultValue => (Attributes & ParameterAttributes.HasDefault) != 0; diff --git a/Il2CppInspector/Reflection/PropertyInfo.cs b/Il2CppInspector/Reflection/PropertyInfo.cs index b34b7e7..ffdb346 100644 --- a/Il2CppInspector/Reflection/PropertyInfo.cs +++ b/Il2CppInspector/Reflection/PropertyInfo.cs @@ -4,6 +4,7 @@ All rights reserved. */ +using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -17,7 +18,8 @@ namespace Il2CppInspector.Reflection { public bool CanRead => GetMethod != null; public bool CanWrite => SetMethod != null; - // TODO: CustomAttributes + // Custom attributes for this member + public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); public MethodInfo GetMethod { get; } public MethodInfo SetMethod { get; } diff --git a/Il2CppInspector/Reflection/TypeInfo.cs b/Il2CppInspector/Reflection/TypeInfo.cs index cfe2780..02dcf09 100644 --- a/Il2CppInspector/Reflection/TypeInfo.cs +++ b/Il2CppInspector/Reflection/TypeInfo.cs @@ -59,6 +59,9 @@ namespace Il2CppInspector.Reflection { + (GenericTypeArguments != null ? "<" + string.Join(", ", GenericTypeArguments.Select(x => x.Name)) + ">" : "") + (IsArray ? "[]" : ""); + // Custom attributes for this member + public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); + public List DeclaredConstructors { get; } = new List(); public List DeclaredEvents { get; } = new List(); public List DeclaredFields { get; } = new List(); @@ -80,6 +83,7 @@ namespace Il2CppInspector.Reflection { public PropertyInfo GetProperty(string name) => DeclaredProperties.First(p => p.Name == name); // Method that the type is declared in if this is a type parameter of a generic method + // TODO: Make a unit test from this: https://docs.microsoft.com/en-us/dotnet/api/system.type.declaringmethod?view=netframework-4.8 public MethodBase DeclaringMethod; // IsGenericTypeParameter and IsGenericMethodParameter from https://github.com/dotnet/corefx/issues/23883 @@ -158,8 +162,6 @@ namespace Il2CppInspector.Reflection { private readonly int arrayRank; public int GetArrayRank() => arrayRank; - // TODO: Custom attribute stuff - public string[] GetEnumNames() => IsEnum? DeclaredFields.Where(x => x.Name != "value__").Select(x => x.Name).ToArray() : throw new InvalidOperationException("Type is not an enumeration"); public TypeInfo GetEnumUnderlyingType() => ElementType;