-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to the
development
branch of Cpp2IL (#533)
- Loading branch information
Showing
4 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.AsmToCecilConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Mono.Cecil; | ||
|
||
namespace BepInEx.Unity.IL2CPP; | ||
|
||
internal static partial class Il2CppInteropManager | ||
{ | ||
private sealed class AsmToCecilConverter : IAssemblyResolver | ||
{ | ||
private readonly Dictionary<string, AsmResolver.DotNet.AssemblyDefinition> asmResolverDictionary; | ||
private readonly Dictionary<string, AssemblyDefinition> cecilDictionary = new(); | ||
private readonly Dictionary<AsmResolver.DotNet.AssemblyDefinition, AssemblyDefinition> asmToCecil = new(); | ||
public AsmToCecilConverter(List<AsmResolver.DotNet.AssemblyDefinition> list) | ||
{ | ||
asmResolverDictionary = list.ToDictionary(a => a.Name?.ToString(), a => a); | ||
} | ||
|
||
public void Dispose() { } | ||
public AssemblyDefinition Resolve(AssemblyNameReference name) => Resolve(name, new() { AssemblyResolver = this }); | ||
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) | ||
{ | ||
var assemblyName = name.Name; | ||
if (!cecilDictionary.TryGetValue(assemblyName, out var cecilAssembly) && asmResolverDictionary.TryGetValue(assemblyName, out var asmAssembly)) | ||
{ | ||
cecilAssembly = Convert(asmAssembly, parameters); | ||
} | ||
return cecilAssembly; | ||
} | ||
|
||
public List<AssemblyDefinition> ConvertAll() | ||
{ | ||
List<AssemblyDefinition> cecilAssemblies = new(asmResolverDictionary.Count); | ||
foreach (var asmResolverAssembly in asmResolverDictionary.Values) | ||
{ | ||
var cecilAssembly = Convert(asmResolverAssembly); | ||
cecilAssemblies.Add(cecilAssembly); | ||
} | ||
return cecilAssemblies; | ||
} | ||
|
||
private AssemblyDefinition Convert(AsmResolver.DotNet.AssemblyDefinition asmResolverAssembly) | ||
{ | ||
return Convert(asmResolverAssembly, new ReaderParameters() { AssemblyResolver = this }); | ||
} | ||
|
||
private AssemblyDefinition Convert(AsmResolver.DotNet.AssemblyDefinition asmResolverAssembly, ReaderParameters readerParameters) | ||
{ | ||
if (asmToCecil.TryGetValue(asmResolverAssembly, out var cecilAssembly)) | ||
{ | ||
return cecilAssembly; | ||
} | ||
MemoryStream stream = new(); | ||
asmResolverAssembly.WriteManifest(stream); | ||
stream.Position = 0; | ||
cecilAssembly = AssemblyDefinition.ReadAssembly(stream, readerParameters); | ||
cecilDictionary.Add(cecilAssembly.Name.Name, cecilAssembly); | ||
asmToCecil.Add(asmResolverAssembly, cecilAssembly); | ||
return cecilAssembly; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters