Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cpp2IL.Core/Cpp2IL.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

<ItemGroup>
<!--Needed for DLL output-->
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.5" />
<PackageReference Include="AssetRipper.CIL" Version="1.2.2" />
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.6" />
<PackageReference Include="AssetRipper.CIL" Version="1.3.0" />

<!--For ARM64 dissassembly-->
<PackageReference Include="Disarm" Version="2022.1.0-master.99" />
Expand Down
25 changes: 12 additions & 13 deletions Cpp2IL.Core/OutputFormats/AsmResolverDummyDllOutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,26 @@ protected virtual void FillMethodBodies(AssemblyAnalysisContext context)

private List<AssemblyDefinition> BuildStubAssemblies(ApplicationAnalysisContext context)
{
var assemblyResolver = new Il2CppAssemblyResolver();
var metadataResolver = new DefaultMetadataResolver(assemblyResolver);

var corlib = context.Assemblies.First(a => a.Name == "mscorlib");
MostRecentCorLib = BuildStubAssembly(corlib, null, metadataResolver);
assemblyResolver.DummyAssemblies.Add(MostRecentCorLib.Name!, MostRecentCorLib);
MostRecentCorLib = BuildStubAssembly(corlib, null, null);

// The runtime info is irrelevant because we're creating our own corlib, but AsmResolver still requires that we specify one.
var runtimeContext = new RuntimeContext(DotNetRuntimeInfo.NetCoreApp(9, 0), (bool?)null, MostRecentCorLib);
runtimeContext.AddAssembly(MostRecentCorLib);

context.PutExtraData("AsmResolverRuntimeContext", runtimeContext);

var ret = context.Assemblies
// .AsParallel()
.Where(a => a.Name != "mscorlib")
.Select(a => BuildStubAssembly(a, MostRecentCorLib, metadataResolver))
.Select(a => BuildStubAssembly(a, MostRecentCorLib, runtimeContext))
.ToList();

ret.ForEach(a => assemblyResolver.DummyAssemblies.Add(a.Name!, a));

ret.Add(MostRecentCorLib);
return ret;
}

private static AssemblyDefinition BuildStubAssembly(AssemblyAnalysisContext assemblyContext, AssemblyDefinition? corLib, IMetadataResolver metadataResolver)
private static AssemblyDefinition BuildStubAssembly(AssemblyAnalysisContext assemblyContext, AssemblyDefinition? corLib, RuntimeContext? runtimeContext)
{
//Get the name of the assembly (= the name of the DLL without the file extension)
//Build an AsmResolver assembly from this definition
Expand All @@ -182,12 +182,11 @@ private static AssemblyDefinition BuildStubAssembly(AssemblyAnalysisContext asse
var moduleName = assemblyContext.CleanAssemblyName + ".dll";

//Use either ourself as corlib, if we are corlib, otherwise the provided one
var managedModule = new ModuleDefinition(moduleName, corLib is not null ? new(corLib) : null)
{
MetadataResolver = metadataResolver
};
var managedModule = new ModuleDefinition(moduleName, corLib is not null ? new(corLib) : null);
ourAssembly.Modules.Add(managedModule);

runtimeContext?.AddAssembly(ourAssembly);

foreach (var il2CppTypeDefinition in assemblyContext.TopLevelTypes)
{
if (il2CppTypeDefinition.Name != "<Module>")
Expand Down
17 changes: 9 additions & 8 deletions Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ private static void CopyEventsInType(ReferenceImporter importer, TypeAnalysisCon
public static void AddExplicitInterfaceImplementations(AssemblyAnalysisContext asmContext)
{
var managedAssembly = asmContext.GetExtraData<AssemblyDefinition>("AsmResolverAssembly") ?? throw new("AsmResolver assembly not found in assembly analysis context for " + asmContext);
var runtimeContext = asmContext.AppContext.GetExtraData<RuntimeContext>("AsmResolverRuntimeContext") ?? throw new("AsmResolver runtime context not found in application analysis context");

var importer = managedAssembly.ManifestModule!.DefaultImporter;

Expand All @@ -514,7 +515,7 @@ public static void AddExplicitInterfaceImplementations(AssemblyAnalysisContext a
try
#endif
{
AddExplicitInterfaceImplementations(managedType, typeContext, importer);
AddExplicitInterfaceImplementations(managedType, typeContext, importer, runtimeContext);
}
#if !DEBUG
catch (Exception e)
Expand All @@ -525,7 +526,7 @@ public static void AddExplicitInterfaceImplementations(AssemblyAnalysisContext a
}
}

private static void AddExplicitInterfaceImplementations(TypeDefinition type, TypeAnalysisContext typeContext, ReferenceImporter importer)
private static void AddExplicitInterfaceImplementations(TypeDefinition type, TypeAnalysisContext typeContext, ReferenceImporter importer, RuntimeContext runtimeContext)
{
List<(PropertyDefinition InterfaceProperty, TypeSignature InterfaceType, MethodDefinition Method)>? getMethodsToCreate = null;
List<(PropertyDefinition InterfaceProperty, TypeSignature InterfaceType, MethodDefinition Method)>? setMethodsToCreate = null;
Expand All @@ -540,20 +541,20 @@ private static void AddExplicitInterfaceImplementations(TypeDefinition type, Typ
var interfaceMethod = (IMethodDefOrRef)overrideContext.ToMethodDescriptor(importer.TargetModule);
var method = methodContext.GetExtraData<MethodDefinition>("AsmResolverMethod") ?? throw new($"AsmResolver method not found in method analysis context for {methodContext}");
type.MethodImplementations.Add(new MethodImplementation(interfaceMethod, method));
var interfaceMethodResolved = interfaceMethod.Resolve();
if (interfaceMethodResolved != null)
var resolutionStatus = interfaceMethod.Resolve(runtimeContext, out var interfaceMethodResolved);
if (resolutionStatus == ResolutionStatus.Success && interfaceMethodResolved != null)
{
if (interfaceMethodResolved.IsGetMethod && !method.IsGetMethod)
{
getMethodsToCreate ??= [];
var interfacePropertyResolved = interfaceMethodResolved.DeclaringType!.Properties.First(p => p.Semantics.Contains(interfaceMethodResolved.Semantics));
getMethodsToCreate.Add((interfacePropertyResolved, interfaceMethod.DeclaringType!.ToTypeSignature(), method));
getMethodsToCreate.Add((interfacePropertyResolved, interfaceMethod.DeclaringType!.ToTypeSignature(runtimeContext), method));
}
else if (interfaceMethodResolved.IsSetMethod && !method.IsSetMethod)
{
setMethodsToCreate ??= [];
var interfacePropertyResolved = interfaceMethodResolved.DeclaringType!.Properties.First(p => p.Semantics.Contains(interfaceMethodResolved.Semantics));
setMethodsToCreate.Add((interfacePropertyResolved, interfaceMethod.DeclaringType!.ToTypeSignature(), method));
setMethodsToCreate.Add((interfacePropertyResolved, interfaceMethod.DeclaringType!.ToTypeSignature(runtimeContext), method));
}
}
}
Expand All @@ -566,7 +567,7 @@ private static void AddExplicitInterfaceImplementations(TypeDefinition type, Typ
{
var (interfaceProperty, interfaceType, getMethod) = entry;
var setMethod = setMethodsToCreate?
.FirstOrDefault(e => e.InterfaceProperty == interfaceProperty && SignatureComparer.Default.Equals(e.InterfaceType, interfaceType))
.FirstOrDefault(e => e.InterfaceProperty == interfaceProperty && runtimeContext.SignatureComparer.Equals(e.InterfaceType, interfaceType))
.Method;

var name = $"{interfaceType.FullName}.{interfaceProperty.Name}";
Expand All @@ -583,7 +584,7 @@ private static void AddExplicitInterfaceImplementations(TypeDefinition type, Typ
foreach (var entry in setMethodsToCreate)
{
var (interfaceProperty, interfaceType, setMethod) = entry;
if (getMethodsToCreate?.Any(e => e.InterfaceProperty == interfaceProperty && SignatureComparer.Default.Equals(e.InterfaceType, interfaceType)) == true)
if (getMethodsToCreate?.Any(e => e.InterfaceProperty == interfaceProperty && runtimeContext.SignatureComparer.Equals(e.InterfaceType, interfaceType)) == true)
continue;
var name = $"{interfaceType.FullName}.{interfaceProperty.Name}";
var propertySignature = setMethod.IsStatic
Expand Down
3 changes: 2 additions & 1 deletion Cpp2IL.Core/Utils/AsmResolver/AsmResolverUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public static TypeDefinition GetPrimitiveTypeDef(Il2CppTypeEnum type) =>
typeArguments[i] = typeArgument;
}

return baseType.MakeGenericInstanceType(typeArguments);
var runtimeContext = Cpp2IlApi.CurrentAppContext!.GetExtraData<RuntimeContext>("RuntimeContext") ?? throw new("AsmResolver runtime context not found in application analysis context");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not massively in love with this adding a new reliance on the static state (CurrentAppContext) I'm somewhat trying to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really new. This method already relied on Cpp2IlApi.CurrentAppContext transitively through TryLookupTypeDefKnownNotGeneric.

return baseType.MakeGenericInstanceType(runtimeContext, typeArguments);
}

private readonly record struct ParsedTypeString(string BaseType, string Suffix, string[] GenericArguments);
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Utils/AsmResolver/ContextToTypeSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private static TypeDefinition GetTypeDefinition(this TypeAnalysisContext context
public static TypeSignature ToTypeSignature(this TypeAnalysisContext context, ModuleDefinition parentModule) => context switch
{
ReferencedTypeAnalysisContext referencedTypeAnalysisContext => referencedTypeAnalysisContext.ToTypeSignature(parentModule),
_ => parentModule.DefaultImporter.ImportType(context.GetTypeDefinition()).ToTypeSignature()
_ => parentModule.DefaultImporter.ImportType(context.GetTypeDefinition()).ToTypeSignature(parentModule.RuntimeContext)
};

public static TypeSignature ToTypeSignature(this ReferencedTypeAnalysisContext context, ModuleDefinition parentModule) => context switch
Expand Down
38 changes: 0 additions & 38 deletions Cpp2IL.Core/Utils/AsmResolver/Il2CppAssemblyResolver.cs

This file was deleted.

Loading