C++: Re-factor custom attribute generator signature handling

This commit is contained in:
Katy Coe
2020-07-18 03:33:33 +02:00
parent 2b1aff5bd1
commit 307cf29dcd
3 changed files with 14 additions and 23 deletions

View File

@@ -53,9 +53,6 @@ namespace Il2CppInspector.Model
// For il2cpp < 19, the key is the string literal ordinal instead of the address
public Dictionary<ulong, string> Strings = new Dictionary<ulong, string>();
// All of the custom attribute generator functions in the library
public Dictionary<CustomAttributeData, AppMethod> CustomAttributeGenerators = new Dictionary<CustomAttributeData, AppMethod>();
public bool StringIndexesAreOrdinals => Package.MetadataUsages == null;
// The .NET type model for the application
@@ -222,17 +219,6 @@ namespace Il2CppInspector.Model
}
}
// Add custom attribute generators to the model
foreach (var cppMethod in ILModel.AttributesByIndices.Values.Where(m => m.VirtualAddress.HasValue)) {
var cppMethodName = declarationGenerator.TypeNamer.GetName(cppMethod.AttributeType) + "_CustomAttributesCacheGenerator";
var fnPtrType = CppFnPtrType.FromSignature(CppTypeCollection, $"void (*{cppMethodName})(CustomAttributesCache *)");
fnPtrType.Name = cppMethodName;
fnPtrType.Group = "custom_attribute_generators";
var appMethod = new AppMethod(null, fnPtrType, cppMethod.VirtualAddress.Value.Start) {Group = fnPtrType.Group};
CustomAttributeGenerators.Add(cppMethod, appMethod);
}
// This is to allow this method to be chained after a new expression
return this;
}

View File

@@ -104,11 +104,13 @@ typedef __int64 int64_t;
writeMethods(model.GetMethodGroup("types_from_generic_methods"));
writeSectionHeader("Custom attributes generators");
writeMethods(model.CustomAttributeGenerators.Values, asRefs: true);
foreach (var method in model.ILModel.AttributesByIndices.Values) {
writeTypedName(method.VirtualAddress.Value.Start, method.Signature, method.Name);
}
writeSectionHeader("Method.Invoke thunks");
foreach (var method in model.ILModel.MethodInvokers.Where(m => m != null)) {
writeTypedName(method.VirtualAddress.Start, method.ToString(), method.Name);
writeTypedName(method.VirtualAddress.Start, method.Signature, method.Name);
}
}
@@ -117,14 +119,10 @@ typedef __int64 int64_t;
writeDecls(type.ToString());
}
private void writeMethods(IEnumerable<AppMethod> methods, bool asRefs = false) {
private void writeMethods(IEnumerable<AppMethod> methods) {
foreach (var method in methods) {
var address = asRefs? method.MethodInfoPtrAddress : method.MethodCodeAddress;
writeTypedName(address, method.CppFnPtrType.ToSignatureString(), method.CppFnPtrType.Name);
// C++-only methods won't have an IL equivalent, eg. invokers and custom attribute generators
if (method.Method != null)
writeComment(address, method.Method);
writeTypedName(method.MethodCodeAddress, method.CppFnPtrType.ToSignatureString(), method.CppFnPtrType.Name);
writeComment(method.MethodCodeAddress, method.Method);
}
}

View File

@@ -25,6 +25,13 @@ namespace Il2CppInspector.Reflection
// The last one will be wrong but there is no way to calculate it
(Model.Package.CustomAttributeGenerators[Index], Model.Package.FunctionAddresses[Model.Package.CustomAttributeGenerators[Index]]);
// C++ method names
// TODO: Known issue here where we should be using CppDeclarationGenerator.TypeNamer to ensure uniqueness
public string Name => $"{AttributeType.Name.ToCIdentifier()}_CustomAttributesCacheGenerator";
// C++ method signature
public string Signature => $"void {Name}(CustomAttributesCache *)";
public override string ToString() => "[" + AttributeType.FullName + "]";
// Get all the custom attributes for a given assembly, type, member or parameter