Skip to content

Auto inject Il2Cpp-Types #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add support for Interfaces in Auto-Injector
  • Loading branch information
PHPCore committed Nov 16, 2021
commit 34ed233707303a75ef8477f3956ab8bc6eb59061
10 changes: 2 additions & 8 deletions BepInEx.IL2CPP/IL2CPPChainloader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -156,13 +156,7 @@ protected override void InitializeLoggers()

public override BasePlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly)
{
foreach(var type in pluginAssembly.DefinedTypes)
{
if(typeof(Il2CppObjectBase).IsAssignableFrom(type) && !ClassInjector.IsTypeRegisteredInIl2Cpp(type))
{
ClassInjector.RegisterTypeInIl2Cpp(type);
}
}
PluginClassInjector.RegisterAssemblyInIl2Cpp(pluginAssembly);

var pluginType = pluginAssembly.GetType(pluginInfo.TypeName);

Expand Down
45 changes: 45 additions & 0 deletions BepInEx.IL2CPP/PluginClassInjector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Reflection;
using UnhollowerBaseLib;
using UnhollowerRuntimeLib;

namespace BepInEx.IL2CPP
{

public class Il2CppInterfaces : Attribute
{

public Type[] Interfaces { get; protected set; }

public Il2CppInterfaces(params Type[] interfaces)
{
Interfaces = interfaces;
}

}

internal class PluginClassInjector
{

public static void RegisterAssemblyInIl2Cpp(Assembly pluginAssembly)
{
foreach (var type in pluginAssembly.DefinedTypes)
if (typeof(Il2CppObjectBase).IsAssignableFrom(type) && !ClassInjector.IsTypeRegisteredInIl2Cpp(type))
RegisterTypeInIl2Cpp(type);
}

public static void RegisterTypeInIl2Cpp(Type type)
{
if (!ClassInjector.IsTypeRegisteredInIl2Cpp(type.BaseType))
RegisterTypeInIl2Cpp(type.BaseType);

var interfaces = type.GetCustomAttribute<Il2CppInterfaces>();

if (interfaces != null)
ClassInjector.RegisterTypeInIl2CppWithInterfaces(type, true, interfaces.Interfaces);
else
ClassInjector.RegisterTypeInIl2Cpp(type);
}

}
}