BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
195 stars 61 forks source link

Il2CppInteropUtils.GetFieldInfoFromMethod will fail for any generic type's field #80

Closed noita-player closed 1 year ago

noita-player commented 1 year ago

System.Reflection.ResolveField isn't valid for fields of generic types, despite the same metadata token returning the same field for each generic type used. It will raise System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)

https://github.com/BepInEx/Il2CppInterop/blob/126c5d07093b0b1caa6ae7162cae5923ed09e4be/Il2CppInterop.Common/Il2CppInteropUtils.cs#L8 image

Not sure how to fix, maybe pass generic specification type from methodbase if it exists? Problem occurs if anyone uses this to hook method of a generic - [HarmonyPatch(typeof(SomeGenericClass<>), "SomeNormalMethod")] - same behavior if you have generic type specifier included as well. Maybe can error on the former gracefully, and handle the latter.

Hacky change for my use that specifies the generics I know of works, but can't PR that - methodModule.ResolveField((int)opArg, new Type[] { typeof(SomeClassTypeSpecifierForThatGeneric) }, new Type[] { });

Kasuromi commented 1 year ago

Fixed in commit https://github.com/BepInEx/Il2CppInterop/commit/24f29f66552fa441f150b5585ec8a95cc313e929