From 666304517f3b8659bc92d17d9735ed01d3705e79 Mon Sep 17 00:00:00 2001 From: limoka8 Date: Thu, 23 Jan 2025 15:34:42 +0300 Subject: [PATCH] Fix generation inconsistencies with previous version --- .../Contexts/AssemblyRewriteContext.cs | 14 +++++++++++ .../Contexts/MethodRewriteContext.cs | 25 +++++++++++-------- .../Passes/Pass19CopyMethodParameters.cs | 2 +- .../Passes/Pass21GenerateValueTypeFields.cs | 2 +- .../Passes/Pass22GenerateEnums.cs | 2 +- .../Passes/Pass40GenerateFieldAccessors.cs | 2 +- .../Passes/Pass70GenerateProperties.cs | 8 +++--- 7 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs b/Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs index 014d5323..5247929d 100644 --- a/Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs +++ b/Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs @@ -72,11 +72,25 @@ public IMethodDefOrRef RewriteMethodRef(IMethodDefOrRef methodRef) return NewAssembly.ManifestModule!.DefaultImporter.ImportMethod(newMethod); } + /// + /// Rewrite passes type + /// + /// type + /// generic context. Ensure you pass NEW types here + /// are we rewriting boxed variant + /// new type public ITypeDefOrRef RewriteTypeRef(ITypeDescriptor typeRef, GenericParameterContext context = default, bool typeIsBoxed = false) { return RewriteTypeRef(typeRef?.ToTypeSignature(), context, typeIsBoxed).ToTypeDefOrRef(); } + /// + /// Rewrite passes type + /// + /// type + /// generic context. Ensure you pass NEW types here + /// are we rewriting boxed variant + /// new type public TypeSignature RewriteTypeRef(TypeSignature? typeRef, GenericParameterContext context = default, bool typeIsBoxed = false) { if (typeRef == null) diff --git a/Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs b/Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs index ed21435a..4cad3d45 100644 --- a/Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs +++ b/Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs @@ -6,10 +6,12 @@ using AsmResolver.DotNet.Collections; using AsmResolver.DotNet.Signatures; using AsmResolver.PE.DotNet.Metadata.Tables; +using Il2CppInterop.Common; using Il2CppInterop.Common.XrefScans; using Il2CppInterop.Generator.Extensions; using Il2CppInterop.Generator.Passes; using Il2CppInterop.Generator.Utils; +using Microsoft.Extensions.Logging; namespace Il2CppInterop.Generator.Contexts; @@ -146,11 +148,14 @@ private bool HasGenericParameter(TypeSignature typeReference, GenericParameterSi public void CtorPhase2() { - UnmangledName = UnmangleMethodName(); - UnmangledNameWithSignature = UnmangleMethodNameWithSignature(); + // Make context manually, NewMethod.DeclaringType is null for now + var genericContext = new GenericParameterContext(DeclaringType.NewType, NewMethod); + + UnmangledName = UnmangleMethodName(genericContext); + UnmangledNameWithSignature = UnmangleMethodNameWithSignature(genericContext); NewMethod.Name = UnmangledName; - NewMethod.Signature!.ReturnType = DeclaringType.AssemblyContext.RewriteTypeRef(OriginalMethod.Signature?.ReturnType, OriginalMethod.GetGenericParameterContext(), DeclaringType.isBoxedTypeVariant); + NewMethod.Signature!.ReturnType = DeclaringType.AssemblyContext.RewriteTypeRef(OriginalMethod.Signature?.ReturnType, genericContext, DeclaringType.isBoxedTypeVariant); var nonGenericMethodInfoPointerField = new FieldDefinition( "NativeMethodInfoPtr_" + UnmangledNameWithSignature, @@ -227,7 +232,7 @@ private MethodAttributes AdjustAttributes(MethodAttributes original, bool stripV return original; } - private string UnmangleMethodName() + private string UnmangleMethodName(GenericParameterContext context) { var method = OriginalMethod; @@ -241,12 +246,12 @@ private string UnmangleMethodName() return ".ctor"; if (method.Name.IsObfuscated(DeclaringType.AssemblyContext.GlobalContext.Options)) - return UnmangleMethodNameWithSignature(); + return UnmangleMethodNameWithSignature(context); return method.Name.MakeValidInSource(); } - private string ProduceMethodSignatureBase() + private string ProduceMethodSignatureBase(GenericParameterContext context) { var method = OriginalMethod; @@ -274,12 +279,12 @@ private string ProduceMethodSignatureBase() builder.Append(str); builder.Append('_'); - builder.Append(DeclaringType.AssemblyContext.RewriteTypeRef(method.Signature?.ReturnType, method.GetGenericParameterContext(), DeclaringType.isBoxedTypeVariant).GetUnmangledName(method.DeclaringType, method)); + builder.Append(DeclaringType.AssemblyContext.RewriteTypeRef(method.Signature?.ReturnType, context, DeclaringType.isBoxedTypeVariant).GetUnmangledName(method.DeclaringType, method)); foreach (var param in method.Parameters) { builder.Append('_'); - builder.Append(DeclaringType.AssemblyContext.RewriteTypeRef(param.ParameterType, method.GetGenericParameterContext(), DeclaringType.isBoxedTypeVariant).GetUnmangledName(method.DeclaringType, method)); + builder.Append(DeclaringType.AssemblyContext.RewriteTypeRef(param.ParameterType, context, DeclaringType.isBoxedTypeVariant).GetUnmangledName(method.DeclaringType, method)); } var address = Rva; @@ -290,9 +295,9 @@ private string ProduceMethodSignatureBase() } - private string UnmangleMethodNameWithSignature() + private string UnmangleMethodNameWithSignature(GenericParameterContext context) { - var unmangleMethodNameWithSignature = ProduceMethodSignatureBase() + "_" + DeclaringType.Methods + var unmangleMethodNameWithSignature = ProduceMethodSignatureBase(context) + "_" + DeclaringType.Methods .Where(ParameterSignatureMatchesThis).TakeWhile(it => it != this).Count(); if (DeclaringType.AssemblyContext.GlobalContext.Options.RenameMap.TryGetValue( diff --git a/Il2CppInterop.Generator/Passes/Pass19CopyMethodParameters.cs b/Il2CppInterop.Generator/Passes/Pass19CopyMethodParameters.cs index 182288c8..0d6b9719 100644 --- a/Il2CppInterop.Generator/Passes/Pass19CopyMethodParameters.cs +++ b/Il2CppInterop.Generator/Passes/Pass19CopyMethodParameters.cs @@ -24,7 +24,7 @@ public static void DoPass(RewriteGlobalContext context) : originalMethodParameter.Name; var newParameter = newMethod.AddParameter( - assemblyContext.RewriteTypeRef(originalMethodParameter.ParameterType, originalMethod.GetGenericParameterContext(), typeContext.isBoxedTypeVariant), + assemblyContext.RewriteTypeRef(originalMethodParameter.ParameterType, newMethod.GetGenericParameterContext(), typeContext.isBoxedTypeVariant), newName, originalMethodParameter.GetOrCreateDefinition().Attributes & ~ParameterAttributes.HasFieldMarshal); diff --git a/Il2CppInterop.Generator/Passes/Pass21GenerateValueTypeFields.cs b/Il2CppInterop.Generator/Passes/Pass21GenerateValueTypeFields.cs index 1eb75618..a4575f17 100644 --- a/Il2CppInterop.Generator/Passes/Pass21GenerateValueTypeFields.cs +++ b/Il2CppInterop.Generator/Passes/Pass21GenerateValueTypeFields.cs @@ -58,7 +58,7 @@ public static void DoPass(RewriteGlobalContext context) if (!field.Signature!.FieldType.IsValueType && field.Signature.FieldType is not PointerTypeSignature and not GenericParameterSignature) rewriteTypeRef = assemblyContext.Imports.Module.IntPtr(); else - rewriteTypeRef = assemblyContext.RewriteTypeRef(field.Signature.FieldType, field.DeclaringType!.GetGenericParameterContext()); + rewriteTypeRef = assemblyContext.RewriteTypeRef(field.Signature.FieldType, newType.GetGenericParameterContext()); var newField = new FieldDefinition(fieldContext.UnmangledName, field.Attributes.ForcePublic(), rewriteTypeRef); diff --git a/Il2CppInterop.Generator/Passes/Pass22GenerateEnums.cs b/Il2CppInterop.Generator/Passes/Pass22GenerateEnums.cs index edb5f012..462ae201 100644 --- a/Il2CppInterop.Generator/Passes/Pass22GenerateEnums.cs +++ b/Il2CppInterop.Generator/Passes/Pass22GenerateEnums.cs @@ -44,7 +44,7 @@ public static void DoPass(RewriteGlobalContext context) fieldName = newName; var newDef = new FieldDefinition(fieldName, fieldDefinition.Attributes | FieldAttributes.HasDefault, - assemblyContext.RewriteTypeRef(fieldDefinition.Signature!.FieldType, type.GetGenericParameterContext())); + assemblyContext.RewriteTypeRef(fieldDefinition.Signature!.FieldType, newType.GetGenericParameterContext())); newType.Fields.Add(newDef); newDef.Constant = fieldDefinition.Constant; diff --git a/Il2CppInterop.Generator/Passes/Pass40GenerateFieldAccessors.cs b/Il2CppInterop.Generator/Passes/Pass40GenerateFieldAccessors.cs index 283ea8f1..0f5f2b66 100644 --- a/Il2CppInterop.Generator/Passes/Pass40GenerateFieldAccessors.cs +++ b/Il2CppInterop.Generator/Passes/Pass40GenerateFieldAccessors.cs @@ -23,7 +23,7 @@ public static void DoPass(RewriteGlobalContext context) var field = fieldContext.OriginalField; var unmangleFieldName = fieldContext.UnmangledName; - var propertyType = assemblyContext.RewriteTypeRef(field.Signature!.FieldType, field.DeclaringType!.GetGenericParameterContext(), typeContext.isBoxedTypeVariant); + var propertyType = assemblyContext.RewriteTypeRef(field.Signature!.FieldType, typeContext.NewType!.GetGenericParameterContext(), typeContext.isBoxedTypeVariant); var signature = field.IsStatic ? PropertySignature.CreateStatic(propertyType) : PropertySignature.CreateInstance(propertyType); diff --git a/Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs b/Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs index eff919b2..0dc9d357 100644 --- a/Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs +++ b/Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs @@ -25,12 +25,12 @@ public static void DoPass(RewriteGlobalContext context) var unmangledPropertyName = UnmanglePropertyName(assemblyContext, oldProperty, typeContext.NewType, propertyCountsByName); - var propertyType = assemblyContext.RewriteTypeRef(oldProperty.Signature!.ReturnType, type.GetGenericParameterContext(), typeContext.isBoxedTypeVariant); + var propertyType = assemblyContext.RewriteTypeRef(oldProperty.Signature!.ReturnType, typeContext.NewType.GetGenericParameterContext(), typeContext.isBoxedTypeVariant); var signature = oldProperty.Signature.HasThis ? PropertySignature.CreateInstance(propertyType) : PropertySignature.CreateStatic(propertyType); foreach (var oldParameter in oldProperty.Signature.ParameterTypes) - signature.ParameterTypes.Add(assemblyContext.RewriteTypeRef(oldParameter, type.GetGenericParameterContext(), typeContext.isBoxedTypeVariant)); + signature.ParameterTypes.Add(assemblyContext.RewriteTypeRef(oldParameter, typeContext.NewType.GetGenericParameterContext(), typeContext.isBoxedTypeVariant)); var property = new PropertyDefinition(unmangledPropertyName, oldProperty.Attributes, signature); @@ -77,12 +77,12 @@ static bool IsDefaultMemberAttributeReal(CustomAttribute attribute) } private static string UnmanglePropertyName(AssemblyRewriteContext assemblyContext, PropertyDefinition prop, - ITypeDefOrRef declaringType, Dictionary countsByBaseName) + TypeDefinition declaringType, Dictionary countsByBaseName) { if (assemblyContext.GlobalContext.Options.PassthroughNames || !prop.Name.IsObfuscated(assemblyContext.GlobalContext.Options)) return prop.Name!; - var baseName = "prop_" + assemblyContext.RewriteTypeRef(prop.Signature!.ReturnType, prop.DeclaringType!.GetGenericParameterContext()).GetUnmangledName(prop.DeclaringType); + var baseName = "prop_" + assemblyContext.RewriteTypeRef(prop.Signature!.ReturnType, declaringType.GetGenericParameterContext()).GetUnmangledName(prop.DeclaringType); countsByBaseName.TryGetValue(baseName, out var index); countsByBaseName[baseName] = index + 1;