Model: Initial framework for custom attributes
This commit is contained in:
@@ -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<int, object> FieldDefaultValue { get; } = new Dictionary<int, object>();
|
||||
public List<long> FieldOffsets { get; }
|
||||
public List<Il2CppType> TypeUsages => Binary.Types;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
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);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ namespace Il2CppInspector
|
||||
|
||||
public int[] InterfaceUsageIndices { get; }
|
||||
public int[] NestedTypeIndices { get; }
|
||||
public int[] AttributeRangeIndices { get; }
|
||||
public int[] AttributeTypeIndices { get; }
|
||||
|
||||
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)));
|
||||
|
||||
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)));
|
||||
}
|
||||
// TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace Il2CppInspector.Reflection {
|
||||
public Il2CppCodeGenModule Module { get; }
|
||||
public int Index { get; }
|
||||
|
||||
// TODO: CustomAttributes
|
||||
// Custom attributes for this assembly
|
||||
public IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
// Name of the assembly
|
||||
public string FullName { get; }
|
||||
|
||||
@@ -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<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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
// Methods for the event
|
||||
public MethodInfo AddMethod { get; }
|
||||
public MethodInfo RemoveMethod { get; }
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Il2CppInspector.Reflection {
|
||||
@@ -15,6 +15,9 @@ namespace Il2CppInspector.Reflection {
|
||||
public int Index { get; }
|
||||
public long Offset { get; }
|
||||
|
||||
// Custom attributes for this member
|
||||
public override IEnumerable<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
public bool HasDefaultValue { get; }
|
||||
public object DefaultValue { get; }
|
||||
|
||||
|
||||
@@ -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<CustomAttributeData> CustomAttributes => throw new NotImplementedException();
|
||||
public abstract IEnumerable<CustomAttributeData> 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;
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ 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<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
public List<TypeInfo> GenericTypeParameters { get; } // System.Reflection.MethodInfo.GetGenericArguments()
|
||||
public List<ParameterInfo> DeclaredParameters { get; } = new List<ParameterInfo>();
|
||||
|
||||
@@ -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<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
// True if the parameter has a default value
|
||||
public bool HasDefaultValue => (Attributes & ParameterAttributes.HasDefault) != 0;
|
||||
|
||||
@@ -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<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
public MethodInfo GetMethod { get; }
|
||||
public MethodInfo SetMethod { get; }
|
||||
|
||||
@@ -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<CustomAttributeData> CustomAttributes => CustomAttributeData.GetCustomAttributes(this);
|
||||
|
||||
public List<ConstructorInfo> DeclaredConstructors { get; } = new List<ConstructorInfo>();
|
||||
public List<EventInfo> DeclaredEvents { get; } = new List<EventInfo>();
|
||||
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);
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user