Model: Initial framework for custom attributes

This commit is contained in:
Katy Coe
2019-11-03 22:25:38 +01:00
parent 7f398f40cb
commit 7351e339f0
12 changed files with 73 additions and 28 deletions

View File

@@ -32,8 +32,10 @@ namespace Il2CppInspector
public Il2CppEventDefinition[] Events => Metadata.Events; public Il2CppEventDefinition[] Events => Metadata.Events;
public Il2CppGenericContainer[] GenericContainers => Metadata.GenericContainers; public Il2CppGenericContainer[] GenericContainers => Metadata.GenericContainers;
public Il2CppGenericParameter[] GenericParameters => Metadata.GenericParameters; public Il2CppGenericParameter[] GenericParameters => Metadata.GenericParameters;
public Il2CppCustomAttributeTypeRange[] AttributeTypeRanges => Metadata.AttributeTypeRanges;
public int[] InterfaceUsageIndices => Metadata.InterfaceUsageIndices; public int[] InterfaceUsageIndices => Metadata.InterfaceUsageIndices;
public int[] NestedTypeIndices => Metadata.NestedTypeIndices; public int[] NestedTypeIndices => Metadata.NestedTypeIndices;
public int[] AttributeTypeIndices => Metadata.AttributeTypeIndices;
public Dictionary<int, object> FieldDefaultValue { get; } = new Dictionary<int, object>(); public Dictionary<int, object> FieldDefaultValue { get; } = new Dictionary<int, object>();
public List<long> FieldOffsets { get; } public List<long> FieldOffsets { get; }
public List<Il2CppType> TypeUsages => Binary.Types; public List<Il2CppType> TypeUsages => Binary.Types;

View File

@@ -107,18 +107,17 @@ namespace Il2CppInspector.Reflection
return newUsage; return newUsage;
} }
// Attribute management // 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
private int getCustomAttributeIndex(Assembly asm, uint token, int customAttributeIndex) { 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 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);
} }
} }

View File

@@ -32,7 +32,7 @@ namespace Il2CppInspector
public int[] InterfaceUsageIndices { get; } public int[] InterfaceUsageIndices { get; }
public int[] NestedTypeIndices { get; } public int[] NestedTypeIndices { get; }
public int[] AttributeRangeIndices { get; } public int[] AttributeTypeIndices { get; }
public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>(); public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>();
@@ -106,7 +106,7 @@ namespace Il2CppInspector
GenericParameters = ReadArray<Il2CppGenericParameter>(Header.genericParametersOffset, Header.genericParametersCount / Sizeof(typeof(Il2CppGenericParameter))); GenericParameters = ReadArray<Il2CppGenericParameter>(Header.genericParametersOffset, Header.genericParametersCount / Sizeof(typeof(Il2CppGenericParameter)));
if (Version >= 21) { if (Version >= 21) {
AttributeRangeIndices = ReadArray<int>(Header.attributeTypesOffset, Header.attributeTypesCount / sizeof(int)); AttributeTypeIndices = ReadArray<int>(Header.attributeTypesOffset, Header.attributeTypesCount / sizeof(int));
AttributeTypeRanges = ReadArray<Il2CppCustomAttributeTypeRange>(Header.attributesInfoOffset, Header.attributesInfoCount / Sizeof(typeof(Il2CppCustomAttributeTypeRange))); AttributeTypeRanges = ReadArray<Il2CppCustomAttributeTypeRange>(Header.attributesInfoOffset, Header.attributesInfoCount / Sizeof(typeof(Il2CppCustomAttributeTypeRange)));
} }
// TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage // TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage

View File

@@ -17,7 +17,8 @@ namespace Il2CppInspector.Reflection {
public Il2CppCodeGenModule Module { get; } public Il2CppCodeGenModule Module { get; }
public int Index { get; } public int Index { get; }
// TODO: CustomAttributes // Custom attributes for this assembly
public IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
// Name of the assembly // Name of the assembly
public string FullName { get; } public string FullName { get; }

View File

@@ -4,12 +4,44 @@
All rights reserved. All rights reserved.
*/ */
using System.Collections.Generic;
namespace Il2CppInspector.Reflection namespace Il2CppInspector.Reflection
{ {
// See: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=netframework-4.8 // See: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=netframework-4.8
public class CustomAttributeData 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<CustomAttributeData> 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<CustomAttributeData> getCustomAttributes(Assembly asm, uint token, int customAttributeIndex)
=> (IList<CustomAttributeData>) getCustomAttributes(asm, asm.Model.GetCustomAttributeIndex(asm, token, customAttributeIndex));
public static IList<CustomAttributeData> GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.Definition.token, -1);
public static IList<CustomAttributeData> GetCustomAttributes(EventInfo evt) => getCustomAttributes(evt.Assembly, evt.Definition.token, evt.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(FieldInfo field) => getCustomAttributes(field.Assembly, field.Definition.token, field.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(MethodBase method) => getCustomAttributes(method.Assembly, method.Definition.token, method.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo param) => getCustomAttributes(param.Member.Assembly, param.Definition.token, param.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(PropertyInfo prop) => getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(TypeInfo type) => getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex);
} }
} }

View File

@@ -4,6 +4,7 @@
All rights reserved. All rights reserved.
*/ */
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@@ -18,6 +19,9 @@ namespace Il2CppInspector.Reflection
// Information/flags about the event // Information/flags about the event
public EventAttributes Attributes { get; } public EventAttributes Attributes { get; }
// Custom attributes for this member
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
// Methods for the event // Methods for the event
public MethodInfo AddMethod { get; } public MethodInfo AddMethod { get; }
public MethodInfo RemoveMethod { get; } public MethodInfo RemoveMethod { get; }

View File

@@ -4,7 +4,7 @@
All rights reserved. All rights reserved.
*/ */
using System; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
namespace Il2CppInspector.Reflection { namespace Il2CppInspector.Reflection {
@@ -14,7 +14,10 @@ namespace Il2CppInspector.Reflection {
public Il2CppFieldDefinition Definition { get; } public Il2CppFieldDefinition Definition { get; }
public int Index { get; } public int Index { get; }
public long Offset { get; } public long Offset { get; }
// Custom attributes for this member
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
public bool HasDefaultValue { get; } public bool HasDefaultValue { get; }
public object DefaultValue { get; } public object DefaultValue { get; }

View File

@@ -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. All rights reserved.
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
@@ -15,7 +14,7 @@ namespace Il2CppInspector.Reflection {
public Assembly Assembly { get; protected set; } public Assembly Assembly { get; protected set; }
// Custom attributes for this member // Custom attributes for this member
public IEnumerable<CustomAttributeData> CustomAttributes => throw new NotImplementedException(); public abstract IEnumerable<CustomAttributeData> CustomAttributes { get; }
// Type that this type is declared in for nested types // Type that this type is declared in for nested types
protected int declaringTypeDefinitionIndex { private get; set; } = -1; protected int declaringTypeDefinitionIndex { private get; set; } = -1;
@@ -27,8 +26,6 @@ namespace Il2CppInspector.Reflection {
// Name of the member // Name of the member
public virtual string Name { get; protected set; } public virtual string Name { get; protected set; }
// TODO: GetCustomAttributes etc.
// For top-level members in an assembly (ie. non-nested types) // For top-level members in an assembly (ie. non-nested types)
protected MemberInfo(Assembly asm) => Assembly = asm; protected MemberInfo(Assembly asm) => Assembly = asm;

View File

@@ -25,8 +25,9 @@ namespace Il2CppInspector.Reflection
// True if the method contains unresolved generic type parameters // True if the method contains unresolved generic type parameters
public bool ContainsGenericParameters { get; } public bool ContainsGenericParameters { get; }
// TODO: Custom attribute stuff // Custom attributes for this member
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
public List<TypeInfo> GenericTypeParameters { get; } // System.Reflection.MethodInfo.GetGenericArguments() public List<TypeInfo> GenericTypeParameters { get; } // System.Reflection.MethodInfo.GetGenericArguments()
public List<ParameterInfo> DeclaredParameters { get; } = new List<ParameterInfo>(); public List<ParameterInfo> DeclaredParameters { get; } = new List<ParameterInfo>();

View File

@@ -5,6 +5,7 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
namespace Il2CppInspector.Reflection namespace Il2CppInspector.Reflection
@@ -18,7 +19,8 @@ namespace Il2CppInspector.Reflection
// Information/flags about the parameter // Information/flags about the parameter
public ParameterAttributes Attributes { get; } public ParameterAttributes Attributes { get; }
// TODO: CustomAttributes // Custom attributes for this parameter
public IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
// True if the parameter has a default value // True if the parameter has a default value
public bool HasDefaultValue => (Attributes & ParameterAttributes.HasDefault) != 0; public bool HasDefaultValue => (Attributes & ParameterAttributes.HasDefault) != 0;

View File

@@ -4,6 +4,7 @@
All rights reserved. All rights reserved.
*/ */
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@@ -17,7 +18,8 @@ namespace Il2CppInspector.Reflection {
public bool CanRead => GetMethod != null; public bool CanRead => GetMethod != null;
public bool CanWrite => SetMethod != null; public bool CanWrite => SetMethod != null;
// TODO: CustomAttributes // Custom attributes for this member
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
public MethodInfo GetMethod { get; } public MethodInfo GetMethod { get; }
public MethodInfo SetMethod { get; } public MethodInfo SetMethod { get; }

View File

@@ -59,6 +59,9 @@ namespace Il2CppInspector.Reflection {
+ (GenericTypeArguments != null ? "<" + string.Join(", ", GenericTypeArguments.Select(x => x.Name)) + ">" : "") + (GenericTypeArguments != null ? "<" + string.Join(", ", GenericTypeArguments.Select(x => x.Name)) + ">" : "")
+ (IsArray ? "[]" : ""); + (IsArray ? "[]" : "");
// Custom attributes for this member
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
public List<ConstructorInfo> DeclaredConstructors { get; } = new List<ConstructorInfo>(); public List<ConstructorInfo> DeclaredConstructors { get; } = new List<ConstructorInfo>();
public List<EventInfo> DeclaredEvents { get; } = new List<EventInfo>(); public List<EventInfo> DeclaredEvents { get; } = new List<EventInfo>();
public List<FieldInfo> DeclaredFields { get; } = new List<FieldInfo>(); public List<FieldInfo> DeclaredFields { get; } = new List<FieldInfo>();
@@ -80,6 +83,7 @@ namespace Il2CppInspector.Reflection {
public PropertyInfo GetProperty(string name) => DeclaredProperties.First(p => p.Name == name); 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 // 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; public MethodBase DeclaringMethod;
// IsGenericTypeParameter and IsGenericMethodParameter from https://github.com/dotnet/corefx/issues/23883 // IsGenericTypeParameter and IsGenericMethodParameter from https://github.com/dotnet/corefx/issues/23883
@@ -158,8 +162,6 @@ namespace Il2CppInspector.Reflection {
private readonly int arrayRank; private readonly int arrayRank;
public int GetArrayRank() => 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 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; public TypeInfo GetEnumUnderlyingType() => ElementType;