Skip to content

Commit 6aabdb5

Browse files
authored
Switch to the development branch of Cpp2IL (#533)
1 parent 0b23557 commit 6aabdb5

4 files changed

Lines changed: 106 additions & 26 deletions

File tree

Runtimes/Unity/BepInEx.Unity.IL2CPP/BepInEx.Unity.IL2CPP.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
</ItemGroup>
1515
<ItemGroup>
1616
<PackageReference Include="HarmonyX" Version="2.10.1"/>
17-
<PackageReference Include="Iced" Version="1.17.0"/>
17+
<PackageReference Include="Iced" Version="1.18.0"/>
1818
<PackageReference Include="Il2CppInterop.Generator" Version="1.4.1"/>
1919
<PackageReference Include="Il2CppInterop.HarmonySupport" Version="1.4.1"/>
2020
<PackageReference Include="Il2CppInterop.ReferenceLibs" Version="1.0.0" IncludeAssets="compile" PrivateAssets="all"/>
2121
<PackageReference Include="Il2CppInterop.Runtime" Version="1.4.1"/>
2222
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" IncludeAssets="compile" PrivateAssets="all"/>
2323
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" IncludeAssets="compile" PrivateAssets="all"/>
2424
<PackageReference Include="MonoMod.RuntimeDetour" Version="22.5.1.1"/>
25-
<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.0.7.2"/>
25+
<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.1.0-development.836"/>
2626
</ItemGroup>
2727

2828
<!-- CopyLocalLockFileAssemblies causes to also output shared assemblies: https://github.com/NuGet/Home/issues/4837#issuecomment-354536302 -->
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using Mono.Cecil;
5+
6+
namespace BepInEx.Unity.IL2CPP;
7+
8+
internal static partial class Il2CppInteropManager
9+
{
10+
private sealed class AsmToCecilConverter : IAssemblyResolver
11+
{
12+
private readonly Dictionary<string, AsmResolver.DotNet.AssemblyDefinition> asmResolverDictionary;
13+
private readonly Dictionary<string, AssemblyDefinition> cecilDictionary = new();
14+
private readonly Dictionary<AsmResolver.DotNet.AssemblyDefinition, AssemblyDefinition> asmToCecil = new();
15+
public AsmToCecilConverter(List<AsmResolver.DotNet.AssemblyDefinition> list)
16+
{
17+
asmResolverDictionary = list.ToDictionary(a => a.Name?.ToString(), a => a);
18+
}
19+
20+
public void Dispose() { }
21+
public AssemblyDefinition Resolve(AssemblyNameReference name) => Resolve(name, new() { AssemblyResolver = this });
22+
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
23+
{
24+
var assemblyName = name.Name;
25+
if (!cecilDictionary.TryGetValue(assemblyName, out var cecilAssembly) && asmResolverDictionary.TryGetValue(assemblyName, out var asmAssembly))
26+
{
27+
cecilAssembly = Convert(asmAssembly, parameters);
28+
}
29+
return cecilAssembly;
30+
}
31+
32+
public List<AssemblyDefinition> ConvertAll()
33+
{
34+
List<AssemblyDefinition> cecilAssemblies = new(asmResolverDictionary.Count);
35+
foreach (var asmResolverAssembly in asmResolverDictionary.Values)
36+
{
37+
var cecilAssembly = Convert(asmResolverAssembly);
38+
cecilAssemblies.Add(cecilAssembly);
39+
}
40+
return cecilAssemblies;
41+
}
42+
43+
private AssemblyDefinition Convert(AsmResolver.DotNet.AssemblyDefinition asmResolverAssembly)
44+
{
45+
return Convert(asmResolverAssembly, new ReaderParameters() { AssemblyResolver = this });
46+
}
47+
48+
private AssemblyDefinition Convert(AsmResolver.DotNet.AssemblyDefinition asmResolverAssembly, ReaderParameters readerParameters)
49+
{
50+
if (asmToCecil.TryGetValue(asmResolverAssembly, out var cecilAssembly))
51+
{
52+
return cecilAssembly;
53+
}
54+
MemoryStream stream = new();
55+
asmResolverAssembly.WriteManifest(stream);
56+
stream.Position = 0;
57+
cecilAssembly = AssemblyDefinition.ReadAssembly(stream, readerParameters);
58+
cecilDictionary.Add(cecilAssembly.Name.Name, cecilAssembly);
59+
asmToCecil.Add(asmResolverAssembly, cecilAssembly);
60+
return cecilAssembly;
61+
}
62+
}
63+
}

Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
using BepInEx.Unity.IL2CPP.Hook;
1515
using BepInEx.Unity.IL2CPP.Logging;
1616
using Cpp2IL.Core;
17+
using Cpp2IL.Core.Api;
18+
using Cpp2IL.Core.InstructionSets;
19+
using Cpp2IL.Core.OutputFormats;
20+
using Cpp2IL.Core.ProcessingLayers;
1721
using HarmonyLib;
1822
using Il2CppInterop.Common;
1923
using Il2CppInterop.Generator;
@@ -29,8 +33,15 @@
2933

3034
namespace BepInEx.Unity.IL2CPP;
3135

32-
internal static class Il2CppInteropManager
36+
internal static partial class Il2CppInteropManager
3337
{
38+
static Il2CppInteropManager()
39+
{
40+
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_32);
41+
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_64);
42+
LibCpp2IlBinaryRegistry.RegisterBuiltInBinarySupport();
43+
}
44+
3445
private static readonly ConfigEntry<bool> UpdateInteropAssemblies =
3546
ConfigFile.CoreConfig.Bind("IL2CPP",
3647
"UpdateInteropAssemblies",
@@ -199,17 +210,18 @@ private static void GenerateInteropAssemblies()
199210

200211
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(UnityBaseLibsDirectory);
201212
DownloadUnityAssemblies();
202-
var dummyAssemblies = RunCpp2Il();
213+
var asmResolverAssemblies = RunCpp2Il();
214+
var cecilAssemblies = new AsmToCecilConverter(asmResolverAssemblies).ConvertAll();
203215

204216
if (DumpDummyAssemblies.Value)
205217
{
206218
var dummyPath = Path.Combine(Paths.BepInExRootPath, "dummy");
207219
Directory.CreateDirectory(dummyPath);
208-
foreach (var assemblyDefinition in dummyAssemblies)
220+
foreach (var assemblyDefinition in cecilAssemblies)
209221
assemblyDefinition.Write(Path.Combine(dummyPath, $"{assemblyDefinition.Name.Name}.dll"));
210222
}
211223

212-
RunIl2CppInteropGenerator(dummyAssemblies);
224+
RunIl2CppInteropGenerator(cecilAssemblies);
213225

214226
File.WriteAllText(HashPath, ComputeHash());
215227
}
@@ -242,7 +254,7 @@ private static void DownloadUnityAssemblies()
242254
}
243255
}
244256

245-
private static List<AssemblyDefinition> RunCpp2Il()
257+
private static List<AsmResolver.DotNet.AssemblyDefinition> RunCpp2Il()
246258
{
247259
Logger.LogMessage("Running Cpp2IL to generate dummy assemblies");
248260

@@ -252,40 +264,44 @@ private static List<AssemblyDefinition> RunCpp2Il()
252264
"Metadata",
253265
"global-metadata.dat");
254266

255-
List<AssemblyDefinition> sourceAssemblies;
256-
257267
var stopwatch = new Stopwatch();
258268
stopwatch.Start();
259269

260270
var cpp2IlLogger = BepInEx.Logging.Logger.CreateLogSource("Cpp2IL");
261271

262-
Cpp2IL.Core.Logger.VerboseLog += (message, s) =>
272+
Cpp2IL.Core.Logging.Logger.VerboseLog += (message, s) =>
263273
cpp2IlLogger.LogDebug($"[{s}] {message.Trim()}");
264-
Cpp2IL.Core.Logger.InfoLog += (message, s) =>
274+
Cpp2IL.Core.Logging.Logger.InfoLog += (message, s) =>
265275
cpp2IlLogger.LogInfo($"[{s}] {message.Trim()}");
266-
Cpp2IL.Core.Logger.WarningLog += (message, s) =>
276+
Cpp2IL.Core.Logging.Logger.WarningLog += (message, s) =>
267277
cpp2IlLogger.LogWarning($"[{s}] {message.Trim()}");
268-
Cpp2IL.Core.Logger.ErrorLog += (message, s) =>
278+
Cpp2IL.Core.Logging.Logger.ErrorLog += (message, s) =>
269279
cpp2IlLogger.LogError($"[{s}] {message.Trim()}");
270280

271281
var unityVersion = UnityInfo.Version;
272-
Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, new int[]
282+
Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, unityVersion, false);
283+
284+
List<Cpp2IlProcessingLayer> processingLayers = new() { new AttributeInjectorProcessingLayer(), };
285+
286+
foreach (var cpp2IlProcessingLayer in processingLayers)
273287
{
274-
unityVersion.Major,
275-
unityVersion.Minor,
276-
unityVersion.Build,
277-
}, false);
278-
sourceAssemblies = Cpp2IlApi.MakeDummyDLLs();
279-
Cpp2IlApi.RunAttributeRestorationForAllAssemblies(null,
280-
LibCpp2IlMain.MetadataVersion >= 29 ||
281-
LibCpp2IlMain.Binary!.InstructionSet is InstructionSet.X86_32
282-
or InstructionSet.X86_64);
283-
Cpp2IlApi.DisposeAndCleanupAll();
288+
cpp2IlProcessingLayer.PreProcess(Cpp2IlApi.CurrentAppContext, processingLayers);
289+
}
290+
291+
foreach (var cpp2IlProcessingLayer in processingLayers)
292+
{
293+
cpp2IlProcessingLayer.Process(Cpp2IlApi.CurrentAppContext);
294+
}
295+
296+
var assemblies = new AsmResolverDummyDllOutputFormat().BuildAssemblies(Cpp2IlApi.CurrentAppContext);
297+
298+
LibCpp2IlMain.Reset();
299+
Cpp2IlApi.CurrentAppContext = null;
284300

285301
stopwatch.Stop();
286302
Logger.LogInfo($"Cpp2IL finished in {stopwatch.Elapsed}");
287303

288-
return sourceAssemblies;
304+
return assemblies;
289305
}
290306

291307
private static void RunIl2CppInteropGenerator(List<AssemblyDefinition> sourceAssemblies)

nuget.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<configuration>
33
<packageSources>
44
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
5+
<add key="Samboy" value="https://nuget.samboy.dev/v3/index.json" />
56
</packageSources>
6-
</configuration>
7+
</configuration>

0 commit comments

Comments
 (0)