Skip to content

Commit

Permalink
Automatically load all interop assemblies before loading plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Feb 9, 2024
1 parent 910844a commit bea0887
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Runtimes/Unity/BepInEx.Unity.IL2CPP/IL2CPPChainloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameter

unhook = true;

Il2CppInteropManager.PreloadInteropAssemblies();

Instance.Execute();
}
catch (Exception ex)
Expand Down
39 changes: 39 additions & 0 deletions Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ static Il2CppInteropManager()
.AppendLine("{BepInEx} - Path to the BepInEx folder.")
.AppendLine("{ProcessName} - Name of the current process")
.ToString());

private static readonly ConfigEntry<bool> PreloadIL2CPPInteropAssemblies = ConfigFile.CoreConfig.Bind(
"IL2CPP", "PreloadIL2CPPInteropAssemblies",
true,
new StringBuilder()
.AppendLine("Automatically load all interop assemblies right before loading plugins.")
.AppendLine("Some plugins may not work properly without this, but it may cause issues in some games.")
.ToString());

private static readonly ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("InteropManager");

Expand Down Expand Up @@ -358,4 +366,35 @@ private static void RunIl2CppInteropGenerator(List<AssemblyDefinition> sourceAss

sourceAssemblies.Do(x => x.Dispose());
}

internal static void PreloadInteropAssemblies()
{
if (!PreloadIL2CPPInteropAssemblies.Value)
return;

var sw = Stopwatch.StartNew();

var dir = new DirectoryInfo(IL2CPPInteropAssemblyPath);
var files = dir.GetFiles();
System.Threading.Tasks.Parallel.ForEach(files, file =>
{
if (file.Name.Equals("netstandard.dll", StringComparison.OrdinalIgnoreCase))
return;
if (file.Extension.Equals(".dll", StringComparison.OrdinalIgnoreCase))
{
try
{
var assemblyName = AssemblyName.GetAssemblyName(file.FullName);
Assembly.Load(assemblyName);
}
catch (Exception e)
{
Logger.Log(BepInEx.Logging.LogLevel.Warning, $"Failed to preload {file.Name} - {e}");
}
}
});

Logger.Log(BepInEx.Logging.LogLevel.Debug, $"Preloaded {files.Length} assemblies in {sw.ElapsedMilliseconds}ms");
}
}

0 comments on commit bea0887

Please sign in to comment.