Conversation
51af7b5 to
6eeeb3c
Compare
js6pak
left a comment
There was a problem hiding this comment.
I have tested these changes
Have you? As is the PR had no chance of working at all.
| var parameterType = parameter.ParameterType; | ||
| if (!IsTypeSupported(parameterType)) | ||
| if (!IsTypeSupported(parameterType) || | ||
| method.IsStatic && parameterType.IsGenericType && parameterType.ContainsGenericParameters) |
There was a problem hiding this comment.
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.
|
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]));
}
} |
|
@limoka can you rebase? |
6eeeb3c to
b13fc2c
Compare
|
@ds5678 rebased as you asked. Sorry it took a while, I was busy with IRL things. |
| var parameterType = parameter.ParameterType; | ||
| if (!IsTypeSupported(parameterType)) | ||
| if (!IsTypeSupported(parameterType) || | ||
| method.IsStatic && parameterType.IsGenericType && parameterType.ContainsGenericParameters) |
There was a problem hiding this comment.
Why does it matter here if the method is static or not?
There was a problem hiding this comment.
I mean Il2cpp Interop supports normal methods that are generic. If I don't include static part, those won't work.
This check is here so that only non generic static methods are included.
Most likely I didn't want to bother supporting generic static methods.
There was a problem hiding this comment.
Do we support generic instance methods?
| body.EmitCalli(OpCodes.Calli, CallingConvention.Cdecl, monoMethod.ReturnType.NativeType(), | ||
| new[] { typeof(IntPtr) }.Concat(monoMethod.GetParameters().Select(it => it.ParameterType.NativeType())) | ||
| .ToArray()); | ||
| body.Emit(OpCodes.Ldarg_1); // methodMetadata |
There was a problem hiding this comment.
On the surface, this looks like it will break instance methods. However, I saw this elsewhere:
nativeParameterTypes.Add(typeof(Il2CppMethodInfo*));so I think this line is actually fine.
| var parameterType = parameter.ParameterType; | ||
| if (!IsTypeSupported(parameterType)) | ||
| if (!IsTypeSupported(parameterType) || | ||
| method.IsStatic && parameterType.IsGenericType && parameterType.ContainsGenericParameters) |
There was a problem hiding this comment.
Do we support generic instance methods?
| if (!monoMethod.IsStatic) | ||
| nativeParameterTypes.Add(typeof(IntPtr)); | ||
| nativeParameterTypes.AddRange(monoMethod.GetParameters().Select(it => it.ParameterType.NativeType())); | ||
| nativeParameterTypes.Add(typeof(Il2CppMethodInfo*)); |
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.