Skip to content

Commit

Permalink
Fix generation inconsistencies with previous version
Browse files Browse the repository at this point in the history
  • Loading branch information
limoka committed Jan 23, 2025
1 parent 8558a20 commit 6663045
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
14 changes: 14 additions & 0 deletions Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,25 @@ public IMethodDefOrRef RewriteMethodRef(IMethodDefOrRef methodRef)
return NewAssembly.ManifestModule!.DefaultImporter.ImportMethod(newMethod);
}

/// <summary>
/// Rewrite passes type
/// </summary>
/// <param name="typeRef">type</param>
/// <param name="context">generic context. Ensure you pass NEW types here</param>
/// <param name="typeIsBoxed">are we rewriting boxed variant</param>
/// <returns>new type</returns>
public ITypeDefOrRef RewriteTypeRef(ITypeDescriptor typeRef, GenericParameterContext context = default, bool typeIsBoxed = false)
{
return RewriteTypeRef(typeRef?.ToTypeSignature(), context, typeIsBoxed).ToTypeDefOrRef();
}

/// <summary>
/// Rewrite passes type
/// </summary>
/// <param name="typeRef">type</param>
/// <param name="context">generic context. Ensure you pass NEW types here</param>
/// <param name="typeIsBoxed">are we rewriting boxed variant</param>
/// <returns>new type</returns>
public TypeSignature RewriteTypeRef(TypeSignature? typeRef, GenericParameterContext context = default, bool typeIsBoxed = false)
{
if (typeRef == null)
Expand Down
25 changes: 15 additions & 10 deletions Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -227,7 +232,7 @@ private MethodAttributes AdjustAttributes(MethodAttributes original, bool stripV
return original;
}

private string UnmangleMethodName()
private string UnmangleMethodName(GenericParameterContext context)
{
var method = OriginalMethod;

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

Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Il2CppInterop.Generator/Passes/Pass22GenerateEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -77,12 +77,12 @@ static bool IsDefaultMemberAttributeReal(CustomAttribute attribute)
}

private static string UnmanglePropertyName(AssemblyRewriteContext assemblyContext, PropertyDefinition prop,
ITypeDefOrRef declaringType, Dictionary<string, int> countsByBaseName)
TypeDefinition declaringType, Dictionary<string, int> 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;
Expand Down

0 comments on commit 6663045

Please sign in to comment.