Skip to content
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

Allow static method injection #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

limoka
Copy link
Contributor

@limoka limoka commented Jun 10, 2023

This PR enables static methods to be injected. This mostly allows to pass such methods to il2cpp delegates, which can be useful in some cases.

My main use of this is for interacting with Burst compiler. It expects us to pass delegates pointing to valid il2cpp methods to consider them for Burst compilation.

I have tested these changes on Core Keeper Unity 2021.3.14.

Copy link
Member

@js6pak js6pak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested these changes

Have you? As is the PR had no chance of working at all.

@@ -559,10 +558,12 @@ private static bool IsMethodEligible(MethodInfo method)
foreach (var parameter in method.GetParameters())
{
var parameterType = parameter.ParameterType;
if (!IsTypeSupported(parameterType))
if (!IsTypeSupported(parameterType) ||
method.IsStatic && parameterType.IsGenericType && parameterType.ContainsGenericParameters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this check for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember exactly, but best guess would be that somewhere there was a static method with one of the parameters being a generic type, and likely that was causing issues.

@js6pak
Copy link
Member

js6pak commented Feb 25, 2024

My test code:

public class Test : Il2CppSystem.Object
{
    public Test() : base(ClassInjector.DerivedConstructorPointer<Test>())
    {
        ClassInjector.DerivedConstructorBody(this);
    }

    public void InstanceFoo(int a, int b)
    {
        System.Console.WriteLine($"InstanceFoo {a} {b}");
    }

    public static void StaticFoo(int a, int b)
    {
        System.Console.WriteLine($"StaticFoo {a} {b}");
    }
}
ClassInjector.RegisterTypeInIl2Cpp<Test>();
var type = Il2CppType.Of<Test>();
var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (var methodInfo in methods)
{
    Log.LogWarning(methodInfo.Name);
    if (methodInfo.Name.EndsWith("Foo"))
    {
        var o = methodInfo.IsStatic ? null : new Test();
        methodInfo.Invoke(o, new Il2CppReferenceArray<Object>([1, 2]));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants