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

@@ -186,7 +186,7 @@ namespace Il2CppInspector.Outputs
// Add token attribute // Add token attribute
if (type.Definition != null) 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 // Add custom attribute attributes
foreach (var ca in type.CustomAttributes) foreach (var ca in type.CustomAttributes)
@@ -222,7 +222,7 @@ namespace Il2CppInspector.Outputs
mField.AddAttribute(module, fieldOffsetAttribute, ("Offset", $"0x{field.Offset:X}")); mField.AddAttribute(module, fieldOffsetAttribute, ("Offset", $"0x{field.Offset:X}"));
// Add token attribute // 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 // Add custom attribute attributes
foreach (var ca in field.CustomAttributes) foreach (var ca in field.CustomAttributes)
@@ -250,7 +250,7 @@ namespace Il2CppInspector.Outputs
// Add token attribute // Add token attribute
// Generic properties and constructed properties (from disperate get/set methods) have no definition // Generic properties and constructed properties (from disperate get/set methods) have no definition
if (prop.Definition != null) 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 // Add custom attribute attributes
foreach (var ca in prop.CustomAttributes) foreach (var ca in prop.CustomAttributes)
@@ -270,7 +270,7 @@ namespace Il2CppInspector.Outputs
mEvent.InvokeMethod = AddMethod(module, mType, evt.RaiseMethod); mEvent.InvokeMethod = AddMethod(module, mType, evt.RaiseMethod);
// Add token attribute // 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 // Add custom attribute attributes
foreach (var ca in evt.CustomAttributes) foreach (var ca in evt.CustomAttributes)
@@ -366,7 +366,7 @@ namespace Il2CppInspector.Outputs
} }
// Add token attribute // 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 // Add method pointer attribute
if (method.VirtualAddress.HasValue) { if (method.VirtualAddress.HasValue) {
@@ -489,7 +489,7 @@ namespace Il2CppInspector.Outputs
// Add token attributes // Add token attributes
module.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.ImageDefinition.token:X8}")); 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) // Add custom attribute attributes (must do this after all assemblies are created due to type referencing)

View File

@@ -27,6 +27,9 @@ namespace Il2CppInspector.Reflection {
// Display name of the assembly // Display name of the assembly
public string ShortName { get; } public string ShortName { get; }
// Metadata token of the assembly
public int MetadataToken { get; }
// Entry point method for the assembly // Entry point method for the assembly
public MethodInfo EntryPoint => throw new NotImplementedException(); public MethodInfo EntryPoint => throw new NotImplementedException();
@@ -45,6 +48,7 @@ namespace Il2CppInspector.Reflection {
if (AssemblyDefinition.imageIndex != imageIndex) if (AssemblyDefinition.imageIndex != imageIndex)
throw new InvalidOperationException("Assembly/image index mismatch"); throw new InvalidOperationException("Assembly/image index mismatch");
MetadataToken = (int) AssemblyDefinition.token;
Index = ImageDefinition.assemblyIndex; Index = ImageDefinition.assemblyIndex;
ShortName = Model.Package.Strings[ImageDefinition.nameIndex]; 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(); 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(Assembly asm) => getCustomAttributes(asm, asm.MetadataToken, asm.AssemblyDefinition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(EventInfo evt) => getCustomAttributes(evt.Assembly, evt.Definition.token, evt.Definition.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.Definition.token, field.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.Definition.token, method.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.Definition.token, param.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) public static IList<CustomAttributeData> GetCustomAttributes(PropertyInfo prop)
=> prop.Definition != null ? getCustomAttributes(prop.Assembly, prop.Definition.token, prop.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.Definition.token, type.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) : public EventInfo(Il2CppInspector pkg, int eventIndex, TypeInfo declaringType) :
base(declaringType) { base(declaringType) {
Definition = pkg.Events[eventIndex]; Definition = pkg.Events[eventIndex];
MetadataToken = (int) Definition.token;
Index = eventIndex; Index = eventIndex;
Name = pkg.Strings[Definition.nameIndex]; Name = pkg.Strings[Definition.nameIndex];
rootDefinition = this; rootDefinition = this;

View File

@@ -91,6 +91,7 @@ namespace Il2CppInspector.Reflection {
public FieldInfo(Il2CppInspector pkg, int fieldIndex, TypeInfo declaringType) : public FieldInfo(Il2CppInspector pkg, int fieldIndex, TypeInfo declaringType) :
base(declaringType) { base(declaringType) {
Definition = pkg.Fields[fieldIndex]; Definition = pkg.Fields[fieldIndex];
MetadataToken = (int) Definition.token;
Index = fieldIndex; Index = fieldIndex;
Name = pkg.Strings[Definition.nameIndex]; Name = pkg.Strings[Definition.nameIndex];

View File

@@ -25,6 +25,9 @@ namespace Il2CppInspector.Reflection {
// What sort of member this is, eg. method, field etc. // What sort of member this is, eg. method, field etc.
public abstract MemberTypes MemberType { get; } public abstract MemberTypes MemberType { get; }
// Metadata token of the member
public int MetadataToken { get; protected set; }
// Name of the member // Name of the member
public virtual string Name { get; set; } public virtual string Name { get; set; }

View File

@@ -117,6 +117,7 @@ namespace Il2CppInspector.Reflection
// Initialize a method from a method definition (MethodDef) // Initialize a method from a method definition (MethodDef)
protected MethodBase(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(declaringType) { protected MethodBase(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(declaringType) {
Definition = pkg.Methods[methodIndex]; Definition = pkg.Methods[methodIndex];
MetadataToken = (int) Definition.token;
Index = methodIndex; Index = methodIndex;
Name = pkg.Strings[Definition.nameIndex]; Name = pkg.Strings[Definition.nameIndex];

View File

@@ -41,6 +41,9 @@ namespace Il2CppInspector.Reflection
// The method in which the parameter is defined // The method in which the parameter is defined
public MethodBase DeclaringMethod { get; } public MethodBase DeclaringMethod { get; }
// Metadata token of the parameter
public int MetadataToken { get; }
// Name of parameter // Name of parameter
public string Name { get; } public string Name { get; }
public string CSharpName => Constants.Keywords.Contains(Name) ? "@" + Name : Name.ToCIdentifier(); public string CSharpName => Constants.Keywords.Contains(Name) ? "@" + Name : Name.ToCIdentifier();
@@ -65,6 +68,7 @@ namespace Il2CppInspector.Reflection
} }
Definition = pkg.Params[Index]; Definition = pkg.Params[Index];
MetadataToken = (int) Definition.token;
Name = pkg.Strings[Definition.nameIndex]; Name = pkg.Strings[Definition.nameIndex];
rootDefinition = this; rootDefinition = this;

View File

@@ -52,6 +52,7 @@ namespace Il2CppInspector.Reflection {
base(declaringType) { base(declaringType) {
Index = propIndex; Index = propIndex;
Definition = pkg.Properties[propIndex]; Definition = pkg.Properties[propIndex];
MetadataToken = (int) Definition.token;
Name = pkg.Strings[Definition.nameIndex]; Name = pkg.Strings[Definition.nameIndex];
rootDefinition = this; rootDefinition = this;

View File

@@ -739,6 +739,7 @@ namespace Il2CppInspector.Reflection
var pkg = Assembly.Model.Package; var pkg = Assembly.Model.Package;
Definition = pkg.TypeDefinitions[typeIndex]; Definition = pkg.TypeDefinitions[typeIndex];
MetadataToken = (int) Definition.token;
Index = typeIndex; Index = typeIndex;
Namespace = Regex.Replace(pkg.Strings[Definition.namespaceIndex], @"[^A-Za-z0-9_\-\.<>{}]", ""); Namespace = Regex.Replace(pkg.Strings[Definition.namespaceIndex], @"[^A-Za-z0-9_\-\.<>{}]", "");
Name = pkg.Strings[Definition.nameIndex]; 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 // 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 // Prior to v24.1, Type, Field, Parameter, Method, Event, Property, Assembly definitions had their own customAttributeIndex field
if (Package.Version <= 24.0) if (Package.Version <= 24.0)
return customAttributeIndex; return customAttributeIndex;
// From v24.1 onwards, token was added to Il2CppCustomAttributeTypeRange and each Il2CppImageDefinition noted the CustomAttributeTypeRanges for the image // 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 -1;
return index; return index;
} }