Model: Implement MetadataToken property
This commit is contained in:
@@ -186,7 +186,7 @@ namespace Il2CppInspector.Outputs
|
||||
|
||||
// Add token attribute
|
||||
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
|
||||
foreach (var ca in type.CustomAttributes)
|
||||
@@ -222,7 +222,7 @@ namespace Il2CppInspector.Outputs
|
||||
mField.AddAttribute(module, fieldOffsetAttribute, ("Offset", $"0x{field.Offset:X}"));
|
||||
|
||||
// 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
|
||||
foreach (var ca in field.CustomAttributes)
|
||||
@@ -250,7 +250,7 @@ namespace Il2CppInspector.Outputs
|
||||
// Add token attribute
|
||||
// Generic properties and constructed properties (from disperate get/set methods) have no definition
|
||||
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
|
||||
foreach (var ca in prop.CustomAttributes)
|
||||
@@ -270,7 +270,7 @@ namespace Il2CppInspector.Outputs
|
||||
mEvent.InvokeMethod = AddMethod(module, mType, evt.RaiseMethod);
|
||||
|
||||
// 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
|
||||
foreach (var ca in evt.CustomAttributes)
|
||||
@@ -366,7 +366,7 @@ namespace Il2CppInspector.Outputs
|
||||
}
|
||||
|
||||
// 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
|
||||
if (method.VirtualAddress.HasValue) {
|
||||
@@ -489,7 +489,7 @@ namespace Il2CppInspector.Outputs
|
||||
|
||||
// Add token attributes
|
||||
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)
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user