Skip to content

Commit 11e28d3

Browse files
committed
Bump to latest Cpp2IL and AsmResolver
1 parent 9163f2a commit 11e28d3

10 files changed

Lines changed: 240 additions & 127 deletions

Il2CppInterop.Generator/ContextResolver.cs

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,31 @@ public readonly struct ContextResolver
1212
private readonly AssemblyAnalysisContext referencedFrom;
1313
private readonly TypeAnalysisContext? referencingType;
1414
private readonly MethodAnalysisContext? referencingMethod;
15+
private readonly RuntimeContext runtimeContext;
1516

16-
public ContextResolver(AssemblyAnalysisContext referencedFrom)
17+
public ContextResolver(AssemblyAnalysisContext referencedFrom, RuntimeContext runtimeContext)
1718
{
1819
this.referencedFrom = referencedFrom;
20+
this.runtimeContext = runtimeContext;
1921
}
2022

21-
public ContextResolver(TypeAnalysisContext referencingType)
23+
public ContextResolver(TypeAnalysisContext referencingType, RuntimeContext runtimeContext)
2224
{
2325
if (referencingType is ReferencedTypeAnalysisContext)
2426
throw new ArgumentException("Must be a simple type", nameof(referencingType));
2527
referencedFrom = referencingType.DeclaringAssembly;
2628
this.referencingType = referencingType;
29+
this.runtimeContext = runtimeContext;
2730
}
2831

29-
public ContextResolver(MethodAnalysisContext referencingMethod)
32+
public ContextResolver(MethodAnalysisContext referencingMethod, RuntimeContext runtimeContext)
3033
{
3134
if (referencingMethod is ConcreteGenericMethodAnalysisContext)
3235
throw new ArgumentException("Must be a simple method", nameof(referencingMethod));
3336
referencedFrom = referencingMethod.CustomAttributeAssembly;
3437
referencingType = referencingMethod.DeclaringType;
3538
this.referencingMethod = referencingMethod;
39+
this.runtimeContext = runtimeContext;
3640
}
3741

3842
public TypeAnalysisContext? Resolve(TypeSignature? type) => type switch
@@ -86,6 +90,16 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
8690

8791
private TypeAnalysisContext? Resolve(TypeDefOrRefSignature typeDefOrRef)
8892
{
93+
return Resolve(typeDefOrRef.Type);
94+
}
95+
96+
private TypeAnalysisContext? Resolve(ITypeDefOrRef typeDefOrRef)
97+
{
98+
if (typeDefOrRef is TypeSpecification typeSpecification)
99+
{
100+
return Resolve(typeSpecification.Signature);
101+
}
102+
89103
if (typeDefOrRef.DeclaringType is not null)
90104
{
91105
if (!TryResolve(typeDefOrRef.DeclaringType, out var declaringType))
@@ -102,9 +116,9 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
102116
return null;
103117
}
104118

105-
if (typeDefOrRef.Type is not TypeDefinition)
119+
if (typeDefOrRef is not TypeDefinition)
106120
{
107-
typeDefOrRef = (TypeDefOrRefSignature?)typeDefOrRef.Resolve()?.ToTypeSignature() ?? typeDefOrRef;
121+
typeDefOrRef = typeDefOrRef.TryResolve(runtimeContext) ?? typeDefOrRef;
108122
}
109123

110124
var assemblyName = GetName(typeDefOrRef.Scope);
@@ -132,7 +146,12 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
132146

133147
public TypeAnalysisContext? Resolve(ITypeDescriptor? type)
134148
{
135-
return Resolve(type?.ToTypeSignature());
149+
return type switch
150+
{
151+
TypeSignature signature => Resolve(signature),
152+
ITypeDefOrRef typeDefOrRef => Resolve(typeDefOrRef),
153+
_ => null,
154+
};
136155
}
137156

138157
public bool TryResolve(ITypeDescriptor? type, [NotNullWhen(true)] out TypeAnalysisContext? result)
@@ -169,7 +188,7 @@ public bool TryResolve(IEnumerable<TypeSignature?> types, [NotNullWhen(true)] ou
169188

170189
public FieldAnalysisContext? Resolve(IFieldDescriptor fieldDescriptor)
171190
{
172-
var declaringType = Resolve(fieldDescriptor.DeclaringType?.ToTypeSignature());
191+
var declaringType = Resolve(fieldDescriptor.DeclaringType);
173192
if (declaringType is null)
174193
return null;
175194

@@ -222,7 +241,7 @@ public bool TryResolve(IFieldDescriptor fieldDescriptor, [NotNullWhen(true)] out
222241

223242
Debug.Assert(nonGenericDeclaringType is not ReferencedTypeAnalysisContext);
224243

225-
var targetMethod = new ContextResolver(nonGenericDeclaringType).ResolveInType(methodDefOrRef);
244+
var targetMethod = new ContextResolver(nonGenericDeclaringType, runtimeContext).ResolveInType(methodDefOrRef);
226245
if (targetMethod is null)
227246
return null;
228247

@@ -245,7 +264,7 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
245264
public MethodAnalysisContext? Resolve(MethodDefinition methodDefinition)
246265
{
247266
// The declaring type can be resolved with nothing, but resolution for the method itself requires a context.
248-
return TryResolve(methodDefinition.DeclaringType, out var declaringType) ? new ContextResolver(declaringType).ResolveInType(methodDefinition) : null;
267+
return TryResolve(methodDefinition.DeclaringType, out var declaringType) ? new ContextResolver(declaringType, runtimeContext).ResolveInType(methodDefinition) : null;
249268
}
250269

251270
public MethodAnalysisContext? ResolveInType(IMethodDefOrRef methodDefOrRef)
@@ -274,7 +293,7 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
274293
continue;
275294

276295
// We need to use a resolver for the method context to resolve potential method generic parameters correctly.
277-
var methodResolver = new ContextResolver(methodContext);
296+
var methodResolver = new ContextResolver(methodContext, runtimeContext);
278297

279298
if (!methodResolver.TryResolve(methodDefOrRef.Signature.ReturnType, out var returnType) ||
280299
!TypeAnalysisContextEqualityComparer.Instance.Equals(methodContext.ReturnType, returnType))
@@ -304,62 +323,4 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
304323

305324
return baseMethod.MakeGenericInstanceMethod(methodTypeArguments);
306325
}
307-
308-
public TypeAnalysisContext ResolveOrThrow(Type? type, bool allowGenericInstance = true)
309-
{
310-
return Resolve(type, allowGenericInstance) ?? throw new($"Unable to resolve type {type?.FullName}");
311-
}
312-
313-
public TypeAnalysisContext? Resolve(Type? type) => Resolve(type, true);
314-
public TypeAnalysisContext? Resolve(Type? type, bool allowGenericInstance)
315-
{
316-
if (type is null)
317-
return null;
318-
319-
if (type.IsSZArray)
320-
return Resolve(type.GetElementType())?.MakeSzArrayType();
321-
322-
if (type.IsByRef)
323-
return Resolve(type.GetElementType())?.MakeByReferenceType();
324-
325-
if (type.IsPointer)
326-
return Resolve(type.GetElementType())?.MakePointerType();
327-
328-
if (type.IsArray)
329-
return Resolve(type.GetElementType())?.MakeArrayType(type.GetArrayRank());
330-
331-
if (type.IsGenericParameter)
332-
{
333-
if (type.IsGenericTypeParameter)
334-
{
335-
return TryGetGenericParameter(referencingType?.GenericParameters, type.GenericParameterPosition);
336-
}
337-
else
338-
{
339-
Debug.Assert(type.IsGenericMethodParameter);
340-
return TryGetGenericParameter(referencingMethod?.GenericParameters, type.GenericParameterPosition);
341-
}
342-
}
343-
344-
if (type.IsGenericType && allowGenericInstance)
345-
{
346-
var genericArguments = type.GetGenericArguments().Select(Resolve).ToArray();
347-
if (genericArguments.Any(x => x is null))
348-
return null;
349-
350-
var genericType = type.GetGenericTypeDefinition();
351-
return Resolve(genericType, false)?.MakeGenericInstanceType(genericArguments!);
352-
}
353-
354-
if (type.IsFunctionPointer)
355-
throw new NotSupportedException($"Function pointers are not supported: {type.Name}");
356-
357-
// Custom modifiers might be possible to support, but probably not necessary
358-
359-
var assemblyName = type.Assembly.GetName().Name!;
360-
if (assemblyName == "System.Private.CoreLib")
361-
assemblyName = "mscorlib";
362-
var assembly = referencedFrom.AppContext.GetAssemblyByName(assemblyName);
363-
return assembly?.GetTypeByFullName(type.FullName!);
364-
}
365326
}

Il2CppInterop.Generator/Extensions/AsmResolverExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Il2CppInterop.Generator.Extensions;
1+
using AsmResolver.DotNet;
2+
3+
namespace Il2CppInterop.Generator.Extensions;
24

35
internal static class AsmResolverExtensions
46
{
@@ -9,4 +11,9 @@ public bool IsLoadConstantI4
911
get => opCode.Code is (>= CilCode.Ldc_I4_M1 and <= CilCode.Ldc_I4_8) or CilCode.Ldc_I4_S or CilCode.Ldc_I4;
1012
}
1113
}
14+
15+
public static TypeDefinition? TryResolve(this ITypeDescriptor typeDescriptor, RuntimeContext? runtimeContext)
16+
{
17+
return typeDescriptor.TryResolve(runtimeContext, out var resolvedType) ? resolvedType : null;
18+
}
1219
}

Il2CppInterop.Generator/Extensions/InjectedTypeAnalysisContextExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Reflection;
22
using System.Runtime.CompilerServices;
33
using Cpp2IL.Core.Model.Contexts;
4-
using Il2CppInterop.Generator;
54
using LibCpp2IL.BinaryStructures;
65

76
namespace Il2CppInterop.Generator.Extensions;
@@ -18,15 +17,15 @@ public void InjectContentFromSourceType()
1817
var appContext = type.AppContext;
1918

2019
type.SetDefaultBaseType(sourceType.BaseType != null
21-
? new ContextResolver(type).ResolveOrThrow(sourceType.BaseType)
20+
? new SystemTypeResolver(type).ResolveOrThrow(sourceType.BaseType)
2221
: null);
2322

2423
foreach (var fieldInfo in sourceType.GetFields())
2524
{
2625
if (fieldInfo.DeclaringType != sourceType)
2726
continue;
2827

29-
var resolver = new ContextResolver(type);
28+
var resolver = new SystemTypeResolver(type);
3029

3130
type.InjectFieldContext(
3231
fieldInfo.Name,
@@ -67,7 +66,7 @@ public void InjectContentFromSourceType()
6766

6867
methodMap.Add(method, methodContext);
6968

70-
var resolver = new ContextResolver(methodContext);
69+
var resolver = new SystemTypeResolver(methodContext);
7170

7271
var returnType = method switch
7372
{
@@ -94,7 +93,7 @@ public void InjectContentFromSourceType()
9493
if (getMethod == null && setMethod == null)
9594
continue;
9695

97-
var resolver = new ContextResolver(type);
96+
var resolver = new SystemTypeResolver(type);
9897

9998
var propertyType = resolver.ResolveOrThrow(property.PropertyType);
10099
type.InjectPropertyContext(
@@ -116,7 +115,7 @@ public void InjectContentFromSourceType()
116115
if (addMethod == null && removeMethod == null && raiseMethod == null)
117116
continue;
118117

119-
var resolver = new ContextResolver(type);
118+
var resolver = new SystemTypeResolver(type);
120119
var eventType = resolver.ResolveOrThrow(eventInfo.EventHandlerType!);
121120
type.InjectEventContext(
122121
eventInfo.Name,

Il2CppInterop.Generator/Il2CppInterop.Generator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="AssetRipper.Cpp2IL.Core" Version="1.0.0" />
16+
<PackageReference Include="AssetRipper.Cpp2IL.Core" Version="1.0.2" />
1717
<PackageReference Include="PolySharp" Version="1.15.0">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Il2CppInterop.Generator/MscorlibAssemblyInjectionProcessingLayer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
2424

2525
assemblyList = Directory.GetFiles(directoryPath, "*.dll", SearchOption.AllDirectories)
2626
.Where(x => x.Contains("mscorlib", StringComparison.OrdinalIgnoreCase))
27-
.Select(AssemblyDefinition.FromFile)
27+
.Select(path => AssemblyDefinition.FromFile(path))
2828
.ToList();
2929
}
3030

Il2CppInterop.Generator/OriginalMethodBody.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Il2CppInterop.Generator;
99

1010
public class OriginalMethodBody : MethodBodyBase
1111
{
12-
public static bool MaybeStoreOriginalMethodBody(MethodDefinition originalMethod, MethodAnalysisContext methodContext)
12+
public static bool MaybeStoreOriginalMethodBody(MethodDefinition originalMethod, MethodAnalysisContext methodContext, RuntimeContext runtimeContext)
1313
{
14-
if (TryResolveOriginalMethodBody(originalMethod, methodContext, out var originalMethodBody))
14+
if (TryResolveOriginalMethodBody(originalMethod, methodContext, runtimeContext, out var originalMethodBody))
1515
{
1616
methodContext.PutExtraData(originalMethodBody);
1717
return true;
@@ -22,7 +22,7 @@ public static bool MaybeStoreOriginalMethodBody(MethodDefinition originalMethod,
2222
}
2323
}
2424

25-
private static bool TryResolveOriginalMethodBody(MethodDefinition method, MethodAnalysisContext methodContext, [NotNullWhen(true)] out OriginalMethodBody? originalMethodBody)
25+
private static bool TryResolveOriginalMethodBody(MethodDefinition method, MethodAnalysisContext methodContext, RuntimeContext runtimeContext, [NotNullWhen(true)] out OriginalMethodBody? originalMethodBody)
2626
{
2727
var body = method.CilMethodBody;
2828
if (body is null)
@@ -31,7 +31,7 @@ private static bool TryResolveOriginalMethodBody(MethodDefinition method, Method
3131
return false;
3232
}
3333

34-
var resolver = new ContextResolver(methodContext);
34+
var resolver = new ContextResolver(methodContext, runtimeContext);
3535

3636
var originalInstructions = body.Instructions;
3737
originalInstructions.ExpandMacrosBackport();
@@ -79,7 +79,7 @@ private static bool TryResolveOriginalMethodBody(MethodDefinition method, Method
7979
}
8080
else
8181
{
82-
exceptionType = resolver.Resolve(exceptionHandler.ExceptionType.ToTypeSignature());
82+
exceptionType = resolver.Resolve(exceptionHandler.ExceptionType);
8383
if (exceptionType is null)
8484
{
8585
originalMethodBody = default;
@@ -109,7 +109,7 @@ private static bool TryResolveOriginalMethodBody(MethodDefinition method, Method
109109
AsmResolver.Utf8String utf8String => utf8String.ToString(),
110110
CilLocalVariable localVariable => newLocalVariables[localVariable.Index],
111111
Parameter parameter => parameter == method.Parameters.ThisParameter ? This.Instance : methodContext.Parameters[parameter.Index],
112-
ITypeDescriptor typeDescriptor => resolver.Resolve(typeDescriptor.ToTypeSignature()),
112+
ITypeDescriptor typeDescriptor => resolver.Resolve(typeDescriptor),
113113
IFieldDescriptor { Signature: not null } fieldDescriptor => resolver.Resolve(fieldDescriptor),
114114
IMethodDescriptor { Signature: not null } methodDescriptor => resolver.Resolve(methodDescriptor),
115115
ICilLabel label => ResolveLabel(newInstructions, originalInstructions, label),

0 commit comments

Comments
 (0)