BepInEx / HarmonyX

Harmony built on top of MonoMod.RuntimeDetours with additional features
MIT License
356 stars 44 forks source link

Does this work with il2cpp on android? #34

Closed friuns2 closed 2 years ago

friuns2 commented 2 years ago

Hello, does Method patching work with il2cpp on android? About page a bit unclear about it.

ghorsington commented 2 years ago

It is indeed unclear about it because it was never tested. Technically MonoMod (the backend we use for patching) supports ARM targets, so it could be possible.

For Il2Cpp support, you'll need to implement a method patcher backend as there isn't one included for Il2Cpp.

friuns2 commented 2 years ago

Hi Thanks i could test it, but I can't find BepInEx.IL2CPP.Hook.IL2CPPDetourMethodPatcher, i downloaded BepInEx.dll from release page but still can't find the namespace. 

ghorsington commented 2 years ago

BepInEx.dll is used in stable BepInEx 5 builds which does not provide Il2Cpp support. Instead, that class is part of BepInEx Bleeding Edge builds in BepInEx.IL2CPP.dll.

In any case, you might want to instead take the source code itself and modify it to your needs. BepInEx is a general-purpose modding framework and it automatically handles registering this patch backend with HarmonyX. If you don't use BepInEx, you're likely better off taking the source of the method patcher and editing it to your needs. Mind you that the Il2Cpp method patcher used in BepInEx relies on Il2CppUnhollower for its functionality (which in turn only supports x86 processors to my knowledge).

friuns2 commented 2 years ago

What do you mean by x86 will it work on android arm7 or arm64? Also i see IL2CPPDetourMethodPatcher.cs have il.Emit() which cannot run on il2cpp platform, will method detour work without il.emit? like swap pointers in vTable?  

For example this library doing hooks without il.Emit but author stated "android il2cpp needs to add a so to call the mprotect method, which is not going to be done now." https://github.com/Misaka-Mikoto-Tech/MonoHook/

I would like to run method detour inside game to replace methods with nlua at runtime

ghorsington commented 2 years ago

x86 is CPU architechture just like ARM. Il2CppUnhollower has been primarily made to work with x86, and I am sure the current upstream version does not support other ARM architectures.

Also i see IL2CPPDetourMethodPatcher.cs have il.Emit() which cannot run on il2cpp platform

Yes, you would need to bootstrap mono in the game manually to use System.Reflection.Emit, MonoMod, HarmonyX or any other managed DLLs. For example, this is what is done in BepInEx on PC games, and a similar approach would have to be used on Android as well. Mono itself is known to work on Android, but I personally never tried it.

Depending on what exactly you want to achieve (e.g. modding an existing game or adding modding support to your own game), you likely might need to edit the APK itself to load some code of yours into the game. After that, you can bootstrap whatever runtime you want (be it mono, Lua or something else) and start detouring. This, however, is beyond the scope of me and this repo, and you might find better information elsewhere on the internet.

You might be able to find some information on MelonLoader for Android repo and its wiki on how to bootstrap mono in an APK. From my understanding, it's unstable, but there is basic support for ARM64-v8a.

ghorsington commented 2 years ago

Closing as resolved, since the main question was about Il2Cpp. Moreover, HarmonyX is not usable in the case OP needed (HarmonyX compiled with Il2Cpp) as HarmonyX was intended to be instead used via mono runtime or CoreCLR injected into Il2Cpp (e.g. BepInEx).