Skip to content

Commit

Permalink
Switch to the development branch of Cpp2IL (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Dec 30, 2022
1 parent 0b23557 commit 6aabdb5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="HarmonyX" Version="2.10.1"/>
<PackageReference Include="Iced" Version="1.17.0"/>
<PackageReference Include="Iced" Version="1.18.0"/>
<PackageReference Include="Il2CppInterop.Generator" Version="1.4.1"/>
<PackageReference Include="Il2CppInterop.HarmonySupport" Version="1.4.1"/>
<PackageReference Include="Il2CppInterop.ReferenceLibs" Version="1.0.0" IncludeAssets="compile" PrivateAssets="all"/>
<PackageReference Include="Il2CppInterop.Runtime" Version="1.4.1"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" IncludeAssets="compile" PrivateAssets="all"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" IncludeAssets="compile" PrivateAssets="all"/>
<PackageReference Include="MonoMod.RuntimeDetour" Version="22.5.1.1"/>
<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.0.7.2"/>
<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.1.0-development.836"/>
</ItemGroup>

<!-- CopyLocalLockFileAssemblies causes to also output shared assemblies: https://github.com/NuGet/Home/issues/4837#issuecomment-354536302 -->
Expand Down
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;
}
}
}
62 changes: 39 additions & 23 deletions Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
using BepInEx.Unity.IL2CPP.Hook;
using BepInEx.Unity.IL2CPP.Logging;
using Cpp2IL.Core;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.InstructionSets;
using Cpp2IL.Core.OutputFormats;
using Cpp2IL.Core.ProcessingLayers;
using HarmonyLib;
using Il2CppInterop.Common;
using Il2CppInterop.Generator;
Expand All @@ -29,8 +33,15 @@

namespace BepInEx.Unity.IL2CPP;

internal static class Il2CppInteropManager
internal static partial class Il2CppInteropManager
{
static Il2CppInteropManager()
{
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_32);
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_64);
LibCpp2IlBinaryRegistry.RegisterBuiltInBinarySupport();
}

private static readonly ConfigEntry<bool> UpdateInteropAssemblies =
ConfigFile.CoreConfig.Bind("IL2CPP",
"UpdateInteropAssemblies",
Expand Down Expand Up @@ -199,17 +210,18 @@ private static void GenerateInteropAssemblies()

AppDomain.CurrentDomain.AddCecilPlatformAssemblies(UnityBaseLibsDirectory);
DownloadUnityAssemblies();
var dummyAssemblies = RunCpp2Il();
var asmResolverAssemblies = RunCpp2Il();
var cecilAssemblies = new AsmToCecilConverter(asmResolverAssemblies).ConvertAll();

if (DumpDummyAssemblies.Value)
{
var dummyPath = Path.Combine(Paths.BepInExRootPath, "dummy");
Directory.CreateDirectory(dummyPath);
foreach (var assemblyDefinition in dummyAssemblies)
foreach (var assemblyDefinition in cecilAssemblies)
assemblyDefinition.Write(Path.Combine(dummyPath, $"{assemblyDefinition.Name.Name}.dll"));
}

RunIl2CppInteropGenerator(dummyAssemblies);
RunIl2CppInteropGenerator(cecilAssemblies);

File.WriteAllText(HashPath, ComputeHash());
}
Expand Down Expand Up @@ -242,7 +254,7 @@ private static void DownloadUnityAssemblies()
}
}

private static List<AssemblyDefinition> RunCpp2Il()
private static List<AsmResolver.DotNet.AssemblyDefinition> RunCpp2Il()
{
Logger.LogMessage("Running Cpp2IL to generate dummy assemblies");

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

List<AssemblyDefinition> sourceAssemblies;

var stopwatch = new Stopwatch();
stopwatch.Start();

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

Cpp2IL.Core.Logger.VerboseLog += (message, s) =>
Cpp2IL.Core.Logging.Logger.VerboseLog += (message, s) =>
cpp2IlLogger.LogDebug($"[{s}] {message.Trim()}");
Cpp2IL.Core.Logger.InfoLog += (message, s) =>
Cpp2IL.Core.Logging.Logger.InfoLog += (message, s) =>
cpp2IlLogger.LogInfo($"[{s}] {message.Trim()}");
Cpp2IL.Core.Logger.WarningLog += (message, s) =>
Cpp2IL.Core.Logging.Logger.WarningLog += (message, s) =>
cpp2IlLogger.LogWarning($"[{s}] {message.Trim()}");
Cpp2IL.Core.Logger.ErrorLog += (message, s) =>
Cpp2IL.Core.Logging.Logger.ErrorLog += (message, s) =>
cpp2IlLogger.LogError($"[{s}] {message.Trim()}");

var unityVersion = UnityInfo.Version;
Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, new int[]
Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, unityVersion, false);

List<Cpp2IlProcessingLayer> processingLayers = new() { new AttributeInjectorProcessingLayer(), };

foreach (var cpp2IlProcessingLayer in processingLayers)
{
unityVersion.Major,
unityVersion.Minor,
unityVersion.Build,
}, false);
sourceAssemblies = Cpp2IlApi.MakeDummyDLLs();
Cpp2IlApi.RunAttributeRestorationForAllAssemblies(null,
LibCpp2IlMain.MetadataVersion >= 29 ||
LibCpp2IlMain.Binary!.InstructionSet is InstructionSet.X86_32
or InstructionSet.X86_64);
Cpp2IlApi.DisposeAndCleanupAll();
cpp2IlProcessingLayer.PreProcess(Cpp2IlApi.CurrentAppContext, processingLayers);
}

foreach (var cpp2IlProcessingLayer in processingLayers)
{
cpp2IlProcessingLayer.Process(Cpp2IlApi.CurrentAppContext);
}

var assemblies = new AsmResolverDummyDllOutputFormat().BuildAssemblies(Cpp2IlApi.CurrentAppContext);

LibCpp2IlMain.Reset();
Cpp2IlApi.CurrentAppContext = null;

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

return sourceAssemblies;
return assemblies;
}

private static void RunIl2CppInteropGenerator(List<AssemblyDefinition> sourceAssemblies)
Expand Down
3 changes: 2 additions & 1 deletion nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<configuration>
<packageSources>
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
<add key="Samboy" value="https://nuget.samboy.dev/v3/index.json" />
</packageSources>
</configuration>
</configuration>

0 comments on commit 6aabdb5

Please sign in to comment.