Model: Implement MetadataToken property

This commit is contained in:
Katy Coe
2021-01-10 08:37:49 +01:00
parent 8ffff24079
commit cd7ddf193a
11 changed files with 32 additions and 16 deletions

View File

@@ -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];

View File

@@ -64,16 +64,16 @@ namespace Il2CppInspector.Reflection
}
}
private static IList<CustomAttributeData> getCustomAttributes(Assembly asm, uint token, int customAttributeIndex) =>
private static IList<CustomAttributeData> getCustomAttributes(Assembly asm, int token, int customAttributeIndex) =>
getCustomAttributes(asm, asm.Model.GetCustomAttributeIndex(asm, token, customAttributeIndex)).ToList();
public static IList<CustomAttributeData> GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.AssemblyDefinition.token, asm.AssemblyDefinition.customAttributeIndex);
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.DeclaringMethod.Assembly, param.Definition.token, param.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(Assembly asm) => getCustomAttributes(asm, asm.MetadataToken, asm.AssemblyDefinition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(EventInfo evt) => getCustomAttributes(evt.Assembly, evt.MetadataToken, evt.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(FieldInfo field) => getCustomAttributes(field.Assembly, field.MetadataToken, field.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(MethodBase method) => getCustomAttributes(method.Assembly, method.MetadataToken, method.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo param) => getCustomAttributes(param.DeclaringMethod.Assembly, param.MetadataToken, param.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(PropertyInfo prop)
=> prop.Definition != null ? getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex) : new List<CustomAttributeData>();
public static IList<CustomAttributeData> GetCustomAttributes(TypeInfo type) => type.Definition != null? getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex) : new List<CustomAttributeData>();
=> prop.Definition != null ? getCustomAttributes(prop.Assembly, prop.MetadataToken, prop.Definition.customAttributeIndex) : new List<CustomAttributeData>();
public static IList<CustomAttributeData> GetCustomAttributes(TypeInfo type) => type.Definition != null? getCustomAttributes(type.Assembly, type.MetadataToken, type.Definition.customAttributeIndex) : new List<CustomAttributeData>();
}
}

View File

@@ -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;

View File

@@ -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];

View File

@@ -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; }

View File

@@ -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];

View File

@@ -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;

View File

@@ -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;

View File

@@ -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];

View File

@@ -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;
}